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

knode

  • sources
  • kde-4.12
  • kdepim
  • knode
knaccountmanager.cpp
Go to the documentation of this file.
1 /*
2  KNode, the KDE newsreader
3  Copyright (c) 1999-2005 the KNode authors.
4  See file AUTHORS for details
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  You should have received a copy of the GNU General Public License
11  along with this program; if not, write to the Free Software Foundation,
12  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
13 */
14 
15 #include "knaccountmanager.h"
16 
17 #include "knconfigmanager.h"
18 #include "knfoldermanager.h"
19 #include "knglobals.h"
20 #include "kngroupmanager.h"
21 #include "utilities.h"
22 
23 #include <QDir>
24 #include <kdebug.h>
25 #include <kconfig.h>
26 #include <klocale.h>
27 #include <kmessagebox.h>
28 #include <kstandarddirs.h>
29 #include <kwallet.h>
30 
31 
32 
33 KWallet::Wallet* KNAccountManager::mWallet = 0;
34 bool KNAccountManager::mWalletOpenFailed = false;
35 
36 KNAccountManager::KNAccountManager( KNGroupManager *gm, QObject * parent )
37  : QObject( parent ), gManager( gm ),
38  mAsyncOpening( false )
39 {
40  loadAccounts();
41 }
42 
43 
44 KNAccountManager::~KNAccountManager()
45 {
46  mAccounts.clear();
47  delete mWallet;
48  mWallet = 0;
49 }
50 
51 
52 void KNAccountManager::prepareShutdown()
53 {
54  for ( KNNntpAccount::List::Iterator it = mAccounts.begin(); it != mAccounts.end(); ++it )
55  (*it)->writeConfig();
56 }
57 
58 
59 void KNAccountManager::loadAccounts()
60 {
61  QString dir( KStandardDirs::locateLocal( "data", "knode/" ) );
62  if (dir.isNull()) {
63  KNHelper::displayInternalFileError();
64  return;
65  }
66  QDir d(dir);
67  KNNntpAccount::Ptr a;
68  QStringList entries(d.entryList(QStringList("nntp.*"), QDir::Dirs));
69 
70  QStringList::Iterator it;
71  for(it = entries.begin(); it != entries.end(); ++it) {
72  a = KNNntpAccount::Ptr( new KNNntpAccount() );
73  if (a->readInfo(dir+(*it) + "/info")) {
74  mAccounts.append(a);
75  gManager->loadGroups(a);
76  emit accountAdded(a);
77  } else {
78  kError(5003) <<"Unable to load account" << (*it) <<"!";
79  }
80  }
81 }
82 
83 
84 KNNntpAccount::Ptr KNAccountManager::account( int id )
85 {
86  if ( id <= 0 )
87  return KNNntpAccount::Ptr();
88  for ( KNNntpAccount::List::ConstIterator it = mAccounts.constBegin(); it != mAccounts.constEnd(); ++it )
89  if ( (*it)->id() == id )
90  return *it;
91  return KNNntpAccount::Ptr();
92 }
93 
94 
95 void KNAccountManager::setCurrentAccount( KNNntpAccount::Ptr a )
96 {
97  c_urrentAccount = a;
98 }
99 
100 
101 // a is new account allocated and configured by the caller
102 bool KNAccountManager::newAccount( KNNntpAccount::Ptr a )
103 {
104  // find a unused id for the new account...
105  QString dir( KStandardDirs::locateLocal( "data", "knode/" ) );
106  if (dir.isNull()) {
107  KNHelper::displayInternalFileError();
108  return false;
109  }
110  QDir d(dir);
111  QStringList entries = d.entryList( QStringList( "nntp.*" ), QDir::Dirs );
112 
113  int id = 1;
114  while (entries.indexOf(QString("nntp.%1").arg(id))!=-1)
115  ++id;
116 
117  a->setId(id);
118 
119  dir = KStandardDirs::locateLocal( "data", QString( "knode/nntp.%1/" ).arg( a->id() ) );
120  if (!dir.isNull()) {
121  mAccounts.append(a);
122  emit(accountAdded(a));
123  return true;
124  } else {
125  KMessageBox::error(knGlobals.topWidget, i18n("Cannot create a folder for this account."));
126  return false;
127  }
128 }
129 
130 
131 // a==0: remove current account
132 bool KNAccountManager::removeAccount( KNNntpAccount::Ptr a )
133 {
134  if(!a) a=c_urrentAccount;
135  if(!a) return false;
136 
137  KNGroup::List lst;
138  if ( knGlobals.folderManager()->unsentForAccount( a->id() ) > 0 ) {
139  KMessageBox::sorry( knGlobals.topWidget,
140  i18n("This account cannot be deleted since there are some unsent messages for it.") );
141  }
142  else if ( KMessageBox::warningContinueCancel ( knGlobals.topWidget,
143  i18n("Do you really want to delete this account?"), "", KGuiItem( i18n("&Delete"), "edit-delete") )
144  ==KMessageBox::Continue ) {
145  lst = gManager->groupsOfAccount( a );
146  for ( KNGroup::List::Iterator it = lst.begin(); it != lst.end(); ++it ) {
147  if ( (*it)->isLocked() ) {
148  KMessageBox::sorry( knGlobals.topWidget, i18n("At least one group of this account is currently in use.\n"
149  "The account cannot be deleted at the moment.") );
150  return false;
151  }
152  }
153  for ( KNGroup::List::Iterator it = lst.begin(); it != lst.end(); ++it )
154  gManager->unsubscribeGroup( (*it) );
155 
156  QDir dir(a->path());
157  if (dir.exists()) {
158  QFileInfoList list = dir.entryInfoList(); // get list of matching files and delete all
159  QFileInfo it;
160  Q_FOREACH( it, list ) {
161  dir.remove(it.fileName());
162  }
163  dir.cdUp(); // directory should now be empty, deleting it
164  dir.rmdir(QString("nntp.%1/").arg(a->id()));
165  }
166 
167  if( c_urrentAccount == a ) {
168  setCurrentAccount( KNNntpAccount::Ptr() );
169  }
170 
171  emit(accountRemoved(a));
172  mAccounts.removeAll( a ); // finally delete a
173  return true;
174  }
175 
176  return false;
177 }
178 
179 
180 void KNAccountManager::editProperties( KNNntpAccount::Ptr a )
181 {
182  if(!a) a=c_urrentAccount;
183  if(!a) return;
184 
185  a->editProperties(knGlobals.topWidget);
186  emit(accountModified(a));
187 }
188 
189 
190 void KNAccountManager::accountRenamed( KNNntpAccount::Ptr a )
191 {
192  if(!a) a=c_urrentAccount;
193  if(!a) return;
194 
195  emit(accountModified(a));
196 }
197 
198 
199 KNNntpAccount::Ptr KNAccountManager::first() const
200 {
201  if ( mAccounts.isEmpty() )
202  return KNNntpAccount::Ptr();
203  return mAccounts.first();
204 }
205 
206 
207 void KNAccountManager::loadPasswordsAsync()
208 {
209  if ( !mWallet && !mWalletOpenFailed ) {
210  if ( knGlobals.top )
211  mWallet = Wallet::openWallet( Wallet::NetworkWallet(),
212  knGlobals.topWidget->topLevelWidget()->winId(),
213  Wallet::Asynchronous );
214  else
215  mWallet = Wallet::openWallet( Wallet::NetworkWallet(), 0, Wallet::Asynchronous );
216  if ( mWallet ) {
217  connect( mWallet, SIGNAL(walletOpened(bool)), SLOT(slotWalletOpened(bool)) );
218  mAsyncOpening = true;
219  }
220  else {
221  mWalletOpenFailed = true;
222  loadPasswords();
223  }
224  return;
225  }
226  if ( mWallet && !mAsyncOpening )
227  loadPasswords();
228 }
229 
230 
231 void KNAccountManager::loadPasswords()
232 {
233  for ( KNNntpAccount::List::Iterator it = mAccounts.begin(); it != mAccounts.end(); ++it )
234  (*it)->readPassword();
235  emit passwordsChanged();
236 }
237 
238 
239 KWallet::Wallet* KNAccountManager::wallet()
240 {
241  if ( mWallet && mWallet->isOpen() )
242  return mWallet;
243 
244  if ( !Wallet::isEnabled() || mWalletOpenFailed )
245  return 0;
246 
247  delete mWallet;
248  if ( knGlobals.top )
249  mWallet = Wallet::openWallet( Wallet::NetworkWallet(),
250  knGlobals.topWidget->topLevelWidget()->winId() );
251  else
252  mWallet = Wallet::openWallet( Wallet::NetworkWallet(), 0 );
253 
254  if ( !mWallet ) {
255  mWalletOpenFailed = true;
256  return 0;
257  }
258 
259  prepareWallet();
260  return mWallet;
261 }
262 
263 
264 void KNAccountManager::prepareWallet()
265 {
266  if ( !mWallet )
267  return;
268  if ( !mWallet->hasFolder("knode") )
269  mWallet->createFolder( "knode" );
270  mWallet->setFolder( "knode" );
271 }
272 
273 
274 void KNAccountManager::slotWalletOpened( bool success )
275 {
276  mAsyncOpening = false;
277  if ( !success ) {
278  mWalletOpenFailed = true;
279  delete mWallet;
280  mWallet = 0;
281  } else {
282  prepareWallet();
283  }
284  loadPasswords();
285 }
286 
287 //--------------------------------
288 
289 #include "knaccountmanager.moc"
KNNntpAccount
Represents an account on a news server.
Definition: knnntpaccount.h:56
KNGroupManager::groupsOfAccount
KNGroup::List groupsOfAccount(KNNntpAccount::Ptr a)
Returns the list of (subscribed) groups in the account a.
Definition: kngroupmanager.cpp:288
utilities.h
KNAccountManager::removeAccount
bool removeAccount(KNNntpAccount::Ptr a=KNNntpAccount::Ptr())
Remove an existing account.
Definition: knaccountmanager.cpp:132
KNAccountManager::wallet
static KWallet::Wallet * wallet()
Returns a pointer to an open wallet if available, 0 otherwise.
Definition: knaccountmanager.cpp:239
KNGroupManager::loadGroups
void loadGroups(KNNntpAccount::Ptr a)
Definition: kngroupmanager.cpp:257
knaccountmanager.h
KNGroupManager::unsubscribeGroup
bool unsubscribeGroup(KNGroup::Ptr g=KNGroup::Ptr())
Definition: kngroupmanager.cpp:454
KNAccountManager::loadAccounts
void loadAccounts()
Definition: knaccountmanager.cpp:59
kngroupmanager.h
QObject
KNNntpAccount::Ptr
boost::shared_ptr< KNNntpAccount > Ptr
Shared pointer to a KNNntpAccount.
Definition: knnntpaccount.h:62
KNAccountManager::gManager
KNGroupManager * gManager
Definition: knaccountmanager.h:91
KNAccountManager::accountAdded
void accountAdded(KNNntpAccount::Ptr a)
KNAccountManager::accountRemoved
void accountRemoved(KNNntpAccount::Ptr a)
KNAccountManager::newAccount
bool newAccount(KNNntpAccount::Ptr a)
Add a new account.
Definition: knaccountmanager.cpp:102
KNHelper::displayInternalFileError
static void displayInternalFileError(QWidget *w=0)
use this for all internal files
Definition: utilities.cpp:344
KNAccountManager::KNAccountManager
KNAccountManager(KNGroupManager *gm, QObject *parent=0)
Create a new account manager.
Definition: knaccountmanager.cpp:36
KNAccountManager::first
KNNntpAccount::Ptr first() const
Returns the first account (used as fallback sometimes).
Definition: knaccountmanager.cpp:199
KNAccountManager::loadPasswords
void loadPasswords()
Loads the passwords of all accounts, allows on-demand wallet opening.
Definition: knaccountmanager.cpp:231
KNAccountManager::editProperties
void editProperties(KNNntpAccount::Ptr a=KNNntpAccount::Ptr())
Show the properties dialog for the given account.
Definition: knaccountmanager.cpp:180
knglobals.h
knfoldermanager.h
KNAccountManager::setCurrentAccount
void setCurrentAccount(KNNntpAccount::Ptr a)
Sets the current account.
Definition: knaccountmanager.cpp:95
KNAccountManager::passwordsChanged
void passwordsChanged()
Emitted if passwords have been loaded from the wallet.
KNAccountManager::prepareShutdown
void prepareShutdown()
Save all accounts.
Definition: knaccountmanager.cpp:52
KNAccountManager::accountRenamed
void accountRenamed(KNNntpAccount::Ptr a=KNNntpAccount::Ptr())
Definition: knaccountmanager.cpp:190
knconfigmanager.h
KNAccountManager::c_urrentAccount
KNNntpAccount::Ptr c_urrentAccount
Definition: knaccountmanager.h:92
KNAccountManager::loadPasswordsAsync
void loadPasswordsAsync()
Loads passwords of all accounts asynchronous.
Definition: knaccountmanager.cpp:207
KNAccountManager::accountModified
void accountModified(KNNntpAccount::Ptr a)
knGlobals
#define knGlobals
Keep compatibility with the old way.
Definition: knglobals.h:28
KNGroupManager
Group manager.
Definition: kngroupmanager.h:83
KNAccountManager::account
KNNntpAccount::Ptr account(int id)
Returns the account with the given id.
Definition: knaccountmanager.cpp:84
QList
KNAccountManager::~KNAccountManager
~KNAccountManager()
Delete this account manager and all managed accounts.
Definition: knaccountmanager.cpp:44
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:36 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

knode

Skip menu "knode"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

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