kmail

sievedebugdialog.cpp
1/*
2 sievedebugdialog.cpp
3
4 KMail, the KDE mail client.
5 Copyright (c) 2005 Martijn Klingens <klingens@kde.org>
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License,
9 version 2.0, as published by the Free Software Foundation.
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// This file is only compiled when debug is enabled, it is
16// not useful enough for non-developers to have this in releases.
17#if !defined(NDEBUG)
18
19#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22
23#include "sievedebugdialog.h"
24
25#include <cassert>
26#include <limits.h>
27
28#include <tqdatetime.h>
29
30#include <kdebug.h>
31#include <tdelocale.h>
32#include <tdemessagebox.h>
33
34#include <kmime_header_parsing.h>
35#include <ksieve/error.h>
36#include <ksieve/parser.h>
37#include <ksieve/scriptbuilder.h>
38#include <libkpimidentities/identity.h>
39#include <libkpimidentities/identitymanager.h>
40
41#include "kmacctimap.h"
42#include "accountmanager.h"
44#include "kmkernel.h"
45#include "sievejob.h"
46#include <tqtextedit.h>
47
48using KMail::SieveJob;
49using KMime::Types::AddrSpecList;
50
51namespace
52{
53
54class SieveDebugDataExtractor : public KSieve::ScriptBuilder
55{
56 enum Context
57 {
58 None = 0,
59
60 // command itself:
61 SieveDebugCommand,
62
63 // tagged args:
64 Days, Addresses
65 };
66
67public:
68 SieveDebugDataExtractor()
69 : KSieve::ScriptBuilder()
70 {
71 kdDebug( 5006 ) << k_funcinfo << endl;
72 }
73
74 virtual ~SieveDebugDataExtractor()
75 {
76 kdDebug( 5006 ) << k_funcinfo << endl;
77 }
78
79private:
80 void commandStart( const TQString & identifier )
81 {
82 kdDebug( 5006 ) << k_funcinfo << "Identifier: '" << identifier << "'" << endl;
83 reset();
84 }
85
86 void commandEnd()
87 {
88 kdDebug( 5006 ) << k_funcinfo << endl;
89 }
90
91 void testStart( const TQString & )
92 {
93 kdDebug( 5006 ) << k_funcinfo << endl;
94 }
95
96 void testEnd()
97 {
98 kdDebug( 5006 ) << k_funcinfo << endl;
99 }
100
101 void testListStart()
102 {
103 kdDebug( 5006 ) << k_funcinfo << endl;
104 }
105
106 void testListEnd()
107 {
108 kdDebug( 5006 ) << k_funcinfo << endl;
109 }
110
111 void blockStart()
112 {
113 kdDebug( 5006 ) << k_funcinfo << endl;
114 }
115
116 void blockEnd()
117 {
118 kdDebug( 5006 ) << k_funcinfo << endl;
119 }
120
121 void hashComment( const TQString & )
122 {
123 kdDebug( 5006 ) << k_funcinfo << endl;
124 }
125
126 void bracketComment( const TQString & )
127 {
128 kdDebug( 5006 ) << k_funcinfo << endl;
129 }
130
131 void lineFeed()
132 {
133 kdDebug( 5006 ) << k_funcinfo << endl;
134 }
135
136 void error( const KSieve::Error & e )
137 {
138 kdDebug( 5006 ) << "### " << k_funcinfo << "Error: " <<
139 e.asString() << " @ " << e.line() << "," << e.column() << endl;
140 }
141
142 void finished()
143 {
144 kdDebug( 5006 ) << k_funcinfo << endl;
145 }
146
147 void taggedArgument( const TQString & tag )
148 {
149 kdDebug( 5006 ) << k_funcinfo << "Tag: '" << tag << "'" << endl;
150 }
151
152 void stringArgument( const TQString & string, bool, const TQString & )
153 {
154 kdDebug( 5006 ) << k_funcinfo << "String: '" << string << "'" << endl;
155 }
156
157 void numberArgument( unsigned long number, char )
158 {
159 kdDebug( 5006 ) << k_funcinfo << "Number: " << number << endl;
160 }
161
162 void stringListArgumentStart()
163 {
164 kdDebug( 5006 ) << k_funcinfo << endl;
165 }
166
167 void stringListEntry( const TQString & string, bool, const TQString & )
168 {
169 kdDebug( 5006 ) << k_funcinfo << "String: '" << string << "'" << endl;
170 }
171
172 void stringListArgumentEnd()
173 {
174 kdDebug( 5006 ) << k_funcinfo << endl;
175 }
176
177private:
178 void reset()
179 {
180 kdDebug( 5006 ) << k_funcinfo << endl;
181 }
182};
183
184} // Anon namespace
185
186namespace KMail
187{
188
189SieveDebugDialog::SieveDebugDialog( TQWidget *parent, const char *name )
190: KDialogBase( parent, name, true, i18n( "Sieve Diagnostics" ), KDialogBase::Ok,
191 KDialogBase::Ok, true ),
192 mSieveJob( 0 )
193{
194 // Collect all accounts
195 AccountManager *am = kmkernel->acctMgr();
196 assert( am );
197 for ( KMAccount *a = am->first(); a; a = am->next() )
198 mAccountList.append( a );
199
200 mEdit = new TQTextEdit( this );
201 mEdit->setReadOnly(true);
202 setMainWidget( mEdit );
203
204 mEdit->setText( i18n( "Collecting diagnostic information about Sieve support...\n\n" ) );
205
206 setInitialSize( TQSize( 640, 480 ) );
207
208 if ( !mAccountList.isEmpty() )
209 TQTimer::singleShot( 0, this, TQ_SLOT( slotDiagNextAccount() ) );
210}
211
212SieveDebugDialog::~SieveDebugDialog()
213{
214 if ( mSieveJob )
215 {
216 mSieveJob->kill();
217 mSieveJob = 0;
218 }
219 kdDebug( 5006 ) << k_funcinfo << endl;
220}
221
222static KURL urlFromAccount( const KMail::ImapAccountBase * a ) {
223 const SieveConfig sieve = a->sieveConfig();
224 if ( !sieve.managesieveSupported() )
225 return KURL();
226
227 KURL u;
228 if ( sieve.reuseConfig() ) {
229 // assemble Sieve url from the settings of the account:
230 u.setProtocol( "sieve" );
231 u.setHost( a->host() );
232 u.setUser( a->login() );
233 u.setPass( a->passwd() );
234 u.setPort( sieve.port() );
235
236 // Translate IMAP LOGIN to PLAIN:
237 u.addQueryItem( "x-mech", a->auth() == "*" ? "PLAIN" : a->auth() );
238 if ( !a->useSSL() && !a->useTLS() )
239 u.addQueryItem( "x-allow-unencrypted", "true" );
240 } else {
241 u = sieve.alternateURL();
242 if ( u.protocol().lower() == "sieve" && !a->useSSL() && !a->useTLS() && u.queryItem("x-allow-unencrypted").isEmpty() )
243 u.addQueryItem( "x-allow-unencrypted", "true" );
244 }
245 return u;
246}
247
248void SieveDebugDialog::slotDiagNextAccount()
249{
250 if ( mAccountList.isEmpty() )
251 return;
252
253 KMAccount *acc = mAccountList.first();
254 mAccountList.pop_front();
255
256 mEdit->append( i18n( "Collecting data for account '%1'...\n" ).arg( acc->name() ) );
257 mEdit->append( i18n( "------------------------------------------------------------\n" ) );
258 mAccountBase = dynamic_cast<KMail::ImapAccountBase *>( acc );
259 if ( mAccountBase )
260 {
261 // Detect URL for this IMAP account
262 const KURL url = urlFromAccount( mAccountBase );
263 if ( !url.isValid() )
264 {
265 mEdit->append( i18n( "(Account does not support Sieve)\n\n" ) );
266 } else {
267 mUrl = url;
268
269 mSieveJob = SieveJob::list( mUrl );
270
271 connect( mSieveJob, TQ_SIGNAL( gotList( KMail::SieveJob *, bool, const TQStringList &, const TQString & ) ),
272 TQ_SLOT( slotGetScriptList( KMail::SieveJob *, bool, const TQStringList &, const TQString & ) ) );
273
274 // Bypass the singleShot timer -- it's fired when we get our data
275 return;
276 }
277 } else {
278 mEdit->append( i18n( "(Account is not an IMAP account)\n\n" ) );
279 }
280
281 // Handle next account async
282 TQTimer::singleShot( 0, this, TQ_SLOT( slotDiagNextAccount() ) );
283}
284
285void SieveDebugDialog::slotDiagNextScript()
286{
287 if ( mScriptList.isEmpty() )
288 {
289 // Continue handling accounts instead
290 mScriptList.clear();
291 TQTimer::singleShot( 0, this, TQ_SLOT( slotDiagNextAccount() ) );
292 return;
293 }
294
295 TQString scriptFile = mScriptList.first();
296 mScriptList.pop_front();
297
298 mEdit->append( i18n( "Contents of script '%1':\n" ).arg( scriptFile ) );
299
300 mUrl = urlFromAccount( mAccountBase );
301 mUrl.setFileName( scriptFile );
302
303 mSieveJob = SieveJob::get( mUrl );
304
305 connect( mSieveJob, TQ_SIGNAL( gotScript( KMail::SieveJob *, bool, const TQString &, bool ) ),
306 TQ_SLOT( slotGetScript( KMail::SieveJob *, bool, const TQString &, bool ) ) );
307}
308
309void SieveDebugDialog::slotGetScript( SieveJob * /* job */, bool success,
310 const TQString &script, bool active )
311{
312 kdDebug( 5006 ) << "SieveDebugDialog::slotGetScript( ??, " << success
313 << ", ?, " << active << " )" << endl
314 << "script:" << endl
315 << script << endl;
316 mSieveJob = 0; // job deletes itself after returning from this slot!
317
318 if ( script.isEmpty() )
319 {
320 mEdit->append( i18n( "(This script is empty.)\n\n" ) );
321 }
322 else
323 {
324 mEdit->append( i18n(
325 "------------------------------------------------------------\n"
326 "%1\n"
327 "------------------------------------------------------------\n\n" ).arg( script ) );
328 }
329
330 // Fetch next script
331 TQTimer::singleShot( 0, this, TQ_SLOT( slotDiagNextScript() ) );
332}
333
334void SieveDebugDialog::slotGetScriptList( SieveJob *job, bool success,
335 const TQStringList &scriptList, const TQString &activeScript )
336{
337 kdDebug( 5006 ) << k_funcinfo << "Success: " << success << ", List: " << scriptList.join( ", " ) <<
338 ", active: " << activeScript << endl;
339 mSieveJob = 0; // job deletes itself after returning from this slot!
340
341 mEdit->append( i18n( "Sieve capabilities:\n" ) );
342 TQStringList caps = job->sieveCapabilities();
343 if ( caps.isEmpty() )
344 {
345 mEdit->append( i18n( "(No special capabilities available)" ) );
346 }
347 else
348 {
349 for ( TQStringList::const_iterator it = caps.begin(); it != caps.end(); ++it )
350 mEdit->append( "* " + *it + "\n" );
351 mEdit->append( "\n" );
352 }
353
354 mEdit->append( i18n( "Available Sieve scripts:\n" ) );
355
356 if ( scriptList.isEmpty() )
357 {
358 mEdit->append( i18n( "(No Sieve scripts available on this server)\n\n" ) );
359 }
360 else
361 {
362 mScriptList = scriptList;
363 for ( TQStringList::const_iterator it = scriptList.begin(); it != scriptList.end(); ++it )
364 mEdit->append( "* " + *it + "\n" );
365 mEdit->append( "\n" );
366 mEdit->append( i18n( "Active script: %1\n\n" ).arg( activeScript ) );
367 }
368
369 // Handle next job: dump scripts for this server
370 TQTimer::singleShot( 0, this, TQ_SLOT( slotDiagNextScript() ) );
371}
372
373void SieveDebugDialog::slotDialogOk()
374{
375 kdDebug(5006) << "SieveDebugDialog::slotDialogOk()" << endl;
376}
377
378void SieveDebugDialog::slotPutActiveResult( SieveJob * job, bool success )
379{
380 handlePutResult( job, success, true );
381}
382
383void SieveDebugDialog::slotPutInactiveResult( SieveJob * job, bool success )
384{
385 handlePutResult( job, success, false );
386}
387
388void SieveDebugDialog::handlePutResult( SieveJob *, bool success, bool activated )
389{
390 if ( success )
391 {
392 KMessageBox::information( 0, activated ? i18n(
393 "Sieve script installed successfully on the server.\n"
394 "Out of Office reply is now active." )
395 : i18n( "Sieve script installed successfully on the server.\n"
396 "Out of Office reply has been deactivated." ) );
397 }
398
399 kdDebug( 5006 ) << "SieveDebugDialog::handlePutResult( ???, " << success << ", ? )" << endl;
400 mSieveJob = 0; // job deletes itself after returning from this slot!
401}
402
403
404} // namespace KMail
405
406#include "sievedebugdialog.moc"
407
408#endif // NDEBUG
409
The account manager is responsible for creating accounts of various types via the factory method crea...
const KMAccount * next() const
Next account of the list.
const KMAccount * first() const
First account of the list.
@ Ok
The user rights/ACL have been fetched from the server sucessfully.
Definition: acljobs.h:66
folderdiaquotatab.h
Definition: aboutdata.cpp:40