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

kmail

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

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