22 #include "pimcommon/widgets/minimumcombobox.h"
24 #include "libkdepim/job/addcontactjob.h"
26 #include <KDE/Akonadi/CollectionComboBox>
27 #include <KDE/KABC/Addressee>
28 #include <KDE/KLineEdit>
29 #include <KDE/KLocale>
30 #include <KDE/KPIMUtils/Email>
32 #include <QGridLayout>
35 using namespace MailCommon;
44 mFromStr( i18nc(
"Email sender",
"From" ) ),
45 mToStr( i18nc(
"Email recipient",
"To" ) ),
46 mCCStr( i18n(
"CC" ) ),
47 mBCCStr( i18n(
"BCC" ) ),
48 mHeaderType( FromHeader ),
50 mCategory( i18n(
"KMail Filter" ) )
61 const KMime::Message::Ptr msg = context.
item().payload<KMime::Message::Ptr>();
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;
71 const QStringList emails = KPIMUtils::splitAddressList( headerLine );
73 foreach (
const QString& singleEmail, emails ) {
75 KABC::Addressee::parseEmailAddress( singleEmail, name, email );
77 KABC::Addressee contact;
78 contact.setNameFromString( name );
79 contact.insertEmail( email,
true );
80 if ( !mCategory.isEmpty() )
81 contact.insertCategory( mCategory );
83 KPIM::AddContactJob *job =
new KPIM::AddContactJob( contact, Akonadi::Collection( mCollectionId ) );
84 job->showMessageBox(
false);
100 QGridLayout *layout =
new QGridLayout( widget );
102 PimCommon::MinimumComboBox *headerCombo =
new PimCommon::MinimumComboBox( widget );
103 headerCombo->setObjectName( QLatin1String(
"HeaderComboBox") );
104 layout->addWidget( headerCombo, 0, 0, 2, 1, Qt::AlignVCenter );
106 QLabel *
label =
new QLabel( i18n(
"with category" ), widget );
107 layout->addWidget( label, 0, 1 );
109 KLineEdit *categoryEdit =
new KLineEdit( widget );
110 categoryEdit->setObjectName( QLatin1String(
"CategoryEdit") );
111 categoryEdit->setTrapReturnKey(
true);
112 layout->addWidget( categoryEdit, 0, 2 );
114 label =
new QLabel( i18n(
"in address book" ), widget );
115 layout->addWidget( label, 1, 1 );
117 Akonadi::CollectionComboBox *collectionComboBox =
new Akonadi::CollectionComboBox( widget );
118 collectionComboBox->setMimeTypeFilter( QStringList() << KABC::Addressee::mimeType() );
119 collectionComboBox->setAccessRightsFilter( Akonadi::Collection::CanCreateItem );
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 );
126 connect( categoryEdit, SIGNAL(textChanged(QString)),
128 connect( headerCombo, SIGNAL(currentIndexChanged(
int)),
130 connect( collectionComboBox, SIGNAL(activated(
int)),
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 );
148 headerCombo->setCurrentIndex( headerCombo->findData( mHeaderType ) );
150 KLineEdit *categoryEdit = paramWidget->findChild<KLineEdit*>( QLatin1String(
"CategoryEdit") );
151 Q_ASSERT( categoryEdit );
152 categoryEdit->setText( mCategory );
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 );
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() );
166 const KLineEdit *categoryEdit = paramWidget->findChild<KLineEdit*>( QLatin1String(
"CategoryEdit") );
167 Q_ASSERT( categoryEdit );
168 mCategory = categoryEdit->text();
170 const Akonadi::CollectionComboBox *collectionComboBox = paramWidget->findChild<Akonadi::CollectionComboBox*>( QLatin1String(
"AddressBookComboBox") );
171 Q_ASSERT( collectionComboBox );
172 const Akonadi::Collection collection = collectionComboBox->currentCollection();
176 if ( collection.isValid() ) {
177 mCollectionId = collection.id();
178 connect( collectionComboBox, SIGNAL(currentIndexChanged(
int)),
181 const QVariant value = collectionComboBox->property(
"collectionId" );
182 if ( value.isValid() )
183 mCollectionId = value.toLongLong();
189 PimCommon::MinimumComboBox *headerCombo = paramWidget->findChild<PimCommon::MinimumComboBox*>( QLatin1String(
"HeaderComboBox") );
190 Q_ASSERT( headerCombo );
191 headerCombo->setCurrentIndex( 0 );
193 KLineEdit *categoryEdit = paramWidget->findChild<KLineEdit*>( QLatin1String(
"CategoryEdit") );
194 Q_ASSERT( categoryEdit );
195 categoryEdit->setText( mCategory );
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;
209 result += QLatin1Char(
'\t' );
210 result += QString::number( mCollectionId );
211 result += QLatin1Char(
'\t' );
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;
231 if ( parts.count() >= 2 )
232 mCollectionId = parts[ 1 ].toLongLong();
234 if ( parts.count() < 3 )
237 mCategory = parts[ 2 ];
240 #include "filteractionaddtoaddressbook.moc"
Go on with applying filter actions.
Abstract base class for filter actions with a fixed set of string parameters.
void applyParamWidgetValue(QWidget *paramWidget)
The filter action shall set it's parameter from the widget's contents.
Abstract base class for mail filter actions.
QWidget * createParamWidget(QWidget *parent) const
Creates a widget for setting the filter action parameter.
QString label() const
Returns i18n'd label, ie.
QString name() const
Returns identifier name, ie.
SearchRule::RequiredPart requiredPart() const
Returns the required part from the item that is needed for the action to operate. ...
QString argsAsString() const
Return extra arguments as string.
FilterActionAddToAddressBook(QObject *parent=0)
ReturnCode process(ItemContext &context, bool applyOnOutbound) const
Execute action on given message (inside the item context).
ReturnCode
Describes the possible return codes of filter processing:
Akonadi::Item & item()
Returns the item of the context.
static FilterAction * newAction()
void filterActionModified()
Called to notify that the current FilterAction has had some value modification.
void argsFromString(const QString &argsStr)
Read extra arguments from given string.
void clearParamWidget(QWidget *paramWidget) const
The filter action shall clear it's parameter widget's contents.
void setParamWidgetValue(QWidget *paramWidget) const
The filter action shall set it's widget's contents from it's parameter.
A helper class for the filtering process.
bool isEmpty() const
Determines whether this action is valid.