• 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
filteractionforward.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 "filteractionforward.h"
21 
22 #include "kernel/mailkernel.h"
23 #include "util/mailutil.h"
24 #include "dialog/filteractionmissingargumentdialog.h"
25 
26 #include <pimcommon/widgets/minimumcombobox.h>
27 #include <messagecomposer/helper/messagefactory.h>
28 #include <messagecomposer/sender/messagesender.h>
29 #include <messagecore/widgets/emailaddressrequester.h>
30 #include <messagecore/utils/stringutil.h>
31 #include <templateparser/customtemplates.h>
32 #include <templateparser/customtemplates_kfg.h>
33 
34 #include <KDE/KLocale>
35 #include <KDE/KLineEdit>
36 
37 #include <QHBoxLayout>
38 
39 using namespace MailCommon;
40 
41 FilterAction *FilterActionForward::newAction()
42 {
43  return new FilterActionForward;
44 }
45 
46 FilterActionForward::FilterActionForward( QObject *parent )
47  : FilterActionWithAddress(QLatin1String( "forward"), i18nc( "Forward directly not with a command", "Forward To" ), parent )
48 {
49 }
50 
51 FilterAction::ReturnCode FilterActionForward::process(ItemContext &context , bool) const
52 {
53  if ( mParameter.isEmpty() )
54  return ErrorButGoOn;
55 
56  const KMime::Message::Ptr msg = context.item().payload<KMime::Message::Ptr>();
57  // avoid endless loops when this action is used in a filter
58  // which applies to sent messages
59  if ( MessageCore::StringUtil::addressIsInAddressList( mParameter,
60  QStringList( msg->to()->asUnicodeString() ) ) ) {
61  kWarning() << "Attempt to forward to receipient of original message, ignoring.";
62  return ErrorButGoOn;
63  }
64 
65  MessageComposer::MessageFactory factory( msg, context.item().id() );
66  factory.setIdentityManager( KernelIf->identityManager() );
67  factory.setFolderIdentity( Util::folderIdentity( context.item() ) );
68  factory.setTemplate( mTemplate );
69 
70  KMime::Message::Ptr fwdMsg = factory.createForward();
71  fwdMsg->to()->fromUnicodeString( fwdMsg->to()->asUnicodeString() + QLatin1Char( ',' ) + mParameter, "utf-8" );
72  if ( !KernelIf->msgSender()->send( fwdMsg, MessageComposer::MessageSender::SendDefault ) ) {
73  kWarning() << "FilterAction: could not forward message (sending failed)";
74  return ErrorButGoOn; // error: couldn't send
75  } else
76  sendMDN( context.item(), KMime::MDN::Dispatched );
77 
78  // (the msgSender takes ownership of the message, so don't delete it here)
79  return GoOn;
80 }
81 
82 SearchRule::RequiredPart FilterActionForward::requiredPart() const
83 {
84  return SearchRule::CompleteMessage;
85 }
86 
87 QWidget* FilterActionForward::createParamWidget( QWidget *parent ) const
88 {
89  QWidget *addressAndTemplate = new QWidget( parent );
90  QHBoxLayout *layout = new QHBoxLayout( addressAndTemplate );
91  layout->setMargin( 0 );
92 
93  QWidget *addressEdit = FilterActionWithAddress::createParamWidget( addressAndTemplate );
94  addressEdit->setObjectName( QLatin1String("addressEdit") );
95  layout->addWidget( addressEdit );
96 
97  MessageCore::EmailAddressRequester *addressRequester = qobject_cast<MessageCore::EmailAddressRequester*>( addressEdit );
98  Q_ASSERT( addressRequester );
99  KLineEdit *lineEdit = addressRequester->lineEdit();
100  lineEdit->setClearButtonShown(true);
101  lineEdit->setTrapReturnKey(true);
102  lineEdit->setToolTip( i18n( "The addressee to whom the message will be forwarded." ) );
103  lineEdit->setWhatsThis( i18n( "The filter will forward the message to the addressee entered here." ) );
104 
105  PimCommon::MinimumComboBox *templateCombo = new PimCommon::MinimumComboBox( addressAndTemplate );
106  templateCombo->setObjectName( QLatin1String("templateCombo") );
107  layout->addWidget( templateCombo );
108 
109  templateCombo->addItem( i18n( "Default Template" ) );
110 
111  const QStringList templateNames = SettingsIf->customTemplates();
112  foreach( const QString &templateName, templateNames ) {
113  TemplateParser::CTemplates templat( templateName );
114  if ( templat.type() == TemplateParser::CustomTemplates::TForward ||
115  templat.type() == TemplateParser::CustomTemplates::TUniversal ) {
116  templateCombo->addItem( templateName );
117  }
118  }
119 
120  templateCombo->setEnabled( templateCombo->count() > 1 );
121  templateCombo->setToolTip( i18n( "The template used when forwarding" ) );
122  templateCombo->setWhatsThis( i18n( "Set the forwarding template that will be used with this filter." ) );
123 
124  connect( templateCombo, SIGNAL(currentIndexChanged(int)),
125  this, SIGNAL(filterActionModified()) );
126  connect( addressRequester, SIGNAL(textChanged()),
127  this, SIGNAL(filterActionModified()) );
128 
129  return addressAndTemplate;
130 }
131 
132 void FilterActionForward::applyParamWidgetValue( QWidget *paramWidget )
133 {
134  QWidget *addressEdit = paramWidget->findChild<QWidget*>( QLatin1String("addressEdit") );
135  Q_ASSERT( addressEdit );
136  FilterActionWithAddress::applyParamWidgetValue( addressEdit );
137 
138  const PimCommon::MinimumComboBox *templateCombo = paramWidget->findChild<PimCommon::MinimumComboBox*>( QLatin1String("templateCombo") );
139  Q_ASSERT( templateCombo );
140 
141  if ( templateCombo->currentIndex() == 0 ) {
142  // Default template, so don't use a custom one
143  mTemplate.clear();
144  } else {
145  mTemplate = templateCombo->currentText();
146  }
147 }
148 
149 void FilterActionForward::setParamWidgetValue( QWidget *paramWidget ) const
150 {
151  QWidget *addressEdit = paramWidget->findChild<QWidget*>( QLatin1String("addressEdit") );
152  Q_ASSERT( addressEdit );
153  FilterActionWithAddress::setParamWidgetValue( addressEdit );
154 
155  PimCommon::MinimumComboBox *templateCombo = paramWidget->findChild<PimCommon::MinimumComboBox*>( QLatin1String("templateCombo") );
156  Q_ASSERT( templateCombo );
157 
158  if ( mTemplate.isEmpty() ) {
159  templateCombo->setCurrentIndex( 0 );
160  } else {
161  int templateIndex = templateCombo->findText( mTemplate );
162  if ( templateIndex != -1 ) {
163  templateCombo->setCurrentIndex( templateIndex );
164  } else {
165  mTemplate.clear();
166  }
167  }
168 }
169 
170 void FilterActionForward::clearParamWidget( QWidget *paramWidget ) const
171 {
172  QWidget *addressEdit = paramWidget->findChild<QWidget*>( QLatin1String("addressEdit") );
173  Q_ASSERT( addressEdit );
174  FilterActionWithAddress::clearParamWidget( addressEdit );
175 
176  PimCommon::MinimumComboBox *templateCombo = paramWidget->findChild<PimCommon::MinimumComboBox*>( QLatin1String("templateCombo") );
177  Q_ASSERT( templateCombo );
178 
179  templateCombo->setCurrentIndex( 0 );
180 }
181 
182 // We simply place a "@$$@" between the two parameters. The template is the last
183 // parameter in the string, for compatibility reasons.
184 static const QString forwardFilterArgsSeperator = QLatin1String("@$$@");
185 
186 void FilterActionForward::argsFromString( const QString &argsStr )
187 {
188  const int seperatorPos = argsStr.indexOf( forwardFilterArgsSeperator );
189 
190  if ( seperatorPos == - 1 ) {
191  // Old config, assume that the whole string is the addressee
192  FilterActionWithAddress::argsFromString( argsStr );
193  } else {
194  const QString addressee = argsStr.left( seperatorPos );
195  mTemplate = argsStr.mid( seperatorPos + forwardFilterArgsSeperator.length() );
196  FilterActionWithAddress::argsFromString( addressee );
197  }
198 }
199 
200 bool FilterActionForward::argsFromStringInteractive( const QString &argsStr, const QString& filterName )
201 {
202  bool needUpdate = false;
203  argsFromString( argsStr );
204  if ( !mTemplate.isEmpty() ) {
205  const QStringList templateNames = SettingsIf->customTemplates();
206  QStringList currentTemplateList;
207  currentTemplateList << i18n( "Default Template" );
208  foreach( const QString &templateName, templateNames ) {
209  TemplateParser::CTemplates templat( templateName );
210  if ( templat.type() == TemplateParser::CustomTemplates::TForward ||
211  templat.type() == TemplateParser::CustomTemplates::TUniversal ) {
212  if ( templateName == mTemplate ) {
213  return false;
214  }
215  currentTemplateList << templateName;
216  }
217  }
218  QPointer<FilterActionMissingTemplateDialog> dlg = new FilterActionMissingTemplateDialog( currentTemplateList, filterName );
219  if ( dlg->exec() ) {
220  mTemplate = dlg->selectedTemplate();
221  needUpdate = true;
222  }
223  delete dlg;
224  }
225  return needUpdate;
226 }
227 
228 
229 QString FilterActionForward::argsAsString() const
230 {
231  return FilterActionWithAddress::argsAsString() + forwardFilterArgsSeperator + mTemplate;
232 }
233 
234 QString FilterActionForward::displayString() const
235 {
236  if ( mTemplate.isEmpty() )
237  return i18n( "Forward to %1 with default template", mParameter );
238  else
239  return i18n( "Forward to %1 with template %2", mParameter, mTemplate );
240 }
241 
242 
243 #include "filteractionforward.moc"
SettingsIf
#define SettingsIf
Definition: mailkernel.h:188
MailCommon::FilterAction::GoOn
Go on with applying filter actions.
Definition: filteraction.h:62
MailCommon::FilterActionWithAddress::createParamWidget
virtual QWidget * createParamWidget(QWidget *parent) const
Creates a widget for setting the filter action parameter.
Definition: filteractionwithaddress.cpp:31
MailCommon::FilterActionWithAddress::clearParamWidget
virtual void clearParamWidget(QWidget *paramWidget) const
The filter action shall clear it's parameter widget's contents.
Definition: filteractionwithaddress.cpp:51
KernelIf
#define KernelIf
Definition: mailkernel.h:186
FilterActionMissingTemplateDialog
Definition: filteractionmissingargumentdialog.h:114
QWidget
forwardFilterArgsSeperator
static const QString forwardFilterArgsSeperator
Definition: filteractionforward.cpp:184
filteractionmissingargumentdialog.h
QObject
MailCommon::SearchRule::CompleteMessage
Definition: searchpattern.h:107
MailCommon::FilterActionForward::FilterActionForward
FilterActionForward(QObject *parent=0)
Definition: filteractionforward.cpp:46
MailCommon::FilterAction
Abstract base class for mail filter actions.
Definition: filteraction.h:52
MailCommon::FilterActionForward::argsFromString
void argsFromString(const QString &argsStr)
Read extra arguments from given string.
Definition: filteractionforward.cpp:186
MailCommon::FilterAction::ErrorButGoOn
A non-critical error occurred.
Definition: filteraction.h:63
MailCommon::FilterAction::sendMDN
static void sendMDN(const Akonadi::Item &item, KMime::MDN::DispositionType d, const QList< KMime::MDN::DispositionModifier > &m=QList< KMime::MDN::DispositionModifier >())
Automates the sending of MDNs from filter actions.
Definition: filteraction.cpp:99
MailCommon::FilterActionForward::setParamWidgetValue
void setParamWidgetValue(QWidget *paramWidget) const
The filter action shall set it's widget's contents from it's parameter.
Definition: filteractionforward.cpp:149
MailCommon::FilterActionForward::requiredPart
SearchRule::RequiredPart requiredPart() const
Returns the required part from the item that is needed for the action to operate. ...
Definition: filteractionforward.cpp:82
MailCommon::FilterActionForward::process
ReturnCode process(ItemContext &context, bool applyOnOutbound) const
Execute action on given message (inside the item context).
Definition: filteractionforward.cpp:51
filteractionforward.h
MailCommon::SearchRule::RequiredPart
RequiredPart
Definition: searchpattern.h:104
MailCommon::FilterActionForward::clearParamWidget
void clearParamWidget(QWidget *paramWidget) const
The filter action shall clear it's parameter widget's contents.
Definition: filteractionforward.cpp:170
MailCommon::FilterActionForward::argsAsString
QString argsAsString() const
Return extra arguments as string.
Definition: filteractionforward.cpp:229
MailCommon::FilterActionWithString::mParameter
QString mParameter
Definition: filteractionwithstring.h:91
MailCommon::FilterActionForward::createParamWidget
QWidget * createParamWidget(QWidget *parent) const
Creates a widget for setting the filter action parameter.
Definition: filteractionforward.cpp:87
MailCommon::Util::folderIdentity
MAILCOMMON_EXPORT uint folderIdentity(const Akonadi::Item &item)
Returns the identity of the folder that contains the given Akonadi::Item.
Definition: mailutil.cpp:446
MailCommon::FilterAction::ReturnCode
ReturnCode
Describes the possible return codes of filter processing:
Definition: filteraction.h:60
MailCommon::FilterActionWithAddress::applyParamWidgetValue
virtual void applyParamWidgetValue(QWidget *paramWidget)
The filter action shall set it's parameter from the widget's contents.
Definition: filteractionwithaddress.cpp:41
MailCommon::FilterActionForward::displayString
QString displayString() const
Returns a translated string describing this filter for visualization purposes, e.g.
Definition: filteractionforward.cpp:234
MailCommon::ItemContext::item
Akonadi::Item & item()
Returns the item of the context.
Definition: itemcontext.cpp:30
mailkernel.h
MailCommon::FilterAction::filterActionModified
void filterActionModified()
Called to notify that the current FilterAction has had some value modification.
context
const char * context
Definition: searchpatternedit.cpp:54
MailCommon::FilterActionWithAddress::setParamWidgetValue
virtual void setParamWidgetValue(QWidget *paramWidget) const
The filter action shall set it's widget's contents from it's parameter.
Definition: filteractionwithaddress.cpp:46
MailCommon::FilterActionForward::newAction
static FilterAction * newAction()
Definition: filteractionforward.cpp:41
MailCommon::FilterActionForward::argsFromStringInteractive
bool argsFromStringInteractive(const QString &argsStr, const QString &filterName)
Read extra arguments from given string.
Definition: filteractionforward.cpp:200
MailCommon::FilterActionWithAddress
Abstract base class for filter actions with a mail address as parameter.
Definition: filteractionwithaddress.h:42
MailCommon::ItemContext
A helper class for the filtering process.
Definition: itemcontext.h:39
MailCommon::FilterActionForward::applyParamWidgetValue
void applyParamWidgetValue(QWidget *paramWidget)
The filter action shall set it's parameter from the widget's contents.
Definition: filteractionforward.cpp:132
mailutil.h
MailCommon::FilterActionWithString::argsAsString
virtual QString argsAsString() const
Return extra arguments as string.
Definition: filteractionwithstring.cpp:70
MailCommon::FilterActionWithString::argsFromString
virtual void argsFromString(const QString &argsStr)
Read extra arguments from given string.
Definition: filteractionwithstring.cpp:65
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