kmail

mailinglistpropertiesdialog.cpp
1 /*******************************************************************************
2 **
3 ** Filename : mailinglistpropertiesdialog.cpp
4 ** Created on : 30 January, 2005
5 ** Copyright : (c) 2005 Till Adam
6 ** Email : adam@kde.org
7 **
8 *******************************************************************************/
9 
10 /*******************************************************************************
11 **
12 ** This program is free software; you can redistribute it and/or modify
13 ** it under the terms of the GNU General Public License as published by
14 ** the Free Software Foundation; either version 2 of the License, or
15 ** (at your option) any later version.
16 **
17 ** In addition, as a special exception, the copyright holders give
18 ** permission to link the code of this program with any edition of
19 ** the TQt library by Trolltech AS, Norway (or with modified versions
20 ** of TQt that use the same license as TQt), and distribute linked
21 ** combinations including the two. You must obey the GNU General
22 ** Public License in all respects for all of the code used other than
23 ** TQt. If you modify this file, you may extend this exception to
24 ** your version of the file, but you are not obligated to do so. If
25 ** you do not wish to do so, delete this exception statement from
26 ** your version.
27 *******************************************************************************/
28 #include <tqlayout.h>
29 #include <tqlabel.h>
30 #include <tqcombobox.h>
31 #include <tqgroupbox.h>
32 #include <tqcheckbox.h>
33 #include <tqpushbutton.h>
34 
35 #include <tdelocale.h>
36 #include <keditlistbox.h>
37 #include <kdialogbase.h>
38 #include <kdebug.h>
39 #include <tdemessagebox.h>
40 #include <kmcommands.h>
41 
42 #include "kmfolder.h"
43 #include "mailinglist-magic.h"
44 #include "mailinglistpropertiesdialog.h"
45 
46 using namespace KMail;
47 
48 MailingListFolderPropertiesDialog::MailingListFolderPropertiesDialog( TQWidget* parent, KMFolder *folder )
49  : KDialogBase( parent, "mailinglist_properties", false, i18n( "Mailinglist Folder Properties" ),
50  KDialogBase::Ok|KDialogBase::Cancel,
51  KDialogBase::Ok, true ),
52  mFolder( folder )
53 {
54  setWFlags( getWFlags() | WDestructiveClose );
55  TQLabel* label;
56  mLastItem = 0;
57 
58  TQVBoxLayout *topLayout = new TQVBoxLayout( layout(), spacingHint(),
59  "topLayout" );
60 
61  TQGroupBox *mlGroup = new TQGroupBox( i18n("Associated Mailing List" ), this );
62  mlGroup->setColumnLayout( 0, TQt::Vertical );
63  TQGridLayout *groupLayout = new TQGridLayout( mlGroup->layout(), 6, 3, spacingHint() );
64  topLayout->addWidget( mlGroup );
65  setMainWidget( mlGroup );
66 
67  mHoldsMailingList = new TQCheckBox( i18n("&Folder holds a mailing list"), mlGroup );
68  connect( mHoldsMailingList, TQ_SIGNAL(toggled(bool)),
69  TQ_SLOT(slotHoldsML(bool)) );
70  groupLayout->addMultiCellWidget( mHoldsMailingList, 0, 0, 0, 2 );
71 
72  groupLayout->addItem( new TQSpacerItem( 0, 10 ), 1, 0 );
73 
74  mDetectButton = new TQPushButton( i18n("Detect Automatically"), mlGroup );
75  mDetectButton->setEnabled( false );
76  connect( mDetectButton, TQ_SIGNAL(pressed()), TQ_SLOT(slotDetectMailingList()) );
77  groupLayout->addWidget( mDetectButton, 2, 1 );
78 
79  groupLayout->addItem( new TQSpacerItem( 0, 10 ), 3, 0 );
80 
81  label = new TQLabel( i18n("Mailing list description:"), mlGroup );
82  label->setEnabled( false );
83  connect( mHoldsMailingList, TQ_SIGNAL(toggled(bool)),
84  label, TQ_SLOT(setEnabled(bool)) );
85  groupLayout->addWidget( label, 4, 0 );
86  mMLId = new TQLabel( label, "", mlGroup );
87  groupLayout->addMultiCellWidget( mMLId, 4, 4, 1, 2 );
88  mMLId->setEnabled( false );
89 
90  //FIXME: add TQWhatsThis
91  label = new TQLabel( i18n("Preferred handler:"), mlGroup );
92  label->setEnabled(false);
93  connect( mHoldsMailingList, TQ_SIGNAL(toggled(bool)),
94  label, TQ_SLOT(setEnabled(bool)) );
95  groupLayout->addWidget( label, 5, 0 );
96  mMLHandlerCombo = new TQComboBox( mlGroup );
97  mMLHandlerCombo->insertItem( i18n("KMail"), MailingList::KMail );
98  mMLHandlerCombo->insertItem( i18n("Browser"), MailingList::Browser );
99  mMLHandlerCombo->setEnabled( false );
100  groupLayout->addMultiCellWidget( mMLHandlerCombo, 5, 5, 1, 2 );
101  connect( mMLHandlerCombo, TQ_SIGNAL(activated(int)),
102  TQ_SLOT(slotMLHandling(int)) );
103  label->setBuddy( mMLHandlerCombo );
104 
105  label = new TQLabel( i18n("&Address type:"), mlGroup );
106  label->setEnabled(false);
107  connect( mHoldsMailingList, TQ_SIGNAL(toggled(bool)),
108  label, TQ_SLOT(setEnabled(bool)) );
109  groupLayout->addWidget( label, 6, 0 );
110  mAddressCombo = new TQComboBox( mlGroup );
111  label->setBuddy( mAddressCombo );
112  groupLayout->addWidget( mAddressCombo, 6, 1 );
113  mAddressCombo->setEnabled( false );
114 
115  //FIXME: if the mailing list actions have either TDEAction's or toolbar buttons
116  // associated with them - remove this button since it's really silly
117  // here
118  TQPushButton *handleButton = new TQPushButton( i18n( "Invoke Handler" ), mlGroup );
119  handleButton->setEnabled( false );
120  if( mFolder)
121  {
122  connect( mHoldsMailingList, TQ_SIGNAL(toggled(bool)),
123  handleButton, TQ_SLOT(setEnabled(bool)) );
124  connect( handleButton, TQ_SIGNAL(clicked()),
125  TQ_SLOT(slotInvokeHandler()) );
126  }
127  groupLayout->addWidget( handleButton, 6, 2 );
128 
129  mEditList = new KEditListBox( mlGroup );
130  mEditList->setEnabled( false );
131  groupLayout->addMultiCellWidget( mEditList, 7, 7, 0, 3 );
132 
133  TQStringList el;
134 
135  //Order is important because the activate handler and fillMLFromWidgets
136  //depend on it
137  el << i18n( "Post to List" )
138  << i18n( "Subscribe to List" )
139  << i18n( "Unsubscribe from List" )
140  << i18n( "List Archives" )
141  << i18n( "List Help" );
142  mAddressCombo->insertStringList( el );
143  connect( mAddressCombo, TQ_SIGNAL(activated(int)),
144  TQ_SLOT(slotAddressChanged(int)) );
145 
146  load();
147  resize( TQSize(295, 204).expandedTo(minimumSizeHint()) );
148  clearWState( WState_Polished );
149 }
150 
151 void MailingListFolderPropertiesDialog::slotOk()
152 {
153  save();
154  KDialogBase::slotOk();
155 }
156 
157 void MailingListFolderPropertiesDialog::load()
158 {
159  if (mFolder) mMailingList = mFolder->mailingList();
160  mMLId->setText( (mMailingList.id().isEmpty() ? i18n("Not available") : mMailingList.id()) );
161  mMLHandlerCombo->setCurrentItem( mMailingList.handler() );
162  mEditList->insertStringList( mMailingList.postURLS().toStringList() );
163 
164  mAddressCombo->setCurrentItem( mLastItem );
165  mHoldsMailingList->setChecked( mFolder && mFolder->isMailingListEnabled() );
166 }
167 
168 //-----------------------------------------------------------------------------
169 bool MailingListFolderPropertiesDialog::save()
170 {
171  if( mFolder )
172  {
173  // settings for mailingList
174  mFolder->setMailingListEnabled( mHoldsMailingList && mHoldsMailingList->isChecked() );
175  fillMLFromWidgets();
176  mFolder->setMailingList( mMailingList );
177  }
178  return true;
179 }
180 
181 //----------------------------------------------------------------------------
182 void MailingListFolderPropertiesDialog::slotHoldsML( bool holdsML )
183 {
184  mMLHandlerCombo->setEnabled( holdsML );
185  if ( mFolder && mFolder->count() )
186  mDetectButton->setEnabled( holdsML );
187  mAddressCombo->setEnabled( holdsML );
188  mEditList->setEnabled( holdsML );
189  mMLId->setEnabled( holdsML );
190 }
191 
192 //----------------------------------------------------------------------------
193 void MailingListFolderPropertiesDialog::slotDetectMailingList()
194 {
195  if ( !mFolder ) return; // in case the folder was just created
196  int num = mFolder->count();
197 
198  kdDebug(5006)<<k_funcinfo<<" Detecting mailing list"<<endl;
199 
200  /* FIXME Till - make work without the folder tree
201  // first try the currently selected message
202  KMFolderTree *folderTree = static_cast<KMFolderTree *>( mDlg->parent() );
203  int curMsgIdx = folderTree->mainWidget()->headers()->currentItemIndex();
204  if ( curMsgIdx > 0 ) {
205  KMMessage *mes = mFolder->getMsg( curMsgIdx );
206  if ( mes )
207  mMailingList = MailingList::detect( mes );
208  }
209  */
210 
211  // next try the 5 most recently added messages
212  if ( !( mMailingList.features() & MailingList::Post ) ) {
213  const int maxchecks = 5;
214  for( int i = --num; i > num-maxchecks; --i ) {
215  KMMessage *mes = mFolder->getMsg( i );
216  if ( !mes )
217  continue;
218  mMailingList = MailingList::detect( mes );
219  if ( mMailingList.features() & MailingList::Post )
220  break;
221  }
222  }
223  if ( !(mMailingList.features() & MailingList::Post) ) {
224  KMessageBox::error( this,
225  i18n("KMail was unable to detect a mailing list in this folder. "
226  "Please fill the addresses by hand.") );
227  } else {
228  mMLId->setText( (mMailingList.id().isEmpty() ? i18n("Not available.") : mMailingList.id() ) );
229  fillEditBox();
230  }
231 }
232 
233 //----------------------------------------------------------------------------
234 void MailingListFolderPropertiesDialog::slotMLHandling( int element )
235 {
236  mMailingList.setHandler( static_cast<MailingList::Handler>( element ) );
237 }
238 
239 //----------------------------------------------------------------------------
240 void MailingListFolderPropertiesDialog::slotAddressChanged( int i )
241 {
242  fillMLFromWidgets();
243  fillEditBox();
244  mLastItem = i;
245 }
246 
247 //----------------------------------------------------------------------------
248 void MailingListFolderPropertiesDialog::fillMLFromWidgets()
249 {
250  if ( !mHoldsMailingList->isChecked() )
251  return;
252 
253  // make sure that email addresses are prepended by "mailto:"
254  bool changed = false;
255  TQStringList oldList = mEditList->items();
256  TQStringList newList; // the correct string list
257  for ( TQStringList::ConstIterator it = oldList.begin();
258  it != oldList.end(); ++it ) {
259  if ( !(*it).startsWith("http:") && !(*it).startsWith("https:") &&
260  !(*it).startsWith("mailto:") && ( (*it).find('@') != -1 ) ) {
261  changed = true;
262  newList << "mailto:" + *it;
263  }
264  else {
265  newList << *it;
266  }
267  }
268  if ( changed ) {
269  mEditList->clear();
270  mEditList->insertStringList( newList );
271  }
272 
273  //mMailingList.setHandler( static_cast<MailingList::Handler>( mMLHandlerCombo->currentItem() ) );
274  switch ( mLastItem ) {
275  case 0:
276  mMailingList.setPostURLS( mEditList->items() );
277  break;
278  case 1:
279  mMailingList.setSubscribeURLS( mEditList->items() );
280  break;
281  case 2:
282  mMailingList.setUnsubscribeURLS( mEditList->items() );
283  break;
284  case 3:
285  mMailingList.setArchiveURLS( mEditList->items() );
286  break;
287  case 4:
288  mMailingList.setHelpURLS( mEditList->items() );
289  break;
290  default:
291  kdWarning( 5006 )<<"Wrong entry in the mailing list entry combo!"<<endl;
292  }
293 }
294 
295 void MailingListFolderPropertiesDialog::fillEditBox()
296 {
297  mEditList->clear();
298  switch ( mAddressCombo->currentItem() ) {
299  case 0:
300  mEditList->insertStringList( mMailingList.postURLS().toStringList() );
301  break;
302  case 1:
303  mEditList->insertStringList( mMailingList.subscribeURLS().toStringList() );
304  break;
305  case 2:
306  mEditList->insertStringList( mMailingList.unsubscribeURLS().toStringList() );
307  break;
308  case 3:
309  mEditList->insertStringList( mMailingList.archiveURLS().toStringList() );
310  break;
311  case 4:
312  mEditList->insertStringList( mMailingList.helpURLS().toStringList() );
313  break;
314  default:
315  kdWarning( 5006 )<<"Wrong entry in the mailing list entry combo!"<<endl;
316  }
317 }
318 
319 void MailingListFolderPropertiesDialog::slotInvokeHandler()
320 {
321  KMCommand *command =0;
322  switch ( mAddressCombo->currentItem() ) {
323  case 0:
324  command = new KMMailingListPostCommand( this, mFolder );
325  break;
326  case 1:
327  command = new KMMailingListSubscribeCommand( this, mFolder );
328  break;
329  case 2:
330  command = new KMMailingListUnsubscribeCommand( this, mFolder );
331  break;
332  case 3:
333  command = new KMMailingListArchivesCommand( this, mFolder );
334  break;
335  case 4:
336  command = new KMMailingListHelpCommand( this, mFolder );
337  break;
338  default:
339  kdWarning( 5006 )<<"Wrong entry in the mailing list entry combo!"<<endl;
340  }
341  if ( command ) command->start();
342 }
343 
344 #include "mailinglistpropertiesdialog.moc"
Mail folder.
Definition: kmfolder.h:69
This is a Mime Message.
Definition: kmmessage.h:68
@ Ok
The user rights/ACL have been fetched from the server sucessfully.
Definition: acljobs.h:66
folderdiaquotatab.h
Definition: aboutdata.cpp:40