kmcomposewin.cpp 116 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * This file is part of KMail.
 * Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com>
 *
 * Based on KMail code by:
 * Copyright (c) 1997 Markus Wuebben <markus.wuebben@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.
 */
22
#include "kmcomposewin.h"
23 24

// KMail includes
25
#include "addressvalidationjob.h"
26
#include "attachmentcontroller.h"
27
#include "messagecomposer/attachmentmodel.h"
28
#include "attachmentview.h"
29
#include "codecaction.h"
30
#include <messagecomposer/emailaddressresolvejob.h>
31 32 33
#include "kleo_util.h"
#include "kmcommands.h"
#include "kmcomposereditor.h"
34 35
#include "kmkernel.h"
#include "globalsettings.h"
36
#include "kmmainwin.h"
37
#include "kmmainwidget.h"
38
#include "kmreadermainwin.h"
39
//#include "mailcomposeradaptor.h" // TODO port all D-Bus stuff...
40
#include "messageviewer/stl_util.h"
41
#include "messageviewer/util.h"
Thomas McGuire's avatar
Thomas McGuire committed
42
#include "messagecore/stringutil.h"
43
#include "messagecore/attachmentcollector.h"
44
#include "util.h"
Thomas McGuire's avatar
Thomas McGuire committed
45
#include "snippetwidget.h"
46
#include <messagecomposer/keyresolver.h>
Laurent Montel's avatar
Laurent Montel committed
47
#include "templatesconfiguration_kfg.h"
Laurent Montel's avatar
Laurent Montel committed
48
#include "foldercollectionmonitor.h"
49

Thomas McGuire's avatar
Thomas McGuire committed
50 51 52 53 54 55 56 57
// KDEPIM includes
#include <libkpgp/kpgpblock.h>
#include <libkleo/ui/progressdialog.h>
#include <libkleo/ui/keyselectiondialog.h>
#include "kleo/cryptobackendfactory.h"
#include "kleo/exportjob.h"
#include "kleo/specialjob.h"
#include <messageviewer/objecttreeemptysource.h>
58
#include <messageviewer/kcursorsaver.h>
59 60
#include <messageviewer/objecttreeparser.h>
#include <messageviewer/nodehelper.h>
Thomas McGuire's avatar
Thomas McGuire committed
61
#include "messageviewer/chiasmuskeyselector.h"
62
#include <messageviewer/globalsettings.h>
Thomas McGuire's avatar
Thomas McGuire committed
63 64 65 66
#include <messagecomposer/composer.h>
#include <messagecomposer/globalpart.h>
#include <messagecomposer/infopart.h>
#include <messagecomposer/textpart.h>
67
#include <messagecomposer/messagehelper.h>
Leo Franchi's avatar
Leo Franchi committed
68
#include <messagecomposersettings.h>
69
#include <messagecomposer/messagehelper.h>
70
#include <messagecomposer/signaturecontroller.h>
Thomas McGuire's avatar
Thomas McGuire committed
71
#include <messagecomposer/inserttextfilejob.h>
72
#include <messagecomposer/composerlineedit.h>
73
#include <messagecomposer/recipientseditor.h>
Thomas McGuire's avatar
Thomas McGuire committed
74
#include <messagecore/attachmentpart.h>
75
#include "messagecore/globalsettings.h"
76 77 78
#include <templateparser/templateparser.h>
#include <templatesconfiguration.h>
#include "messagecore/nodehelper.h"
79
#include <akonadi/kmime/messagestatus.h>
80
#include "messagecore/messagehelpers.h"
Thomas McGuire's avatar
Thomas McGuire committed
81 82 83 84 85 86 87 88

// LIBKDEPIM includes
#include <libkdepim/recentaddresses.h>

// KDEPIMLIBS includes
#include <akonadi/collectioncombobox.h>
#include <akonadi/changerecorder.h>
#include <akonadi/itemcreatejob.h>
89
#include <akonadi/entitymimetypefiltermodel.h>
Thomas McGuire's avatar
Thomas McGuire committed
90 91 92 93 94 95 96 97 98 99 100 101
#include <akonadi/itemfetchjob.h>
#include <kpimutils/email.h>
#include <kpimidentities/identitymanager.h>
#include <kpimidentities/identitycombo.h>
#include <kpimidentities/identity.h>
#include <mailtransport/transportcombobox.h>
#include <mailtransport/transportmanager.h>
#include <mailtransport/transport.h>
#include <mailtransport/messagequeuejob.h>
#include <mailtransport/sentbehaviourattribute.h>
#include <kmime/kmime_codecs.h>
#include <kmime/kmime_message.h>
102

Thomas McGuire's avatar
Thomas McGuire committed
103 104 105 106
// KDELIBS includes
#include <kactioncollection.h>
#include <kactionmenu.h>
#include <kapplication.h>
107
#include <kcharsets.h>
108
#include <kdebug.h>
109
#include <libkdepim/kdescendantsproxymodel_p.h>
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
#include <kedittoolbar.h>
#include <kencodingfiledialog.h>
#include <kinputdialog.h>
#include <kmenu.h>
#include <kmimetypetrader.h>
#include <kmessagebox.h>
#include <kpushbutton.h>
#include <krecentfilesaction.h>
#include <krun.h>
#include <ksavefile.h>
#include <kshortcutsdialog.h>
#include <kstandarddirs.h>
#include <kstandardshortcut.h>
#include <kstatusbar.h>
#include <ktempdir.h>
#include <ktoggleaction.h>
#include <ktoolbar.h>
#include <ktoolinvocation.h>
#include <kwindowsystem.h>
#include <kzip.h>
#include <sonnet/dictionarycombobox.h>
#include <kencodingprober.h>
#include <kio/jobuidelegate.h>
#include <kio/scheduler.h>

// Qt includes
#include <QClipboard>
#include <QEvent>
#include <QSplitter>
139 140
#include <QUuid>
#include <QDir>
141 142 143 144 145 146

// System includes
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <memory>
Thomas McGuire's avatar
Thomas McGuire committed
147
#include <boost/shared_ptr.hpp>
148 149

// MOC
150
#include "kmcomposewin.moc"
151

Thomas McGuire's avatar
Thomas McGuire committed
152 153 154 155
using Sonnet::DictionaryComboBox;
using MailTransport::TransportManager;
using MailTransport::Transport;
using KPIM::RecentAddresses;
156

157
KMail::Composer *KMail::makeComposer( const KMime::Message::Ptr &msg, Composer::TemplateContext context,
158 159 160
                                      uint identity, const QString & textSelection,
                                      const QString & customTemplate ) {
  return KMComposeWin::create( msg, context, identity, textSelection, customTemplate );
161 162
}

163
KMail::Composer *KMComposeWin::create( const KMime::Message::Ptr &msg, Composer::TemplateContext context,
164 165 166
                                       uint identity, const QString & textSelection,
                                       const QString & customTemplate ) {
  return new KMComposeWin( msg, context, identity, textSelection, customTemplate );
167 168 169 170 171
}

int KMComposeWin::s_composerNumber = 0;

//-----------------------------------------------------------------------------
172
KMComposeWin::KMComposeWin( const KMime::Message::Ptr &aMsg, Composer::TemplateContext context, uint id,
173
                            const QString & textSelection, const QString & customTemplate )
174 175
  : KMail::Composer( "kmail-composer#" ),
    mDone( false ),
176 177 178
    //mAtmModified( false ),
    mTextSelection( textSelection ),
    mCustomTemplate( customTemplate ),
179
    mSigningAndEncryptionExplicitlyDisabled( false ),
180
    mFolder( Akonadi::Collection( -1 ) ),
181 182
    mForceDisableHtml( false ),
    mId( id ),
183
    mContext( context ),
184 185 186 187 188 189 190 191 192 193
    mSignAction( 0 ), mEncryptAction( 0 ), mRequestMDNAction( 0 ),
    mUrgentAction( 0 ), mAllFieldsAction( 0 ), mFromAction( 0 ),
    mReplyToAction( 0 ), mSubjectAction( 0 ),
    mIdentityAction( 0 ), mTransportAction( 0 ), mFccAction( 0 ),
    mWordWrapAction( 0 ), mFixedFontAction( 0 ), mAutoSpellCheckingAction( 0 ),
    mDictionaryAction( 0 ), mSnippetAction( 0 ),
    mCodecAction( 0 ),
    mCryptoModuleAction( 0 ),
    mEncryptChiasmusAction( 0 ),
    mEncryptWithChiasmus( false ),
194
    mDummyComposer( 0 ),
195
    mPendingQueueJobs( 0 ),
196
    mPendingCreateItemJobs( 0 ),
197
    mLabelWidth( 0 ),
198
    mComposerBase( 0 ),
199
    mSignatureStateIndicator( 0 ), mEncryptionStateIndicator( 0 ),
200 201
    mPreventFccOverwrite( false ),
    mIgnoreStickyFields( false )
202
{
203 204 205

  mComposerBase = new Message::ComposerViewBase( this );
  mComposerBase->setIdentityManager( kmkernel->identityManager() );
206
  mComposerBase->setParentWidgetForGui( this );
207

208 209 210 211 212 213 214
  connect( mComposerBase, SIGNAL( disableHtml( Message::ComposerViewBase::Confirmation ) ),
           this, SLOT( disableHtml( Message::ComposerViewBase::Confirmation ) ) );

  connect( mComposerBase, SIGNAL( enableHtml() ),
           this, SLOT( enableHtml() ) );
  connect( mComposerBase, SIGNAL( failed( QString) ), this, SLOT( slotSendFailed( QString ) ) );
  connect( mComposerBase, SIGNAL( sentSuccessfully() ), this, SLOT( slotSendSuccessful() ) );
Leo Franchi's avatar
Leo Franchi committed
215
  connect( mComposerBase, SIGNAL( modified( bool ) ), this, SLOT( setModified( bool ) ) );
216

217 218
  //(void) new MailcomposerAdaptor( this );
  mdbusObjectPath = "/Composer_" + QString::number( ++s_composerNumber );
219
  //QDBusConnection::sessionBus().registerObject( mdbusObjectPath, this );
220

221 222 223
  Message::SignatureController* sigController = new Message::SignatureController( this );
  connect( sigController, SIGNAL(enableHtml()), SLOT(enableHtml()) );
  mComposerBase->setSignatureController( sigController );
224

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
  if ( kmkernel->xmlGuiInstance().isValid() ) {
    setComponentData( kmkernel->xmlGuiInstance() );
  }
  mMainWidget = new QWidget( this );
  // splitter between the headers area and the actual editor
  mHeadersToEditorSplitter = new QSplitter( Qt::Vertical, mMainWidget );
  mHeadersToEditorSplitter->setObjectName( "mHeadersToEditorSplitter" );
  mHeadersToEditorSplitter->setChildrenCollapsible( false );
  mHeadersArea = new QWidget( mHeadersToEditorSplitter );
  mHeadersArea->setSizePolicy( mHeadersToEditorSplitter->sizePolicy().horizontalPolicy(),
                               QSizePolicy::Expanding );
  mHeadersToEditorSplitter->addWidget( mHeadersArea );
  QList<int> defaultSizes;
  defaultSizes << 0;
  mHeadersToEditorSplitter->setSizes( defaultSizes );
  QVBoxLayout *v = new QVBoxLayout( mMainWidget );
241
  v->setMargin(0);
242
  v->addWidget( mHeadersToEditorSplitter );
243
  KPIMIdentities::IdentityCombo* identity = new KPIMIdentities::IdentityCombo( kmkernel->identityManager(),
244
                                                 mHeadersArea );
245 246
  identity->setToolTip( i18n( "Select an identity for this message" ) );
  mComposerBase->setIdentityCombo( identity );
247

248 249
  sigController->setIdentityCombo( identity );
  sigController->suspend(); // we have to do identity change tracking ourselves due to the template code
Thomas McGuire's avatar
Thomas McGuire committed
250

251
  mDictionaryCombo = new DictionaryComboBox( mHeadersArea );
Thomas McGuire's avatar
Thomas McGuire committed
252 253
  mDictionaryCombo->setToolTip( i18n( "Select the dictionary to use when spell-checking this message" ) );

Till Adam's avatar
Till Adam committed
254
  Akonadi::CollectionComboBox* fcc = new Akonadi::CollectionComboBox( kmkernel->collectionModel(), mHeadersArea );
255
  fcc->setMimeTypeFilter( QStringList()<<KMime::Message::mimeType() );
256 257 258 259 260 261
  fcc->setAccessRightsFilter( Akonadi::Collection::CanCreateItem );
  fcc->setToolTip( i18n( "Select the sent-mail folder where a copy of this message will be saved" ) );
  mComposerBase->setFccCombo( fcc );
  MailTransport::TransportComboBox* transport = new MailTransport::TransportComboBox( mHeadersArea );
  transport->setToolTip( i18n( "Select the outgoing account to use for sending this message" ) );
  mComposerBase->setTransportCombo( transport );
262

263 264
  mEdtFrom = new MessageComposer::ComposerLineEdit( false, mHeadersArea );
  mEdtFrom->setObjectName( "fromLine" );
265
  mEdtFrom->setRecentAddressConfig( MessageComposer::MessageComposerSettings::self()->config() );
Thomas McGuire's avatar
Thomas McGuire committed
266
  mEdtFrom->setToolTip( i18n( "Set the \"From:\" email address for this message" ) );
267 268
  mEdtReplyTo = new MessageComposer::ComposerLineEdit( true, mHeadersArea );
  mEdtReplyTo->setObjectName( "replyToLine" );
269
  mEdtReplyTo->setRecentAddressConfig( MessageComposer::MessageComposerSettings::self()->config() );
Thomas McGuire's avatar
Thomas McGuire committed
270
  mEdtReplyTo->setToolTip( i18n( "Set the \"Reply-To:\" email address for this message" ) );
271 272 273
  connect( mEdtReplyTo, SIGNAL(completionModeChanged(KGlobalSettings::Completion)),
          SLOT(slotCompletionModeChanged(KGlobalSettings::Completion)) );

274
  MessageComposer::RecipientsEditor* recipientsEditor = new MessageComposer::RecipientsEditor( mHeadersArea );
275
  recipientsEditor->setRecentAddressConfig( MessageComposer::MessageComposerSettings::self()->config() );
276
  connect( recipientsEditor,
277 278
           SIGNAL( completionModeChanged( KGlobalSettings::Completion ) ),
           SLOT( slotCompletionModeChanged( KGlobalSettings::Completion ) ) );
279 280
  connect( recipientsEditor, SIGNAL(sizeHintChanged()), SLOT(recipientEditorSizeHintChanged()) );
  mComposerBase->setRecipientsEditor( recipientsEditor );
281

282 283
  mEdtSubject = new MessageComposer::ComposerLineEdit( false, mHeadersArea );
  mEdtSubject->setObjectName( "subjectLine" );
284
  mEdtSubject->setRecentAddressConfig(  MessageComposer::MessageComposerSettings::self()->config() );
Thomas McGuire's avatar
Thomas McGuire committed
285
  mEdtSubject->setToolTip( i18n( "Set a subject for this message" ) );
286 287 288 289 290 291 292 293 294
  mLblIdentity = new QLabel( i18n("&Identity:"), mHeadersArea );
  mDictionaryLabel = new QLabel( i18n("&Dictionary:"), mHeadersArea );
  mLblFcc = new QLabel( i18n("&Sent-Mail folder:"), mHeadersArea );
  mLblTransport = new QLabel( i18n("&Mail transport:"), mHeadersArea );
  mLblFrom = new QLabel( i18nc("sender address field", "&From:"), mHeadersArea );
  mLblReplyTo = new QLabel( i18n("&Reply to:"), mHeadersArea );
  mLblSubject = new QLabel( i18nc("@label:textbox Subject of email.", "S&ubject:"), mHeadersArea );
  QString sticky = i18nc("@option:check Sticky identity.", "Sticky");
  mBtnIdentity = new QCheckBox( sticky, mHeadersArea );
Thomas McGuire's avatar
Thomas McGuire committed
295
  mBtnIdentity->setToolTip( i18n( "Use the selected value as your identity for future messages" ) );
296
  mBtnFcc = new QCheckBox( sticky, mHeadersArea );
Thomas McGuire's avatar
Thomas McGuire committed
297
  mBtnFcc->setToolTip( i18n( "Use the selected value as your sent-mail folder for future messages" ) );
298
  mBtnTransport = new QCheckBox( sticky, mHeadersArea );
Thomas McGuire's avatar
Thomas McGuire committed
299
  mBtnTransport->setToolTip( i18n( "Use the selected value as your outgoing account for future messages" ) );
300 301
  mBtnDictionary = new QCheckBox( sticky, mHeadersArea );
  mBtnDictionary->setToolTip( i18n( "Use the selected value as your dictionary for future messages" ) );
Thomas McGuire's avatar
Thomas McGuire committed
302

303 304 305
  mShowHeaders = GlobalSettings::self()->headers();
  mDone = false;
  mGrid = 0;
306 307
  //mAtmListView = 0;
  //mAtmModified = false;
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
  mFixedFontAction = 0;
  // the attachment view is separated from the editor by a splitter
  mSplitter = new QSplitter( Qt::Vertical, mMainWidget );
  mSplitter->setObjectName( "mSplitter" );
  mSplitter->setChildrenCollapsible( false );
  mSnippetSplitter = new QSplitter( Qt::Horizontal, mSplitter );
  mSnippetSplitter->setObjectName( "mSnippetSplitter" );
  mSnippetSplitter->setChildrenCollapsible( false );
  mSplitter->addWidget( mSnippetSplitter );

  QWidget *editorAndCryptoStateIndicators = new QWidget( mSplitter );
  QVBoxLayout *vbox = new QVBoxLayout( editorAndCryptoStateIndicators );
  vbox->setMargin(0);
  QHBoxLayout *hbox = new QHBoxLayout();
  {
    hbox->setMargin(0);
    mSignatureStateIndicator = new QLabel( editorAndCryptoStateIndicators );
    mSignatureStateIndicator->setAlignment( Qt::AlignHCenter );
    hbox->addWidget( mSignatureStateIndicator );

    KConfigGroup reader( KMKernel::config(), "Reader" );

    // Get the colors for the label
    QPalette p( mSignatureStateIndicator->palette() );
    KColorScheme scheme( QPalette::Active, KColorScheme::View );
    QColor defaultSignedColor =  // pgp signed
        scheme.background( KColorScheme::PositiveBackground ).color();
    QColor defaultEncryptedColor( 0x00, 0x80, 0xFF ); // light blue // pgp encrypted
    QColor signedColor = defaultSignedColor;
    QColor encryptedColor = defaultEncryptedColor;
338
    if ( !MessageCore::GlobalSettings::self()->useDefaultColors() ) {
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
      signedColor = reader.readEntry( "PGPMessageOkKeyOk", defaultSignedColor );
      encryptedColor = reader.readEntry( "PGPMessageEncr", defaultEncryptedColor );
    }

    p.setColor( QPalette::Window, signedColor );
    mSignatureStateIndicator->setPalette( p );
    mSignatureStateIndicator->setAutoFillBackground( true );

    mEncryptionStateIndicator = new QLabel( editorAndCryptoStateIndicators );
    mEncryptionStateIndicator->setAlignment( Qt::AlignHCenter );
    hbox->addWidget( mEncryptionStateIndicator );
    p.setColor( QPalette::Window, encryptedColor);
    mEncryptionStateIndicator->setPalette( p );
    mEncryptionStateIndicator->setAutoFillBackground( true );
  }

355 356
  KMComposerEditor* editor = new KMComposerEditor( this, editorAndCryptoStateIndicators );
  connect( editor, SIGNAL(textChanged()),
Allen Winter's avatar
Allen Winter committed
357
           this, SLOT(slotEditorTextChanged()) );
358
  mComposerBase->setEditor( editor );
359
  vbox->addLayout( hbox );
360
  vbox->addWidget( editor );
361

362 363
  mSnippetSplitter->insertWidget( 0, editorAndCryptoStateIndicators );
  mSnippetSplitter->setOpaqueResize( true );
364
  sigController->setEditor( editor );
365 366

  mHeadersToEditorSplitter->addWidget( mSplitter );
367
  editor->setAcceptDrops( true );
368
  connect( mDictionaryCombo, SIGNAL( dictionaryChanged( const QString & ) ),
369 370
           editor, SLOT( setSpellCheckingLanguage( const QString & ) ) );
  connect( editor, SIGNAL( languageChanged(const QString &) ),
371
           this, SLOT( slotLanguageChanged(const QString&) ) );
372
  connect( editor, SIGNAL( spellCheckStatus(const QString &)),
373 374
           this, SLOT( slotSpellCheckingStatus(const QString &) ) );

375
  mSnippetWidget = new SnippetWidget( editor, actionCollection(), mSnippetSplitter );
376 377 378 379 380 381
  mSnippetWidget->setVisible( GlobalSettings::self()->showSnippetManager() );
  mSnippetSplitter->addWidget( mSnippetWidget );
  mSnippetSplitter->setCollapsible( 0, false );

  mSplitter->setOpaqueResize( true );

Laurent Montel's avatar
Laurent Montel committed
382 383 384
  mBtnIdentity->setWhatsThis( GlobalSettings::self()->stickyIdentityItem()->whatsThis() );
  mBtnFcc->setWhatsThis( GlobalSettings::self()->stickyFccItem()->whatsThis() );
  mBtnTransport->setWhatsThis( GlobalSettings::self()->stickyTransportItem()->whatsThis() );
385
  mBtnDictionary->setWhatsThis( GlobalSettings::self()->stickyDictionaryItem()->whatsThis() );
386 387 388 389 390 391 392

  setCaption( i18n("Composer") );
  setMinimumSize( 200, 200 );

  mBtnIdentity->setFocusPolicy( Qt::NoFocus );
  mBtnFcc->setFocusPolicy( Qt::NoFocus );
  mBtnTransport->setFocusPolicy( Qt::NoFocus );
393
  mBtnDictionary->setFocusPolicy( Qt::NoFocus );
394

395 396
  Message::AttachmentModel* attachmentModel = new Message::AttachmentModel( this );
  mAttachmentView = new KMail::AttachmentView( attachmentModel, mSplitter );
397
  mAttachmentView->hideIfEmpty();
398
  KMail::AttachmentController* attachmentController = new KMail::AttachmentController( attachmentModel, mAttachmentView, this );
399

400 401
  mComposerBase->setAttachmentModel( attachmentModel );
  mComposerBase->setAttachmentController( attachmentController );
402

403 404 405 406 407 408 409 410 411 412 413
  readConfig();
  setupStatusBar();
  setupActions();
  setupEditor();
  rethinkFields();
  slotUpdateSignatureAndEncrypionStateIndicators();

  applyMainWindowSettings( KMKernel::config()->group( "Composer") );

  connect( mEdtSubject, SIGNAL(textChanged(const QString&)),
           SLOT(slotUpdWinTitle(const QString&)) );
414
  connect( identity, SIGNAL(identityChanged(uint)),
415 416 417 418 419 420
           SLOT(slotIdentityChanged(uint)) );
  connect( kmkernel->identityManager(), SIGNAL(changed(uint)),
           SLOT(slotIdentityChanged(uint)) );

  connect( mEdtFrom, SIGNAL(completionModeChanged(KGlobalSettings::Completion)),
           SLOT(slotCompletionModeChanged(KGlobalSettings::Completion)) );
421
  connect( kmkernel->monitor(), SIGNAL(collectionRemoved(const Akonadi::Collection&)),
Laurent Montel's avatar
Laurent Montel committed
422
           SLOT(slotFolderRemoved(const Akonadi::Collection&)) );
423 424 425 426 427 428 429 430 431 432
  connect( kmkernel, SIGNAL( configChanged() ),
           this, SLOT( slotConfigChanged() ) );

  mMainWidget->resize( 480, 510 );
  setCentralWidget( mMainWidget );

  if ( GlobalSettings::self()->useHtmlMarkup() )
    enableHtml();

  if ( GlobalSettings::self()->useExternalEditor() ) {
433 434
    editor->setUseExternalEditor( true );
    editor->setExternalEditorPath( GlobalSettings::self()->externalEditor() );
435 436 437 438 439
  }

  if ( aMsg ) {
    setMsg( aMsg );
  }
440

441 442
  mComposerBase->identityCombo()->setFocus();
  editor->updateActionStates(); // set toolbar buttons to correct values
443 444

  mDone = true;
445

446
  mDummyComposer = new Message::Composer( this );
447
  mDummyComposer->globalPart()->setParentWidgetForGui( this );
448 449 450 451 452 453 454
}

//-----------------------------------------------------------------------------
KMComposeWin::~KMComposeWin()
{
  writeConfig();

455 456 457 458 459 460 461
  // When we have a collection set, store the message back to that collection.
  // Note that when we save the message or sent it, mFolder is set back to 0.
  // So this for example kicks in when opening a draft and then closing the window.
  if ( mFolder.isValid() && mMsg ) {
    Akonadi::Item item;
    item.setPayload( mMsg );
    item.setMimeType( "message/rfc822" );
462 463
    MessageStatus status;
    status.setRead();
464
    item.setFlags( status.statusFlags() );
465 466 467
    new Akonadi::ItemCreateJob( item, mFolder );
    // FIXME: listen to the result signal. The whole thing needs to be moved
    //        out of the destructor for this
468 469 470 471 472
  }

  foreach ( KTempDir *const dir, mTempDirs ) {
    delete dir;
  }
Leo Franchi's avatar
Leo Franchi committed
473
  delete mComposerBase;
474 475 476 477 478 479 480 481
}


QString KMComposeWin::dbusObjectPath() const
{
  return mdbusObjectPath;
}

Allen Winter's avatar
Allen Winter committed
482 483
void KMComposeWin::slotEditorTextChanged()
{
484
  const bool textIsNotEmpty = !mComposerBase->editor()->document()->isEmpty();
Allen Winter's avatar
Allen Winter committed
485 486 487
  mFindText->setEnabled( textIsNotEmpty );
  mFindNextText->setEnabled( textIsNotEmpty );
  mReplaceText->setEnabled( textIsNotEmpty );
488
  mSelectAll->setEnabled( textIsNotEmpty );
Allen Winter's avatar
Allen Winter committed
489 490
}

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
//-----------------------------------------------------------------------------
void KMComposeWin::send( int how )
{
  switch ( how ) {
  case 1:
    slotSendNow();
    break;
  default:
  case 0:
    // TODO: find out, what the default send method is and send it this way
  case 2:
    slotSendLater();
    break;
  }
}

//-----------------------------------------------------------------------------
void KMComposeWin::addAttachmentsAndSend( const KUrl::List &urls, const QString &comment, int how )
{
510
  kDebug() << "addAttachment and sending!";
Laurent Montel's avatar
Laurent Montel committed
511 512
  const int nbUrl = urls.count();
  for( int i =0; i < nbUrl; ++i ) {
513 514
    addAttachment( urls[i], comment );
  }
Laurent Montel's avatar
Laurent Montel committed
515

516
  send( how );
517 518 519 520 521 522
}

//-----------------------------------------------------------------------------
void KMComposeWin::addAttachment( const KUrl &url, const QString &comment )
{
  Q_UNUSED( comment );
523
  mComposerBase->addAttachment( url, comment );
524 525
}

526 527

void KMComposeWin::addAttachment( const QString& name,
528
                                  KMime::Headers::contentEncoding cte,
529 530 531
                                  const QString& charset,
                                  const QByteArray& data,
                                  const QByteArray& mimeType )
532
{
533
  mComposerBase->addAttachment( name, name, charset, data, mimeType );
534 535 536 537 538 539 540 541 542 543 544 545 546
}

//-----------------------------------------------------------------------------
void KMComposeWin::readConfig( bool reload /* = false */ )
{
  mBtnIdentity->setChecked( GlobalSettings::self()->stickyIdentity() );
  if (mBtnIdentity->isChecked()) {
    mId = ( GlobalSettings::self()->previousIdentity() != 0 ) ?
      GlobalSettings::self()->previousIdentity() : mId;
  }
  mBtnFcc->setChecked( GlobalSettings::self()->stickyFcc() );
  mBtnTransport->setChecked( GlobalSettings::self()->stickyTransport() );
  QString currentTransport = GlobalSettings::self()->currentTransport();
547
  mBtnDictionary->setChecked( GlobalSettings::self()->stickyDictionary() );
548 549

  mEdtFrom->setCompletionMode( (KGlobalSettings::Completion)GlobalSettings::self()->completionMode() );
550
  mComposerBase->recipientsEditor()->setCompletionMode( (KGlobalSettings::Completion)GlobalSettings::self()->completionMode() );
551 552
  mEdtReplyTo->setCompletionMode( (KGlobalSettings::Completion)GlobalSettings::self()->completionMode() );

553
  if ( MessageCore::GlobalSettings::self()->useDefaultFonts() ) {
554 555 556 557
    mBodyFont = KGlobalSettings::generalFont();
    mFixedFont = KGlobalSettings::fixedFont();
  } else {
    mBodyFont = GlobalSettings::self()->composerFont();
558
    mFixedFont = MessageViewer::GlobalSettings::self()->fixedFont();
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
  }

  slotUpdateFont();
  mEdtFrom->setFont( mBodyFont );
  mEdtReplyTo->setFont( mBodyFont );
  mEdtSubject->setFont( mBodyFont );

  if ( !reload ) {
    QSize siz = GlobalSettings::self()->composerSize();
    if ( siz.width() < 200 ) {
      siz.setWidth( 200 );
    }
    if ( siz.height() < 200 ) {
      siz.setHeight( 200 );
    }
    resize( siz );

    if ( !GlobalSettings::self()->snippetSplitterPosition().isEmpty() ) {
      mSnippetSplitter->setSizes( GlobalSettings::self()->snippetSplitterPosition() );
    } else {
      QList<int> defaults;
      defaults << (int)(width() * 0.8) << (int)(width() * 0.2);
      mSnippetSplitter->setSizes( defaults );
    }
  }

585 586
  mComposerBase->identityCombo()->setCurrentIdentity( mId );
  kDebug() << mComposerBase->identityCombo()->currentIdentityName();
587
  const KPIMIdentities::Identity & ident =
588
    kmkernel->identityManager()->identityForUoid( mId );
589 590 591 592 593

  if ( mBtnTransport->isChecked() && !currentTransport.isEmpty() ) {
    Transport *transport =
        TransportManager::self()->transportByName( currentTransport );
    if ( transport )
594
      mComposerBase->transportComboBox()->setCurrentTransport( transport->id() );
595 596
  }

Leo Franchi's avatar
Leo Franchi committed
597
  mComposerBase->setAutoSaveInterval( GlobalSettings::self()->autosaveInterval() * 1000 * 60 );
598

Leo Franchi's avatar
Leo Franchi committed
599

600 601 602 603 604 605
  if ( mBtnDictionary->isChecked() ) {
    mDictionaryCombo->setCurrentByDictionaryName( GlobalSettings::self()->previousDictionary() );
  } else {
    mDictionaryCombo->setCurrentByDictionaryName( ident.dictionary() );
  }

606 607 608 609 610 611 612 613 614 615 616 617 618 619
  QString fccName = "";
  if ( mBtnFcc->isChecked() ) {
    fccName = GlobalSettings::self()->previousFcc();
  } else if ( !ident.fcc().isEmpty() ) {
    fccName = ident.fcc();
  }
  setFcc( fccName );
}

//-----------------------------------------------------------------------------
void KMComposeWin::writeConfig( void )
{
  GlobalSettings::self()->setHeaders( mShowHeaders );
  GlobalSettings::self()->setStickyFcc( mBtnFcc->isChecked() );
620
  if ( !mIgnoreStickyFields ) {
621
    GlobalSettings::self()->setCurrentTransport( mComposerBase->transportComboBox()->currentText() );
622
    GlobalSettings::self()->setStickyTransport( mBtnTransport->isChecked() );
623
    GlobalSettings::self()->setStickyDictionary( mBtnDictionary->isChecked() );
624
    GlobalSettings::self()->setStickyIdentity( mBtnIdentity->isChecked() );
625
    GlobalSettings::self()->setPreviousIdentity( mComposerBase->identityCombo()->currentIdentity() );
626
  }
627
  GlobalSettings::self()->setPreviousFcc( QString::number(mComposerBase->fccCombo()->currentCollection().id()) );
628
  GlobalSettings::self()->setPreviousDictionary( mDictionaryCombo->currentDictionaryName() );
629 630
  GlobalSettings::self()->setAutoSpellChecking(
                                               mAutoSpellCheckingAction->isChecked() );
Thomas McGuire's avatar
Thomas McGuire committed
631
  MessageViewer::GlobalSettings::self()->setUseFixedFont( mFixedFontAction->isChecked() );
632
  if ( !mForceDisableHtml )
633
    GlobalSettings::self()->setUseHtmlMarkup( mComposerBase->editor()->textMode() == KMeditor::Rich );
634 635 636 637 638 639 640 641
  GlobalSettings::self()->setComposerSize( size() );
  GlobalSettings::self()->setShowSnippetManager( mSnippetAction->isChecked() );

  saveMainWindowSettings( KMKernel::config()->group( "Composer" ) );
  if ( mSnippetAction->isChecked() )
    GlobalSettings::setSnippetSplitterPosition( mSnippetSplitter->sizes() );

  // make sure config changes are written to disk, cf. bug 127538
642
  KMKernel::self()->slotSyncConfig();
643 644
}

645 646
Message::Composer* KMComposeWin::createSimpleComposer()
{
647 648 649 650 651 652 653 654
  QList< QByteArray > charsets = mCodecAction->mimeCharsets();
  if( !mOriginalPreferredCharset.isEmpty() ) {
    charsets.insert( 0, mOriginalPreferredCharset );
  }
  mComposerBase->setFrom( from() );
  mComposerBase->setReplyTo( replyTo() );
  mComposerBase->setSubject( subject() );
  mComposerBase->setCharsets( charsets );
655
  return mComposerBase->createSimpleComposer();
656 657
}

658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
//-----------------------------------------------------------------------------
void KMComposeWin::slotView( void )
{
  if ( !mDone ) {
    return; // otherwise called from rethinkFields during the construction
            // which is not the intended behavior
  }
  int id;

  //This sucks awfully, but no, I cannot get an activated(int id) from
  // actionContainer()
  KToggleAction *act = ::qobject_cast<KToggleAction *>( sender() );
  if ( !act ) {
    return;
  }

  if ( act == mAllFieldsAction ) {
    id = 0;
  } else if ( act == mIdentityAction ) {
    id = HDR_IDENTITY;
  } else if ( act == mTransportAction ) {
    id = HDR_TRANSPORT;
  } else if ( act == mFromAction ) {
    id = HDR_FROM;
  } else if ( act == mReplyToAction ) {
    id = HDR_REPLY_TO;
  } else if ( act == mSubjectAction ) {
    id = HDR_SUBJECT;
  } else if ( act == mFccAction ) {
    id = HDR_FCC;
  } else if ( act == mDictionaryAction ) {
    id = HDR_DICTIONARY;
  } else {
    id = 0;
    kDebug() <<"Something is wrong (Oh, yeah?)";
    return;
  }

  // sanders There's a bug here this logic doesn't work if no
  // fields are shown and then show all fields is selected.
  // Instead of all fields being shown none are.
  if ( !act->isChecked() ) {
    // hide header
    if ( id > 0 ) {
      mShowHeaders = mShowHeaders & ~id;
    } else {
      mShowHeaders = abs( mShowHeaders );
    }
  } else {
    // show header
    if ( id > 0 ) {
      mShowHeaders |= id;
    } else {
      mShowHeaders = -abs( mShowHeaders );
    }
  }
  rethinkFields( true );
}

int KMComposeWin::calcColumnWidth( int which, long allShowing, int width ) const
{
  if ( ( allShowing & which ) == 0 ) {
    return width;
  }

  QLabel *w;
  if ( which == HDR_IDENTITY ) {
    w = mLblIdentity;
  } else if ( which == HDR_DICTIONARY ) {
    w = mDictionaryLabel;
  } else if ( which == HDR_FCC ) {
    w = mLblFcc;
  } else if ( which == HDR_TRANSPORT ) {
    w = mLblTransport;
  } else if ( which == HDR_FROM ) {
    w = mLblFrom;
  } else if ( which == HDR_REPLY_TO ) {
    w = mLblReplyTo;
  } else if ( which == HDR_SUBJECT ) {
    w = mLblSubject;
  } else {
    return width;
  }

742
  w->setBuddy( mComposerBase->editor() ); // set dummy so we don't calculate width of '&' for this label.
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
  w->adjustSize();
  w->show();
  return qMax( width, w->sizeHint().width() );
}

void KMComposeWin::rethinkFields( bool fromSlot )
{
  //This sucks even more but again no ids. sorry (sven)
  int mask, row, numRows;
  long showHeaders;

  if ( mShowHeaders < 0 ) {
    showHeaders = HDR_ALL;
  } else {
    showHeaders = mShowHeaders;
  }

  for ( mask=1, mNumHeaders=0; mask<=showHeaders; mask<<=1 ) {
    if ( ( showHeaders & mask ) != 0 ) {
      mNumHeaders++;
    }
  }

  numRows = mNumHeaders + 1;

  delete mGrid;
  mGrid = new QGridLayout( mHeadersArea );
  mGrid->setSpacing( KDialog::spacingHint() );
  mGrid->setMargin( KDialog::marginHint() / 2 );
  mGrid->setColumnStretch( 0, 1 );
  mGrid->setColumnStretch( 1, 100 );
  mGrid->setColumnStretch( 2, 1 );
  mGrid->setRowStretch( mNumHeaders + 1, 100 );

  row = 0;
  kDebug();

780
  mLabelWidth = mComposerBase->recipientsEditor()->setFirstColumnWidth( 0 );
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
  mLabelWidth = calcColumnWidth( HDR_IDENTITY, showHeaders, mLabelWidth );
  mLabelWidth = calcColumnWidth( HDR_DICTIONARY, showHeaders, mLabelWidth );
  mLabelWidth = calcColumnWidth( HDR_FCC, showHeaders, mLabelWidth );
  mLabelWidth = calcColumnWidth( HDR_TRANSPORT, showHeaders, mLabelWidth );
  mLabelWidth = calcColumnWidth( HDR_FROM, showHeaders, mLabelWidth );
  mLabelWidth = calcColumnWidth( HDR_REPLY_TO, showHeaders, mLabelWidth );
  mLabelWidth = calcColumnWidth( HDR_SUBJECT, showHeaders, mLabelWidth );

  if ( !fromSlot ) {
    mAllFieldsAction->setChecked( showHeaders == HDR_ALL );
  }

  if ( !fromSlot ) {
    mIdentityAction->setChecked( abs( mShowHeaders )&HDR_IDENTITY );
  }
796
  rethinkHeaderLine( showHeaders,HDR_IDENTITY, row, mLblIdentity, mComposerBase->identityCombo(),
797 798 799 800 801 802
                     mBtnIdentity );

  if ( !fromSlot ) {
    mDictionaryAction->setChecked( abs( mShowHeaders )&HDR_DICTIONARY );
  }
  rethinkHeaderLine( showHeaders,HDR_DICTIONARY, row, mDictionaryLabel,
803
                     mDictionaryCombo, mBtnDictionary );
804 805 806 807

  if ( !fromSlot ) {
    mFccAction->setChecked( abs( mShowHeaders )&HDR_FCC );
  }
808
  rethinkHeaderLine( showHeaders,HDR_FCC, row, mLblFcc, mComposerBase->fccCombo(), mBtnFcc );
809 810 811 812

  if ( !fromSlot ) {
    mTransportAction->setChecked( abs( mShowHeaders )&HDR_TRANSPORT );
  }
813
  rethinkHeaderLine( showHeaders,HDR_TRANSPORT, row, mLblTransport, mComposerBase->transportComboBox(),
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
                     mBtnTransport );

  if ( !fromSlot ) {
    mFromAction->setChecked( abs( mShowHeaders )&HDR_FROM );
  }
  rethinkHeaderLine( showHeaders,HDR_FROM, row, mLblFrom, mEdtFrom );

  QWidget *prevFocus = mEdtFrom;

  if ( !fromSlot ) {
    mReplyToAction->setChecked( abs( mShowHeaders )&HDR_REPLY_TO );
  }
  rethinkHeaderLine( showHeaders, HDR_REPLY_TO, row, mLblReplyTo, mEdtReplyTo );
  if ( showHeaders & HDR_REPLY_TO ) {
    prevFocus = connectFocusMoving( prevFocus, mEdtReplyTo );
  }

831
  mGrid->addWidget( mComposerBase->recipientsEditor(), row, 0, 1, 3 );
832 833
  ++row;
  if ( showHeaders & HDR_REPLY_TO ) {
834
    connect( mEdtReplyTo, SIGNAL( focusDown() ), mComposerBase->recipientsEditor(),
835
             SLOT( setFocusTop() ) );
836
    connect( mComposerBase->recipientsEditor(), SIGNAL( focusUp() ), mEdtReplyTo,
837 838
             SLOT( setFocus() ) );
  } else {
839
    connect( mEdtFrom, SIGNAL( focusDown() ), mComposerBase->recipientsEditor(),
840
             SLOT( setFocusTop() ) );
841
    connect( mComposerBase->recipientsEditor(), SIGNAL( focusUp() ), mEdtFrom,
842 843 844
             SLOT( setFocus() ) );
  }

845
  connect( mComposerBase->recipientsEditor(), SIGNAL( focusDown() ), mEdtSubject,
846
           SLOT( setFocus() ) );
847
  connect( mEdtSubject, SIGNAL( focusUp() ), mComposerBase->recipientsEditor(),
848 849
           SLOT( setFocusBottom() ) );

850
  prevFocus = mComposerBase->recipientsEditor();
851 852 853 854 855

  if ( !fromSlot ) {
    mSubjectAction->setChecked( abs( mShowHeaders )&HDR_SUBJECT );
  }
  rethinkHeaderLine(showHeaders,HDR_SUBJECT, row, mLblSubject, mEdtSubject );
856
  connectFocusMoving( mEdtSubject, mComposerBase->editor() );
857 858 859 860

  assert( row <= mNumHeaders + 1 );


861
#if 0
862 863 864 865 866
  if ( !mAtmList.isEmpty() ) {
    mAtmListView->show();
  } else {
    mAtmListView->hide();
  }
867
#endif
868 869 870 871 872 873 874 875 876 877 878 879

  mHeadersArea->setMaximumHeight( mHeadersArea->sizeHint().height() );

  mIdentityAction->setEnabled(!mAllFieldsAction->isChecked());
  mDictionaryAction->setEnabled( !mAllFieldsAction->isChecked() );
  mTransportAction->setEnabled(!mAllFieldsAction->isChecked());
  mFromAction->setEnabled(!mAllFieldsAction->isChecked());
  if ( mReplyToAction ) {
    mReplyToAction->setEnabled( !mAllFieldsAction->isChecked() );
  }
  mFccAction->setEnabled( !mAllFieldsAction->isChecked() );
  mSubjectAction->setEnabled( !mAllFieldsAction->isChecked() );
880
  mComposerBase->recipientsEditor()->setFirstColumnWidth( mLabelWidth );
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
}

QWidget *KMComposeWin::connectFocusMoving( QWidget *prev, QWidget *next )
{
  connect( prev, SIGNAL( focusDown() ), next, SLOT( setFocus() ) );
  connect( next, SIGNAL( focusUp() ), prev, SLOT( setFocus() ) );

  return next;
}

//-----------------------------------------------------------------------------
void KMComposeWin::rethinkHeaderLine( int aValue, int aMask, int &aRow,
                                      QLabel *aLbl, KLineEdit *aEdt,
                                      QPushButton *aBtn )
{
  if ( aValue & aMask ) {
    aLbl->setFixedWidth( mLabelWidth );
    aLbl->setBuddy( aEdt );
    mGrid->addWidget( aLbl, aRow, 0 );
    aEdt->show();

    if ( aBtn ) {
      mGrid->addWidget( aEdt, aRow, 1 );
      mGrid->addWidget( aBtn, aRow, 2 );
      aBtn->show();
    } else {
      mGrid->addWidget( aEdt, aRow, 1, 1, 2 );
    }
    aRow++;
  } else {
    aLbl->hide();
    aEdt->hide();
    if ( aBtn ) {
      aBtn->hide();
    }
  }
}

//-----------------------------------------------------------------------------
void KMComposeWin::rethinkHeaderLine( int aValue, int aMask, int &aRow,
                                      QLabel *aLbl, QComboBox *aCbx, // krazy:exclude=qclasses
                                      QCheckBox *aChk )
{
  if ( aValue & aMask ) {
    aLbl->setBuddy( aCbx );
    mGrid->addWidget( aLbl, aRow, 0 );

    mGrid->addWidget( aCbx, aRow, 1 );
    aCbx->show();
    if ( aChk ) {
      mGrid->addWidget( aChk, aRow, 2 );
      aChk->show();
    }
    aRow++;
  } else {
    aLbl->hide();
    aCbx->hide();
    if ( aChk ) {
      aChk->hide();
    }
  }
}

944 945 946
//-----------------------------------------------------------------------------
void KMComposeWin::applyTemplate( uint uoid )
{
947
  const KPIMIdentities::Identity &ident = kmkernel->identityManager()->identityForUoid( uoid );
948 949
  if ( ident.isNull() )
    return;
950 951 952
  KMime::Headers::Generic *header = new KMime::Headers::Generic( "X-KMail-Templates", mMsg.get(), ident.templates(), "utf-8" );
  mMsg->setHeader( header );

953
  TemplateParser::TemplateParser::Mode mode;
954 955
  switch ( mContext ) {
    case New:
956
      mode = TemplateParser::TemplateParser::NewMessage;
957 958
      break;
    case Reply:
959
      mode = TemplateParser::TemplateParser::Reply;
960 961
      break;
    case ReplyToAll:
962
      mode = TemplateParser::TemplateParser::ReplyAll;
963 964
      break;
    case Forward:
965
      mode = TemplateParser::TemplateParser::Forward;
966 967 968 969 970
      break;
    default:
      return;
  }

971 972
  if ( mode == TemplateParser::TemplateParser::NewMessage ) {
    TemplateParser::TemplateParser parser( mMsg, mode );
Tobias Koenig's avatar
Tobias Koenig committed
973 974 975
    parser.setSelection( mTextSelection );
    parser.setAllowDecryption( MessageViewer::GlobalSettings::self()->automaticDecrypt() );

976
    if ( !mCustomTemplate.isEmpty() )
977
      parser.process( mCustomTemplate, KMime::Message::Ptr() );
978
    else
979
      parser.processWithIdentity( uoid, KMime::Message::Ptr() );
Tobias Koenig's avatar
Tobias Koenig committed
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003

    return;
  }

  if ( mMsg->headerByType( "X-KMail-Link-Message" ) ) {
    Akonadi::Item::List items;
    foreach( const QString& serNumStr, mMsg->headerByType( "X-KMail-Link-Message" )->asUnicodeString().split( ',' ) )
      items << Akonadi::Item( serNumStr.toLongLong() );


    Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( items, this );
    job->fetchScope().fetchFullPayload( true );
    job->fetchScope().setAncestorRetrieval( Akonadi::ItemFetchScope::Parent );
    job->setProperty( "mode", (int)mode );
    job->setProperty( "uoid", uoid );
    connect( job, SIGNAL( result( KJob* ) ), SLOT( slotDelayedApplyTemplate( KJob* ) ) );
  }
}

void KMComposeWin::slotDelayedApplyTemplate( KJob *job )
{
  const Akonadi::ItemFetchJob *fetchJob = qobject_cast<Akonadi::ItemFetchJob*>( job );
  const Akonadi::Item::List items = fetchJob->items();

1004
  const TemplateParser::TemplateParser::Mode mode = static_cast<TemplateParser::TemplateParser::Mode>( fetchJob->property( "mode" ).toInt() );
Tobias Koenig's avatar
Tobias Koenig committed
1005 1006
  const uint uoid = fetchJob->property( "uoid" ).toUInt();

1007
  TemplateParser::TemplateParser parser( mMsg, mode );
Tobias Koenig's avatar
Tobias Koenig committed
1008 1009 1010 1011 1012
  parser.setSelection( mTextSelection );
  parser.setAllowDecryption( MessageViewer::GlobalSettings::self()->automaticDecrypt() );

  foreach ( const Akonadi::Item &item, items ) {
    if ( !mCustomTemplate.isEmpty() )
1013
      parser.process( mCustomTemplate, MessageCore::Util::message( item ) );
Tobias Koenig's avatar
Tobias Koenig committed
1014
    else
1015
      parser.processWithIdentity( uoid, MessageCore::Util::message( item ) );
1016
  }
Tobias Koenig's avatar
Tobias Koenig committed
1017