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