knode
knaccountmanager.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <stdlib.h>
00016
00017 #include <QDir>
00018
00019 #include <kdebug.h>
00020 #include <kconfig.h>
00021 #include <klocale.h>
00022 #include <kmessagebox.h>
00023 #include <kstandarddirs.h>
00024 #include <kwallet.h>
00025
00026 #include "kngroupmanager.h"
00027 #include "knnntpaccount.h"
00028 #include "knglobals.h"
00029 #include "knconfigmanager.h"
00030 #include "utilities.h"
00031 #include "knaccountmanager.h"
00032 #include "knfoldermanager.h"
00033
00034 KWallet::Wallet* KNAccountManager::mWallet = 0;
00035 bool KNAccountManager::mWalletOpenFailed = false;
00036
00037 KNAccountManager::KNAccountManager( KNGroupManager *gm, QObject * parent )
00038 : QObject( parent ), gManager( gm ), c_urrentAccount( 0 ),
00039 mAsyncOpening( false )
00040 {
00041 loadAccounts();
00042 }
00043
00044
00045 KNAccountManager::~KNAccountManager()
00046 {
00047 qDeleteAll( mAccounts );
00048 mAccounts.clear();
00049 delete mWallet;
00050 mWallet = 0;
00051 }
00052
00053
00054 void KNAccountManager::prepareShutdown()
00055 {
00056 for ( List::Iterator it = mAccounts.begin(); it != mAccounts.end(); ++it )
00057 (*it)->saveInfo();
00058 }
00059
00060
00061 void KNAccountManager::loadAccounts()
00062 {
00063 QString dir(KStandardDirs::locateLocal("data","knode/"));
00064 if (dir.isNull()) {
00065 KNHelper::displayInternalFileError();
00066 return;
00067 }
00068 QDir d(dir);
00069 KNNntpAccount *a;
00070 QStringList entries(d.entryList(QStringList("nntp.*"), QDir::Dirs));
00071
00072 QStringList::Iterator it;
00073 for(it = entries.begin(); it != entries.end(); ++it) {
00074 a = new KNNntpAccount();
00075 if (a->readInfo(dir+(*it) + "/info")) {
00076 mAccounts.append(a);
00077 gManager->loadGroups(a);
00078 emit accountAdded(a);
00079 } else {
00080 delete a;
00081 kError(5003) <<"Unable to load account" << (*it) <<"!";
00082 }
00083 }
00084 }
00085
00086
00087 KNNntpAccount* KNAccountManager::account( int id )
00088 {
00089 if ( id <= 0 )
00090 return 0;
00091 for ( List::ConstIterator it = mAccounts.begin(); it != mAccounts.end(); ++it )
00092 if ( (*it)->id() == id )
00093 return *it;
00094 return 0;
00095 }
00096
00097
00098 void KNAccountManager::setCurrentAccount(KNNntpAccount *a)
00099 {
00100 c_urrentAccount = a;
00101 }
00102
00103
00104
00105 bool KNAccountManager::newAccount(KNNntpAccount *a)
00106 {
00107
00108 QString dir(KStandardDirs::locateLocal("data","knode/"));
00109 if (dir.isNull()) {
00110 delete a;
00111 KNHelper::displayInternalFileError();
00112 return false;
00113 }
00114 QDir d(dir);
00115 QStringList entries = d.entryList( QStringList( "nntp.*" ), QDir::Dirs );
00116
00117 int id = 1;
00118 while (entries.indexOf(QString("nntp.%1").arg(id))!=-1)
00119 ++id;
00120
00121 a->setId(id);
00122
00123 dir = KStandardDirs::locateLocal("data",QString("knode/nntp.%1/").arg(a->id()));
00124 if (!dir.isNull()) {
00125 mAccounts.append(a);
00126 emit(accountAdded(a));
00127 return true;
00128 } else {
00129 delete a;
00130 KMessageBox::error(knGlobals.topWidget, i18n("Cannot create a folder for this account."));
00131 return false;
00132 }
00133 }
00134
00135
00136
00137 bool KNAccountManager::removeAccount(KNNntpAccount *a)
00138 {
00139 if(!a) a=c_urrentAccount;
00140 if(!a) return false;
00141
00142 KNGroup::List lst;
00143 if ( knGlobals.folderManager()->unsentForAccount( a->id() ) > 0 ) {
00144 KMessageBox::sorry( knGlobals.topWidget,
00145 i18n("This account cannot be deleted since there are some unsent messages for it.") );
00146 }
00147 else if ( KMessageBox::warningContinueCancel ( knGlobals.topWidget,
00148 i18n("Do you really want to delete this account?"), "", KGuiItem( i18n("&Delete"), "edit-delete") )
00149 ==KMessageBox::Continue ) {
00150 lst = gManager->groupsOfAccount( a );
00151 for ( KNGroup::List::Iterator it = lst.begin(); it != lst.end(); ++it ) {
00152 if ( (*it)->isLocked() ) {
00153 KMessageBox::sorry( knGlobals.topWidget, i18n("At least one group of this account is currently in use.\n"
00154 "The account cannot be deleted at the moment.") );
00155 return false;
00156 }
00157 }
00158 for ( KNGroup::List::Iterator it = lst.begin(); it != lst.end(); ++it )
00159 gManager->unsubscribeGroup( (*it) );
00160
00161 QDir dir(a->path());
00162 if (dir.exists()) {
00163 QFileInfoList list = dir.entryInfoList();
00164 QFileInfo it;
00165 Q_FOREACH( it, list ) {
00166 dir.remove(it.fileName());
00167 }
00168 dir.cdUp();
00169 dir.rmdir(QString("nntp.%1/").arg(a->id()));
00170 }
00171
00172 if(c_urrentAccount==a) setCurrentAccount(0);
00173
00174 emit(accountRemoved(a));
00175 mAccounts.removeAll( a );
00176 return true;
00177 }
00178
00179 return false;
00180 }
00181
00182
00183 void KNAccountManager::editProperties(KNNntpAccount *a)
00184 {
00185 if(!a) a=c_urrentAccount;
00186 if(!a) return;
00187
00188 a->editProperties(knGlobals.topWidget);
00189 emit(accountModified(a));
00190 }
00191
00192
00193 void KNAccountManager::accountRenamed(KNNntpAccount *a)
00194 {
00195 if(!a) a=c_urrentAccount;
00196 if(!a) return;
00197
00198 emit(accountModified(a));
00199 }
00200
00201
00202 KNNntpAccount* KNAccountManager::first() const
00203 {
00204 if ( mAccounts.isEmpty() )
00205 return 0;
00206 return mAccounts.first();
00207 }
00208
00209
00210 void KNAccountManager::loadPasswordsAsync()
00211 {
00212 if ( !mWallet && !mWalletOpenFailed ) {
00213 if ( knGlobals.top )
00214 mWallet = Wallet::openWallet( Wallet::NetworkWallet(),
00215 knGlobals.topWidget->topLevelWidget()->winId(),
00216 Wallet::Asynchronous );
00217 else
00218 mWallet = Wallet::openWallet( Wallet::NetworkWallet(), 0, Wallet::Asynchronous );
00219 if ( mWallet ) {
00220 connect( mWallet, SIGNAL(walletOpened(bool)), SLOT(slotWalletOpened(bool)) );
00221 mAsyncOpening = true;
00222 }
00223 else {
00224 mWalletOpenFailed = true;
00225 loadPasswords();
00226 }
00227 return;
00228 }
00229 if ( mWallet && !mAsyncOpening )
00230 loadPasswords();
00231 }
00232
00233
00234 void KNAccountManager::loadPasswords()
00235 {
00236 for ( List::Iterator it = mAccounts.begin(); it != mAccounts.end(); ++it )
00237 (*it)->readPassword();
00238 emit passwordsChanged();
00239 }
00240
00241
00242 KWallet::Wallet* KNAccountManager::wallet()
00243 {
00244 if ( mWallet && mWallet->isOpen() )
00245 return mWallet;
00246
00247 if ( !Wallet::isEnabled() || mWalletOpenFailed )
00248 return 0;
00249
00250 delete mWallet;
00251 if ( knGlobals.top )
00252 mWallet = Wallet::openWallet( Wallet::NetworkWallet(),
00253 knGlobals.topWidget->topLevelWidget()->winId() );
00254 else
00255 mWallet = Wallet::openWallet( Wallet::NetworkWallet(), 0 );
00256
00257 if ( !mWallet ) {
00258 mWalletOpenFailed = true;
00259 return 0;
00260 }
00261
00262 prepareWallet();
00263 return mWallet;
00264 }
00265
00266
00267 void KNAccountManager::prepareWallet()
00268 {
00269 if ( !mWallet )
00270 return;
00271 if ( !mWallet->hasFolder("knode") )
00272 mWallet->createFolder( "knode" );
00273 mWallet->setFolder( "knode" );
00274 }
00275
00276
00277 void KNAccountManager::slotWalletOpened( bool success )
00278 {
00279 mAsyncOpening = false;
00280 if ( !success ) {
00281 mWalletOpenFailed = true;
00282 delete mWallet;
00283 mWallet = 0;
00284 } else {
00285 prepareWallet();
00286 }
00287 loadPasswords();
00288 }
00289
00290
00291
00292 #include "knaccountmanager.moc"