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

mailcommon

  • sources
  • kde-4.12
  • kdepim
  • mailcommon
  • filter
filteractionaddtoaddressbook.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 1996-1998 Stefan Taferner <taferner@kde.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  */
19 
20 #include "filteractionaddtoaddressbook.h"
21 
22 #include "pimcommon/widgets/minimumcombobox.h"
23 
24 #include "libkdepim/job/addcontactjob.h"
25 
26 #include <KDE/Akonadi/CollectionComboBox>
27 #include <KDE/KABC/Addressee>
28 #include <KDE/KLineEdit>
29 #include <KDE/KLocale>
30 #include <KDE/KPIMUtils/Email>
31 
32 #include <QGridLayout>
33 #include <QLabel>
34 
35 using namespace MailCommon;
36 
37 FilterAction* FilterActionAddToAddressBook::newAction()
38 {
39  return new FilterActionAddToAddressBook;
40 }
41 
42 FilterActionAddToAddressBook::FilterActionAddToAddressBook( QObject *parent )
43  : FilterActionWithStringList( QLatin1String("add to address book"), i18n( "Add to Address Book" ), parent ),
44  mFromStr( i18nc( "Email sender", "From" ) ),
45  mToStr( i18nc( "Email recipient", "To" ) ),
46  mCCStr( i18n( "CC" ) ),
47  mBCCStr( i18n( "BCC" ) ),
48  mHeaderType( FromHeader ),
49  mCollectionId( -1 ),
50  mCategory( i18n( "KMail Filter" ) )
51 {
52 }
53 
54 bool FilterActionAddToAddressBook::isEmpty() const
55 {
56  return false;
57 }
58 
59 FilterAction::ReturnCode FilterActionAddToAddressBook::process(ItemContext &context , bool) const
60 {
61  const KMime::Message::Ptr msg = context.item().payload<KMime::Message::Ptr>();
62 
63  QString headerLine;
64  switch ( mHeaderType ) {
65  case FromHeader: headerLine = msg->from()->asUnicodeString(); break;
66  case ToHeader: headerLine = msg->to()->asUnicodeString(); break;
67  case CcHeader: headerLine = msg->cc()->asUnicodeString(); break;
68  case BccHeader: headerLine = msg->bcc()->asUnicodeString(); break;
69  }
70 
71  const QStringList emails = KPIMUtils::splitAddressList( headerLine );
72 
73  foreach ( const QString& singleEmail, emails ) {
74  QString name, email;
75  KABC::Addressee::parseEmailAddress( singleEmail, name, email );
76 
77  KABC::Addressee contact;
78  contact.setNameFromString( name );
79  contact.insertEmail( email, true );
80  if ( !mCategory.isEmpty() )
81  contact.insertCategory( mCategory );
82 
83  KPIM::AddContactJob *job = new KPIM::AddContactJob( contact, Akonadi::Collection( mCollectionId ) );
84  job->showMessageBox(false);
85  job->start();
86  }
87 
88  return GoOn;
89 }
90 
91 SearchRule::RequiredPart FilterActionAddToAddressBook::requiredPart() const
92 {
93  return SearchRule::Envelope;
94 }
95 
96 
97 QWidget* FilterActionAddToAddressBook::createParamWidget( QWidget *parent ) const
98 {
99  QWidget *widget = new QWidget( parent );
100  QGridLayout *layout = new QGridLayout( widget );
101 
102  PimCommon::MinimumComboBox *headerCombo = new PimCommon::MinimumComboBox( widget );
103  headerCombo->setObjectName( QLatin1String("HeaderComboBox") );
104  layout->addWidget( headerCombo, 0, 0, 2, 1, Qt::AlignVCenter );
105 
106  QLabel *label = new QLabel( i18n( "with category" ), widget );
107  layout->addWidget( label, 0, 1 );
108 
109  KLineEdit *categoryEdit = new KLineEdit( widget );
110  categoryEdit->setObjectName( QLatin1String("CategoryEdit") );
111  categoryEdit->setTrapReturnKey(true);
112  layout->addWidget( categoryEdit, 0, 2 );
113 
114  label = new QLabel( i18n( "in address book" ), widget );
115  layout->addWidget( label, 1, 1 );
116 
117  Akonadi::CollectionComboBox *collectionComboBox = new Akonadi::CollectionComboBox( widget );
118  collectionComboBox->setMimeTypeFilter( QStringList() << KABC::Addressee::mimeType() );
119  collectionComboBox->setAccessRightsFilter( Akonadi::Collection::CanCreateItem );
120 
121  collectionComboBox->setObjectName( QLatin1String("AddressBookComboBox") );
122  collectionComboBox->setToolTip( i18n( "<p>This defines the preferred address book.<br />"
123  "If it is not accessible, the filter will fallback to the default address book.</p>" ) );
124  layout->addWidget( collectionComboBox, 1, 2 );
125 
126  connect( categoryEdit, SIGNAL(textChanged(QString)),
127  this, SIGNAL(filterActionModified()) );
128  connect( headerCombo, SIGNAL(currentIndexChanged(int)),
129  this, SIGNAL(filterActionModified()) );
130  connect( collectionComboBox, SIGNAL(activated(int)),
131  this, SIGNAL(filterActionModified()) );
132 
133  setParamWidgetValue( widget );
134 
135  return widget;
136 }
137 
138 void FilterActionAddToAddressBook::setParamWidgetValue( QWidget *paramWidget ) const
139 {
140  PimCommon::MinimumComboBox *headerCombo = paramWidget->findChild<PimCommon::MinimumComboBox*>( QLatin1String("HeaderComboBox") );
141  Q_ASSERT( headerCombo );
142  headerCombo->clear();
143  headerCombo->addItem( mFromStr, FromHeader );
144  headerCombo->addItem( mToStr, ToHeader );
145  headerCombo->addItem( mCCStr, CcHeader );
146  headerCombo->addItem( mBCCStr, BccHeader );
147 
148  headerCombo->setCurrentIndex( headerCombo->findData( mHeaderType ) );
149 
150  KLineEdit *categoryEdit = paramWidget->findChild<KLineEdit*>( QLatin1String("CategoryEdit") );
151  Q_ASSERT( categoryEdit );
152  categoryEdit->setText( mCategory );
153 
154  Akonadi::CollectionComboBox *collectionComboBox = paramWidget->findChild<Akonadi::CollectionComboBox*>( QLatin1String("AddressBookComboBox") );
155  Q_ASSERT( collectionComboBox );
156  collectionComboBox->setDefaultCollection( Akonadi::Collection( mCollectionId ) );
157  collectionComboBox->setProperty( "collectionId", mCollectionId );
158 }
159 
160 void FilterActionAddToAddressBook::applyParamWidgetValue( QWidget *paramWidget )
161 {
162  const PimCommon::MinimumComboBox *headerCombo = paramWidget->findChild<PimCommon::MinimumComboBox*>( QLatin1String("HeaderComboBox") );
163  Q_ASSERT( headerCombo );
164  mHeaderType = static_cast<HeaderType>( headerCombo->itemData( headerCombo->currentIndex() ).toInt() );
165 
166  const KLineEdit *categoryEdit = paramWidget->findChild<KLineEdit*>( QLatin1String("CategoryEdit") );
167  Q_ASSERT( categoryEdit );
168  mCategory = categoryEdit->text();
169 
170  const Akonadi::CollectionComboBox *collectionComboBox = paramWidget->findChild<Akonadi::CollectionComboBox*>( QLatin1String("AddressBookComboBox") );
171  Q_ASSERT( collectionComboBox );
172  const Akonadi::Collection collection = collectionComboBox->currentCollection();
173 
174  // it might be that the model of collectionComboBox has not finished loading yet, so
175  // we use the previously 'stored' value from the 'collectionId' property
176  if ( collection.isValid() ) {
177  mCollectionId = collection.id();
178  connect( collectionComboBox, SIGNAL(currentIndexChanged(int)),
179  this, SIGNAL(filterActionModified()) );
180  } else {
181  const QVariant value = collectionComboBox->property( "collectionId" );
182  if ( value.isValid() )
183  mCollectionId = value.toLongLong();
184  }
185 }
186 
187 void FilterActionAddToAddressBook::clearParamWidget( QWidget *paramWidget ) const
188 {
189  PimCommon::MinimumComboBox *headerCombo = paramWidget->findChild<PimCommon::MinimumComboBox*>( QLatin1String("HeaderComboBox") );
190  Q_ASSERT( headerCombo );
191  headerCombo->setCurrentIndex( 0 );
192 
193  KLineEdit *categoryEdit = paramWidget->findChild<KLineEdit*>( QLatin1String("CategoryEdit") );
194  Q_ASSERT( categoryEdit );
195  categoryEdit->setText( mCategory );
196 }
197 
198 QString FilterActionAddToAddressBook::argsAsString() const
199 {
200  QString result;
201 
202  switch ( mHeaderType ) {
203  case FromHeader: result = QLatin1String( "From" ); break;
204  case ToHeader: result = QLatin1String( "To" ); break;
205  case CcHeader: result = QLatin1String( "CC" ); break;
206  case BccHeader: result = QLatin1String( "BCC" ); break;
207  }
208 
209  result += QLatin1Char( '\t' );
210  result += QString::number( mCollectionId );
211  result += QLatin1Char( '\t' );
212  result += mCategory;
213 
214  return result;
215 }
216 
217 
218 void FilterActionAddToAddressBook::argsFromString( const QString &argsStr )
219 {
220  const QStringList parts = argsStr.split( QLatin1Char( '\t' ), QString::KeepEmptyParts );
221  const QString firstElement = parts[ 0 ];
222  if ( firstElement == QLatin1String( "From" ) )
223  mHeaderType = FromHeader;
224  else if ( firstElement == QLatin1String( "To" ) )
225  mHeaderType = ToHeader;
226  else if ( firstElement == QLatin1String( "CC" ) )
227  mHeaderType = CcHeader;
228  else if ( firstElement == QLatin1String( "BCC" ) )
229  mHeaderType = BccHeader;
230 
231  if ( parts.count() >= 2 )
232  mCollectionId = parts[ 1 ].toLongLong();
233 
234  if ( parts.count() < 3 )
235  mCategory.clear();
236  else
237  mCategory = parts[ 2 ];
238 }
239 
240 #include "filteractionaddtoaddressbook.moc"
MailCommon::FilterAction::GoOn
Go on with applying filter actions.
Definition: filteraction.h:62
MailCommon::FilterActionWithStringList
Abstract base class for filter actions with a fixed set of string parameters.
Definition: filteractionwithstringlist.h:49
MailCommon::FilterActionAddToAddressBook::applyParamWidgetValue
void applyParamWidgetValue(QWidget *paramWidget)
The filter action shall set it's parameter from the widget's contents.
Definition: filteractionaddtoaddressbook.cpp:160
QWidget
QObject
filteractionaddtoaddressbook.h
MailCommon::FilterAction
Abstract base class for mail filter actions.
Definition: filteraction.h:52
MailCommon::SearchRule::RequiredPart
RequiredPart
Definition: searchpattern.h:104
MailCommon::FilterActionAddToAddressBook::createParamWidget
QWidget * createParamWidget(QWidget *parent) const
Creates a widget for setting the filter action parameter.
Definition: filteractionaddtoaddressbook.cpp:97
MailCommon::FilterAction::label
QString label() const
Returns i18n'd label, ie.
Definition: filteraction.cpp:45
MailCommon::FilterAction::name
QString name() const
Returns identifier name, ie.
Definition: filteraction.cpp:50
MailCommon::FilterActionAddToAddressBook::requiredPart
SearchRule::RequiredPart requiredPart() const
Returns the required part from the item that is needed for the action to operate. ...
Definition: filteractionaddtoaddressbook.cpp:91
MailCommon::FilterActionAddToAddressBook::argsAsString
QString argsAsString() const
Return extra arguments as string.
Definition: filteractionaddtoaddressbook.cpp:198
MailCommon::FilterActionAddToAddressBook::FilterActionAddToAddressBook
FilterActionAddToAddressBook(QObject *parent=0)
Definition: filteractionaddtoaddressbook.cpp:42
MailCommon::SearchRule::Envelope
Definition: searchpattern.h:105
MailCommon::FilterActionAddToAddressBook::process
ReturnCode process(ItemContext &context, bool applyOnOutbound) const
Execute action on given message (inside the item context).
Definition: filteractionaddtoaddressbook.cpp:59
MailCommon::FilterAction::ReturnCode
ReturnCode
Describes the possible return codes of filter processing:
Definition: filteraction.h:60
MailCommon::ItemContext::item
Akonadi::Item & item()
Returns the item of the context.
Definition: itemcontext.cpp:30
MailCommon::FilterActionAddToAddressBook::newAction
static FilterAction * newAction()
Definition: filteractionaddtoaddressbook.cpp:37
MailCommon::FilterAction::filterActionModified
void filterActionModified()
Called to notify that the current FilterAction has had some value modification.
MailCommon::FilterActionAddToAddressBook::argsFromString
void argsFromString(const QString &argsStr)
Read extra arguments from given string.
Definition: filteractionaddtoaddressbook.cpp:218
context
const char * context
Definition: searchpatternedit.cpp:54
MailCommon::FilterActionAddToAddressBook::clearParamWidget
void clearParamWidget(QWidget *paramWidget) const
The filter action shall clear it's parameter widget's contents.
Definition: filteractionaddtoaddressbook.cpp:187
MailCommon::FilterActionAddToAddressBook::setParamWidgetValue
void setParamWidgetValue(QWidget *paramWidget) const
The filter action shall set it's widget's contents from it's parameter.
Definition: filteractionaddtoaddressbook.cpp:138
MailCommon::ItemContext
A helper class for the filtering process.
Definition: itemcontext.h:39
MailCommon::FilterActionAddToAddressBook::isEmpty
bool isEmpty() const
Determines whether this action is valid.
Definition: filteractionaddtoaddressbook.cpp:54
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:14 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

mailcommon

Skip menu "mailcommon"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

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