Exemple #1
0
static bool storeInWallet( KWallet::Wallet* wallet, const QString& key, const KIO::AuthInfo &info )
{
    if ( !wallet->hasFolder( KWallet::Wallet::PasswordFolder() ) )
        if ( !wallet->createFolder( KWallet::Wallet::PasswordFolder() ) )
            return false;
    wallet->setFolder( KWallet::Wallet::PasswordFolder() );
    // Before saving, check if there's already an entry with this login.
    // If so, replace it (with the new password). Otherwise, add a new entry.
    typedef QMap<QString,QString> Map;
    int entryNumber = 1;
    Map map;
    QString walletKey = makeWalletKey( key, info.realmValue );
    kdDebug(130) << "storeInWallet: walletKey=" << walletKey << "  reading existing map" << endl;
    if ( wallet->readMap( walletKey, map ) == 0 ) {
        Map::ConstIterator end = map.end();
        Map::ConstIterator it = map.find( "login" );
        while ( it != end ) {
            if ( it.data() == info.username ) {
                break; // OK, overwrite this entry
            }
            it = map.find( QString( "login-" ) + QString::number( ++entryNumber ) );
        }
        // If no entry was found, create a new entry - entryNumber is set already.
    }
    const QString loginKey = makeMapKey( "login", entryNumber );
    const QString passwordKey = makeMapKey( "password", entryNumber );
    kdDebug(130) << "storeInWallet: writing to " << loginKey << "," << passwordKey << endl;
    // note the overwrite=true by default
    map.insert( loginKey, info.username );
    map.insert( passwordKey, info.password );
    wallet->writeMap( walletKey, map );
    return true;
}
Exemple #2
0
static bool readFromWallet(KWallet::Wallet *wallet, const QString &key, const QString &realm, QString &username, QString &password, bool userReadOnly,
                           QMap< QString, QString > &knownLogins)
{
    // kdDebug(130) << "readFromWallet: key=" << key << " username="******" password="******" userReadOnly=" << userReadOnly
    // << " realm=" << realm << endl;
    if(wallet->hasFolder(KWallet::Wallet::PasswordFolder()))
    {
        wallet->setFolder(KWallet::Wallet::PasswordFolder());

        QMap< QString, QString > map;
        if(wallet->readMap(makeWalletKey(key, realm), map) == 0)
        {
            typedef QMap< QString, QString > Map;
            int entryNumber = 1;
            Map::ConstIterator end = map.end();
            Map::ConstIterator it = map.find("login");
            while(it != end)
            {
                // kdDebug(130) << "readFromWallet: found " << it.key() << "=" << it.data() << endl;
                Map::ConstIterator pwdIter = map.find(makeMapKey("password", entryNumber));
                if(pwdIter != end)
                {
                    if(it.data() == username)
                        password = pwdIter.data();
                    knownLogins.insert(it.data(), pwdIter.data());
                }

                it = map.find(QString("login-") + QString::number(++entryNumber));
            }
            // kdDebug(130) << knownLogins.count() << " known logins" << endl;

            if(!userReadOnly && !knownLogins.isEmpty() && username.isEmpty())
            {
                // Pick one, any one...
                username = knownLogins.begin().key();
                password = knownLogins.begin().data();
                // kdDebug(130) << "readFromWallet: picked the first one : " << username << endl;
            }

            return true;
        }
    }
    return false;
}