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

kwallet

  • sources
  • kde-4.14
  • kdeutils
  • kwalletmanager
  • src
  • manager
kwalletmanager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003,2004 George Staikos <staikos@kde.org>
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; see the file COPYING. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 
21 #include "kwalletmanager.h"
22 #include "kwalletmanagerwidget.h"
23 #include "kwalletpopup.h"
24 #include "kwalleteditor.h"
25 #include "allyourbase.h"
26 #include "kwallet_interface.h"
27 #include "registercreateactionmethod.h"
28 
29 #include <kaction.h>
30 #include <kapplication.h>
31 #include <kconfig.h>
32 #include <kdebug.h>
33 #include <kiconloader.h>
34 #include <kinputdialog.h>
35 #include <klocale.h>
36 #include <kmessagebox.h>
37 #include <kstandarddirs.h>
38 #include <kstandardaction.h>
39 #include <kstatusnotifieritem.h>
40 #include <kwallet.h>
41 #include <kxmlguifactory.h>
42 #include <QPointer>
43 #include <QRegExp>
44 
45 #include <QRegExpValidator>
46 #include <QTimer>
47 #include <ktoolinvocation.h>
48 #include <kicon.h>
49 #include <kactioncollection.h>
50 #include <kconfiggroup.h>
51 
52 KWalletManager::KWalletManager(QWidget *parent, const char *name, Qt::WFlags f)
53  : KXmlGuiWindow(parent, f)
54 {
55  RegisterCreateActionsMethod::createActions(actionCollection());
56 
57  setObjectName(QLatin1String( name ) );
58  QDBusConnection::sessionBus().registerObject(QLatin1String( "/KWalletManager" ), this, QDBusConnection::ExportScriptableSlots);
59  KGlobal::dirs()->addResourceType("kwallet", 0, QLatin1String( "share/apps/kwallet" ));
60  _kwalletdLaunch = false;
61  _shuttingDown = false;
62  m_kwalletdModule = 0;
63  KConfig cfg( QLatin1String( "kwalletrc" )); // not sure why this setting isn't in kwalletmanagerrc...
64  KConfigGroup walletConfigGroup(&cfg, "Wallet");
65  if (walletConfigGroup.readEntry("Launch Manager", false)) {
66  _tray = new KStatusNotifierItem(this);
67  _tray->setObjectName( QLatin1String("kwalletmanager tray" ));
68  _tray->setCategory( KStatusNotifierItem::SystemServices );
69  _tray->setStatus( KStatusNotifierItem::Passive );
70  _tray->setIconByName(QLatin1String( "wallet-closed" ));
71  _tray->setToolTip( QLatin1String( "wallet-closed" ), i18n("KDE Wallet"), i18n("No wallets open."));
72  //connect(_tray, SIGNAL(quitSelected()), SLOT(shuttingDown()));
73  const QStringList wl = KWallet::Wallet::walletList();
74  bool isOpen = false;
75  for (QStringList::ConstIterator it = wl.begin(); it != wl.end(); ++it) {
76  if (KWallet::Wallet::isOpen(*it)) {
77  _tray->setIconByName(QLatin1String( "wallet-open" ));
78  _tray->setToolTip( QLatin1String( "wallet-open" ), i18n("KDE Wallet"), i18n("A wallet is open."));
79  isOpen = true;
80  break;
81  }
82  }
83  if (!isOpen && qApp->isSessionRestored()) {
84  delete _tray;
85  _tray = 0;
86  QTimer::singleShot( 0, qApp, SLOT(quit()));
87  return;
88  }
89  } else {
90  _tray = 0;
91  }
92 
93  _managerWidget = new KWalletManagerWidget(this);
94 
95  updateWalletDisplay();
96  setCentralWidget(_managerWidget);
97  setAutoSaveSettings(QLatin1String("MainWindow"), true);
98 // _managerWidget->setMinimumSize(320, 200);
99 
100  m_kwalletdModule = new org::kde::KWallet(QLatin1String( "org.kde.kwalletd" ), QLatin1String( "/modules/kwalletd" ), QDBusConnection::sessionBus());
101  connect(QDBusConnection::sessionBus().interface(),
102  SIGNAL(serviceOwnerChanged(QString,QString,QString)),
103  this,
104  SLOT(possiblyRescan(QString,QString,QString)));
105  connect( m_kwalletdModule, SIGNAL(allWalletsClosed()),
106  this, SLOT(allWalletsClosed()) );
107  connect( m_kwalletdModule, SIGNAL(walletClosed(QString)),
108  this, SLOT(updateWalletDisplay()) );
109  connect( m_kwalletdModule, SIGNAL(walletOpened(QString)),
110  this, SLOT(aWalletWasOpened()) );
111  connect( m_kwalletdModule, SIGNAL(walletDeleted(QString)),
112  this, SLOT(updateWalletDisplay()) );
113  connect( m_kwalletdModule, SIGNAL(walletListDirty()),
114  this, SLOT(updateWalletDisplay()) );
115  connect( m_kwalletdModule, SIGNAL(walletCreated(QString)), this, SLOT(walletCreated(QString)));
116  // FIXME: slight race - a wallet can open, then we get launched, but the
117  // wallet closes before we are done opening. We will then stay
118  // open. Must check that a wallet is still open here.
119 
120  QAction *action = actionCollection()->addAction(QLatin1String( "wallet_create" ));
121  action->setText(i18n("&New Wallet..."));
122  action->setIcon(KIcon( QLatin1String( "kwalletmanager" )));
123  connect(action, SIGNAL(triggered()), SLOT(createWallet()));
124 
125  action = actionCollection()->addAction(QLatin1String( "wallet_open") );
126  action->setText(i18n("Open Wallet..."));
127  connect(action, SIGNAL(triggered()), this, SLOT(openWallet()));
128 
129  action = actionCollection()->addAction(QLatin1String( "wallet_delete" ));
130  action->setText(i18n("&Delete Wallet..."));
131  action->setIcon(KIcon( QLatin1String( "trash-empty" )));
132  connect(action, SIGNAL(triggered()), SLOT(deleteWallet()));
133  QAction *act = actionCollection()->addAction(QLatin1String( "wallet_settings" ));
134  act->setText(i18n("Configure &Wallet..."));
135  act->setIcon(KIcon( QLatin1String( "configure" )));
136 
137  connect(act, SIGNAL(triggered()), SLOT(setupWallet()));
138  if (_tray) {
139  _tray->contextMenu()->addAction( act );
140  }
141  act = actionCollection()->addAction(QLatin1String( "close_all_wallets" ));
142  act->setText(i18n("Close &All Wallets"));
143  connect(act, SIGNAL(triggered()), SLOT(closeAllWallets()));
144  if (_tray) {
145  _tray->contextMenu()->addAction( act );
146  }
147  KStandardAction::quit(this, SLOT(shuttingDown()), actionCollection());
148  KStandardAction::keyBindings(guiFactory(), SLOT(configureShortcuts()),
149 actionCollection());
150 
151  setupGUI( Keys | Save | Create, QLatin1String( "kwalletmanager.rc" ));
152  setStandardToolBarMenuEnabled(false);
153 
154  if (_tray) {
155 // _tray->show();
156  } else {
157  show();
158  }
159 
160  qApp->setObjectName( QLatin1String("kwallet" )); // hack to fix docs
161 }
162 
163 
164 KWalletManager::~KWalletManager() {
165  _tray = 0L;
166  delete m_kwalletdModule;
167  m_kwalletdModule=0L;
168 }
169 
170 
171 void KWalletManager::kwalletdLaunch() {
172  _kwalletdLaunch = true;
173 }
174 
175 
176 bool KWalletManager::queryClose() {
177  if (!_shuttingDown && !kapp->sessionSaving()) {
178  if (!_tray) {
179  qApp->quit();
180  } else {
181  hide();
182  }
183  return false;
184  }
185  return true;
186 }
187 
188 
189 void KWalletManager::aWalletWasOpened() {
190  if (_tray) {
191  _tray->setIconByName(QLatin1String( "wallet-open" ));
192  _tray->setToolTip( QLatin1String( "wallet-open" ), i18n("KDE Wallet"), i18n("A wallet is open."));
193  _tray->setStatus(KStatusNotifierItem::Active);
194  }
195  updateWalletDisplay();
196  createGUI( QLatin1String( "kwalletmanager.rc" ));
197 }
198 
199 
200 void KWalletManager::updateWalletDisplay() {
201  _managerWidget->updateWalletDisplay();
202 }
203 
204 void KWalletManager::walletCreated(const QString& newWalletName)
205 {
206  _managerWidget->updateWalletDisplay(newWalletName);
207 }
208 
209 void KWalletManager::contextMenu(const QPoint& ) {
210 }
211 
212 
213 void KWalletManager::closeWallet(const QString& walletName) {
214  int rc = KWallet::Wallet::closeWallet(walletName, false);
215  if (rc != 0) {
216  rc = KMessageBox::warningYesNo(this, i18n("Unable to close wallet cleanly. It is probably in use by other applications. Do you wish to force it closed?"), QString(), KGuiItem(i18n("Force Closure")), KGuiItem(i18n("Do Not Force")));
217  if (rc == KMessageBox::Yes) {
218  rc = KWallet::Wallet::closeWallet(walletName, true);
219  if (rc != 0) {
220  KMessageBox::sorry(this, i18n("Unable to force the wallet closed. Error code was %1.", rc));
221  }
222  }
223  }
224 
225  updateWalletDisplay();
226 }
227 
228 void KWalletManager::changeWalletPassword(const QString &walletName)
229 {
230  KWallet::Wallet::changePassword(walletName, effectiveWinId());
231 }
232 
233 
234 void KWalletManager::openWalletFile(const QString& path) {
235  if (!_managerWidget->openWalletFile(path)) {
236  KMessageBox::sorry(this, i18n("Error opening wallet %1.", path));
237  }
238 }
239 
240 void KWalletManager::allWalletsClosed() {
241  if (_tray) {
242  _tray->setIconByName(QLatin1String( "wallet-closed" ));
243  _tray->setToolTip( QLatin1String( "wallet-closed" ), i18n("KDE Wallet"), i18n("No wallets open."));
244  _tray->setStatus(KStatusNotifierItem::Passive);
245  }
246  possiblyQuit();
247 }
248 
249 
250 void KWalletManager::possiblyQuit() {
251  KConfig _cfg( QLatin1String( "kwalletrc" ) );
252  KConfigGroup cfg(&_cfg, "Wallet");
253  if (_windows.isEmpty() &&
254  !isVisible() &&
255  !cfg.readEntry("Leave Manager Open", false) &&
256  _kwalletdLaunch) {
257  qApp->quit();
258  }
259 }
260 
261 
262 void KWalletManager::editorClosed(KXmlGuiWindow* e) {
263  _windows.removeAll(e);
264 }
265 
266 
267 void KWalletManager::possiblyRescan(const QString& app, const QString& oldOwner, const QString& newOwner) {
268  Q_UNUSED( oldOwner );
269  Q_UNUSED( newOwner );
270  if (app == QLatin1String( "org.kde.kwalletd" )) {
271  updateWalletDisplay();
272  }
273 }
274 
275 void KWalletManager::createWallet() {
276  QString n;
277  bool ok;
278  QString txt = i18n("Please choose a name for the new wallet:");
279  QRegExpValidator validator(QRegExp( QLatin1String( "^[\\w\\^\\&\\'\\@\\{\\}\\[\\]\\,\\$\\=\\!\\-\\#\\(\\)\\%\\.\\+\\_\\s]+$" )), this);
280 
281  if (!KWallet::Wallet::isEnabled()) {
282  // FIXME: KMessageBox::warningYesNo(this, i1_8n("KWallet is not enabled. Do you want to enable it?"), QString(), i18n("Enable"), i18n("Keep Disabled"));
283  return;
284  }
285 
286  do {
287  n = KInputDialog::getText(i18n("New Wallet"), txt, QString(), &ok, this,
288  &validator);
289 
290  if (!ok) {
291  return;
292  }
293 
294  if (_managerWidget->hasWallet(n)) {
295  int rc = KMessageBox::questionYesNo(this, i18n("Sorry, that wallet already exists. Try a new name?"), QString(), KGuiItem(i18n("Try New")), KGuiItem(i18n("Do Not Try")));
296  if (rc == KMessageBox::No) {
297  return;
298  }
299  n.clear();
300  } else {
301  break;
302  }
303  } while (true);
304 
305  // Small race here - the wallet could be created on us already.
306  if (!n.isEmpty()) {
307  // attempt open the wallet to create it, then dispose it
308  // as it'll appear in on the main window via the walletCreated signal
309  // emmitted by the kwalletd
310  KWallet::Wallet::openWallet(n, effectiveWinId());
311  }
312 }
313 
314 void KWalletManager::deleteWallet()
315 {
316  QString walletName = _managerWidget->activeWalletName();
317  int rc = KMessageBox::warningContinueCancel(this, i18n("Are you sure you wish to delete the wallet '%1'?", walletName),QString(),KStandardGuiItem::del());
318  if (rc != KMessageBox::Continue) {
319  return;
320  }
321  rc = KWallet::Wallet::deleteWallet(walletName);
322  if (rc != 0) {
323  KMessageBox::sorry(this, i18n("Unable to delete the wallet. Error code was %1.", rc));
324  }
325 }
326 
327 void KWalletManager::openWallet(const QString& walletName)
328 {
329  _managerWidget->openWallet(walletName);
330 }
331 
332 void KWalletManager::openWallet()
333 {
334  qWarning("TODO: implement openWallet from file");
335 }
336 
337 void KWalletManager::shuttingDown() {
338  _shuttingDown = true;
339  qApp->quit();
340 }
341 
342 
343 void KWalletManager::setupWallet() {
344  KToolInvocation::startServiceByDesktopName( QLatin1String( "kwalletconfig" ));
345 }
346 
347 
348 void KWalletManager::closeAllWallets() {
349  m_kwalletdModule->closeAllWallets();
350 }
351 
352 #include "kwalletmanager.moc"
KWalletManagerWidget::openWallet
bool openWallet(const QString &name)
Definition: kwalletmanagerwidget.cpp:109
KWalletManager::walletCreated
void walletCreated(const QString &walletName)
Definition: kwalletmanager.cpp:204
QAction::setText
void setText(const QString &text)
QWidget
QRegExpValidator
kwalleteditor.h
QAction::setIcon
void setIcon(const QIcon &icon)
QDBusConnection::registerObject
bool registerObject(const QString &path, QObject *object, QFlags< QDBusConnection::RegisterOption > options)
KWalletManager::openWallet
void openWallet(const QString &walletName)
Definition: kwalletmanager.cpp:327
QDBusConnection::sessionBus
QDBusConnection sessionBus()
QPoint
KWalletManager::deleteWallet
void deleteWallet()
Definition: kwalletmanager.cpp:314
KWalletManager::kwalletdLaunch
void kwalletdLaunch()
Definition: kwalletmanager.cpp:171
QString::clear
void clear()
kwalletpopup.h
QRegExp
RegisterCreateActionsMethod::createActions
static void createActions(KActionCollection *actionCollection)
Definition: registercreateactionmethod.cpp:32
KXmlGuiWindow
KWalletManager::updateWalletDisplay
Q_SCRIPTABLE void updateWalletDisplay()
Definition: kwalletmanager.cpp:200
KWalletManagerWidget::openWalletFile
bool openWalletFile(const QString &path)
Definition: kwalletmanagerwidget.cpp:102
KWalletManager::openWalletFile
void openWalletFile(const QString &path)
Definition: kwalletmanager.cpp:234
KWalletManager::queryClose
virtual bool queryClose()
Definition: kwalletmanager.cpp:176
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
QList::removeAll
int removeAll(const T &value)
QString
Qt::WFlags
typedef WFlags
QStringList
QList::end
iterator end()
KWalletManagerWidget::hasWallet
bool hasWallet(const QString &) const
Definition: kwalletmanagerwidget.cpp:97
KWalletManager::KWalletManager
KWalletManager(QWidget *parent=0, const char *name=0, Qt::WFlags f=0)
Definition: kwalletmanager.cpp:52
KWalletManager::changeWalletPassword
void changeWalletPassword(const QString &walletName)
Definition: kwalletmanager.cpp:228
kwalletmanagerwidget.h
KWalletManager::~KWalletManager
virtual ~KWalletManager()
Definition: kwalletmanager.cpp:164
registercreateactionmethod.h
QLatin1String
Yes
Definition: kwalleteditor.cpp:945
QAction
QList::ConstIterator
typedef ConstIterator
allyourbase.h
KWalletManager::allWalletsClosed
Q_SCRIPTABLE void allWalletsClosed()
Definition: kwalletmanager.cpp:240
kwalletmanager.h
KWalletManagerWidget::updateWalletDisplay
void updateWalletDisplay(QString selectWallet=QString())
Definition: kwalletmanagerwidget.cpp:52
KWalletManager::closeWallet
void closeWallet(const QString &walletName)
Definition: kwalletmanager.cpp:213
KWalletManagerWidget
Definition: kwalletmanagerwidget.h:29
KWalletManagerWidget::activeWalletName
const QString & activeWalletName() const
Definition: kwalletmanagerwidget.cpp:120
QList::begin
iterator begin()
No
Definition: kwalleteditor.cpp:945
KWalletManager::aWalletWasOpened
Q_SCRIPTABLE void aWalletWasOpened()
Definition: kwalletmanager.cpp:189
KWalletManager::contextMenu
void contextMenu(const QPoint &pos)
Definition: kwalletmanager.cpp:209
KWalletManager::createWallet
void createWallet()
Definition: kwalletmanager.cpp:275
QTimer::singleShot
singleShot
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:42:01 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kwallet

Skip menu "kwallet"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • ktimer
  • kwallet
  • sweeper

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