kmcomposewin.h 20.4 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 23 24 25 26 27

#ifndef __KMComposeWin
#define __KMComposeWin

// KMail includes
#include "composer.h"
28
#include "recipientseditor.h"
29 30 31 32 33 34 35 36 37 38

// Qt includes
#include <QFont>
#include <QList>
#include <QPalette>

// KDE includes
#include <kglobalsettings.h>

// LIBKDEPIM includes
39
#include <messagecomposer/kmeditor.h>
40

41 42
#include "messagecomposer/messagesender.h"

43 44
// KDEPIMLIBS includes
#include <kmime/kmime_message.h>
45
#include <kmime/kmime_headers.h>
46

47 48
// Other includes
#include "kleo/enum.h"
49
#include <composerviewbase.h>
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

class QByteArray;
class QCheckBox;
class QComboBox;
class QGridLayout;
class QLabel;
class QPushButton;
class QSplitter;

class CodecAction;
class KLineEdit;
class KMComposeWin;
class KMComposerEditor;
class KSelectAction;
class KSelectAction;
class KAction;
class KJob;
class KToggleAction;
class KTemporaryFile;
class KTempDir;
class KToggleAction;
class KUrl;
class KRecentFilesAction;
class SnippetWidget;

75 76 77 78
namespace boost {
  template <typename T> class shared_ptr;
}

79 80 81 82 83 84 85 86
namespace Sonnet {
  class DictionaryComboBox;
}

namespace KPIMIdentities {
  class Identity;
}

87 88 89 90
namespace Akonadi {
  class CollectionComboBox;
}

91 92

namespace KMail {
93 94 95 96
  class AttachmentController;
  class AttachmentView;
}

97 98 99 100
namespace KIO {
  class Job;
}

101
namespace Message {
102 103 104
  class Composer;
}

105 106 107
namespace MessageComposer
{
  class ComposerLineEdit;
108 109
}

110 111 112 113 114 115 116 117 118
//-----------------------------------------------------------------------------
class KMComposeWin : public KMail::Composer
{
  Q_OBJECT
  Q_CLASSINFO("D-Bus Interface", "org.kde.kmail.mailcomposer")

  friend class ::KMComposerEditor;

  private: // mailserviceimpl, kmkernel, kmcommands, callback, kmmainwidget
119
    explicit KMComposeWin( const KMime::Message::Ptr &msg = KMime::Message::Ptr(), TemplateContext context = NoTemplate,
120 121
                           uint identity = 0, const QString & textSelection = QString(),
                           const QString & customTemplate = QString() );
122 123 124
    ~KMComposeWin();

  public:
125
    static Composer *create( const KMime::Message::Ptr &msg = KMime::Message::Ptr(), TemplateContext context = NoTemplate,
126 127
                             uint identity = 0, const QString & textSelection = QString(),
                             const QString & customTemplate = QString() );
128 129 130 131 132 133 134 135

  QString dbusObjectPath() const;
  QString smartQuote( const QString & msg );

  /**
   * Start of D-Bus callable stuff. The D-Bus methods need to be public slots,
   * otherwise they can't be accessed.
   */
136
  // TODO clean-up dbus stuff; make the adaptor a friend; etc.
137 138 139 140 141 142 143 144 145 146 147 148
  public slots:

    Q_SCRIPTABLE void send( int how );

    Q_SCRIPTABLE void addAttachmentsAndSend( const KUrl::List & urls,
                                             const QString & comment,
                                             int how );

    Q_SCRIPTABLE void addAttachment( const KUrl & url,
                                     const QString & comment );

    Q_SCRIPTABLE void addAttachment( const QString & name,
149
                                     KMime::Headers::contentEncoding cte,
150
                                     const QString& charset,
151
                                     const QByteArray & data,
152
                                     const QByteArray & mimeType );
153 154 155 156 157

  /**
   * End of D-Bus callable stuff
   */

158 159 160
  signals:
    void identityChanged( const KPIMIdentities::Identity &identity );

161 162 163 164 165 166 167 168 169 170 171 172
  private:

    /**
     * Write settings to app's config file.
     */
    void writeConfig( void );

  public: // kmkernel, kmcommands, callback
    /**
     * Set the message the composer shall work with. This discards
     * previous messages without calling applyChanges() on them before.
     */
173
    void setMsg( const KMime::Message::Ptr &newMsg, bool mayAutoSign=true,
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
                 bool allowDecryption=false, bool isModified=false );

     /**
      * Disables word wrap completely. No wrapping at all will occur, not even
      * at the right end of the editor.
      * This is useful when sending invitations.
      */
     void disableWordWrap();

     /**
      * Disables HTML completely. It disables HTML at the point of calling this and disables it
      * again when sending the message, to be sure. Useful when sending invitations.
      * This will <b>not</b> remove the actions for activating HTML mode again, it is only
      * meant for automatic invitation sending.
      * Also calls @sa disableHtml() internally.
      */
     void forceDisableHtml();

     /**
      * Returns @c true while the message composing is in progress.
      */
195
     bool isComposing() const { return mComposerBase && mComposerBase->isComposing(); }
196

197 198 199 200 201 202 203 204 205 206
     /**
      * Set the text selection the message is a response to.
      */
     void setTextSelection( const QString& selection );

     /**
      * Set custom template to be used for the message.
      */
     void setCustomTemplate( const QString& customTemplate );

207 208 209 210 211
  private: // kmedit
    /**
     * Returns message of the composer. To apply the user changes to the
     * message, call applyChanges() first.
     */
212
    KMime::Message::Ptr msg() const { return mMsg; }
213 214 215 216 217 218 219 220 221 222 223 224

  private:
    /**
     * Returns true if the message was modified by the user.
     */
    bool isModified() const;

  public: // kmcommand
    /**
     * If this folder is set, the original message is inserted back after
     * canceling
     */
Laurent Montel's avatar
Laurent Montel committed
225
  void setFolder(const Akonadi::Collection &aFolder ) { mFolder = aFolder; }
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

  public: // kmcommand
    /**
     * Sets the focus to the edit-widget.
     */
     void setReplyFocus( bool hasMessage = true );

    /**
     * Sets the focus to the subject line edit. For use when creating a
     * message to a known recipient.
     */
     void setFocusToSubject();

  private:
    /**
     * determines whether inline signing/encryption is selected
     */
     bool inlineSigningEncryptionSelected();


     /**
      * Tries to find the given mimetype @p type in the KDE Mimetype registry.
      * If found, returns its localized description, otherwise the @p type
      * in lowercase.
      */
     static QString prettyMimeType( const QString &type );

  public: // callback
    /** Disabled signing and encryption completely for this composer window. */
    void setSigningAndEncryptionDisabled( bool v )
    {
      mSigningAndEncryptionExplicitlyDisabled = v;
    }

  private slots:
261 262 263 264 265
     /**
      * Disables the HTML mode, by hiding the HTML toolbar and unchecking the
      * "Formatting" action. Also, removes all rich-text formatting.
      */
     void disableHtml( Message::ComposerViewBase::Confirmation confirmation );
266 267 268 269 270
    /**
     * Enables HTML mode, by showing the HTML toolbar and checking the
     * "Formatting" action
     */
    void enableHtml();
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

    /**
     * Actions:
     */
    void slotPrint();
    void slotInsertRecentFile( const KUrl & );
    void slotRecentListFileClear();

  public slots: // kmkernel, callback
    void slotSendNow();

  private slots:
    void slotSendNowVia( QAction * );
    void slotSendLater();
    void slotSendLaterVia( QAction * );
    void getTransportMenu();

    /**
     * Returns true when saving was successful.
     */
    void slotSaveDraft();
    void slotSaveTemplate();
    void slotNewComposer();
    void slotNewMailReader();
    void slotClose();
    void slotHelp();
    void slotUndo();
    void slotRedo();
    void slotCut();
    void slotCopy();
    void slotPaste();
    void slotPasteAsAttachment();
    void slotFormatReset();
    void slotMarkAll();

Laurent Montel's avatar
Laurent Montel committed
306
  void slotFolderRemoved( const Akonadi::Collection& );
307 308
    void slotLanguageChanged( const QString &language );

Allen Winter's avatar
Allen Winter committed
309 310
    void slotEditorTextChanged();

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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
  public slots: // kmkernel
    /**
       Tell the composer to always send the message, even if the user
       hasn't changed the next. This is useful if a message is
       autogenerated (e.g., via a D-Bus call), and the user should
       simply be able to confirm the message and send it.
    */
    void slotSetAlwaysSend( bool bAlwaysSend );

  private slots:
    /**
     * toggle fixed width font.
     */
    void slotUpdateFont();

    /**
     * Open addressbook editor dialog.
     */
    void slotAddrBook();

    /**
     * Insert a file to the end of the text in the editor.
     */
    void slotInsertFile();

    /**
     * Check spelling of text.
     */
    void slotSpellcheckConfig();

    /**
     * Change crypto plugin to be used for signing/encrypting messages,
     * or switch to built-in OpenPGP code.
     */
    void slotSelectCryptoModule( bool init = false );

    /**
     * XML-GUI stuff
     */
    void slotEditToolbars();
    void slotUpdateToolbars();
    void slotEditKeys();

    /**
     * Read settings from app's config file.
     */
    void readConfig( bool reload = false );

    /**
     * Change window title to given string.
     */
    void slotUpdWinTitle( const QString & );

    /**
     * Switch the icon to lock or unlock respectivly.
     * Change states of all encrypt check boxes in the attachments listview
     */
    void slotEncryptToggled( bool );

    /**
     * Change states of all sign check boxes in the attachments listview
     */
    void slotSignToggled( bool );

  public slots: // kmkernel, callback
    /**
     * Switch wordWrap on/off
     */
    void slotWordWrapToggled( bool );

  private slots:
    void slotToggleMarkup();
    void slotTextModeChanged( KRichTextEdit::Mode );
    void htmlToolBarVisibilityChanged( bool visible );
    void slotSpellcheckDoneClearStatus();

  public slots: // kmkernel
    void autoSaveMessage();
Leo Franchi's avatar
Leo Franchi committed
389 390 391 392 393

    /**
     * Set whether the message should be treated as modified or not.
     */
    void setModified( bool modified );
Laurent Montel's avatar
Cleanup  
Laurent Montel committed
394

395 396 397 398 399 400
  private slots:

    void slotView();

    /**
     * Update composer field to reflect new identity
Volker Krause's avatar
Volker Krause committed
401 402 403
     * @param initalChange if true, don't apply the template. This is useful when calling
     *                     this function from setMsg(), because there, the message already has the
     *                     template, and we want to avoid calling the template parser unnecessarily.
404
     */
Volker Krause's avatar
Volker Krause committed
405
    void slotIdentityChanged( uint uoid, bool initalChange = false );
406 407 408 409 410

    void slotCursorPositionChanged();

    void slotSpellCheckingStatus( const QString & status );

Tobias Koenig's avatar
Tobias Koenig committed
411 412
    void slotDelayedApplyTemplate( KJob* );

413
  public: // kmcommand
414
    // FIXME we need to remove these, but they're pure virtual in Composer.
415
    void addAttach( KMime::Content *msgPart );
416 417 418 419 420 421 422

  public: // AttachmentController
    const KPIMIdentities::Identity &identity() const;

  public:
    /** Don't check if there are too many recipients for a mail, eg. when sending out invitations. */
    virtual void disableRecipientNumberCheck();
423

424 425 426
    /** Don't check for forgotten attachments for a mail, eg. when sending out invitations. */
    void disableForgottenAttachmentsCheck();

427 428 429 430 431 432 433 434 435 436 437
    /**
    * Ignore the "sticky" setting of the transport combo box and prefer the X-KMail-Transport
    * header field of the message instead.
    * Do the same for the identity combo box, don't obey the "sticky" setting but use the
    * X-KMail-Identity header field instead.
    *
    * This is useful when sending out invitations, since you don't see the GUI and want the
    * identity and transport to be set to the values stored in the messages.
    */
    void ignoreStickyFields();

438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
  private:
    Kleo::CryptoMessageFormat cryptoMessageFormat() const;

  private:
    /**
     * Install grid management and header fields. If fields exist that
     * should not be there they are removed. Those that are needed are
     * created if necessary.
     */
    void rethinkFields( bool fromslot=false );

    /**
      Connect signals for moving focus by arrow keys. Returns next edit.
    */
    QWidget *connectFocusMoving( QWidget *prev, QWidget *next );

    /**
     * Show or hide header lines
     */

    void rethinkHeaderLine( int aValue, int aMask, int &aRow,
                            QLabel *aLbl, KLineEdit *aEdt,
                            QPushButton *aBtn = 0 );

    void rethinkHeaderLine( int value, int mask, int &row,
                            QLabel *lbl, QComboBox *cbx, QCheckBox *chk ); // krazy:exclude=qclasses

465 466 467 468 469 470 471 472 473 474
    /**
     * Apply template to new or unmodified message.
     */
    void applyTemplate( uint uoid );

    /**
     * Set the quote prefix according to identity.
     */
    void setQuotePrefix( uint uoid );

475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
    /**
     * Checks how many recipients are and warns if there are too many.
     * @return true, if the user accepted the warning and the message should be sent
    */
    bool checkRecipientNumber() const;

    bool checkTransport() const;
    /**
     * Initialization methods
     */
    void setupActions();
    void setupStatusBar();
    void setupEditor();

    /**
     * Header fields.
     */
    QString subject() const;
    QString from() const;
    QString replyTo() const;

    /**
     * Use the given folder as sent-mail folder if the given folder exists.
     * Else show an error message and use the default sent-mail folder as
     * sent-mail folder.
     */
    void setFcc( const QString &idString );

503 504 505
    void setCharset( const QByteArray &charset );
    void setAutoCharset();

506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
    /**
     * Ask for confirmation if the message was changed before close.
     */
    virtual bool queryClose();

    /**
     * prevent kmail from exiting when last window is deleted (kernel rules)
     */
    virtual bool queryExit();

  private:
    /**
     * Turn encryption on/off. If setByUser is true then a message box is shown
     * in case encryption isn't possible.
     */
    void setEncryption( bool encrypt, bool setByUser = false );

    /**
     * Turn signing on/off. If setByUser is true then a message box is shown
     * in case signing isn't possible.
     */
    void setSigning( bool sign, bool setByUser = false );

    /**
      Returns true if the user forgot to attach something.
    */
    bool userForgotAttachment();

    /**
     * Decrypt an OpenPGP block or strip off the OpenPGP envelope of a text
     * block with a clear text signature. This is only done if the given
     * string contains exactly one OpenPGP block.
     * This function is for example used to restore the unencrypted/unsigned
     * message text for editting.
     */
    static void decryptOrStripOffCleartextSignature( QByteArray & );

    /**
544
     * Send the message.
545
     */
546
    void doSend( MessageSender::SendMethod method=MessageSender::SendDefault,
547
                 MessageSender::SaveIn saveIn = MessageSender::SaveInNone );
548

549
    void doDelayedSend( MessageSender::SendMethod method, MessageSender::SaveIn saveIn );
550

551

552 553 554 555

  private slots:
    void recipientEditorSizeHintChanged();
    void setMaximumHeaderSize();
556
    void slotDoDelayedSend( KJob* );
557 558 559 560

  private:
    QWidget   *mMainWidget;
    Sonnet::DictionaryComboBox *mDictionaryCombo;
561
    Akonadi::CollectionComboBox *mFcc;
562 563
    MessageComposer::ComposerLineEdit *mEdtFrom, *mEdtReplyTo;
    MessageComposer::ComposerLineEdit *mEdtSubject;
564 565 566 567
    QLabel    *mLblIdentity, *mLblTransport, *mLblFcc;
    QLabel    *mLblFrom, *mLblReplyTo;
    QLabel    *mLblSubject;
    QLabel    *mDictionaryLabel;
568
    QCheckBox *mBtnIdentity, *mBtnDictionary, *mBtnTransport, *mBtnFcc;
569 570
    bool mDone;

571
    KMime::Message::Ptr mMsg;
572
    QGridLayout *mGrid;
573 574
    QString mTextSelection;
    QString mCustomTemplate;
575 576
    QAction *mOpenId, *mViewId, *mRemoveId, *mSaveAsId, *mPropertiesId,
            *mEditAction, *mEditWithAction;
577
    bool mLastSignActionState, mLastEncryptActionState, mSigningAndEncryptionExplicitlyDisabled;
578
    bool mLastIdentityHasSigningKey, mLastIdentityHasEncryptionKey;
579
    Akonadi::Collection mFolder;
580 581 582 583 584 585 586 587 588
    long mShowHeaders;
    bool mConfirmSend;
    //bool mDisableBreaking;
    bool mForceDisableHtml;     // Completely disable any HTML. Useful when sending invitations in the
                                // mail body.
    int mNumHeaders;
    QFont mBodyFont, mFixedFont;
    QPalette mPalette;
    uint mId;
589
    TemplateContext mContext;
590

591
    KAction *mCleanSpace;
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
    KRecentFilesAction *mRecentAction;

    KToggleAction *mSignAction, *mEncryptAction, *mRequestMDNAction;
    KToggleAction *mUrgentAction, *mAllFieldsAction, *mFromAction;
    KToggleAction *mReplyToAction;
    KToggleAction *mSubjectAction;
    KToggleAction *mIdentityAction, *mTransportAction, *mFccAction;
    KToggleAction *mWordWrapAction, *mFixedFontAction, *mAutoSpellCheckingAction;
    KToggleAction *mDictionaryAction, *mSnippetAction;

    KToggleAction *markupAction;
    KAction *actionFormatReset;

    CodecAction *mCodecAction;
    KSelectAction *mCryptoModuleAction;

608
    KAction *mFindText, *mFindNextText, *mReplaceText, *mSelectAll;
Allen Winter's avatar
Allen Winter committed
609

610 611 612 613 614
    bool mAlwaysSend;

    QStringList mFolderNames;

  private:
615

616 617 618 619 620 621 622 623 624
    /**
     * Creates a simple composer that creates a KMime::Message out of the composer content.
     * Crypto handling is not done, therefore the name "simple".
     * This is used when autosaving or printing a message.
     *
     * The caller takes ownership of the composer.
     */
    Message::Composer* createSimpleComposer();

625 626 627 628 629 630 631 632
    bool canSignEncryptAttachments() const {
      return cryptoMessageFormat() != Kleo::InlineOpenPGPFormat;
    }

    QString addQuotesToText( const QString &inputText ) const;
    // helper method for rethinkFields
    int calcColumnWidth( int which, long allShowing, int width ) const;

633 634 635 636 637 638

    /** Initialize header fields. Should be called on new messages
      if they are not set manually. E.g. before composing. Calling
      of setAutomaticFields(), see below, is still required. */
    void initHeader( KMime::Message *message, uint identity=0 );

Leo Franchi's avatar
Leo Franchi committed
639 640 641 642 643 644 645 646 647 648 649
    /**
     * Helper methods to read from config various encryption settings
     */
    inline bool encryptToSelf();
    inline bool showKeyApprovalDialog();
    inline int encryptKeyNearExpiryWarningThresholdInDays();
    inline int signingKeyNearExpiryWarningThresholdInDays();
    inline int encryptRootCertNearExpiryWarningThresholdInDays();
    inline int signingRootCertNearExpiryWarningThresholdInDays();
    inline int encryptChainCertNearExpiryWarningThresholdInDays();
    inline int signingChainCertNearExpiryWarningThresholdInDays();
Laurent Montel's avatar
Laurent Montel committed
650

651 652 653
  private slots:
    void slotCompletionModeChanged( KGlobalSettings::Completion );
    void slotConfigChanged();
Laurent Montel's avatar
Laurent Montel committed
654

655
    void slotPrintComposeResult( KJob *job );
656 657 658

    void slotEncryptChiasmusToggled( bool );

659 660 661
    void slotSendFailed( const QString& msg );
    void slotSendSuccessful();

662 663 664 665 666 667 668 669 670
    /**
     *  toggle automatic spellchecking
     */
    void slotAutoSpellCheckingToggled( bool );

    /**
     * Updates the visibility and text of the signature and encryption state indicators.
     */
    void slotUpdateSignatureAndEncrypionStateIndicators();
Laurent Montel's avatar
Cleanup  
Laurent Montel committed
671

Leo Franchi's avatar
Leo Franchi committed
672
    virtual void setAutoSaveFileName( const QString& fileName );
673 674 675 676
  private:
    QFont mSaveFont;
    QSplitter *mHeadersToEditorSplitter;
    QWidget* mHeadersArea;
Leo Franchi's avatar
Leo Franchi committed
677
    QSplitter *mSplitter;
678
    QSplitter *mSnippetSplitter;
Leo Franchi's avatar
Leo Franchi committed
679
    KMail::AttachmentView *mAttachmentView;
680
    QByteArray mOriginalPreferredCharset;
681 682

    // These are for passing on methods over the applyChanges calls
683
    MessageSender::SendMethod mSendMethod;
684
    MessageSender::SaveIn mSaveIn;
685 686 687 688

    KToggleAction *mEncryptChiasmusAction;
    bool mEncryptWithChiasmus;

689
    Message::Composer *mDummyComposer;
690
    int mPendingQueueJobs;
691
    int mPendingCreateItemJobs;
692 693
    // used for auto saving, printing, etc. Not for sending, which happens in ComposerViewBase
    QList< Message::Composer* > mMiscComposers;
694 695 696 697 698 699 700 701 702

    int mLabelWidth;

    QMenu *mActNowMenu;
    QMenu *mActLaterMenu;

    QString mdbusObjectPath;
    static int s_composerNumber;

703
    Message::ComposerViewBase* mComposerBase;
Laurent Montel's avatar
Cleanup  
Laurent Montel committed
704

705 706 707 708 709 710 711 712

    SnippetWidget *mSnippetWidget;
    QList<KTempDir*> mTempDirs;

    QLabel *mSignatureStateIndicator;
    QLabel *mEncryptionStateIndicator;

    bool mPreventFccOverwrite;
713
    bool mCheckForForgottenAttachments;
714
    bool mIgnoreStickyFields;
715 716 717
};

#endif