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

mailcommon

  • sources
  • kde-4.14
  • kdepim
  • mailcommon
  • filter
  • filteractions
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 #include "libkdepim/widgets/tagwidgets.h"
26 
27 #include <KDE/Akonadi/CollectionComboBox>
28 #include <KDE/KABC/Addressee>
29 #include <KDE/KLineEdit>
30 #include <KDE/KLocale>
31 #include <KDE/KPIMUtils/Email>
32 
33 #include <QGridLayout>
34 #include <QLabel>
35 
36 using namespace MailCommon;
37 
38 FilterAction* FilterActionAddToAddressBook::newAction()
39 {
40  return new FilterActionAddToAddressBook;
41 }
42 
43 FilterActionAddToAddressBook::FilterActionAddToAddressBook( QObject *parent )
44  : FilterActionWithStringList( QLatin1String("add to address book"), i18n( "Add to Address Book" ), parent ),
45  mFromStr( i18nc( "Email sender", "From" ) ),
46  mToStr( i18nc( "Email recipient", "To" ) ),
47  mCCStr( i18n( "CC" ) ),
48  mBCCStr( i18n( "BCC" ) ),
49  mHeaderType( UnknownHeader ),
50  mCollectionId( -1 ),
51  mCategory( i18n( "KMail Filter" ) )
52 {
53 }
54 
55 bool FilterActionAddToAddressBook::isEmpty() const
56 {
57  return (mCollectionId == -1) || (mHeaderType == UnknownHeader);
58 }
59 
60 FilterAction::ReturnCode FilterActionAddToAddressBook::process(ItemContext &context , bool) const
61 {
62  if ( isEmpty() )
63  return ErrorButGoOn;
64 
65  const KMime::Message::Ptr msg = context.item().payload<KMime::Message::Ptr>();
66 
67  QString headerLine;
68  switch ( mHeaderType ) {
69  case FromHeader: headerLine = msg->from()->asUnicodeString(); break;
70  case ToHeader: headerLine = msg->to()->asUnicodeString(); break;
71  case CcHeader: headerLine = msg->cc()->asUnicodeString(); break;
72  case BccHeader: headerLine = msg->bcc()->asUnicodeString(); break;
73  case UnknownHeader: break;
74  }
75  if (headerLine.isEmpty())
76  return ErrorButGoOn;
77 
78  const QStringList emails = KPIMUtils::splitAddressList( headerLine );
79 
80  foreach ( const QString& singleEmail, emails ) {
81  QString name, email;
82  KABC::Addressee::parseEmailAddress( singleEmail, name, email );
83 
84  KABC::Addressee contact;
85  contact.setNameFromString( name );
86  contact.insertEmail( email, true );
87  if ( !mCategory.isEmpty() )
88  contact.setCategories( mCategory.split(QLatin1String(";")) );
89 
90  KPIM::AddContactJob *job = new KPIM::AddContactJob( contact, Akonadi::Collection( mCollectionId ) );
91  job->showMessageBox(false);
92  job->start();
93  }
94 
95  return GoOn;
96 }
97 
98 SearchRule::RequiredPart FilterActionAddToAddressBook::requiredPart() const
99 {
100  return SearchRule::Envelope;
101 }
102 
103 
104 QWidget* FilterActionAddToAddressBook::createParamWidget( QWidget *parent ) const
105 {
106  QWidget *widget = new QWidget( parent );
107  QGridLayout *layout = new QGridLayout( widget );
108 
109  PimCommon::MinimumComboBox *headerCombo = new PimCommon::MinimumComboBox( widget );
110  headerCombo->setObjectName( QLatin1String("HeaderComboBox") );
111  layout->addWidget( headerCombo, 0, 0, 2, 1, Qt::AlignVCenter );
112 
113  QLabel *label = new QLabel( i18n( "with category" ), widget );
114  label->setObjectName(QLatin1String("label_with_category"));
115  layout->addWidget( label, 0, 1 );
116 
117  KPIM::TagWidget *categoryEdit = new KPIM::TagWidget( widget );
118  categoryEdit->setObjectName( QLatin1String("CategoryEdit") );
119  layout->addWidget( categoryEdit, 0, 2 );
120 
121  label = new QLabel( i18n( "in address book" ), widget );
122  label->setObjectName(QLatin1String("label_in_addressbook"));
123  layout->addWidget( label, 1, 1 );
124 
125  Akonadi::CollectionComboBox *collectionComboBox = new Akonadi::CollectionComboBox( widget );
126  collectionComboBox->setMimeTypeFilter( QStringList() << KABC::Addressee::mimeType() );
127  collectionComboBox->setAccessRightsFilter( Akonadi::Collection::CanCreateItem );
128 
129  collectionComboBox->setObjectName( QLatin1String("AddressBookComboBox") );
130  collectionComboBox->setToolTip( i18n( "<p>This defines the preferred address book.<br />"
131  "If it is not accessible, the filter will fallback to the default address book.</p>" ) );
132  layout->addWidget( collectionComboBox, 1, 2 );
133 
134  connect( categoryEdit, SIGNAL(selectionChanged(QStringList)),
135  this, SIGNAL(filterActionModified()) );
136  connect( headerCombo, SIGNAL(currentIndexChanged(int)),
137  this, SIGNAL(filterActionModified()) );
138  connect( collectionComboBox, SIGNAL(activated(int)),
139  this, SIGNAL(filterActionModified()) );
140 
141  setParamWidgetValue( widget );
142 
143  return widget;
144 }
145 
146 void FilterActionAddToAddressBook::setParamWidgetValue( QWidget *paramWidget ) const
147 {
148  PimCommon::MinimumComboBox *headerCombo = paramWidget->findChild<PimCommon::MinimumComboBox*>( QLatin1String("HeaderComboBox") );
149  Q_ASSERT( headerCombo );
150  headerCombo->clear();
151  headerCombo->addItem( mFromStr, FromHeader );
152  headerCombo->addItem( mToStr, ToHeader );
153  headerCombo->addItem( mCCStr, CcHeader );
154  headerCombo->addItem( mBCCStr, BccHeader );
155 
156  headerCombo->setCurrentIndex( headerCombo->findData( mHeaderType ) );
157 
158  KPIM::TagWidget *categoryEdit = paramWidget->findChild<KPIM::TagWidget*>( QLatin1String("CategoryEdit") );
159  Q_ASSERT( categoryEdit );
160  categoryEdit->setSelection( mCategory.split(QLatin1String(";")) );
161 
162  Akonadi::CollectionComboBox *collectionComboBox = paramWidget->findChild<Akonadi::CollectionComboBox*>( QLatin1String("AddressBookComboBox") );
163  Q_ASSERT( collectionComboBox );
164  collectionComboBox->setDefaultCollection( Akonadi::Collection( mCollectionId ) );
165  collectionComboBox->setProperty( "collectionId", mCollectionId );
166 }
167 
168 void FilterActionAddToAddressBook::applyParamWidgetValue( QWidget *paramWidget )
169 {
170  const PimCommon::MinimumComboBox *headerCombo = paramWidget->findChild<PimCommon::MinimumComboBox*>( QLatin1String("HeaderComboBox") );
171  Q_ASSERT( headerCombo );
172  mHeaderType = static_cast<HeaderType>( headerCombo->itemData( headerCombo->currentIndex() ).toInt() );
173 
174  const KPIM::TagWidget *categoryEdit = paramWidget->findChild<KPIM::TagWidget*>( QLatin1String("CategoryEdit") );
175  Q_ASSERT( categoryEdit );
176  mCategory = categoryEdit->selection().join(QLatin1String(";"));
177 
178  const Akonadi::CollectionComboBox *collectionComboBox = paramWidget->findChild<Akonadi::CollectionComboBox*>( QLatin1String("AddressBookComboBox") );
179  Q_ASSERT( collectionComboBox );
180  const Akonadi::Collection collection = collectionComboBox->currentCollection();
181 
182  // it might be that the model of collectionComboBox has not finished loading yet, so
183  // we use the previously 'stored' value from the 'collectionId' property
184  if ( collection.isValid() ) {
185  mCollectionId = collection.id();
186  connect( collectionComboBox, SIGNAL(currentIndexChanged(int)),
187  this, SIGNAL(filterActionModified()) );
188  } else {
189  const QVariant value = collectionComboBox->property( "collectionId" );
190  if ( value.isValid() )
191  mCollectionId = value.toLongLong();
192  }
193 }
194 
195 void FilterActionAddToAddressBook::clearParamWidget( QWidget *paramWidget ) const
196 {
197  PimCommon::MinimumComboBox *headerCombo = paramWidget->findChild<PimCommon::MinimumComboBox*>( QLatin1String("HeaderComboBox") );
198  Q_ASSERT( headerCombo );
199  headerCombo->setCurrentIndex( 0 );
200 
201  KPIM::TagWidget *categoryEdit = paramWidget->findChild<KPIM::TagWidget*>( QLatin1String("CategoryEdit") );
202  Q_ASSERT( categoryEdit );
203  categoryEdit->setSelection( mCategory.split(QLatin1String(";")) );
204 }
205 
206 QString FilterActionAddToAddressBook::argsAsString() const
207 {
208  QString result;
209 
210  switch ( mHeaderType ) {
211  case FromHeader: result = QLatin1String( "From" ); break;
212  case ToHeader: result = QLatin1String( "To" ); break;
213  case CcHeader: result = QLatin1String( "CC" ); break;
214  case BccHeader: result = QLatin1String( "BCC" ); break;
215  case UnknownHeader: break;
216  }
217 
218  result += QLatin1Char( '\t' );
219  result += QString::number( mCollectionId );
220  result += QLatin1Char( '\t' );
221  result += mCategory;
222 
223  return result;
224 }
225 
226 
227 void FilterActionAddToAddressBook::argsFromString( const QString &argsStr )
228 {
229  const QStringList parts = argsStr.split( QLatin1Char( '\t' ), QString::KeepEmptyParts );
230  const QString firstElement = parts[ 0 ];
231  if ( firstElement == QLatin1String( "From" ) )
232  mHeaderType = FromHeader;
233  else if ( firstElement == QLatin1String( "To" ) )
234  mHeaderType = ToHeader;
235  else if ( firstElement == QLatin1String( "CC" ) )
236  mHeaderType = CcHeader;
237  else if ( firstElement == QLatin1String( "BCC" ) )
238  mHeaderType = BccHeader;
239  else
240  mHeaderType = UnknownHeader;
241 
242  if ( parts.count() >= 2 )
243  mCollectionId = parts[ 1 ].toLongLong();
244 
245  if ( parts.count() < 3 )
246  mCategory.clear();
247  else
248  mCategory = parts[ 2 ];
249 }
250 
251 QString FilterActionAddToAddressBook::informationAboutNotValidAction() const
252 {
253  //KF5 add i18n
254  QString result;
255  if (mHeaderType == UnknownHeader) {
256  result = QLatin1String("Header type selected is unknown.");
257  }
258  if (mCollectionId == -1) {
259  if (!result.isEmpty()) {
260  result += QLatin1Char('\n');
261  }
262  result += QLatin1String("No addressbook selected.");
263  }
264  return result;
265 }
QVariant::toLongLong
qlonglong toLongLong(bool *ok) const
QWidget
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
QGridLayout::addWidget
void addWidget(QWidget *widget, int row, int column, QFlags< Qt::AlignmentFlag > alignment)
MailCommon::FilterActionAddToAddressBook::informationAboutNotValidAction
QString informationAboutNotValidAction() const
Definition: filteractionaddtoaddressbook.cpp:251
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
MailCommon::FilterActionAddToAddressBook::applyParamWidgetValue
void applyParamWidgetValue(QWidget *paramWidget)
The filter action shall set it's parameter from the widget's contents.
Definition: filteractionaddtoaddressbook.cpp:168
QGridLayout
filteractionaddtoaddressbook.h
QString::clear
void clear()
MailCommon::FilterAction
Abstract base class for mail filter actions.
Definition: filteraction.h:52
QString::number
QString number(int n, int base)
QList::count
int count(const T &value) const
MailCommon::FilterAction::ErrorButGoOn
A non-critical error occurred.
Definition: filteraction.h:63
QObject
QObject::setObjectName
void setObjectName(const QString &name)
QString::isEmpty
bool isEmpty() const
MailCommon::SearchRule::RequiredPart
RequiredPart
Definition: searchrule.h:79
MailCommon::FilterActionAddToAddressBook::createParamWidget
QWidget * createParamWidget(QWidget *parent) const
Creates a widget for setting the filter action parameter.
Definition: filteractionaddtoaddressbook.cpp:104
QString
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
QStringList
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:98
MailCommon::FilterActionAddToAddressBook::argsAsString
QString argsAsString() const
Return extra arguments as string.
Definition: filteractionaddtoaddressbook.cpp:206
MailCommon::FilterActionAddToAddressBook::FilterActionAddToAddressBook
FilterActionAddToAddressBook(QObject *parent=0)
Definition: filteractionaddtoaddressbook.cpp:43
QLatin1Char
MailCommon::SearchRule::Envelope
Definition: searchrule.h:80
MailCommon::FilterActionAddToAddressBook::process
ReturnCode process(ItemContext &context, bool applyOnOutbound) const
Execute action on given message (inside the item context).
Definition: filteractionaddtoaddressbook.cpp:60
MailCommon::FilterAction::ReturnCode
ReturnCode
Describes the possible return codes of filter processing:
Definition: filteraction.h:60
QLatin1String
MailCommon::ItemContext::item
Akonadi::Item & item()
Returns the item of the context.
Definition: itemcontext.cpp:29
MailCommon::FilterActionAddToAddressBook::newAction
static FilterAction * newAction()
Definition: filteractionaddtoaddressbook.cpp:38
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:227
context
const char * context
Definition: searchpatternedit.cpp:54
QVariant::isValid
bool isValid() const
MailCommon::FilterActionAddToAddressBook::clearParamWidget
void clearParamWidget(QWidget *paramWidget) const
The filter action shall clear it's parameter widget's contents.
Definition: filteractionaddtoaddressbook.cpp:195
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:146
MailCommon::ItemContext
A helper class for the filtering process.
Definition: itemcontext.h:39
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QLabel
MailCommon::FilterActionAddToAddressBook::isEmpty
bool isEmpty() const
Determines whether this action is valid.
Definition: filteractionaddtoaddressbook.cpp:55
QVariant
QObject::findChild
T findChild(const QString &name) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:31:40 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
  • 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