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