kwallet Library API Documentation

kwallet.cc

00001 /* This file is part of the KDE project
00002  *
00003  * Copyright (C) 2002-2004 George Staikos <staikos@kde.org>
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Library General Public License
00016  * along with this library; see the file COPYING.LIB.  If not, write to
00017  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018  * Boston, MA 02111-1307, USA.
00019  */
00020 
00021 #include "kwallettypes.h"
00022 #include "kwallet.h"
00023 #include <kconfig.h>
00024 #include <kdebug.h>
00025 #include <kdeversion.h>
00026 #include <dcopclient.h>
00027 #include <dcopref.h>
00028 #include <qpopupmenu.h>
00029 #include <qapplication.h>
00030 
00031 #include <assert.h>
00032 
00033 using namespace KWallet;
00034 
00035 
00036 const QString Wallet::LocalWallet() {
00037     KConfig cfg("kwalletrc");
00038     cfg.setGroup("Wallet");
00039     if (!cfg.readBoolEntry("Use One Wallet", true)) {
00040         return cfg.readEntry("Local Wallet", "localwallet");
00041     }
00042 
00043     return cfg.readEntry("Default Wallet", "kdewallet");
00044 }
00045 
00046 const QString Wallet::NetworkWallet() {
00047     KConfig cfg("kwalletrc");
00048     cfg.setGroup("Wallet");
00049 
00050     return cfg.readEntry("Default Wallet", "kdewallet");
00051 }
00052 
00053 const QString Wallet::PasswordFolder() {
00054     return "Passwords";
00055 }
00056 
00057 const QString Wallet::FormDataFolder() {
00058     return "Form Data";
00059 }
00060 
00061 
00062 
00063 Wallet::Wallet(int handle, const QString& name)
00064 : QObject(0L), DCOPObject(), d(0L), _name(name), _handle(handle) {
00065 
00066     _dcopRef = new DCOPRef("kded", "kwalletd");
00067 
00068     _dcopRef->dcopClient()->setNotifications(true);
00069     connect(_dcopRef->dcopClient(),
00070             SIGNAL(applicationRemoved(const QCString&)),
00071             this,
00072             SLOT(slotAppUnregistered(const QCString&)));
00073 
00074     connectDCOPSignal(_dcopRef->app(), _dcopRef->obj(), "walletClosed(int)", "slotWalletClosed(int)", false);
00075     connectDCOPSignal(_dcopRef->app(), _dcopRef->obj(), "folderListUpdated(QString)", "slotFolderListUpdated(QString)", false);
00076     connectDCOPSignal(_dcopRef->app(), _dcopRef->obj(), "folderUpdated(QString, QString)", "slotFolderUpdated(QString, QString)", false);
00077     connectDCOPSignal(_dcopRef->app(), _dcopRef->obj(), "applicationDisconnected(QString, QCString)", "slotApplicationDisconnected(QString, QCString)", false);
00078 
00079     // Verify that the wallet is still open
00080     if (_handle != -1) {
00081         DCOPReply r = _dcopRef->call("isOpen", _handle);
00082         if (r.isValid()) {
00083             bool rc = false;
00084             r.get(rc);
00085             if (!rc) {
00086                 _handle = -1;
00087                 _name = QString::null;
00088             }
00089         }
00090     }
00091 }
00092 
00093 
00094 Wallet::~Wallet() {
00095     if (_handle != -1) {
00096         _dcopRef->call("close", _handle, false);
00097         _handle = -1;
00098         _folder = QString::null;
00099         _name = QString::null;
00100     }
00101 
00102     delete _dcopRef;
00103     _dcopRef = 0L;
00104 }
00105 
00106 
00107 QStringList Wallet::walletList() {
00108     DCOPReply r = DCOPRef("kded", "kwalletd").call("wallets");
00109     QStringList rc;
00110     if (r.isValid()) {
00111         r.get(rc);
00112     }
00113     return rc;
00114 }
00115 
00116 
00117 void Wallet::changePassword(const QString& name, WId w) {
00118     DCOPRef("kded", "kwalletd").send("changePassword", name, uint(w));
00119 }
00120 
00121 
00122 bool Wallet::isEnabled() {
00123     DCOPReply r = DCOPRef("kded", "kwalletd").call("isEnabled");
00124     bool rc = false;
00125     if (r.isValid()) {
00126         r.get(rc);
00127     }
00128     return rc;
00129 }
00130 
00131 
00132 bool Wallet::isOpen(const QString& name) {
00133     DCOPReply r = DCOPRef("kded", "kwalletd").call("isOpen", name);
00134     bool rc = false;
00135     if (r.isValid()) {
00136         r.get(rc);
00137     }
00138     return rc;
00139 }
00140 
00141 
00142 int Wallet::closeWallet(const QString& name, bool force) {
00143     DCOPReply r = DCOPRef("kded", "kwalletd").call("close", name, force);
00144     int rc = -1;
00145     if (r.isValid()) {
00146         r.get(rc);
00147     }
00148     return rc;
00149 }
00150 
00151 
00152 int Wallet::deleteWallet(const QString& name) {
00153     DCOPReply r = DCOPRef("kded", "kwalletd").call("deleteWallet", name);
00154     int rc = -1;
00155     if (r.isValid()) {
00156         r.get(rc);
00157     }
00158     return rc;
00159 }
00160 
00161 
00162 Wallet *Wallet::openWallet(const QString& name, WId w, OpenType ot) {
00163     if (ot == Asynchronous) {
00164         Wallet *wallet = new Wallet(-1, name);
00165         DCOPRef("kded", "kwalletd").send("openAsynchronous", name, wallet->objId(), uint(w));
00166         return wallet;
00167     }
00168 
00169         // avoid deadlock if the app has some popup open (#65978/#71048)
00170         while( QWidget* widget = qApp->activePopupWidget())
00171             widget->close();
00172 
00173     bool isPath = ot == Path;
00174     DCOPReply r;
00175 
00176     if (isPath) {
00177         r = DCOPRef("kded", "kwalletd").call("openPath", name, uint(w));
00178     } else {
00179         r = DCOPRef("kded", "kwalletd").call("open", name, uint(w));
00180     }
00181 
00182     if (r.isValid()) {
00183         int drc = -1;
00184         r.get(drc);
00185         if (drc != -1) {
00186             return new Wallet(drc, name);
00187         }
00188     }
00189 
00190     return 0;
00191 }
00192 
00193 
00194 bool Wallet::disconnectApplication(const QString& wallet, const QCString& app) {
00195     DCOPReply r = DCOPRef("kded", "kwalletd").call("disconnectApplication", wallet, app);
00196     bool rc = false;
00197     if (r.isValid()) {
00198         r.get(rc);
00199     }
00200     return rc;
00201 }
00202 
00203 
00204 QStringList Wallet::users(const QString& name) {
00205     DCOPReply r = DCOPRef("kded", "kwalletd").call("users", name);
00206     QStringList drc;
00207     if (r.isValid()) {
00208         r.get(drc);
00209     }
00210     return drc;
00211 }
00212 
00213 
00214 int Wallet::sync() {
00215     if (_handle == -1) {
00216         return -1;
00217     }
00218 
00219     _dcopRef->call("sync", _handle);
00220     return 0;
00221 }
00222 
00223 
00224 int Wallet::lockWallet() {
00225     if (_handle == -1) {
00226         return -1;
00227     }
00228 
00229     DCOPReply r = _dcopRef->call("close", _handle, true);
00230     _handle = -1;
00231     _folder = QString::null;
00232     _name = QString::null;
00233     if (r.isValid()) {
00234         int drc = -1;
00235         r.get(drc);
00236         return drc;
00237     }
00238     return -1;
00239 }
00240 
00241 
00242 const QString& Wallet::walletName() const {
00243     return _name;
00244 }
00245 
00246 
00247 bool Wallet::isOpen() const {
00248     return _handle != -1;
00249 }
00250 
00251 
00252 void Wallet::requestChangePassword(WId w) {
00253     if (_handle == -1) {
00254         return;
00255     }
00256 
00257     _dcopRef->send("changePassword", _name, uint(w));
00258 }
00259 
00260 
00261 void Wallet::slotWalletClosed(int handle) {
00262     if (_handle == handle) {
00263         _handle = -1;
00264         _folder = QString::null;
00265         _name = QString::null;
00266         emit walletClosed();
00267     }
00268 }
00269 
00270 
00271 QStringList Wallet::folderList() {
00272     QStringList rc;
00273 
00274     if (_handle == -1) {
00275         return rc;
00276     }
00277 
00278     DCOPReply r = _dcopRef->call("folderList", _handle);
00279     if (r.isValid()) {
00280         r.get(rc);
00281     }
00282 
00283     return rc;
00284 }
00285 
00286 
00287 QStringList Wallet::entryList() {
00288     QStringList rc;
00289 
00290     if (_handle == -1) {
00291         return rc;
00292     }
00293 
00294     DCOPReply r = _dcopRef->call("entryList", _handle, _folder);
00295     if (r.isValid()) {
00296         r.get(rc);
00297     }
00298 
00299     return rc;
00300 }
00301 
00302 
00303 bool Wallet::hasFolder(const QString& f) {
00304     bool rc = false;
00305 
00306     if (_handle == -1) {
00307         return rc;
00308     }
00309 
00310     DCOPReply r = _dcopRef->call("hasFolder", _handle, f);
00311     if (r.isValid()) {
00312         r.get(rc);
00313     }
00314 
00315     return rc;
00316 }
00317 
00318 
00319 bool Wallet::createFolder(const QString& f) {
00320     bool rc = true;
00321 
00322     if (_handle == -1) {
00323         return false;
00324     }
00325 
00326     if (!hasFolder(f)) {
00327         DCOPReply r = _dcopRef->call("createFolder", _handle, f);
00328         if (r.isValid()) {
00329             r.get(rc);
00330         }
00331     }
00332 
00333     return rc;
00334 }
00335 
00336 
00337 bool Wallet::setFolder(const QString& f) {
00338     bool rc = false;
00339 
00340     if (_handle == -1) {
00341         return rc;
00342     }
00343 
00344     // Don't do this - the folder could have disappeared?
00345 #if 0
00346     if (f == _folder) {
00347         return true;
00348     }
00349 #endif
00350 
00351     if (hasFolder(f)) {
00352         _folder = f;
00353         rc = true;
00354     }
00355 
00356     return rc;
00357 }
00358 
00359 
00360 bool Wallet::removeFolder(const QString& f) {
00361     bool rc = false;
00362 
00363     if (_handle == -1) {
00364         return rc;
00365     }
00366 
00367     DCOPReply r = _dcopRef->call("removeFolder", _handle, f);
00368     if (r.isValid()) {
00369         r.get(rc);
00370     }
00371 
00372     if (_folder == f) {
00373         setFolder(QString::null);
00374     }
00375 
00376     return rc;
00377 }
00378 
00379 
00380 const QString& Wallet::currentFolder() const {
00381     return _folder;
00382 }
00383 
00384 
00385 int Wallet::readEntry(const QString& key, QByteArray& value) {
00386     int rc = -1;
00387 
00388     if (_handle == -1) {
00389         return rc;
00390     }
00391 
00392     DCOPReply r = _dcopRef->call("readEntry", _handle, _folder, key);
00393     if (r.isValid()) {
00394         r.get(value);
00395         rc = 0;
00396     }
00397 
00398     return rc;
00399 }
00400 
00401 
00402 int Wallet::readEntryList(const QString& key, QMap<QString, QByteArray>& value) {
00403     int rc = -1;
00404 
00405     if (_handle == -1) {
00406         return rc;
00407     }
00408 
00409     DCOPReply r = _dcopRef->call("readEntryList", _handle, _folder, key);
00410     if (r.isValid()) {
00411         r.get(value);
00412         rc = 0;
00413     }
00414 
00415     return rc;
00416 }
00417 
00418 
00419 int Wallet::renameEntry(const QString& oldName, const QString& newName) {
00420     int rc = -1;
00421 
00422     if (_handle == -1) {
00423         return rc;
00424     }
00425 
00426     DCOPReply r = _dcopRef->call("renameEntry", _handle, _folder, oldName, newName);
00427     if (r.isValid()) {
00428         r.get(rc);
00429     }
00430 
00431     return rc;
00432 }
00433 
00434 
00435 int Wallet::readMap(const QString& key, QMap<QString,QString>& value) {
00436     int rc = -1;
00437 
00438     if (_handle == -1) {
00439         return rc;
00440     }
00441 
00442     DCOPReply r = _dcopRef->call("readMap", _handle, _folder, key);
00443     if (r.isValid()) {
00444         QByteArray v;
00445         r.get(v);
00446         if (!v.isEmpty()) {
00447             QDataStream ds(v, IO_ReadOnly);
00448             ds >> value;
00449         }
00450         rc = 0;
00451     }
00452 
00453     return rc;
00454 }
00455 
00456 
00457 int Wallet::readMapList(const QString& key, QMap<QString, QMap<QString, QString> >& value) {
00458     int rc = -1;
00459 
00460     if (_handle == -1) {
00461         return rc;
00462     }
00463 
00464     DCOPReply r = _dcopRef->call("readMapList", _handle, _folder, key);
00465     if (r.isValid()) {
00466         QMap<QString,QByteArray> unparsed;
00467         r.get(unparsed);
00468         for (QMap<QString,QByteArray>::ConstIterator i = unparsed.begin(); i != unparsed.end(); ++i) {
00469             if (!i.data().isEmpty()) {
00470                 QDataStream ds(i.data(), IO_ReadOnly);
00471                 QMap<QString,QString> v;
00472                 ds >> v;
00473                 value.insert(i.key(), v);
00474             }
00475         }
00476         rc = 0;
00477     }
00478 
00479     return rc;
00480 }
00481 
00482 
00483 int Wallet::readPassword(const QString& key, QString& value) {
00484     int rc = -1;
00485 
00486     if (_handle == -1) {
00487         return rc;
00488     }
00489 
00490     DCOPReply r = _dcopRef->call("readPassword", _handle, _folder, key);
00491     if (r.isValid()) {
00492         r.get(value);
00493         rc = 0;
00494     }
00495 
00496     return rc;
00497 }
00498 
00499 
00500 int Wallet::readPasswordList(const QString& key, QMap<QString, QString>& value) {
00501     int rc = -1;
00502 
00503     if (_handle == -1) {
00504         return rc;
00505     }
00506 
00507     DCOPReply r = _dcopRef->call("readPasswordList", _handle, _folder, key);
00508     if (r.isValid()) {
00509         r.get(value);
00510         rc = 0;
00511     }
00512 
00513     return rc;
00514 }
00515 
00516 
00517 int Wallet::writeEntry(const QString& key, const QByteArray& value, EntryType entryType) {
00518     int rc = -1;
00519 
00520     if (_handle == -1) {
00521         return rc;
00522     }
00523 
00524     DCOPReply r = _dcopRef->call("writeEntry", _handle, _folder, key, value, int(entryType));
00525     if (r.isValid()) {
00526         r.get(rc);
00527     }
00528 
00529     return rc;
00530 }
00531 
00532 
00533 int Wallet::writeEntry(const QString& key, const QByteArray& value) {
00534     int rc = -1;
00535 
00536     if (_handle == -1) {
00537         return rc;
00538     }
00539 
00540     DCOPReply r = _dcopRef->call("writeEntry", _handle, _folder, key, value);
00541     if (r.isValid()) {
00542         r.get(rc);
00543     }
00544 
00545     return rc;
00546 }
00547 
00548 
00549 int Wallet::writeMap(const QString& key, const QMap<QString,QString>& value) {
00550     int rc = -1;
00551 
00552     if (_handle == -1) {
00553         return rc;
00554     }
00555 
00556     QByteArray a;
00557     QDataStream ds(a, IO_WriteOnly);
00558     ds << value;
00559     DCOPReply r = _dcopRef->call("writeMap", _handle, _folder, key, a);
00560     if (r.isValid()) {
00561         r.get(rc);
00562     }
00563 
00564     return rc;
00565 }
00566 
00567 
00568 int Wallet::writePassword(const QString& key, const QString& value) {
00569     int rc = -1;
00570 
00571     if (_handle == -1) {
00572         return rc;
00573     }
00574 
00575     DCOPReply r = _dcopRef->call("writePassword", _handle, _folder, key, value);
00576     if (r.isValid()) {
00577         r.get(rc);
00578     }
00579 
00580     return rc;
00581 }
00582 
00583 
00584 bool Wallet::hasEntry(const QString& key) {
00585     bool rc = false;
00586 
00587     if (_handle == -1) {
00588         return rc;
00589     }
00590 
00591     DCOPReply r = _dcopRef->call("hasEntry", _handle, _folder, key);
00592     if (r.isValid()) {
00593         r.get(rc);
00594     }
00595 
00596     return rc;
00597 }
00598 
00599 
00600 int Wallet::removeEntry(const QString& key) {
00601     int rc = -1;
00602 
00603     if (_handle == -1) {
00604         return rc;
00605     }
00606 
00607     DCOPReply r = _dcopRef->call("removeEntry", _handle, _folder, key);
00608     if (r.isValid()) {
00609         r.get(rc);
00610     }
00611 
00612     return rc;
00613 }
00614 
00615 
00616 Wallet::EntryType Wallet::entryType(const QString& key) {
00617     int rc = 0;
00618 
00619     if (_handle == -1) {
00620         return Wallet::Unknown;
00621     }
00622 
00623     DCOPReply r = _dcopRef->call("entryType", _handle, _folder, key);
00624     if (r.isValid()) {
00625         r.get(rc);
00626     }
00627 
00628     return static_cast<EntryType>(rc);
00629 }
00630 
00631 
00632 void Wallet::slotAppUnregistered(const QCString& app) {
00633     if (_handle >= 0 && app == "kded") {
00634         slotWalletClosed(_handle);
00635     }
00636 }
00637 
00638 
00639 void Wallet::slotFolderUpdated(const QString& wallet, const QString& folder) {
00640     if (_name == wallet) {
00641         emit folderUpdated(folder);
00642     }
00643 }
00644 
00645 
00646 void Wallet::slotFolderListUpdated(const QString& wallet) {
00647     if (_name == wallet) {
00648         emit folderListUpdated();
00649     }
00650 }
00651 
00652 
00653 void Wallet::slotApplicationDisconnected(const QString& wallet, const QCString& application) {
00654     if (_handle >= 0
00655             && _name == wallet
00656             && application == _dcopRef->dcopClient()->appId()) {
00657         slotWalletClosed(_handle);
00658     }
00659 }
00660 
00661 
00662 void Wallet::walletOpenResult(int id) {
00663     if (_handle != -1) {
00664         // This is BAD.
00665         return;
00666     }
00667 
00668     if (id > 0) {
00669         _handle = id;
00670         emit walletOpened(true);
00671     } else if (id < 0) {
00672         emit walletOpened(false);
00673     } // id == 0 => wait
00674 }
00675 
00676 
00677 bool Wallet::folderDoesNotExist(const QString& wallet, const QString& folder) {
00678 DCOPReply r = DCOPRef("kded", "kwalletd").call("folderDoesNotExist", wallet, folder);
00679 bool rc = true;
00680     if (r.isValid()) {
00681         r.get(rc);
00682     }
00683 return rc;
00684 }
00685 
00686 
00687 bool Wallet::keyDoesNotExist(const QString& wallet, const QString& folder, const QString& key) {
00688 DCOPReply r = DCOPRef("kded", "kwalletd").call("keyDoesNotExist", wallet, folder, key);
00689 bool rc = true;
00690     if (r.isValid()) {
00691         r.get(rc);
00692     }
00693 return rc;
00694 }
00695 
00696 
00697 void Wallet::virtual_hook(int, void*) {
00698     //BASE::virtual_hook( id, data );
00699 }
00700 
00701 #include "kwallet.moc"
KDE Logo
This file is part of the documentation for kwallet Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Jan 23 19:33:07 2006 by doxygen 1.4.3 written by Dimitri van Heesch, © 1997-2003