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

Nepomuk-Core

  • KTp
wallet-interface.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of telepathy-accounts-kcm
3  *
4  * Copyright (C) 2011 David Edmundson <kde@davidedmundson.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "wallet-interface.h"
22 #include "pending-wallet.h"
23 
24 #include <KDebug>
25 #include <KGlobal>
26 
27 
28 class KTp::WalletInterfacePrivate : public QObject
29 {
30  Q_OBJECT
31 
32 public:
33  WalletInterfacePrivate();
34  void ensureWalletIsReady();
35 
36  QScopedPointer<KWallet::Wallet> wallet;
37  static const QLatin1String folderName;
38  static const QLatin1String mapsPrefix;
39 
40  bool isOpening;
41 
42 private Q_SLOTS:
43  void onWalletOpened(bool success);
44 };
45 
46 using KTp::WalletInterface;
47 using KTp::WalletInterfacePrivate;
48 
49 const QLatin1String WalletInterfacePrivate::folderName = QLatin1String("telepathy-kde");
50 const QLatin1String WalletInterfacePrivate::mapsPrefix = QLatin1String("maps/");
51 
52 void WalletInterfacePrivate::ensureWalletIsReady()
53 {
54  // If wallet was force-closed since last WalletInterface::openWallet(),
55  // try to reopen it.
56  if (!wallet || !wallet->isOpen()) {
57  // If the wallet is not already being opened, we try to open it
58  if (!isOpening) {
59  isOpening = true;
60  wallet.reset(KWallet::Wallet::openWallet(KWallet::Wallet::NetworkWallet(), 0, KWallet::Wallet::Asynchronous));
61  connect(wallet.data(), SIGNAL(walletOpened(bool)), SLOT(onWalletOpened(bool)));
62  }
63  }
64 }
65 
66 
67 WalletInterfacePrivate::WalletInterfacePrivate() :
68  wallet(NULL),
69  isOpening(false)
70 {
71  ensureWalletIsReady();
72 }
73 
74 KTp::PendingWallet* WalletInterface::openWallet()
75 {
76  K_GLOBAL_STATIC(KTp::WalletInterface, s_instance);
77 
78  s_instance->d->ensureWalletIsReady();
79  return new PendingWallet(s_instance);
80 }
81 
82 void WalletInterfacePrivate::onWalletOpened(bool success)
83 {
84  if (!success) {
85  kWarning() << "Couldn't open wallet";
86  }
87 
88  disconnect(wallet.data(), SIGNAL(walletOpened(bool)), this, SLOT(onWalletOpened(bool)));
89  isOpening = false;
90 }
91 
92 
93 WalletInterface::WalletInterface():
94  d(new WalletInterfacePrivate)
95 {
96 }
97 
98 WalletInterface::~WalletInterface()
99 {
100  delete d;
101 }
102 
103 bool WalletInterface::hasPassword(const Tp::AccountPtr &account)
104 {
105  if (d->wallet.isNull() || !d->wallet->hasFolder(d->folderName)) {
106  return false;
107  }
108 
109  d->wallet->setFolder(d->folderName);
110  return d->wallet->hasEntry(account->uniqueIdentifier());
111 }
112 
113 QString WalletInterface::password(const Tp::AccountPtr &account)
114 {
115  if (d->wallet.isNull() || !d->wallet->hasFolder(d->folderName)) {
116  return QString();
117  }
118 
119  d->wallet->setFolder(d->folderName);
120  QString password;
121  if (d->wallet->hasEntry(account->uniqueIdentifier())) {
122  int rc = d->wallet->readPassword(account->uniqueIdentifier(), password);
123  if (rc != 0) {
124  password.clear();
125  kWarning() << "failed to read password from KWallet";
126  }
127  }
128  return password;
129 }
130 
131 void WalletInterface::setPassword(const Tp::AccountPtr &account, const QString &password)
132 {
133  if (d->wallet.isNull()) {
134  return;
135  }
136 
137  if (!d->wallet->hasFolder(d->folderName)) {
138  d->wallet->createFolder(d->folderName);
139  }
140 
141  d->wallet->setFolder(d->folderName);
142  d->wallet->writePassword(account->uniqueIdentifier(), password);
143 
144  setLastLoginFailed(account, false);
145 
146  //sync normally happens on close, but in this case we need it to happen /now/ as it needs to be synced before the auth-client starts
147  d->wallet->sync();
148 }
149 
150 void WalletInterface::setLastLoginFailed(const Tp::AccountPtr &account, bool failed)
151 {
152  if (failed) {
153  setEntry(account, QLatin1String("lastLoginFailed"), QLatin1String("true"));
154  } else {
155  if (hasEntry(account, QLatin1String("lastLoginFailed"))) {
156  removeEntry(account, QLatin1String("lastLoginFailed"));
157  }
158  }
159 }
160 
161 bool WalletInterface::lastLoginFailed(const Tp::AccountPtr &account)
162 {
163  if (d->wallet.isNull()) {
164  return false;
165  }
166  return hasEntry(account, QLatin1String("lastLoginFailed"));
167 }
168 
169 void WalletInterface::removePassword(const Tp::AccountPtr &account)
170 {
171  if (d->wallet.isNull() || !d->wallet->hasFolder(d->folderName)) {
172  return;
173  }
174 
175  d->wallet->setFolder(d->folderName);
176  d->wallet->removeEntry(account->uniqueIdentifier());
177  d->wallet->sync();
178 }
179 
180 bool WalletInterface::hasEntry(const Tp::AccountPtr &account, const QString &key)
181 {
182  if (d->wallet.isNull() || !d->wallet->hasFolder(d->folderName)) {
183  return false;
184  }
185 
186  d->wallet->setFolder(d->folderName);
187  QMap< QString, QString > map;
188  if (d->wallet->hasEntry(d->mapsPrefix + account->uniqueIdentifier())) {
189  int rc = d->wallet->readMap(d->mapsPrefix + account->uniqueIdentifier(), map);
190  if (rc != 0) {
191  kWarning() << "failed to read map from KWallet (probably it is not a map)";
192  return false;
193  }
194  }
195  return map.contains(key);
196 }
197 
198 QString WalletInterface::entry(const Tp::AccountPtr &account, const QString &key)
199 {
200  if (d->wallet.isNull() || !d->wallet->hasFolder(d->folderName)) {
201  return QString();
202  }
203 
204  d->wallet->setFolder(d->folderName);
205  QString value;
206  QMap< QString, QString > map;
207  if (d->wallet->hasEntry(d->mapsPrefix + account->uniqueIdentifier())) {
208  int rc = d->wallet->readMap(d->mapsPrefix + account->uniqueIdentifier(), map);
209  if (rc != 0) {
210  kWarning() << "failed to read map from KWallet (probably it is not a map)";
211  return QString();
212  }
213  }
214  return map.value(key);
215 }
216 
217 void WalletInterface::setEntry(const Tp::AccountPtr &account, const QString &key, const QString &value)
218 {
219  if (d->wallet.isNull()) {
220  return;
221  }
222 
223  if (! d->wallet->hasFolder(d->folderName)) {
224  d->wallet->createFolder(d->folderName);
225  }
226 
227  d->wallet->setFolder(d->folderName);
228  QMap< QString, QString > map;
229  if (d->wallet->hasEntry(d->mapsPrefix + account->uniqueIdentifier())) {
230  int rc = d->wallet->readMap(d->mapsPrefix + account->uniqueIdentifier(), map);
231  if (rc != 0) {
232  kWarning() << "failed to read map from KWallet (probably it is not a map)";
233  return;
234  }
235  }
236  map[key] = value;
237 
238  d->wallet->writeMap(d->mapsPrefix + account->uniqueIdentifier(), map);
239  //sync normally happens on close, but in this case we need it to happen /now/ as it needs to be synced before the auth-client starts
240  d->wallet->sync();
241 }
242 
243 void WalletInterface::removeEntry(const Tp::AccountPtr &account, const QString &key)
244 {
245  if (d->wallet.isNull() || !d->wallet->hasFolder(d->folderName)) {
246  return;
247  }
248 
249  d->wallet->setFolder(d->folderName);
250  QMap< QString, QString > map;
251  if (d->wallet->hasEntry(d->mapsPrefix + account->uniqueIdentifier())) {
252  int rc = d->wallet->readMap(d->mapsPrefix + account->uniqueIdentifier(), map);
253  if (rc != 0) {
254  kWarning() << "failed to read map from KWallet (probably it is not a map)";
255  return;
256  }
257  }
258  map.remove(key);
259 
260  if (!map.empty()) {
261  d->wallet->writeMap(d->mapsPrefix + account->uniqueIdentifier(), map);
262  } else {
263  d->wallet->removeEntry(d->mapsPrefix + account->uniqueIdentifier());
264  }
265  //sync normally happens on close, but in this case we need it to happen /now/ as it needs to be synced before the auth-client starts
266  d->wallet->sync();
267 }
268 
269 void WalletInterface::removeAllEntries(const Tp::AccountPtr& account)
270 {
271  if (d->wallet.isNull() || !d->wallet->hasFolder(d->folderName)) {
272  return;
273  }
274 
275  d->wallet->setFolder(d->folderName);
276  d->wallet->removeEntry(d->mapsPrefix + account->uniqueIdentifier());
277 }
278 
279 void WalletInterface::removeAccount(const Tp::AccountPtr& account)
280 {
281  removePassword(account);
282  removeAllEntries(account);
283 }
284 
285 bool WalletInterface::isOpen()
286 {
287  return (!d->wallet.isNull() && d->wallet->isOpen());
288 }
289 
290 KWallet::Wallet *WalletInterface::wallet() const
291 {
292  return d->wallet.data();
293 }
294 
295 #include "wallet-interface.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Fri Mar 22 2013 10:58:52 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

ktp-common-internals API Reference

Skip menu "ktp-common-internals API Reference"
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