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

kopete/libkopete

  • sources
  • kde-4.14
  • kdenetwork
  • kopete
  • libkopete
kopetepassword.cpp
Go to the documentation of this file.
1 /*
2  kopetepassword.cpp - Kopete Password
3 
4  Copyright (c) 2004 by Richard Smith <kde@metafoo.co.uk>
5  Kopete (c) 2002-2004 by the Kopete developers <kopete-devel@kde.org>
6 
7  *************************************************************************
8  * *
9  * This library is free software; you can redistribute it and/or *
10  * modify it under the terms of the GNU Lesser General Public *
11  * License as published by the Free Software Foundation; either *
12  * version 2 of the License, or (at your option) any later version. *
13  * *
14  *************************************************************************
15 */
16 
17 #include "kopetepassword.h"
18 #include "kopeteuiglobal.h"
19 #include "kopetewalletmanager.h"
20 
21 #include <kwallet.h>
22 
23 #include <QApplication>
24 #include <QLabel>
25 #include <QLineEdit>
26 #include <QCheckBox>
27 #include <k3activelabel.h>
28 #include <kconfig.h>
29 #include <kdebug.h>
30 #include <kdialog.h>
31 #include <klocale.h>
32 #include <kmessagebox.h>
33 #include <kiconloader.h>
34 #include <kstringhandler.h>
35 #include <kpassworddialog.h>
36 
37 class Kopete::Password::Private
38 {
39 public:
40  Private( const QString &group, bool blanksAllowed )
41  : refCount( 1 ), configGroup( group ), remembered( false ),
42  isWrong( false ), allowBlankPassword( blanksAllowed )
43  {
44  }
45  Private *incRef()
46  {
47  ++refCount;
48  return this;
49  }
50  void decRef()
51  {
52  if( --refCount == 0 )
53  delete this;
54  }
56  int refCount;
58  const QString configGroup;
60  bool remembered;
62  QString passwordFromKConfig;
64  bool isWrong;
66  bool allowBlankPassword;
68  QString cachedValue;
69 };
70 
76 class KopetePasswordRequest : public KopetePasswordRequestBase
77 {
78 public:
79  KopetePasswordRequest( QObject *owner, Kopete::Password &pass )
80  : KopetePasswordRequestBase( owner ), mPassword( pass ), mWallet( 0 )
81  {
82  }
83 
87  void begin()
88  {
89  kDebug( 14010 );
90 
91  Kopete::WalletManager::self()->openWallet( this, SLOT(walletReceived(KWallet::Wallet*)) );
92  }
93 
94  void walletReceived( KWallet::Wallet *wallet )
95  {
96  kDebug( 14010 ) ;
97  mWallet = wallet;
98  processRequest();
99  }
100 
104  virtual void processRequest() = 0;
105 
106  void gotPassword(const QString&, bool) {}
107  void slotCancelPressed() {}
108 
109 protected:
110  Kopete::Password mPassword;
111  KWallet::Wallet *mWallet;
112 };
113 
119 class KopetePasswordGetRequest : public KopetePasswordRequest
120 {
121 public:
122  KopetePasswordGetRequest( QObject *owner, Kopete::Password &pass )
123  : KopetePasswordRequest( owner, pass )
124  {
125  }
126 
127  QString grabPassword()
128  {
129  // Before trying to read from the wallet, check if the config file holds a password.
130  // If so, remove it from the config and set it through KWallet instead.
131  QString pwd;
132 
133  if ( mPassword.d->remembered && !mPassword.d->passwordFromKConfig.isNull() )
134  {
135  pwd = mPassword.d->passwordFromKConfig;
136  mPassword.set( pwd );
137  return pwd;
138  }
139 
140  if ( mWallet && mWallet->readPassword( mPassword.d->configGroup, pwd ) == 0 && !pwd.isEmpty() )
141  return pwd;
142 
143  if ( mPassword.d->remembered && !mPassword.d->passwordFromKConfig.isEmpty() )
144  return mPassword.d->passwordFromKConfig;
145 
146  return QString();
147  }
148 
149  void finished( const QString &result )
150  {
151  mPassword.d->cachedValue = result;
152  emit requestFinished( result );
153  delete this;
154  }
155 };
156 
157 class KopetePasswordGetRequestPrompt : public KopetePasswordGetRequest
158 {
159 public:
160  KopetePasswordGetRequestPrompt( QObject *owner, Kopete::Password &pass, const QPixmap &image, const QString &prompt, Kopete::Password::PasswordSource source )
161  : KopetePasswordGetRequest( owner, pass ), mImage( image ), mPrompt( prompt ), mSource( source ), mView( 0 )
162  {
163  }
164 
165  void processRequest()
166  {
167  const QString result = grabPassword();
168  if ( mSource == Kopete::Password::FromUser || result.isNull() )
169  doPasswordDialog();
170  else
171  finished( result );
172  }
173 
174  void doPasswordDialog()
175  {
176  kDebug( 14010 ) ;
177 
178  KPasswordDialog *passwdDialog = new KPasswordDialog( Kopete::UI::Global::mainWidget(), KPasswordDialog::ShowKeepPassword );
179  passwdDialog->setWindowTitle( i18n( "Password Required" ) );
180  passwdDialog->setPrompt( mPrompt );
181  passwdDialog->setPixmap( mImage );
182 
183  connect( passwdDialog, SIGNAL(gotPassword(QString,bool)), SLOT(gotPassword(QString,bool))) ;
184  connect( passwdDialog, SIGNAL(rejected()), SLOT(slotCancelPressed()) );
185  connect( this, SIGNAL(destroyed()), passwdDialog, SLOT(deleteLater()) );
186  passwdDialog->show();
187  }
188 
189  void gotPassword(const QString& pass, bool keep)
190  {
191  if ( keep )
192  mPassword.set( pass );
193 
194  finished( pass );
195  }
196 
197  void slotCancelPressed()
198  {
199  finished( QString::null ); //krazy:exclude=nullstrassign for old broken gcc
200  }
201 
202 private:
203  QPixmap mImage;
204  QString mPrompt;
205  Kopete::Password::PasswordSource mSource;
206  QWidget *mView;
207 };
208 
209 class KopetePasswordGetRequestNoPrompt : public KopetePasswordGetRequest
210 {
211 public:
212  KopetePasswordGetRequestNoPrompt( QObject *owner, Kopete::Password &pass )
213  : KopetePasswordGetRequest( owner, pass )
214  {
215  }
216 
217  void processRequest()
218  {
219  finished( grabPassword() );
220  }
221 };
222 
228 class KopetePasswordSetRequest : public KopetePasswordRequest
229 {
230 public:
231  KopetePasswordSetRequest( Kopete::Password &pass, const QString &newPass )
232  : KopetePasswordRequest( 0, pass ), mNewPass( newPass )
233  {
234  KGlobal::ref();
235  }
236  ~KopetePasswordSetRequest()
237  {
238  KGlobal::deref();
239  kDebug( 14010 ) << "job complete";
240  }
241  void processRequest()
242  {
243  if ( setPassword() )
244  {
245  mPassword.setWrong( false );
246  mPassword.d->cachedValue = mNewPass;
247  }
248  delete this;
249  }
250  bool setPassword()
251  {
252  kDebug( 14010 ) << " setting password for " << mPassword.d->configGroup;
253 
254  if ( mWallet && mWallet->writePassword( mPassword.d->configGroup, mNewPass ) == 0 )
255  {
256  mPassword.d->remembered = true;
257  mPassword.d->passwordFromKConfig.clear();
258  mPassword.writeConfig();
259  return true;
260  }
261 
262  if ( KWallet::Wallet::isEnabled() )
263  {
264  // If we end up here, the wallet is enabled, but failed somehow.
265  // Ask the user what to do now.
266 
267  //NOTE: This will start a nested event loop. However, this is fine; the only code we
268  // call after this point is in Kopete::Password, so as long as we've not been deleted
269  // everything should work out OK. We have no parent QObject, so we should survive.
270  if ( KMessageBox::warningContinueCancel( Kopete::UI::Global::mainWidget(),
271  i18n( "<qt>Kopete is unable to save your password securely in your wallet;<br />"
272  "do you want to save the password in the <b>unsafe</b> configuration file instead?</qt>" ),
273  i18n( "Unable to Store Secure Password" ),
274  KGuiItem( i18n( "Store &Unsafe" ), QString::fromLatin1( "unlock" ) ), KStandardGuiItem::cancel(),
275  QString::fromLatin1( "KWalletFallbackToKConfig" ) ) != KMessageBox::Continue )
276  {
277  return false;
278  }
279  }
280  mPassword.d->remembered = true;
281  mPassword.d->passwordFromKConfig = mNewPass;
282  mPassword.writeConfig();
283  return true;
284  }
285 
286 private:
287  QString mNewPass;
288 };
289 
290 class KopetePasswordClearRequest : public KopetePasswordRequest
291 {
292 public:
293  KopetePasswordClearRequest( Kopete::Password &pass )
294  : KopetePasswordRequest( 0, pass )
295  {
296  KGlobal::ref();
297  }
298  ~KopetePasswordClearRequest()
299  {
300  KGlobal::deref();
301  kDebug( 14010 ) << "job complete";
302  }
303  void processRequest()
304  {
305  if ( clearPassword() )
306  {
307  mPassword.setWrong( true );
308  mPassword.d->cachedValue.clear();
309  }
310 
311  delete this;
312  }
313  bool clearPassword()
314  {
315  kDebug( 14010 ) << " clearing password";
316 
317  mPassword.d->remembered = false;
318  mPassword.d->passwordFromKConfig.clear();
319  mPassword.writeConfig();
320  if ( mWallet )
321  mWallet->removeEntry( mPassword.d->configGroup );
322  return true;
323  }
324 };
325 
326 
327 Kopete::Password::Password( const QString &configGroup, bool allowBlankPassword )
328  : QObject( 0 ), d( new Private( configGroup, allowBlankPassword ) )
329 {
330  readConfig();
331 }
332 
333 Kopete::Password::Password( const Password &other )
334  : QObject( 0 ), d( other.d->incRef() )
335 {
336 }
337 
338 Kopete::Password::~Password()
339 {
340  d->decRef();
341 }
342 
343 Kopete::Password &Kopete::Password::operator=( Password &other )
344 {
345  if ( d == other.d ) return *this;
346  d->decRef();
347  d = other.d->incRef();
348  return *this;
349 }
350 
351 void Kopete::Password::readConfig()
352 {
353  KConfigGroup config(KGlobal::config(), d->configGroup );
354 
355  const QString passwordCrypted = config.readEntry( "Password", QString() );
356  if ( passwordCrypted.isNull() )
357  d->passwordFromKConfig.clear();
358  else
359  d->passwordFromKConfig = KStringHandler::obscure( passwordCrypted );
360 
361  d->remembered = config.readEntry( "RememberPassword", false );
362  d->isWrong = config.readEntry( "PasswordIsWrong", false );
363 }
364 
365 void Kopete::Password::writeConfig()
366 {
367  KSharedConfig::Ptr config = KGlobal::config();
368  if(!config->hasGroup(d->configGroup))
369  {
370  //### (KOPETE)
371  // if the kopete account has been removed, we have no way to know it.
372  // but we don't want in any case to recreate the group.
373  // see Bug 106460
374  // (the problem is that when we remove the account, we remove the password
375  // also, which cause a call to this function )
376  return;
377  }
378 
379  KConfigGroup group = config->group( d->configGroup );
380 
381  if ( d->remembered && !d->passwordFromKConfig.isNull() )
382  group.writeEntry( "Password", KStringHandler::obscure( d->passwordFromKConfig ) );
383  else
384  group.deleteEntry( "Password" );
385 
386  group.writeEntry( "RememberPassword", d->remembered );
387  group.writeEntry( "PasswordIsWrong", d->isWrong );
388 }
389 
390 int Kopete::Password::preferredImageSize()
391 {
392  return IconSize(KIconLoader::Toolbar);
393 }
394 
395 bool Kopete::Password::allowBlankPassword()
396 {
397  return d->allowBlankPassword;
398 }
399 
400 bool Kopete::Password::isWrong()
401 {
402  return d->isWrong;
403 }
404 
405 void Kopete::Password::setWrong( bool bWrong )
406 {
407  d->isWrong = bWrong;
408  writeConfig();
409 
410  if ( bWrong ) d->cachedValue.clear();
411 }
412 
413 void Kopete::Password::requestWithoutPrompt( QObject *returnObj, const char *slot )
414 {
415  KopetePasswordRequest *request = new KopetePasswordGetRequestNoPrompt( returnObj, *this );
416  // call connect on returnObj so we can still connect if 'slot' is protected/private
417  returnObj->connect( request, SIGNAL(requestFinished(QString)), slot );
418  request->begin();
419 }
420 
421 void Kopete::Password::request( QObject *returnObj, const char *slot, const QPixmap &image, const QString &prompt, Kopete::Password::PasswordSource source )
422 {
423  KopetePasswordRequest *request = new KopetePasswordGetRequestPrompt( returnObj, *this, image, prompt, source );
424  returnObj->connect( request, SIGNAL(requestFinished(QString)), slot );
425  request->begin();
426 }
427 
428 QString Kopete::Password::cachedValue()
429 {
430  return d->cachedValue;
431 }
432 
433 void Kopete::Password::set( const QString &pass )
434 {
435  // if we're being told to forget the password, and we aren't remembering one,
436  // don't try to open the wallet. fixes bug #71804.
437  if( pass.isNull() && !d->allowBlankPassword )
438  {
439  if( remembered() )
440  clear();
441  return;
442  }
443 
444  KopetePasswordRequest *request = new KopetePasswordSetRequest( *this, pass );
445  request->begin();
446 }
447 
448 void Kopete::Password::clear()
449 {
450  KopetePasswordClearRequest *request = new KopetePasswordClearRequest( *this );
451  request->begin();
452 }
453 
454 bool Kopete::Password::remembered()
455 {
456  return d->remembered;
457 }
458 
459 #include "kopetepassword.moc"
460 
461 // vim: set noet ts=4 sts=4 sw=4:
KopetePasswordRequestBase::gotPassword
virtual void gotPassword(const QString &, bool)=0
Kopete::Password::set
void set(const QString &pass=QString())
Set the password for this account.
Definition: kopetepassword.cpp:433
QWidget
Kopete::Password::Password
Password(const QString &configGroup, bool allowBlankPassword=false)
Create a new Kopete::Password object.
Definition: kopetepassword.cpp:327
Kopete::Password::clear
void clear()
Unconditionally clears the stored password.
Definition: kopetepassword.cpp:448
Kopete::Password::PasswordSource
PasswordSource
Type of password request to perform: FromConfigOrUser : get the password from the config file...
Definition: kopetepassword.h:98
Kopete::Password::setWrong
void setWrong(bool bWrong=true)
Flag the password as being incorrect.
Definition: kopetepassword.cpp:405
Kopete::Password::isWrong
bool isWrong()
Returns whether the password currently stored by this object is known to be incorrect.
Definition: kopetepassword.cpp:400
Kopete::Password::preferredImageSize
static int preferredImageSize()
Returns the preferred size for images passed to the retrieve and request functions.
Definition: kopetepassword.cpp:390
Kopete::Password::FromUser
Definition: kopetepassword.h:98
kopeteuiglobal.h
QString::isNull
bool isNull() const
QString::clear
void clear()
kopetewalletmanager.h
QObject
QString::isEmpty
bool isEmpty() const
Kopete::WalletManager::self
static WalletManager * self()
Retrieve the wallet manager instance.
Definition: kopetewalletmanager.cpp:64
QObject::deleteLater
void deleteLater()
QString
KopetePasswordRequestBase::slotCancelPressed
virtual void slotCancelPressed()=0
Kopete::Password::requestWithoutPrompt
void requestWithoutPrompt(QObject *receiver, const char *slot)
Start an asynchronous password request without a prompt.
Definition: kopetepassword.cpp:413
QPixmap
KopetePasswordRequestBase::requestFinished
void requestFinished(const QString &password)
KopetePasswordRequestBase
This class is an implementation detail of KopetePassword.
Definition: kopetepassword.h:186
Kopete::UI::Global::mainWidget
KOPETE_EXPORT QWidget * mainWidget()
Returns the main widget - this is the widget that message boxes and KNotify stuff should use as a par...
Definition: kopeteuiglobal.cpp:37
KopetePasswordRequestBase::walletReceived
virtual void walletReceived(KWallet::Wallet *wallet)=0
Kopete::Password
Definition: kopetepassword.h:48
Kopete::Password::request
void request(QObject *receiver, const char *slot, const QPixmap &image, const QString &prompt, PasswordSource source=FromConfigOrUser)
Start an asynchronous call to get the password.
Definition: kopetepassword.cpp:421
Kopete::Password::operator=
Password & operator=(Password &other)
Assignment operator for passwords: make this object represent a different password.
Definition: kopetepassword.cpp:343
Kopete::Password::~Password
~Password()
Definition: kopetepassword.cpp:338
kopetepassword.h
QString::fromLatin1
QString fromLatin1(const char *str, int size)
Kopete::WalletManager::openWallet
void openWallet(QObject *object, const char *slot)
Attempt to open the KWallet asyncronously, then signal an object to indicate the task is complete...
Definition: kopetewalletmanager.cpp:70
Kopete::Password::remembered
bool remembered()
Definition: kopetepassword.cpp:454
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Kopete::Password::allowBlankPassword
bool allowBlankPassword()
Definition: kopetepassword.cpp:395
Kopete::Password::cachedValue
QString cachedValue()
When a password request succeeds, the password is cached.
Definition: kopetepassword.cpp:428
QObject::destroyed
void destroyed(QObject *obj)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:29:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kopete/libkopete

Skip menu "kopete/libkopete"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

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