00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "kwalletaccess.h"
00019
00020
00021 bool KWalletAccess::savePassword( const QString & account, const QString & password )
00022 {
00023
00024 if( !KWallet::Wallet::isEnabled() )
00025 {
00026 KMessageBox::error( NULL, i18n( "KWallet is not available." ) );
00027 return false;
00028 }
00029
00030
00031 QString name = KWallet::Wallet::NetworkWallet();
00032 if( name.isEmpty() )
00033 {
00034 KMessageBox::error( NULL, i18n( "Could not get wallet name for network data from KWallet." ) );
00035 return false;
00036 }
00037
00038
00039
00040 static KWallet::Wallet* wallet;
00041
00042 if( wallet == NULL )
00043 {
00044 wallet = KWallet::Wallet::openWallet( name, 0 );
00045 }
00046 else if( !wallet->isOpen() )
00047 {
00048 delete wallet;
00049 wallet = KWallet::Wallet::openWallet( name, 0 );
00050 }
00051
00052 if( wallet == NULL )
00053 {
00054 KMessageBox::error( NULL, i18n( "Could not open KWallet." ) );
00055 return false;
00056 }
00057
00058
00059 if( !wallet->hasFolder( "KShowmail" ) )
00060 {
00061 bool createFolderSuccess = wallet->createFolder( "KShowmail" );
00062
00063 if( !createFolderSuccess )
00064 {
00065 KMessageBox::error( NULL, i18n( "Could not create folder for KShowmail in KWallet." ) );
00066 return false;
00067 }
00068 }
00069
00070
00071 bool setFolderSuccess = wallet->setFolder( "KShowmail" );
00072 if( !setFolderSuccess )
00073 {
00074 KMessageBox::error( NULL, i18n( "Could not open folder for KShowmail in KWallet." ) );
00075 return false;
00076 }
00077
00078
00079 int writePasswordSuccess = wallet->writePassword( account, password );
00080 if( writePasswordSuccess != 0 )
00081 {
00082 KMessageBox::error( NULL, i18n( "Could not save password in KWallet." ) );
00083 return false;
00084 }
00085
00086
00087 return true;
00088 }
00089
00090 QString KWalletAccess::getPassword( const QString & account )
00091 {
00092
00093 if( !KWallet::Wallet::isEnabled() )
00094 {
00095 KMessageBox::error( NULL, i18n( "KWallet is not available." ) );
00096 return QString();
00097 }
00098
00099
00100 QString name = KWallet::Wallet::NetworkWallet();
00101 if( name.isEmpty() )
00102 {
00103 KMessageBox::error( NULL, i18n( "Could not get wallet name for network data from KWallet." ) );
00104 return QString();
00105 }
00106
00107
00108
00109 static KWallet::Wallet* wallet;
00110
00111 if( wallet == NULL )
00112 {
00113 wallet = KWallet::Wallet::openWallet( name, 0 );
00114 }
00115 else if( !wallet->isOpen() )
00116 {
00117 delete wallet;
00118 wallet = KWallet::Wallet::openWallet( name, 0 );
00119 }
00120
00121 if( wallet == NULL )
00122 {
00123 KMessageBox::error( NULL, i18n( "Could not open KWallet." ) );
00124 return QString();
00125 }
00126
00127
00128 bool setFolderSuccess = wallet->setFolder( "KShowmail" );
00129 if( !setFolderSuccess )
00130 {
00131 KMessageBox::error( NULL, i18n( "Could not open folder for KShowmail in KWallet." ) );
00132 return QString();
00133 }
00134
00135
00136 QString password;
00137
00138 int readPasswordSuccess = wallet->readPassword( account, password );
00139 if( readPasswordSuccess != 0 )
00140 {
00141 KMessageBox::error( NULL, i18nc( "@info error message", "Could not get password of account <resource>%1</resource> from KWallet.", account) );
00142 return QString();
00143 }
00144
00145 return password;
00146 }