kmail

mailinglistpropertiesdialog.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 **
00003 ** Filename   : mailinglistpropertiesdialog.cpp
00004 ** Created on : 30 January, 2005
00005 ** Copyright  : (c) 2005 Till Adam
00006 ** Email      : adam@kde.org
00007 **
00008 *******************************************************************************/
00009 
00010 /*******************************************************************************
00011 **
00012 **   This program is free software; you can redistribute it and/or modify
00013 **   it under the terms of the GNU General Public License as published by
00014 **   the Free Software Foundation; either version 2 of the License, or
00015 **   (at your option) any later version.
00016 **
00017 **   In addition, as a special exception, the copyright holders give
00018 **   permission to link the code of this program with any edition of
00019 **   the Qt library by Trolltech AS, Norway (or with modified versions
00020 **   of Qt that use the same license as Qt), and distribute linked
00021 **   combinations including the two.  You must obey the GNU General
00022 **   Public License in all respects for all of the code used other than
00023 **   Qt.  If you modify this file, you may extend this exception to
00024 **   your version of the file, but you are not obligated to do so.  If
00025 **   you do not wish to do so, delete this exception statement from
00026 **   your version.
00027 *******************************************************************************/
00028 #include <qlayout.h>
00029 #include <qlabel.h>
00030 #include <qcombobox.h>
00031 #include <qgroupbox.h>
00032 #include <qcheckbox.h>
00033 #include <qpushbutton.h>
00034 
00035 #include <klocale.h>
00036 #include <keditlistbox.h>
00037 #include <kdialogbase.h>
00038 #include <kdebug.h>
00039 #include <kmessagebox.h>
00040 #include <kmcommands.h>
00041 
00042 #include "kmfolder.h"
00043 #include "mailinglist-magic.h"
00044 #include "mailinglistpropertiesdialog.h"
00045 
00046 using namespace KMail;
00047 
00048 MailingListFolderPropertiesDialog::MailingListFolderPropertiesDialog( QWidget* parent, KMFolder *folder )
00049     : KDialogBase( parent, "mailinglist_properties", false, i18n( "Mailinglist Folder Properties" ), 
00050                    KDialogBase::Ok|KDialogBase::Cancel, 
00051                    KDialogBase::Ok, true ),
00052       mFolder( folder )
00053 {
00054   setWFlags( getWFlags() | WDestructiveClose );
00055   QLabel* label;
00056   mLastItem = 0;
00057 
00058   QVBoxLayout *topLayout = new QVBoxLayout( layout(), spacingHint(),
00059                                             "topLayout" );
00060 
00061   QGroupBox *mlGroup = new QGroupBox( i18n("Associated Mailing List" ), this );
00062   mlGroup->setColumnLayout( 0,  Qt::Vertical );
00063   QGridLayout *groupLayout = new QGridLayout( mlGroup->layout(), 6, 3, spacingHint() );
00064   topLayout->addWidget( mlGroup );
00065   setMainWidget( mlGroup );
00066 
00067   mHoldsMailingList = new QCheckBox( i18n("&Folder holds a mailing list"), mlGroup );
00068   QObject::connect( mHoldsMailingList, SIGNAL(toggled(bool)),
00069                     SLOT(slotHoldsML(bool)) );
00070   groupLayout->addMultiCellWidget( mHoldsMailingList, 0, 0, 0, 2 );
00071 
00072   groupLayout->addItem( new QSpacerItem( 0, 10 ), 1, 0 );
00073 
00074   mDetectButton = new QPushButton( i18n("Detect Automatically"), mlGroup );
00075   mDetectButton->setEnabled( false );
00076   QObject::connect( mDetectButton, SIGNAL(pressed()), SLOT(slotDetectMailingList()) );
00077   groupLayout->addWidget( mDetectButton, 2, 1 );
00078 
00079   groupLayout->addItem( new QSpacerItem( 0, 10 ), 3, 0 );
00080 
00081   label = new QLabel( i18n("Mailing list description:"), mlGroup );
00082   label->setEnabled( false );
00083   QObject::connect( mHoldsMailingList, SIGNAL(toggled(bool)),
00084             label, SLOT(setEnabled(bool)) );
00085   groupLayout->addWidget( label, 4, 0 );
00086   mMLId = new QLabel( label, "", mlGroup );
00087   groupLayout->addMultiCellWidget( mMLId, 4, 4, 1, 2 );
00088   mMLId->setEnabled( false );
00089 
00090   //FIXME: add QWhatsThis
00091   label = new QLabel( i18n("Preferred handler:"), mlGroup );
00092   label->setEnabled(false);
00093   QObject::connect( mHoldsMailingList, SIGNAL(toggled(bool)),
00094             label, SLOT(setEnabled(bool)) );
00095   groupLayout->addWidget( label, 5, 0 );
00096   mMLHandlerCombo = new QComboBox( mlGroup );
00097   mMLHandlerCombo->insertItem( i18n("KMail"), MailingList::KMail );
00098   mMLHandlerCombo->insertItem( i18n("Browser"), MailingList::Browser );
00099   mMLHandlerCombo->setEnabled( false );
00100   groupLayout->addMultiCellWidget( mMLHandlerCombo, 5, 5, 1, 2 );
00101   QObject::connect( mMLHandlerCombo, SIGNAL(activated(int)),
00102                     SLOT(slotMLHandling(int)) );
00103   label->setBuddy( mMLHandlerCombo );
00104 
00105   label = new QLabel( i18n("&Address type:"), mlGroup );
00106   label->setEnabled(false);
00107   QObject::connect( mHoldsMailingList, SIGNAL(toggled(bool)),
00108             label, SLOT(setEnabled(bool)) );
00109   groupLayout->addWidget( label, 6, 0 );
00110   mAddressCombo = new QComboBox( mlGroup );
00111   label->setBuddy( mAddressCombo );
00112   groupLayout->addWidget( mAddressCombo, 6, 1 );
00113   mAddressCombo->setEnabled( false );
00114 
00115   //FIXME: if the mailing list actions have either KAction's or toolbar buttons
00116   //       associated with them - remove this button since it's really silly
00117   //       here
00118   QPushButton *handleButton = new QPushButton( i18n( "Invoke Handler" ), mlGroup );
00119   handleButton->setEnabled( false );
00120   if( mFolder)
00121   {
00122     QObject::connect( mHoldsMailingList, SIGNAL(toggled(bool)),
00123                 handleButton, SLOT(setEnabled(bool)) );
00124     QObject::connect( handleButton, SIGNAL(clicked()),
00125                     SLOT(slotInvokeHandler()) );
00126   }
00127   groupLayout->addWidget( handleButton, 6, 2 );
00128 
00129   mEditList = new KEditListBox( mlGroup );
00130   mEditList->setEnabled( false );
00131   groupLayout->addMultiCellWidget( mEditList, 7, 7, 0, 3 );
00132 
00133   QStringList el;
00134 
00135   //Order is important because the activate handler and fillMLFromWidgets
00136   //depend on it
00137   el << i18n( "Post to List" )
00138      << i18n( "Subscribe to List" )
00139      << i18n( "Unsubscribe from List" )
00140      << i18n( "List Archives" )
00141      << i18n( "List Help" );
00142   mAddressCombo->insertStringList( el );
00143   QObject::connect( mAddressCombo, SIGNAL(activated(int)),
00144                     SLOT(slotAddressChanged(int)) );
00145 
00146   load();
00147   resize( QSize(295, 204).expandedTo(minimumSizeHint()) );
00148   clearWState( WState_Polished );
00149 }
00150 
00151 void MailingListFolderPropertiesDialog::slotOk()
00152 {
00153   save();
00154   KDialogBase::slotOk();
00155 }
00156 
00157 void MailingListFolderPropertiesDialog::load()
00158 {
00159   if (mFolder) mMailingList = mFolder->mailingList();
00160   mMLId->setText( (mMailingList.id().isEmpty() ? i18n("Not available") : mMailingList.id()) );
00161   mMLHandlerCombo->setCurrentItem( mMailingList.handler() );
00162   mEditList->insertStringList( mMailingList.postURLS().toStringList() );
00163 
00164   mAddressCombo->setCurrentItem( mLastItem );
00165   mHoldsMailingList->setChecked( mFolder && mFolder->isMailingListEnabled() );
00166 }
00167 
00168 //-----------------------------------------------------------------------------
00169 bool MailingListFolderPropertiesDialog::save()
00170 {
00171   if( mFolder )
00172   {
00173     // settings for mailingList
00174     mFolder->setMailingListEnabled( mHoldsMailingList && mHoldsMailingList->isChecked() );
00175     fillMLFromWidgets();
00176     mFolder->setMailingList( mMailingList );
00177   }
00178   return true;
00179 }
00180 
00181 //----------------------------------------------------------------------------
00182 void MailingListFolderPropertiesDialog::slotHoldsML( bool holdsML )
00183 {
00184   mMLHandlerCombo->setEnabled( holdsML );
00185   if ( mFolder && mFolder->count() )
00186     mDetectButton->setEnabled( holdsML );
00187   mAddressCombo->setEnabled( holdsML );
00188   mEditList->setEnabled( holdsML );
00189   mMLId->setEnabled( holdsML );
00190 }
00191 
00192 //----------------------------------------------------------------------------
00193 void MailingListFolderPropertiesDialog::slotDetectMailingList()
00194 {
00195   if ( !mFolder ) return; // in case the folder was just created
00196   int num = mFolder->count();
00197 
00198   kdDebug(5006)<<k_funcinfo<<" Detecting mailing list"<<endl;
00199 
00200   /* FIXME Till - make work without the folder tree
00201   // first try the currently selected message
00202   KMFolderTree *folderTree = static_cast<KMFolderTree *>( mDlg->parent() );
00203   int curMsgIdx = folderTree->mainWidget()->headers()->currentItemIndex();
00204   if ( curMsgIdx > 0 ) {
00205     KMMessage *mes = mFolder->getMsg( curMsgIdx );
00206     if ( mes )
00207       mMailingList = MailingList::detect( mes );
00208   }
00209   */
00210 
00211   // next try the 5 most recently added messages
00212   if ( !( mMailingList.features() & MailingList::Post ) ) {
00213     const int maxchecks = 5;
00214     for( int i = --num; i > num-maxchecks; --i ) {
00215       KMMessage *mes = mFolder->getMsg( i );
00216       if ( !mes )
00217         continue;
00218       mMailingList = MailingList::detect( mes );
00219       if ( mMailingList.features() & MailingList::Post )
00220         break;
00221     }
00222   }
00223   if ( !(mMailingList.features() & MailingList::Post) ) {
00224     KMessageBox::error( this,
00225               i18n("KMail was unable to detect a mailing list in this folder. "
00226                    "Please fill the addresses by hand.") );
00227   } else {
00228     mMLId->setText( (mMailingList.id().isEmpty() ? i18n("Not available.") : mMailingList.id() ) );
00229     fillEditBox();
00230   }
00231 }
00232 
00233 //----------------------------------------------------------------------------
00234 void MailingListFolderPropertiesDialog::slotMLHandling( int element )
00235 {
00236   mMailingList.setHandler( static_cast<MailingList::Handler>( element ) );
00237 }
00238 
00239 //----------------------------------------------------------------------------
00240 void MailingListFolderPropertiesDialog::slotAddressChanged( int i )
00241 {
00242   fillMLFromWidgets();
00243   fillEditBox();
00244   mLastItem = i;
00245 }
00246 
00247 //----------------------------------------------------------------------------
00248 void MailingListFolderPropertiesDialog::fillMLFromWidgets()
00249 {
00250   if ( !mHoldsMailingList->isChecked() )
00251     return;
00252 
00253   // make sure that email addresses are prepended by "mailto:"
00254   bool changed = false;
00255   QStringList oldList = mEditList->items();
00256   QStringList newList; // the correct string list
00257   for ( QStringList::ConstIterator it = oldList.begin();
00258         it != oldList.end(); ++it ) {
00259     if ( !(*it).startsWith("http:") && !(*it).startsWith("https:") &&
00260          !(*it).startsWith("mailto:") && ( (*it).find('@') != -1 ) ) {
00261       changed = true;
00262       newList << "mailto:" + *it;
00263     }
00264     else {
00265       newList << *it;
00266     }
00267   }
00268   if ( changed ) {
00269     mEditList->clear();
00270     mEditList->insertStringList( newList );
00271   }
00272 
00273   //mMailingList.setHandler( static_cast<MailingList::Handler>( mMLHandlerCombo->currentItem() ) );
00274   switch ( mLastItem ) {
00275   case 0:
00276     mMailingList.setPostURLS( mEditList->items() );
00277     break;
00278   case 1:
00279     mMailingList.setSubscribeURLS( mEditList->items() );
00280     break;
00281   case 2:
00282     mMailingList.setUnsubscribeURLS( mEditList->items() );
00283     break;
00284   case 3:
00285     mMailingList.setArchiveURLS( mEditList->items() );
00286     break;
00287   case 4:
00288     mMailingList.setHelpURLS( mEditList->items() );
00289     break;
00290   default:
00291     kdWarning( 5006 )<<"Wrong entry in the mailing list entry combo!"<<endl;
00292   }
00293 }
00294 
00295 void MailingListFolderPropertiesDialog::fillEditBox()
00296 {
00297   mEditList->clear();
00298   switch ( mAddressCombo->currentItem() ) {
00299   case 0:
00300     mEditList->insertStringList( mMailingList.postURLS().toStringList() );
00301     break;
00302   case 1:
00303     mEditList->insertStringList( mMailingList.subscribeURLS().toStringList() );
00304     break;
00305   case 2:
00306     mEditList->insertStringList( mMailingList.unsubscribeURLS().toStringList() );
00307     break;
00308   case 3:
00309     mEditList->insertStringList( mMailingList.archiveURLS().toStringList() );
00310     break;
00311   case 4:
00312     mEditList->insertStringList( mMailingList.helpURLS().toStringList() );
00313     break;
00314   default:
00315     kdWarning( 5006 )<<"Wrong entry in the mailing list entry combo!"<<endl;
00316   }
00317 }
00318 
00319 void MailingListFolderPropertiesDialog::slotInvokeHandler()
00320 {
00321   KMCommand *command =0;
00322   switch ( mAddressCombo->currentItem() ) {
00323   case 0:
00324     command = new KMMailingListPostCommand( this, mFolder );
00325     break;
00326   case 1:
00327     command = new KMMailingListSubscribeCommand( this, mFolder );
00328     break;
00329   case 2:
00330     command = new KMMailingListUnsubscribeCommand( this, mFolder );
00331     break;
00332   case 3:
00333     command = new KMMailingListArchivesCommand( this, mFolder );
00334     break;
00335   case 4:
00336     command = new KMMailingListHelpCommand( this, mFolder );
00337     break;
00338   default:
00339     kdWarning( 5006 )<<"Wrong entry in the mailing list entry combo!"<<endl;
00340   }
00341   if ( command ) command->start();
00342 }
00343 
00344 #include "mailinglistpropertiesdialog.moc"