blokkalwalletmanager.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00074
00075
00076
00077
00078
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
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 }