KDEUI
kwallet.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE project 00002 * 00003 * Copyright (C) 2002-2004 George Staikos <staikos@kde.org> 00004 * Copyright (C) 2008 Michael Leupold <lemma@confuego.org> 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Library General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Library General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Library General Public License 00017 * along with this library; see the file COPYING.LIB. If not, write to 00018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 00020 */ 00021 00022 #include "kwallet.h" 00023 #include <ksharedconfig.h> 00024 #include <kdebug.h> 00025 #include <kdeversion.h> 00026 #include <QtGui/QApplication> 00027 #include <QtCore/QPointer> 00028 #include <QtGui/QWidget> 00029 #include <QtDBus/QtDBus> 00030 #include <ktoolinvocation.h> 00031 00032 #include <assert.h> 00033 #include <kglobal.h> 00034 #include <kcomponentdata.h> 00035 #include <kaboutdata.h> 00036 #include <kconfiggroup.h> 00037 #include <kwindowsystem.h> 00038 00039 #include "kwallet_interface.h" 00040 00041 using namespace KWallet; 00042 00043 typedef QMap<QString, QString> StringStringMap; 00044 Q_DECLARE_METATYPE(StringStringMap) 00045 typedef QMap<QString, StringStringMap> StringToStringStringMapMap; 00046 Q_DECLARE_METATYPE(StringToStringStringMapMap) 00047 typedef QMap<QString, QByteArray> StringByteArrayMap; 00048 Q_DECLARE_METATYPE(StringByteArrayMap) 00049 00050 static QString appid() 00051 { 00052 if (KGlobal::hasMainComponent()) { 00053 KComponentData cData = KGlobal::mainComponent(); 00054 if (cData.isValid()) { 00055 const KAboutData* aboutData = cData.aboutData(); 00056 if (aboutData) { 00057 return aboutData->programName(); 00058 } 00059 return cData.componentName(); 00060 } 00061 } 00062 return qApp->applicationName(); 00063 } 00064 00065 static void registerTypes() 00066 { 00067 static bool registered = false; 00068 if (!registered) { 00069 qDBusRegisterMetaType<StringStringMap>(); 00070 qDBusRegisterMetaType<StringToStringStringMapMap>(); 00071 qDBusRegisterMetaType<StringByteArrayMap>(); 00072 registered = true; 00073 } 00074 } 00075 00076 const QString Wallet::LocalWallet() { 00077 KConfigGroup cfg(KSharedConfig::openConfig("kwalletrc")->group("Wallet")); 00078 if (!cfg.readEntry("Use One Wallet", true)) { 00079 QString tmp = cfg.readEntry("Local Wallet", "localwallet"); 00080 if (tmp.isEmpty()) { 00081 return "localwallet"; 00082 } 00083 return tmp; 00084 } 00085 00086 QString tmp = cfg.readEntry("Default Wallet", "kdewallet"); 00087 if (tmp.isEmpty()) { 00088 return "kdewallet"; 00089 } 00090 return tmp; 00091 } 00092 00093 const QString Wallet::NetworkWallet() { 00094 KConfigGroup cfg(KSharedConfig::openConfig("kwalletrc")->group("Wallet")); 00095 00096 QString tmp = cfg.readEntry("Default Wallet", "kdewallet"); 00097 if (tmp.isEmpty()) { 00098 return "kdewallet"; 00099 } 00100 return tmp; 00101 } 00102 00103 const QString Wallet::PasswordFolder() { 00104 return "Passwords"; 00105 } 00106 00107 const QString Wallet::FormDataFolder() { 00108 return "Form Data"; 00109 } 00110 00111 class Wallet::WalletPrivate 00112 { 00113 public: 00114 WalletPrivate(Wallet *wallet, int h, const QString &n) 00115 : q(wallet), name(n), handle(h) 00116 {} 00117 00118 void walletServiceUnregistered(); 00119 00120 Wallet *q; 00121 QString name; 00122 QString folder; 00123 int handle; 00124 int transactionId; 00125 QPointer<QEventLoop> loop; 00126 }; 00127 00128 class KWalletDLauncher 00129 { 00130 public: 00131 KWalletDLauncher(); 00132 ~KWalletDLauncher(); 00133 org::kde::KWallet &getInterface(); 00134 private: 00135 org::kde::KWallet m_wallet; 00136 KConfigGroup m_cgroup; 00137 }; 00138 00139 K_GLOBAL_STATIC(KWalletDLauncher, walletLauncher) 00140 00141 static const char s_kwalletdServiceName[] = "org.kde.kwalletd"; 00142 00143 Wallet::Wallet(int handle, const QString& name) 00144 : QObject(0L), d(new WalletPrivate(this, handle, name)) 00145 { 00146 QDBusServiceWatcher *watcher = new QDBusServiceWatcher(QString::fromLatin1(s_kwalletdServiceName), QDBusConnection::sessionBus(), 00147 QDBusServiceWatcher::WatchForUnregistration, this); 00148 connect(watcher, SIGNAL(serviceUnregistered(QString)), 00149 this, SLOT(walletServiceUnregistered())); 00150 00151 connect(&walletLauncher->getInterface(), SIGNAL(walletClosed(int)), SLOT(slotWalletClosed(int))); 00152 connect(&walletLauncher->getInterface(), SIGNAL(folderListUpdated(QString)), SLOT(slotFolderListUpdated(QString))); 00153 connect(&walletLauncher->getInterface(), SIGNAL(folderUpdated(QString,QString)), SLOT(slotFolderUpdated(QString, QString))); 00154 connect(&walletLauncher->getInterface(), SIGNAL(applicationDisconnected(QString, QString)), SLOT(slotApplicationDisconnected(QString, QString))); 00155 00156 // Verify that the wallet is still open 00157 if (d->handle != -1) { 00158 QDBusReply<bool> r = walletLauncher->getInterface().isOpen(d->handle); 00159 if (r.isValid() && !r) { 00160 d->handle = -1; 00161 d->name.clear(); 00162 } 00163 } 00164 } 00165 00166 00167 Wallet::~Wallet() { 00168 if (d->handle != -1) { 00169 if (!walletLauncher.isDestroyed()) { 00170 walletLauncher->getInterface().close(d->handle, false, appid()); 00171 } else { 00172 kDebug(285) << "Problem with static destruction sequence." 00173 "Destroy any static Wallet before the event-loop exits."; 00174 } 00175 d->handle = -1; 00176 d->folder.clear(); 00177 d->name.clear(); 00178 } 00179 delete d; 00180 } 00181 00182 00183 QStringList Wallet::walletList() { 00184 QDBusReply<QStringList> r = walletLauncher->getInterface().wallets(); 00185 00186 if (!r.isValid()) 00187 { 00188 kDebug(285) << "Invalid DBus reply: " << r.error(); 00189 return QStringList(); 00190 } 00191 else 00192 return r; 00193 } 00194 00195 00196 void Wallet::changePassword(const QString& name, WId w) { 00197 if( w == 0 ) 00198 kDebug(285) << "Pass a valid window to KWallet::Wallet::changePassword()."; 00199 00200 // Make sure the password prompt window will be visible and activated 00201 KWindowSystem::allowExternalProcessWindowActivation(); 00202 00203 walletLauncher->getInterface().changePassword(name, (qlonglong)w, appid()); 00204 } 00205 00206 00207 bool Wallet::isEnabled() { 00208 QDBusReply<bool> r = walletLauncher->getInterface().isEnabled(); 00209 00210 if (!r.isValid()) 00211 { 00212 kDebug(285) << "Invalid DBus reply: " << r.error(); 00213 return false; 00214 } 00215 else 00216 return r; 00217 } 00218 00219 00220 bool Wallet::isOpen(const QString& name) { 00221 QDBusReply<bool> r = walletLauncher->getInterface().isOpen(name); 00222 00223 if (!r.isValid()) 00224 { 00225 kDebug(285) << "Invalid DBus reply: " << r.error(); 00226 return false; 00227 } 00228 else 00229 return r; 00230 } 00231 00232 int Wallet::closeWallet(const QString& name, bool force) { 00233 QDBusReply<int> r = walletLauncher->getInterface().close(name, force); 00234 00235 if (!r.isValid()) 00236 { 00237 kDebug(285) << "Invalid DBus reply: " << r.error(); 00238 return -1; 00239 } 00240 else 00241 return r; 00242 } 00243 00244 00245 int Wallet::deleteWallet(const QString& name) { 00246 QDBusReply<int> r = walletLauncher->getInterface().deleteWallet(name); 00247 00248 if (!r.isValid()) 00249 { 00250 kDebug(285) << "Invalid DBus reply: " << r.error(); 00251 return -1; 00252 } 00253 else 00254 return r; 00255 } 00256 00257 Wallet *Wallet::openWallet(const QString& name, WId w, OpenType ot) { 00258 if( w == 0 ) 00259 kDebug(285) << "Pass a valid window to KWallet::Wallet::openWallet()."; 00260 00261 Wallet *wallet = new Wallet(-1, name); 00262 00263 // connect the daemon's opened signal to the slot filtering the 00264 // signals we need 00265 connect(&walletLauncher->getInterface(), SIGNAL(walletAsyncOpened(int, int)), 00266 wallet, SLOT(walletAsyncOpened(int, int))); 00267 00268 // Use an eventloop for synchronous calls 00269 QEventLoop loop; 00270 if (ot == Synchronous || ot == Path) { 00271 connect(wallet, SIGNAL(walletOpened(bool)), &loop, SLOT(quit())); 00272 } 00273 00274 // Make sure the password prompt window will be visible and activated 00275 KWindowSystem::allowExternalProcessWindowActivation(); 00276 00277 // do the call 00278 QDBusReply<int> r; 00279 if (ot == Synchronous || ot == Asynchronous) { 00280 r = walletLauncher->getInterface().openAsync(name, (qlonglong)w, appid(), true); 00281 } else if (ot == Path) { 00282 r = walletLauncher->getInterface().openPathAsync(name, (qlonglong)w, appid(), true); 00283 } else { 00284 delete wallet; 00285 return 0; 00286 } 00287 // error communicating with the daemon (maybe not running) 00288 if (!r.isValid()) { 00289 kDebug(285) << "Invalid DBus reply: " << r.error(); 00290 delete wallet; 00291 return 0; 00292 } 00293 wallet->d->transactionId = r.value(); 00294 00295 if (ot == Synchronous || ot == Path) { 00296 // check for an immediate error 00297 if (wallet->d->transactionId < 0) { 00298 delete wallet; 00299 wallet = 0; 00300 } else { 00301 // wait for the daemon's reply 00302 // store a pointer to the event loop so it can be quit in error case 00303 wallet->d->loop = &loop; 00304 loop.exec(); 00305 if (wallet->d->handle < 0) { 00306 delete wallet; 00307 return 0; 00308 } 00309 } 00310 } else if (ot == Asynchronous) { 00311 if (wallet->d->transactionId < 0) { 00312 QTimer::singleShot(0, wallet, SLOT(emitWalletAsyncOpenError())); 00313 // client code is responsible for deleting the wallet 00314 } 00315 } 00316 00317 return wallet; 00318 } 00319 00320 00321 bool Wallet::disconnectApplication(const QString& wallet, const QString& app) { 00322 QDBusReply<bool> r = walletLauncher->getInterface().disconnectApplication(wallet, app); 00323 00324 if (!r.isValid()) 00325 { 00326 kDebug(285) << "Invalid DBus reply: " << r.error(); 00327 return false; 00328 } 00329 else 00330 return r; 00331 } 00332 00333 00334 QStringList Wallet::users(const QString& name) { 00335 QDBusReply<QStringList> r = walletLauncher->getInterface().users(name); 00336 if (!r.isValid()) 00337 { 00338 kDebug(285) << "Invalid DBus reply: " << r.error(); 00339 return QStringList(); 00340 } 00341 else 00342 return r; 00343 } 00344 00345 00346 int Wallet::sync() { 00347 if (d->handle == -1) { 00348 return -1; 00349 } 00350 00351 walletLauncher->getInterface().sync(d->handle, appid()); 00352 return 0; 00353 } 00354 00355 00356 int Wallet::lockWallet() { 00357 if (d->handle == -1) { 00358 return -1; 00359 } 00360 00361 QDBusReply<int> r = walletLauncher->getInterface().close(d->handle, true, appid()); 00362 d->handle = -1; 00363 d->folder.clear(); 00364 d->name.clear(); 00365 if (r.isValid()) { 00366 return r; 00367 } 00368 else { 00369 kDebug(285) << "Invalid DBus reply: " << r.error(); 00370 return -1; 00371 } 00372 } 00373 00374 00375 const QString& Wallet::walletName() const { 00376 return d->name; 00377 } 00378 00379 00380 bool Wallet::isOpen() const { 00381 return d->handle != -1; 00382 } 00383 00384 00385 void Wallet::requestChangePassword(WId w) { 00386 if( w == 0 ) 00387 kDebug(285) << "Pass a valid window to KWallet::Wallet::requestChangePassword()."; 00388 if (d->handle == -1) { 00389 return; 00390 } 00391 00392 // Make sure the password prompt window will be visible and activated 00393 KWindowSystem::allowExternalProcessWindowActivation(); 00394 00395 walletLauncher->getInterface().changePassword(d->name, (qlonglong)w, appid()); 00396 } 00397 00398 00399 void Wallet::slotWalletClosed(int handle) { 00400 if (d->handle == handle) { 00401 d->handle = -1; 00402 d->folder.clear(); 00403 d->name.clear(); 00404 emit walletClosed(); 00405 } 00406 } 00407 00408 00409 QStringList Wallet::folderList() { 00410 if (d->handle == -1) { 00411 return QStringList(); 00412 } 00413 00414 QDBusReply<QStringList> r = walletLauncher->getInterface().folderList(d->handle, appid()); 00415 if (!r.isValid()) 00416 { 00417 kDebug(285) << "Invalid DBus reply: " << r.error(); 00418 return QStringList(); 00419 } 00420 else 00421 return r; 00422 } 00423 00424 00425 QStringList Wallet::entryList() { 00426 if (d->handle == -1) { 00427 return QStringList(); 00428 } 00429 00430 QDBusReply<QStringList> r = walletLauncher->getInterface().entryList(d->handle, d->folder, appid()); 00431 if (!r.isValid()) 00432 { 00433 kDebug(285) << "Invalid DBus reply: " << r.error(); 00434 return QStringList(); 00435 } 00436 else 00437 return r; 00438 } 00439 00440 00441 bool Wallet::hasFolder(const QString& f) { 00442 if (d->handle == -1) { 00443 return false; 00444 } 00445 00446 QDBusReply<bool> r = walletLauncher->getInterface().hasFolder(d->handle, f, appid()); 00447 if (!r.isValid()) 00448 { 00449 kDebug(285) << "Invalid DBus reply: " << r.error(); 00450 return false; 00451 } 00452 else 00453 return r; 00454 00455 } 00456 00457 00458 bool Wallet::createFolder(const QString& f) { 00459 if (d->handle == -1) { 00460 return false; 00461 } 00462 00463 if (!hasFolder(f)) { 00464 QDBusReply<bool> r = walletLauncher->getInterface().createFolder(d->handle, f, appid()); 00465 00466 if (!r.isValid()) 00467 { 00468 kDebug(285) << "Invalid DBus reply: " << r.error(); 00469 return false; 00470 } 00471 else 00472 return r; 00473 } 00474 00475 return true; // folder already exists 00476 } 00477 00478 00479 bool Wallet::setFolder(const QString& f) { 00480 bool rc = false; 00481 00482 if (d->handle == -1) { 00483 return rc; 00484 } 00485 00486 // Don't do this - the folder could have disappeared? 00487 #if 0 00488 if (f == d->folder) { 00489 return true; 00490 } 00491 #endif 00492 00493 if (hasFolder(f)) { 00494 d->folder = f; 00495 rc = true; 00496 } 00497 00498 return rc; 00499 } 00500 00501 00502 bool Wallet::removeFolder(const QString& f) { 00503 if (d->handle == -1) { 00504 return false; 00505 } 00506 00507 QDBusReply<bool> r = walletLauncher->getInterface().removeFolder(d->handle, f, appid()); 00508 if (d->folder == f) { 00509 setFolder(QString()); 00510 } 00511 00512 if (!r.isValid()) 00513 { 00514 kDebug(285) << "Invalid DBus reply: " << r.error(); 00515 return false; 00516 } 00517 else 00518 return r; 00519 } 00520 00521 00522 const QString& Wallet::currentFolder() const { 00523 return d->folder; 00524 } 00525 00526 00527 int Wallet::readEntry(const QString& key, QByteArray& value) { 00528 int rc = -1; 00529 00530 if (d->handle == -1) { 00531 return rc; 00532 } 00533 00534 QDBusReply<QByteArray> r = walletLauncher->getInterface().readEntry(d->handle, d->folder, key, appid()); 00535 if (r.isValid()) { 00536 value = r; 00537 rc = 0; 00538 } 00539 00540 return rc; 00541 } 00542 00543 00544 int Wallet::readEntryList(const QString& key, QMap<QString, QByteArray>& value) { 00545 registerTypes(); 00546 00547 int rc = -1; 00548 00549 if (d->handle == -1) { 00550 return rc; 00551 } 00552 00553 QDBusReply<QVariantMap> r = walletLauncher->getInterface().readEntryList(d->handle, d->folder, key, appid()); 00554 if (r.isValid()) { 00555 rc = 0; 00556 // convert <QString, QVariant> to <QString, QByteArray> 00557 const QVariantMap val = r.value(); 00558 for( QVariantMap::const_iterator it = val.begin(); it != val.end(); ++it ) { 00559 value.insert(it.key(), it.value().toByteArray()); 00560 } 00561 } 00562 00563 return rc; 00564 } 00565 00566 00567 int Wallet::renameEntry(const QString& oldName, const QString& newName) { 00568 int rc = -1; 00569 00570 if (d->handle == -1) { 00571 return rc; 00572 } 00573 00574 QDBusReply<int> r = walletLauncher->getInterface().renameEntry(d->handle, d->folder, oldName, newName, appid()); 00575 if (r.isValid()) { 00576 rc = r; 00577 } 00578 00579 return rc; 00580 } 00581 00582 00583 int Wallet::readMap(const QString& key, QMap<QString,QString>& value) { 00584 registerTypes(); 00585 00586 int rc = -1; 00587 00588 if (d->handle == -1) { 00589 return rc; 00590 } 00591 00592 QDBusReply<QByteArray> r = walletLauncher->getInterface().readMap(d->handle, d->folder, key, appid()); 00593 if (r.isValid()) { 00594 rc = 0; 00595 QByteArray v = r; 00596 if (!v.isEmpty()) { 00597 QDataStream ds(&v, QIODevice::ReadOnly); 00598 ds >> value; 00599 } 00600 } 00601 00602 return rc; 00603 } 00604 00605 00606 int Wallet::readMapList(const QString& key, QMap<QString, QMap<QString, QString> >& value) { 00607 registerTypes(); 00608 00609 int rc = -1; 00610 00611 if (d->handle == -1) { 00612 return rc; 00613 } 00614 00615 QDBusReply<QVariantMap> r = 00616 walletLauncher->getInterface().readMapList(d->handle, d->folder, key, appid()); 00617 if (r.isValid()) { 00618 rc = 0; 00619 const QVariantMap val = r.value(); 00620 for( QVariantMap::const_iterator it = val.begin(); it != val.end(); ++it ) { 00621 QByteArray mapData = it.value().toByteArray(); 00622 if (!mapData.isEmpty()) { 00623 QDataStream ds(&mapData, QIODevice::ReadOnly); 00624 QMap<QString,QString> v; 00625 ds >> v; 00626 value.insert(it.key(), v); 00627 } 00628 } 00629 } 00630 00631 return rc; 00632 } 00633 00634 00635 int Wallet::readPassword(const QString& key, QString& value) { 00636 int rc = -1; 00637 00638 if (d->handle == -1) { 00639 return rc; 00640 } 00641 00642 QDBusReply<QString> r = walletLauncher->getInterface().readPassword(d->handle, d->folder, key, appid()); 00643 if (r.isValid()) { 00644 value = r; 00645 rc = 0; 00646 } 00647 00648 return rc; 00649 } 00650 00651 00652 int Wallet::readPasswordList(const QString& key, QMap<QString, QString>& value) { 00653 registerTypes(); 00654 00655 int rc = -1; 00656 00657 if (d->handle == -1) { 00658 return rc; 00659 } 00660 00661 QDBusReply<QVariantMap> r = walletLauncher->getInterface().readPasswordList(d->handle, d->folder, key, appid()); 00662 if (r.isValid()) { 00663 rc = 0; 00664 const QVariantMap val = r.value(); 00665 for( QVariantMap::const_iterator it = val.begin(); it != val.end(); ++it ) { 00666 value.insert(it.key(), it.value().toString()); 00667 } 00668 } 00669 00670 return rc; 00671 } 00672 00673 00674 int Wallet::writeEntry(const QString& key, const QByteArray& value, EntryType entryType) { 00675 int rc = -1; 00676 00677 if (d->handle == -1) { 00678 return rc; 00679 } 00680 00681 QDBusReply<int> r = walletLauncher->getInterface().writeEntry(d->handle, d->folder, key, value, int(entryType), appid()); 00682 if (r.isValid()) { 00683 rc = r; 00684 } 00685 00686 return rc; 00687 } 00688 00689 00690 int Wallet::writeEntry(const QString& key, const QByteArray& value) { 00691 int rc = -1; 00692 00693 if (d->handle == -1) { 00694 return rc; 00695 } 00696 00697 QDBusReply<int> r = walletLauncher->getInterface().writeEntry(d->handle, d->folder, key, value, appid()); 00698 if (r.isValid()) { 00699 rc = r; 00700 } 00701 00702 return rc; 00703 } 00704 00705 00706 int Wallet::writeMap(const QString& key, const QMap<QString,QString>& value) { 00707 registerTypes(); 00708 00709 int rc = -1; 00710 00711 if (d->handle == -1) { 00712 return rc; 00713 } 00714 00715 QByteArray mapData; 00716 QDataStream ds(&mapData, QIODevice::WriteOnly); 00717 ds << value; 00718 QDBusReply<int> r = walletLauncher->getInterface().writeMap(d->handle, d->folder, key, mapData, appid()); 00719 if (r.isValid()) { 00720 rc = r; 00721 } 00722 00723 return rc; 00724 } 00725 00726 00727 int Wallet::writePassword(const QString& key, const QString& value) { 00728 int rc = -1; 00729 00730 if (d->handle == -1) { 00731 return rc; 00732 } 00733 00734 QDBusReply<int> r = walletLauncher->getInterface().writePassword(d->handle, d->folder, key, value, appid()); 00735 if (r.isValid()) { 00736 rc = r; 00737 } 00738 00739 return rc; 00740 } 00741 00742 00743 bool Wallet::hasEntry(const QString& key) { 00744 if (d->handle == -1) { 00745 return false; 00746 } 00747 00748 QDBusReply<bool> r = walletLauncher->getInterface().hasEntry(d->handle, d->folder, key, appid()); 00749 if (!r.isValid()) 00750 { 00751 kDebug(285) << "Invalid DBus reply: " << r.error(); 00752 return false; 00753 } 00754 else 00755 return r; 00756 } 00757 00758 00759 int Wallet::removeEntry(const QString& key) { 00760 int rc = -1; 00761 00762 if (d->handle == -1) { 00763 return rc; 00764 } 00765 00766 QDBusReply<int> r = walletLauncher->getInterface().removeEntry(d->handle, d->folder, key, appid()); 00767 if (r.isValid()) { 00768 rc = r; 00769 } 00770 00771 return rc; 00772 } 00773 00774 00775 Wallet::EntryType Wallet::entryType(const QString& key) { 00776 int rc = 0; 00777 00778 if (d->handle == -1) { 00779 return Wallet::Unknown; 00780 } 00781 00782 QDBusReply<int> r = walletLauncher->getInterface().entryType(d->handle, d->folder, key, appid()); 00783 if (r.isValid()) { 00784 rc = r; 00785 } 00786 00787 return static_cast<EntryType>(rc); 00788 } 00789 00790 00791 void Wallet::WalletPrivate::walletServiceUnregistered() 00792 { 00793 if (loop) { 00794 loop->quit(); 00795 } 00796 00797 if (handle >= 0) { 00798 q->slotWalletClosed(handle); 00799 } 00800 } 00801 00802 void Wallet::slotFolderUpdated(const QString& wallet, const QString& folder) { 00803 if (d->name == wallet) { 00804 emit folderUpdated(folder); 00805 } 00806 } 00807 00808 00809 void Wallet::slotFolderListUpdated(const QString& wallet) { 00810 if (d->name == wallet) { 00811 emit folderListUpdated(); 00812 } 00813 } 00814 00815 00816 void Wallet::slotApplicationDisconnected(const QString& wallet, const QString& application) { 00817 if (d->handle >= 0 00818 && d->name == wallet 00819 && application == appid()) { 00820 slotWalletClosed(d->handle); 00821 } 00822 } 00823 00824 void Wallet::walletAsyncOpened(int tId, int handle) { 00825 // ignore responses to calls other than ours 00826 if (d->transactionId != tId || d->handle != -1) { 00827 return; 00828 } 00829 00830 // disconnect the async signal 00831 disconnect(this, SLOT(walletAsyncOpened(int, int))); 00832 00833 d->handle = handle; 00834 emit walletOpened(handle > 0); 00835 } 00836 00837 void Wallet::emitWalletAsyncOpenError() { 00838 emit walletOpened(false); 00839 } 00840 00841 bool Wallet::folderDoesNotExist(const QString& wallet, const QString& folder) 00842 { 00843 QDBusReply<bool> r = walletLauncher->getInterface().folderDoesNotExist(wallet, folder); 00844 if (!r.isValid()) 00845 { 00846 kDebug(285) << "Invalid DBus reply: " << r.error(); 00847 return false; 00848 } 00849 else 00850 return r; 00851 } 00852 00853 00854 bool Wallet::keyDoesNotExist(const QString& wallet, const QString& folder, const QString& key) 00855 { 00856 QDBusReply<bool> r = walletLauncher->getInterface().keyDoesNotExist(wallet, folder, key); 00857 if (!r.isValid()) 00858 { 00859 kDebug(285) << "Invalid DBus reply: " << r.error(); 00860 return false; 00861 } 00862 else 00863 return r; 00864 } 00865 00866 void Wallet::virtual_hook(int, void*) { 00867 //BASE::virtual_hook( id, data ); 00868 } 00869 00870 KWalletDLauncher::KWalletDLauncher() 00871 : m_wallet(QString::fromLatin1(s_kwalletdServiceName), "/modules/kwalletd", QDBusConnection::sessionBus()), 00872 m_cgroup(KSharedConfig::openConfig("kwalletrc", KConfig::NoGlobals)->group("Wallet")) 00873 { 00874 } 00875 00876 KWalletDLauncher::~KWalletDLauncher() 00877 { 00878 } 00879 00880 org::kde::KWallet &KWalletDLauncher::getInterface() 00881 { 00882 // check if kwalletd is already running 00883 if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(QString::fromLatin1(s_kwalletdServiceName))) 00884 { 00885 // not running! check if it is enabled. 00886 bool walletEnabled = m_cgroup.readEntry("Enabled", true); 00887 if (walletEnabled) { 00888 // wallet is enabled! try launching it 00889 QString error; 00890 int ret = KToolInvocation::startServiceByDesktopPath("kwalletd.desktop", QStringList(), &error); 00891 if (ret > 0) 00892 { 00893 kError(285) << "Couldn't start kwalletd: " << error << endl; 00894 } 00895 00896 if 00897 (!QDBusConnection::sessionBus().interface()->isServiceRegistered(QString::fromLatin1(s_kwalletdServiceName))) { 00898 kDebug(285) << "The kwalletd service is still not registered"; 00899 } else { 00900 kDebug(285) << "The kwalletd service has been registered"; 00901 } 00902 } else { 00903 kError(285) << "The kwalletd service has been disabled"; 00904 } 00905 } 00906 00907 return m_wallet; 00908 } 00909 00910 #include "kwallet.moc"
KDE 4.6 API Reference