kmail

managesievescriptsdialog.cpp
1 #include "managesievescriptsdialog.h"
2 #include "managesievescriptsdialog_p.h"
3 
4 #include "sieveconfig.h"
5 #include "accountmanager.h"
6 #include "imapaccountbase.h"
7 #include "sievejob.h"
8 #include "kmkernel.h"
9 
10 #include <tdelocale.h>
11 #include <kiconloader.h>
12 #include <twin.h>
13 #include <tdeapplication.h>
14 #include <kinputdialog.h>
15 #include <tdeglobalsettings.h>
16 #include <tdemessagebox.h>
17 
18 #include <tqlayout.h>
19 #include <tqlistview.h>
20 #include <tqtextedit.h>
21 #include <tqpopupmenu.h>
22 
23 #include <cassert>
24 
25 inline TQCheckListItem * qcli_cast( TQListViewItem * lvi ) {
26  return lvi && lvi->rtti() == 1 ? static_cast<TQCheckListItem*>( lvi ) : 0 ;
27 }
28 inline const TQCheckListItem * qcli_cast( const TQListViewItem * lvi ) {
29  return lvi && lvi->rtti() == 1 ? static_cast<const TQCheckListItem*>( lvi ) : 0 ;
30 }
31 
32 KMail::ManageSieveScriptsDialog::ManageSieveScriptsDialog( TQWidget * parent, const char * name )
33  : KDialogBase( Plain, i18n( "Manage Sieve Scripts" ), Close, Close,
34  parent, name, false ),
35  mSieveEditor( 0 ),
36  mContextMenuItem( 0 ),
37  mWasActive( false )
38 {
39  setWFlags( WGroupLeader|WDestructiveClose );
40  KWin::setIcons( winId(), kapp->icon(), kapp->miniIcon() );
41 
42  TQVBoxLayout * vlay = new TQVBoxLayout( plainPage(), 0, 0 );
43 
44  mListView = new TQListView( plainPage() );
45  mListView->addColumn( i18n( "Available Scripts" ) );
46  mListView->setResizeMode( TQListView::LastColumn );
47  mListView->setRootIsDecorated( true );
48  mListView->setSelectionMode( TQListView::Single );
49  connect( mListView, TQ_SIGNAL(contextMenuRequested(TQListViewItem*,const TQPoint&,int)),
50  this, TQ_SLOT(slotContextMenuRequested(TQListViewItem*, const TQPoint&)) );
51  connect( mListView, TQ_SIGNAL(doubleClicked(TQListViewItem*,const TQPoint&,int)),
52  this, TQ_SLOT(slotDoubleClicked(TQListViewItem*)) );
53  connect( mListView, TQ_SIGNAL(selectionChanged(TQListViewItem*)),
54  this, TQ_SLOT(slotSelectionChanged(TQListViewItem*)) );
55  vlay->addWidget( mListView );
56 
57  resize( 2 * sizeHint().width(), sizeHint().height() );
58 
59  slotRefresh();
60 }
61 
62 KMail::ManageSieveScriptsDialog::~ManageSieveScriptsDialog() {
63  killAllJobs();
64 }
65 
66 void KMail::ManageSieveScriptsDialog::killAllJobs() {
67  for ( TQMap<SieveJob*,TQCheckListItem*>::const_iterator it = mJobs.constBegin(), end = mJobs.constEnd() ; it != end ; ++it )
68  it.key()->kill();
69  mJobs.clear();
70 }
71 
72 static KURL findUrlForAccount( const KMail::ImapAccountBase * a ) {
73  assert( a );
74  const KMail::SieveConfig sieve = a->sieveConfig();
75  if ( !sieve.managesieveSupported() )
76  return KURL();
77  if ( sieve.reuseConfig() ) {
78  // assemble Sieve url from the settings of the account:
79  KURL u;
80  u.setProtocol( "sieve" );
81  u.setHost( a->host() );
82  u.setUser( a->login() );
83  u.setPass( a->passwd() );
84  u.setPort( sieve.port() );
85  // Translate IMAP LOGIN to PLAIN:
86  u.addQueryItem( "x-mech", a->auth() == "*" ? "PLAIN" : a->auth() );
87  if ( !a->useSSL() && !a->useTLS() )
88  u.addQueryItem( "x-allow-unencrypted", "true" );
89  return u;
90  } else {
91  KURL u = sieve.alternateURL();
92  if ( u.protocol().lower() == "sieve" && !a->useSSL() && !a->useTLS() && u.queryItem("x-allow-unencrypted").isEmpty() )
93  u.addQueryItem( "x-allow-unencrypted", "true" );
94  return u;
95  }
96 }
97 
98 void KMail::ManageSieveScriptsDialog::slotRefresh() {
99  killAllJobs();
100  mUrls.clear();
101  mListView->clear();
102 
103  KMail::AccountManager * am = kmkernel->acctMgr();
104  assert( am );
105  TQCheckListItem * last = 0;
106  for ( KMAccount * a = am->first() ; a ; a = am->next() ) {
107  last = new TQCheckListItem( mListView, last, a->name(), TQCheckListItem::Controller );
108  last->setPixmap( 0, SmallIcon( "server" ) );
109  if ( ImapAccountBase * iab = dynamic_cast<ImapAccountBase*>( a ) ) {
110  const KURL u = ::findUrlForAccount( iab );
111  if ( u.isEmpty() )
112  continue;
113  SieveJob * job = SieveJob::list( u );
114  connect( job, TQ_SIGNAL(item(KMail::SieveJob*,const TQString&,bool)),
115  this, TQ_SLOT(slotItem(KMail::SieveJob*,const TQString&,bool)) );
116  connect( job, TQ_SIGNAL(result(KMail::SieveJob*,bool,const TQString&,bool)),
117  this, TQ_SLOT(slotResult(KMail::SieveJob*,bool,const TQString&,bool)) );
118  mJobs.insert( job, last );
119  mUrls.insert( last, u );
120  } else {
121  TQListViewItem * item = new TQListViewItem( last, i18n( "No Sieve URL configured" ) );
122  item->setEnabled( false );
123  last->setOpen( true );
124  }
125  }
126 }
127 
128 void KMail::ManageSieveScriptsDialog::slotResult( KMail::SieveJob * job, bool success, const TQString &, bool ) {
129  TQCheckListItem * parent = mJobs[job];
130  if ( !parent )
131  return;
132 
133  mJobs.remove( job );
134 
135  parent->setOpen( true );
136 
137  if ( success )
138  return;
139 
140  TQListViewItem * item = new TQListViewItem( parent, i18n( "Failed to fetch the list of scripts" ) );
141  item->setEnabled( false );
142 }
143 
144 void KMail::ManageSieveScriptsDialog::slotItem( KMail::SieveJob * job, const TQString & filename, bool isActive ) {
145  TQCheckListItem * parent = mJobs[job];
146  if ( !parent )
147  return;
148  TQCheckListItem * item = new TQCheckListItem( parent, filename, TQCheckListItem::RadioButton );
149  if ( isActive ) {
150  item->setOn( true );
151  mSelectedItems[parent] = item;
152  }
153 }
154 
155 void KMail::ManageSieveScriptsDialog::slotContextMenuRequested( TQListViewItem * i, const TQPoint & p ) {
156  TQCheckListItem * item = qcli_cast( i );
157  if ( !item )
158  return;
159  if ( !item->depth() && !mUrls.count( item ) )
160  return;
161  TQPopupMenu menu;
162  mContextMenuItem = item;
163  if ( item->depth() ) {
164  // script items:
165  menu.insertItem( i18n( "Delete Script" ), this, TQ_SLOT(slotDeleteScript()) );
166  menu.insertItem( i18n( "Edit Script..." ), this, TQ_SLOT(slotEditScript()) );
167  menu.insertItem( i18n( "Deactivate Script" ), this, TQ_SLOT(slotDeactivateScript()) );
168  } else {
169  // top-levels:
170  menu.insertItem( i18n( "New Script..." ), this, TQ_SLOT(slotNewScript()) );
171  }
172  menu.exec( p );
173  mContextMenuItem = 0;
174 }
175 
176 
177 void KMail::ManageSieveScriptsDialog::slotDeactivateScript() {
178  if ( !mContextMenuItem )
179  return;
180 
181  TQCheckListItem * parent = qcli_cast( mContextMenuItem->parent() );
182  if ( !parent )
183  return;
184  if ( mContextMenuItem->isOn()) {
185  mSelectedItems[parent] = mContextMenuItem;
186  changeActiveScript( parent,false );
187  }
188 }
189 
190 void KMail::ManageSieveScriptsDialog::slotSelectionChanged( TQListViewItem * i ) {
191  TQCheckListItem * item = qcli_cast( i );
192  if ( !item )
193  return;
194  TQCheckListItem * parent = qcli_cast( item->parent() );
195  if ( !parent )
196  return;
197  if ( item->isOn() && mSelectedItems[parent] != item ) {
198  mSelectedItems[parent] = item;
199  changeActiveScript( parent,true );
200  }
201 }
202 
203 void KMail::ManageSieveScriptsDialog::changeActiveScript( TQCheckListItem * item , bool activate) {
204  if ( !item )
205  return;
206  if ( !mUrls.count( item ) )
207  return;
208  if ( !mSelectedItems.count( item ) )
209  return;
210  KURL u = mUrls[item];
211  if ( u.isEmpty() )
212  return;
213  TQCheckListItem * selected = mSelectedItems[item];
214  if ( !selected )
215  return;
216  u.setFileName( selected->text( 0 ) );
217  SieveJob * job;
218  if ( activate )
219  job = SieveJob::activate( u );
220  else
221  job = SieveJob::desactivate( u );
222  connect( job, TQ_SIGNAL(result(KMail::SieveJob*,bool,const TQString&,bool)),
223  this, TQ_SLOT(slotRefresh()) );
224 }
225 
226 void KMail::ManageSieveScriptsDialog::slotDoubleClicked( TQListViewItem * i ) {
227  TQCheckListItem * item = qcli_cast( i );
228  if ( !item )
229  return;
230  if ( !item->depth() )
231  return;
232  mContextMenuItem = item;
233  slotEditScript();
234  mContextMenuItem = 0;
235 }
236 
237 void KMail::ManageSieveScriptsDialog::slotDeleteScript() {
238  if ( !mContextMenuItem )
239  return;
240  if ( !mContextMenuItem->depth() )
241  return;
242 
243  TQCheckListItem * parent = qcli_cast( mContextMenuItem->parent() );
244  if ( !parent )
245  return;
246 
247  if ( !mUrls.count( parent ) )
248  return;
249 
250  KURL u = mUrls[parent];
251  if ( u.isEmpty() )
252  return;
253 
254  u.setFileName( mContextMenuItem->text( 0 ) );
255 
256  if ( KMessageBox::warningContinueCancel( this, i18n( "Really delete script \"%1\" from the server?" ).arg( u.fileName() ),
257  i18n( "Delete Sieve Script Confirmation" ),
258  KStdGuiItem::del() )
259  != KMessageBox::Continue )
260  return;
261  SieveJob * job = SieveJob::del( u );
262  connect( job, TQ_SIGNAL(result(KMail::SieveJob*,bool,const TQString&,bool)),
263  this, TQ_SLOT(slotRefresh()) );
264 }
265 
266 void KMail::ManageSieveScriptsDialog::slotEditScript() {
267  if ( !mContextMenuItem )
268  return;
269  if ( !mContextMenuItem->depth() )
270  return;
271  TQCheckListItem * parent = qcli_cast( mContextMenuItem->parent() );
272  if ( !mUrls.count( parent ) )
273  return;
274  KURL url = mUrls[parent];
275  if ( url.isEmpty() )
276  return;
277  url.setFileName( mContextMenuItem->text( 0 ) );
278  mCurrentURL = url;
279  SieveJob * job = SieveJob::get( url );
280  connect( job, TQ_SIGNAL(result(KMail::SieveJob*,bool,const TQString&,bool)),
281  this, TQ_SLOT(slotGetResult(KMail::SieveJob*,bool,const TQString&,bool)) );
282 }
283 
284 void KMail::ManageSieveScriptsDialog::slotNewScript() {
285  if ( !mContextMenuItem )
286  return;
287  if ( mContextMenuItem->depth() )
288  mContextMenuItem = qcli_cast( mContextMenuItem->parent() );
289  if ( !mContextMenuItem )
290  return;
291 
292  if ( !mUrls.count( mContextMenuItem ) )
293  return;
294 
295  KURL u = mUrls[mContextMenuItem];
296  if ( u.isEmpty() )
297  return;
298 
299  bool ok = false;
300  const TQString name = KInputDialog::getText( i18n( "New Sieve Script" ),
301  i18n( "Please enter a name for the new Sieve script:" ),
302  i18n( "unnamed" ), &ok, this );
303  if ( !ok || name.isEmpty() )
304  return;
305 
306  u.setFileName( name );
307 
308  (void) new TQCheckListItem( mContextMenuItem, name, TQCheckListItem::RadioButton );
309 
310  mCurrentURL = u;
311  slotGetResult( 0, true, TQString(), false );
312 }
313 
314 KMail::SieveEditor::SieveEditor( TQWidget * parent, const char * name )
315  : KDialogBase( Plain, i18n( "Edit Sieve Script" ), Ok|Cancel, Ok, parent, name )
316 {
317  TQVBoxLayout * vlay = new TQVBoxLayout( plainPage(), 0, spacingHint() );
318  mTextEdit = new TQTextEdit( plainPage() );
319  vlay->addWidget( mTextEdit );
320  mTextEdit->setFocus();
321  mTextEdit->setTextFormat( TQTextEdit::PlainText );
322  mTextEdit->setWordWrap( TQTextEdit::NoWrap );
323  mTextEdit->setFont( TDEGlobalSettings::fixedFont() );
324  connect( mTextEdit, TQ_SIGNAL( textChanged () ), TQ_SLOT( slotTextChanged() ) );
325  resize( 3 * sizeHint() );
326 }
327 
328 KMail::SieveEditor::~SieveEditor() {}
329 
330 
331 void KMail::SieveEditor::slotTextChanged()
332 {
333  enableButtonOK( !script().isEmpty() );
334 }
335 
336 void KMail::ManageSieveScriptsDialog::slotGetResult( KMail::SieveJob *, bool success, const TQString & script, bool isActive ) {
337  if ( !success )
338  return;
339 
340  if ( mSieveEditor )
341  return;
342 
343  mSieveEditor = new SieveEditor( this );
344  mSieveEditor->setScript( script );
345  connect( mSieveEditor, TQ_SIGNAL(okClicked()), this, TQ_SLOT(slotSieveEditorOkClicked()) );
346  connect( mSieveEditor, TQ_SIGNAL(cancelClicked()), this, TQ_SLOT(slotSieveEditorCancelClicked()) );
347  mSieveEditor->show();
348  mWasActive = isActive;
349 }
350 
351 void KMail::ManageSieveScriptsDialog::slotSieveEditorOkClicked() {
352  if ( !mSieveEditor )
353  return;
354  SieveJob * job = SieveJob::put( mCurrentURL,mSieveEditor->script(), mWasActive, mWasActive );
355  connect( job, TQ_SIGNAL(result(KMail::SieveJob*,bool,const TQString&,bool)),
356  this, TQ_SLOT(slotPutResult(KMail::SieveJob*,bool)) );
357 }
358 
359 void KMail::ManageSieveScriptsDialog::slotSieveEditorCancelClicked() {
360  mSieveEditor->deleteLater(); mSieveEditor = 0;
361  mCurrentURL = KURL();
362  slotRefresh();
363 }
364 
365 void KMail::ManageSieveScriptsDialog::slotPutResult( KMail::SieveJob *, bool success ) {
366  if ( success ) {
367  KMessageBox::information( this, i18n( "The Sieve script was successfully uploaded." ),
368  i18n( "Sieve Script Upload" ) );
369  mSieveEditor->deleteLater(); mSieveEditor = 0;
370  mCurrentURL = KURL();
371  } else {
372  mSieveEditor->show();
373  }
374 }
375 
376 #include "managesievescriptsdialog.moc"
377 #include "managesievescriptsdialog_p.moc"
The account manager is responsible for creating accounts of various types via the factory method crea...
const KMAccount * first() const
First account of the list.
const KMAccount * next() const
Next account of the list.
@ Ok
The user rights/ACL have been fetched from the server sucessfully.
Definition: acljobs.h:66