Blokkal
an Extendable KDE Blogging Client
SourceForge.net Logo

blokkalwalletmanager.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2006 by Martin Müller                                   *
00003  *   orvio@orvio.de                                                        *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include "blokkalwalletmanager.h"
00021 #include "blokkalwalletmanager.moc"
00022 
00023 #include "blokkalui/globalsettings.h"
00024 
00025 #include <kwallet.h>
00026 #include <kstandarddirs.h>
00027 #include <kdebug.h>
00028 
00029 #include <QTimer>
00030 
00031 #include <iostream>
00032 
00033 class Blokkal::WalletManagerPrivate {
00034 public:
00035         WalletManagerPrivate( void ):
00036         wallet( 0 ),
00037         signal( new WalletManagerSignal() ),
00038         openTimer( 0 ) {}
00039         
00040         ~WalletManagerPrivate( void )
00041         {
00042                 delete signal;
00043                 delete wallet;
00044         }
00045         
00046         KWallet::Wallet * wallet;
00047         WalletManagerSignal * signal;
00048         QTimer * openTimer;
00049 
00050         Blokkal::WalletManager instance;
00051 };
00052 
00053 K_GLOBAL_STATIC( Blokkal::WalletManagerPrivate, _bwmp );
00054 
00055 Blokkal::WalletManager::WalletManager( void ) :
00056 QObject( 0 )
00057 {
00058         setObjectName( "BlokkalWalletManager" );
00059 }
00060 
00061 Blokkal::WalletManager::~WalletManager( void )
00062 {
00063 }
00064 
00065 Blokkal::WalletManager * Blokkal::WalletManager::self( void )
00066 {
00067         return &_bwmp->instance;
00068 }
00069 
00070 void Blokkal::WalletManager::openWallet( QObject * receiver, const char * slot )
00071 {
00072         if( !_bwmp->wallet ) {
00073                 //sometimes opening the wallet does not work correctly
00074                 //on session restore.
00075                 //the wallet is opened, but the calling app is never notified
00076                 //the wallet manager will give "anonymous" or "DCOPServer" or
00077                 //"konqueror" as the requesing app's name, but never the correct one.
00078                 //so if we do not get a feedback after a minute, the process is repeated.
00079                 _bwmp->openTimer = new QTimer( this );
00080                 connect( _bwmp->openTimer, SIGNAL( timeout( void ) ),
00081                          this, SLOT( retryOpen( void ) ) );
00082                 _bwmp->openTimer->start( 1 * 60 * 1000 );
00083                 _bwmp->wallet = KWallet::Wallet::openWallet( KWallet::Wallet::NetworkWallet(),
00084                                                          Blokkal::Ui::GlobalSettings::self()->mainWindowWId(),
00085                                                          KWallet::Wallet::Asynchronous );
00086                 connect( _bwmp->wallet, SIGNAL( walletOpened( bool ) ),
00087                          this, SLOT( slotWalletOpened( void ) ) );
00088         }
00089 
00090         receiver->connect( _bwmp->signal, SIGNAL( walletOpened( KWallet::Wallet * ) ), slot );
00091         
00092         if( _bwmp->wallet->isOpen() ) {
00093                 QTimer::singleShot( 0, this, SLOT( slotWalletOpened( void ) ) );
00094         }
00095 }
00096 
00097 void Blokkal::WalletManager::slotWalletOpened( void )
00098 {
00099         if( _bwmp->openTimer ) {
00100                 _bwmp->openTimer->stop();
00101                 delete _bwmp->openTimer;
00102                 _bwmp->openTimer = 0;
00103         }
00104         
00105         if( _bwmp->wallet->isOpen() ) {
00106                 const QString appFolder = "Blokkal";
00107                 if( !_bwmp->wallet->hasFolder( appFolder ) ) {
00108                         _bwmp->wallet->createFolder( appFolder);
00109                 }
00110                 _bwmp->wallet->setFolder( appFolder );
00111                 emit _bwmp->signal->walletOpened( _bwmp->wallet );
00112         }
00113         else {
00114                 emit _bwmp->signal->walletOpened( 0 );
00115                 delete _bwmp->wallet;
00116                 _bwmp->wallet = 0;
00117         }
00118         
00119         
00120         delete _bwmp->signal;
00121         _bwmp->signal = new WalletManagerSignal();
00122 }
00123 
00124 void Blokkal::WalletManager::retryOpen( void )
00125 {
00126         if( _bwmp->wallet && !_bwmp->wallet->isOpen() ) {
00127                 delete _bwmp->wallet;
00128                 _bwmp->wallet = KWallet::Wallet::openWallet( KWallet::Wallet::NetworkWallet(),
00129                                                          Blokkal::Ui::GlobalSettings::self()->mainWindowWId(),
00130                                                          KWallet::Wallet::Asynchronous );
00131                 connect( _bwmp->wallet, SIGNAL( walletOpened( bool ) ),
00132                          this, SLOT( slotWalletOpened( void ) ) );
00133 
00134         }
00135 }
00136 
00137 //---------------
00138 //PasswordDeleter
00139 //---------------
00140 class Blokkal::PasswordDeleter::Private {
00141 public:
00142         Private( const QString & accountId ) :
00143         accountId( accountId ){}
00144 
00145         QString accountId;
00146 };
00147 
00148 Blokkal::PasswordDeleter::PasswordDeleter( const QString & accountId ):
00149 QObject(),
00150 d( new Private( accountId ) )
00151 {
00152         WalletManager::self()->openWallet( this, SLOT( slotWalletOpened( KWallet::Wallet * ) ) );
00153 }
00154 
00155 Blokkal::PasswordDeleter::~PasswordDeleter( void )
00156 {
00157         delete d;
00158 }
00159 
00160 void Blokkal::PasswordDeleter::slotWalletOpened( KWallet::Wallet * wallet )
00161 {
00162         if( wallet && wallet->hasEntry( d->accountId ) ) {
00163                 wallet->removeEntry( d->accountId );
00164         }
00165 
00166         deleteLater();
00167 }