• 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
scheduler.cpp
Go to the documentation of this file.
1 /*
2  KNode, the KDE newsreader
3  Copyright (c) 1999-2006 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 <klocale.h>
16 #include <kdebug.h>
17 #include <kpassworddialog.h>
18 
19 #include "knaccountmanager.h"
20 #include "knarticle.h"
21 #include "knglobals.h"
22 #include "kngroupmanager.h"
23 #include "knjobdata.h"
24 #include "scheduler.h"
25 #include "knserverinfo.h"
26 
27 using namespace KNode;
28 using KPIM::ProgressManager;
29 using KPIM::ProgressItem;
30 
31 
32 Scheduler::Scheduler( QObject *parent ) :
33  QObject( parent ),
34  currentNntpJob( 0 ),
35  currentSmtpJob( 0 )
36 {
37  connect( knGlobals.accountManager(), SIGNAL(passwordsChanged()), SLOT(slotPasswordsChanged()) );
38 }
39 
40 
41 Scheduler::~Scheduler()
42 {
43 }
44 
45 
46 void Scheduler::addJob(KNJobData *job)
47 {
48  // kDebug(5003) <<"Scheduler::addJob() : job queued";
49  if ( job->type() != KNJobData::JTmail && job->account() == 0 ) {
50  job->setError( KIO::ERR_INTERNAL, i18n("Internal Error: No account set for this job.") );
51  job->notifyConsumer();
52  return;
53  }
54 
55  job->createProgressItem();
56  connect( job->progressItem(), SIGNAL(progressItemCanceled(KPIM::ProgressItem*)), SLOT(slotCancelJob(KPIM::ProgressItem*)) );
57  emit netActive( true );
58 
59  // put jobs which are waiting for the wallet into an extra queue
60  if ( job->account() && !job->account()->readyForLogin() ) {
61  kDebug(5003) <<"Job waits for KWallet.";
62  mWalletQueue.append( job );
63  knGlobals.accountManager()->loadPasswordsAsync();
64  job->setStatus( i18n( "Waiting for KWallet..." ) );
65  return;
66  }
67 
68  if (job->type()==KNJobData::JTmail) {
69  smtpJobs.append( job );
70  startJob( job );
71  } else {
72 
73  /*
74  TODO: the following code doesn't really belong here, it should
75  be moved to KNGroupManager, or elsewere...
76  */
77 
78  // avoid duplicate fetchNewHeader jobs...
79  bool duplicate = false;
80  if ( job->type() == KNJobData::JTfetchNewHeaders ) {
81  QList<KNJobData*>::ConstIterator it;
82  for ( it = nntpJobQueue.constBegin(); it != nntpJobQueue.constEnd(); ++it ) {
83  if ( ( (*it)->type() == KNJobData::JTfetchNewHeaders )
84  && (*it)->data() == job->data() ) // job works on the same group...
85  duplicate = true;
86  }
87  }
88 
89  if (!duplicate) {
90  // give a lower priority to fetchNewHeaders and postArticle jobs
91  if ( job->type() == KNJobData::JTfetchNewHeaders
92  || job->type() == KNJobData::JTpostArticle ) {
93  nntpJobQueue.append( job );
94  } else {
95  nntpJobQueue.prepend( job );
96  }
97  }
98  }
99  schedule();
100  updateStatus();
101 }
102 
103 
104 void Scheduler::schedule()
105 {
106  if ( !currentNntpJob && !nntpJobQueue.isEmpty() ) {
107  currentNntpJob = nntpJobQueue.first();
108  nntpJobQueue.removeFirst();
109  startJob( currentNntpJob );
110  }
111 }
112 
113 
114 void Scheduler::startJob( KNJobData * job )
115 {
116  job->prepareForExecution();
117  if ( job->success() ) {
118  connect( job, SIGNAL(finished(KNJobData*)),
119  SLOT(slotJobFinished(KNJobData*)) );
120  job->execute();
121  } else
122  slotJobFinished( job );
123 }
124 
125 
126 void Scheduler::cancelJobs( int type, KPIM::ProgressItem * item )
127 {
128  KNJobData *tmp = 0;
129  QList<KNJobData*>::Iterator it;
130  for ( it = nntpJobQueue.begin(); it != nntpJobQueue.end();) {
131  tmp = *it;
132  if ( ( item && tmp->progressItem() == item ) || type == 0 || type == tmp->type() ) {
133  it = nntpJobQueue.erase( it );
134  tmp->cancel();
135  tmp->notifyConsumer();
136  } else
137  ++it;
138  }
139  for ( it = smtpJobs.begin(); it != smtpJobs.end();) {
140  tmp = *it;
141  if ( ( item && tmp->progressItem() == item ) || type == 0 || type == tmp->type() ) {
142  it = smtpJobs.erase( it );
143  tmp->cancel();
144  tmp->notifyConsumer();
145  } else
146  ++it;
147  }
148  for ( it = mWalletQueue.begin(); it != mWalletQueue.end();) {
149  tmp = *it;
150  if ( ( item && tmp->progressItem() == item ) || type == 0 || type == tmp->type() ) {
151  it = mWalletQueue.erase( it );
152  tmp->cancel();
153  tmp->notifyConsumer();
154  } else
155  ++it;
156  }
157 
158  if ( currentNntpJob )
159  if ( ( item && currentNntpJob->progressItem() == item ) || type == 0 || type == currentNntpJob->type() )
160  currentNntpJob->cancel();
161 
162  updateStatus();
163 }
164 
165 
166 void Scheduler::slotJobFinished( KNJobData * job )
167 {
168  // handle authentication errors, ie. request password and try again
169  if ( job->error() == KIO::ERR_COULD_NOT_LOGIN ) {
170  kDebug(5003) <<"authentication error";
171  KNServerInfo::Ptr account = job->account();
172  if ( account ) {
173  QString user = account->user();
174  QString pass = account->pass();
175 
176  KPasswordDialog dlg( 0, KPasswordDialog::ShowUsernameLine );
177  dlg.setUsername( user );
178  dlg.setPassword( pass );
179  dlg.setKeepPassword( false );
180  dlg.setPrompt( i18n( "You need to supply a username and a\npassword to access this server" ) );
181  dlg.setUsernameReadOnly( false );
182  dlg.setCaption( i18n( "Authentication Failed" ) );
183  dlg.addCommentLine( i18n( "Server:" ), account->server() );
184 
185  if ( dlg.exec() == KDialog::Accepted ) {
186  account->setNeedsLogon( true );
187  account->setUser( user );
188  account->setPass( pass );
189  job->setError( 0, QString() );
190  // restart job
191  job->execute();
192  return;
193  }
194  }
195  }
196 
197  if ( currentNntpJob && job == currentNntpJob )
198  currentNntpJob = 0;
199  smtpJobs.removeAll( job );
200 
201  job->setComplete();
202  job->notifyConsumer();
203 
204  schedule();
205  updateStatus();
206 }
207 
208 
209 void Scheduler::slotPasswordsChanged()
210 {
211  QList<KNJobData*>::ConstIterator it;
212  for ( it = mWalletQueue.constBegin(); it != mWalletQueue.constEnd(); ++it ) {
213  (*it)->setStatus( i18n("Waiting...") );
214  Q_ASSERT( (*it)->type() != KNJobData::JTmail );
215  nntpJobQueue.append( (*it) );
216  }
217  mWalletQueue.clear();
218  schedule();
219 }
220 
221 
222 void Scheduler::slotCancelJob( KPIM::ProgressItem *item )
223 {
224  cancelJobs( 0, item );
225 }
226 
227 
228 void Scheduler::updateStatus( )
229 {
230  if ( nntpJobQueue.isEmpty() && smtpJobs.isEmpty() && !currentNntpJob
231  && mWalletQueue.isEmpty() )
232  emit netActive( false );
233  else
234  emit netActive( true );
235 }
236 
237 
238 #include "scheduler.moc"
KNJobData::setComplete
void setComplete()
Tells the progress item to indicate that the job has finished if available.
Definition: knjobdata.h:172
KNJobData::setStatus
void setStatus(const QString &msg)
Set the status message of the progress item if available.
Definition: knjobdata.h:164
KNJobData
Abstract base class for all KNode internal jobs.
Definition: knjobdata.h:101
knaccountmanager.h
KNJobData::JTpostArticle
Definition: knjobdata.h:113
kngroupmanager.h
KNJobData::cancel
void cancel()
Cancels this job.
Definition: knjobdata.cpp:105
QObject
scheduler.h
knjobdata.h
KPIM::ProgressManager
KNJobData::execute
virtual void execute()=0
Performs the actual operation of a job, needs to be reimplemented for every job.
KNJobData::type
jobType type() const
Definition: knjobdata.h:120
KNJobData::progressItem
KPIM::ProgressItem * progressItem() const
Returns the progress item for this job.
Definition: knjobdata.h:157
KNJobData::data
KNJobItem::Ptr data() const
Definition: knjobdata.h:123
KNJobData::account
KNServerInfo::Ptr account() const
Definition: knjobdata.h:122
KNJobData::notifyConsumer
void notifyConsumer()
Definition: knjobdata.cpp:96
KNode::Scheduler::netActive
void netActive(bool)
Indicates whether there are currently active jobs, useful to e.g.
KNode::Scheduler::cancelJobs
void cancelJobs(int type=0, KPIM::ProgressItem *item=0)
Cancel the selected jobs.
Definition: scheduler.cpp:126
KNode::Scheduler::~Scheduler
~Scheduler()
Definition: scheduler.cpp:41
KNJobData::JTfetchNewHeaders
Definition: knjobdata.h:111
KNJobData::prepareForExecution
void prepareForExecution()
Definition: knjobdata.h:146
knglobals.h
KNode::Scheduler::currentNntpJob
KNJobData * currentNntpJob
the currently active NNTP job
Definition: scheduler.h:69
KNode::Scheduler::Scheduler
Scheduler(QObject *parent=0)
Create a new Scheduler object.
Definition: scheduler.cpp:32
KNJobData::createProgressItem
void createProgressItem()
Creates a KPIM::ProgressItem for this job.
Definition: knjobdata.cpp:149
KNJobData::success
bool success() const
Returns true if the job finished successfully.
Definition: knjobdata.h:130
KPIM::ProgressItem
knarticle.h
KNServerInfo::Ptr
boost::shared_ptr< KNServerInfo > Ptr
Shared pointer to a KNServerInfo.
Definition: knserverinfo.h:37
KNJobData::JTmail
Definition: knjobdata.h:114
KNJobData::error
int error() const
Returns the error code (see KIO::Error).
Definition: knjobdata.h:126
KNJobData::setError
void setError(int err, const QString &errMsg)
Set job error information.
Definition: knjobdata.cpp:201
knGlobals
#define knGlobals
Keep compatibility with the old way.
Definition: knglobals.h:28
QList
knserverinfo.h
KNode::Scheduler::addJob
void addJob(KNJobData *job)
Adds a new job to the scheduler queue.
Definition: scheduler.cpp:46
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