kmail

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 <qlayout.h>
00019 #include <qlistview.h>
00020 #include <qtextedit.h>
00021 #include <qpopupmenu.h>
00022 
00023 #include <cassert>
00024 
00025 inline QCheckListItem * qcli_cast( QListViewItem * lvi ) {
00026   return lvi && lvi->rtti() == 1 ? static_cast<QCheckListItem*>( lvi ) : 0 ;
00027 }
00028 inline const QCheckListItem * qcli_cast( const QListViewItem * lvi ) {
00029   return lvi && lvi->rtti() == 1 ? static_cast<const QCheckListItem*>( lvi ) : 0 ;
00030 }
00031 
00032 KMail::ManageSieveScriptsDialog::ManageSieveScriptsDialog( QWidget * 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   QVBoxLayout * vlay = new QVBoxLayout( plainPage(), 0, 0 );
00043 
00044   mListView = new QListView( plainPage() );
00045   mListView->addColumn( i18n( "Available Scripts" ) );
00046   mListView->setResizeMode( QListView::LastColumn );
00047   mListView->setRootIsDecorated( true );
00048   mListView->setSelectionMode( QListView::Single );
00049   connect( mListView, SIGNAL(contextMenuRequested(QListViewItem*,const QPoint&,int)),
00050            this, SLOT(slotContextMenuRequested(QListViewItem*, const QPoint&)) );
00051   connect( mListView, SIGNAL(doubleClicked(QListViewItem*,const QPoint&,int)),
00052            this, SLOT(slotDoubleClicked(QListViewItem*)) );
00053   connect( mListView, SIGNAL(selectionChanged(QListViewItem*)),
00054            this, SLOT(slotSelectionChanged(QListViewItem*)) );
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 ( QMap<SieveJob*,QCheckListItem*>::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   QCheckListItem * last = 0;
00106   for ( KMAccount * a = am->first() ; a ; a = am->next() ) {
00107     last = new QCheckListItem( mListView, last, a->name(), QCheckListItem::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, SIGNAL(item(KMail::SieveJob*,const QString&,bool)),
00115                this, SLOT(slotItem(KMail::SieveJob*,const QString&,bool)) );
00116       connect( job, SIGNAL(result(KMail::SieveJob*,bool,const QString&,bool)),
00117                this, SLOT(slotResult(KMail::SieveJob*,bool,const QString&,bool)) );
00118       mJobs.insert( job, last );
00119       mUrls.insert( last, u );
00120     } else {
00121       QListViewItem * item = new QListViewItem( 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 QString &, bool ) {
00129   QCheckListItem * 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   QListViewItem * item = new QListViewItem( parent, i18n( "Failed to fetch the list of scripts" ) );
00141   item->setEnabled( false );
00142 }
00143 
00144 void KMail::ManageSieveScriptsDialog::slotItem( KMail::SieveJob * job, const QString & filename, bool isActive ) {
00145   QCheckListItem * parent = mJobs[job];
00146   if ( !parent )
00147     return;
00148   QCheckListItem * item = new QCheckListItem( parent, filename, QCheckListItem::RadioButton );
00149   if ( isActive ) {
00150     item->setOn( true );
00151     mSelectedItems[parent] = item;
00152   }
00153 }
00154 
00155 void KMail::ManageSieveScriptsDialog::slotContextMenuRequested( QListViewItem * i, const QPoint & p ) {
00156   QCheckListItem * item = qcli_cast( i );
00157   if ( !item )
00158     return;
00159   if ( !item->depth() && !mUrls.count( item ) )
00160     return;
00161   QPopupMenu menu;
00162   mContextMenuItem = item;
00163   if ( item->depth() ) {
00164     // script items:
00165     menu.insertItem( i18n( "Delete Script" ), this, SLOT(slotDeleteScript()) );
00166     menu.insertItem( i18n( "Edit Script..." ), this, SLOT(slotEditScript()) );
00167     menu.insertItem( i18n( "Deactivate Script" ), this, SLOT(slotDeactivateScript()) );
00168   } else {
00169     // top-levels:
00170     menu.insertItem( i18n( "New Script..." ), this, 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   QCheckListItem * 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( QListViewItem * i ) {
00191   QCheckListItem * item = qcli_cast( i );
00192   if ( !item )
00193     return;
00194   QCheckListItem * 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( QCheckListItem * 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   QCheckListItem * 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, SIGNAL(result(KMail::SieveJob*,bool,const QString&,bool)),
00223            this, SLOT(slotRefresh()) );
00224 }
00225 
00226 void KMail::ManageSieveScriptsDialog::slotDoubleClicked( QListViewItem * i ) {
00227   QCheckListItem * 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   QCheckListItem * 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, SIGNAL(result(KMail::SieveJob*,bool,const QString&,bool)),
00263            this, SLOT(slotRefresh()) );
00264 }
00265 
00266 void KMail::ManageSieveScriptsDialog::slotEditScript() {
00267   if ( !mContextMenuItem )
00268     return;
00269   if ( !mContextMenuItem->depth() )
00270     return;
00271   QCheckListItem * 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, SIGNAL(result(KMail::SieveJob*,bool,const QString&,bool)),
00281            this, SLOT(slotGetResult(KMail::SieveJob*,bool,const QString&,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 QString 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 QCheckListItem( mContextMenuItem, name, QCheckListItem::RadioButton );
00309 
00310   mCurrentURL = u;
00311   slotGetResult( 0, true, QString::null, false );
00312 }
00313 
00314 KMail::SieveEditor::SieveEditor( QWidget * parent, const char * name )
00315   : KDialogBase( Plain, i18n( "Edit Sieve Script" ), Ok|Cancel, Ok, parent, name )
00316 {
00317   QVBoxLayout * vlay = new QVBoxLayout( plainPage(), 0, spacingHint() );
00318   mTextEdit = new QTextEdit( plainPage() );
00319   vlay->addWidget( mTextEdit );
00320   mTextEdit->setFocus();
00321   mTextEdit->setTextFormat( QTextEdit::PlainText );
00322   mTextEdit->setWordWrap( QTextEdit::NoWrap );
00323   mTextEdit->setFont( KGlobalSettings::fixedFont() );
00324   connect( mTextEdit, SIGNAL( textChanged () ), 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 QString & 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, SIGNAL(okClicked()), this, SLOT(slotSieveEditorOkClicked()) );
00346   connect( mSieveEditor, SIGNAL(cancelClicked()), this, 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, SIGNAL(result(KMail::SieveJob*,bool,const QString&,bool)),
00356            this, 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"
KDE Home | KDE Accessibility Home | Description of Access Keys