• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kmail

  • sources
  • kde-4.12
  • kdepim
  • kmail
  • collectionpage
collectionmailinglistpage.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-file-style: "gnu" -*-
2  This file is part of KMail, the KDE mail client.
3  Copyright (c) 2005 Till Adam <adam@kde.org>
4  Copyright (c) 2011 Montel Laurent <montel@kde.org>
5  Copyright (c) 2012 Jonathan Marten <jjm@keelhaul.me.uk>
6 
7  KMail is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License, version 2, as
9  published by the Free Software Foundation.
10 
11  KMail is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License along
17  with this program; if not, write to the Free Software Foundation, Inc.,
18  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
21 
22 #include "collectionmailinglistpage.h"
23 #include "kmkernel.h"
24 #include "kernel/mailkernel.h"
25 #include "util/mailutil.h"
26 #include "util.h"
27 
28 #include <akonadi/itemfetchjob.h>
29 #include <akonadi/itemfetchscope.h>
30 #include <akonadi/kmime/messageparts.h>
31 
32 #include <QGridLayout>
33 #include <QLabel>
34 #include <QCheckBox>
35 #include <QGroupBox>
36 #include <QPushButton>
37 #include <QSpacerItem>
38 #include <QVBoxLayout>
39 
40 #include <KComboBox>
41 #include <KDialog>
42 #include <KLineEdit>
43 #include <KEditListWidget>
44 #include <KLocale>
45 #include <KMessageBox>
46 #include <KSqueezedTextLabel>
47 
48 using namespace MailCommon;
49 
50 
51 CollectionMailingListPage::CollectionMailingListPage(QWidget * parent) :
52  CollectionPropertiesPage( parent ), mLastItem(0), changed(false)
53 {
54  setObjectName( QLatin1String( "KMail::CollectionMailingListPage" ) );
55  setPageTitle( i18nc( "@title:tab Mailing list settings for a folder.", "Mailing List" ) );
56 }
57 
58 CollectionMailingListPage::~CollectionMailingListPage()
59 {
60 }
61 
62 void CollectionMailingListPage::slotConfigChanged()
63 {
64  changed = true;
65 }
66 
67 bool CollectionMailingListPage::canHandle( const Akonadi::Collection &col ) const
68 {
69  QSharedPointer<FolderCollection> fd = FolderCollection::forCollection( col, false );
70  return ( !CommonKernel->isSystemFolderCollection( col ) &&
71  !fd->isStructural() &&
72  !MailCommon::Util::isVirtualCollection( col ) );
73 }
74 
75 void CollectionMailingListPage::init(const Akonadi::Collection & col)
76 {
77  mCurrentCollection = col;
78  mFolder = FolderCollection::forCollection( col, false );
79 
80  QVBoxLayout *topLayout = new QVBoxLayout( this );
81  topLayout->setSpacing( KDialog::spacingHint() );
82 
83  mHoldsMailingList = new QCheckBox( i18n("Folder holds a mailing list"), this );
84  connect( mHoldsMailingList, SIGNAL(toggled(bool)),
85  SLOT(slotHoldsML(bool)) );
86  connect( mHoldsMailingList, SIGNAL(toggled(bool)), SLOT(slotConfigChanged()) );
87  topLayout->addWidget( mHoldsMailingList );
88 
89  mGroupWidget = new QWidget( this );
90  QGridLayout *groupLayout = new QGridLayout( mGroupWidget );
91  groupLayout->setSpacing( KDialog::spacingHint() );
92 
93  mDetectButton = new QPushButton( i18n("Detect Automatically"), mGroupWidget );
94  connect( mDetectButton, SIGNAL(pressed()),
95  SLOT(slotDetectMailingList()) );
96  groupLayout->addWidget( mDetectButton, 2, 1 );
97 
98  groupLayout->addItem( new QSpacerItem( 0, 10 ), 3, 0 );
99 
100  QLabel *label = new QLabel( i18n("Mailing list description:"), mGroupWidget );
101  groupLayout->addWidget( label, 4, 0 );
102  mMLId = new KSqueezedTextLabel( QString(), mGroupWidget );
103  mMLId->setTextElideMode( Qt::ElideRight );
104  groupLayout->addWidget( mMLId, 4, 1, 1, 2 );
105 
106  //FIXME: add QWhatsThis
107  label = new QLabel( i18n("Preferred handler:"), mGroupWidget );
108  groupLayout->addWidget( label, 5, 0 );
109  mMLHandlerCombo = new KComboBox( mGroupWidget );
110  mMLHandlerCombo->addItem( i18n("KMail"), MailingList::KMail );
111  mMLHandlerCombo->addItem( i18n("Browser"), MailingList::Browser );
112  groupLayout->addWidget( mMLHandlerCombo, 5, 1, 1, 2 );
113  connect( mMLHandlerCombo, SIGNAL(activated(int)),
114  SLOT(slotMLHandling(int)) );
115  label->setBuddy( mMLHandlerCombo );
116 
117  label = new QLabel( i18n("Address type:"), mGroupWidget );
118  groupLayout->addWidget( label, 6, 0 );
119  mAddressCombo = new KComboBox( mGroupWidget );
120  label->setBuddy( mAddressCombo );
121  groupLayout->addWidget( mAddressCombo, 6, 1 );
122 
123  //FIXME: if the mailing list actions have either KAction's or toolbar buttons
124  // associated with them - remove this button since it's really silly
125  // here
126  QPushButton *handleButton = new QPushButton( i18n( "Invoke Handler" ), mGroupWidget );
127  if (mFolder) {
128  connect( handleButton, SIGNAL(clicked()),
129  SLOT(slotInvokeHandler()) );
130  } else {
131  handleButton->setEnabled( false );
132  }
133 
134  groupLayout->addWidget( handleButton, 6, 2 );
135 
136  mEditList = new KEditListWidget( mGroupWidget );
137  mEditList->lineEdit()->setClearButtonShown(true);
138  connect(mEditList, SIGNAL(changed()),SLOT(slotConfigChanged()));
139  groupLayout->addWidget( mEditList, 7, 0, 1, 4 );
140 
141  QStringList el;
142 
143  //Order is important because the activate handler and fillMLFromWidgets
144  //depend on it
145  el << i18n( "Post to List" )
146  << i18n( "Subscribe to List" )
147  << i18n( "Unsubscribe From List" )
148  << i18n( "List Archives" )
149  << i18n( "List Help" );
150  mAddressCombo->addItems( el );
151  connect( mAddressCombo, SIGNAL(activated(int)),
152  SLOT(slotAddressChanged(int)) );
153 
154  topLayout->addWidget( mGroupWidget );
155  mGroupWidget->setEnabled( false );
156 }
157 
158 void CollectionMailingListPage::load( const Akonadi::Collection & col )
159 {
160  init( col );
161 
162  if ( mFolder )
163  mMailingList = mFolder->mailingList();
164 
165  mMLId->setText( (mMailingList.id().isEmpty() ? i18n("Not available") : mMailingList.id()) );
166  mMLHandlerCombo->setCurrentIndex( mMailingList.handler() );
167  mEditList->insertStringList( mMailingList.postUrls().toStringList() );
168 
169  mAddressCombo->setCurrentIndex( mLastItem );
170  mHoldsMailingList->setChecked( mFolder && mFolder->isMailingListEnabled() );
171  slotHoldsML( mHoldsMailingList->isChecked() );
172  changed = false;
173 }
174 
175 void CollectionMailingListPage::save( Akonadi::Collection & col )
176 {
177  Q_UNUSED( col );
178  if( changed ) {
179  if ( mFolder ) {
180  // settings for mailingList
181  mFolder->setMailingListEnabled( mHoldsMailingList && mHoldsMailingList->isChecked() );
182  fillMLFromWidgets();
183  mFolder->setMailingList( mMailingList );
184  }
185  }
186 }
187 
188 //----------------------------------------------------------------------------
189 void CollectionMailingListPage::slotHoldsML( bool holdsML )
190 {
191  mGroupWidget->setEnabled( holdsML );
192  mDetectButton->setEnabled( mFolder && mFolder->count()!=0 );
193 }
194 
195 //----------------------------------------------------------------------------
196 void CollectionMailingListPage::slotDetectMailingList()
197 {
198  if ( !mFolder )
199  return; // in case the folder was just created
200 
201  kDebug()<< "Detecting mailing list";
202 
203  // next try the 5 most recently added messages
204  if ( !( mMailingList.features() & MailingList::Post ) ) {
205  //FIXME not load all folder
206  Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( mFolder->collection(), this );
207  job->fetchScope().fetchPayloadPart( Akonadi::MessagePart::Header );
208  connect( job, SIGNAL(result(KJob*)), this, SLOT(slotFetchDone(KJob*)) );
209  //Don't allow to reactive it
210  mDetectButton->setEnabled( false );
211  } else {
212  mMLId->setText( (mMailingList.id().isEmpty() ? i18n("Not available.") : mMailingList.id() ) );
213  fillEditBox();
214  }
215 }
216 
217 
218 void CollectionMailingListPage::slotFetchDone( KJob* job )
219 {
220  mDetectButton->setEnabled( true );
221  if ( MailCommon::Util::showJobErrorMessage(job) ) {
222  return;
223  }
224  Akonadi::ItemFetchJob *fjob = dynamic_cast<Akonadi::ItemFetchJob*>( job );
225  Q_ASSERT( fjob );
226  Akonadi::Item::List items = fjob->items();
227  const int maxchecks = 5;
228  int num = items.size();
229  for ( int i = --num ; ( i > num - maxchecks ) && ( i >= 0 ); --i ) {
230  Akonadi::Item item = items[i];
231  if ( item.hasPayload<KMime::Message::Ptr>() ) {
232  KMime::Message::Ptr message = item.payload<KMime::Message::Ptr>();
233  mMailingList = MessageCore::MailingList::detect( message );
234  if ( mMailingList.features() & MailingList::Post )
235  break;
236  }
237  }
238  if ( !(mMailingList.features() & MailingList::Post) ) {
239  if ( mMailingList.features() == MailingList::None ) {
240  KMessageBox::error( this,
241  i18n("KMail was unable to detect any mailing list in this folder.") );
242  } else {
243  KMessageBox::error( this,
244  i18n("KMail was unable to fully detect a mailing list in this folder. "
245  "Please fill in the addresses by hand.") );
246  }
247  } else {
248  mMLId->setText( (mMailingList.id().isEmpty() ? i18n("Not available.") : mMailingList.id() ) );
249  fillEditBox();
250  }
251 
252 }
253 
254 //----------------------------------------------------------------------------
255 void CollectionMailingListPage::slotMLHandling( int element )
256 {
257  mMailingList.setHandler( static_cast<MailingList::Handler>( element ) );
258  slotConfigChanged();
259 }
260 
261 //----------------------------------------------------------------------------
262 void CollectionMailingListPage::slotAddressChanged( int i )
263 {
264  fillMLFromWidgets();
265  fillEditBox();
266  mLastItem = i;
267  slotConfigChanged();
268 }
269 
270 //----------------------------------------------------------------------------
271 void CollectionMailingListPage::fillMLFromWidgets()
272 {
273  if ( !mHoldsMailingList->isChecked() )
274  return;
275 
276  // make sure that email addresses are prepended by "mailto:"
277  bool listChanged = false;
278  const QStringList oldList = mEditList->items();
279  QStringList newList; // the correct string list
280  QStringList::ConstIterator end = oldList.constEnd();
281  for ( QStringList::ConstIterator it = oldList.constBegin(); it != end; ++it ) {
282  if ( !(*it).startsWith(QLatin1String("http:")) && !(*it).startsWith(QLatin1String("https:")) &&
283  !(*it).startsWith(QLatin1String("mailto:")) && ( (*it).contains(QLatin1Char('@')) ) ) {
284  listChanged = true;
285  newList << QLatin1String("mailto:") + *it;
286  }
287  else {
288  newList << *it;
289  }
290  }
291  if ( listChanged ) {
292  mEditList->clear();
293  mEditList->insertStringList( newList );
294  }
295 
296  //mMailingList.setHandler( static_cast<MailingList::Handler>( mMLHandlerCombo->currentIndex() ) );
297  switch ( mLastItem ) {
298  case 0:
299  mMailingList.setPostUrls( mEditList->items() );
300  break;
301  case 1:
302  mMailingList.setSubscribeUrls( mEditList->items() );
303  break;
304  case 2:
305  mMailingList.setUnsubscribeUrls( mEditList->items() );
306  break;
307  case 3:
308  mMailingList.setArchiveUrls( mEditList->items() );
309  break;
310  case 4:
311  mMailingList.setHelpUrls( mEditList->items() );
312  break;
313  default:
314  kWarning()<<"Wrong entry in the mailing list entry combo!";
315  }
316 }
317 
318 void CollectionMailingListPage::fillEditBox()
319 {
320  mEditList->clear();
321  switch ( mAddressCombo->currentIndex() ) {
322  case 0:
323  mEditList->insertStringList( mMailingList.postUrls().toStringList() );
324  break;
325  case 1:
326  mEditList->insertStringList( mMailingList.subscribeUrls().toStringList() );
327  break;
328  case 2:
329  mEditList->insertStringList( mMailingList.unsubscribeUrls().toStringList() );
330  break;
331  case 3:
332  mEditList->insertStringList( mMailingList.archiveUrls().toStringList() );
333  break;
334  case 4:
335  mEditList->insertStringList( mMailingList.helpUrls().toStringList() );
336  break;
337  default:
338  kWarning()<<"Wrong entry in the mailing list entry combo!";
339  }
340 }
341 
342 void CollectionMailingListPage::slotInvokeHandler()
343 {
344  save( mCurrentCollection );
345  switch ( mAddressCombo->currentIndex() ) {
346  case 0:
347  KMail::Util::mailingListPost( mFolder );
348  break;
349  case 1:
350  KMail::Util::mailingListSubscribe( mFolder );
351  break;
352  case 2:
353  KMail::Util::mailingListUnsubscribe( mFolder );
354  break;
355  case 3:
356  KMail::Util::mailingListArchives( mFolder );
357  break;
358  case 4:
359  KMail::Util::mailingListHelp( mFolder );
360  break;
361  default:
362  kWarning()<<"Wrong entry in the mailing list entry combo!";
363  }
364 }
365 
366 #include "collectionmailinglistpage.moc"
KMail::Util::mailingListSubscribe
bool mailingListSubscribe(const QSharedPointer< MailCommon::FolderCollection > &fd)
Definition: util.cpp:172
QSharedPointer
Definition: collectionmailinglistpage.h:34
KMail::Util::mailingListPost
bool mailingListPost(const QSharedPointer< MailCommon::FolderCollection > &fd)
Definition: util.cpp:165
CollectionMailingListPage::init
void init(const Akonadi::Collection &)
Definition: collectionmailinglistpage.cpp:75
KMail::Util::mailingListUnsubscribe
bool mailingListUnsubscribe(const QSharedPointer< MailCommon::FolderCollection > &fd)
Definition: util.cpp:179
CollectionMailingListPage::CollectionMailingListPage
CollectionMailingListPage(QWidget *parent=0)
Definition: collectionmailinglistpage.cpp:51
QWidget
collectionmailinglistpage.h
CollectionMailingListPage::canHandle
bool canHandle(const Akonadi::Collection &col) const
Definition: collectionmailinglistpage.cpp:67
util.h
KMail::Util::mailingListArchives
bool mailingListArchives(const QSharedPointer< MailCommon::FolderCollection > &fd)
Definition: util.cpp:186
CollectionMailingListPage::slotFetchDone
void slotFetchDone(KJob *job)
Definition: collectionmailinglistpage.cpp:218
KMail::Util::mailingListHelp
bool mailingListHelp(const QSharedPointer< MailCommon::FolderCollection > &fd)
Definition: util.cpp:193
kmkernel.h
QLabel
KComboBox
CollectionMailingListPage::~CollectionMailingListPage
~CollectionMailingListPage()
Definition: collectionmailinglistpage.cpp:58
CollectionMailingListPage::save
void save(Akonadi::Collection &col)
Definition: collectionmailinglistpage.cpp:175
CollectionMailingListPage::load
void load(const Akonadi::Collection &col)
Definition: collectionmailinglistpage.cpp:158
KJob
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:51 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kmail

Skip menu "kmail"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal