1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
|
/*
KNode, the KDE newsreader
Copyright (c) 1999-2005 the KNode authors.
See file AUTHORS for details
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
*/
#include <unistd.h>
#include <fcntl.h>
#include <tqsocketnotifier.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kdebug.h>
#include <tdeio/job.h>
#include <tdeio/passdlg.h>
#include <ksocks.h>
#include <tdeapplication.h>
#include "knaccountmanager.h"
#include "knarticle.h"
#include "knmainwidget.h"
#include "knjobdata.h"
#include "knnntpclient.h"
#include "knglobals.h"
#include "knnetaccess.h"
#include "knwidgets.h"
using KPIM::ProgressManager;
KNNetAccess::KNNetAccess(TQObject *parent, const char *name )
: TQObject(parent,name), currentNntpJob(0), currentSmtpJob(0)
{
if ( pipe(nntpInPipe) == -1 || pipe(nntpOutPipe) == -1 ) {
KMessageBox::error(knGlobals.topWidget, i18n("Internal error:\nFailed to open pipes for internal communication."));
kapp->exit(1);
}
if ( fcntl( nntpInPipe[0], F_SETFL, O_NONBLOCK ) == -1 ||
fcntl( nntpOutPipe[0], F_SETFL, O_NONBLOCK ) == -1 ) {
KMessageBox::error(knGlobals.topWidget, i18n("Internal error:\nFailed to open pipes for internal communication."));
kapp->exit(1);
}
nntpNotifier=new TQSocketNotifier(nntpInPipe[0], TQSocketNotifier::Read);
connect(nntpNotifier, TQT_SIGNAL(activated(int)), this, TQT_SLOT(slotThreadSignal(int)));
// initialize the KSocks stuff in the main thread, otherwise we get
// strange effects on FreeBSD
(void) KSocks::self();
nntpClient=new KNNntpClient(nntpOutPipe[0],nntpInPipe[1],nntp_Mutex);
nntpClient->start();
connect( knGlobals.accountManager(), TQT_SIGNAL(passwordsChanged()), TQT_SLOT(slotPasswordsChanged()) );
}
KNNetAccess::~KNNetAccess()
{
disconnect(nntpNotifier, TQT_SIGNAL(activated(int)), this, TQT_SLOT(slotThreadSignal(int)));
nntpClient->terminateClient();
triggerAsyncThread(nntpOutPipe[1]);
nntpClient->wait();
delete nntpClient;
delete nntpNotifier;
if ( ::close(nntpInPipe[0]) == -1 ||
::close(nntpInPipe[1]) == -1 ||
::close(nntpOutPipe[0]) == -1 ||
::close(nntpOutPipe[1]) == -1 )
kdDebug(5003) << "Can't close pipes" << endl;
}
void KNNetAccess::addJob(KNJobData *job)
{
// kdDebug(5003) << "KNNetAccess::addJob() : job queued" << endl;
if(job->account()==0) {
job->setErrorString(i18n("Internal Error: No account set for this job."));
job->notifyConsumer();
return;
}
job->createProgressItem();
connect( job->progressItem(), TQT_SIGNAL(progressItemCanceled(KPIM::ProgressItem*)), TQT_SLOT(slotCancelJob(KPIM::ProgressItem*)) );
emit netActive( true );
// put jobs which are waiting for the wallet into an extra queue
if ( !job->account()->readyForLogin() ) {
mWalletQueue.append( job );
knGlobals.accountManager()->loadPasswordsAsync();
job->setStatus( i18n( "Waiting for KWallet..." ) );
return;
}
if (job->type()==KNJobData::JTmail) {
smtpJobQueue.append(job);
if (!currentSmtpJob) // no active job, start the new one
startJobSmtp();
} else {
/*
TODO: the following code doesn't really belong here, it should
be moved to KNGroupManager, or elsewere...
*/
// avoid duplicate fetchNewHeader jobs...
bool duplicate = false;
if ( job->type() == KNJobData::JTfetchNewHeaders || job->type() == KNJobData::JTsilentFetchNewHeaders ) {
TQValueList<KNJobData*>::ConstIterator it;
for ( it = nntpJobQueue.begin(); it != nntpJobQueue.end(); ++it ) {
if ( ( (*it)->type() == KNJobData::JTfetchNewHeaders || (*it)->type() == KNJobData::JTsilentFetchNewHeaders )
&& (*it)->data() == job->data() ) // job works on the same group...
duplicate = true;
}
}
if (!duplicate) {
// give a lower priority to fetchNewHeaders and postArticle jobs
if ( job->type() == KNJobData::JTfetchNewHeaders
|| job->type() == KNJobData::JTsilentFetchNewHeaders
|| job->type() == KNJobData::JTpostArticle ) {
nntpJobQueue.append( job );
} else {
nntpJobQueue.prepend( job );
}
if (!currentNntpJob) // no active job, start the new one
startJobNntp();
}
}
updateStatus();
}
void KNNetAccess::cancelCurrentNntpJob( int type )
{
if ((currentNntpJob && !currentNntpJob->canceled()) && ((type==0)||(currentNntpJob->type()==type))) { // stop active job
currentNntpJob->cancel();
triggerAsyncThread(nntpOutPipe[1]);
}
}
void KNNetAccess::stopJobsNntp( int type )
{
cancelCurrentNntpJob( type );
KNJobData *tmp = 0;
TQValueList<KNJobData*>::Iterator it;
for ( it = nntpJobQueue.begin(); it != nntpJobQueue.end();) {
tmp = *it;
if ( type == 0 || tmp->type() == type ) {
it = nntpJobQueue.remove( it );
tmp->cancel();
tmp->notifyConsumer();
} else
++it;
}
for ( it = mWalletQueue.begin(); it != mWalletQueue.end();) {
tmp = *it;
if ( type == 0 || tmp->type() == type ) {
it = mWalletQueue.remove( it );
tmp->cancel();
tmp->notifyConsumer();
} else
++it;
}
updateStatus();
}
// type==0 => all jobs
void KNNetAccess::cancelCurrentSmtpJob( int type )
{
if ((currentSmtpJob && !currentSmtpJob->canceled()) && ((type==0)||(currentSmtpJob->type()==type))) { // stop active job
currentSmtpJob->cancel();
threadDoneSmtp();
}
}
void KNNetAccess::stopJobsSmtp( int type )
{
cancelCurrentSmtpJob( type );
KNJobData *tmp = 0;
TQValueList<KNJobData*>::Iterator it;
for ( it = smtpJobQueue.begin(); it != smtpJobQueue.end();) {
tmp = *it;
if ( type == 0 || tmp->type() == type ) {
it = smtpJobQueue.remove( it );
tmp->cancel();
tmp->notifyConsumer();
} else
++it;
}
updateStatus();
}
// passes a signal through the ipc-pipe to the net-thread
void KNNetAccess::triggerAsyncThread(int pipeFd)
{
int signal=0;
// kdDebug(5003) << "KNNetAccess::triggerAsyncThread() : sending signal to net thread" << endl;
write(pipeFd, &signal, sizeof(int));
}
void KNNetAccess::startJobNntp()
{
if ( nntpJobQueue.isEmpty() )
return;
currentNntpJob = nntpJobQueue.first();
nntpJobQueue.remove( nntpJobQueue.begin() );
currentNntpJob->prepareForExecution();
if (currentNntpJob->success()) {
nntpClient->insertJob(currentNntpJob);
triggerAsyncThread(nntpOutPipe[1]);
kdDebug(5003) << "KNNetAccess::startJobNntp(): job started" << endl;
} else {
threadDoneNntp();
}
}
void KNNetAccess::startJobSmtp()
{
if ( smtpJobQueue.isEmpty() )
return;
currentSmtpJob = smtpJobQueue.first();
smtpJobQueue.remove( smtpJobQueue.begin() );
currentSmtpJob->prepareForExecution();
if (currentSmtpJob->success()) {
KNLocalArticle *art = static_cast<KNLocalArticle*>( currentSmtpJob->data() );
// create url query part
TQString query("headers=0&from=");
query += KURL::encode_string( art->from()->email() );
TQStrList emails;
art->to()->emails( &emails );
for ( char *e = emails.first(); e; e = emails.next() ) {
query += "&to=" + KURL::encode_string( e );
}
// create url
KURL destination;
KNServerInfo *account = currentSmtpJob->account();
if ( account->encryption() == KNServerInfo::SSL )
destination.setProtocol( "smtps" );
else
destination.setProtocol( "smtp" );
destination.setHost( account->server() );
destination.setPort( account->port() );
destination.setQuery( query );
if ( account->needsLogon() ) {
destination.setUser( account->user() );
destination.setPass( account->pass() );
}
TDEIO::Job* job = TDEIO::storedPut( art->encodedContent(true), destination, -1, false, false, false );
connect( job, TQT_SIGNAL( result(TDEIO::Job*) ),
TQT_SLOT( slotJobResult(TDEIO::Job*) ) );
if ( account->encryption() == KNServerInfo::TLS )
job->addMetaData( "tls", "on" );
else
job->addMetaData( "tls", "off" );
currentSmtpJob->setJob( job );
kdDebug(5003) << "KNNetAccess::startJobSmtp(): job started" << endl;
} else {
threadDoneSmtp();
}
}
void KNNetAccess::threadDoneNntp()
{
KNJobData *tmp;
if (!currentNntpJob) {
kdWarning(5003) << "KNNetAccess::threadDoneNntp(): no current job?? aborting" << endl;
return;
}
kdDebug(5003) << "KNNetAccess::threadDoneNntp(): job done" << endl;
tmp = currentNntpJob;
if (!tmp->success() && tmp->authError()) {
kdDebug(5003) << "KNNetAccess::threadDoneNntp(): authentication error" << endl;
KNServerInfo *info = tmp->account();
if (info) {
TQString user = info->user();
TQString pass = info->pass();
bool keep=false;
if (KDialog::Accepted == TDEIO::PasswordDialog::getNameAndPassword(user, pass, &keep,
i18n("You need to supply a username and a\npassword to access this server"), false,
kapp->makeStdCaption(i18n("Authentication Failed")),info->server(),i18n("Server:"))) {
info->setNeedsLogon(true);
info->setUser(user);
info->setPass(pass);
tmp->setAuthError(false);
tmp->setErrorString(TQString());
kdDebug(5003) << "KNNetAccess::threadDoneNntp(): trying again with authentication data" << endl;
// restart job...
triggerAsyncThread(nntpOutPipe[1]);
return;
}
}
}
nntpClient->removeJob();
currentNntpJob = 0L;
currMsg = TQString();
knGlobals.setStatusMsg();
tmp->setComplete();
tmp->notifyConsumer();
if (!nntpJobQueue.isEmpty())
startJobNntp();
updateStatus();
}
void KNNetAccess::threadDoneSmtp()
{
KNJobData *tmp;
if (!currentSmtpJob) {
kdWarning(5003) << "KNNetAccess::threadDoneSmtp(): no current job?? aborting" << endl;
return;
}
kdDebug(5003) << "KNNetAccess::threadDoneSmtp(): job done" << endl;
tmp = currentSmtpJob;
currentSmtpJob = 0L;
if (!currentNntpJob) {
currMsg = TQString();
knGlobals.setStatusMsg();
}
tmp->setComplete();
tmp->notifyConsumer();
if (!smtpJobQueue.isEmpty())
startJobSmtp();
updateStatus();
}
void KNNetAccess::cancelAllJobs()
{
stopJobsNntp(0);
stopJobsSmtp(0);
}
void KNNetAccess::slotThreadSignal(int i)
{
int signal;
TQString tmp;
//kdDebug(5003) << "KNNetAccess::slotThreadSignal() : signal received from net thread" << endl;
if(read(i, &signal, sizeof(int))==-1) {
kdDebug(5003) << "KNNetAccess::slotThreadSignal() : cannot read from pipe" << endl;
return;
}
if (i == nntpInPipe[0]) { // signal from nntp thread
switch(signal) {
case KNProtocolClient::TSworkDone:
threadDoneNntp();
break;
case KNProtocolClient::TSconnect:
currMsg = i18n(" Connecting to server...");
knGlobals.setStatusMsg(currMsg);
currentNntpJob->setStatus(currMsg);
break;
case KNProtocolClient::TSloadGrouplist:
currMsg = i18n(" Loading group list from disk...");
knGlobals.setStatusMsg(currMsg);
currentNntpJob->setStatus(currMsg);
break;
case KNProtocolClient::TSwriteGrouplist:
currMsg = i18n(" Writing group list to disk...");
knGlobals.setStatusMsg(currMsg);
currentNntpJob->setStatus(currMsg);
break;
case KNProtocolClient::TSdownloadGrouplist:
currMsg = i18n(" Downloading group list...");
knGlobals.setStatusMsg(currMsg);
currentNntpJob->setStatus(currMsg);
break;
case KNProtocolClient::TSdownloadNewGroups:
currMsg = i18n(" Looking for new groups...");
knGlobals.setStatusMsg(currMsg);
currentNntpJob->setStatus(currMsg);
break;
case KNProtocolClient::TSdownloadDesc:
currMsg = i18n(" Downloading group descriptions...");
knGlobals.setStatusMsg(currMsg);
currentNntpJob->setStatus(currMsg);
break;
case KNProtocolClient::TSdownloadNew:
currMsg = i18n(" Downloading new headers...");
knGlobals.setStatusMsg(currMsg);
currentNntpJob->setStatus(currMsg);
break;
case KNProtocolClient::TSsortNew:
currMsg = i18n(" Sorting...");
knGlobals.setStatusMsg(currMsg);
currentNntpJob->setStatus(currMsg);
break;
case KNProtocolClient::TSdownloadArticle:
currMsg = i18n(" Downloading article...");
knGlobals.setStatusMsg(currMsg);
currentNntpJob->setStatus(currMsg);
break;
case KNProtocolClient::TSsendArticle:
currMsg = i18n(" Sending article...");
knGlobals.setStatusMsg(currMsg);
currentNntpJob->setStatus(currMsg);
break;
case KNProtocolClient::TSjobStarted:
currentNntpJob->setProgress(0);
break;
case KNProtocolClient::TSprogressUpdate:
currentNntpJob->setProgress(nntpClient->getProgressValue()/10);
break;
};
}
}
void KNNetAccess::slotJobResult( TDEIO::Job *job )
{
if ( job == currentSmtpJob->job() ) {
if ( job->error() )
currentSmtpJob->setErrorString( job->errorString() );
threadDoneSmtp();
return;
}
if ( job == currentNntpJob->job() ) {
// TODO
return;
}
kdError(5003) << k_funcinfo << "unknown job" << endl;
}
void KNNetAccess::slotPasswordsChanged()
{
TQValueList<KNJobData*>::ConstIterator it;
for ( it = mWalletQueue.begin(); it != mWalletQueue.end(); ++it ) {
(*it)->setStatus( i18n("Waiting...") );
if ( (*it)->type() == KNJobData::JTmail )
smtpJobQueue.append( (*it) );
else
nntpJobQueue.append( (*it) );
}
mWalletQueue.clear();
if ( !currentNntpJob )
startJobNntp();
if ( !currentSmtpJob )
startJobSmtp();
}
void KNNetAccess::slotCancelJob( KPIM::ProgressItem *item )
{
KNJobData *tmp = 0;
TQValueList<KNJobData*>::Iterator it;
for ( it = nntpJobQueue.begin(); it != nntpJobQueue.end();) {
tmp = *it;
if ( tmp->progressItem() == item ) {
it = nntpJobQueue.remove( it );
tmp->cancel();
tmp->notifyConsumer();
} else
++it;
}
for ( it = smtpJobQueue.begin(); it != smtpJobQueue.end();) {
tmp = *it;
if ( tmp->progressItem() == item ) {
it = smtpJobQueue.remove( it );
tmp->cancel();
tmp->notifyConsumer();
} else
++it;
}
for ( it = mWalletQueue.begin(); it != mWalletQueue.end();) {
tmp = *it;
if ( tmp->progressItem() == item ) {
it = mWalletQueue.remove( it );
tmp->cancel();
tmp->notifyConsumer();
} else
++it;
}
if ( currentNntpJob && currentNntpJob->progressItem() == item )
cancelCurrentNntpJob();
if ( currentSmtpJob && currentSmtpJob->progressItem() == item )
cancelCurrentSmtpJob();
updateStatus();
}
void KNNetAccess::updateStatus( )
{
if ( nntpJobQueue.isEmpty() && smtpJobQueue.isEmpty() && !currentNntpJob
&& !currentSmtpJob && mWalletQueue.isEmpty() )
emit netActive( false );
else
emit netActive( true );
}
//--------------------------------
#include "knnetaccess.moc"
|