managesievescriptsdialog.cpp
00001 #include "managesievescriptsdialog.h" 00002 #include "managesievescriptsdialog_p.h" 00003 00004 #include "sieveconfig.h" 00005 #include "accountmanager.h" 00006 #include "imapaccountbase.h" 00007 #include "sievejob.h" 00008 #include "kmkernel.h" 00009 00010 #include <klocale.h> 00011 #include <kiconloader.h> 00012 #include <kwin.h> 00013 #include <kapplication.h> 00014 #include <kinputdialog.h> 00015 #include <kglobalsettings.h> 00016 #include <kmessagebox.h> 00017 00018 #include <tqlayout.h> 00019 #include <tqlistview.h> 00020 #include <tqtextedit.h> 00021 #include <tqpopupmenu.h> 00022 00023 #include <cassert> 00024 00025 inline TQCheckListItem * qcli_cast( TQListViewItem * lvi ) { 00026 return lvi && lvi->rtti() == 1 ? static_cast<TQCheckListItem*>( lvi ) : 0 ; 00027 } 00028 inline const TQCheckListItem * qcli_cast( const TQListViewItem * lvi ) { 00029 return lvi && lvi->rtti() == 1 ? static_cast<const TQCheckListItem*>( lvi ) : 0 ; 00030 } 00031 00032 KMail::ManageSieveScriptsDialog::ManageSieveScriptsDialog( TQWidget * parent, const char * name ) 00033 : KDialogBase( Plain, i18n( "Manage Sieve Scripts" ), Close, Close, 00034 parent, name, false ), 00035 mSieveEditor( 0 ), 00036 mContextMenuItem( 0 ), 00037 mWasActive( false ) 00038 { 00039 setWFlags( WGroupLeader|WDestructiveClose ); 00040 KWin::setIcons( winId(), kapp->icon(), kapp->miniIcon() ); 00041 00042 TQVBoxLayout * vlay = new TQVBoxLayout( plainPage(), 0, 0 ); 00043 00044 mListView = new TQListView( plainPage() ); 00045 mListView->addColumn( i18n( "Available Scripts" ) ); 00046 mListView->setResizeMode( TQListView::LastColumn ); 00047 mListView->setRootIsDecorated( true ); 00048 mListView->setSelectionMode( TQListView::Single ); 00049 connect( mListView, TQT_SIGNAL(contextMenuRequested(TQListViewItem*,const TQPoint&,int)), 00050 this, TQT_SLOT(slotContextMenuRequested(TQListViewItem*, const TQPoint&)) ); 00051 connect( mListView, TQT_SIGNAL(doubleClicked(TQListViewItem*,const TQPoint&,int)), 00052 this, TQT_SLOT(slotDoubleClicked(TQListViewItem*)) ); 00053 connect( mListView, TQT_SIGNAL(selectionChanged(TQListViewItem*)), 00054 this, TQT_SLOT(slotSelectionChanged(TQListViewItem*)) ); 00055 vlay->addWidget( mListView ); 00056 00057 resize( 2 * sizeHint().width(), sizeHint().height() ); 00058 00059 slotRefresh(); 00060 } 00061 00062 KMail::ManageSieveScriptsDialog::~ManageSieveScriptsDialog() { 00063 killAllJobs(); 00064 } 00065 00066 void KMail::ManageSieveScriptsDialog::killAllJobs() { 00067 for ( TQMap<SieveJob*,TQCheckListItem*>::const_iterator it = mJobs.constBegin(), end = mJobs.constEnd() ; it != end ; ++it ) 00068 it.key()->kill(); 00069 mJobs.clear(); 00070 } 00071 00072 static KURL findUrlForAccount( const KMail::ImapAccountBase * a ) { 00073 assert( a ); 00074 const KMail::SieveConfig sieve = a->sieveConfig(); 00075 if ( !sieve.managesieveSupported() ) 00076 return KURL(); 00077 if ( sieve.reuseConfig() ) { 00078 // assemble Sieve url from the settings of the account: 00079 KURL u; 00080 u.setProtocol( "sieve" ); 00081 u.setHost( a->host() ); 00082 u.setUser( a->login() ); 00083 u.setPass( a->passwd() ); 00084 u.setPort( sieve.port() ); 00085 // Translate IMAP LOGIN to PLAIN: 00086 u.addQueryItem( "x-mech", a->auth() == "*" ? "PLAIN" : a->auth() ); 00087 if ( !a->useSSL() && !a->useTLS() ) 00088 u.addQueryItem( "x-allow-unencrypted", "true" ); 00089 return u; 00090 } else { 00091 KURL u = sieve.alternateURL(); 00092 if ( u.protocol().lower() == "sieve" && !a->useSSL() && !a->useTLS() && u.queryItem("x-allow-unencrypted").isEmpty() ) 00093 u.addQueryItem( "x-allow-unencrypted", "true" ); 00094 return u; 00095 } 00096 } 00097 00098 void KMail::ManageSieveScriptsDialog::slotRefresh() { 00099 killAllJobs(); 00100 mUrls.clear(); 00101 mListView->clear(); 00102 00103 KMail::AccountManager * am = kmkernel->acctMgr(); 00104 assert( am ); 00105 TQCheckListItem * last = 0; 00106 for ( KMAccount * a = am->first() ; a ; a = am->next() ) { 00107 last = new TQCheckListItem( mListView, last, a->name(), TQCheckListItem::Controller ); 00108 last->setPixmap( 0, SmallIcon( "server" ) ); 00109 if ( ImapAccountBase * iab = dynamic_cast<ImapAccountBase*>( a ) ) { 00110 const KURL u = ::findUrlForAccount( iab ); 00111 if ( u.isEmpty() ) 00112 continue; 00113 SieveJob * job = SieveJob::list( u ); 00114 connect( job, TQT_SIGNAL(item(KMail::SieveJob*,const TQString&,bool)), 00115 this, TQT_SLOT(slotItem(KMail::SieveJob*,const TQString&,bool)) ); 00116 connect( job, TQT_SIGNAL(result(KMail::SieveJob*,bool,const TQString&,bool)), 00117 this, TQT_SLOT(slotResult(KMail::SieveJob*,bool,const TQString&,bool)) ); 00118 mJobs.insert( job, last ); 00119 mUrls.insert( last, u ); 00120 } else { 00121 TQListViewItem * item = new TQListViewItem( last, i18n( "No Sieve URL configured" ) ); 00122 item->setEnabled( false ); 00123 last->setOpen( true ); 00124 } 00125 } 00126 } 00127 00128 void KMail::ManageSieveScriptsDialog::slotResult( KMail::SieveJob * job, bool success, const TQString &, bool ) { 00129 TQCheckListItem * parent = mJobs[job]; 00130 if ( !parent ) 00131 return; 00132 00133 mJobs.remove( job ); 00134 00135 parent->setOpen( true ); 00136 00137 if ( success ) 00138 return; 00139 00140 TQListViewItem * item = new TQListViewItem( parent, i18n( "Failed to fetch the list of scripts" ) ); 00141 item->setEnabled( false ); 00142 } 00143 00144 void KMail::ManageSieveScriptsDialog::slotItem( KMail::SieveJob * job, const TQString & filename, bool isActive ) { 00145 TQCheckListItem * parent = mJobs[job]; 00146 if ( !parent ) 00147 return; 00148 TQCheckListItem * item = new TQCheckListItem( parent, filename, TQCheckListItem::RadioButton ); 00149 if ( isActive ) { 00150 item->setOn( true ); 00151 mSelectedItems[parent] = item; 00152 } 00153 } 00154 00155 void KMail::ManageSieveScriptsDialog::slotContextMenuRequested( TQListViewItem * i, const TQPoint & p ) { 00156 TQCheckListItem * item = qcli_cast( i ); 00157 if ( !item ) 00158 return; 00159 if ( !item->depth() && !mUrls.count( item ) ) 00160 return; 00161 TQPopupMenu menu; 00162 mContextMenuItem = item; 00163 if ( item->depth() ) { 00164 // script items: 00165 menu.insertItem( i18n( "Delete Script" ), this, TQT_SLOT(slotDeleteScript()) ); 00166 menu.insertItem( i18n( "Edit Script..." ), this, TQT_SLOT(slotEditScript()) ); 00167 menu.insertItem( i18n( "Deactivate Script" ), this, TQT_SLOT(slotDeactivateScript()) ); 00168 } else { 00169 // top-levels: 00170 menu.insertItem( i18n( "New Script..." ), this, TQT_SLOT(slotNewScript()) ); 00171 } 00172 menu.exec( p ); 00173 mContextMenuItem = 0; 00174 } 00175 00176 00177 void KMail::ManageSieveScriptsDialog::slotDeactivateScript() { 00178 if ( !mContextMenuItem ) 00179 return; 00180 00181 TQCheckListItem * parent = qcli_cast( mContextMenuItem->parent() ); 00182 if ( !parent ) 00183 return; 00184 if ( mContextMenuItem->isOn()) { 00185 mSelectedItems[parent] = mContextMenuItem; 00186 changeActiveScript( parent,false ); 00187 } 00188 } 00189 00190 void KMail::ManageSieveScriptsDialog::slotSelectionChanged( TQListViewItem * i ) { 00191 TQCheckListItem * item = qcli_cast( i ); 00192 if ( !item ) 00193 return; 00194 TQCheckListItem * parent = qcli_cast( item->parent() ); 00195 if ( !parent ) 00196 return; 00197 if ( item->isOn() && mSelectedItems[parent] != item ) { 00198 mSelectedItems[parent] = item; 00199 changeActiveScript( parent,true ); 00200 } 00201 } 00202 00203 void KMail::ManageSieveScriptsDialog::changeActiveScript( TQCheckListItem * item , bool activate) { 00204 if ( !item ) 00205 return; 00206 if ( !mUrls.count( item ) ) 00207 return; 00208 if ( !mSelectedItems.count( item ) ) 00209 return; 00210 KURL u = mUrls[item]; 00211 if ( u.isEmpty() ) 00212 return; 00213 TQCheckListItem * selected = mSelectedItems[item]; 00214 if ( !selected ) 00215 return; 00216 u.setFileName( selected->text( 0 ) ); 00217 SieveJob * job; 00218 if ( activate ) 00219 job = SieveJob::activate( u ); 00220 else 00221 job = SieveJob::desactivate( u ); 00222 connect( job, TQT_SIGNAL(result(KMail::SieveJob*,bool,const TQString&,bool)), 00223 this, TQT_SLOT(slotRefresh()) ); 00224 } 00225 00226 void KMail::ManageSieveScriptsDialog::slotDoubleClicked( TQListViewItem * i ) { 00227 TQCheckListItem * item = qcli_cast( i ); 00228 if ( !item ) 00229 return; 00230 if ( !item->depth() ) 00231 return; 00232 mContextMenuItem = item; 00233 slotEditScript(); 00234 mContextMenuItem = 0; 00235 } 00236 00237 void KMail::ManageSieveScriptsDialog::slotDeleteScript() { 00238 if ( !mContextMenuItem ) 00239 return; 00240 if ( !mContextMenuItem->depth() ) 00241 return; 00242 00243 TQCheckListItem * parent = qcli_cast( mContextMenuItem->parent() ); 00244 if ( !parent ) 00245 return; 00246 00247 if ( !mUrls.count( parent ) ) 00248 return; 00249 00250 KURL u = mUrls[parent]; 00251 if ( u.isEmpty() ) 00252 return; 00253 00254 u.setFileName( mContextMenuItem->text( 0 ) ); 00255 00256 if ( KMessageBox::warningContinueCancel( this, i18n( "Really delete script \"%1\" from the server?" ).arg( u.fileName() ), 00257 i18n( "Delete Sieve Script Confirmation" ), 00258 KStdGuiItem::del() ) 00259 != KMessageBox::Continue ) 00260 return; 00261 SieveJob * job = SieveJob::del( u ); 00262 connect( job, TQT_SIGNAL(result(KMail::SieveJob*,bool,const TQString&,bool)), 00263 this, TQT_SLOT(slotRefresh()) ); 00264 } 00265 00266 void KMail::ManageSieveScriptsDialog::slotEditScript() { 00267 if ( !mContextMenuItem ) 00268 return; 00269 if ( !mContextMenuItem->depth() ) 00270 return; 00271 TQCheckListItem * parent = qcli_cast( mContextMenuItem->parent() ); 00272 if ( !mUrls.count( parent ) ) 00273 return; 00274 KURL url = mUrls[parent]; 00275 if ( url.isEmpty() ) 00276 return; 00277 url.setFileName( mContextMenuItem->text( 0 ) ); 00278 mCurrentURL = url; 00279 SieveJob * job = SieveJob::get( url ); 00280 connect( job, TQT_SIGNAL(result(KMail::SieveJob*,bool,const TQString&,bool)), 00281 this, TQT_SLOT(slotGetResult(KMail::SieveJob*,bool,const TQString&,bool)) ); 00282 } 00283 00284 void KMail::ManageSieveScriptsDialog::slotNewScript() { 00285 if ( !mContextMenuItem ) 00286 return; 00287 if ( mContextMenuItem->depth() ) 00288 mContextMenuItem = qcli_cast( mContextMenuItem->parent() ); 00289 if ( !mContextMenuItem ) 00290 return; 00291 00292 if ( !mUrls.count( mContextMenuItem ) ) 00293 return; 00294 00295 KURL u = mUrls[mContextMenuItem]; 00296 if ( u.isEmpty() ) 00297 return; 00298 00299 bool ok = false; 00300 const TQString name = KInputDialog::getText( i18n( "New Sieve Script" ), 00301 i18n( "Please enter a name for the new Sieve script:" ), 00302 i18n( "unnamed" ), &ok, this ); 00303 if ( !ok || name.isEmpty() ) 00304 return; 00305 00306 u.setFileName( name ); 00307 00308 (void) new TQCheckListItem( mContextMenuItem, name, TQCheckListItem::RadioButton ); 00309 00310 mCurrentURL = u; 00311 slotGetResult( 0, true, TQString(), false ); 00312 } 00313 00314 KMail::SieveEditor::SieveEditor( TQWidget * parent, const char * name ) 00315 : KDialogBase( Plain, i18n( "Edit Sieve Script" ), Ok|Cancel, Ok, parent, name ) 00316 { 00317 TQVBoxLayout * vlay = new TQVBoxLayout( plainPage(), 0, spacingHint() ); 00318 mTextEdit = new TQTextEdit( plainPage() ); 00319 vlay->addWidget( mTextEdit ); 00320 mTextEdit->setFocus(); 00321 mTextEdit->setTextFormat( TQTextEdit::PlainText ); 00322 mTextEdit->setWordWrap( TQTextEdit::NoWrap ); 00323 mTextEdit->setFont( KGlobalSettings::fixedFont() ); 00324 connect( mTextEdit, TQT_SIGNAL( textChanged () ), TQT_SLOT( slotTextChanged() ) ); 00325 resize( 3 * sizeHint() ); 00326 } 00327 00328 KMail::SieveEditor::~SieveEditor() {} 00329 00330 00331 void KMail::SieveEditor::slotTextChanged() 00332 { 00333 enableButtonOK( !script().isEmpty() ); 00334 } 00335 00336 void KMail::ManageSieveScriptsDialog::slotGetResult( KMail::SieveJob *, bool success, const TQString & script, bool isActive ) { 00337 if ( !success ) 00338 return; 00339 00340 if ( mSieveEditor ) 00341 return; 00342 00343 mSieveEditor = new SieveEditor( this ); 00344 mSieveEditor->setScript( script ); 00345 connect( mSieveEditor, TQT_SIGNAL(okClicked()), this, TQT_SLOT(slotSieveEditorOkClicked()) ); 00346 connect( mSieveEditor, TQT_SIGNAL(cancelClicked()), this, TQT_SLOT(slotSieveEditorCancelClicked()) ); 00347 mSieveEditor->show(); 00348 mWasActive = isActive; 00349 } 00350 00351 void KMail::ManageSieveScriptsDialog::slotSieveEditorOkClicked() { 00352 if ( !mSieveEditor ) 00353 return; 00354 SieveJob * job = SieveJob::put( mCurrentURL,mSieveEditor->script(), mWasActive, mWasActive ); 00355 connect( job, TQT_SIGNAL(result(KMail::SieveJob*,bool,const TQString&,bool)), 00356 this, TQT_SLOT(slotPutResult(KMail::SieveJob*,bool)) ); 00357 } 00358 00359 void KMail::ManageSieveScriptsDialog::slotSieveEditorCancelClicked() { 00360 mSieveEditor->deleteLater(); mSieveEditor = 0; 00361 mCurrentURL = KURL(); 00362 slotRefresh(); 00363 } 00364 00365 void KMail::ManageSieveScriptsDialog::slotPutResult( KMail::SieveJob *, bool success ) { 00366 if ( success ) { 00367 KMessageBox::information( this, i18n( "The Sieve script was successfully uploaded." ), 00368 i18n( "Sieve Script Upload" ) ); 00369 mSieveEditor->deleteLater(); mSieveEditor = 0; 00370 mCurrentURL = KURL(); 00371 } else { 00372 mSieveEditor->show(); 00373 } 00374 } 00375 00376 #include "managesievescriptsdialog.moc" 00377 #include "managesievescriptsdialog_p.moc"