File: filteractionforward.cpp

package info (click to toggle)
kdepim 4%3A4.14.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 62,764 kB
  • sloc: cpp: 530,022; xml: 5,446; perl: 1,434; sh: 812; ansic: 433; php: 44; makefile: 22
file content (242 lines) | stat: -rw-r--r-- 9,511 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * Copyright (c) 1996-1998 Stefan Taferner <taferner@kde.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "filteractionforward.h"

#include "kernel/mailkernel.h"
#include "util/mailutil.h"
#include "dialog/filteractionmissingargumentdialog.h"

#include <pimcommon/widgets/minimumcombobox.h>
#include <messagecomposer/helper/messagefactory.h>
#include <messagecomposer/sender/messagesender.h>
#include <messagecore/widgets/emailaddressrequester.h>
#include <messagecore/utils/stringutil.h>
#include <templateparser/customtemplates.h>
#include <templateparser/customtemplates_kfg.h>

#include <KDE/KLocale>
#include <KDE/KLineEdit>

#include <QHBoxLayout>

using namespace MailCommon;

FilterAction *FilterActionForward::newAction()
{
    return new FilterActionForward;
}

FilterActionForward::FilterActionForward( QObject *parent )
    : FilterActionWithAddress(QLatin1String( "forward"), i18nc( "Forward directly not with a command", "Forward To" ), parent )
{
}

FilterAction::ReturnCode FilterActionForward::process(ItemContext &context , bool) const
{
    if ( mParameter.isEmpty() )
        return ErrorButGoOn;

    const KMime::Message::Ptr msg = context.item().payload<KMime::Message::Ptr>();
    // avoid endless loops when this action is used in a filter
    // which applies to sent messages
    if ( MessageCore::StringUtil::addressIsInAddressList( mParameter,
                                                          QStringList( msg->to()->asUnicodeString() ) ) ) {
        kWarning() << "Attempt to forward to receipient of original message, ignoring.";
        return ErrorButGoOn;
    }

    MessageComposer::MessageFactory factory( msg, context.item().id() );
    factory.setIdentityManager( KernelIf->identityManager() );
    factory.setFolderIdentity( Util::folderIdentity( context.item() ) );
    factory.setTemplate( mTemplate );

    KMime::Message::Ptr fwdMsg = factory.createForward();
    fwdMsg->to()->fromUnicodeString( fwdMsg->to()->asUnicodeString() + QLatin1Char( ',' ) + mParameter, "utf-8" );
    if ( !KernelIf->msgSender()->send( fwdMsg, MessageComposer::MessageSender::SendDefault ) ) {
        kWarning() << "FilterAction: could not forward message (sending failed)";
        return ErrorButGoOn; // error: couldn't send
    } else
        sendMDN( context.item(), KMime::MDN::Dispatched );

    // (the msgSender takes ownership of the message, so don't delete it here)
    return GoOn;
}

SearchRule::RequiredPart FilterActionForward::requiredPart() const
{
    return SearchRule::CompleteMessage;
}

QWidget* FilterActionForward::createParamWidget( QWidget *parent ) const
{
    QWidget *addressAndTemplate = new QWidget( parent );
    QHBoxLayout *layout = new QHBoxLayout( addressAndTemplate );
    layout->setMargin( 0 );

    QWidget *addressEdit = FilterActionWithAddress::createParamWidget( addressAndTemplate );
    addressEdit->setObjectName( QLatin1String("addressEdit") );
    layout->addWidget( addressEdit );

    MessageCore::EmailAddressRequester *addressRequester = qobject_cast<MessageCore::EmailAddressRequester*>( addressEdit );
    Q_ASSERT( addressRequester );
    KLineEdit *lineEdit = addressRequester->lineEdit();
    lineEdit->setClearButtonShown(true);
    lineEdit->setTrapReturnKey(true);
    lineEdit->setToolTip( i18n( "The addressee to whom the message will be forwarded." ) );
    lineEdit->setWhatsThis( i18n( "The filter will forward the message to the addressee entered here." ) );

    PimCommon::MinimumComboBox *templateCombo = new PimCommon::MinimumComboBox( addressAndTemplate );
    templateCombo->setObjectName( QLatin1String("templateCombo") );
    layout->addWidget( templateCombo );

    templateCombo->addItem( i18n( "Default Template" ) );

    const QStringList templateNames = SettingsIf->customTemplates();
    foreach( const QString &templateName, templateNames ) {
        TemplateParser::CTemplates templat( templateName );
        if ( templat.type() == TemplateParser::CustomTemplates::TForward ||
             templat.type() == TemplateParser::CustomTemplates::TUniversal ) {
            templateCombo->addItem( templateName );
        }
    }

    templateCombo->setEnabled( templateCombo->count() > 1 );
    templateCombo->setToolTip( i18n( "The template used when forwarding" ) );
    templateCombo->setWhatsThis( i18n( "Set the forwarding template that will be used with this filter." ) );

    connect( templateCombo, SIGNAL(currentIndexChanged(int)),
             this, SIGNAL(filterActionModified()) );
    connect( addressRequester, SIGNAL(textChanged()),
             this, SIGNAL(filterActionModified()) );

    return addressAndTemplate;
}

void FilterActionForward::applyParamWidgetValue( QWidget *paramWidget )
{
    QWidget *addressEdit = paramWidget->findChild<QWidget*>( QLatin1String("addressEdit") );
    Q_ASSERT( addressEdit );
    FilterActionWithAddress::applyParamWidgetValue( addressEdit );

    const PimCommon::MinimumComboBox *templateCombo = paramWidget->findChild<PimCommon::MinimumComboBox*>( QLatin1String("templateCombo") );
    Q_ASSERT( templateCombo );

    if ( templateCombo->currentIndex() == 0 ) {
        // Default template, so don't use a custom one
        mTemplate.clear();
    } else {
        mTemplate = templateCombo->currentText();
    }
}

void FilterActionForward::setParamWidgetValue( QWidget *paramWidget ) const
{
    QWidget *addressEdit = paramWidget->findChild<QWidget*>( QLatin1String("addressEdit") );
    Q_ASSERT( addressEdit );
    FilterActionWithAddress::setParamWidgetValue( addressEdit );

    PimCommon::MinimumComboBox *templateCombo = paramWidget->findChild<PimCommon::MinimumComboBox*>( QLatin1String("templateCombo") );
    Q_ASSERT( templateCombo );

    if ( mTemplate.isEmpty() ) {
        templateCombo->setCurrentIndex( 0 );
    } else {
        int templateIndex = templateCombo->findText( mTemplate );
        if ( templateIndex != -1 ) {
            templateCombo->setCurrentIndex( templateIndex );
        } else {
            mTemplate.clear();
        }
    }
}

void FilterActionForward::clearParamWidget( QWidget *paramWidget ) const
{
    QWidget *addressEdit = paramWidget->findChild<QWidget*>( QLatin1String("addressEdit") );
    Q_ASSERT( addressEdit );
    FilterActionWithAddress::clearParamWidget( addressEdit );

    PimCommon::MinimumComboBox *templateCombo = paramWidget->findChild<PimCommon::MinimumComboBox*>( QLatin1String("templateCombo") );
    Q_ASSERT( templateCombo );

    templateCombo->setCurrentIndex( 0 );
}

// We simply place a "@$$@" between the two parameters. The template is the last
// parameter in the string, for compatibility reasons.
static const QString forwardFilterArgsSeperator = QLatin1String("@$$@");

void FilterActionForward::argsFromString( const QString &argsStr )
{
    const int seperatorPos = argsStr.indexOf( forwardFilterArgsSeperator );

    if ( seperatorPos == - 1 ) {
        // Old config, assume that the whole string is the addressee
        FilterActionWithAddress::argsFromString( argsStr );
    } else {
        const QString addressee = argsStr.left( seperatorPos );
        mTemplate = argsStr.mid( seperatorPos + forwardFilterArgsSeperator.length() );
        FilterActionWithAddress::argsFromString( addressee );
    }
}

bool FilterActionForward::argsFromStringInteractive( const QString &argsStr, const QString& filterName )
{
    bool needUpdate = false;
    argsFromString( argsStr );
    if ( !mTemplate.isEmpty() ) {
        const QStringList templateNames = SettingsIf->customTemplates();
        QStringList currentTemplateList;
        currentTemplateList << i18n( "Default Template" );
        foreach( const QString &templateName, templateNames ) {
            TemplateParser::CTemplates templat( templateName );
            if ( templat.type() == TemplateParser::CustomTemplates::TForward ||
                 templat.type() == TemplateParser::CustomTemplates::TUniversal ) {
                if ( templateName == mTemplate ) {
                    return false;
                }
                currentTemplateList << templateName;
            }
        }
        QPointer<FilterActionMissingTemplateDialog> dlg = new FilterActionMissingTemplateDialog( currentTemplateList, filterName );
        if ( dlg->exec() ) {
            mTemplate = dlg->selectedTemplate();
            needUpdate = true;
        }
        delete dlg;
    }
    return needUpdate;
}


QString FilterActionForward::argsAsString() const
{
    return FilterActionWithAddress::argsAsString() + forwardFilterArgsSeperator + mTemplate;
}

QString FilterActionForward::displayString() const
{
    if ( mTemplate.isEmpty() )
        return i18n( "Forward to %1 with default template", mParameter );
    else
        return i18n( "Forward to %1 with template %2", mParameter, mTemplate );
}