• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • sources
  • kde-4.12
  • kdelibs
  • kdeui
  • util
kwallet.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2  *
3  * Copyright (C) 2002-2004 George Staikos <staikos@kde.org>
4  * Copyright (C) 2008 Michael Leupold <lemma@confuego.org>
5  * Copyright (C) 2011 Valentin Rusu <kde@rusu.info>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #include "kwallet.h"
24 #include "config-kwallet.h"
25 
26 #include <QtGui/QApplication>
27 #include <QtCore/QPointer>
28 #include <QtGui/QWidget>
29 #include <QtDBus/QtDBus>
30 #include <ktoolinvocation.h>
31 
32 #include <assert.h>
33 #include <kcomponentdata.h>
34 #include <kconfiggroup.h>
35 #include <kdebug.h>
36 #include <kdeversion.h>
37 #include <kglobal.h>
38 #include <kaboutdata.h>
39 #include <ksharedconfig.h>
40 #include <kwindowsystem.h>
41 
42 #ifdef HAVE_KSECRETSSERVICE
43 #include "ksecretsservice/ksecretsservicecollection.h"
44 #endif
45 
46 #include "kwallet_interface.h"
47 
48 #ifdef HAVE_KSECRETSSERVICE
49 typedef QMap<QString, KSecretsService::StringStringMap> StringToStringStringMapMap;
50 Q_DECLARE_METATYPE(StringToStringStringMapMap)
51 #endif
52 typedef QMap<QString, QByteArray> StringByteArrayMap;
53 Q_DECLARE_METATYPE(StringByteArrayMap)
54 
55 namespace KWallet
56 {
57 
58 class KWalletDLauncher
59 {
60 public:
61  KWalletDLauncher();
62  ~KWalletDLauncher();
63  org::kde::KWallet &getInterface();
64 
65  // this static variable is used below to switch between old KWallet
66  // infrastructure and the new one which is built on top of the new
67  // KSecretsService infrastructure. It's value can be changed via the
68  // the Wallet configuration module in System Settings
69  bool m_useKSecretsService;
70  org::kde::KWallet *m_wallet;
71  KConfigGroup m_cgroup;
72 };
73 
74 K_GLOBAL_STATIC(KWalletDLauncher, walletLauncher)
75 
76 static QString appid()
77 {
78  if (KGlobal::hasMainComponent()) {
79  KComponentData cData = KGlobal::mainComponent();
80  if (cData.isValid()) {
81  const KAboutData* aboutData = cData.aboutData();
82  if (aboutData) {
83  return aboutData->programName();
84  }
85  return cData.componentName();
86  }
87  }
88  return qApp->applicationName();
89 }
90 
91 static void registerTypes()
92 {
93  static bool registered = false;
94  if (!registered) {
95 #ifdef HAVE_KSECRETSSERVICE
96  qDBusRegisterMetaType<KSecretsService::StringStringMap>();
97  qDBusRegisterMetaType<StringToStringStringMapMap>();
98 #endif
99  qDBusRegisterMetaType<StringByteArrayMap>();
100  registered = true;
101  }
102 }
103 
104 bool Wallet::isUsingKSecretsService()
105 {
106  return walletLauncher->m_useKSecretsService;
107 }
108 
109 const QString Wallet::LocalWallet() {
110  // NOTE: This method stays unchanged for KSecretsService
111  KConfigGroup cfg(KSharedConfig::openConfig("kwalletrc")->group("Wallet"));
112  if (!cfg.readEntry("Use One Wallet", true)) {
113  QString tmp = cfg.readEntry("Local Wallet", "localwallet");
114  if (tmp.isEmpty()) {
115  return "localwallet";
116  }
117  return tmp;
118  }
119 
120  QString tmp = cfg.readEntry("Default Wallet", "kdewallet");
121  if (tmp.isEmpty()) {
122  return "kdewallet";
123  }
124  return tmp;
125 }
126 
127 const QString Wallet::NetworkWallet() {
128  // NOTE: This method stays unchanged for KSecretsService
129  KConfigGroup cfg(KSharedConfig::openConfig("kwalletrc")->group("Wallet"));
130 
131  QString tmp = cfg.readEntry("Default Wallet", "kdewallet");
132  if (tmp.isEmpty()) {
133  return "kdewallet";
134  }
135  return tmp;
136 }
137 
138 const QString Wallet::PasswordFolder() {
139  return "Passwords";
140 }
141 
142 const QString Wallet::FormDataFolder() {
143  return "Form Data";
144 }
145 
146 class Wallet::WalletPrivate
147 {
148 public:
149  WalletPrivate(Wallet *wallet, int h, const QString &n)
150  : q(wallet), name(n), handle(h)
151 #ifdef HAVE_KSECRETSSERVICE
152  , secretsCollection(0)
153 #endif
154  {}
155 
156  void walletServiceUnregistered();
157 
158 #ifdef HAVE_KSECRETSSERVICE
159  template <typename T>
160  int writeEntry( const QString& key, const T &value, Wallet::EntryType entryType ) {
161  int rc = -1;
162  KSecretsService::Secret secret;
163  secret.setValue( QVariant::fromValue<T>(value) );
164 
165  KSecretsService::StringStringMap attrs;
166  attrs[KSS_ATTR_ENTRYFOLDER] = folder;
167  attrs[KSS_ATTR_WALLETTYPE] = QString("%1").arg((int)entryType);
168  KSecretsService::CreateCollectionItemJob *createItemJob = secretsCollection->createItem( key, attrs, secret );
169 
170  if ( !createItemJob->exec() ) {
171  kDebug(285) << "Cannot execute CreateCollectionItemJob : " << createItemJob->errorString();
172  }
173  rc = createItemJob->error();
174  return rc;
175  }
176 
177  QExplicitlySharedDataPointer<KSecretsService::SecretItem> findItem( const QString& key ) const;
178  template <typename T> int readEntry( const QString& key, T& value ) const;
179  bool readSecret( const QString& key, KSecretsService::Secret& value ) const;
180 
181  template <typename V>
182  int forEachItemThatMatches( const QString &key, V verb ) {
183  int rc = -1;
184  KSecretsService::StringStringMap attrs;
185  attrs[KSS_ATTR_ENTRYFOLDER] = folder;
186  KSecretsService::SearchCollectionItemsJob *searchItemsJob = secretsCollection->searchItems(attrs);
187  if ( searchItemsJob->exec() ) {
188  QRegExp re(key, Qt::CaseSensitive, QRegExp::Wildcard);
189  foreach( KSecretsService::SearchCollectionItemsJob::Item item , searchItemsJob->items() ) {
190  KSecretsService::ReadItemPropertyJob *readLabelJob = item->label();
191  if ( readLabelJob->exec() ) {
192  QString label = readLabelJob->propertyValue().toString();
193  if ( re.exactMatch( label ) ) {
194  if ( verb( this, label, item.data() ) ) {
195  rc = 0; // one successfull iteration already produced results, so success return
196  }
197  }
198  }
199  else {
200  kDebug(285) << "Cannot execute ReadItemPropertyJob " << readLabelJob->errorString();
201  }
202  }
203  }
204  else {
205  kDebug(285) << "Cannot execute KSecretsService::SearchCollectionItemsJob " << searchItemsJob->errorString();
206  }
207  return rc;
208  }
209 
210  void createDefaultFolders();
211 
212  struct InsertIntoEntryList;
213  struct InsertIntoMapList;
214  struct InsertIntoPasswordList;
215 
216  KSecretsService::Collection *secretsCollection;
217 #endif // HAVE_KSECRETSSERVICE
218 
219  Wallet *q;
220  QString name;
221  QString folder;
222  int handle;
223  int transactionId;
224 };
225 
226 #ifdef HAVE_KSECRETSSERVICE
227 void Wallet::WalletPrivate::createDefaultFolders()
228 {
229 // NOTE: KWalletManager expects newly created wallets to have two default folders
230 // b->createFolder(KWallet::Wallet::PasswordFolder());
231 // b->createFolder(KWallet::Wallet::FormDataFolder());
232  QString strDummy("");
233  folder = PasswordFolder();
234  writeEntry( PasswordFolder(), strDummy, KWallet::Wallet::Unknown );
235 
236  folder = FormDataFolder();
237  writeEntry( FormDataFolder(), strDummy, KWallet::Wallet::Unknown );
238 }
239 #endif // HAVE_KSECRETSSERVICE
240 
241 static const char s_kwalletdServiceName[] = "org.kde.kwalletd";
242 
243 Wallet::Wallet(int handle, const QString& name)
244  : QObject(0L), d(new WalletPrivate(this, handle, name))
245 {
246  if (walletLauncher->m_useKSecretsService) {
247  // see openWallet for initialization code; this constructor does not have any code
248  }
249  else {
250  QDBusServiceWatcher *watcher = new QDBusServiceWatcher(QString::fromLatin1(s_kwalletdServiceName), QDBusConnection::sessionBus(),
251  QDBusServiceWatcher::WatchForUnregistration, this);
252  connect(watcher, SIGNAL(serviceUnregistered(QString)),
253  this, SLOT(walletServiceUnregistered()));
254 
255  connect(&walletLauncher->getInterface(), SIGNAL(walletClosed(int)), SLOT(slotWalletClosed(int)));
256  connect(&walletLauncher->getInterface(), SIGNAL(folderListUpdated(QString)), SLOT(slotFolderListUpdated(QString)));
257  connect(&walletLauncher->getInterface(), SIGNAL(folderUpdated(QString,QString)), SLOT(slotFolderUpdated(QString,QString)));
258  connect(&walletLauncher->getInterface(), SIGNAL(applicationDisconnected(QString,QString)), SLOT(slotApplicationDisconnected(QString,QString)));
259 
260  // Verify that the wallet is still open
261  if (d->handle != -1) {
262  QDBusReply<bool> r = walletLauncher->getInterface().isOpen(d->handle);
263  if (r.isValid() && !r) {
264  d->handle = -1;
265  d->name.clear();
266  }
267  }
268  }
269 }
270 
271 
272 Wallet::~Wallet() {
273 #ifdef HAVE_KSECRETSSERVICE
274  if (walletLauncher->m_useKSecretsService) {
275  d->folder.clear();
276  d->name.clear();
277  delete d->secretsCollection;
278  }
279  else {
280 #endif
281  if (d->handle != -1) {
282  if (!walletLauncher.isDestroyed()) {
283  walletLauncher->getInterface().close(d->handle, false, appid());
284  } else {
285  kDebug(285) << "Problem with static destruction sequence."
286  "Destroy any static Wallet before the event-loop exits.";
287  }
288  d->handle = -1;
289  d->folder.clear();
290  d->name.clear();
291  }
292 #ifdef HAVE_KSECRETSSERVICE
293  }
294 #endif
295  delete d;
296 }
297 
298 
299 QStringList Wallet::walletList() {
300  QStringList result;
301 #ifdef HAVE_KSECRETSSERVICE
302  if (walletLauncher->m_useKSecretsService) {
303  KSecretsService::ListCollectionsJob *listJob = KSecretsService::Collection::listCollections();
304  if ( listJob->exec() ) {
305  result = listJob->collections();
306  }
307  else {
308  kDebug(285) << "Cannot execute ListCollectionsJob: " << listJob->errorString();
309  }
310  }
311  else {
312 #endif
313  QDBusReply<QStringList> r = walletLauncher->getInterface().wallets();
314 
315  if (!r.isValid())
316  {
317  kDebug(285) << "Invalid DBus reply: " << r.error();
318  }
319  else
320  result = r;
321 #ifdef HAVE_KSECRETSSERVICE
322  }
323 #endif
324  return result;
325 }
326 
327 
328 void Wallet::changePassword(const QString& name, WId w) {
329  if( w == 0 )
330  kDebug(285) << "Pass a valid window to KWallet::Wallet::changePassword().";
331 
332  // Make sure the password prompt window will be visible and activated
333  KWindowSystem::allowExternalProcessWindowActivation();
334 #ifdef HAVE_KSECRETSSERVICE
335  if (walletLauncher->m_useKSecretsService) {
336  KSecretsService::Collection *coll = KSecretsService::Collection::findCollection( name );
337  KSecretsService::ChangeCollectionPasswordJob* changePwdJob = coll->changePassword();
338  if ( !changePwdJob->exec() ) {
339  kDebug(285) << "Cannot execute change password job: " << changePwdJob->errorString();
340  }
341  coll->deleteLater();
342  }
343  else {
344 #endif
345  walletLauncher->getInterface().changePassword(name, (qlonglong)w, appid());
346 #ifdef HAVE_KSECRETSSERVICE
347  }
348 #endif
349 }
350 
351 
352 bool Wallet::isEnabled() {
353 #ifdef HAVE_KSECRETSSERVICE
354  if (walletLauncher->m_useKSecretsService) {
355  return walletLauncher->m_cgroup.readEntry("Enabled", true);
356  }
357  else {
358 #endif
359  QDBusReply<bool> r = walletLauncher->getInterface().isEnabled();
360 
361  if (!r.isValid())
362  {
363  kDebug(285) << "Invalid DBus reply: " << r.error();
364  return false;
365  }
366  else
367  return r;
368 #ifdef HAVE_KSECRETSSERVICE
369  }
370 #endif
371 }
372 
373 
374 bool Wallet::isOpen(const QString& name) {
375 #ifdef HAVE_KSECRETSSERVICE
376  if (walletLauncher->m_useKSecretsService) {
377  KSecretsService::Collection *coll = KSecretsService::Collection::findCollection( name, KSecretsService::Collection::OpenOnly );
378  KSecretsService::ReadCollectionPropertyJob *readLocked = coll->isLocked();
379  if ( readLocked->exec() ) {
380  return !readLocked->propertyValue().toBool();
381  }
382  else {
383  kDebug() << "ReadLocked job failed";
384  return false;
385  }
386  }
387  else {
388 #endif
389  QDBusReply<bool> r = walletLauncher->getInterface().isOpen(name);
390 
391  if (!r.isValid())
392  {
393  kDebug(285) << "Invalid DBus reply: " << r.error();
394  return false;
395  }
396  else
397  return r;
398 #ifdef HAVE_KSECRETSSERVICE
399  }
400 #endif
401 }
402 
403 int Wallet::closeWallet(const QString& name, bool force) {
404 #ifdef HAVE_KSECRETSSERVICE
405  if (walletLauncher->m_useKSecretsService) {
406  kDebug(285) << "Wallet::closeWallet NOOP";
407  return 0;
408  }
409  else {
410 #endif
411  QDBusReply<int> r = walletLauncher->getInterface().close(name, force);
412 
413  if (!r.isValid())
414  {
415  kDebug(285) << "Invalid DBus reply: " << r.error();
416  return -1;
417  }
418  else
419  return r;
420 #ifdef HAVE_KSECRETSSERVICE
421  }
422 #endif
423 }
424 
425 
426 int Wallet::deleteWallet(const QString& name) {
427 #ifdef HAVE_KSECRETSSERVICE
428  if (walletLauncher->m_useKSecretsService) {
429  KSecretsService::Collection *coll = KSecretsService::Collection::findCollection(name, KSecretsService::Collection::OpenOnly);
430  KJob *deleteJob = coll->deleteCollection();
431  if (!deleteJob->exec()) {
432  kDebug(285) << "Cannot execute delete job " << deleteJob->errorString();
433  }
434  return deleteJob->error();
435  }
436  else {
437 #endif
438  QDBusReply<int> r = walletLauncher->getInterface().deleteWallet(name);
439 
440  if (!r.isValid())
441  {
442  kDebug(285) << "Invalid DBus reply: " << r.error();
443  return -1;
444  }
445  else
446  return r;
447 #ifdef HAVE_KSECRETSSERVICE
448  }
449 #endif
450 }
451 
452 Wallet *Wallet::openWallet(const QString& name, WId w, OpenType ot) {
453  if( w == 0 )
454  kDebug(285) << "Pass a valid window to KWallet::Wallet::openWallet().";
455 
456 #ifdef HAVE_KSECRETSSERVICE
457  if (walletLauncher->m_useKSecretsService) {
458  Wallet *wallet = new Wallet(-1, name);
459  // FIXME: should we specify CreateCollection or OpenOnly here?
460  wallet->d->secretsCollection = KSecretsService::Collection::findCollection(name, KSecretsService::Collection::CreateCollection, QVariantMap(), w);
461  connect( wallet->d->secretsCollection, SIGNAL(statusChanged(int)), wallet, SLOT(slotCollectionStatusChanged(int)) );
462  connect( wallet->d->secretsCollection, SIGNAL(deleted()), wallet, SLOT(slotCollectionDeleted()) );
463  if ( ot == Synchronous ) {
464  kDebug() << "WARNING openWallet OpenType=Synchronous requested";
465  // TODO: not sure what to do with in this case; however, all other KSecretsService API methods are already
466  // async and will perform sync inside this API because of it's design
467  }
468  return wallet;
469  }
470  else {
471 #endif
472  Wallet *wallet = new Wallet(-1, name);
473 
474  // connect the daemon's opened signal to the slot filtering the
475  // signals we need
476  connect(&walletLauncher->getInterface(), SIGNAL(walletAsyncOpened(int,int)),
477  wallet, SLOT(walletAsyncOpened(int,int)));
478 
479  // Make sure the password prompt window will be visible and activated
480  KWindowSystem::allowExternalProcessWindowActivation();
481 
482  // do the call
483  QDBusReply<int> r;
484  if (ot == Synchronous) {
485  r = walletLauncher->getInterface().open(name, (qlonglong)w, appid());
486  // after this call, r would contain a transaction id >0 if OK or -1 if NOK
487  // if OK, the slot walletAsyncOpened should have been received, but the transaction id
488  // will not match. We'll get that handle from the reply - see below
489  }
490  else if (ot == Asynchronous) {
491  r = walletLauncher->getInterface().openAsync(name, (qlonglong)w, appid(), true);
492  } else if (ot == Path) {
493  r = walletLauncher->getInterface().openPathAsync(name, (qlonglong)w, appid(), true);
494  } else {
495  delete wallet;
496  return 0;
497  }
498  // error communicating with the daemon (maybe not running)
499  if (!r.isValid()) {
500  kDebug(285) << "Invalid DBus reply: " << r.error();
501  delete wallet;
502  return 0;
503  }
504  wallet->d->transactionId = r.value();
505 
506  if (ot == Synchronous || ot == Path) {
507  // check for an immediate error
508  if (wallet->d->transactionId < 0) {
509  delete wallet;
510  wallet = 0;
511  } else {
512  wallet->d->handle = r.value();
513  }
514  } else if (ot == Asynchronous) {
515  if (wallet->d->transactionId < 0) {
516  QTimer::singleShot(0, wallet, SLOT(emitWalletAsyncOpenError()));
517  // client code is responsible for deleting the wallet
518  }
519  }
520 
521  return wallet;
522 #ifdef HAVE_KSECRETSSERVICE
523  }
524 #endif
525 }
526 
527 void Wallet::slotCollectionStatusChanged(int status)
528 {
529 #ifdef HAVE_KSECRETSSERVICE
530  KSecretsService::Collection::Status collStatus = (KSecretsService::Collection::Status)status;
531  switch ( collStatus ) {
532  case KSecretsService::Collection::NewlyCreated:
533  d->createDefaultFolders();
534  // fall through
535  case KSecretsService::Collection::FoundExisting:
536  emitWalletOpened();
537  break;
538  case KSecretsService::Collection::Deleted:
539  case KSecretsService::Collection::Invalid:
540  case KSecretsService::Collection::Pending:
541  // nothing to do
542  break;
543  case KSecretsService::Collection::NotFound:
544  emitWalletAsyncOpenError();
545  break;
546  }
547 #endif
548 }
549 
550 void Wallet::slotCollectionDeleted()
551 {
552  d->folder.clear();
553  d->name.clear();
554  emit walletClosed();
555 }
556 
557 bool Wallet::disconnectApplication(const QString& wallet, const QString& app) {
558 #ifdef HAVE_KSECRETSSERVICE
559  if (walletLauncher->m_useKSecretsService) {
560  kDebug() << "Wallet::disconnectApplication NOOP";
561  return true;
562  }
563  else {
564 #endif
565  QDBusReply<bool> r = walletLauncher->getInterface().disconnectApplication(wallet, app);
566 
567  if (!r.isValid())
568  {
569  kDebug(285) << "Invalid DBus reply: " << r.error();
570  return false;
571  }
572  else
573  return r;
574 #ifdef HAVE_KSECRETSSERVICE
575  }
576 #endif
577 }
578 
579 
580 QStringList Wallet::users(const QString& name) {
581 #ifdef HAVE_KSECRETSSERVICE
582  if (walletLauncher->m_useKSecretsService) {
583  kDebug() << "KSecretsService does not handle users list";
584  return QStringList();
585  }
586  else {
587 #endif
588  QDBusReply<QStringList> r = walletLauncher->getInterface().users(name);
589  if (!r.isValid())
590  {
591  kDebug(285) << "Invalid DBus reply: " << r.error();
592  return QStringList();
593  }
594  else
595  return r;
596 #ifdef HAVE_KSECRETSSERVICE
597  }
598 #endif
599 }
600 
601 
602 int Wallet::sync() {
603 #ifdef HAVE_KSECRETSSERVICE
604  if (walletLauncher->m_useKSecretsService) {
605  // NOOP with KSecretsService
606  }
607  else {
608 #endif
609  if (d->handle == -1) {
610  return -1;
611  }
612 
613  walletLauncher->getInterface().sync(d->handle, appid());
614 #ifdef HAVE_KSECRETSSERVICE
615  }
616 #endif
617  return 0;
618 }
619 
620 
621 int Wallet::lockWallet() {
622 #ifdef HAVE_KSECRETSSERVICE
623  if (walletLauncher->m_useKSecretsService) {
624  KSecretsService::CollectionLockJob *lockJob = d->secretsCollection->lock();
625  if (lockJob->exec()) {
626  d->folder.clear();
627  d->name.clear();
628  }
629  else {
630  kDebug(285) << "Cannot execute KSecretsService::CollectionLockJob : " << lockJob->errorString();
631  return -1;
632  }
633  return lockJob->error();
634  }
635  else {
636 #endif
637  if (d->handle == -1) {
638  return -1;
639  }
640 
641  QDBusReply<int> r = walletLauncher->getInterface().close(d->handle, true, appid());
642  d->handle = -1;
643  d->folder.clear();
644  d->name.clear();
645  if (r.isValid()) {
646  return r;
647  }
648  else {
649  kDebug(285) << "Invalid DBus reply: " << r.error();
650  return -1;
651  }
652 #ifdef HAVE_KSECRETSSERVICE
653  }
654 #endif
655 }
656 
657 
658 const QString& Wallet::walletName() const {
659  return d->name;
660 }
661 
662 
663 bool Wallet::isOpen() const {
664 #ifdef HAVE_KSECRETSSERVICE
665  if (walletLauncher->m_useKSecretsService) {
666  return !d->secretsCollection->isLocked();
667  }
668  else {
669 #endif
670  return d->handle != -1;
671 #ifdef HAVE_KSECRETSSERVICE
672  }
673 #endif
674 }
675 
676 
677 void Wallet::requestChangePassword(WId w) {
678  if( w == 0 )
679  kDebug(285) << "Pass a valid window to KWallet::Wallet::requestChangePassword().";
680 
681 #ifdef HAVE_KSECRETSSERVICE
682  if (walletLauncher->m_useKSecretsService) {
683  KSecretsService::ChangeCollectionPasswordJob *changePwdJob = d->secretsCollection->changePassword();
684  if (!changePwdJob->exec()) {
685  kDebug(285) << "Cannot execute ChangeCollectionPasswordJob : " << changePwdJob->errorString();
686  }
687  }
688  else {
689 #endif
690  if (d->handle == -1) {
691  return;
692  }
693 
694  // Make sure the password prompt window will be visible and activated
695  KWindowSystem::allowExternalProcessWindowActivation();
696 
697  walletLauncher->getInterface().changePassword(d->name, (qlonglong)w, appid());
698 #ifdef HAVE_KSECRETSSERVICE
699  }
700 #endif
701 }
702 
703 
704 void Wallet::slotWalletClosed(int handle) {
705 #ifdef HAVE_KSECRETSSERVICE
706  if (walletLauncher->m_useKSecretsService) {
707  // TODO: implement this
708  Q_ASSERT(0);
709  }
710  else {
711 #endif
712  if (d->handle == handle) {
713  d->handle = -1;
714  d->folder.clear();
715  d->name.clear();
716  emit walletClosed();
717  }
718 #ifdef HAVE_KSECRETSSERVICE
719  }
720 #endif
721 }
722 
723 
724 QStringList Wallet::folderList() {
725 #ifdef HAVE_KSECRETSSERVICE
726  if (walletLauncher->m_useKSecretsService) {
727  QStringList result;
728 
729  KSecretsService::StringStringMap attrs;
730  attrs[KSS_ATTR_ENTRYFOLDER] = ""; // search for items having this attribute no matter what value it has
731  KSecretsService::SearchCollectionItemsJob *searchJob = d->secretsCollection->searchItems(attrs);
732 
733  if (searchJob->exec()) {
734  KSecretsService::ReadCollectionItemsJob::ItemList itemList = searchJob->items();
735  foreach( const KSecretsService::ReadCollectionItemsJob::Item &item, itemList ) {
736  KSecretsService::ReadItemPropertyJob *readAttrsJob = item->attributes();
737  if (readAttrsJob->exec()) {
738  KSecretsService::StringStringMap attrs = readAttrsJob->propertyValue().value<KSecretsService::StringStringMap>();
739  const QString folder = attrs[KSS_ATTR_ENTRYFOLDER];
740  if (!folder.isEmpty() && !result.contains(folder)) {
741  result.append(folder);
742  }
743  }
744  else {
745  kDebug(285) << "Cannot read item attributes : " << readAttrsJob->errorString();
746  }
747  }
748  }
749  else {
750  kDebug(285) << "Cannot execute ReadCollectionItemsJob : " << searchJob->errorString();
751  }
752  return result;
753  }
754  else {
755 #endif
756  if (d->handle == -1) {
757  return QStringList();
758  }
759 
760  QDBusReply<QStringList> r = walletLauncher->getInterface().folderList(d->handle, appid());
761  if (!r.isValid())
762  {
763  kDebug(285) << "Invalid DBus reply: " << r.error();
764  return QStringList();
765  }
766  else
767  return r;
768 #ifdef HAVE_KSECRETSSERVICE
769  }
770 #endif
771 }
772 
773 
774 QStringList Wallet::entryList() {
775 #ifdef HAVE_KSECRETSSERVICE
776  if (walletLauncher->m_useKSecretsService) {
777  QStringList result;
778  KSecretsService::StringStringMap attrs;
779  attrs[KSS_ATTR_ENTRYFOLDER] = d->folder;
780  KSecretsService::SearchCollectionItemsJob *readItemsJob = d->secretsCollection->searchItems( attrs );
781  if ( readItemsJob->exec() ) {
782  foreach( KSecretsService::SearchCollectionItemsJob::Item item, readItemsJob->items() ) {
783  KSecretsService::ReadItemPropertyJob *readLabelJob = item->label();
784  if ( readLabelJob->exec() ) {
785  result.append( readLabelJob->propertyValue().toString() );
786  }
787  else {
788  kDebug(285) << "Cannot execute readLabelJob" << readItemsJob->errorString();
789  }
790  }
791  }
792  else {
793  kDebug(285) << "Cannot execute readItemsJob" << readItemsJob->errorString();
794  }
795  return result;
796  }
797  else {
798 #endif
799  if (d->handle == -1) {
800  return QStringList();
801  }
802 
803  QDBusReply<QStringList> r = walletLauncher->getInterface().entryList(d->handle, d->folder, appid());
804  if (!r.isValid())
805  {
806  kDebug(285) << "Invalid DBus reply: " << r.error();
807  return QStringList();
808  }
809  else
810  return r;
811 #ifdef HAVE_KSECRETSSERVICE
812  }
813 #endif
814 }
815 
816 
817 bool Wallet::hasFolder(const QString& f) {
818 #ifdef HAVE_KSECRETSSERVICE
819  if (walletLauncher->m_useKSecretsService) {
820  // FIXME: well, this is not the best implementation, but it's done quickly :)
821  // the best way would be to searchItems with the attribute label having the value f
822  // doing that would reduce DBus traffic. But KWallet API wille not last.
823  QStringList folders = folderList();
824  return folders.contains(f);
825  }
826  else {
827 #endif
828  if (d->handle == -1) {
829  return false;
830  }
831 
832  QDBusReply<bool> r = walletLauncher->getInterface().hasFolder(d->handle, f, appid());
833  if (!r.isValid())
834  {
835  kDebug(285) << "Invalid DBus reply: " << r.error();
836  return false;
837  }
838  else
839  return r;
840 #ifdef HAVE_KSECRETSSERVICE
841  }
842 #endif
843 }
844 
845 
846 bool Wallet::createFolder(const QString& f) {
847 #ifdef HAVE_KSECRETSSERVICE
848  if (walletLauncher->m_useKSecretsService) {
849  QString strDummy("");
850  d->folder = f;
851  d->writeEntry( f, strDummy, KWallet::Wallet::Unknown );
852  return true;
853  }
854  else {
855 #endif
856  if (d->handle == -1) {
857  return false;
858  }
859 
860  if (!hasFolder(f)) {
861  QDBusReply<bool> r = walletLauncher->getInterface().createFolder(d->handle, f, appid());
862 
863  if (!r.isValid())
864  {
865  kDebug(285) << "Invalid DBus reply: " << r.error();
866  return false;
867  }
868  else
869  return r;
870  }
871 
872  return true; // folder already exists
873 #ifdef HAVE_KSECRETSSERVICE
874  }
875 #endif
876 }
877 
878 
879 bool Wallet::setFolder(const QString& f) {
880  bool rc = false;
881 
882 #ifdef HAVE_KSECRETSSERVICE
883  if (walletLauncher->m_useKSecretsService) {
884  if (hasFolder(f)) {
885  d->folder = f;
886  rc = true;
887  }
888  }
889  else {
890 #endif
891  if (d->handle == -1) {
892  return rc;
893  }
894 
895  // Don't do this - the folder could have disappeared?
896  #if 0
897  if (f == d->folder) {
898  return true;
899  }
900  #endif
901 
902  if (hasFolder(f)) {
903  d->folder = f;
904  rc = true;
905  }
906 #ifdef HAVE_KSECRETSSERVICE
907  }
908 #endif
909 
910  return rc;
911 }
912 
913 
914 bool Wallet::removeFolder(const QString& f) {
915 #ifdef HAVE_KSECRETSSERVICE
916  if (walletLauncher->m_useKSecretsService) {
917  bool result = false;
918  // search for all items having the folder f then delete them
919  KSecretsService::StringStringMap attrs;
920  attrs[KSS_ATTR_ENTRYFOLDER] = f;
921  KSecretsService::SearchCollectionItemsJob *searchJob = d->secretsCollection->searchItems(attrs);
922  if (searchJob->exec()) {
923  KSecretsService::SearchCollectionItemsJob::ItemList itemList = searchJob->items();
924  if ( !itemList.isEmpty() ) {
925  result = true;
926  foreach( const KSecretsService::SearchCollectionItemsJob::Item &item, itemList ) {
927  KSecretsService::SecretItemDeleteJob *deleteJob = item->deleteItem();
928  if (!deleteJob->exec()) {
929  kDebug(285) << "Cannot delete item : " << deleteJob->errorString();
930  result = false;
931  }
932  result &= true;
933  }
934  }
935  }
936  else {
937  kDebug(285) << "Cannot execute KSecretsService::SearchCollectionItemsJob : " << searchJob->errorString();
938  }
939  return result;
940  }
941  else {
942 #endif
943  if (d->handle == -1) {
944  return false;
945  }
946 
947  QDBusReply<bool> r = walletLauncher->getInterface().removeFolder(d->handle, f, appid());
948  if (d->folder == f) {
949  setFolder(QString());
950  }
951 
952  if (!r.isValid())
953  {
954  kDebug(285) << "Invalid DBus reply: " << r.error();
955  return false;
956  }
957  else
958  return r;
959 #ifdef HAVE_KSECRETSSERVICE
960  }
961 #endif
962 }
963 
964 
965 const QString& Wallet::currentFolder() const {
966  return d->folder;
967 }
968 
969 #ifdef HAVE_KSECRETSSERVICE
970 QExplicitlySharedDataPointer<KSecretsService::SecretItem> Wallet::WalletPrivate::findItem( const QString& key ) const
971 {
972  QExplicitlySharedDataPointer<KSecretsService::SecretItem> result;
973  KSecretsService::StringStringMap attrs;
974  attrs[KSS_ATTR_ENTRYFOLDER] = folder;
975  attrs["Label"] = key;
976  KSecretsService::SearchCollectionItemsJob *searchJob = secretsCollection->searchItems(attrs);
977  if (searchJob->exec()) {
978  KSecretsService::SearchCollectionItemsJob::ItemList itemList = searchJob->items();
979  if ( !itemList.isEmpty() ) {
980  result = itemList.first();
981  }
982  else {
983  kDebug(285) << "entry named " << key << " not found in folder " << folder;
984  }
985  }
986  else {
987  kDebug(285) << "Cannot exec KSecretsService::SearchCollectionItemsJob : " << searchJob->errorString();
988  }
989 
990  return result;
991 }
992 
993 template <typename T>
994 int Wallet::WalletPrivate::readEntry(const QString& key, T& value) const
995 {
996  int rc = -1;
997  QExplicitlySharedDataPointer<KSecretsService::SecretItem> item = findItem(key);
998  if ( item ) {
999  KSecretsService::GetSecretItemSecretJob *readJob = item->getSecret();
1000  if ( readJob->exec() ) {
1001  KSecretsService::Secret theSecret = readJob->secret();
1002  kDebug(285) << "Secret contentType is " << theSecret.contentType();
1003  value = theSecret.value().value<T>();
1004  rc = 0;
1005  }
1006  else {
1007  kDebug(285) << "Cannot exec GetSecretItemSecretJob : " << readJob->errorString();
1008  }
1009  }
1010  return rc;
1011 }
1012 
1013 bool Wallet::WalletPrivate::readSecret(const QString& key, KSecretsService::Secret& value) const
1014 {
1015  bool result = false;
1016  QExplicitlySharedDataPointer<KSecretsService::SecretItem> item = findItem(key);
1017  if ( item ) {
1018  KSecretsService::GetSecretItemSecretJob *readJob = item->getSecret();
1019  if ( readJob->exec() ) {
1020  value = readJob->secret();
1021  result = true;
1022  }
1023  else {
1024  kDebug(285) << "Cannot exec GetSecretItemSecretJob : " << readJob->errorString();
1025  }
1026  }
1027  return result;
1028 }
1029 #endif
1030 
1031 int Wallet::readEntry(const QString& key, QByteArray& value) {
1032  int rc = -1;
1033 
1034 #ifdef HAVE_KSECRETSSERVICE
1035  if (walletLauncher->m_useKSecretsService) {
1036  return d->readEntry<QByteArray>(key, value);
1037  }
1038  else {
1039 #endif
1040  if (d->handle == -1) {
1041  return rc;
1042  }
1043 
1044  QDBusReply<QByteArray> r = walletLauncher->getInterface().readEntry(d->handle, d->folder, key, appid());
1045  if (r.isValid()) {
1046  value = r;
1047  rc = 0;
1048  }
1049 #ifdef HAVE_KSECRETSSERVICE
1050  }
1051 #endif
1052 
1053  return rc;
1054 }
1055 
1056 #ifdef HAVE_KSECRETSSERVICE
1057 struct Wallet::WalletPrivate::InsertIntoEntryList {
1058  InsertIntoEntryList( QMap< QString, QByteArray> &value ) : _value( value ) {}
1059  bool operator() ( Wallet::WalletPrivate*, const QString& label, KSecretsService::SecretItem* item ) {
1060  bool result = false;
1061  KSecretsService::GetSecretItemSecretJob *readSecretJob = item->getSecret();
1062  if ( readSecretJob->exec() ) {
1063  _value.insert( label, readSecretJob->secret().value().toByteArray() );
1064  result = true;
1065  }
1066  else {
1067  kDebug(285) << "Cannot execute GetSecretItemSecretJob " << readSecretJob->errorString();
1068  }
1069  return result;
1070  }
1071  QMap< QString, QByteArray > _value;
1072 };
1073 #endif
1074 
1075 int Wallet::readEntryList(const QString& key, QMap<QString, QByteArray>& value) {
1076 
1077  int rc = -1;
1078 
1079 #ifdef HAVE_KSECRETSSERVICE
1080  if (walletLauncher->m_useKSecretsService) {
1081  rc = d->forEachItemThatMatches( key, WalletPrivate::InsertIntoEntryList( value ) );
1082  }
1083  else {
1084 #endif
1085  registerTypes();
1086 
1087  if (d->handle == -1) {
1088  return rc;
1089  }
1090 
1091  QDBusReply<QVariantMap> r = walletLauncher->getInterface().readEntryList(d->handle, d->folder, key, appid());
1092  if (r.isValid()) {
1093  rc = 0;
1094  // convert <QString, QVariant> to <QString, QByteArray>
1095  const QVariantMap val = r.value();
1096  for( QVariantMap::const_iterator it = val.begin(); it != val.end(); ++it ) {
1097  value.insert(it.key(), it.value().toByteArray());
1098  }
1099  }
1100 #ifdef HAVE_KSECRETSSERVICE
1101  }
1102 #endif
1103 
1104  return rc;
1105 }
1106 
1107 
1108 int Wallet::renameEntry(const QString& oldName, const QString& newName) {
1109  int rc = -1;
1110 
1111 #ifdef HAVE_KSECRETSSERVICE
1112  if (walletLauncher->m_useKSecretsService) {
1113  QExplicitlySharedDataPointer<KSecretsService::SecretItem> item = d->findItem(oldName);
1114  if (item) {
1115  KSecretsService::WriteItemPropertyJob *writeJob = item->setLabel(newName);
1116  if (!writeJob->exec()) {
1117  kDebug(285) << "Cannot exec WriteItemPropertyJob : " << writeJob->errorString();
1118  }
1119  rc = writeJob->error();
1120  }
1121  else {
1122  kDebug(285) << "Cannot locate item " << oldName << " in folder " << d->folder;
1123  }
1124  }
1125  else {
1126 #endif
1127  if (d->handle == -1) {
1128  return rc;
1129  }
1130 
1131  QDBusReply<int> r = walletLauncher->getInterface().renameEntry(d->handle, d->folder, oldName, newName, appid());
1132  if (r.isValid()) {
1133  rc = r;
1134  }
1135 #ifdef HAVE_KSECRETSSERVICE
1136  }
1137 #endif
1138 
1139  return rc;
1140 }
1141 
1142 
1143 int Wallet::readMap(const QString& key, QMap<QString,QString>& value) {
1144  int rc = -1;
1145 
1146 #ifdef HAVE_KSECRETSSERVICE
1147  if (walletLauncher->m_useKSecretsService) {
1148  QByteArray ba;
1149  rc = d->readEntry< QByteArray >(key, ba);
1150  if ( rc == 0 && !ba.isEmpty()){
1151  QDataStream ds( &ba, QIODevice::ReadOnly );
1152  ds >> value;
1153  }
1154  }
1155  else {
1156 #endif
1157  registerTypes();
1158 
1159  if (d->handle == -1) {
1160  return rc;
1161  }
1162 
1163  QDBusReply<QByteArray> r = walletLauncher->getInterface().readMap(d->handle, d->folder, key, appid());
1164  if (r.isValid()) {
1165  rc = 0;
1166  QByteArray v = r;
1167  if (!v.isEmpty()) {
1168  QDataStream ds(&v, QIODevice::ReadOnly);
1169  ds >> value;
1170  }
1171  }
1172 #ifdef HAVE_KSECRETSSERVICE
1173  }
1174 #endif
1175 
1176  return rc;
1177 }
1178 
1179 #ifdef HAVE_KSECRETSSERVICE
1180 struct Wallet::WalletPrivate::InsertIntoMapList {
1181  InsertIntoMapList( QMap< QString, QMap< QString, QString > > &value ) : _value( value ) {}
1182  bool operator() ( Wallet::WalletPrivate* d, const QString& label, KSecretsService::SecretItem* ) {
1183  bool result = false;
1184  QMap<QString, QString> map;
1185  if ( d->readEntry< QMap< QString, QString> >(label, map) ) {
1186  _value.insert( label, map );
1187  result = true;
1188  }
1189  return result;
1190  }
1191  QMap< QString, QMap< QString, QString> > &_value;
1192 };
1193 #endif
1194 
1195 int Wallet::readMapList(const QString& key, QMap<QString, QMap<QString, QString> >& value) {
1196  int rc = -1;
1197 
1198 #ifdef HAVE_KSECRETSSERVICE
1199  if (walletLauncher->m_useKSecretsService) {
1200  rc = d->forEachItemThatMatches( key, WalletPrivate::InsertIntoMapList( value ) );
1201  }
1202  else {
1203 #endif
1204  registerTypes();
1205 
1206  if (d->handle == -1) {
1207  return rc;
1208  }
1209 
1210  QDBusReply<QVariantMap> r =
1211  walletLauncher->getInterface().readMapList(d->handle, d->folder, key, appid());
1212  if (r.isValid()) {
1213  rc = 0;
1214  const QVariantMap val = r.value();
1215  for( QVariantMap::const_iterator it = val.begin(); it != val.end(); ++it ) {
1216  QByteArray mapData = it.value().toByteArray();
1217  if (!mapData.isEmpty()) {
1218  QDataStream ds(&mapData, QIODevice::ReadOnly);
1219  QMap<QString,QString> v;
1220  ds >> v;
1221  value.insert(it.key(), v);
1222  }
1223  }
1224  }
1225 #ifdef HAVE_KSECRETSSERVICE
1226  }
1227 #endif
1228 
1229  return rc;
1230 }
1231 
1232 
1233 int Wallet::readPassword(const QString& key, QString& value) {
1234  int rc = -1;
1235 
1236 #ifdef HAVE_KSECRETSSERVICE
1237  if (walletLauncher->m_useKSecretsService) {
1238  rc = d->readEntry<QString>(key, value);
1239  }
1240  else {
1241 #endif
1242  if (d->handle == -1) {
1243  return rc;
1244  }
1245 
1246  QDBusReply<QString> r = walletLauncher->getInterface().readPassword(d->handle, d->folder, key, appid());
1247  if (r.isValid()) {
1248  value = r;
1249  rc = 0;
1250  }
1251 #ifdef HAVE_KSECRETSSERVICE
1252  }
1253 #endif
1254 
1255  return rc;
1256 }
1257 
1258 #ifdef HAVE_KSECRETSSERVICE
1259 struct Wallet::WalletPrivate::InsertIntoPasswordList {
1260  InsertIntoPasswordList( QMap< QString, QString> &value ) : _value( value ) {}
1261  bool operator() ( Wallet::WalletPrivate* d, const QString& label, KSecretsService::SecretItem* ) {
1262  bool result = false;
1263  QString pwd;
1264  if ( d->readEntry<QString>( label, pwd ) == 0 ) {
1265  _value.insert( label, pwd );
1266  result = true;
1267  }
1268  return result;
1269  }
1270  QMap< QString, QString > &_value;
1271 };
1272 #endif
1273 
1274 int Wallet::readPasswordList(const QString& key, QMap<QString, QString>& value) {
1275  int rc = -1;
1276 
1277 #ifdef HAVE_KSECRETSSERVICE
1278  if (walletLauncher->m_useKSecretsService) {
1279  rc = d->forEachItemThatMatches( key, WalletPrivate::InsertIntoPasswordList( value ) );
1280  }
1281  else {
1282 #endif
1283  registerTypes();
1284 
1285  if (d->handle == -1) {
1286  return rc;
1287  }
1288 
1289  QDBusReply<QVariantMap> r = walletLauncher->getInterface().readPasswordList(d->handle, d->folder, key, appid());
1290  if (r.isValid()) {
1291  rc = 0;
1292  const QVariantMap val = r.value();
1293  for( QVariantMap::const_iterator it = val.begin(); it != val.end(); ++it ) {
1294  value.insert(it.key(), it.value().toString());
1295  }
1296  }
1297 #ifdef HAVE_KSECRETSSERVICE
1298  }
1299 #endif
1300 
1301  return rc;
1302 }
1303 
1304 
1305 int Wallet::writeEntry(const QString& key, const QByteArray& value, EntryType entryType) {
1306  int rc = -1;
1307 
1308 #ifdef HAVE_KSECRETSSERVICE
1309  if (walletLauncher->m_useKSecretsService) {
1310  rc = d->writeEntry( key, value, entryType );
1311  }
1312  else {
1313 #endif
1314  if (d->handle == -1) {
1315  return rc;
1316  }
1317 
1318  QDBusReply<int> r = walletLauncher->getInterface().writeEntry(d->handle, d->folder, key, value, int(entryType), appid());
1319  if (r.isValid()) {
1320  rc = r;
1321  }
1322 #ifdef HAVE_KSECRETSSERVICE
1323  }
1324 #endif
1325 
1326  return rc;
1327 }
1328 
1329 
1330 int Wallet::writeEntry(const QString& key, const QByteArray& value) {
1331  int rc = -1;
1332 
1333 #ifdef HAVE_KSECRETSSERVICE
1334  if (walletLauncher->m_useKSecretsService) {
1335  rc = writeEntry( key, value, Stream );
1336  }
1337  else {
1338 #endif
1339  if (d->handle == -1) {
1340  return rc;
1341  }
1342 
1343  QDBusReply<int> r = walletLauncher->getInterface().writeEntry(d->handle, d->folder, key, value, appid());
1344  if (r.isValid()) {
1345  rc = r;
1346  }
1347 #ifdef HAVE_KSECRETSSERVICE
1348  }
1349 #endif
1350 
1351  return rc;
1352 }
1353 
1354 
1355 int Wallet::writeMap(const QString& key, const QMap<QString,QString>& value) {
1356  int rc = -1;
1357 
1358 #ifdef HAVE_KSECRETSSERVICE
1359  if (walletLauncher->m_useKSecretsService) {
1360  d->writeEntry( key, value, Map );
1361  }
1362  else {
1363 #endif
1364  registerTypes();
1365 
1366  if (d->handle == -1) {
1367  return rc;
1368  }
1369 
1370  QByteArray mapData;
1371  QDataStream ds(&mapData, QIODevice::WriteOnly);
1372  ds << value;
1373  QDBusReply<int> r = walletLauncher->getInterface().writeMap(d->handle, d->folder, key, mapData, appid());
1374  if (r.isValid()) {
1375  rc = r;
1376  }
1377 #ifdef HAVE_KSECRETSSERVICE
1378  }
1379 #endif
1380 
1381  return rc;
1382 }
1383 
1384 
1385 int Wallet::writePassword(const QString& key, const QString& value) {
1386  int rc = -1;
1387 
1388 #ifdef HAVE_KSECRETSSERVICE
1389  if (walletLauncher->m_useKSecretsService) {
1390  rc = d->writeEntry( key, value, Password );
1391  }
1392  else {
1393 #endif
1394  if (d->handle == -1) {
1395  return rc;
1396  }
1397 
1398  QDBusReply<int> r = walletLauncher->getInterface().writePassword(d->handle, d->folder, key, value, appid());
1399  if (r.isValid()) {
1400  rc = r;
1401  }
1402 #ifdef HAVE_KSECRETSSERVICE
1403  }
1404 #endif
1405 
1406  return rc;
1407 }
1408 
1409 
1410 bool Wallet::hasEntry(const QString& key) {
1411 #ifdef HAVE_KSECRETSSERVICE
1412  if (walletLauncher->m_useKSecretsService) {
1413  QExplicitlySharedDataPointer<KSecretsService::SecretItem> item = d->findItem( key );
1414  return item;
1415  }
1416  else {
1417 #endif
1418  if (d->handle == -1) {
1419  return false;
1420  }
1421 
1422  QDBusReply<bool> r = walletLauncher->getInterface().hasEntry(d->handle, d->folder, key, appid());
1423  if (!r.isValid())
1424  {
1425  kDebug(285) << "Invalid DBus reply: " << r.error();
1426  return false;
1427  }
1428  else
1429  return r;
1430 #ifdef HAVE_KSECRETSSERVICE
1431  }
1432 #endif
1433 }
1434 
1435 
1436 int Wallet::removeEntry(const QString& key) {
1437  int rc = -1;
1438 
1439 #ifdef HAVE_KSECRETSSERVICE
1440  if (walletLauncher->m_useKSecretsService) {
1441  QExplicitlySharedDataPointer<KSecretsService::SecretItem> item = d->findItem( key );
1442  if ( item ) {
1443  KSecretsService::SecretItemDeleteJob *deleteJob = item->deleteItem();
1444  if ( !deleteJob->exec() ) {
1445  kDebug(285) << "Cannot execute SecretItemDeleteJob " << deleteJob->errorString();
1446  }
1447  rc = deleteJob->error();
1448  }
1449  }
1450  else {
1451 #endif
1452  if (d->handle == -1) {
1453  return rc;
1454  }
1455 
1456  QDBusReply<int> r = walletLauncher->getInterface().removeEntry(d->handle, d->folder, key, appid());
1457  if (r.isValid()) {
1458  rc = r;
1459  }
1460 #ifdef HAVE_KSECRETSSERVICE
1461  }
1462 #endif
1463 
1464  return rc;
1465 }
1466 
1467 
1468 Wallet::EntryType Wallet::entryType(const QString& key) {
1469  int rc = 0;
1470 
1471 #ifdef HAVE_KSECRETSSERVICE
1472  if (walletLauncher->m_useKSecretsService) {
1473  QExplicitlySharedDataPointer<KSecretsService::SecretItem> item = d->findItem( key );
1474  if ( item ) {
1475  KSecretsService::ReadItemPropertyJob *readAttrsJob = item->attributes();
1476  if ( readAttrsJob->exec() ) {
1477  KSecretsService::StringStringMap attrs = readAttrsJob->propertyValue().value<KSecretsService::StringStringMap>();
1478  if ( attrs.contains( KSS_ATTR_WALLETTYPE ) ) {
1479  QString entryType = attrs[KSS_ATTR_WALLETTYPE];
1480  bool ok = false;
1481  rc = entryType.toInt( &ok );
1482  if ( !ok ) {
1483  rc = 0;
1484  kDebug(285) << KSS_ATTR_WALLETTYPE << " attribute holds non int value " << attrs[KSS_ATTR_WALLETTYPE];
1485  }
1486  }
1487  }
1488  else {
1489  kDebug(285) << "Cannot execute GetSecretItemSecretJob " << readAttrsJob->errorString();
1490  }
1491  }
1492  }
1493  else {
1494 #endif
1495  if (d->handle == -1) {
1496  return Wallet::Unknown;
1497  }
1498 
1499  QDBusReply<int> r = walletLauncher->getInterface().entryType(d->handle, d->folder, key, appid());
1500  if (r.isValid()) {
1501  rc = r;
1502  }
1503 #ifdef HAVE_KSECRETSSERVICE
1504  }
1505 #endif
1506  return static_cast<EntryType>(rc);
1507 }
1508 
1509 
1510 void Wallet::WalletPrivate::walletServiceUnregistered()
1511 {
1512  if (handle >= 0) {
1513  q->slotWalletClosed(handle);
1514  }
1515 }
1516 
1517 void Wallet::slotFolderUpdated(const QString& wallet, const QString& folder) {
1518 #ifdef HAVE_KSECRETSSERVICE
1519  if (walletLauncher->m_useKSecretsService) {
1520  // TODO: implement this
1521  Q_ASSERT(0);
1522  }
1523  else {
1524 #endif
1525  if (d->name == wallet) {
1526  emit folderUpdated(folder);
1527  }
1528 #ifdef HAVE_KSECRETSSERVICE
1529  }
1530 #endif
1531 }
1532 
1533 
1534 void Wallet::slotFolderListUpdated(const QString& wallet) {
1535 #ifdef HAVE_KSECRETSSERVICE
1536  if (walletLauncher->m_useKSecretsService) {
1537  // TODO: implement this
1538  Q_ASSERT(0);
1539  }
1540  else {
1541 #endif
1542  if (d->name == wallet) {
1543  emit folderListUpdated();
1544  }
1545 #ifdef HAVE_KSECRETSSERVICE
1546  }
1547 #endif
1548 }
1549 
1550 
1551 void Wallet::slotApplicationDisconnected(const QString& wallet, const QString& application) {
1552 #ifdef HAVE_KSECRETSSERVICE
1553  if (walletLauncher->m_useKSecretsService) {
1554  // TODO: implement this
1555  Q_ASSERT(0);
1556  }
1557  else {
1558 #endif
1559  if (d->handle >= 0
1560  && d->name == wallet
1561  && application == appid()) {
1562  slotWalletClosed(d->handle);
1563  }
1564 #ifdef HAVE_KSECRETSSERVICE
1565  }
1566 #endif
1567 }
1568 
1569 void Wallet::walletAsyncOpened(int tId, int handle) {
1570 #ifdef HAVE_KSECRETSSERVICE
1571  if (walletLauncher->m_useKSecretsService) {
1572  // TODO: implement this
1573  Q_ASSERT(0);
1574  }
1575  else {
1576 #endif
1577  // ignore responses to calls other than ours
1578  if (d->transactionId != tId || d->handle != -1) {
1579  return;
1580  }
1581 
1582  // disconnect the async signal
1583  disconnect(this, SLOT(walletAsyncOpened(int,int)));
1584 
1585  d->handle = handle;
1586  emit walletOpened(handle > 0);
1587 #ifdef HAVE_KSECRETSSERVICE
1588  }
1589 #endif
1590 }
1591 
1592 void Wallet::emitWalletAsyncOpenError() {
1593  emit walletOpened(false);
1594 }
1595 
1596 void Wallet::emitWalletOpened() {
1597  emit walletOpened(true);
1598 }
1599 
1600 bool Wallet::folderDoesNotExist(const QString& wallet, const QString& folder)
1601 {
1602 #ifdef HAVE_KSECRETSSERVICE
1603  if (walletLauncher->m_useKSecretsService) {
1604  kDebug(285) << "WARNING: changing semantics of folderDoesNotExist with KSS: will prompt for the password";
1605  Wallet *w = openWallet( wallet, 0, Synchronous );
1606  if ( w ) {
1607  return !w->hasFolder( folder );
1608  }
1609  else {
1610  return true;
1611  }
1612  }
1613  else {
1614 #endif
1615  QDBusReply<bool> r = walletLauncher->getInterface().folderDoesNotExist(wallet, folder);
1616  if (!r.isValid())
1617  {
1618  kDebug(285) << "Invalid DBus reply: " << r.error();
1619  return false;
1620  }
1621  else
1622  return r;
1623 #ifdef HAVE_KSECRETSSERVICE
1624  }
1625 #endif
1626 }
1627 
1628 
1629 bool Wallet::keyDoesNotExist(const QString& wallet, const QString& folder, const QString& key)
1630 {
1631 #ifdef HAVE_KSECRETSSERVICE
1632  if (walletLauncher->m_useKSecretsService) {
1633  kDebug(285) << "WARNING: changing semantics of keyDoesNotExist with KSS: will prompt for the password";
1634  Wallet *w = openWallet( wallet, 0, Synchronous );
1635  if ( w ) {
1636  return !w->hasEntry(key);
1637  }
1638  return false;
1639  }
1640  else {
1641 #endif
1642  QDBusReply<bool> r = walletLauncher->getInterface().keyDoesNotExist(wallet, folder, key);
1643  if (!r.isValid())
1644  {
1645  kDebug(285) << "Invalid DBus reply: " << r.error();
1646  return false;
1647  }
1648  else
1649  return r;
1650 #ifdef HAVE_KSECRETSSERVICE
1651  }
1652 #endif
1653 }
1654 
1655 void Wallet::virtual_hook(int, void*) {
1656  //BASE::virtual_hook( id, data );
1657 }
1658 
1659 
1660 KWalletDLauncher::KWalletDLauncher()
1661  : m_wallet(0),
1662  m_cgroup(KSharedConfig::openConfig("kwalletrc", KConfig::NoGlobals)->group("Wallet"))
1663 {
1664  m_useKSecretsService = m_cgroup.readEntry("UseKSecretsService", false);
1665 #ifdef HAVE_KSECRETSSERVICE
1666  if (m_useKSecretsService) {
1667  // NOOP
1668  }
1669  else {
1670 #endif
1671  m_wallet = new org::kde::KWallet(QString::fromLatin1(s_kwalletdServiceName), "/modules/kwalletd", QDBusConnection::sessionBus());
1672 #ifdef HAVE_KSECRETSSERVICE
1673  }
1674 #endif
1675 }
1676 
1677 KWalletDLauncher::~KWalletDLauncher()
1678 {
1679  delete m_wallet;
1680 }
1681 
1682 org::kde::KWallet &KWalletDLauncher::getInterface()
1683 {
1684 // Q_ASSERT(!m_useKSecretsService);
1685  Q_ASSERT(m_wallet != 0);
1686 
1687  // check if kwalletd is already running
1688  if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(QString::fromLatin1(s_kwalletdServiceName)))
1689  {
1690  // not running! check if it is enabled.
1691  bool walletEnabled = m_cgroup.readEntry("Enabled", true);
1692  if (walletEnabled) {
1693  // wallet is enabled! try launching it
1694  QString error;
1695  int ret = KToolInvocation::startServiceByDesktopPath("kwalletd.desktop", QStringList(), &error);
1696  if (ret > 0)
1697  {
1698  kError(285) << "Couldn't start kwalletd: " << error << endl;
1699  }
1700 
1701  if
1702  (!QDBusConnection::sessionBus().interface()->isServiceRegistered(QString::fromLatin1(s_kwalletdServiceName))) {
1703  kDebug(285) << "The kwalletd service is still not registered";
1704  } else {
1705  kDebug(285) << "The kwalletd service has been registered";
1706  }
1707  } else {
1708  kError(285) << "The kwalletd service has been disabled";
1709  }
1710  }
1711 
1712  return *m_wallet;
1713 }
1714 
1715 } // namespace KWallet
1716 
1717 #include "kwallet.moc"
KWallet::Wallet::Synchronous
Definition: kwallet.h:135
KWallet::Wallet::hasEntry
virtual bool hasEntry(const QString &key)
Determine if the current folder has they entry key.
Definition: kwallet.cpp:1410
KWallet::Wallet::requestChangePassword
virtual void requestChangePassword(WId w)
Request to the wallet service to change the password of the current wallet.
Definition: kwallet.cpp:677
KWallet::Wallet::Wallet
Wallet(int handle, const QString &name)
Construct a KWallet object.
Definition: kwallet.cpp:243
KWallet::Wallet::walletName
virtual const QString & walletName() const
The name of the current wallet.
Definition: kwallet.cpp:658
readEntry
KAutostart::StartPhase readEntry(const KConfigGroup &group, const char *key, const KAutostart::StartPhase &aDefault)
KWallet::Wallet::setFolder
virtual bool setFolder(const QString &f)
Set the current working folder to f.
Definition: kwallet.cpp:879
KSharedConfig
KWallet::Wallet::Unknown
Definition: kwallet.h:80
KWallet::Wallet::renameEntry
virtual int renameEntry(const QString &oldName, const QString &newName)
Rename the entry oldName to newName.
Definition: kwallet.cpp:1108
kdebug.h
KWallet::appid
static QString appid()
Definition: kwallet.cpp:76
KWallet::Wallet
KDE Wallet.
Definition: kwallet.h:62
KWallet::Wallet::entryType
virtual EntryType entryType(const QString &key)
Determine the type of the entry key in this folder.
Definition: kwallet.cpp:1468
KWallet::Wallet::folderListUpdated
void folderListUpdated()
Emitted when the folder list is changed in this wallet.
T
#define T
KWallet::Wallet::virtual_hook
virtual void virtual_hook(int id, void *data)
Definition: kwallet.cpp:1655
StringToStringStringMapMap
QMap< QString, StringStringMap > StringToStringStringMapMap
Definition: kwallet_mac.cpp:47
group
KWallet::Wallet::Map
Definition: kwallet.h:80
KStandardShortcut::label
QString label(StandardShortcut id)
Returns a localized label for user-visible display.
Definition: kstandardshortcut.cpp:267
K_GLOBAL_STATIC
#define K_GLOBAL_STATIC(TYPE, NAME)
KStandardAction::name
const char * name(StandardAction id)
This will return the internal name of a given standard action.
Definition: kstandardaction.cpp:223
KWallet::Wallet::~Wallet
virtual ~Wallet()
Destroy a KWallet object.
Definition: kwallet.cpp:272
kError
static QDebug kError(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KWallet::Wallet::isEnabled
static bool isEnabled()
Determine if the KDE wallet is enabled.
Definition: kwallet.cpp:352
KWallet::Wallet::readMap
virtual int readMap(const QString &key, QMap< QString, QString > &value)
Read the map entry key from the current folder.
Definition: kwallet.cpp:1143
KWallet::Wallet::removeEntry
virtual int removeEntry(const QString &key)
Remove the entry key from the current folder.
Definition: kwallet.cpp:1436
KComponentData::aboutData
const KAboutData * aboutData() const
QString
StringByteArrayMap
QMap< QString, QByteArray > StringByteArrayMap
Definition: kwallet.cpp:52
KWallet::Wallet::readPasswordList
int readPasswordList(const QString &key, QMap< QString, QString > &value)
Read the password entry key from the current folder.
Definition: kwallet.cpp:1274
ktoolinvocation.h
QObject
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KWallet::Wallet::openWallet
static Wallet * openWallet(const QString &name, WId w, OpenType ot=Synchronous)
Open the wallet name.
Definition: kwallet.cpp:452
KWallet::Wallet::OpenType
OpenType
Definition: kwallet.h:135
KWallet::Wallet::writePassword
virtual int writePassword(const QString &key, const QString &value)
Write key = value as a password to the current folder.
Definition: kwallet.cpp:1385
KWallet::Wallet::walletOpened
void walletOpened(bool success)
Emitted when a wallet is opened in asynchronous mode.
KWallet::Wallet::Stream
Definition: kwallet.h:80
KWallet::Wallet::lockWallet
virtual int lockWallet()
This closes and locks the current wallet.
Definition: kwallet.cpp:621
KWallet::Wallet::hasFolder
virtual bool hasFolder(const QString &f)
Determine if the folder f exists in the wallet.
Definition: kwallet.cpp:817
kglobal.h
KToolInvocation::startServiceByDesktopPath
static int startServiceByDesktopPath(const QString &_name, const QString &URL, QString *error=0, QString *serviceName=0, int *pid=0, const QByteArray &startup_id=QByteArray(), bool noWait=false)
KWallet::Wallet::folderUpdated
void folderUpdated(const QString &folder)
Emitted when a folder in this wallet is updated.
KWallet::Wallet::removeFolder
virtual bool removeFolder(const QString &f)
Remove the folder f and all its entries from the wallet.
Definition: kwallet.cpp:914
KWallet::Wallet::readPassword
virtual int readPassword(const QString &key, QString &value)
Read the password entry key from the current folder.
Definition: kwallet.cpp:1233
KWallet::Wallet::entryList
virtual QStringList entryList()
Return the list of keys of all entries in this folder.
Definition: kwallet.cpp:774
QStringList
KWallet::Wallet::readEntryList
int readEntryList(const QString &key, QMap< QString, QByteArray > &value)
Read the entries matching key from the current folder.
Definition: kwallet.cpp:1075
KWallet::Wallet::Asynchronous
Definition: kwallet.h:135
KAboutData
ksharedconfig.h
KComponentData::componentName
QString componentName() const
KWallet::Wallet::walletList
static QStringList walletList()
List all the wallets available.
Definition: kwallet.cpp:299
KWallet::Wallet::disconnectApplication
static bool disconnectApplication(const QString &wallet, const QString &app)
Disconnect the application app from wallet.
Definition: kwallet.cpp:557
KWallet::Wallet::walletClosed
void walletClosed()
Emitted when this wallet is closed.
KWallet::Wallet::readEntry
virtual int readEntry(const QString &key, QByteArray &value)
Read the entry key from the current folder.
Definition: kwallet.cpp:1031
KWallet::Wallet::changePassword
static void changePassword(const QString &name, WId w)
Request to the wallet service to change the password of the wallet name.
Definition: kwallet.cpp:328
KStandardGuiItem::ok
KGuiItem ok()
Returns the 'Ok' gui item.
Definition: kstandardguiitem.cpp:107
KSS_ATTR_ENTRYFOLDER
#define KSS_ATTR_ENTRYFOLDER
NOTE: KSecretsService folder semantics The KWallet API uses folders for organising items...
Definition: kwallet.h:46
KWindowSystem::allowExternalProcessWindowActivation
static void allowExternalProcessWindowActivation(int pid=-1)
Allows a window from another process to raise and activate itself.
Definition: kwindowsystem_mac.cpp:622
KAboutData::programName
QString programName() const
KConfigGroup
KConfig
KWallet::Wallet::users
static QStringList users(const QString &wallet)
List the applications that are using the wallet wallet.
Definition: kwallet.cpp:580
KWallet::Wallet::Path
Definition: kwallet.h:135
kwallet.h
KWallet::Wallet::readMapList
int readMapList(const QString &key, QMap< QString, QMap< QString, QString > > &value)
Read the map entry key from the current folder.
Definition: kwallet.cpp:1195
KWallet::Wallet::createFolder
virtual bool createFolder(const QString &f)
Created the folder f.
Definition: kwallet.cpp:846
KWallet::Wallet::sync
virtual int sync()
This syncs the wallet file on disk with what is in memory.
Definition: kwallet.cpp:602
KWallet::Wallet::closeWallet
static int closeWallet(const QString &name, bool force)
Close the wallet name.
Definition: kwallet.cpp:403
KWallet::Wallet::folderList
virtual QStringList folderList()
Obtain the list of all folders contained in the wallet.
Definition: kwallet.cpp:724
KComponentData::isValid
bool isValid() const
KGlobal::mainComponent
const KComponentData & mainComponent()
KGlobal::hasMainComponent
bool hasMainComponent()
KWallet::Wallet::keyDoesNotExist
static bool keyDoesNotExist(const QString &wallet, const QString &folder, const QString &key)
Determine if an entry in a folder does not exist in a wallet.
Definition: kwallet.cpp:1629
StringStringMap
QMap< QString, QString > StringStringMap
Definition: kwallet_mac.cpp:45
kwindowsystem.h
KWallet::Wallet::deleteWallet
static int deleteWallet(const QString &name)
Delete the wallet name.
Definition: kwallet.cpp:426
appid
static QString appid()
Definition: kwallet_mac.cpp:76
KWallet::registerTypes
static void registerTypes()
Definition: kwallet.cpp:91
kaboutdata.h
KSS_ATTR_WALLETTYPE
#define KSS_ATTR_WALLETTYPE
Definition: kwallet.h:47
kcomponentdata.h
KSharedConfig::openConfig
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, const char *resourceType="config")
KWallet::s_kwalletdServiceName
static const char s_kwalletdServiceName[]
Definition: kwallet.cpp:241
KWallet::Wallet::currentFolder
virtual const QString & currentFolder() const
Determine the current working folder in the wallet.
Definition: kwallet.cpp:965
KConfigGroup::readEntry
T readEntry(const QString &key, const T &aDefault) const
KJob
KWallet::Wallet::writeEntry
virtual int writeEntry(const QString &key, const QByteArray &value, EntryType entryType)
Write key = value as a binary entry to the current folder.
Definition: kwallet.cpp:1305
KWallet::Wallet::EntryType
EntryType
Definition: kwallet.h:80
KComponentData
KWallet::Wallet::isOpen
virtual bool isOpen() const
Determine if the current wallet is open, and is a valid wallet handle.
Definition: kwallet.cpp:663
QMap
KWallet::Wallet::folderDoesNotExist
static bool folderDoesNotExist(const QString &wallet, const QString &folder)
Determine if a folder does not exist in a wallet.
Definition: kwallet.cpp:1600
kconfiggroup.h
KWallet::Wallet::writeMap
virtual int writeMap(const QString &key, const QMap< QString, QString > &value)
Write key = value as a map to the current folder.
Definition: kwallet.cpp:1355
KWallet::Wallet::Password
Definition: kwallet.h:80
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:16 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal