kmail

kmcomposewin.cpp

00001 // -*- mode: C++; c-file-style: "gnu" -*-
00002 // kmcomposewin.cpp
00003 // Author: Markus Wuebben <markus.wuebben@kde.org>
00004 // This code is published under the GPL.
00005 
00006 #undef GrayScale
00007 #undef Color
00008 #include <config.h>
00009 
00010 #define REALLY_WANT_KMCOMPOSEWIN_H
00011 #include "kmcomposewin.h"
00012 #undef REALLY_WANT_KMCOMPOSEWIN_H
00013 
00014 #include "kmedit.h"
00015 #include "kmlineeditspell.h"
00016 #include "kmatmlistview.h"
00017 
00018 #include "kmmainwin.h"
00019 #include "kmreadermainwin.h"
00020 #include "messagesender.h"
00021 #include "kmmsgpartdlg.h"
00022 #include <kpgpblock.h>
00023 #include <kaddrbook.h>
00024 #include "kmaddrbook.h"
00025 #include "kmmsgdict.h"
00026 #include "kmfolderimap.h"
00027 #include "kmfoldermgr.h"
00028 #include "kmfoldercombobox.h"
00029 #include "kmtransport.h"
00030 #include "kmcommands.h"
00031 #include "kcursorsaver.h"
00032 #include "partNode.h"
00033 #include "encodingdetector.h"
00034 #include "attachmentlistview.h"
00035 #include "transportmanager.h"
00036 using KMail::AttachmentListView;
00037 #include "dictionarycombobox.h"
00038 using KMail::DictionaryComboBox;
00039 #include "addressesdialog.h"
00040 using KPIM::AddressesDialog;
00041 #include "addresseeemailselection.h"
00042 using KPIM::AddresseeEmailSelection;
00043 using KPIM::AddresseeSelectorDialog;
00044 #include <maillistdrag.h>
00045 using KPIM::MailListDrag;
00046 #include "recentaddresses.h"
00047 using KRecentAddress::RecentAddresses;
00048 #include "kleo_util.h"
00049 #include "stl_util.h"
00050 #include "recipientseditor.h"
00051 #include "editorwatcher.h"
00052 
00053 #include "attachmentcollector.h"
00054 #include "objecttreeparser.h"
00055 
00056 #include "kmfoldermaildir.h"
00057 
00058 #include <libkpimidentities/identitymanager.h>
00059 #include <libkpimidentities/identitycombo.h>
00060 #include <libkpimidentities/identity.h>
00061 #include <libkdepim/kfileio.h>
00062 #include <libemailfunctions/email.h>
00063 #include <kleo/cryptobackendfactory.h>
00064 #include <kleo/exportjob.h>
00065 #include <kleo/specialjob.h>
00066 #include <ui/progressdialog.h>
00067 #include <ui/keyselectiondialog.h>
00068 
00069 #include <gpgmepp/context.h>
00070 #include <gpgmepp/key.h>
00071 
00072 #include <kio/netaccess.h>
00073 
00074 #include "klistboxdialog.h"
00075 
00076 #include "messagecomposer.h"
00077 #include "chiasmuskeyselector.h"
00078 
00079 #include <kcharsets.h>
00080 #include <kcompletionbox.h>
00081 #include <kcursor.h>
00082 #include <kcombobox.h>
00083 #include <kstdaccel.h>
00084 #include <kpopupmenu.h>
00085 #include <kedittoolbar.h>
00086 #include <kkeydialog.h>
00087 #include <kdebug.h>
00088 #include <kfiledialog.h>
00089 #include <kwin.h>
00090 #include <kinputdialog.h>
00091 #include <kmessagebox.h>
00092 #include <kurldrag.h>
00093 #include <kio/scheduler.h>
00094 #include <ktempfile.h>
00095 #include <klocale.h>
00096 #include <kapplication.h>
00097 #include <kstatusbar.h>
00098 #include <kaction.h>
00099 #include <kstdaction.h>
00100 #include <kdirwatch.h>
00101 #include <kstdguiitem.h>
00102 #include <kiconloader.h>
00103 #include <kpushbutton.h>
00104 #include <kuserprofile.h>
00105 #include <krun.h>
00106 #include <ktempdir.h>
00107 #include <kstandarddirs.h>
00108 //#include <keditlistbox.h>
00109 #include "globalsettings.h"
00110 #include "replyphrases.h"
00111 
00112 #include <kspell.h>
00113 #include <kspelldlg.h>
00114 #include <spellingfilter.h>
00115 #include <ksyntaxhighlighter.h>
00116 #include <kcolordialog.h>
00117 #include <kzip.h>
00118 #include <ksavefile.h>
00119 
00120 #include <qtabdialog.h>
00121 #include <qregexp.h>
00122 #include <qbuffer.h>
00123 #include <qtooltip.h>
00124 #include <qtextcodec.h>
00125 #include <qheader.h>
00126 #include <qwhatsthis.h>
00127 #include <qfontdatabase.h>
00128 
00129 #include <mimelib/mimepp.h>
00130 
00131 #include <algorithm>
00132 #include <memory>
00133 
00134 #include <sys/stat.h>
00135 #include <sys/types.h>
00136 #include <stdlib.h>
00137 #include <unistd.h>
00138 #include <errno.h>
00139 #include <fcntl.h>
00140 #include <assert.h>
00141 
00142 #include "kmcomposewin.moc"
00143 
00144 #include "snippetwidget.h"
00145 
00146 KMail::Composer * KMail::makeComposer( KMMessage * msg, uint identitiy ) {
00147   return KMComposeWin::create( msg, identitiy );
00148 }
00149 
00150 KMail::Composer * KMComposeWin::create( KMMessage * msg, uint identitiy ) {
00151   return new KMComposeWin( msg, identitiy );
00152 }
00153 
00154 //-----------------------------------------------------------------------------
00155 KMComposeWin::KMComposeWin( KMMessage *aMsg, uint id  )
00156   : MailComposerIface(), KMail::Composer( "kmail-composer#" ),
00157     mSpellCheckInProgress( false ),
00158     mDone( false ),
00159     mAtmModified( false ),
00160     mAtmSelectNew( 0 ),
00161     mMsg( 0 ),
00162     mAttachMenu( 0 ),
00163     mSigningAndEncryptionExplicitlyDisabled( false ),
00164     mFolder( 0 ),
00165     mUseHTMLEditor( false ),
00166     mId( id ),
00167     mAttachPK( 0 ), mAttachMPK( 0 ),
00168     mAttachRemoveAction( 0 ), mAttachSaveAction( 0 ), mAttachPropertiesAction( 0 ),
00169     mAppendSignatureAction( 0 ), mPrependSignatureAction( 0 ), mInsertSignatureAction( 0 ),
00170     mSignAction( 0 ), mEncryptAction( 0 ), mRequestMDNAction( 0 ),
00171     mUrgentAction( 0 ), mAllFieldsAction( 0 ), mFromAction( 0 ),
00172     mReplyToAction( 0 ), mToAction( 0 ), mCcAction( 0 ), mBccAction( 0 ),
00173     mSubjectAction( 0 ),
00174     mIdentityAction( 0 ), mTransportAction( 0 ), mFccAction( 0 ),
00175     mWordWrapAction( 0 ), mFixedFontAction( 0 ), mAutoSpellCheckingAction( 0 ),
00176     mDictionaryAction( 0 ), mSnippetAction( 0 ),
00177     mEncodingAction( 0 ),
00178     mCryptoModuleAction( 0 ),
00179     mEncryptChiasmusAction( 0 ),
00180     mEncryptWithChiasmus( false ),
00181     mComposer( 0 ),
00182     mLabelWidth( 0 ),
00183     mAutoSaveTimer( 0 ), mLastAutoSaveErrno( 0 ),
00184     mSignatureStateIndicator( 0 ), mEncryptionStateIndicator( 0 ),
00185     mPreserveUserCursorPosition( false ),
00186     mPreventFccOverwrite( false ),
00187     mCheckForRecipients( true ),
00188     mCheckForForgottenAttachments( true ),
00189     mIgnoreStickyFields( false )
00190 {
00191   mClassicalRecipients = GlobalSettings::self()->recipientsEditorType() ==
00192     GlobalSettings::EnumRecipientsEditorType::Classic;
00193 
00194   mSubjectTextWasSpellChecked = false;
00195   if (kmkernel->xmlGuiInstance())
00196     setInstance( kmkernel->xmlGuiInstance() );
00197   mMainWidget = new QWidget(this);
00198   // splitter between the headers area and the actual editor
00199   mHeadersToEditorSplitter = new QSplitter( Qt::Vertical, mMainWidget, "mHeadersToEditorSplitter" );
00200   mHeadersToEditorSplitter->setChildrenCollapsible( false );
00201   mHeadersArea = new QWidget( mHeadersToEditorSplitter );
00202   mHeadersArea->setSizePolicy( mHeadersToEditorSplitter->sizePolicy().horData(), QSizePolicy::Maximum );
00203   QVBoxLayout *v = new QVBoxLayout( mMainWidget );
00204   v->addWidget( mHeadersToEditorSplitter );
00205   mIdentity = new KPIM::IdentityCombo(kmkernel->identityManager(), mHeadersArea);
00206   QToolTip::add( mIdentity,
00207                  i18n( "Select an identity for this message" ) );
00208 
00209   mDictionaryCombo = new DictionaryComboBox( mHeadersArea );
00210   QToolTip::add( mDictionaryCombo,
00211                  i18n( "Select the dictionary to use when spell-checking this message" ) );
00212 
00213   mFcc = new KMFolderComboBox(mHeadersArea);
00214   mFcc->showOutboxFolder( false );
00215   QToolTip::add( mFcc,
00216                  i18n( "Select the sent-mail folder where a copy of this message will be saved" ) );
00217 
00218   mTransport = new QComboBox(true, mHeadersArea);
00219   QToolTip::add( mTransport,
00220                  i18n( "Select the outgoing account to use for sending this message" ) );
00221 
00222   mEdtFrom = new KMLineEdit(false,mHeadersArea, "fromLine");
00223   QToolTip::add( mEdtFrom,
00224                  i18n( "Set the \"From:\" email address for this message" ) );
00225 
00226   mEdtReplyTo = new KMLineEdit(true,mHeadersArea, "replyToLine");
00227   QToolTip::add( mEdtReplyTo,
00228                  i18n( "Set the \"Reply-To:\" email address for this message" ) );
00229   connect(mEdtReplyTo,SIGNAL(completionModeChanged(KGlobalSettings::Completion)),
00230           SLOT(slotCompletionModeChanged(KGlobalSettings::Completion)));
00231 
00232   if ( mClassicalRecipients ) {
00233     mRecipientsEditor = 0;
00234 
00235     mEdtTo = new KMLineEdit(true,mHeadersArea, "toLine");
00236     mEdtCc = new KMLineEdit(true,mHeadersArea, "ccLine");
00237     mEdtBcc = new KMLineEdit(true,mHeadersArea, "bccLine");
00238 
00239     mLblTo = new QLabel(mHeadersArea);
00240     mLblCc = new QLabel(mHeadersArea);
00241     mLblBcc = new QLabel(mHeadersArea);
00242 
00243     mBtnTo = new QPushButton("...",mHeadersArea);
00244     mBtnCc = new QPushButton("...",mHeadersArea);
00245     mBtnBcc = new QPushButton("...",mHeadersArea);
00246     //mBtnFrom = new QPushButton("...",mHeadersArea);
00247 
00248     QString tip = i18n("Select email address(es)");
00249     QToolTip::add( mBtnTo, tip );
00250     QToolTip::add( mBtnCc, tip );
00251     QToolTip::add( mBtnBcc, tip );
00252 
00253     mBtnTo->setFocusPolicy(QWidget::NoFocus);
00254     mBtnCc->setFocusPolicy(QWidget::NoFocus);
00255     mBtnBcc->setFocusPolicy(QWidget::NoFocus);
00256     //mBtnFrom->setFocusPolicy(QWidget::NoFocus);
00257 
00258     connect(mBtnTo,SIGNAL(clicked()),SLOT(slotAddrBookTo()));
00259     connect(mBtnCc,SIGNAL(clicked()),SLOT(slotAddrBookTo()));
00260     connect(mBtnBcc,SIGNAL(clicked()),SLOT(slotAddrBookTo()));
00261     //connect(mBtnFrom,SIGNAL(clicked()),SLOT(slotAddrBookFrom()));
00262 
00263     connect(mEdtTo,SIGNAL(completionModeChanged(KGlobalSettings::Completion)),
00264             SLOT(slotCompletionModeChanged(KGlobalSettings::Completion)));
00265     connect(mEdtCc,SIGNAL(completionModeChanged(KGlobalSettings::Completion)),
00266             SLOT(slotCompletionModeChanged(KGlobalSettings::Completion)));
00267     connect(mEdtBcc,SIGNAL(completionModeChanged(KGlobalSettings::Completion)),
00268             SLOT(slotCompletionModeChanged(KGlobalSettings::Completion)));
00269 
00270     mEdtTo->setFocus();
00271   } else {
00272     mEdtTo = 0;
00273     mEdtCc = 0;
00274     mEdtBcc = 0;
00275 
00276     mLblTo = 0;
00277     mLblCc = 0;
00278     mLblBcc = 0;
00279 
00280     mBtnTo = 0;
00281     mBtnCc = 0;
00282     mBtnBcc = 0;
00283     //mBtnFrom = 0;
00284 
00285     mRecipientsEditor = new RecipientsEditor( mHeadersArea );
00286     connect( mRecipientsEditor,
00287              SIGNAL( completionModeChanged( KGlobalSettings::Completion ) ),
00288              SLOT( slotCompletionModeChanged( KGlobalSettings::Completion ) ) );
00289     connect( mRecipientsEditor, SIGNAL(sizeHintChanged()), SLOT(recipientEditorSizeHintChanged()) );
00290 
00291     mRecipientsEditor->setFocus();
00292   }
00293   mEdtSubject = new KMLineEditSpell(false,mHeadersArea, "subjectLine");
00294   QToolTip::add( mEdtSubject,
00295                  i18n( "Set a subject for this message" ) );
00296 
00297   mLblIdentity = new QLabel( i18n("&Identity:"), mHeadersArea );
00298   mDictionaryLabel = new QLabel( i18n("&Dictionary:"), mHeadersArea );
00299   mLblFcc = new QLabel( i18n("&Sent-Mail folder:"), mHeadersArea );
00300   mLblTransport = new QLabel( i18n("&Mail transport:"), mHeadersArea );
00301   mLblFrom = new QLabel( i18n("sender address field", "&From:"), mHeadersArea );
00302   mLblReplyTo = new QLabel( i18n("&Reply to:"), mHeadersArea );
00303   mLblSubject = new QLabel( i18n("S&ubject:"), mHeadersArea );
00304 
00305   QString sticky = i18n("Sticky");
00306   mBtnIdentity = new QCheckBox(sticky,mHeadersArea);
00307   QToolTip::add( mBtnIdentity,
00308                  i18n( "Use the selected value as your identity for future messages" ) );
00309   mBtnFcc = new QCheckBox(sticky,mHeadersArea);
00310   QToolTip::add( mBtnFcc,
00311                  i18n( "Use the selected value as your sent-mail folder for future messages" ) );
00312   mBtnTransport = new QCheckBox(sticky,mHeadersArea);
00313   QToolTip::add( mBtnTransport,
00314                  i18n( "Use the selected value as your outgoing account for future messages" ) );
00315   mBtnDictionary = new QCheckBox( sticky, mHeadersArea );
00316   QToolTip::add( mBtnDictionary,
00317                  i18n( "Use the selected value as your dictionary for future messages" ) );
00318 
00319   //setWFlags( WType_TopLevel | WStyle_Dialog );
00320   mHtmlMarkup = GlobalSettings::self()->useHtmlMarkup();
00321   mShowHeaders = GlobalSettings::self()->headers();
00322   mDone = false;
00323   mGrid = 0;
00324   mAtmListView = 0;
00325   mAtmList.setAutoDelete(true);
00326   mAtmTempList.setAutoDelete(true);
00327   mAtmModified = false;
00328   mAutoDeleteMsg = false;
00329   mFolder = 0;
00330   mAutoCharset = true;
00331   mFixedFontAction = 0;
00332   mTempDir = 0;
00333   // the attachment view is separated from the editor by a splitter
00334   mSplitter = new QSplitter( Qt::Vertical, mHeadersToEditorSplitter, "mSplitter" );
00335   mSplitter->setChildrenCollapsible( false );
00336   mSnippetSplitter = new QSplitter( Qt::Horizontal, mSplitter, "mSnippetSplitter");
00337   mSnippetSplitter->setChildrenCollapsible( false );
00338 
00339   QWidget *editorAndCryptoStateIndicators = new QWidget( mSnippetSplitter );
00340   QVBoxLayout *vbox = new QVBoxLayout( editorAndCryptoStateIndicators );
00341   QHBoxLayout *hbox = new QHBoxLayout( vbox );
00342   {
00343       mSignatureStateIndicator = new QLabel( editorAndCryptoStateIndicators );
00344       mSignatureStateIndicator->setAlignment( Qt::AlignHCenter );
00345       hbox->addWidget( mSignatureStateIndicator );
00346 
00347       KConfigGroup reader( KMKernel::config(), "Reader" );
00348       QPalette p( mSignatureStateIndicator->palette() );
00349 
00350       QColor defaultSignedColor( 0x40, 0xFF, 0x40 ); // light green // pgp ok, trusted key
00351       QColor defaultEncryptedColor( 0x00, 0x80, 0xFF ); // light blue // pgp encrypted
00352       p.setColor( QColorGroup::Background, reader.readColorEntry( "PGPMessageOkKeyOk", &defaultSignedColor ) );
00353       mSignatureStateIndicator->setPalette( p );
00354 
00355       mEncryptionStateIndicator = new QLabel( editorAndCryptoStateIndicators );
00356       mEncryptionStateIndicator->setAlignment( Qt::AlignHCenter );
00357       hbox->addWidget( mEncryptionStateIndicator );
00358       p.setColor( QColorGroup::Background, reader.readColorEntry( "PGPMessageEncr" , &defaultEncryptedColor ) );
00359       mEncryptionStateIndicator->setPalette( p );
00360   }
00361 
00362   mEditor = new KMEdit( editorAndCryptoStateIndicators, this, mDictionaryCombo->spellConfig() );
00363   vbox->addWidget( mEditor );
00364 
00365   mSnippetWidget = new SnippetWidget( mEditor, actionCollection(), mSnippetSplitter );
00366   mSnippetWidget->setShown( GlobalSettings::self()->showSnippetManager() );
00367 
00368   //  mSplitter->moveToFirst( editorAndCryptoStateIndicators );
00369   mSplitter->setOpaqueResize( true );
00370 
00371   mEditor->initializeAutoSpellChecking();
00372   mEditor->setTextFormat(Qt::PlainText);
00373   mEditor->setAcceptDrops( true );
00374 
00375   QWhatsThis::add( mBtnIdentity,
00376     GlobalSettings::self()->stickyIdentityItem()->whatsThis() );
00377   QWhatsThis::add( mBtnFcc,
00378     GlobalSettings::self()->stickyFccItem()->whatsThis() );
00379   QWhatsThis::add( mBtnTransport,
00380     GlobalSettings::self()->stickyTransportItem()->whatsThis() );
00381   QWhatsThis::add( mBtnTransport,
00382     GlobalSettings::self()->stickyDictionaryItem()->whatsThis() );
00383 
00384   mSpellCheckInProgress=false;
00385 
00386   setCaption( i18n("Composer") );
00387   setMinimumSize(200,200);
00388 
00389   mBtnIdentity->setFocusPolicy(QWidget::NoFocus);
00390   mBtnFcc->setFocusPolicy(QWidget::NoFocus);
00391   mBtnTransport->setFocusPolicy(QWidget::NoFocus);
00392   mBtnDictionary->setFocusPolicy( QWidget::NoFocus );
00393 
00394   mAtmListView = new AttachmentListView( this, mSplitter,
00395                                          "attachment list view" );
00396   mAtmListView->setSelectionMode( QListView::Extended );
00397   mAtmListView->setSorting( 0 );
00398   mAtmListView->setShowSortIndicator( true );
00399   mAtmListView->addColumn( i18n("Name"), 200 );
00400   mAtmListView->addColumn( i18n("Size"), 80 );
00401   mAtmListView->addColumn( i18n("Encoding"), 120 );
00402   int atmColType = mAtmListView->addColumn( i18n("Type"), 120 );
00403   // Stretch "Type".
00404   mAtmListView->header()->setStretchEnabled( true, atmColType );
00405   mAtmEncryptColWidth = 80;
00406   mAtmSignColWidth = 80;
00407   mAtmCompressColWidth = 100;
00408   mAtmColCompress = mAtmListView->addColumn( i18n("Compress"),
00409                                             mAtmCompressColWidth );
00410   mAtmColEncrypt = mAtmListView->addColumn( i18n("Encrypt"),
00411                                             mAtmEncryptColWidth );
00412   mAtmColSign    = mAtmListView->addColumn( i18n("Sign"),
00413                                             mAtmSignColWidth );
00414   mAtmListView->setColumnWidth( mAtmColEncrypt, 0 );
00415   mAtmListView->setColumnWidth( mAtmColSign,    0 );
00416   mAtmListView->setAllColumnsShowFocus( true );
00417 
00418   connect( mAtmListView,
00419            SIGNAL( doubleClicked( QListViewItem* ) ),
00420            SLOT( slotAttachEdit() ) );
00421   connect( mAtmListView,
00422            SIGNAL( rightButtonPressed( QListViewItem*, const QPoint&, int ) ),
00423            SLOT( slotAttachPopupMenu( QListViewItem*, const QPoint&, int ) ) );
00424   connect( mAtmListView,
00425            SIGNAL( selectionChanged() ),
00426            SLOT( slotUpdateAttachActions() ) );
00427   connect( mAtmListView,
00428            SIGNAL( attachmentDeleted() ),
00429            SLOT( slotAttachRemove() ) );
00430   connect( mAtmListView,
00431            SIGNAL( dragStarted() ),
00432            SLOT( slotAttachmentDragStarted() ) );
00433   mAttachMenu = 0;
00434 
00435   readConfig();
00436   setupStatusBar();
00437   setupActions();
00438   setupEditor();
00439   slotUpdateSignatureAndEncrypionStateIndicators();
00440 
00441   applyMainWindowSettings(KMKernel::config(), "Composer");
00442 
00443   connect( mEdtSubject, SIGNAL( subjectTextSpellChecked() ),
00444            SLOT( slotSubjectTextSpellChecked() ) );
00445   connect(mEdtSubject,SIGNAL(textChanged(const QString&)),
00446           SLOT(slotUpdWinTitle(const QString&)));
00447   connect(mIdentity,SIGNAL(identityChanged(uint)),
00448           SLOT(slotIdentityChanged(uint)));
00449   connect( kmkernel->identityManager(), SIGNAL(changed(uint)),
00450           SLOT(slotIdentityChanged(uint)));
00451 
00452   connect(mEdtFrom,SIGNAL(completionModeChanged(KGlobalSettings::Completion)),
00453           SLOT(slotCompletionModeChanged(KGlobalSettings::Completion)));
00454   connect(kmkernel->folderMgr(),SIGNAL(folderRemoved(KMFolder*)),
00455                                   SLOT(slotFolderRemoved(KMFolder*)));
00456   connect(kmkernel->imapFolderMgr(),SIGNAL(folderRemoved(KMFolder*)),
00457                                   SLOT(slotFolderRemoved(KMFolder*)));
00458   connect(kmkernel->dimapFolderMgr(),SIGNAL(folderRemoved(KMFolder*)),
00459                                   SLOT(slotFolderRemoved(KMFolder*)));
00460   connect( kmkernel, SIGNAL( configChanged() ),
00461            this, SLOT( slotConfigChanged() ) );
00462 
00463   connect (mEditor, SIGNAL (spellcheck_done(int)),
00464     this, SLOT (slotSpellcheckDone (int)));
00465   connect (mEditor, SIGNAL( attachPNGImageData(const QByteArray &) ),
00466     this, SLOT ( slotAttachPNGImageData(const QByteArray &) ) );
00467   connect (mEditor, SIGNAL( focusChanged(bool) ),
00468     this, SLOT (editorFocusChanged(bool)) );
00469 
00470   mMainWidget->resize(480,510);
00471   setCentralWidget(mMainWidget);
00472   rethinkFields();
00473 
00474   if ( !mClassicalRecipients ) {
00475     // This is ugly, but if it isn't called the line edits in the recipients
00476     // editor aren't wide enough until the first resize event comes.
00477     rethinkFields();
00478   }
00479 
00480   if ( GlobalSettings::self()->useExternalEditor() ) {
00481     mEditor->setUseExternalEditor(true);
00482     mEditor->setExternalEditorPath( GlobalSettings::self()->externalEditor() );
00483   }
00484 
00485   initAutoSave();
00486   slotUpdateSignatureActions();
00487   mMsg = 0;
00488   if (aMsg)
00489     setMsg(aMsg);
00490   fontChanged( mEditor->currentFont() ); // set toolbar buttons to correct values
00491 
00492   mDone = true;
00493 }
00494 
00495 //-----------------------------------------------------------------------------
00496 KMComposeWin::~KMComposeWin()
00497 {
00498   writeConfig();
00499   if (mFolder && mMsg)
00500   {
00501     mAutoDeleteMsg = false;
00502     mFolder->addMsg(mMsg);
00503     // Ensure that the message is correctly and fully parsed
00504     mFolder->unGetMsg( mFolder->count() - 1 );
00505   }
00506   if (mAutoDeleteMsg) {
00507     delete mMsg;
00508     mMsg = 0;
00509   }
00510   QMap<KIO::Job*, atmLoadData>::Iterator it = mMapAtmLoadData.begin();
00511   while ( it != mMapAtmLoadData.end() )
00512   {
00513     KIO::Job *job = it.key();
00514     mMapAtmLoadData.remove( it );
00515     job->kill();
00516     it = mMapAtmLoadData.begin();
00517   }
00518   deleteAll( mComposedMessages );
00519 
00520   for ( std::set<KTempDir*>::iterator it = mTempDirs.begin() ; it != mTempDirs.end() ; ++it ) {
00521       delete *it;
00522   }
00523 }
00524 
00525 void KMComposeWin::setAutoDeleteWindow( bool f )
00526 {
00527   if ( f )
00528     setWFlags( getWFlags() | WDestructiveClose );
00529   else
00530     setWFlags( getWFlags() & ~WDestructiveClose );
00531 }
00532 
00533 //-----------------------------------------------------------------------------
00534 void KMComposeWin::send(int how)
00535 {
00536   switch (how) {
00537     case 1:
00538       slotSendNow();
00539       break;
00540     default:
00541     case 0:
00542       // TODO: find out, what the default send method is and send it this way
00543     case 2:
00544       slotSendLater();
00545       break;
00546   }
00547 }
00548 
00549 //-----------------------------------------------------------------------------
00550 void KMComposeWin::addAttachmentsAndSend(const KURL::List &urls, const QString &/*comment*/, int how)
00551 {
00552   if (urls.isEmpty())
00553   {
00554     send(how);
00555     return;
00556   }
00557   mAttachFilesSend = how;
00558   mAttachFilesPending = urls;
00559   connect(this, SIGNAL(attachmentAdded(const KURL&, bool)), SLOT(slotAttachedFile(const KURL&)));
00560   for( KURL::List::ConstIterator itr = urls.begin(); itr != urls.end(); ++itr ) {
00561     if (!addAttach( *itr ))
00562       mAttachFilesPending.remove(mAttachFilesPending.find(*itr)); // only remove one copy of the url
00563   }
00564 
00565   if (mAttachFilesPending.isEmpty() && mAttachFilesSend == how)
00566   {
00567     send(mAttachFilesSend);
00568     mAttachFilesSend = -1;
00569   }
00570 }
00571 
00572 void KMComposeWin::slotAttachedFile(const KURL &url)
00573 {
00574   if (mAttachFilesPending.isEmpty())
00575     return;
00576   mAttachFilesPending.remove(mAttachFilesPending.find(url)); // only remove one copy of url
00577   if (mAttachFilesPending.isEmpty())
00578   {
00579     send(mAttachFilesSend);
00580     mAttachFilesSend = -1;
00581   }
00582 }
00583 
00584 //-----------------------------------------------------------------------------
00585 void KMComposeWin::addAttachment(KURL url,QString /*comment*/)
00586 {
00587   addAttach(url);
00588 }
00589 
00590 //-----------------------------------------------------------------------------
00591 void KMComposeWin::addAttachment(const QString &name,
00592                                  const QCString &/*cte*/,
00593                                  const QByteArray &data,
00594                                  const QCString &type,
00595                                  const QCString &subType,
00596                                  const QCString &paramAttr,
00597                                  const QString &paramValue,
00598                                  const QCString &contDisp)
00599 {
00600   if (!data.isEmpty()) {
00601     KMMessagePart *msgPart = new KMMessagePart;
00602     msgPart->setName(name);
00603     if( type == "message" && subType == "rfc822" ) {
00604        msgPart->setMessageBody( data );
00605     } else {
00606        QValueList<int> dummy;
00607        msgPart->setBodyAndGuessCte(data, dummy,
00608               kmkernel->msgSender()->sendQuotedPrintable());
00609     }
00610     msgPart->setTypeStr(type);
00611     msgPart->setSubtypeStr(subType);
00612     msgPart->setParameter(paramAttr,paramValue);
00613     msgPart->setContentDisposition(contDisp);
00614     addAttach(msgPart);
00615   }
00616 }
00617 
00618 //-----------------------------------------------------------------------------
00619 void KMComposeWin::slotAttachPNGImageData(const QByteArray &image)
00620 {
00621   bool ok;
00622 
00623   QString attName = KInputDialog::getText( "KMail", i18n("Name of the attachment:"), QString::null, &ok, this );
00624   if ( !ok )
00625     return;
00626 
00627   if ( !attName.lower().endsWith(".png") ) attName += ".png";
00628 
00629   addAttachment( attName, "base64", image, "image", "png", QCString(), QString(), QCString() );
00630 }
00631 
00632 //-----------------------------------------------------------------------------
00633 void KMComposeWin::setBody(QString body)
00634 {
00635   mEditor->setText(body);
00636 }
00637 
00638 //-----------------------------------------------------------------------------
00639 bool KMComposeWin::event(QEvent *e)
00640 {
00641   if (e->type() == QEvent::ApplicationPaletteChange)
00642   {
00643      readColorConfig();
00644   }
00645   return KMail::Composer::event(e);
00646 }
00647 
00648 
00649 //-----------------------------------------------------------------------------
00650 void KMComposeWin::readColorConfig(void)
00651 {
00652   if ( GlobalSettings::self()->useDefaultColors() ) {
00653     mForeColor = QColor(kapp->palette().active().text());
00654     mBackColor = QColor(kapp->palette().active().base());
00655   } else {
00656     mForeColor = GlobalSettings::self()->foregroundColor();
00657     mBackColor = GlobalSettings::self()->backgroundColor();
00658   }
00659 
00660   // Color setup
00661   mPalette = kapp->palette();
00662   QColorGroup cgrp  = mPalette.active();
00663   cgrp.setColor( QColorGroup::Base, mBackColor);
00664   cgrp.setColor( QColorGroup::Text, mForeColor);
00665   mPalette.setDisabled(cgrp);
00666   mPalette.setActive(cgrp);
00667   mPalette.setInactive(cgrp);
00668 
00669   mEdtFrom->setPalette(mPalette);
00670   mEdtReplyTo->setPalette(mPalette);
00671   if ( mClassicalRecipients ) {
00672     mEdtTo->setPalette(mPalette);
00673     mEdtCc->setPalette(mPalette);
00674     mEdtBcc->setPalette(mPalette);
00675   }
00676   mEdtSubject->setPalette(mPalette);
00677   mTransport->setPalette(mPalette);
00678   mEditor->setPalette(mPalette);
00679   mFcc->setPalette(mPalette);
00680 }
00681 
00682 //-----------------------------------------------------------------------------
00683 void KMComposeWin::readConfig( bool reload /* = false */ )
00684 {
00685   mDefCharset = KMMessage::defaultCharset();
00686   mBtnIdentity->setChecked( GlobalSettings::self()->stickyIdentity() );
00687   if (mBtnIdentity->isChecked()) {
00688     mId = (GlobalSettings::self()->previousIdentity()!=0) ?
00689            GlobalSettings::self()->previousIdentity() : mId;
00690   }
00691   mBtnFcc->setChecked( GlobalSettings::self()->stickyFcc() );
00692   mBtnTransport->setChecked( GlobalSettings::self()->stickyTransport() );
00693   mBtnDictionary->setChecked( GlobalSettings::self()->stickyDictionary() );
00694   QStringList transportHistory = GlobalSettings::self()->transportHistory();
00695   QString currentTransport = GlobalSettings::self()->currentTransport();
00696 
00697   mEdtFrom->setCompletionMode( (KGlobalSettings::Completion)GlobalSettings::self()->completionMode() );
00698   mEdtReplyTo->setCompletionMode( (KGlobalSettings::Completion)GlobalSettings::self()->completionMode() );
00699   if ( mClassicalRecipients ) {
00700     mEdtTo->setCompletionMode( (KGlobalSettings::Completion)GlobalSettings::self()->completionMode() );
00701     mEdtCc->setCompletionMode( (KGlobalSettings::Completion)GlobalSettings::self()->completionMode() );
00702     mEdtBcc->setCompletionMode( (KGlobalSettings::Completion)GlobalSettings::self()->completionMode() );
00703   }
00704   else
00705     mRecipientsEditor->setCompletionMode( (KGlobalSettings::Completion)GlobalSettings::self()->completionMode() );
00706 
00707   readColorConfig();
00708 
00709   if ( GlobalSettings::self()->useDefaultFonts() ) {
00710     mBodyFont = KGlobalSettings::generalFont();
00711     mFixedFont = KGlobalSettings::fixedFont();
00712   } else {
00713     mBodyFont = GlobalSettings::self()->composerFont();
00714     mFixedFont = GlobalSettings::self()->fixedFont();
00715   }
00716 
00717   slotUpdateFont();
00718   mEdtFrom->setFont(mBodyFont);
00719   mEdtReplyTo->setFont(mBodyFont);
00720   if ( mClassicalRecipients ) {
00721     mEdtTo->setFont(mBodyFont);
00722     mEdtCc->setFont(mBodyFont);
00723     mEdtBcc->setFont(mBodyFont);
00724   }
00725   mEdtSubject->setFont(mBodyFont);
00726 
00727   if ( !reload ) {
00728     QSize siz = GlobalSettings::self()->composerSize();
00729     if (siz.width() < 200) siz.setWidth(200);
00730     if (siz.height() < 200) siz.setHeight(200);
00731     resize(siz);
00732 
00733     if ( !GlobalSettings::self()->snippetSplitterPosition().isEmpty() ) {
00734       mSnippetSplitter->setSizes( GlobalSettings::self()->snippetSplitterPosition() );
00735     } else {
00736       QValueList<int> defaults;
00737       defaults << (int)(width() * 0.8) << (int)(width() * 0.2);
00738       mSnippetSplitter->setSizes( defaults );
00739     }
00740   }
00741 
00742   mIdentity->setCurrentIdentity( mId );
00743 
00744   kdDebug(5006) << "KMComposeWin::readConfig. " << mIdentity->currentIdentityName() << endl;
00745   const KPIM::Identity & ident =
00746     kmkernel->identityManager()->identityForUoid( mIdentity->currentIdentity() );
00747 
00748   mTransport->clear();
00749   mTransport->insertStringList( KMTransportInfo::availableTransports() );
00750   while ( transportHistory.count() > (uint)GlobalSettings::self()->maxTransportEntries() )
00751     transportHistory.remove( transportHistory.last() );
00752   mTransport->insertStringList( transportHistory );
00753   mTransport->setCurrentText( GlobalSettings::self()->defaultTransport() );
00754   if ( mBtnTransport->isChecked() ) {
00755     setTransport( currentTransport );
00756   }
00757 
00758   if ( mBtnDictionary->isChecked() ) {
00759     mDictionaryCombo->setCurrentByDictionaryName( GlobalSettings::self()->previousDictionary() );
00760   } else {
00761     mDictionaryCombo->setCurrentByDictionary( ident.dictionary() );
00762   }
00763 
00764   QString fccName = "";
00765   if ( mBtnFcc->isChecked() ) {
00766     fccName = GlobalSettings::self()->previousFcc();
00767   } else if ( !ident.fcc().isEmpty() ) {
00768       fccName = ident.fcc();
00769   }
00770 
00771   setFcc( fccName );
00772 }
00773 
00774 //-----------------------------------------------------------------------------
00775 void KMComposeWin::writeConfig(void)
00776 {
00777   GlobalSettings::self()->setHeaders( mShowHeaders );
00778   GlobalSettings::self()->setStickyFcc( mBtnFcc->isChecked() );
00779   if ( !mIgnoreStickyFields ) {
00780     GlobalSettings::self()->setCurrentTransport( mTransport->currentText() );
00781     GlobalSettings::self()->setStickyTransport( mBtnTransport->isChecked() );
00782     GlobalSettings::self()->setStickyDictionary( mBtnDictionary->isChecked() );
00783     GlobalSettings::self()->setStickyIdentity( mBtnIdentity->isChecked() );
00784     GlobalSettings::self()->setPreviousIdentity( mIdentity->currentIdentity() );
00785   }
00786   GlobalSettings::self()->setPreviousFcc( mFcc->getFolder()->idString() );
00787   GlobalSettings::self()->setPreviousDictionary( mDictionaryCombo->currentDictionaryName() );
00788   GlobalSettings::self()->setAutoSpellChecking(
00789                         mAutoSpellCheckingAction->isChecked() );
00790   QStringList transportHistory = GlobalSettings::self()->transportHistory();
00791   transportHistory.remove(mTransport->currentText());
00792     if (KMTransportInfo::availableTransports().findIndex(mTransport
00793     ->currentText()) == -1) {
00794       transportHistory.prepend(mTransport->currentText());
00795   }
00796   GlobalSettings::self()->setTransportHistory( transportHistory );
00797   GlobalSettings::self()->setUseFixedFont( mFixedFontAction->isChecked() );
00798   GlobalSettings::self()->setUseHtmlMarkup( mHtmlMarkup );
00799   GlobalSettings::self()->setComposerSize( size() );
00800   GlobalSettings::self()->setShowSnippetManager( mSnippetAction->isChecked() );
00801 
00802   KConfigGroupSaver saver( KMKernel::config(), "Geometry" );
00803   saveMainWindowSettings( KMKernel::config(), "Composer" );
00804   GlobalSettings::setSnippetSplitterPosition( mSnippetSplitter->sizes() );
00805 
00806   // make sure config changes are written to disk, cf. bug 127538
00807   GlobalSettings::self()->writeConfig();
00808 }
00809 
00810 //-----------------------------------------------------------------------------
00811 void KMComposeWin::autoSaveMessage()
00812 {
00813   kdDebug(5006) << k_funcinfo << endl;
00814   if ( !mMsg || mComposer || mAutoSaveFilename.isEmpty() )
00815     return;
00816   kdDebug(5006) << k_funcinfo << "autosaving message" << endl;
00817 
00818   if ( mAutoSaveTimer )
00819     mAutoSaveTimer->stop();
00820 
00821   connect( this, SIGNAL( applyChangesDone( bool ) ),
00822            this, SLOT( slotContinueAutoSave() ) );
00823   // This method is called when KMail crashed, so don't try signing/encryption
00824   // and don't disable controls because it is also called from a timer and
00825   // then the disabling is distracting.
00826   applyChanges( true, true );
00827 
00828   // Don't continue before the applyChanges is done!
00829 }
00830 
00831 void KMComposeWin::slotContinueAutoSave()
00832 {
00833   disconnect( this, SIGNAL( applyChangesDone( bool ) ),
00834               this, SLOT( slotContinueAutoSave() ) );
00835 
00836   // Ok, it's done now - continue dead letter saving
00837   if ( mComposedMessages.isEmpty() ) {
00838     kdDebug(5006) << "Composing the message failed." << endl;
00839     return;
00840   }
00841   KMMessage *msg = mComposedMessages.first();
00842   if ( !msg ) // a bit of extra defensiveness
00843     return;
00844 
00845   kdDebug(5006) << k_funcinfo << "opening autoSaveFile " << mAutoSaveFilename
00846                 << endl;
00847   const QString filename =
00848     KMKernel::localDataPath() + "autosave/cur/" + mAutoSaveFilename;
00849   KSaveFile autoSaveFile( filename, 0600 );
00850   int status = autoSaveFile.status();
00851   kdDebug(5006) << k_funcinfo << "autoSaveFile.status() = " << status << endl;
00852   if ( status == 0 ) { // no error
00853     kdDebug(5006) << "autosaving message in " << filename << endl;
00854     int fd = autoSaveFile.handle();
00855     const DwString& msgStr = msg->asDwString();
00856     if ( ::write( fd, msgStr.data(), msgStr.length() ) == -1 )
00857       status = errno;
00858   }
00859   if ( status == 0 ) {
00860     kdDebug(5006) << k_funcinfo << "closing autoSaveFile" << endl;
00861     autoSaveFile.close();
00862     mLastAutoSaveErrno = 0;
00863   }
00864   else {
00865     kdDebug(5006) << k_funcinfo << "autosaving failed" << endl;
00866     autoSaveFile.abort();
00867     if ( status != mLastAutoSaveErrno ) {
00868       // don't show the same error message twice
00869       KMessageBox::queuedMessageBox( 0, KMessageBox::Sorry,
00870                                      i18n("Autosaving the message as %1 "
00871                                           "failed.\n"
00872                                           "Reason: %2" )
00873                                      .arg( filename, strerror( status ) ),
00874                                      i18n("Autosaving Failed") );
00875       mLastAutoSaveErrno = status;
00876     }
00877   }
00878 
00879   if ( autoSaveInterval() > 0 )
00880     updateAutoSave();
00881 }
00882 
00883 //-----------------------------------------------------------------------------
00884 void KMComposeWin::slotView(void)
00885 {
00886   if (!mDone)
00887     return; // otherwise called from rethinkFields during the construction
00888             // which is not the intended behavior
00889   int id;
00890 
00891   //This sucks awfully, but no, I cannot get an activated(int id) from
00892   // actionContainer()
00893   if (!sender()->isA("KToggleAction"))
00894     return;
00895   KToggleAction *act = (KToggleAction *) sender();
00896 
00897   if (act == mAllFieldsAction)
00898     id = 0;
00899   else if (act == mIdentityAction)
00900     id = HDR_IDENTITY;
00901   else if (act == mTransportAction)
00902     id = HDR_TRANSPORT;
00903   else if (act == mFromAction)
00904     id = HDR_FROM;
00905   else if (act == mReplyToAction)
00906     id = HDR_REPLY_TO;
00907   else if (act == mToAction)
00908     id = HDR_TO;
00909   else if (act == mCcAction)
00910     id = HDR_CC;
00911   else  if (act == mBccAction)
00912     id = HDR_BCC;
00913   else if (act == mSubjectAction)
00914     id = HDR_SUBJECT;
00915   else if (act == mFccAction)
00916     id = HDR_FCC;
00917   else if ( act == mDictionaryAction )
00918     id = HDR_DICTIONARY;
00919   else
00920    {
00921      id = 0;
00922      kdDebug(5006) << "Something is wrong (Oh, yeah?)" << endl;
00923      return;
00924    }
00925 
00926   // sanders There's a bug here this logic doesn't work if no
00927   // fields are shown and then show all fields is selected.
00928   // Instead of all fields being shown none are.
00929   if (!act->isChecked())
00930   {
00931     // hide header
00932     if (id > 0) mShowHeaders = mShowHeaders & ~id;
00933     else mShowHeaders = abs(mShowHeaders);
00934   }
00935   else
00936   {
00937     // show header
00938     if (id > 0) mShowHeaders |= id;
00939     else mShowHeaders = -abs(mShowHeaders);
00940   }
00941   rethinkFields(true);
00942 }
00943 
00944 int KMComposeWin::calcColumnWidth(int which, long allShowing, int width)
00945 {
00946   if ( (allShowing & which) == 0 )
00947     return width;
00948 
00949   QLabel *w;
00950   if ( which == HDR_IDENTITY )
00951     w = mLblIdentity;
00952   else if ( which == HDR_DICTIONARY )
00953     w = mDictionaryLabel;
00954   else if ( which == HDR_FCC )
00955     w = mLblFcc;
00956   else if ( which == HDR_TRANSPORT )
00957     w = mLblTransport;
00958   else if ( which == HDR_FROM )
00959     w = mLblFrom;
00960   else if ( which == HDR_REPLY_TO )
00961     w = mLblReplyTo;
00962   else if ( which == HDR_SUBJECT )
00963     w = mLblSubject;
00964   else
00965     return width;
00966 
00967   w->setBuddy( mEditor ); // set dummy so we don't calculate width of '&' for this label.
00968   w->adjustSize();
00969   w->show();
00970   return QMAX( width, w->sizeHint().width() );
00971 }
00972 
00973 void KMComposeWin::rethinkFields(bool fromSlot)
00974 {
00975   //This sucks even more but again no ids. sorry (sven)
00976   int mask, row, numRows;
00977   long showHeaders;
00978 
00979   if (mShowHeaders < 0)
00980     showHeaders = HDR_ALL;
00981   else
00982     showHeaders = mShowHeaders;
00983 
00984   for (mask=1,mNumHeaders=0; mask<=showHeaders; mask<<=1)
00985     if ((showHeaders&mask) != 0) mNumHeaders++;
00986 
00987   numRows = mNumHeaders + 1;
00988 
00989   delete mGrid;
00990 
00991   mGrid = new QGridLayout( mHeadersArea, numRows, 3, KDialogBase::marginHint()/2, KDialogBase::spacingHint());
00992   mGrid->setColStretch(0, 1);
00993   mGrid->setColStretch(1, 100);
00994   mGrid->setColStretch(2, 1);
00995   mGrid->setRowStretch( mNumHeaders + 1, 100 );
00996 
00997   row = 0;
00998   kdDebug(5006) << "KMComposeWin::rethinkFields" << endl;
00999   if (mRecipientsEditor)
01000     mLabelWidth = mRecipientsEditor->setFirstColumnWidth( 0 );
01001   mLabelWidth = calcColumnWidth( HDR_IDENTITY, showHeaders, mLabelWidth );
01002   mLabelWidth = calcColumnWidth( HDR_DICTIONARY, showHeaders, mLabelWidth );
01003   mLabelWidth = calcColumnWidth( HDR_FCC, showHeaders, mLabelWidth );
01004   mLabelWidth = calcColumnWidth( HDR_TRANSPORT, showHeaders, mLabelWidth );
01005   mLabelWidth = calcColumnWidth( HDR_FROM, showHeaders, mLabelWidth );
01006   mLabelWidth = calcColumnWidth( HDR_REPLY_TO, showHeaders, mLabelWidth );
01007   mLabelWidth = calcColumnWidth( HDR_SUBJECT, showHeaders, mLabelWidth );
01008 
01009   if (!fromSlot) mAllFieldsAction->setChecked(showHeaders==HDR_ALL);
01010 
01011   if (!fromSlot) mIdentityAction->setChecked(abs(mShowHeaders)&HDR_IDENTITY);
01012   rethinkHeaderLine(showHeaders,HDR_IDENTITY, row,
01013                     mLblIdentity, mIdentity, mBtnIdentity);
01014 
01015   if (!fromSlot) mDictionaryAction->setChecked(abs(mShowHeaders)&HDR_DICTIONARY);
01016   rethinkHeaderLine(showHeaders,HDR_DICTIONARY, row,
01017                     mDictionaryLabel, mDictionaryCombo, mBtnDictionary );
01018 
01019   if (!fromSlot) mFccAction->setChecked(abs(mShowHeaders)&HDR_FCC);
01020   rethinkHeaderLine(showHeaders,HDR_FCC, row,
01021                     mLblFcc, mFcc, mBtnFcc);
01022 
01023   if (!fromSlot) mTransportAction->setChecked(abs(mShowHeaders)&HDR_TRANSPORT);
01024   rethinkHeaderLine(showHeaders,HDR_TRANSPORT, row,
01025                     mLblTransport, mTransport, mBtnTransport);
01026 
01027   if (!fromSlot) mFromAction->setChecked(abs(mShowHeaders)&HDR_FROM);
01028   rethinkHeaderLine(showHeaders,HDR_FROM, row,
01029                     mLblFrom, mEdtFrom /*, mBtnFrom */ );
01030 
01031   QWidget *prevFocus = mEdtFrom;
01032 
01033   if (!fromSlot) mReplyToAction->setChecked(abs(mShowHeaders)&HDR_REPLY_TO);
01034   rethinkHeaderLine(showHeaders,HDR_REPLY_TO,row,
01035                   mLblReplyTo, mEdtReplyTo, 0);
01036   if ( showHeaders & HDR_REPLY_TO ) {
01037     prevFocus = connectFocusMoving( prevFocus, mEdtReplyTo );
01038   }
01039 
01040   if ( mClassicalRecipients ) {
01041     if (!fromSlot) mToAction->setChecked(abs(mShowHeaders)&HDR_TO);
01042     rethinkHeaderLine(showHeaders, HDR_TO, row,
01043                     mLblTo, mEdtTo, mBtnTo,
01044                     i18n("Primary Recipients"),
01045                     i18n("<qt>The email addresses you put "
01046                          "in this field receive a copy of the email.</qt>"));
01047     if ( showHeaders & HDR_TO ) {
01048       prevFocus = connectFocusMoving( prevFocus, mEdtTo );
01049     }
01050 
01051     if (!fromSlot) mCcAction->setChecked(abs(mShowHeaders)&HDR_CC);
01052     rethinkHeaderLine(showHeaders, HDR_CC, row,
01053                     mLblCc, mEdtCc, mBtnCc,
01054                     i18n("Additional Recipients"),
01055                     i18n("<qt>The email addresses you put "
01056                          "in this field receive a copy of the email. "
01057                          "Technically it is the same thing as putting all the "
01058                          "addresses in the <b>To:</b> field but differs in "
01059                          "that it usually symbolises the receiver of the "
01060                          "Carbon Copy (CC) is a listener, not the main "
01061                          "recipient.</qt>"));
01062     if ( showHeaders & HDR_CC ) {
01063       prevFocus = connectFocusMoving( prevFocus, mEdtCc );
01064     }
01065 
01066     if (!fromSlot) mBccAction->setChecked(abs(mShowHeaders)&HDR_BCC);
01067     rethinkHeaderLine(showHeaders,HDR_BCC, row,
01068                     mLblBcc, mEdtBcc, mBtnBcc,
01069                     i18n("Hidden Recipients"),
01070                     i18n("<qt>Essentially the same thing "
01071                          "as the <b>Copy To:</b> field but differs in that "
01072                          "all other recipients do not see who receives a "
01073                          "blind copy.</qt>"));
01074     if ( showHeaders & HDR_BCC ) {
01075       prevFocus = connectFocusMoving( prevFocus, mEdtBcc );
01076     }
01077   } else {
01078     mGrid->addMultiCellWidget( mRecipientsEditor, row, row, 0, 2 );
01079     ++row;
01080 
01081     if ( showHeaders & HDR_REPLY_TO ) {
01082       connect( mEdtReplyTo, SIGNAL( focusDown() ), mRecipientsEditor,
01083         SLOT( setFocusTop() ) );
01084     } else {
01085     connect( mEdtFrom, SIGNAL( focusDown() ), mRecipientsEditor,
01086       SLOT( setFocusTop() ) );
01087     }
01088     if ( showHeaders & HDR_REPLY_TO ) {
01089       connect( mRecipientsEditor, SIGNAL( focusUp() ), mEdtReplyTo, SLOT( setFocus() ) );
01090     } else {
01091       connect( mRecipientsEditor, SIGNAL( focusUp() ), mEdtFrom, SLOT( setFocus() ) );
01092     }
01093 
01094     connect( mRecipientsEditor, SIGNAL( focusDown() ), mEdtSubject,
01095       SLOT( setFocus() ) );
01096     connect( mEdtSubject, SIGNAL( focusUp() ), mRecipientsEditor,
01097       SLOT( setFocusBottom() ) );
01098 
01099     prevFocus = mRecipientsEditor;
01100   }
01101   if (!fromSlot) mSubjectAction->setChecked(abs(mShowHeaders)&HDR_SUBJECT);
01102   rethinkHeaderLine(showHeaders,HDR_SUBJECT, row,
01103                     mLblSubject, mEdtSubject);
01104   connectFocusMoving( mEdtSubject, mEditor );
01105 
01106   assert(row <= mNumHeaders + 1);
01107 
01108 
01109   if( !mAtmList.isEmpty() )
01110     mAtmListView->show();
01111   else
01112     mAtmListView->hide();
01113   resize(this->size());
01114   repaint();
01115 
01116   mHeadersArea->setMaximumHeight( mHeadersArea->sizeHint().height() );
01117   mGrid->activate();
01118   mHeadersArea->show();
01119 
01120   slotUpdateAttachActions();
01121   mIdentityAction->setEnabled(!mAllFieldsAction->isChecked());
01122   mDictionaryAction->setEnabled( !mAllFieldsAction->isChecked() );
01123   mTransportAction->setEnabled(!mAllFieldsAction->isChecked());
01124   mFromAction->setEnabled(!mAllFieldsAction->isChecked());
01125   if ( mReplyToAction ) mReplyToAction->setEnabled(!mAllFieldsAction->isChecked());
01126   if ( mToAction ) mToAction->setEnabled(!mAllFieldsAction->isChecked());
01127   if ( mCcAction ) mCcAction->setEnabled(!mAllFieldsAction->isChecked());
01128   if ( mBccAction ) mBccAction->setEnabled(!mAllFieldsAction->isChecked());
01129   mFccAction->setEnabled(!mAllFieldsAction->isChecked());
01130   mSubjectAction->setEnabled(!mAllFieldsAction->isChecked());
01131   if (mRecipientsEditor)
01132     mRecipientsEditor->setFirstColumnWidth( mLabelWidth );
01133 }
01134 
01135 QWidget *KMComposeWin::connectFocusMoving( QWidget *prev, QWidget *next )
01136 {
01137   connect( prev, SIGNAL( focusDown() ), next, SLOT( setFocus() ) );
01138   connect( next, SIGNAL( focusUp() ), prev, SLOT( setFocus() ) );
01139 
01140   return next;
01141 }
01142 
01143 //-----------------------------------------------------------------------------
01144 void KMComposeWin::rethinkHeaderLine(int aValue, int aMask, int& aRow,
01145                                      QLabel* aLbl,
01146                                      QLineEdit* aEdt, QPushButton* aBtn,
01147                                      const QString &toolTip, const QString &whatsThis )
01148 {
01149   if (aValue & aMask)
01150   {
01151     if ( !toolTip.isEmpty() )
01152       QToolTip::add( aLbl, toolTip );
01153     if ( !whatsThis.isEmpty() )
01154       QWhatsThis::add( aLbl, whatsThis );
01155     aLbl->setFixedWidth( mLabelWidth );
01156     aLbl->setBuddy(aEdt);
01157     mGrid->addWidget(aLbl, aRow, 0);
01158     aEdt->setBackgroundColor( mBackColor );
01159     aEdt->show();
01160 
01161     if (aBtn) {
01162       mGrid->addWidget(aEdt, aRow, 1);
01163 
01164       mGrid->addWidget(aBtn, aRow, 2);
01165       aBtn->show();
01166     } else {
01167       mGrid->addMultiCellWidget(aEdt, aRow, aRow, 1, 2 );
01168     }
01169     aRow++;
01170   }
01171   else
01172   {
01173     aLbl->hide();
01174     aEdt->hide();
01175     if (aBtn) aBtn->hide();
01176   }
01177 }
01178 
01179 //-----------------------------------------------------------------------------
01180 void KMComposeWin::rethinkHeaderLine(int aValue, int aMask, int& aRow,
01181                                      QLabel* aLbl,
01182                                      QComboBox* aCbx, QCheckBox* aChk)
01183 {
01184   if (aValue & aMask)
01185   {
01186     aLbl->adjustSize();
01187     aLbl->resize((int)aLbl->sizeHint().width(),aLbl->sizeHint().height() + 6);
01188     aLbl->setMinimumSize(aLbl->size());
01189     aLbl->show();
01190     aLbl->setBuddy(aCbx);
01191     mGrid->addWidget(aLbl, aRow, 0);
01192     aCbx->show();
01193     aCbx->setMinimumSize(100, aLbl->height()+2);
01194 
01195     mGrid->addWidget(aCbx, aRow, 1);
01196     if ( aChk ) {
01197       mGrid->addWidget(aChk, aRow, 2);
01198       aChk->setFixedSize(aChk->sizeHint().width(), aLbl->height());
01199       aChk->show();
01200     }
01201     aRow++;
01202   }
01203   else
01204   {
01205     aLbl->hide();
01206     aCbx->hide();
01207     if ( aChk )
01208       aChk->hide();
01209   }
01210 }
01211 
01212 //-----------------------------------------------------------------------------
01213 void KMComposeWin::getTransportMenu()
01214 {
01215   QStringList availTransports;
01216 
01217   mActNowMenu->clear();
01218   mActLaterMenu->clear();
01219   availTransports = KMail::TransportManager::transportNames();
01220   QStringList::Iterator it;
01221   int id = 0;
01222   for(it = availTransports.begin(); it != availTransports.end() ; ++it, id++)
01223   {
01224     mActNowMenu->insertItem((*it).replace("&", "&&"), id);
01225     mActLaterMenu->insertItem((*it).replace("&", "&&"), id);
01226   }
01227 }
01228 
01229 
01230 //-----------------------------------------------------------------------------
01231 void KMComposeWin::setupActions(void)
01232 {
01233   KActionMenu *actActionNowMenu, *actActionLaterMenu;
01234 
01235   if (kmkernel->msgSender()->sendImmediate()) //default == send now?
01236   {
01237     //default = send now, alternative = queue
01238     ( void )  new KAction( i18n("&Send Mail"), "mail_send", CTRL+Key_Return,
01239                         this, SLOT(slotSendNow()), actionCollection(),"send_default");
01240 
01241     // FIXME: change to mail_send_via icon when this exits.
01242     actActionNowMenu =  new KActionMenu (i18n("&Send Mail Via"), "mail_send",
01243             actionCollection(), "send_default_via" );
01244 
01245     (void) new KAction (i18n("Send &Later"), "queue", 0, this,
01246             SLOT(slotSendLater()), actionCollection(),"send_alternative");
01247     actActionLaterMenu = new KActionMenu (i18n("Send &Later Via"), "queue",
01248             actionCollection(), "send_alternative_via" );
01249 
01250   }
01251   else //no, default = send later
01252   {
01253     //default = queue, alternative = send now
01254     (void) new KAction (i18n("Send &Later"), "queue",
01255                         CTRL+Key_Return,
01256                         this, SLOT(slotSendLater()), actionCollection(),"send_default");
01257     actActionLaterMenu = new KActionMenu (i18n("Send &Later Via"), "queue",
01258             actionCollection(), "send_default_via" );
01259 
01260    ( void )  new KAction( i18n("&Send Mail"), "mail_send", 0,
01261                         this, SLOT(slotSendNow()), actionCollection(),"send_alternative");
01262 
01263     // FIXME: change to mail_send_via icon when this exits.
01264     actActionNowMenu =  new KActionMenu (i18n("&Send Mail Via"), "mail_send",
01265             actionCollection(), "send_alternative_via" );
01266 
01267   }
01268 
01269   // needed for sending "default transport"
01270   actActionNowMenu->setDelayed(true);
01271   actActionLaterMenu->setDelayed(true);
01272 
01273   connect(  actActionNowMenu, SIGNAL(  activated() ), this,
01274             SLOT( slotSendNow() ) );
01275   connect(  actActionLaterMenu, SIGNAL(  activated() ), this,
01276             SLOT( slotSendLater() ) );
01277 
01278 
01279   mActNowMenu = actActionNowMenu->popupMenu();
01280   mActLaterMenu = actActionLaterMenu->popupMenu();
01281 
01282   connect(  mActNowMenu, SIGNAL(  activated( int ) ), this,
01283             SLOT( slotSendNowVia( int ) ) );
01284   connect(  mActNowMenu, SIGNAL(  aboutToShow() ), this,
01285             SLOT( getTransportMenu() ) );
01286 
01287   connect(  mActLaterMenu, SIGNAL(  activated( int ) ), this,
01288           SLOT( slotSendLaterVia( int ) ) );
01289   connect(  mActLaterMenu, SIGNAL(  aboutToShow() ), this,
01290           SLOT( getTransportMenu() ) );
01291 
01292 
01293 
01294 
01295   (void) new KAction (i18n("Save as &Draft"), "filesave", 0,
01296                       this, SLOT(slotSaveDraft()),
01297                       actionCollection(), "save_in_drafts");
01298   (void) new KAction (i18n("Save as &Template"), "filesave", 0,
01299                       this, SLOT(slotSaveTemplate()),
01300                       actionCollection(), "save_in_templates");
01301   (void) new KAction (i18n("&Insert File..."), "fileopen", 0,
01302                       this,  SLOT(slotInsertFile()),
01303                       actionCollection(), "insert_file");
01304   mRecentAction = new KRecentFilesAction (i18n("&Insert File Recent"),
01305               "fileopen", 0,
01306               this,  SLOT(slotInsertRecentFile(const KURL&)),
01307               actionCollection(), "insert_file_recent");
01308 
01309   mRecentAction->loadEntries( KMKernel::config() );
01310 
01311   (void) new KAction (i18n("&Address Book"), "contents",0,
01312                       this, SLOT(slotAddrBook()),
01313                       actionCollection(), "addressbook");
01314   (void) new KAction (i18n("&New Composer"), "mail_new",
01315                       KStdAccel::shortcut(KStdAccel::New),
01316                       this, SLOT(slotNewComposer()),
01317                       actionCollection(), "new_composer");
01318   (void) new KAction (i18n("New Main &Window"), "window_new", 0,
01319                       this, SLOT(slotNewMailReader()),
01320                       actionCollection(), "open_mailreader");
01321 
01322   if ( !mClassicalRecipients ) {
01323     new KAction( i18n("Select &Recipients..."), CTRL + Key_L, mRecipientsEditor,
01324       SLOT( selectRecipients() ), actionCollection(), "select_recipients" );
01325     new KAction( i18n("Save &Distribution List..."), 0, mRecipientsEditor,
01326       SLOT( saveDistributionList() ), actionCollection(),
01327       "save_distribution_list" );
01328   }
01329 
01330   //KStdAction::save(this, SLOT(), actionCollection(), "save_message");
01331   KStdAction::print (this, SLOT(slotPrint()), actionCollection());
01332   KStdAction::close (this, SLOT(slotClose()), actionCollection());
01333 
01334   KStdAction::undo (this, SLOT(slotUndo()), actionCollection());
01335   KStdAction::redo (this, SLOT(slotRedo()), actionCollection());
01336   KStdAction::cut (this, SLOT(slotCut()), actionCollection());
01337   KStdAction::copy (this, SLOT(slotCopy()), actionCollection());
01338   KStdAction::pasteText (this, SLOT(slotPasteClipboard()), actionCollection());
01339   KStdAction::selectAll (this, SLOT(slotMarkAll()), actionCollection());
01340 
01341   KStdAction::find (this, SLOT(slotFind()), actionCollection());
01342   KStdAction::findNext(this, SLOT(slotSearchAgain()), actionCollection());
01343 
01344   KStdAction::replace (this, SLOT(slotReplace()), actionCollection());
01345   KStdAction::spelling (this, SLOT(slotSpellcheck()), actionCollection(), "spellcheck");
01346 
01347   mPasteQuotation = new KAction (i18n("Pa&ste as Quotation"),0,this,SLOT( slotPasteClipboardAsQuotation()),
01348                       actionCollection(), "paste_quoted");
01349 
01350   (void) new KAction (i18n("Paste as Attac&hment"),0,this,SLOT( slotPasteClipboardAsAttachment()),
01351                       actionCollection(), "paste_att");
01352 
01353   KAction * addq = new KAction(i18n("Add &Quote Characters"), 0, this,
01354               SLOT(slotAddQuotes()), actionCollection(), "tools_quote");
01355   connect( mEditor, SIGNAL(selectionAvailable(bool)),
01356            addq, SLOT(setEnabled(bool)) );
01357 
01358   KAction * remq = new KAction(i18n("Re&move Quote Characters"), 0, this,
01359               SLOT(slotRemoveQuotes()), actionCollection(), "tools_unquote");
01360   connect( mEditor, SIGNAL(selectionAvailable(bool)),
01361            remq, SLOT(setEnabled(bool)) );
01362 
01363 
01364   (void) new KAction (i18n("Cl&ean Spaces"), 0, this, SLOT(slotCleanSpace()),
01365                       actionCollection(), "clean_spaces");
01366 
01367   mFixedFontAction = new KToggleAction( i18n("Use Fi&xed Font"), 0, this,
01368                       SLOT(slotUpdateFont()), actionCollection(), "toggle_fixedfont" );
01369   mFixedFontAction->setChecked( GlobalSettings::self()->useFixedFont() );
01370 
01371   //these are checkable!!!
01372   mUrgentAction = new KToggleAction (i18n("&Urgent"), 0,
01373                                     actionCollection(),
01374                                     "urgent");
01375   mRequestMDNAction = new KToggleAction ( i18n("&Request Disposition Notification"), 0,
01376                                          actionCollection(),
01377                                          "options_request_mdn");
01378   mRequestMDNAction->setChecked(GlobalSettings::self()->requestMDN());
01379   //----- Message-Encoding Submenu
01380   mEncodingAction = new KSelectAction( i18n( "Se&t Encoding" ), "charset",
01381                                       0, this, SLOT(slotSetCharset() ),
01382                                       actionCollection(), "charsets" );
01383   mWordWrapAction = new KToggleAction (i18n("&Wordwrap"), 0,
01384                       actionCollection(), "wordwrap");
01385   mWordWrapAction->setChecked(GlobalSettings::self()->wordWrap());
01386   connect(mWordWrapAction, SIGNAL(toggled(bool)), SLOT(slotWordWrapToggled(bool)));
01387 
01388   mSnippetAction = new KToggleAction ( i18n("&Snippets"), 0,
01389                                        actionCollection(), "snippets");
01390   connect(mSnippetAction, SIGNAL(toggled(bool)), mSnippetWidget, SLOT(setShown(bool)) );
01391   mSnippetAction->setChecked( GlobalSettings::self()->showSnippetManager() );
01392 
01393   mAutoSpellCheckingAction =
01394     new KToggleAction( i18n( "&Automatic Spellchecking" ), "spellcheck", 0,
01395                        actionCollection(), "options_auto_spellchecking" );
01396   const bool spellChecking = GlobalSettings::self()->autoSpellChecking();
01397   mAutoSpellCheckingAction->setEnabled( !GlobalSettings::self()->useExternalEditor() );
01398   mAutoSpellCheckingAction->setChecked( !GlobalSettings::self()->useExternalEditor() && spellChecking );
01399   slotAutoSpellCheckingToggled( !GlobalSettings::self()->useExternalEditor() && spellChecking );
01400   connect( mAutoSpellCheckingAction, SIGNAL( toggled( bool ) ),
01401            this, SLOT( slotAutoSpellCheckingToggled( bool ) ) );
01402 
01403   QStringList encodings = KMMsgBase::supportedEncodings(true);
01404   encodings.prepend( i18n("Auto-Detect"));
01405   mEncodingAction->setItems( encodings );
01406   mEncodingAction->setCurrentItem( -1 );
01407 
01408   //these are checkable!!!
01409   markupAction = new KToggleAction (i18n("Formatting (HTML)"), 0, this,
01410                                     SLOT(slotToggleMarkup()),
01411                       actionCollection(), "html");
01412 
01413   mAllFieldsAction = new KToggleAction (i18n("&All Fields"), 0, this,
01414                                        SLOT(slotView()),
01415                                        actionCollection(), "show_all_fields");
01416   mIdentityAction = new KToggleAction (i18n("&Identity"), 0, this,
01417                                       SLOT(slotView()),
01418                                       actionCollection(), "show_identity");
01419   mDictionaryAction = new KToggleAction (i18n("&Dictionary"), 0, this,
01420                                          SLOT(slotView()),
01421                                          actionCollection(), "show_dictionary");
01422   mFccAction = new KToggleAction (i18n("&Sent-Mail Folder"), 0, this,
01423                                  SLOT(slotView()),
01424                                  actionCollection(), "show_fcc");
01425   mTransportAction = new KToggleAction (i18n("&Mail Transport"), 0, this,
01426                                       SLOT(slotView()),
01427                                       actionCollection(), "show_transport");
01428   mFromAction = new KToggleAction (i18n("&From"), 0, this,
01429                                   SLOT(slotView()),
01430                                   actionCollection(), "show_from");
01431   mReplyToAction = new KToggleAction (i18n("&Reply To"), 0, this,
01432                                        SLOT(slotView()),
01433                                        actionCollection(), "show_reply_to");
01434   if ( mClassicalRecipients ) {
01435     mToAction = new KToggleAction (i18n("&To"), 0, this,
01436                                   SLOT(slotView()),
01437                                   actionCollection(), "show_to");
01438     mCcAction = new KToggleAction (i18n("&CC"), 0, this,
01439                                   SLOT(slotView()),
01440                                   actionCollection(), "show_cc");
01441     mBccAction = new KToggleAction (i18n("&BCC"), 0, this,
01442                                    SLOT(slotView()),
01443                                    actionCollection(), "show_bcc");
01444   }
01445   mSubjectAction = new KToggleAction (i18n("S&ubject"), 0, this,
01446                                      SLOT(slotView()),
01447                                      actionCollection(), "show_subject");
01448   //end of checkable
01449 
01450   mAppendSignatureAction = new KAction (i18n("Append S&ignature"), 0, this,
01451                       SLOT(slotAppendSignature()),
01452                       actionCollection(), "append_signature");
01453   mPrependSignatureAction =  new KAction (i18n("Prepend S&ignature"), 0, this,
01454                       SLOT(slotPrependSignature()),
01455                       actionCollection(), "prepend_signature");
01456 
01457   mInsertSignatureAction =  new KAction (i18n("Insert Signature At C&ursor Position"), "edit", 0, this,
01458                       SLOT(slotInsertSignatureAtCursor()),
01459                       actionCollection(), "insert_signature_at_cursor_position");
01460 
01461   mAttachPK  = new KAction (i18n("Attach &Public Key..."), 0, this,
01462                            SLOT(slotInsertPublicKey()),
01463                            actionCollection(), "attach_public_key");
01464   mAttachMPK = new KAction (i18n("Attach &My Public Key"), 0, this,
01465                            SLOT(slotInsertMyPublicKey()),
01466                            actionCollection(), "attach_my_public_key");
01467   (void) new KAction (i18n("&Attach File..."), "attach",
01468                       0, this, SLOT(slotAttachFile()),
01469                       actionCollection(), "attach");
01470   mAttachRemoveAction = new KAction (i18n("&Remove Attachment"), 0, this,
01471                       SLOT(slotAttachRemove()),
01472                       actionCollection(), "remove");
01473   mAttachSaveAction = new KAction (i18n("&Save Attachment As..."), "filesave",0,
01474                       this, SLOT(slotAttachSave()),
01475                       actionCollection(), "attach_save");
01476   mAttachPropertiesAction = new KAction (i18n("Attachment Pr&operties"), 0, this,
01477                       SLOT(slotAttachProperties()),
01478                       actionCollection(), "attach_properties");
01479 
01480   setStandardToolBarMenuEnabled(true);
01481 
01482   KStdAction::keyBindings(this, SLOT(slotEditKeys()), actionCollection());
01483   KStdAction::configureToolbars(this, SLOT(slotEditToolbars()), actionCollection());
01484   KStdAction::preferences(kmkernel, SLOT(slotShowConfigurationDialog()), actionCollection());
01485 
01486   (void) new KAction (i18n("&Spellchecker..."), 0, this, SLOT(slotSpellcheckConfig()),
01487                       actionCollection(), "setup_spellchecker");
01488 
01489   if ( Kleo::CryptoBackendFactory::instance()->protocol( "Chiasmus" ) ) {
01490     const QString iconName =
01491       KGlobal::instance()->iconLoader()->iconPath( "chiasmus_chi", KIcon::Small );
01492     KToggleAction * a = new KToggleAction( i18n( "Encrypt Message with Chiasmus..." ),
01493                                            iconName, 0, actionCollection(),
01494                                            "encrypt_message_chiasmus" );
01495     a->setCheckedState( KGuiItem( i18n( "Encrypt Message with Chiasmus..." ), "chiencrypted" ) );
01496     mEncryptChiasmusAction = a;
01497     connect( mEncryptChiasmusAction, SIGNAL(toggled(bool)),
01498              this, SLOT(slotEncryptChiasmusToggled(bool)) );
01499   } else {
01500     mEncryptChiasmusAction = 0;
01501   }
01502 
01503   mEncryptAction = new KToggleAction (i18n("&Encrypt Message"),
01504                                      "decrypted", 0,
01505                                      actionCollection(), "encrypt_message");
01506   mSignAction = new KToggleAction (i18n("&Sign Message"),
01507                                   "signature", 0,
01508                                   actionCollection(), "sign_message");
01509   // get PGP user id for the chosen identity
01510   const KPIM::Identity & ident =
01511     kmkernel->identityManager()->identityForUoidOrDefault( mIdentity->currentIdentity() );
01512   // PENDING(marc): check the uses of this member and split it into
01513   // smime/openpgp and or enc/sign, if necessary:
01514   mLastIdentityHasSigningKey = !ident.pgpSigningKey().isEmpty() || !ident.smimeSigningKey().isEmpty();
01515   mLastIdentityHasEncryptionKey = !ident.pgpEncryptionKey().isEmpty() || !ident.smimeEncryptionKey().isEmpty();
01516 
01517   mLastEncryptActionState = false;
01518   mLastSignActionState = GlobalSettings::self()->pgpAutoSign();
01519 
01520   // "Attach public key" is only possible if OpenPGP support is available:
01521   mAttachPK->setEnabled( Kleo::CryptoBackendFactory::instance()->openpgp() );
01522 
01523   // "Attach my public key" is only possible if OpenPGP support is
01524   // available and the user specified his key for the current identity:
01525   mAttachMPK->setEnabled( Kleo::CryptoBackendFactory::instance()->openpgp() &&
01526               !ident.pgpEncryptionKey().isEmpty() );
01527 
01528   if ( !Kleo::CryptoBackendFactory::instance()->openpgp() && !Kleo::CryptoBackendFactory::instance()->smime() ) {
01529     // no crypto whatsoever
01530     mEncryptAction->setEnabled( false );
01531     setEncryption( false );
01532     mSignAction->setEnabled( false );
01533     setSigning( false );
01534   } else {
01535     const bool canOpenPGPSign = Kleo::CryptoBackendFactory::instance()->openpgp()
01536       && !ident.pgpSigningKey().isEmpty();
01537     const bool canSMIMESign = Kleo::CryptoBackendFactory::instance()->smime()
01538       && !ident.smimeSigningKey().isEmpty();
01539 
01540     setEncryption( false );
01541     setSigning( ( canOpenPGPSign || canSMIMESign ) && GlobalSettings::self()->pgpAutoSign() );
01542   }
01543 
01544   connect(mEncryptAction, SIGNAL(toggled(bool)),
01545                          SLOT(slotEncryptToggled( bool )));
01546   connect(mSignAction,    SIGNAL(toggled(bool)),
01547                          SLOT(slotSignToggled(    bool )));
01548 
01549   QStringList l;
01550   for ( int i = 0 ; i < numCryptoMessageFormats ; ++i )
01551     l.push_back( Kleo::cryptoMessageFormatToLabel( cryptoMessageFormats[i] ) );
01552 
01553   mCryptoModuleAction = new KSelectAction( i18n( "&Cryptographic Message Format" ), 0,
01554                        this, SLOT(slotSelectCryptoModule()),
01555                        actionCollection(), "options_select_crypto" );
01556   mCryptoModuleAction->setItems( l );
01557   mCryptoModuleAction->setCurrentItem( format2cb( ident.preferredCryptoMessageFormat() ) );
01558   mCryptoModuleAction->setToolTip( i18n( "Select a cryptographic format for this message" ) );
01559   slotSelectCryptoModule( true /* initialize */ );
01560 
01561   QStringList styleItems;
01562   styleItems << i18n( "Standard" );
01563   styleItems << i18n( "Bulleted List (Disc)" );
01564   styleItems << i18n( "Bulleted List (Circle)" );
01565   styleItems << i18n( "Bulleted List (Square)" );
01566   styleItems << i18n( "Ordered List (Decimal)" );
01567   styleItems << i18n( "Ordered List (Alpha lower)" );
01568   styleItems << i18n( "Ordered List (Alpha upper)" );
01569 
01570   listAction = new KSelectAction( i18n( "Select Style" ), 0, actionCollection(),
01571                                  "text_list" );
01572   listAction->setItems( styleItems );
01573   listAction->setToolTip( i18n( "Select a list style" ) );
01574   connect( listAction, SIGNAL( activated( const QString& ) ),
01575            SLOT( slotListAction( const QString& ) ) );
01576   fontAction = new KFontAction( "Select Font", 0, actionCollection(),
01577                                "text_font" );
01578   fontAction->setToolTip( i18n( "Select a font" ) );
01579   connect( fontAction, SIGNAL( activated( const QString& ) ),
01580            SLOT( slotFontAction( const QString& ) ) );
01581   fontSizeAction = new KFontSizeAction( "Select Size", 0, actionCollection(),
01582                                        "text_size" );
01583   fontSizeAction->setToolTip( i18n( "Select a font size" ) );
01584   connect( fontSizeAction, SIGNAL( fontSizeChanged( int ) ),
01585            SLOT( slotSizeAction( int ) ) );
01586 
01587   alignLeftAction = new KToggleAction (i18n("Align Left"), "text_left", 0,
01588                       this, SLOT(slotAlignLeft()), actionCollection(),
01589                       "align_left");
01590   alignLeftAction->setChecked( true );
01591   alignRightAction = new KToggleAction (i18n("Align Right"), "text_right", 0,
01592                       this, SLOT(slotAlignRight()), actionCollection(),
01593                       "align_right");
01594   alignCenterAction = new KToggleAction (i18n("Align Center"), "text_center", 0,
01595                        this, SLOT(slotAlignCenter()), actionCollection(),
01596                        "align_center");
01597   textBoldAction = new KToggleAction( i18n("&Bold"), "text_bold", CTRL+Key_B,
01598                                      this, SLOT(slotTextBold()),
01599                                      actionCollection(), "text_bold");
01600   textItalicAction = new KToggleAction( i18n("&Italic"), "text_italic", CTRL+Key_I,
01601                                        this, SLOT(slotTextItalic()),
01602                                        actionCollection(), "text_italic");
01603   textUnderAction = new KToggleAction( i18n("&Underline"), "text_under", CTRL+Key_U,
01604                                      this, SLOT(slotTextUnder()),
01605                                      actionCollection(), "text_under");
01606   actionFormatReset = new KAction( i18n( "Reset Font Settings" ), "eraser", 0,
01607                                      this, SLOT( slotFormatReset() ),
01608                                      actionCollection(), "format_reset");
01609   actionFormatColor = new KAction( i18n( "Text Color..." ), "colorize", 0,
01610                                      this, SLOT( slotTextColor() ),
01611                                      actionCollection(), "format_color");
01612 
01613   //  editorFocusChanged(false);
01614   createGUI("kmcomposerui.rc");
01615 
01616   connect( toolBar("htmlToolBar"), SIGNAL( visibilityChanged(bool) ),
01617            this, SLOT( htmlToolBarVisibilityChanged(bool) ) );
01618 
01619   // In Kontact, this entry would read "Configure Kontact", but bring
01620   // up KMail's config dialog. That's sensible, though, so fix the label.
01621   KAction* configureAction = actionCollection()->action("options_configure" );
01622   if ( configureAction )
01623     configureAction->setText( i18n("Configure KMail..." ) );
01624 }
01625 
01626 //-----------------------------------------------------------------------------
01627 void KMComposeWin::setupStatusBar(void)
01628 {
01629   statusBar()->insertItem("", 0, 1);
01630   statusBar()->setItemAlignment(0, AlignLeft | AlignVCenter);
01631 
01632   statusBar()->insertItem(i18n( " Spellcheck: %1 ").arg( "   " ), 3, 0, true );
01633   statusBar()->insertItem(i18n( " Column: %1 ").arg("     "), 2, 0, true);
01634   statusBar()->insertItem(i18n( " Line: %1 ").arg("     "), 1, 0, true);
01635 }
01636 
01637 
01638 //-----------------------------------------------------------------------------
01639 void KMComposeWin::updateCursorPosition()
01640 {
01641   int col,line;
01642   QString temp;
01643   line = mEditor->currentLine();
01644   col = mEditor->currentColumn();
01645   temp = i18n(" Line: %1 ").arg(line+1);
01646   statusBar()->changeItem(temp,1);
01647   temp = i18n(" Column: %1 ").arg(col+1);
01648   statusBar()->changeItem(temp,2);
01649 }
01650 
01651 
01652 //-----------------------------------------------------------------------------
01653 void KMComposeWin::setupEditor(void)
01654 {
01655   //QPopupMenu* menu;
01656   mEditor->setModified(false);
01657   QFontMetrics fm(mBodyFont);
01658   mEditor->setTabStopWidth(fm.width(QChar(' ')) * 8);
01659   //mEditor->setFocusPolicy(QWidget::ClickFocus);
01660 
01661   slotWordWrapToggled( GlobalSettings::self()->wordWrap() );
01662 
01663   // Font setup
01664   slotUpdateFont();
01665 
01666   /* installRBPopup() is broken in kdelibs, we should wait for
01667           the new klibtextedit (dnaber, 2002-01-01)
01668   menu = new QPopupMenu(this);
01669   //#ifdef BROKEN
01670   menu->insertItem(i18n("Undo"),mEditor,
01671                    SLOT(undo()), KStdAccel::shortcut(KStdAccel::Undo));
01672   menu->insertItem(i18n("Redo"),mEditor,
01673                    SLOT(redo()), KStdAccel::shortcut(KStdAccel::Redo));
01674   menu->insertSeparator();
01675   //#endif //BROKEN
01676   menu->insertItem(i18n("Cut"), this, SLOT(slotCut()));
01677   menu->insertItem(i18n("Copy"), this, SLOT(slotCopy()));
01678   menu->insertItem(i18n("Paste"), this, SLOT(slotPasteClipboard()));
01679   menu->insertItem(i18n("Mark All"),this, SLOT(slotMarkAll()));
01680   menu->insertSeparator();
01681   menu->insertItem(i18n("Find..."), this, SLOT(slotFind()));
01682   menu->insertItem(i18n("Replace..."), this, SLOT(slotReplace()));
01683   menu->insertSeparator();
01684   menu->insertItem(i18n("Fixed Font Widths"), this, SLOT(slotUpdateFont()));
01685   mEditor->installRBPopup(menu);
01686   */
01687   updateCursorPosition();
01688   connect(mEditor,SIGNAL(CursorPositionChanged()),SLOT(updateCursorPosition()));
01689   connect( mEditor, SIGNAL( currentFontChanged( const QFont & ) ),
01690           this, SLOT( fontChanged( const QFont & ) ) );
01691   connect( mEditor, SIGNAL( currentAlignmentChanged( int ) ),
01692           this, SLOT( alignmentChanged( int ) ) );
01693 
01694 }
01695 
01696 
01697 //-----------------------------------------------------------------------------
01698 static QString cleanedUpHeaderString( const QString & s )
01699 {
01700   // remove invalid characters from the header strings
01701   QString res( s );
01702   res.replace( '\r', "" );
01703   res.replace( '\n', " " );
01704   return res.stripWhiteSpace();
01705 }
01706 
01707 //-----------------------------------------------------------------------------
01708 QString KMComposeWin::subject() const
01709 {
01710   return cleanedUpHeaderString( mEdtSubject->text() );
01711 }
01712 
01713 //-----------------------------------------------------------------------------
01714 QString KMComposeWin::to() const
01715 {
01716   if ( mEdtTo ) {
01717     return cleanedUpHeaderString( mEdtTo->text() );
01718   } else if ( mRecipientsEditor ) {
01719     return mRecipientsEditor->recipientString( Recipient::To );
01720   } else {
01721     return QString::null;
01722   }
01723 }
01724 
01725 //-----------------------------------------------------------------------------
01726 QString KMComposeWin::cc() const
01727 {
01728   if ( mEdtCc && !mEdtCc->isHidden() ) {
01729     return cleanedUpHeaderString( mEdtCc->text() );
01730   } else if ( mRecipientsEditor ) {
01731     return mRecipientsEditor->recipientString( Recipient::Cc );
01732   } else {
01733     return QString::null;
01734   }
01735 }
01736 
01737 //-----------------------------------------------------------------------------
01738 QString KMComposeWin::bcc() const
01739 {
01740   if ( mEdtBcc && !mEdtBcc->isHidden() ) {
01741     return cleanedUpHeaderString( mEdtBcc->text() );
01742   } else if ( mRecipientsEditor ) {
01743     return mRecipientsEditor->recipientString( Recipient::Bcc );
01744   } else {
01745     return QString::null;
01746   }
01747 }
01748 
01749 //-----------------------------------------------------------------------------
01750 QString KMComposeWin::from() const
01751 {
01752   return cleanedUpHeaderString( mEdtFrom->text() );
01753 }
01754 
01755 //-----------------------------------------------------------------------------
01756 QString KMComposeWin::replyTo() const
01757 {
01758   if ( mEdtReplyTo ) {
01759     return cleanedUpHeaderString( mEdtReplyTo->text() );
01760   } else {
01761     return QString::null;
01762   }
01763 }
01764 
01765 //-----------------------------------------------------------------------------
01766 void KMComposeWin::verifyWordWrapLengthIsAdequate(const QString &body)
01767 {
01768   int maxLineLength = 0;
01769   int curPos;
01770   int oldPos = 0;
01771   if (mEditor->QTextEdit::wordWrap() == QTextEdit::FixedColumnWidth) {
01772     for (curPos = 0; curPos < (int)body.length(); ++curPos)
01773         if (body[curPos] == '\n') {
01774           if ((curPos - oldPos) > maxLineLength)
01775             maxLineLength = curPos - oldPos;
01776           oldPos = curPos;
01777         }
01778     if ((curPos - oldPos) > maxLineLength)
01779       maxLineLength = curPos - oldPos;
01780     if (mEditor->wrapColumnOrWidth() < maxLineLength) // column
01781       mEditor->setWrapColumnOrWidth(maxLineLength);
01782   }
01783 }
01784 
01785 //-----------------------------------------------------------------------------
01786 void KMComposeWin::decryptOrStripOffCleartextSignature( QCString& body )
01787 {
01788   QPtrList<Kpgp::Block> pgpBlocks;
01789   QStrList nonPgpBlocks;
01790   if( Kpgp::Module::prepareMessageForDecryption( body,
01791                                                  pgpBlocks, nonPgpBlocks ) )
01792   {
01793     // Only decrypt/strip off the signature if there is only one OpenPGP
01794     // block in the message
01795     if( pgpBlocks.count() == 1 )
01796     {
01797       Kpgp::Block* block = pgpBlocks.first();
01798       if( ( block->type() == Kpgp::PgpMessageBlock ) ||
01799           ( block->type() == Kpgp::ClearsignedBlock ) )
01800       {
01801         if( block->type() == Kpgp::PgpMessageBlock )
01802           // try to decrypt this OpenPGP block
01803           block->decrypt();
01804         else
01805           // strip off the signature
01806           block->verify();
01807 
01808         body = nonPgpBlocks.first()
01809              + block->text()
01810              + nonPgpBlocks.last();
01811       }
01812     }
01813   }
01814 }
01815 
01816 //-----------------------------------------------------------------------------
01817 void KMComposeWin::setTransport( const QString & transport )
01818 {
01819   kdDebug(5006) << "KMComposeWin::setTransport( \"" << transport << "\" )" << endl;
01820   // Don't change the transport combobox if transport is empty
01821   if ( transport.isEmpty() )
01822     return;
01823 
01824   bool transportFound = false;
01825   for ( int i = 0; i < mTransport->count(); ++i ) {
01826     if ( mTransport->text(i) == transport ) {
01827       transportFound = true;
01828       mTransport->setCurrentItem(i);
01829       kdDebug(5006) << "transport found, it's no. " << i << " in the list" << endl;
01830       break;
01831     }
01832   }
01833   if ( !transportFound ) { // unknown transport
01834     kdDebug(5006) << "unknown transport \"" << transport << "\"" << endl;
01835     if ( transport.startsWith("smtp://") || transport.startsWith("smtps://") ||
01836          transport.startsWith("file://") ) {
01837       // set custom transport
01838       mTransport->setEditText( transport );
01839     }
01840     else {
01841       // neither known nor custom transport -> use default transport
01842       mTransport->setCurrentText( GlobalSettings::self()->defaultTransport() );
01843     }
01844   }
01845 }
01846 
01847 //-----------------------------------------------------------------------------
01848 void KMComposeWin::setMsg(KMMessage* newMsg, bool mayAutoSign,
01849                           bool allowDecryption, bool isModified)
01850 {
01851   //assert(newMsg!=0);
01852   if(!newMsg)
01853     {
01854       kdDebug(5006) << "KMComposeWin::setMsg() : newMsg == 0!" << endl;
01855       return;
01856     }
01857   mMsg = newMsg;
01858   KPIM::IdentityManager * im = kmkernel->identityManager();
01859 
01860   mEdtFrom->setText(mMsg->from());
01861   mEdtReplyTo->setText(mMsg->replyTo());
01862   if ( mClassicalRecipients ) {
01863     mEdtTo->setText(mMsg->to());
01864     mEdtCc->setText(mMsg->cc());
01865     mEdtBcc->setText(mMsg->bcc());
01866   } else {
01867     mRecipientsEditor->setRecipientString( mMsg->to(), Recipient::To );
01868     mRecipientsEditor->setRecipientString( mMsg->cc(), Recipient::Cc );
01869     mRecipientsEditor->setRecipientString( mMsg->bcc(), Recipient::Bcc );
01870     mRecipientsEditor->setFocusBottom();
01871   }
01872   mEdtSubject->setText(mMsg->subject());
01873 
01874   const bool stickyIdentity = mBtnIdentity->isChecked() && !mIgnoreStickyFields;
01875   const bool messageHasIdentity = !newMsg->headerField("X-KMail-Identity").isEmpty();
01876   if (!stickyIdentity && messageHasIdentity)
01877     mId = newMsg->headerField("X-KMail-Identity").stripWhiteSpace().toUInt();
01878 
01879   // don't overwrite the header values with identity specific values
01880   // unless the identity is sticky
01881   if ( !stickyIdentity ) {
01882     disconnect(mIdentity,SIGNAL(identityChanged(uint)),
01883                this, SLOT(slotIdentityChanged(uint)));
01884   }
01885   // load the mId into the gui, sticky or not, without emitting
01886   mIdentity->setCurrentIdentity( mId );
01887   const uint idToApply = mId;
01888   if ( !stickyIdentity ) {
01889     connect(mIdentity,SIGNAL(identityChanged(uint)),
01890             this, SLOT(slotIdentityChanged(uint)));
01891   }  else {
01892     // load the message's state into the mId, without applying it to the gui
01893     // that's so we can detect that the id changed (because a sticky was set)
01894     // on apply()
01895     if ( messageHasIdentity )
01896       mId = newMsg->headerField("X-KMail-Identity").stripWhiteSpace().toUInt();
01897     else
01898       mId = im->defaultIdentity().uoid();
01899   }
01900   // manually load the identity's value into the fields; either the one from the
01901   // messge, where appropriate, or the one from the sticky identity. What's in
01902   // mId might have changed meanwhile, thus the save value
01903   slotIdentityChanged( idToApply );
01904 
01905   const KPIM::Identity & ident = im->identityForUoid( mIdentity->currentIdentity() );
01906 
01907   // check for the presence of a DNT header, indicating that MDN's were
01908   // requested
01909   QString mdnAddr = newMsg->headerField("Disposition-Notification-To");
01910   mRequestMDNAction->setChecked( ( !mdnAddr.isEmpty() &&
01911                                   im->thatIsMe( mdnAddr ) ) ||
01912                                   GlobalSettings::self()->requestMDN() );
01913 
01914   // check for presence of a priority header, indicating urgent mail:
01915   mUrgentAction->setChecked( newMsg->isUrgent() );
01916 
01917   if (!ident.isXFaceEnabled() || ident.xface().isEmpty())
01918     mMsg->removeHeaderField("X-Face");
01919   else
01920   {
01921     QString xface = ident.xface();
01922     if (!xface.isEmpty())
01923     {
01924       int numNL = ( xface.length() - 1 ) / 70;
01925       for ( int i = numNL; i > 0; --i )
01926         xface.insert( i*70, "\n\t" );
01927       mMsg->setHeaderField("X-Face", xface);
01928     }
01929   }
01930 
01931   // enable/disable encryption if the message was/wasn't encrypted
01932   switch ( mMsg->encryptionState() ) {
01933     case KMMsgFullyEncrypted: // fall through
01934     case KMMsgPartiallyEncrypted:
01935       mLastEncryptActionState = true;
01936       break;
01937     case KMMsgNotEncrypted:
01938       mLastEncryptActionState = false;
01939       break;
01940     default: // nothing
01941       break;
01942   }
01943 
01944   // enable/disable signing if the message was/wasn't signed
01945   switch ( mMsg->signatureState() ) {
01946     case KMMsgFullySigned: // fall through
01947     case KMMsgPartiallySigned:
01948       mLastSignActionState = true;
01949       break;
01950     case KMMsgNotSigned:
01951       mLastSignActionState = false;
01952       break;
01953     default: // nothing
01954       break;
01955   }
01956 
01957   // if these headers are present, the state of the message should be overruled
01958   if ( mMsg->headers().FindField( "X-KMail-SignatureActionEnabled" ) )
01959     mLastSignActionState = (mMsg->headerField( "X-KMail-SignatureActionEnabled" ) == "true");
01960   if ( mMsg->headers().FindField( "X-KMail-EncryptActionEnabled" ) )
01961     mLastEncryptActionState = (mMsg->headerField( "X-KMail-EncryptActionEnabled" ) == "true");
01962   if ( mMsg->headers().FindField( "X-KMail-CryptoMessageFormat" ) )
01963     mCryptoModuleAction->setCurrentItem( format2cb( static_cast<Kleo::CryptoMessageFormat>(
01964                     mMsg->headerField( "X-KMail-CryptoMessageFormat" ).toInt() ) ) );
01965 
01966   mLastIdentityHasSigningKey = !ident.pgpSigningKey().isEmpty() || !ident.smimeSigningKey().isEmpty();
01967   mLastIdentityHasEncryptionKey = !ident.pgpEncryptionKey().isEmpty() || !ident.smimeEncryptionKey().isEmpty();
01968 
01969   if ( Kleo::CryptoBackendFactory::instance()->openpgp() || Kleo::CryptoBackendFactory::instance()->smime() ) {
01970     const bool canOpenPGPSign = Kleo::CryptoBackendFactory::instance()->openpgp()
01971       && !ident.pgpSigningKey().isEmpty();
01972     const bool canSMIMESign = Kleo::CryptoBackendFactory::instance()->smime()
01973       && !ident.smimeSigningKey().isEmpty();
01974 
01975     setEncryption( mLastEncryptActionState );
01976     setSigning( ( canOpenPGPSign || canSMIMESign ) && mLastSignActionState );
01977   }
01978   slotUpdateSignatureAndEncrypionStateIndicators();
01979 
01980   // "Attach my public key" is only possible if the user uses OpenPGP
01981   // support and he specified his key:
01982   mAttachMPK->setEnabled( Kleo::CryptoBackendFactory::instance()->openpgp() &&
01983               !ident.pgpEncryptionKey().isEmpty() );
01984 
01985   QString transport = newMsg->headerField("X-KMail-Transport");
01986   const bool stickyTransport = mBtnTransport->isChecked() && !mIgnoreStickyFields;
01987   if (!stickyTransport && !transport.isEmpty())
01988     setTransport( transport );
01989 
01990   if (!mBtnFcc->isChecked())
01991   {
01992     if (!mMsg->fcc().isEmpty())
01993       setFcc(mMsg->fcc());
01994     else
01995       setFcc(ident.fcc());
01996   }
01997 
01998   const bool stickyDictionary = mBtnDictionary->isChecked() && !mIgnoreStickyFields;
01999   if ( !stickyDictionary ) {
02000     mDictionaryCombo->setCurrentByDictionary( ident.dictionary() );
02001   }
02002 
02003   partNode * root = partNode::fromMessage( mMsg );
02004 
02005   KMail::ObjectTreeParser otp; // all defaults are ok
02006   otp.parseObjectTree( root );
02007 
02008   KMail::AttachmentCollector ac;
02009   ac.collectAttachmentsFrom( root );
02010 
02011   for ( std::vector<partNode*>::const_iterator it = ac.attachments().begin() ; it != ac.attachments().end() ; ++it )
02012     addAttach( new KMMessagePart( (*it)->msgPart() ) );
02013 
02014   mEditor->setText( otp.textualContent() );
02015   mCharset = otp.textualContentCharset();
02016   if ( partNode * n = root->findType( DwMime::kTypeText, DwMime::kSubtypeHtml ) )
02017     if ( partNode * p = n->parentNode() )
02018       if ( p->hasType( DwMime::kTypeMultipart ) &&
02019            p->hasSubType( DwMime::kSubtypeAlternative ) )
02020         if ( mMsg->headerField( "X-KMail-Markup" ) == "true" ) {
02021           toggleMarkup( true );
02022 
02023           // get cte decoded body part
02024           mCharset = n->msgPart().charset();
02025           QCString bodyDecoded = n->msgPart().bodyDecoded();
02026 
02027           // respect html part charset
02028           const QTextCodec *codec = KMMsgBase::codecForName( mCharset );
02029           if ( codec ) {
02030             mEditor->setText( codec->toUnicode( bodyDecoded ) );
02031           } else {
02032             mEditor->setText( QString::fromLocal8Bit( bodyDecoded ) );
02033           }
02034         }
02035 
02036   if ( mCharset.isEmpty() )
02037     mCharset = mMsg->charset();
02038   if ( mCharset.isEmpty() )
02039     mCharset = mDefCharset;
02040   setCharset( mCharset );
02041 
02042   /* Handle the special case of non-mime mails */
02043   if ( mMsg->numBodyParts() == 0 && otp.textualContent().isEmpty() ) {
02044     mCharset=mMsg->charset();
02045     if ( mCharset.isEmpty() ||  mCharset == "default" )
02046       mCharset = mDefCharset;
02047 
02048     QCString bodyDecoded = mMsg->bodyDecoded();
02049 
02050     if( allowDecryption )
02051       decryptOrStripOffCleartextSignature( bodyDecoded );
02052 
02053     const QTextCodec *codec = KMMsgBase::codecForName(mCharset);
02054     if (codec) {
02055       mEditor->setText(codec->toUnicode(bodyDecoded));
02056     } else
02057       mEditor->setText(QString::fromLocal8Bit(bodyDecoded));
02058   }
02059 #ifdef BROKEN_FOR_OPAQUE_SIGNED_OR_ENCRYPTED_MAILS
02060   const int num = mMsg->numBodyParts();
02061   kdDebug(5006) << "KMComposeWin::setMsg() mMsg->numBodyParts="
02062                 << mMsg->numBodyParts() << endl;
02063 
02064   if ( num > 0 ) {
02065     KMMessagePart bodyPart;
02066     int firstAttachment = 0;
02067 
02068     mMsg->bodyPart(1, &bodyPart);
02069     if ( bodyPart.typeStr().lower() == "text" &&
02070          bodyPart.subtypeStr().lower() == "html" ) {
02071       // check whether we are inside a mp/al body part
02072       partNode *root = partNode::fromMessage( mMsg );
02073       partNode *node = root->findType( DwMime::kTypeText,
02074                                        DwMime::kSubtypeHtml );
02075       if ( node && node->parentNode() &&
02076            node->parentNode()->hasType( DwMime::kTypeMultipart ) &&
02077            node->parentNode()->hasSubType( DwMime::kSubtypeAlternative ) ) {
02078         // we have a mp/al body part with a text and an html body
02079       kdDebug(5006) << "KMComposeWin::setMsg() : text/html found" << endl;
02080       firstAttachment = 2;
02081         if ( mMsg->headerField( "X-KMail-Markup" ) == "true" )
02082           toggleMarkup( true );
02083       }
02084       delete root; root = 0;
02085     }
02086     if ( firstAttachment == 0 ) {
02087         mMsg->bodyPart(0, &bodyPart);
02088         if ( bodyPart.typeStr().lower() == "text" ) {
02089           // we have a mp/mx body with a text body
02090         kdDebug(5006) << "KMComposeWin::setMsg() : text/* found" << endl;
02091           firstAttachment = 1;
02092         }
02093       }
02094 
02095     if ( firstAttachment != 0 ) // there's text to show
02096     {
02097       mCharset = bodyPart.charset();
02098       if ( mCharset.isEmpty() || mCharset == "default" )
02099         mCharset = mDefCharset;
02100 
02101       QCString bodyDecoded = bodyPart.bodyDecoded();
02102 
02103       if( allowDecryption )
02104         decryptOrStripOffCleartextSignature( bodyDecoded );
02105 
02106       // As nobody seems to know the purpose of the following line and
02107       // as it breaks word wrapping of long lines if drafts with attachments
02108       // are opened for editting in the composer (cf. Bug#41102) I comment it
02109       // out. Ingo, 2002-04-21
02110       //verifyWordWrapLengthIsAdequate(bodyDecoded);
02111 
02112       const QTextCodec *codec = KMMsgBase::codecForName(mCharset);
02113       if (codec)
02114         mEditor->setText(codec->toUnicode(bodyDecoded));
02115       else
02116         mEditor->setText(QString::fromLocal8Bit(bodyDecoded));
02117       //mEditor->insertLine("\n", -1); <-- why ?
02118     } else mEditor->setText("");
02119     for( int i = firstAttachment; i < num; ++i )
02120     {
02121       KMMessagePart *msgPart = new KMMessagePart;
02122       mMsg->bodyPart(i, msgPart);
02123       QCString mimeType = msgPart->typeStr().lower() + '/'
02124                         + msgPart->subtypeStr().lower();
02125       // don't add the detached signature as attachment when editting a
02126       // PGP/MIME signed message
02127       if( mimeType != "application/pgp-signature" ) {
02128         addAttach(msgPart);
02129       }
02130     }
02131   } else{
02132     mCharset=mMsg->charset();
02133     if ( mCharset.isEmpty() ||  mCharset == "default" )
02134       mCharset = mDefCharset;
02135 
02136     QCString bodyDecoded = mMsg->bodyDecoded();
02137 
02138     if( allowDecryption )
02139       decryptOrStripOffCleartextSignature( bodyDecoded );
02140 
02141     const QTextCodec *codec = KMMsgBase::codecForName(mCharset);
02142     if (codec) {
02143       mEditor->setText(codec->toUnicode(bodyDecoded));
02144     } else
02145       mEditor->setText(QString::fromLocal8Bit(bodyDecoded));
02146   }
02147 
02148   setCharset(mCharset);
02149 #endif // BROKEN_FOR_OPAQUE_SIGNED_OR_ENCRYPTED_MAILS
02150 
02151   if( (GlobalSettings::self()->autoTextSignature()=="auto") && mayAutoSign ) {
02152     //
02153     // Espen 2000-05-16
02154     // Delay the signature appending. It may start a fileseletor.
02155     // Not user friendy if this modal fileseletor opens before the
02156     // composer.
02157     //
02158     //QTimer::singleShot( 200, this, SLOT(slotAppendSignature()) );
02159       if ( GlobalSettings::self()->prependSignature() ) {
02160         QTimer::singleShot( 0, this, SLOT(slotPrependSignature()) );
02161       } else {
02162         QTimer::singleShot( 0, this, SLOT(slotAppendSignature()) );
02163       }
02164   }
02165 
02166   if ( mMsg->getCursorPos() > 0 ) {
02167     // The message has a cursor position explicitly set, so avoid
02168     // changing it when appending the signature.
02169     mPreserveUserCursorPosition = true;
02170   }
02171   setModified( isModified );
02172 
02173   // do this even for new messages
02174   mEditor->setCursorPositionFromStart( (unsigned int) mMsg->getCursorPos() );
02175 
02176   // honor "keep reply in this folder" setting even when the identity is changed later on
02177   mPreventFccOverwrite = ( !newMsg->fcc().isEmpty() && ident.fcc() != newMsg->fcc() );
02178 }
02179 
02180 
02181 //-----------------------------------------------------------------------------
02182 void KMComposeWin::setFcc( const QString &idString )
02183 {
02184   // check if the sent-mail folder still exists
02185   if ( ! idString.isEmpty() && kmkernel->findFolderById( idString ) ) {
02186     mFcc->setFolder( idString );
02187   } else {
02188     mFcc->setFolder( kmkernel->sentFolder() );
02189   }
02190 }
02191 
02192 
02193 //-----------------------------------------------------------------------------
02194 bool KMComposeWin::isModified() const
02195 {
02196   return ( mEditor->isModified() ||
02197            mEdtFrom->edited() ||
02198            ( mEdtReplyTo && mEdtReplyTo->edited() ) ||
02199            ( mEdtTo && mEdtTo->edited() ) ||
02200            ( mEdtCc && mEdtCc->edited() ) ||
02201            ( mEdtBcc && mEdtBcc->edited() ) ||
02202            ( mRecipientsEditor && mRecipientsEditor->isModified() ) ||
02203            mEdtSubject->edited() ||
02204            mAtmModified ||
02205            ( mTransport->lineEdit() && mTransport->lineEdit()->edited() ) );
02206 }
02207 
02208 
02209 //-----------------------------------------------------------------------------
02210 void KMComposeWin::setModified( bool modified )
02211 {
02212   mEditor->setModified( modified );
02213   if ( !modified ) {
02214     mEdtFrom->setEdited( false );
02215     if ( mEdtReplyTo ) mEdtReplyTo->setEdited( false );
02216     if ( mEdtTo ) mEdtTo->setEdited( false );
02217     if ( mEdtCc ) mEdtCc->setEdited( false );
02218     if ( mEdtBcc ) mEdtBcc->setEdited( false );
02219     if ( mRecipientsEditor ) mRecipientsEditor->clearModified();
02220     mEdtSubject->setEdited( false );
02221     mAtmModified =  false ;
02222     if ( mTransport->lineEdit() )
02223       mTransport->lineEdit()->setEdited( false );
02224   }
02225 }
02226 
02227 
02228 //-----------------------------------------------------------------------------
02229 bool KMComposeWin::queryClose ()
02230 {
02231   if ( !mEditor->checkExternalEditorFinished() )
02232     return false;
02233   if ( kmkernel->shuttingDown() || kapp->sessionSaving() )
02234     return true;
02235   if ( mComposer && mComposer->isPerformingSignOperation() ) // since the non-gpg-agent gpg plugin gets a passphrase using QDialog::exec()
02236     return false;                                            // the user can try to close the window, which destroys mComposer mid-call.
02237 
02238   if ( isModified() ) {
02239     bool istemplate = ( mFolder!=0 && mFolder->isTemplates() );
02240     const QString savebut = ( istemplate ?
02241                               i18n("Re&save as Template") :
02242                               i18n("&Save as Draft") );
02243     const QString savetext = ( istemplate ?
02244                                i18n("Resave this message in the Templates folder. "
02245                                     "It can then be used at a later time.") :
02246                                i18n("Save this message in the Drafts folder. "
02247                                     "It can then be edited and sent at a later time.") );
02248 
02249     const int rc = KMessageBox::warningYesNoCancel( this,
02250            i18n("Do you want to save the message for later or discard it?"),
02251            i18n("Close Composer"),
02252            KGuiItem(savebut, "filesave", QString::null, savetext),
02253            KStdGuiItem::discard() );
02254     if ( rc == KMessageBox::Cancel )
02255       return false;
02256     else if ( rc == KMessageBox::Yes ) {
02257       // doSend will close the window. Just return false from this method
02258       if ( istemplate ) {
02259         slotSaveTemplate();
02260       } else {
02261         slotSaveDraft();
02262       }
02263       return false;
02264     }
02265   }
02266   cleanupAutoSave();
02267   return true;
02268 }
02269 
02270 //-----------------------------------------------------------------------------
02271 bool KMComposeWin::userForgotAttachment()
02272 {
02273   bool checkForForgottenAttachments =
02274     mCheckForForgottenAttachments && GlobalSettings::self()->showForgottenAttachmentWarning();
02275 
02276   if ( !checkForForgottenAttachments || ( mAtmList.count() > 0 ) )
02277     return false;
02278 
02279 
02280   QStringList attachWordsList = GlobalSettings::self()->attachmentKeywords();
02281 
02282   if ( attachWordsList.isEmpty() ) {
02283     // default value (FIXME: this is duplicated in configuredialog.cpp)
02284     attachWordsList << QString::fromLatin1("attachment")
02285                     << QString::fromLatin1("attached");
02286     if ( QString::fromLatin1("attachment") != i18n("attachment") )
02287       attachWordsList << i18n("attachment");
02288     if ( QString::fromLatin1("attached") != i18n("attached") )
02289       attachWordsList << i18n("attached");
02290   }
02291 
02292   QRegExp rx ( QString::fromLatin1("\\b") +
02293                attachWordsList.join("\\b|\\b") +
02294                QString::fromLatin1("\\b") );
02295   rx.setCaseSensitive( false );
02296 
02297   bool gotMatch = false;
02298 
02299   // check whether the subject contains one of the attachment key words
02300   // unless the message is a reply or a forwarded message
02301   QString subj = subject();
02302   gotMatch =    ( KMMessage::stripOffPrefixes( subj ) == subj )
02303              && ( rx.search( subj ) >= 0 );
02304 
02305   if ( !gotMatch ) {
02306     // check whether the non-quoted text contains one of the attachment key
02307     // words
02308     QRegExp quotationRx ("^([ \\t]*([|>:}#]|[A-Za-z]+>))+");
02309     for ( int i = 0; i < mEditor->numLines(); ++i ) {
02310       QString line = mEditor->textLine( i );
02311       gotMatch =    ( quotationRx.search( line ) < 0 )
02312                  && ( rx.search( line ) >= 0 );
02313       if ( gotMatch )
02314         break;
02315     }
02316   }
02317 
02318   if ( !gotMatch )
02319     return false;
02320 
02321   int rc = KMessageBox::warningYesNoCancel( this,
02322              i18n("The message you have composed seems to refer to an "
02323                   "attached file but you have not attached anything.\n"
02324                   "Do you want to attach a file to your message?"),
02325              i18n("File Attachment Reminder"),
02326              i18n("&Attach File..."),
02327              i18n("&Send as Is") );
02328   if ( rc == KMessageBox::Cancel )
02329     return true;
02330   if ( rc == KMessageBox::Yes ) {
02331     slotAttachFile();
02332     //preceed with editing
02333     return true;
02334   }
02335   return false;
02336 }
02337 
02338 //-----------------------------------------------------------------------------
02339 void KMComposeWin::applyChanges( bool dontSignNorEncrypt, bool dontDisable )
02340 {
02341   kdDebug(5006) << "entering KMComposeWin::applyChanges" << endl;
02342 
02343   if(!mMsg || mComposer) {
02344     kdDebug(5006) << "KMComposeWin::applyChanges() : mMsg == 0!\n" << endl;
02345     emit applyChangesDone( false );
02346     return;
02347   }
02348 
02349   // Make new job and execute it
02350   mComposer = new MessageComposer( this );
02351   connect( mComposer, SIGNAL( done( bool ) ),
02352            this, SLOT( slotComposerDone( bool ) ) );
02353 
02354   // TODO: Add a cancel button for the following operations?
02355   // Disable any input to the window, so that we have a snapshot of the
02356   // composed stuff
02357   if ( !dontDisable ) setEnabled( false );
02358   // apply the current state to the composer and let it do it's thing
02359   mComposer->setDisableBreaking( mDisableBreaking ); // FIXME
02360   mComposer->applyChanges( dontSignNorEncrypt );
02361 }
02362 
02363 void KMComposeWin::slotComposerDone( bool rc )
02364 {
02365   deleteAll( mComposedMessages );
02366   mComposedMessages = mComposer->composedMessageList();
02367   emit applyChangesDone( rc );
02368   delete mComposer;
02369   mComposer = 0;
02370 
02371   // re-enable the composewin, the messsage composition is now done
02372   setEnabled( true );
02373 }
02374 
02375 const KPIM::Identity & KMComposeWin::identity() const {
02376   return kmkernel->identityManager()->identityForUoidOrDefault( mIdentity->currentIdentity() );
02377 }
02378 
02379 uint KMComposeWin::identityUid() const {
02380   return mIdentity->currentIdentity();
02381 }
02382 
02383 Kleo::CryptoMessageFormat KMComposeWin::cryptoMessageFormat() const {
02384   if ( !mCryptoModuleAction )
02385     return Kleo::AutoFormat;
02386   return cb2format( mCryptoModuleAction->currentItem() );
02387 }
02388 
02389 bool KMComposeWin::encryptToSelf() const {
02390 //   return !Kpgp::Module::getKpgp() || Kpgp::Module::getKpgp()->encryptToSelf();
02391     KConfigGroup group( KMKernel::config(), "Composer" );
02392     return group.readBoolEntry( "crypto-encrypt-to-self", true );
02393 }
02394 
02395 bool KMComposeWin::queryExit ()
02396 {
02397   return true;
02398 }
02399 
02400 //-----------------------------------------------------------------------------
02401 bool KMComposeWin::addAttach(const KURL aUrl)
02402 {
02403   if ( !aUrl.isValid() ) {
02404     KMessageBox::sorry( this, i18n( "<qt><p>KMail could not recognize the location of the attachment (%1);</p>"
02405                                  "<p>you have to specify the full path if you wish to attach a file.</p></qt>" )
02406                         .arg( aUrl.prettyURL() ) );
02407     return false;
02408   }
02409 
02410   const int maxAttachmentSize = GlobalSettings::maximumAttachmentSize();
02411   const uint maximumAttachmentSizeInByte = maxAttachmentSize*1024*1024;
02412   if ( aUrl.isLocalFile() && QFileInfo( aUrl.pathOrURL() ).size() > maximumAttachmentSizeInByte ) {
02413     KMessageBox::sorry( this, i18n( "<qt><p>Your administrator has disallowed attaching files bigger than %1 MB.</p>" ).arg( maxAttachmentSize ) );
02414     return false;
02415   }
02416 
02417   KIO::TransferJob *job = KIO::get(aUrl);
02418   KIO::Scheduler::scheduleJob( job );
02419   atmLoadData ld;
02420   ld.url = aUrl;
02421   ld.data = QByteArray();
02422   ld.insert = false;
02423   if( !aUrl.fileEncoding().isEmpty() )
02424     ld.encoding = aUrl.fileEncoding().latin1();
02425 
02426   mMapAtmLoadData.insert(job, ld);
02427   mAttachJobs[job] = aUrl;
02428   connect(job, SIGNAL(result(KIO::Job *)),
02429           this, SLOT(slotAttachFileResult(KIO::Job *)));
02430   connect(job, SIGNAL(data(KIO::Job *, const QByteArray &)),
02431           this, SLOT(slotAttachFileData(KIO::Job *, const QByteArray &)));
02432   return true;
02433 }
02434 
02435 
02436 //-----------------------------------------------------------------------------
02437 void KMComposeWin::addAttach(const KMMessagePart* msgPart)
02438 {
02439   mAtmList.append(msgPart);
02440 
02441   // show the attachment listbox if it does not up to now
02442   if (mAtmList.count()==1)
02443   {
02444     mAtmListView->resize(mAtmListView->width(), 50);
02445     mAtmListView->show();
02446     resize(size());
02447   }
02448 
02449   // add a line in the attachment listbox
02450   KMAtmListViewItem *lvi = new KMAtmListViewItem( mAtmListView );
02451   msgPartToItem(msgPart, lvi);
02452   mAtmItemList.append(lvi);
02453 
02454   // the Attach file job has finished, so the possibly present tmp dir can be deleted now.
02455   if ( mTempDir != 0 ) {
02456     delete mTempDir;
02457     mTempDir = 0;
02458   }
02459 
02460   connect( lvi, SIGNAL( compress( int ) ),
02461       this, SLOT( compressAttach( int ) ) );
02462   connect( lvi, SIGNAL( uncompress( int ) ),
02463       this, SLOT( uncompressAttach( int ) ) );
02464 
02465   slotUpdateAttachActions();
02466   mAtmListView->sort();
02467 }
02468 
02469 
02470 //-----------------------------------------------------------------------------
02471 void KMComposeWin::slotUpdateAttachActions()
02472 {
02473   int selectedCount = 0;
02474   for ( QPtrListIterator<QListViewItem> it(mAtmItemList); *it; ++it ) {
02475     if ( (*it)->isSelected() ) {
02476       ++selectedCount;
02477     }
02478   }
02479 
02480   mAttachRemoveAction->setEnabled( selectedCount >= 1 );
02481   mAttachSaveAction->setEnabled( selectedCount == 1 );
02482   mAttachPropertiesAction->setEnabled( selectedCount == 1 );
02483 }
02484 
02485 
02486 //-----------------------------------------------------------------------------
02487 
02488 QString KMComposeWin::prettyMimeType( const QString& type )
02489 {
02490   QString t = type.lower();
02491   KServiceType::Ptr st = KServiceType::serviceType( t );
02492   return st ? st->comment() : t;
02493 }
02494 
02495 void KMComposeWin::msgPartToItem(const KMMessagePart* msgPart,
02496                                  KMAtmListViewItem *lvi, bool loadDefaults)
02497 {
02498   assert(msgPart != 0);
02499 
02500   if (!msgPart->fileName().isEmpty())
02501     lvi->setText(0, msgPart->fileName());
02502   else
02503     lvi->setText(0, msgPart->name());
02504   lvi->setText(1, KIO::convertSize( msgPart->decodedSize()));
02505   lvi->setText(2, msgPart->contentTransferEncodingStr());
02506   lvi->setText(3, prettyMimeType(msgPart->typeStr() + "/" + msgPart->subtypeStr()));
02507   lvi->setAttachmentSize(msgPart->decodedSize());
02508 
02509   if ( loadDefaults ) {
02510     if( canSignEncryptAttachments() ) {
02511       lvi->enableCryptoCBs( true );
02512       lvi->setEncrypt( mEncryptAction->isChecked() );
02513       lvi->setSign(    mSignAction->isChecked() );
02514     } else {
02515       lvi->enableCryptoCBs( false );
02516     }
02517   }
02518 }
02519 
02520 
02521 //-----------------------------------------------------------------------------
02522 void KMComposeWin::removeAttach(const QString &aUrl)
02523 {
02524   int idx;
02525   KMMessagePart* msgPart;
02526   for(idx=0,msgPart=mAtmList.first(); msgPart;
02527       msgPart=mAtmList.next(),idx++) {
02528     if (msgPart->name() == aUrl) {
02529       removeAttach(idx);
02530       return;
02531     }
02532   }
02533 }
02534 
02535 
02536 //-----------------------------------------------------------------------------
02537 void KMComposeWin::removeAttach(int idx)
02538 {
02539   mAtmModified = true;
02540 
02541   KMAtmListViewItem *item = static_cast<KMAtmListViewItem*>( mAtmItemList.at( idx ) );
02542   if ( item->itemBelow() )
02543     mAtmSelectNew = item->itemBelow();
02544   else if ( item->itemAbove() )
02545     mAtmSelectNew = item->itemAbove();
02546 
02547   mAtmList.remove(idx);
02548   delete mAtmItemList.take(idx);
02549 
02550   if( mAtmList.isEmpty() )
02551   {
02552     mAtmListView->hide();
02553     mAtmListView->setMinimumSize(0, 0);
02554     resize(size());
02555   } else {
02556     mAtmListView->sort();
02557   }
02558 }
02559 
02560 
02561 //-----------------------------------------------------------------------------
02562 bool KMComposeWin::encryptFlagOfAttachment(int idx)
02563 {
02564   return (int)(mAtmItemList.count()) > idx
02565     ? static_cast<KMAtmListViewItem*>( mAtmItemList.at( idx ) )->isEncrypt()
02566     : false;
02567 }
02568 
02569 
02570 //-----------------------------------------------------------------------------
02571 bool KMComposeWin::signFlagOfAttachment(int idx)
02572 {
02573   return (int)(mAtmItemList.count()) > idx
02574     ? ((KMAtmListViewItem*)(mAtmItemList.at( idx )))->isSign()
02575     : false;
02576 }
02577 
02578 
02579 //-----------------------------------------------------------------------------
02580 void KMComposeWin::addrBookSelInto()
02581 {
02582   if ( mClassicalRecipients ) {
02583     if ( GlobalSettings::self()->addresseeSelectorType() ==
02584          GlobalSettings::EnumAddresseeSelectorType::New ) {
02585       addrBookSelIntoNew();
02586     } else {
02587       addrBookSelIntoOld();
02588     }
02589   } else {
02590     kdWarning() << "To be implemented: call recipients picker." << endl;
02591   }
02592 }
02593 
02594 void KMComposeWin::addrBookSelIntoOld()
02595 {
02596   AddressesDialog dlg( this );
02597   QString txt;
02598   QStringList lst;
02599 
02600   txt = to();
02601   if ( !txt.isEmpty() ) {
02602       lst = KPIM::splitEmailAddrList( txt );
02603       dlg.setSelectedTo( lst );
02604   }
02605 
02606   txt = mEdtCc->text();
02607   if ( !txt.isEmpty() ) {
02608       lst = KPIM::splitEmailAddrList( txt );
02609       dlg.setSelectedCC( lst );
02610   }
02611 
02612   txt = mEdtBcc->text();
02613   if ( !txt.isEmpty() ) {
02614       lst = KPIM::splitEmailAddrList( txt );
02615       dlg.setSelectedBCC( lst );
02616   }
02617 
02618   dlg.setRecentAddresses( RecentAddresses::self( KMKernel::config() )->kabcAddresses() );
02619 
02620   if (dlg.exec()==QDialog::Rejected) return;
02621 
02622   mEdtTo->setText( dlg.to().join(", ") );
02623   mEdtTo->setEdited( true );
02624 
02625   mEdtCc->setText( dlg.cc().join(", ") );
02626   mEdtCc->setEdited( true );
02627 
02628   mEdtBcc->setText( dlg.bcc().join(", ") );
02629   mEdtBcc->setEdited( true );
02630 
02631   //Make sure BCC field is shown if needed
02632   if ( !mEdtBcc->text().isEmpty() ) {
02633     mShowHeaders |= HDR_BCC;
02634     rethinkFields( false );
02635   }
02636 }
02637 
02638 void KMComposeWin::addrBookSelIntoNew()
02639 {
02640   AddresseeEmailSelection selection;
02641 
02642   AddresseeSelectorDialog dlg( &selection );
02643 
02644   QString txt;
02645   QStringList lst;
02646 
02647   txt = to();
02648   if ( !txt.isEmpty() ) {
02649       lst = KPIM::splitEmailAddrList( txt );
02650       selection.setSelectedTo( lst );
02651   }
02652 
02653   txt = mEdtCc->text();
02654   if ( !txt.isEmpty() ) {
02655       lst = KPIM::splitEmailAddrList( txt );
02656       selection.setSelectedCC( lst );
02657   }
02658 
02659   txt = mEdtBcc->text();
02660   if ( !txt.isEmpty() ) {
02661       lst = KPIM::splitEmailAddrList( txt );
02662       selection.setSelectedBCC( lst );
02663   }
02664 
02665   if (dlg.exec()==QDialog::Rejected) return;
02666 
02667   QStringList list = selection.to() + selection.toDistributionLists();
02668   mEdtTo->setText( list.join(", ") );
02669   mEdtTo->setEdited( true );
02670 
02671   list = selection.cc() + selection.ccDistributionLists();
02672   mEdtCc->setText( list.join(", ") );
02673   mEdtCc->setEdited( true );
02674 
02675   list = selection.bcc() + selection.bccDistributionLists();
02676   mEdtBcc->setText( list.join(", ") );
02677   mEdtBcc->setEdited( true );
02678 
02679   //Make sure BCC field is shown if needed
02680   if ( !mEdtBcc->text().isEmpty() ) {
02681     mShowHeaders |= HDR_BCC;
02682     rethinkFields( false );
02683   }
02684 }
02685 
02686 
02687 //-----------------------------------------------------------------------------
02688 void KMComposeWin::setCharset(const QCString& aCharset, bool forceDefault)
02689 {
02690   if ((forceDefault && GlobalSettings::self()->forceReplyCharset()) || aCharset.isEmpty())
02691     mCharset = mDefCharset;
02692   else
02693     mCharset = aCharset.lower();
02694 
02695   if ( mCharset.isEmpty() || mCharset == "default" )
02696      mCharset = mDefCharset;
02697 
02698   if (mAutoCharset)
02699   {
02700     mEncodingAction->setCurrentItem( 0 );
02701     return;
02702   }
02703 
02704   QStringList encodings = mEncodingAction->items();
02705   int i = 0;
02706   bool charsetFound = false;
02707   for ( QStringList::Iterator it = encodings.begin(); it != encodings.end();
02708      ++it, i++ )
02709   {
02710     if (i > 0 && ((mCharset == "us-ascii" && i == 1) ||
02711      (i != 1 && KGlobal::charsets()->codecForName(
02712       KGlobal::charsets()->encodingForName(*it))
02713       == KGlobal::charsets()->codecForName(mCharset))))
02714     {
02715       mEncodingAction->setCurrentItem( i );
02716       slotSetCharset();
02717       charsetFound = true;
02718       break;
02719     }
02720   }
02721   if (!aCharset.isEmpty() && !charsetFound) setCharset("", true);
02722 }
02723 
02724 
02725 //-----------------------------------------------------------------------------
02726 void KMComposeWin::slotAddrBook()
02727 {
02728   KAddrBookExternal::openAddressBook(this);
02729 }
02730 
02731 
02732 //-----------------------------------------------------------------------------
02733 void KMComposeWin::slotAddrBookFrom()
02734 {
02735   addrBookSelInto();
02736 }
02737 
02738 
02739 //-----------------------------------------------------------------------------
02740 void KMComposeWin::slotAddrBookReplyTo()
02741 {
02742   addrBookSelInto();
02743 }
02744 
02745 
02746 //-----------------------------------------------------------------------------
02747 void KMComposeWin::slotAddrBookTo()
02748 {
02749   addrBookSelInto();
02750 }
02751 
02752 //-----------------------------------------------------------------------------
02753 void KMComposeWin::slotAttachFile()
02754 {
02755   // Create File Dialog and return selected file(s)
02756   // We will not care about any permissions, existence or whatsoever in
02757   // this function.
02758 
02759   // Handle the case where the last savedir is gone. kolab/issue4057
02760   QString recent;
02761   KURL recentURL = KFileDialog::getStartURL( QString::null, recent );
02762   if ( !recentURL.url().isEmpty() &&
02763        !KIO::NetAccess::exists( recentURL, true, this ) ) {
02764     recentURL = KURL( QDir::homeDirPath() );
02765   }
02766 
02767   KFileDialog fdlg( recentURL.url(), QString::null, this, 0, true );
02768   fdlg.setOperationMode( KFileDialog::Other );
02769   fdlg.setCaption( i18n( "Attach File" ) );
02770   fdlg.okButton()->setGuiItem( KGuiItem( i18n( "&Attach" ),"fileopen" ) );
02771   fdlg.setMode( KFile::Files );
02772   fdlg.exec();
02773   KURL::List files = fdlg.selectedURLs();
02774 
02775   for (KURL::List::Iterator it = files.begin(); it != files.end(); ++it)
02776     addAttach(*it);
02777 }
02778 
02779 
02780 //-----------------------------------------------------------------------------
02781 void KMComposeWin::slotAttachFileData(KIO::Job *job, const QByteArray &data)
02782 {
02783   QMap<KIO::Job*, atmLoadData>::Iterator it = mMapAtmLoadData.find(job);
02784   assert(it != mMapAtmLoadData.end());
02785   QBuffer buff((*it).data);
02786   buff.open(IO_WriteOnly | IO_Append);
02787   buff.writeBlock(data.data(), data.size());
02788   buff.close();
02789 }
02790 
02791 
02792 //-----------------------------------------------------------------------------
02793 void KMComposeWin::slotAttachFileResult(KIO::Job *job)
02794 {
02795   QMap<KIO::Job*, atmLoadData>::Iterator it = mMapAtmLoadData.find(job);
02796   assert(it != mMapAtmLoadData.end());
02797   KURL attachURL;
02798   QMap<KIO::Job*, KURL>::iterator jit = mAttachJobs.find(job);
02799   bool attachURLfound = (jit != mAttachJobs.end());
02800   if (attachURLfound)
02801   {
02802     attachURL = jit.data();
02803     mAttachJobs.remove(jit);
02804   }
02805   if (job->error())
02806   {
02807     mMapAtmLoadData.remove(it);
02808     job->showErrorDialog();
02809     if (attachURLfound)
02810       emit attachmentAdded(attachURL, false);
02811     return;
02812   }
02813   if ((*it).insert)
02814   {
02815     (*it).data.resize((*it).data.size() + 1);
02816     (*it).data[(*it).data.size() - 1] = '\0';
02817     if ( const QTextCodec * codec = KGlobal::charsets()->codecForName((*it).encoding) )
02818       mEditor->insert( codec->toUnicode( (*it).data ) );
02819     else
02820       mEditor->insert( QString::fromLocal8Bit( (*it).data ) );
02821     mMapAtmLoadData.remove(it);
02822     if (attachURLfound)
02823       emit attachmentAdded(attachURL, true);
02824     return;
02825   }
02826   QCString partCharset;
02827   if ( !( *it ).url.fileEncoding().isEmpty() ) {
02828     partCharset = QCString( ( *it ).url.fileEncoding().latin1() );
02829   } else {
02830     EncodingDetector ed;
02831     KLocale *loc = KGlobal::locale();
02832     ed.setAutoDetectLanguage( EncodingDetector::scriptForLanguageCode ( loc->language() ) );
02833     ed.analyze( (*it).data );
02834     partCharset = ed.encoding();
02835     if ( partCharset.isEmpty() ) //shouldn't happen
02836       partCharset = mCharset;
02837   }
02838 
02839   KMMessagePart* msgPart;
02840 
02841   KCursorSaver busy(KBusyPtr::busy());
02842   QString name( (*it).url.fileName() );
02843   // ask the job for the mime type of the file
02844   QString mimeType = static_cast<KIO::MimetypeJob*>(job)->mimetype();
02845 
02846   if ( name.isEmpty() ) {
02847     // URL ends with '/' (e.g. http://www.kde.org/)
02848     // guess a reasonable filename
02849     if( mimeType == "text/html" )
02850       name = "index.html";
02851     else {
02852       // try to determine a reasonable extension
02853       QStringList patterns( KMimeType::mimeType( mimeType )->patterns() );
02854       QString ext;
02855       if( !patterns.isEmpty() ) {
02856         ext = patterns[0];
02857         int i = ext.findRev( '.' );
02858         if( i == -1 )
02859           ext.prepend( '.' );
02860         else if( i > 0 )
02861           ext = ext.mid( i );
02862       }
02863       name = QString("unknown") += ext;
02864     }
02865   }
02866 
02867   name.truncate( 256 ); // is this needed?
02868 
02869   QCString encoding = KMMsgBase::autoDetectCharset(partCharset,
02870     KMMessage::preferredCharsets(), name);
02871   if ( encoding.isEmpty() )
02872     encoding = "utf-8";
02873 
02874   QCString encName;
02875   if ( GlobalSettings::self()->outlookCompatibleAttachments() )
02876     encName = KMMsgBase::encodeRFC2047String( name, encoding );
02877   else
02878     encName = KMMsgBase::encodeRFC2231String( name, encoding );
02879   bool RFC2231encoded = false;
02880   if ( !GlobalSettings::self()->outlookCompatibleAttachments() )
02881     RFC2231encoded = name != QString( encName );
02882 
02883   // create message part
02884   msgPart = new KMMessagePart;
02885   msgPart->setName(name);
02886   QValueList<int> allowedCTEs;
02887   if ( mimeType == "message/rfc822" ) {
02888     msgPart->setMessageBody( (*it).data );
02889     allowedCTEs << DwMime::kCte7bit;
02890     allowedCTEs << DwMime::kCte8bit;
02891   } else {
02892     msgPart->setBodyAndGuessCte((*it).data, allowedCTEs,
02893                                !kmkernel->msgSender()->sendQuotedPrintable());
02894     kdDebug(5006) << "autodetected cte: " << msgPart->cteStr() << endl;
02895   }
02896   int slash = mimeType.find( '/' );
02897   if( slash == -1 )
02898     slash = mimeType.length();
02899   msgPart->setTypeStr( mimeType.left( slash ).latin1() );
02900   msgPart->setSubtypeStr( mimeType.mid( slash + 1 ).latin1() );
02901   msgPart->setContentDisposition(QCString("attachment;\n\tfilename")
02902     + ( RFC2231encoded ? "*=" + encName : "=\"" + encName + '"' ) );
02903 
02904   mMapAtmLoadData.remove(it);
02905 
02906   if ( msgPart->typeStr().lower() == "text" ) {
02907     msgPart->setCharset(partCharset);
02908   }
02909 
02910   // show message part dialog, if not configured away (default):
02911   KConfigGroup composer(KMKernel::config(), "Composer");
02912   if ( GlobalSettings::self()->showMessagePartDialogOnAttach() ) {
02913     const KCursorSaver saver( QCursor::ArrowCursor );
02914     KMMsgPartDialogCompat dlg(mMainWidget);
02915     int encodings = 0;
02916     for ( QValueListConstIterator<int> it = allowedCTEs.begin() ;
02917           it != allowedCTEs.end() ; ++it )
02918       switch ( *it ) {
02919       case DwMime::kCteBase64: encodings |= KMMsgPartDialog::Base64; break;
02920       case DwMime::kCteQp: encodings |= KMMsgPartDialog::QuotedPrintable; break;
02921       case DwMime::kCte7bit: encodings |= KMMsgPartDialog::SevenBit; break;
02922       case DwMime::kCte8bit: encodings |= KMMsgPartDialog::EightBit; break;
02923       default: ;
02924       }
02925     dlg.setShownEncodings( encodings );
02926     dlg.setMsgPart(msgPart);
02927     if (!dlg.exec()) {
02928       delete msgPart;
02929       msgPart = 0;
02930       if (attachURLfound)
02931         emit attachmentAdded(attachURL, false);
02932       return;
02933     }
02934   }
02935   mAtmModified = true;
02936 
02937   // add the new attachment to the list
02938   addAttach(msgPart);
02939 
02940   if (attachURLfound)
02941     emit attachmentAdded(attachURL, true);
02942 }
02943 
02944 
02945 //-----------------------------------------------------------------------------
02946 void KMComposeWin::slotInsertFile()
02947 {
02948   KFileDialog fdlg(QString::null, QString::null, this, 0, true);
02949   fdlg.setOperationMode( KFileDialog::Opening );
02950   fdlg.okButton()->setText(i18n("&Insert"));
02951   fdlg.setCaption(i18n("Insert File"));
02952   fdlg.toolBar()->insertCombo(KMMsgBase::supportedEncodings(false), 4711,
02953     false, 0, 0, 0);
02954   KComboBox *combo = fdlg.toolBar()->getCombo(4711);
02955   for (int i = 0; i < combo->count(); i++)
02956     if (KGlobal::charsets()->codecForName(KGlobal::charsets()->
02957       encodingForName(combo->text(i)))
02958       == QTextCodec::codecForLocale()) combo->setCurrentItem(i);
02959   if (!fdlg.exec()) return;
02960 
02961   KURL u = fdlg.selectedURL();
02962   mRecentAction->addURL(u);
02963   // Prevent race condition updating list when multiple composers are open
02964   {
02965     KConfig *config = KMKernel::config();
02966     KConfigGroupSaver saver( config, "Composer" );
02967     QString encoding = KGlobal::charsets()->encodingForName(combo->currentText()).latin1();
02968     QStringList urls = config->readListEntry( "recent-urls" );
02969     QStringList encodings = config->readListEntry( "recent-encodings" );
02970     // Prevent config file from growing without bound
02971     // Would be nicer to get this constant from KRecentFilesAction
02972     uint mMaxRecentFiles = 30;
02973     while (urls.count() > mMaxRecentFiles)
02974       urls.erase( urls.fromLast() );
02975     while (encodings.count() > mMaxRecentFiles)
02976       encodings.erase( encodings.fromLast() );
02977     // sanity check
02978     if (urls.count() != encodings.count()) {
02979       urls.clear();
02980       encodings.clear();
02981     }
02982     urls.prepend( u.prettyURL() );
02983     encodings.prepend( encoding );
02984     config->writeEntry( "recent-urls", urls );
02985     config->writeEntry( "recent-encodings", encodings );
02986     mRecentAction->saveEntries( config );
02987   }
02988   slotInsertRecentFile(u);
02989 }
02990 
02991 
02992 //-----------------------------------------------------------------------------
02993 void KMComposeWin::slotInsertRecentFile(const KURL& u)
02994 {
02995   if (u.fileName().isEmpty()) return;
02996 
02997   KIO::Job *job = KIO::get(u);
02998   atmLoadData ld;
02999   ld.url = u;
03000   ld.data = QByteArray();
03001   ld.insert = true;
03002   // Get the encoding previously used when inserting this file
03003   {
03004     KConfig *config = KMKernel::config();
03005     KConfigGroupSaver saver( config, "Composer" );
03006     QStringList urls = config->readListEntry( "recent-urls" );
03007     QStringList encodings = config->readListEntry( "recent-encodings" );
03008     int index = urls.findIndex( u.prettyURL() );
03009     if (index != -1) {
03010       QString encoding = encodings[ index ];
03011       ld.encoding = encoding.latin1();
03012     }
03013   }
03014   mMapAtmLoadData.insert(job, ld);
03015   connect(job, SIGNAL(result(KIO::Job *)),
03016           this, SLOT(slotAttachFileResult(KIO::Job *)));
03017   connect(job, SIGNAL(data(KIO::Job *, const QByteArray &)),
03018           this, SLOT(slotAttachFileData(KIO::Job *, const QByteArray &)));
03019 }
03020 
03021 
03022 //-----------------------------------------------------------------------------
03023 void KMComposeWin::slotSetCharset()
03024 {
03025   if (mEncodingAction->currentItem() == 0)
03026   {
03027     mAutoCharset = true;
03028     return;
03029   }
03030   mAutoCharset = false;
03031 
03032   mCharset = KGlobal::charsets()->encodingForName( mEncodingAction->
03033     currentText() ).latin1();
03034 }
03035 
03036 
03037 //-----------------------------------------------------------------------------
03038 void KMComposeWin::slotSelectCryptoModule( bool init )
03039 {
03040   if ( !init ) {
03041     setModified( true );
03042   }
03043   if( canSignEncryptAttachments() ) {
03044     // if the encrypt/sign columns are hidden then show them
03045     if( 0 == mAtmListView->columnWidth( mAtmColEncrypt ) ) {
03046       // set/unset signing/encryption for all attachments according to the
03047       // state of the global sign/encrypt action
03048       if( !mAtmList.isEmpty() ) {
03049         for( KMAtmListViewItem* lvi = static_cast<KMAtmListViewItem*>( mAtmItemList.first() );
03050              lvi;
03051              lvi = static_cast<KMAtmListViewItem*>( mAtmItemList.next() ) ) {
03052           lvi->setSign( mSignAction->isChecked() );
03053           lvi->setEncrypt( mEncryptAction->isChecked() );
03054         }
03055       }
03056       int totalWidth = 0;
03057       // determine the total width of the columns
03058       for( int col=0; col < mAtmColEncrypt; col++ )
03059         totalWidth += mAtmListView->columnWidth( col );
03060       int reducedTotalWidth = totalWidth - mAtmEncryptColWidth
03061                                          - mAtmSignColWidth;
03062       // reduce the width of all columns so that the encrypt and sign column
03063       // fit
03064       int usedWidth = 0;
03065       for( int col=0; col < mAtmColEncrypt-1; col++ ) {
03066         int newWidth = mAtmListView->columnWidth( col ) * reducedTotalWidth
03067                                                        / totalWidth;
03068         mAtmListView->setColumnWidth( col, newWidth );
03069         usedWidth += newWidth;
03070       }
03071       // the last column before the encrypt column gets the remaining space
03072       // (because of rounding errors the width of this column isn't calculated
03073       // the same way as the width of the other columns)
03074       mAtmListView->setColumnWidth( mAtmColEncrypt-1,
03075                                     reducedTotalWidth - usedWidth );
03076       mAtmListView->setColumnWidth( mAtmColEncrypt, mAtmEncryptColWidth );
03077       mAtmListView->setColumnWidth( mAtmColSign,    mAtmSignColWidth );
03078       for( KMAtmListViewItem* lvi = static_cast<KMAtmListViewItem*>( mAtmItemList.first() );
03079            lvi;
03080            lvi = static_cast<KMAtmListViewItem*>( mAtmItemList.next() ) ) {
03081         lvi->enableCryptoCBs( true );
03082       }
03083     }
03084   } else {
03085     // if the encrypt/sign columns are visible then hide them
03086     if( 0 != mAtmListView->columnWidth( mAtmColEncrypt ) ) {
03087       mAtmEncryptColWidth = mAtmListView->columnWidth( mAtmColEncrypt );
03088       mAtmSignColWidth = mAtmListView->columnWidth( mAtmColSign );
03089       int totalWidth = 0;
03090       // determine the total width of the columns
03091       for( int col=0; col < mAtmListView->columns(); col++ )
03092         totalWidth += mAtmListView->columnWidth( col );
03093       int reducedTotalWidth = totalWidth - mAtmEncryptColWidth
03094                                          - mAtmSignColWidth;
03095       // increase the width of all columns so that the visible columns take
03096       // up the whole space
03097       int usedWidth = 0;
03098       for( int col=0; col < mAtmColEncrypt-1; col++ ) {
03099         int newWidth = mAtmListView->columnWidth( col ) * totalWidth
03100                                                        / reducedTotalWidth;
03101         mAtmListView->setColumnWidth( col, newWidth );
03102         usedWidth += newWidth;
03103       }
03104       // the last column before the encrypt column gets the remaining space
03105       // (because of rounding errors the width of this column isn't calculated
03106       // the same way as the width of the other columns)
03107       mAtmListView->setColumnWidth( mAtmColEncrypt-1, totalWidth - usedWidth );
03108       mAtmListView->setColumnWidth( mAtmColEncrypt, 0 );
03109       mAtmListView->setColumnWidth( mAtmColSign,    0 );
03110       for( KMAtmListViewItem* lvi = static_cast<KMAtmListViewItem*>( mAtmItemList.first() );
03111            lvi;
03112            lvi = static_cast<KMAtmListViewItem*>( mAtmItemList.next() ) ) {
03113         lvi->enableCryptoCBs( false );
03114       }
03115     }
03116   }
03117 }
03118 
03119 static void showExportError( QWidget * w, const GpgME::Error & err ) {
03120   assert( err );
03121   const QString msg = i18n("<qt><p>An error occurred while trying to export "
03122                "the key from the backend:</p>"
03123                "<p><b>%1</b></p></qt>")
03124     .arg( QString::fromLocal8Bit( err.asString() ) );
03125   KMessageBox::error( w, msg, i18n("Key Export Failed") );
03126 }
03127 
03128 
03129 //-----------------------------------------------------------------------------
03130 void KMComposeWin::slotInsertMyPublicKey()
03131 {
03132   // get PGP user id for the chosen identity
03133   mFingerprint =
03134     kmkernel->identityManager()->identityForUoidOrDefault( mIdentity->currentIdentity() ).pgpEncryptionKey();
03135   if ( !mFingerprint.isEmpty() )
03136     startPublicKeyExport();
03137 }
03138 
03139 void KMComposeWin::startPublicKeyExport() {
03140   if ( mFingerprint.isEmpty() || !Kleo::CryptoBackendFactory::instance()->openpgp() )
03141     return;
03142   Kleo::ExportJob * job = Kleo::CryptoBackendFactory::instance()->openpgp()->publicKeyExportJob( true );
03143   assert( job );
03144 
03145   connect( job, SIGNAL(result(const GpgME::Error&,const QByteArray&)),
03146        this, SLOT(slotPublicKeyExportResult(const GpgME::Error&,const QByteArray&)) );
03147 
03148   const GpgME::Error err = job->start( mFingerprint );
03149   if ( err )
03150     showExportError( this, err );
03151   else
03152     (void)new Kleo::ProgressDialog( job, i18n("Exporting key..."), this );
03153 }
03154 
03155 void KMComposeWin::slotPublicKeyExportResult( const GpgME::Error & err, const QByteArray & keydata ) {
03156   if ( err ) {
03157     showExportError( this, err );
03158     return;
03159   }
03160 
03161   // create message part
03162   KMMessagePart * msgPart = new KMMessagePart();
03163   msgPart->setName( i18n("OpenPGP key 0x%1").arg( mFingerprint ) );
03164   msgPart->setTypeStr("application");
03165   msgPart->setSubtypeStr("pgp-keys");
03166   QValueList<int> dummy;
03167   msgPart->setBodyAndGuessCte(keydata, dummy, false);
03168   msgPart->setContentDisposition( "attachment;\n\tfilename=0x" + QCString( mFingerprint.latin1() ) + ".asc" );
03169 
03170   // add the new attachment to the list
03171   addAttach(msgPart);
03172   rethinkFields(); //work around initial-size bug in Qt-1.32
03173 }
03174 
03175 //-----------------------------------------------------------------------------
03176 void KMComposeWin::slotInsertPublicKey()
03177 {
03178   Kleo::KeySelectionDialog dlg( i18n("Attach Public OpenPGP Key"),
03179                                 i18n("Select the public key which should "
03180                                      "be attached."),
03181                 std::vector<GpgME::Key>(),
03182                 Kleo::KeySelectionDialog::PublicKeys|Kleo::KeySelectionDialog::OpenPGPKeys,
03183                 false /* no multi selection */,
03184                 false /* no remember choice box */,
03185                 this, "attach public key selection dialog" );
03186   if ( dlg.exec() != QDialog::Accepted )
03187     return;
03188 
03189   mFingerprint = dlg.fingerprint();
03190   startPublicKeyExport();
03191 }
03192 
03193 
03194 //-----------------------------------------------------------------------------
03195 void KMComposeWin::slotAttachPopupMenu(QListViewItem *, const QPoint &, int)
03196 {
03197   if (!mAttachMenu)
03198   {
03199      mAttachMenu = new QPopupMenu(this);
03200 
03201      mOpenId = mAttachMenu->insertItem(i18n("to open", "Open"), this,
03202                              SLOT(slotAttachOpen()));
03203      mOpenWithId = mAttachMenu->insertItem(i18n("Open With..."), this,
03204                              SLOT(slotAttachOpenWith()));
03205      mViewId = mAttachMenu->insertItem(i18n("to view", "View"), this,
03206                              SLOT(slotAttachView()));
03207      mEditId = mAttachMenu->insertItem( i18n("Edit"), this, SLOT(slotAttachEdit()) );
03208      mEditWithId = mAttachMenu->insertItem( i18n("Edit With..."), this,
03209                                             SLOT(slotAttachEditWith()) );
03210      mRemoveId = mAttachMenu->insertItem(i18n("Remove"), this, SLOT(slotAttachRemove()));
03211      mSaveAsId = mAttachMenu->insertItem( SmallIconSet("filesaveas"), i18n("Save As..."), this,
03212                                           SLOT( slotAttachSave() ) );
03213      mPropertiesId = mAttachMenu->insertItem( i18n("Properties"), this,
03214                                               SLOT( slotAttachProperties() ) );
03215      mAttachMenu->insertSeparator();
03216      mAttachMenu->insertItem(i18n("Add Attachment..."), this, SLOT(slotAttachFile()));
03217   }
03218 
03219   int selectedCount = 0;
03220   for ( QPtrListIterator<QListViewItem> it(mAtmItemList); *it; ++it ) {
03221     if ( (*it)->isSelected() ) {
03222       ++selectedCount;
03223     }
03224   }
03225 
03226   mAttachMenu->setItemEnabled( mOpenId, selectedCount > 0 );
03227   mAttachMenu->setItemEnabled( mOpenWithId, selectedCount > 0 );
03228   mAttachMenu->setItemEnabled( mViewId, selectedCount > 0 );
03229   mAttachMenu->setItemEnabled( mEditId, selectedCount == 1 );
03230   mAttachMenu->setItemEnabled( mEditWithId, selectedCount == 1 );
03231   mAttachMenu->setItemEnabled( mRemoveId, selectedCount > 0 );
03232   mAttachMenu->setItemEnabled( mSaveAsId, selectedCount == 1 );
03233   mAttachMenu->setItemEnabled( mPropertiesId, selectedCount == 1 );
03234 
03235   mAttachMenu->popup(QCursor::pos());
03236 }
03237 
03238 //-----------------------------------------------------------------------------
03239 int KMComposeWin::currentAttachmentNum()
03240 {
03241   int i = 0;
03242   for ( QPtrListIterator<QListViewItem> it(mAtmItemList); *it; ++it, ++i )
03243     if ( *it == mAtmListView->currentItem() )
03244       return i;
03245   return -1;
03246 }
03247 
03248 //-----------------------------------------------------------------------------
03249 void KMComposeWin::slotAttachProperties()
03250 {
03251   int idx = currentAttachmentNum();
03252 
03253   if (idx < 0) return;
03254 
03255   KMMessagePart* msgPart = mAtmList.at(idx);
03256 
03257   KMMsgPartDialogCompat dlg(mMainWidget);
03258   dlg.setMsgPart(msgPart);
03259   KMAtmListViewItem* listItem = (KMAtmListViewItem*)(mAtmItemList.at(idx));
03260   if( canSignEncryptAttachments() && listItem ) {
03261     dlg.setCanSign(    true );
03262     dlg.setCanEncrypt( true );
03263     dlg.setSigned(    listItem->isSign()    );
03264     dlg.setEncrypted( listItem->isEncrypt() );
03265   } else {
03266     dlg.setCanSign(    false );
03267     dlg.setCanEncrypt( false );
03268   }
03269   if (dlg.exec())
03270   {
03271     mAtmModified = true;
03272     // values may have changed, so recreate the listbox line
03273     if( listItem ) {
03274       msgPartToItem(msgPart, listItem);
03275       if( canSignEncryptAttachments() ) {
03276         listItem->setSign(    dlg.isSigned()    );
03277         listItem->setEncrypt( dlg.isEncrypted() );
03278       }
03279     }
03280   }
03281   if (msgPart->typeStr().lower() != "text") msgPart->setCharset(QCString());
03282 }
03283 
03284 //-----------------------------------------------------------------------------
03285 void KMComposeWin::compressAttach( int idx )
03286 {
03287   if (idx < 0) return;
03288 
03289   unsigned int i;
03290   for ( i = 0; i < mAtmItemList.count(); ++i )
03291       if ( mAtmItemList.at( i )->itemPos() == idx )
03292           break;
03293 
03294   if ( i > mAtmItemList.count() )
03295       return;
03296 
03297   KMMessagePart* msgPart;
03298   msgPart = mAtmList.at( i );
03299   QByteArray array;
03300   QBuffer dev( array );
03301   KZip zip( &dev );
03302   QByteArray decoded = msgPart->bodyDecodedBinary();
03303   if ( ! zip.open( IO_WriteOnly ) ) {
03304     KMessageBox::sorry(0, i18n("KMail could not compress the file.") );
03305     static_cast<KMAtmListViewItem*>( mAtmItemList.at( i ) )->setCompress( false );
03306     return;
03307   }
03308 
03309   zip.setCompression( KZip::DeflateCompression );
03310   if ( ! zip.writeFile( msgPart->name(), "", "", decoded.size(),
03311            decoded.data() ) ) {
03312     KMessageBox::sorry(0, i18n("KMail could not compress the file.") );
03313     static_cast<KMAtmListViewItem*>( mAtmItemList.at( i ) )->setCompress( false );
03314     return;
03315   }
03316   zip.close();
03317   if ( array.size() >= decoded.size() ) {
03318     if ( KMessageBox::questionYesNo( this, i18n("The compressed file is larger "
03319         "than the original. Do you want to keep the original one?" ), QString::null, i18n("Keep"), i18n("Compress") )
03320          == KMessageBox::Yes ) {
03321       static_cast<KMAtmListViewItem*>( mAtmItemList.at( i ) )->setCompress( false );
03322       return;
03323     }
03324   }
03325   static_cast<KMAtmListViewItem*>( mAtmItemList.at( i ) )->setUncompressedCodec(
03326       msgPart->cteStr() );
03327 
03328   msgPart->setCteStr( "base64" );
03329   msgPart->setBodyEncodedBinary( array );
03330   QString name = msgPart->name() + ".zip";
03331 
03332   msgPart->setName( name );
03333 
03334   QCString cDisp = "attachment;";
03335   QCString encoding = KMMsgBase::autoDetectCharset( msgPart->charset(),
03336     KMMessage::preferredCharsets(), name );
03337   kdDebug(5006) << "encoding: " << encoding << endl;
03338   if ( encoding.isEmpty() ) encoding = "utf-8";
03339   kdDebug(5006) << "encoding after: " << encoding << endl;
03340   QCString encName;
03341   if ( GlobalSettings::self()->outlookCompatibleAttachments() )
03342     encName = KMMsgBase::encodeRFC2047String( name, encoding );
03343   else
03344     encName = KMMsgBase::encodeRFC2231String( name, encoding );
03345 
03346   cDisp += "\n\tfilename";
03347   if ( name != QString( encName ) )
03348     cDisp += "*=" + encName;
03349   else
03350     cDisp += "=\"" + encName + '"';
03351   msgPart->setContentDisposition( cDisp );
03352 
03353   static_cast<KMAtmListViewItem*>( mAtmItemList.at( i ) )->setUncompressedMimeType(
03354       msgPart->typeStr(), msgPart->subtypeStr() );
03355   msgPart->setTypeStr( "application" );
03356   msgPart->setSubtypeStr( "x-zip" );
03357 
03358   KMAtmListViewItem* listItem = static_cast<KMAtmListViewItem*>( mAtmItemList.at( i ) );
03359   msgPartToItem( msgPart, listItem, false );
03360 
03361   // The size has changed, so re-sort, might be sorted by size
03362   mAtmListView->sort();
03363 }
03364 
03365 //-----------------------------------------------------------------------------
03366 
03367 void KMComposeWin::uncompressAttach( int idx )
03368 {
03369   if (idx < 0) return;
03370 
03371   unsigned int i;
03372   for ( i = 0; i < mAtmItemList.count(); ++i )
03373       if ( mAtmItemList.at( i )->itemPos() == idx )
03374           break;
03375 
03376   if ( i > mAtmItemList.count() )
03377       return;
03378 
03379   KMMessagePart* msgPart;
03380   msgPart = mAtmList.at( i );
03381 
03382   QBuffer dev( msgPart->bodyDecodedBinary() );
03383   KZip zip( &dev );
03384   QByteArray decoded;
03385 
03386   decoded = msgPart->bodyDecodedBinary();
03387   if ( ! zip.open( IO_ReadOnly ) ) {
03388     KMessageBox::sorry(0, i18n("KMail could not uncompress the file.") );
03389     static_cast<KMAtmListViewItem *>( mAtmItemList.at( i ) )->setCompress( true );
03390     return;
03391   }
03392   const KArchiveDirectory *dir = zip.directory();
03393 
03394   KZipFileEntry *entry;
03395   if ( dir->entries().count() != 1 ) {
03396     KMessageBox::sorry(0, i18n("KMail could not uncompress the file.") );
03397     static_cast<KMAtmListViewItem *>( mAtmItemList.at( i ) )->setCompress( true );
03398     return;
03399   }
03400   entry = (KZipFileEntry*)dir->entry( dir->entries()[0] );
03401 
03402   msgPart->setCteStr(
03403       static_cast<KMAtmListViewItem*>( mAtmItemList.at(i) )->uncompressedCodec() );
03404 
03405   msgPart->setBodyEncodedBinary( entry->data() );
03406   QString name = entry->name();
03407   msgPart->setName( name );
03408 
03409   zip.close();
03410 
03411   QCString cDisp = "attachment;";
03412   QCString encoding = KMMsgBase::autoDetectCharset( msgPart->charset(),
03413     KMMessage::preferredCharsets(), name );
03414   if ( encoding.isEmpty() ) encoding = "utf-8";
03415 
03416   QCString encName;
03417   if ( GlobalSettings::self()->outlookCompatibleAttachments() )
03418     encName = KMMsgBase::encodeRFC2047String( name, encoding );
03419   else
03420     encName = KMMsgBase::encodeRFC2231String( name, encoding );
03421 
03422   cDisp += "\n\tfilename";
03423   if ( name != QString( encName ) )
03424     cDisp += "*=" + encName;
03425   else
03426     cDisp += "=\"" + encName + '"';
03427   msgPart->setContentDisposition( cDisp );
03428 
03429   QCString type, subtype;
03430   static_cast<KMAtmListViewItem*>( mAtmItemList.at( i ) )->uncompressedMimeType( type,
03431         subtype );
03432 
03433   msgPart->setTypeStr( type );
03434   msgPart->setSubtypeStr( subtype );
03435 
03436   KMAtmListViewItem* listItem = static_cast<KMAtmListViewItem*>(mAtmItemList.at( i ));
03437   msgPartToItem( msgPart, listItem, false );
03438   mAtmListView->sort();
03439 }
03440 
03441 
03442 //-----------------------------------------------------------------------------
03443 void KMComposeWin::slotAttachView()
03444 {
03445   int i = 0;
03446   for ( QPtrListIterator<QListViewItem> it(mAtmItemList); *it; ++it, ++i ) {
03447     if ( (*it)->isSelected() ) {
03448       viewAttach( i );
03449     }
03450   }
03451 }
03452 //-----------------------------------------------------------------------------
03453 void KMComposeWin::slotAttachOpen()
03454 {
03455   int i = 0;
03456   for ( QPtrListIterator<QListViewItem> it(mAtmItemList); *it; ++it, ++i ) {
03457     if ( (*it)->isSelected() ) {
03458       openAttach( i, false );
03459     }
03460   }
03461 }
03462 
03463 //-----------------------------------------------------------------------------
03464 void KMComposeWin::slotAttachOpenWith()
03465 {
03466   int i = 0;
03467   for ( QPtrListIterator<QListViewItem> it(mAtmItemList); *it; ++it, ++i ) {
03468     if ( (*it)->isSelected() ) {
03469       openAttach( i, true );
03470     }
03471   }
03472 }
03473 
03474 void KMComposeWin::slotAttachEdit()
03475 {
03476   int i = 0;
03477   for ( QPtrListIterator<QListViewItem> it(mAtmItemList); *it; ++it, ++i ) {
03478     if ( (*it)->isSelected() ) {
03479       editAttach( i, false );
03480     }
03481   }
03482 }
03483 
03484 void KMComposeWin::slotAttachEditWith()
03485 {
03486   int i = 0;
03487   for ( QPtrListIterator<QListViewItem> it(mAtmItemList); *it; ++it, ++i ) {
03488     if ( (*it)->isSelected() ) {
03489       editAttach( i, true );
03490     }
03491   }
03492 }
03493 
03494 //-----------------------------------------------------------------------------
03495 bool KMComposeWin::inlineSigningEncryptionSelected() {
03496   if ( !mSignAction->isChecked() && !mEncryptAction->isChecked() )
03497     return false;
03498   return cryptoMessageFormat() == Kleo::InlineOpenPGPFormat;
03499 }
03500 
03501 //-----------------------------------------------------------------------------
03502 void KMComposeWin::viewAttach( int index )
03503 {
03504   QString pname;
03505   KMMessagePart* msgPart;
03506   msgPart = mAtmList.at(index);
03507   pname = msgPart->name().stripWhiteSpace();
03508   if (pname.isEmpty()) pname=msgPart->contentDescription();
03509   if (pname.isEmpty()) pname="unnamed";
03510 
03511   KTempFile* atmTempFile = new KTempFile();
03512   mAtmTempList.append( atmTempFile );
03513   atmTempFile->setAutoDelete( true );
03514   KPIM::kByteArrayToFile(msgPart->bodyDecodedBinary(), atmTempFile->name(), false, false,
03515     false);
03516   KMReaderMainWin *win = new KMReaderMainWin(msgPart, false,
03517     atmTempFile->name(), pname, mCharset );
03518   win->show();
03519 }
03520 
03521 //-----------------------------------------------------------------------------
03522 void KMComposeWin::openAttach( int index, bool with )
03523 {
03524   KMMessagePart* msgPart = mAtmList.at(index);
03525   const QString contentTypeStr =
03526     ( msgPart->typeStr() + '/' + msgPart->subtypeStr() ).lower();
03527 
03528   KMimeType::Ptr mimetype;
03529   mimetype = KMimeType::mimeType( contentTypeStr );
03530 
03531   KTempFile* atmTempFile = new KTempFile();
03532   mAtmTempList.append( atmTempFile );
03533   const bool autoDelete = true;
03534   atmTempFile->setAutoDelete( autoDelete );
03535 
03536   KURL url;
03537   url.setPath( atmTempFile->name() );
03538 
03539   KPIM::kByteArrayToFile( msgPart->bodyDecodedBinary(), atmTempFile->name(), false, false,
03540     false );
03541   if ( ::chmod( QFile::encodeName( atmTempFile->name() ), S_IRUSR ) != 0) {
03542     QFile::remove(url.path());
03543     return;
03544   }
03545 
03546   KService::Ptr offer =
03547     KServiceTypeProfile::preferredService( mimetype->name(), "Application" );
03548 
03549   if ( with || !offer || mimetype->name() == "application/octet-stream" ) {
03550     if ( ( !KRun::displayOpenWithDialog( url, autoDelete ) ) && autoDelete ) {
03551       QFile::remove(url.path());
03552     }
03553   }
03554   else {
03555     if ( ( !KRun::run( *offer, url, autoDelete ) ) && autoDelete ) {
03556         QFile::remove( url.path() );
03557     }
03558   }
03559 }
03560 
03561 void KMComposeWin::editAttach(int index, bool openWith)
03562 {
03563   KMMessagePart* msgPart = mAtmList.at(index);
03564   const QString contentTypeStr =
03565     ( msgPart->typeStr() + '/' + msgPart->subtypeStr() ).lower();
03566 
03567   KTempFile* atmTempFile = new KTempFile();
03568   mAtmTempList.append( atmTempFile );
03569   atmTempFile->setAutoDelete( true );
03570   atmTempFile->file()->writeBlock( msgPart->bodyDecodedBinary() );
03571   atmTempFile->file()->flush();
03572 
03573 
03574   KMail::EditorWatcher *watcher =
03575           new KMail::EditorWatcher( KURL( atmTempFile->name() ), contentTypeStr, openWith,
03576                                     this, this );
03577   connect( watcher, SIGNAL(editDone(KMail::EditorWatcher*)), SLOT(slotEditDone(KMail::EditorWatcher*)) );
03578   if ( watcher->start() ) {
03579     mEditorMap.insert( watcher, msgPart );
03580     mEditorTempFiles.insert( watcher, atmTempFile );
03581   }
03582 }
03583 
03584 //-----------------------------------------------------------------------------
03585 void KMComposeWin::slotAttachSave()
03586 {
03587   KMMessagePart* msgPart;
03588   QString fileName, pname;
03589   int idx = currentAttachmentNum();
03590 
03591   if (idx < 0) return;
03592 
03593   msgPart = mAtmList.at(idx);
03594   pname = msgPart->name();
03595   if (pname.isEmpty()) pname="unnamed";
03596 
03597   KURL url = KFileDialog::getSaveURL(pname, QString::null, 0, i18n("Save Attachment As"));
03598 
03599   if( url.isEmpty() )
03600     return;
03601 
03602   kmkernel->byteArrayToRemoteFile(msgPart->bodyDecodedBinary(), url);
03603 }
03604 
03605 
03606 //-----------------------------------------------------------------------------
03607 void KMComposeWin::slotAttachRemove()
03608 {
03609   mAtmSelectNew = 0;
03610   bool attachmentRemoved = false;
03611   int i = 0;
03612   for ( QPtrListIterator<QListViewItem> it(mAtmItemList); *it; ) {
03613     if ( (*it)->isSelected() ) {
03614       removeAttach( i );
03615       attachmentRemoved = true;
03616     }
03617     else {
03618       ++it;
03619       ++i;
03620     }
03621   }
03622 
03623   if ( attachmentRemoved ) {
03624     setModified( true );
03625     slotUpdateAttachActions();
03626     if ( mAtmSelectNew ) {
03627       mAtmListView->setSelected( mAtmSelectNew, true );
03628       mAtmListView->setCurrentItem( mAtmSelectNew );
03629     }
03630   }
03631 }
03632 
03633 //-----------------------------------------------------------------------------
03634 void KMComposeWin::slotFind()
03635 {
03636   mEditor->search();
03637 }
03638 
03639 void KMComposeWin::slotSearchAgain()
03640 {
03641   mEditor->repeatSearch();
03642 }
03643 
03644 //-----------------------------------------------------------------------------
03645 void KMComposeWin::slotReplace()
03646 {
03647   mEditor->replace();
03648 }
03649 
03650 //-----------------------------------------------------------------------------
03651 void KMComposeWin::slotUpdateFont()
03652 {
03653   kdDebug() << "KMComposeWin::slotUpdateFont " << endl;
03654   if ( ! mFixedFontAction ) {
03655     return;
03656   }
03657   mEditor->setFont( mFixedFontAction->isChecked() ? mFixedFont : mBodyFont );
03658 }
03659 
03660 QString KMComposeWin::quotePrefixName() const
03661 {
03662     if ( !msg() )
03663         return QString::null;
03664 
03665     int languageNr = GlobalSettings::self()->replyCurrentLanguage();
03666     ReplyPhrases replyPhrases( QString::number(languageNr) );
03667     replyPhrases.readConfig();
03668     QString quotePrefix = msg()->formatString(
03669                  replyPhrases.indentPrefix() );
03670 
03671     quotePrefix = msg()->formatString(quotePrefix);
03672     return quotePrefix;
03673 }
03674 
03675 void KMComposeWin::slotPasteClipboardAsQuotation()
03676 {
03677     if( mEditor->hasFocus() && msg() )
03678     {
03679         QString s = QApplication::clipboard()->text();
03680         if (!s.isEmpty())
03681             mEditor->insert(addQuotesToText(s));
03682     }
03683 }
03684 
03685 void KMComposeWin::slotPasteClipboardAsAttachment()
03686 {
03687   KURL url( QApplication::clipboard()->text( QClipboard::Clipboard ) );
03688   if ( url.isValid() ) {
03689     addAttach(QApplication::clipboard()->text( QClipboard::Clipboard ) );
03690     return;
03691   }
03692 
03693   QMimeSource *mimeSource = QApplication::clipboard()->data();
03694   if ( QImageDrag::canDecode(mimeSource) ) {
03695     slotAttachPNGImageData(mimeSource->encodedData("image/png"));
03696   }
03697   else {
03698     bool ok;
03699     QString attName = KInputDialog::getText( "KMail", i18n("Name of the attachment:"), QString::null, &ok, this );
03700     if ( !ok )
03701       return;
03702     KMMessagePart *msgPart = new KMMessagePart;
03703     msgPart->setName(attName);
03704     QValueList<int> dummy;
03705     msgPart->setBodyAndGuessCte(QCString(QApplication::clipboard()->text().latin1()), dummy,
03706                                 kmkernel->msgSender()->sendQuotedPrintable());
03707     addAttach(msgPart);
03708   }
03709 }
03710 
03711 void KMComposeWin::slotAddQuotes()
03712 {
03713     if( mEditor->hasFocus() && msg() )
03714     {
03715         // TODO: I think this is backwards.
03716         // i.e, if no region is marked then add quotes to every line
03717         // else add quotes only on the lines that are marked.
03718 
03719         if ( mEditor->hasMarkedText() ) {
03720             QString s = mEditor->markedText();
03721             if(!s.isEmpty())
03722                 mEditor->insert(addQuotesToText(s));
03723         } else {
03724             int l =  mEditor->currentLine();
03725             int c =  mEditor->currentColumn();
03726             QString s =  mEditor->textLine(l);
03727             s.prepend(quotePrefixName());
03728             mEditor->insertLine(s,l);
03729             mEditor->removeLine(l+1);
03730             mEditor->setCursorPosition(l,c+2);
03731         }
03732     }
03733 }
03734 
03735 QString KMComposeWin::addQuotesToText(const QString &inputText)
03736 {
03737     QString answer = QString( inputText );
03738     QString indentStr = quotePrefixName();
03739     answer.replace( '\n', '\n' + indentStr);
03740     answer.prepend( indentStr );
03741     answer += '\n';
03742     return KMMessage::smartQuote( answer, GlobalSettings::self()->lineWrapWidth() );
03743 }
03744 
03745 QString KMComposeWin::removeQuotesFromText(const QString &inputText)
03746 {
03747     QString s = inputText;
03748 
03749     // remove first leading quote
03750     QString quotePrefix = '^' + quotePrefixName();
03751     QRegExp rx(quotePrefix);
03752     s.remove(rx);
03753 
03754     // now remove all remaining leading quotes
03755     quotePrefix = '\n' + quotePrefixName();
03756     rx = quotePrefix;
03757     s.replace(rx, "\n");
03758 
03759     return s;
03760 }
03761 
03762 void KMComposeWin::slotRemoveQuotes()
03763 {
03764     if( mEditor->hasFocus() && msg() )
03765     {
03766         // TODO: I think this is backwards.
03767         // i.e, if no region is marked then remove quotes from every line
03768         // else remove quotes only on the lines that are marked.
03769 
03770         if ( mEditor->hasMarkedText() ) {
03771             QString s = mEditor->markedText();
03772             mEditor->insert(removeQuotesFromText(s));
03773         } else {
03774             int l = mEditor->currentLine();
03775             int c = mEditor->currentColumn();
03776             QString s = mEditor->textLine(l);
03777             mEditor->insertLine(removeQuotesFromText(s),l);
03778             mEditor->removeLine(l+1);
03779             mEditor->setCursorPosition(l,c-2);
03780         }
03781     }
03782 }
03783 
03784 //-----------------------------------------------------------------------------
03785 void KMComposeWin::slotUndo()
03786 {
03787   QWidget* fw = focusWidget();
03788   if (!fw) return;
03789 
03790   if ( ::qt_cast<KEdit*>(fw) )
03791       static_cast<QTextEdit*>(fw)->undo();
03792   else if (::qt_cast<QLineEdit*>(fw))
03793       static_cast<QLineEdit*>(fw)->undo();
03794 }
03795 
03796 void KMComposeWin::slotRedo()
03797 {
03798   QWidget* fw = focusWidget();
03799   if (!fw) return;
03800 
03801   if (::qt_cast<KEdit*>(fw))
03802       static_cast<KEdit*>(fw)->redo();
03803   else if (::qt_cast<QLineEdit*>(fw))
03804       static_cast<QLineEdit*>(fw)->redo();
03805 }
03806 
03807 //-----------------------------------------------------------------------------
03808 void KMComposeWin::slotCut()
03809 {
03810   QWidget* fw = focusWidget();
03811   if (!fw) return;
03812 
03813   if (::qt_cast<KEdit*>(fw))
03814       static_cast<KEdit*>(fw)->cut();
03815   else if (::qt_cast<QLineEdit*>(fw))
03816       static_cast<QLineEdit*>(fw)->cut();
03817 }
03818 
03819 
03820 //-----------------------------------------------------------------------------
03821 void KMComposeWin::slotCopy()
03822 {
03823   QWidget* fw = focusWidget();
03824   if (!fw) return;
03825 
03826 #ifdef KeyPress
03827 #undef KeyPress
03828 #endif
03829 
03830   QKeyEvent k(QEvent::KeyPress, Key_C, 0, ControlButton);
03831   kapp->notify(fw, &k);
03832 }
03833 
03834 
03835 //-----------------------------------------------------------------------------
03836 void KMComposeWin::slotPasteClipboard()
03837 {
03838   paste( QClipboard::Clipboard );
03839 }
03840 
03841 void KMComposeWin::paste( QClipboard::Mode mode )
03842 {
03843   QWidget* fw = focusWidget();
03844   if (!fw) return;
03845 
03846   QMimeSource *mimeSource = QApplication::clipboard()->data( mode );
03847   if ( mimeSource->provides("image/png") )  {
03848     slotAttachPNGImageData(mimeSource->encodedData("image/png"));
03849   } else if ( KURLDrag::canDecode( mimeSource ) ) {
03850         KURL::List urlList;
03851         if( KURLDrag::decode( mimeSource, urlList ) ) {
03852             const QString asText = i18n("Add as Text");
03853             const QString asAttachment = i18n("Add as Attachment");
03854             const QString text = i18n("Please select whether you want to insert the content as text into the editor, "
03855                     "or append the referenced file as an attachment.");
03856             const QString caption = i18n("Paste as text or attachment?");
03857 
03858             int id = KMessageBox::questionYesNoCancel( this, text, caption,
03859                     KGuiItem( asText ), KGuiItem( asAttachment) );
03860             switch ( id) {
03861               case KMessageBox::Yes:
03862                 for ( KURL::List::Iterator it = urlList.begin();
03863                      it != urlList.end(); ++it ) {
03864                   mEditor->insert( (*it).url() );
03865                 }
03866                 break;
03867               case KMessageBox::No:
03868                 for ( KURL::List::Iterator it = urlList.begin();
03869                      it != urlList.end(); ++it ) {
03870                   addAttach( *it );
03871                 }
03872                 break;
03873             }
03874         }
03875   } else if ( QTextDrag::canDecode( mimeSource ) ) {
03876       QString s;
03877       if ( QTextDrag::decode( mimeSource, s ) )
03878           mEditor->insert( s );
03879   }
03880 }
03881 
03882 
03883 //-----------------------------------------------------------------------------
03884 void KMComposeWin::slotMarkAll()
03885 {
03886   QWidget* fw = focusWidget();
03887   if (!fw) return;
03888 
03889   if (::qt_cast<QLineEdit*>(fw))
03890       static_cast<QLineEdit*>(fw)->selectAll();
03891   else if (::qt_cast<KEdit*>(fw))
03892       static_cast<KEdit*>(fw)->selectAll();
03893 }
03894 
03895 
03896 //-----------------------------------------------------------------------------
03897 void KMComposeWin::slotClose()
03898 {
03899   close(false);
03900 }
03901 
03902 
03903 //-----------------------------------------------------------------------------
03904 void KMComposeWin::slotNewComposer()
03905 {
03906   KMComposeWin* win;
03907   KMMessage* msg = new KMMessage;
03908 
03909   msg->initHeader();
03910   win = new KMComposeWin(msg);
03911   win->show();
03912 }
03913 
03914 
03915 //-----------------------------------------------------------------------------
03916 void KMComposeWin::slotNewMailReader()
03917 {
03918   KMMainWin *kmmwin = new KMMainWin(0);
03919   kmmwin->show();
03920   //d->resize(d->size());
03921 }
03922 
03923 
03924 //-----------------------------------------------------------------------------
03925 void KMComposeWin::slotUpdWinTitle(const QString& text)
03926 {
03927   QString s( text );
03928   // Remove characters that show badly in most window decorations:
03929   // newlines tend to become boxes.
03930   if (text.isEmpty())
03931     setCaption("("+i18n("unnamed")+")");
03932   else setCaption( s.replace( QChar('\n'), ' ' ) );
03933 }
03934 
03935 
03936 //-----------------------------------------------------------------------------
03937 void KMComposeWin::slotEncryptToggled(bool on)
03938 {
03939   setEncryption( on, true /* set by the user */ );
03940   slotUpdateSignatureAndEncrypionStateIndicators();
03941 }
03942 
03943 
03944 //-----------------------------------------------------------------------------
03945 void KMComposeWin::setEncryption( bool encrypt, bool setByUser )
03946 {
03947   bool wasModified = isModified();
03948   if ( setByUser )
03949     setModified( true );
03950   if ( !mEncryptAction->isEnabled() )
03951     encrypt = false;
03952   // check if the user wants to encrypt messages to himself and if he defined
03953   // an encryption key for the current identity
03954   else if ( encrypt && encryptToSelf() && !mLastIdentityHasEncryptionKey ) {
03955     if ( setByUser ) {
03956       KMessageBox::sorry( this,
03957                           i18n("<qt><p>You have requested that messages be "
03958                    "encrypted to yourself, but the currently selected "
03959                    "identity does not define an (OpenPGP or S/MIME) "
03960                    "encryption key to use for this.</p>"
03961                                "<p>Please select the key(s) to use "
03962                                "in the identity configuration.</p>"
03963                                "</qt>"),
03964                           i18n("Undefined Encryption Key") );
03965       setModified( wasModified );
03966     }
03967     encrypt = false;
03968   }
03969 
03970   // make sure the mEncryptAction is in the right state
03971   mEncryptAction->setChecked( encrypt );
03972 
03973   // show the appropriate icon
03974   if ( encrypt )
03975     mEncryptAction->setIcon("encrypted");
03976   else
03977     mEncryptAction->setIcon("decrypted");
03978 
03979   // mark the attachments for (no) encryption
03980   if ( canSignEncryptAttachments() ) {
03981     for ( KMAtmListViewItem* entry =
03982             static_cast<KMAtmListViewItem*>( mAtmItemList.first() );
03983           entry;
03984           entry = static_cast<KMAtmListViewItem*>( mAtmItemList.next() ) )
03985       entry->setEncrypt( encrypt );
03986   }
03987 }
03988 
03989 
03990 //-----------------------------------------------------------------------------
03991 void KMComposeWin::slotSignToggled(bool on)
03992 {
03993   setSigning( on, true /* set by the user */ );
03994   slotUpdateSignatureAndEncrypionStateIndicators();
03995 }
03996 
03997 
03998 //-----------------------------------------------------------------------------
03999 void KMComposeWin::setSigning( bool sign, bool setByUser )
04000 {
04001   bool wasModified = isModified();
04002   if ( setByUser )
04003     setModified( true );
04004   if ( !mSignAction->isEnabled() )
04005     sign = false;
04006 
04007   // check if the user defined a signing key for the current identity
04008   if ( sign && !mLastIdentityHasSigningKey ) {
04009     if ( setByUser ) {
04010       KMessageBox::sorry( this,
04011                           i18n("<qt><p>In order to be able to sign "
04012                                "this message you first have to "
04013                                "define the (OpenPGP or S/MIME) signing key "
04014                    "to use.</p>"
04015                                "<p>Please select the key to use "
04016                                "in the identity configuration.</p>"
04017                                "</qt>"),
04018                           i18n("Undefined Signing Key") );
04019       setModified( wasModified );
04020     }
04021     sign = false;
04022   }
04023 
04024   // make sure the mSignAction is in the right state
04025   mSignAction->setChecked( sign );
04026 
04027   // mark the attachments for (no) signing
04028   if ( canSignEncryptAttachments() ) {
04029     for ( KMAtmListViewItem* entry =
04030             static_cast<KMAtmListViewItem*>( mAtmItemList.first() );
04031           entry;
04032           entry = static_cast<KMAtmListViewItem*>( mAtmItemList.next() ) )
04033       entry->setSign( sign );
04034   }
04035 }
04036 
04037 
04038 //-----------------------------------------------------------------------------
04039 void KMComposeWin::slotWordWrapToggled(bool on)
04040 {
04041   if (on)
04042   {
04043     mEditor->setWordWrap( QTextEdit::FixedColumnWidth );
04044     mEditor->setWrapColumnOrWidth( GlobalSettings::self()->lineWrapWidth() );
04045   }
04046   else
04047   {
04048     mEditor->setWordWrap( QTextEdit::WidgetWidth );
04049   }
04050 }
04051 
04052 
04053 void KMComposeWin::disableWordWrap()
04054 {
04055     mEditor->setWordWrap( QTextEdit::NoWrap );
04056 }
04057 
04058 void KMComposeWin::disableRecipientNumberCheck()
04059 {
04060   mCheckForRecipients = false;
04061 }
04062 
04063 void KMComposeWin::disableForgottenAttachmentsCheck()
04064 {
04065   mCheckForForgottenAttachments = false;
04066 }
04067 
04068 void KMComposeWin::ignoreStickyFields()
04069 {
04070   mIgnoreStickyFields = true;
04071   mBtnTransport->setChecked( false );
04072   mBtnDictionary->setChecked( false );
04073   mBtnIdentity->setChecked( false );
04074   mBtnTransport->setEnabled( false );
04075   mBtnDictionary->setEnabled( false );
04076   mBtnIdentity->setEnabled( false );
04077 }
04078 
04079 //-----------------------------------------------------------------------------
04080 void KMComposeWin::slotPrint()
04081 {
04082   mMessageWasModified = isModified();
04083   connect( this, SIGNAL( applyChangesDone( bool ) ),
04084            this, SLOT( slotContinuePrint( bool ) ) );
04085   applyChanges( true );
04086 }
04087 
04088 void KMComposeWin::slotContinuePrint( bool rc )
04089 {
04090   disconnect( this, SIGNAL( applyChangesDone( bool ) ),
04091               this, SLOT( slotContinuePrint( bool ) ) );
04092 
04093   if( rc ) {
04094     if ( mComposedMessages.isEmpty() ) {
04095       kdDebug(5006) << "Composing the message failed." << endl;
04096       return;
04097     }
04098     KMCommand *command = new KMPrintCommand( this, mComposedMessages.first() );
04099     command->start();
04100     setModified( mMessageWasModified );
04101   }
04102 }
04103 
04104 //----------------------------------------------------------------------------
04105 bool KMComposeWin::validateAddresses( QWidget * parent, const QString & addresses )
04106 {
04107   QString brokenAddress;
04108   KPIM::EmailParseResult errorCode = KMMessage::isValidEmailAddressList( KMMessage::expandAliases( addresses ), brokenAddress );
04109   if ( !( errorCode == KPIM::AddressOk || errorCode == KPIM::AddressEmpty ) ) {
04110     QString errorMsg( "<qt><p><b>" + brokenAddress +
04111                       "</b></p><p>" + KPIM::emailParseResultToString( errorCode ) +
04112                       "</p></qt>" );
04113     KMessageBox::sorry( parent, errorMsg, i18n("Invalid Email Address") );
04114     return false;
04115   }
04116   return true;
04117 }
04118 
04119 //----------------------------------------------------------------------------
04120 void KMComposeWin::doSend( KMail::MessageSender::SendMethod method,
04121                            KMComposeWin::SaveIn saveIn )
04122 {
04123   if ( method != KMail::MessageSender::SendLater && kmkernel->isOffline() ) {
04124     KMessageBox::information( this,
04125                               i18n("KMail is currently in offline mode,"
04126                                    "your messages will be kept in the outbox until you go online."),
04127                               i18n("Online/Offline"), "kmailIsOffline" );
04128     mSendMethod = KMail::MessageSender::SendLater;
04129   } else {
04130     mSendMethod = method;
04131   }
04132   mSaveIn = saveIn;
04133 
04134   if ( saveIn == KMComposeWin::None ) {
04135     if ( KPIM::getFirstEmailAddress( from() ).isEmpty() ) {
04136       if ( !( mShowHeaders & HDR_FROM ) ) {
04137         mShowHeaders |= HDR_FROM;
04138         rethinkFields( false );
04139       }
04140       mEdtFrom->setFocus();
04141       KMessageBox::sorry( this,
04142                           i18n("You must enter your email address in the "
04143                                "From: field. You should also set your email "
04144                                "address for all identities, so that you do "
04145                                "not have to enter it for each message.") );
04146       return;
04147     }
04148     if ( to().isEmpty() )
04149     {
04150         if (  cc().isEmpty() && bcc().isEmpty()) {
04151           if ( mEdtTo ) mEdtTo->setFocus();
04152           KMessageBox::information( this,
04153                                 i18n("You must specify at least one receiver,"
04154                                      "either in the To: field or as CC or as BCC.") );
04155           return;
04156         }
04157         else {
04158                 if ( mEdtTo ) mEdtTo->setFocus();
04159                 int rc =
04160                             KMessageBox::questionYesNo( this,
04161                                                         i18n("To field is missing."
04162                                                               "Send message anyway?"),
04163                                                         i18n("No To: specified") );
04164                 if ( rc == KMessageBox::No ){
04165                    return;
04166                 }
04167         }
04168     }
04169 
04170     // Validate the To:, CC: and BCC fields
04171     if ( !validateAddresses( this, to().stripWhiteSpace() ) ) {
04172       return;
04173     }
04174 
04175     if ( !validateAddresses( this, cc().stripWhiteSpace() ) ) {
04176       return;
04177     }
04178 
04179     if ( !validateAddresses( this, bcc().stripWhiteSpace() ) ) {
04180       return;
04181     }
04182 
04183     if (subject().isEmpty())
04184     {
04185         mEdtSubject->setFocus();
04186         int rc =
04187           KMessageBox::questionYesNo( this,
04188                                       i18n("You did not specify a subject. "
04189                                            "Send message anyway?"),
04190                                       i18n("No Subject Specified"),
04191                                       i18n("S&end as Is"),
04192                                       i18n("&Specify the Subject"),
04193                                       "no_subject_specified" );
04194         if( rc == KMessageBox::No )
04195         {
04196            return;
04197         }
04198     }
04199 
04200     if ( userForgotAttachment() )
04201       return;
04202   }
04203 
04204   KCursorSaver busy(KBusyPtr::busy());
04205   mMsg->setDateToday();
04206 
04207   // If a user sets up their outgoing messages preferences wrong and then
04208   // sends mail that gets 'stuck' in their outbox, they should be able to
04209   // rectify the problem by editing their outgoing preferences and
04210   // resending.
04211   // Hence this following conditional
04212   QString hf = mMsg->headerField("X-KMail-Transport");
04213   if ((mTransport->currentText() != mTransport->text(0)) ||
04214       (!hf.isEmpty() && (hf != mTransport->text(0))))
04215     mMsg->setHeaderField("X-KMail-Transport", mTransport->currentText());
04216 
04217   mDisableBreaking = ( saveIn != KMComposeWin::None );
04218 
04219   const bool neverEncrypt = ( mDisableBreaking && GlobalSettings::self()->neverEncryptDrafts() )
04220                            || mSigningAndEncryptionExplicitlyDisabled;
04221   connect( this, SIGNAL( applyChangesDone( bool ) ),
04222            SLOT( slotContinueDoSend( bool ) ) );
04223 
04224   if ( mEditor->textFormat() == Qt::RichText )
04225     mMsg->setHeaderField( "X-KMail-Markup", "true" );
04226   else
04227     mMsg->removeHeaderField( "X-KMail-Markup" );
04228   if ( mEditor->textFormat() == Qt::RichText && inlineSigningEncryptionSelected() ) {
04229     QString keepBtnText = mEncryptAction->isChecked() ?
04230       mSignAction->isChecked() ? i18n( "&Keep markup, do not sign/encrypt" )
04231                                : i18n( "&Keep markup, do not encrypt" )
04232       : i18n( "&Keep markup, do not sign" );
04233     QString yesBtnText = mEncryptAction->isChecked() ?
04234       mSignAction->isChecked() ? i18n("Sign/Encrypt (delete markup)")
04235       : i18n( "Encrypt (delete markup)" )
04236       : i18n( "Sign (delete markup)" );
04237     int ret = KMessageBox::warningYesNoCancel(this,
04238                                       i18n("<qt><p>Inline signing/encrypting of HTML messages is not possible;</p>"
04239                                            "<p>do you want to delete your markup?</p></qt>"),
04240                                            i18n("Sign/Encrypt Message?"),
04241                                            KGuiItem( yesBtnText ),
04242                                            KGuiItem( keepBtnText ) );
04243     if ( KMessageBox::Cancel == ret )
04244       return;
04245     if ( KMessageBox::No == ret ) {
04246       mEncryptAction->setChecked(false);
04247       mSignAction->setChecked(false);
04248     }
04249     else {
04250       toggleMarkup(false);
04251     }
04252   }
04253 
04254   if (neverEncrypt && saveIn != KMComposeWin::None ) {
04255       // we can't use the state of the mail itself, to remember the
04256       // signing and encryption state, so let's add a header instead
04257     mMsg->setHeaderField( "X-KMail-SignatureActionEnabled", mSignAction->isChecked()? "true":"false" );
04258     mMsg->setHeaderField( "X-KMail-EncryptActionEnabled", mEncryptAction->isChecked()? "true":"false"  );
04259     mMsg->setHeaderField( "X-KMail-CryptoMessageFormat", QString::number( cryptoMessageFormat() ) );
04260   } else {
04261     mMsg->removeHeaderField( "X-KMail-SignatureActionEnabled" );
04262     mMsg->removeHeaderField( "X-KMail-EncryptActionEnabled" );
04263     mMsg->removeHeaderField( "X-KMail-CryptoMessageFormat" );
04264   }
04265 
04266 
04267   kdDebug(5006) << "KMComposeWin::doSend() - calling applyChanges()"
04268                 << endl;
04269   applyChanges( neverEncrypt );
04270 }
04271 
04272 bool KMComposeWin::saveDraftOrTemplate( const QString &folderName,
04273                                         KMMessage *msg )
04274 {
04275   KMFolder *theFolder = 0, *imapTheFolder = 0;
04276   // get the draftsFolder
04277   if ( !folderName.isEmpty() ) {
04278     theFolder = kmkernel->folderMgr()->findIdString( folderName );
04279     if ( theFolder == 0 )
04280       // This is *NOT* supposed to be "imapDraftsFolder", because a
04281       // dIMAP folder works like a normal folder
04282       theFolder = kmkernel->dimapFolderMgr()->findIdString( folderName );
04283     if ( theFolder == 0 )
04284       imapTheFolder = kmkernel->imapFolderMgr()->findIdString( folderName );
04285     if ( !theFolder && !imapTheFolder ) {
04286       const KPIM::Identity & id = kmkernel->identityManager()
04287         ->identityForUoidOrDefault( msg->headerField( "X-KMail-Identity" ).stripWhiteSpace().toUInt() );
04288       KMessageBox::information( 0,
04289                                 i18n("The custom drafts or templates folder for "
04290                                      "identify \"%1\" does not exist (anymore); "
04291                                      "therefore, the default drafts or templates "
04292                                      "folder will be used.")
04293                                 .arg( id.identityName() ) );
04294     }
04295   }
04296   if ( imapTheFolder && imapTheFolder->noContent() )
04297     imapTheFolder = 0;
04298 
04299   bool didOpen = false;
04300   if ( theFolder == 0 ) {
04301     theFolder = ( mSaveIn==KMComposeWin::Drafts ?
04302                   kmkernel->draftsFolder() : kmkernel->templatesFolder() );
04303   } else {
04304     //XXX this looks really, really fishy
04305     theFolder->open( "composer" );
04306     didOpen = true;
04307   }
04308   kdDebug(5006) << k_funcinfo << "theFolder=" << theFolder->name() << endl;
04309   if ( imapTheFolder )
04310     kdDebug(5006) << k_funcinfo << "imapTheFolder=" << imapTheFolder->name() << endl;
04311 
04312   bool sentOk = !( theFolder->addMsg( msg ) );
04313 
04314   // Ensure the message is correctly and fully parsed
04315   theFolder->unGetMsg( theFolder->count() - 1 );
04316   msg = theFolder->getMsg( theFolder->count() - 1 );
04317   // Does that assignment needs to be propagated out to the caller?
04318   // Assuming the send is OK, the iterator is set to 0 immediately afterwards.
04319   if ( imapTheFolder ) {
04320     // move the message to the imap-folder and highlight it
04321     imapTheFolder->moveMsg( msg );
04322     (static_cast<KMFolderImap*>( imapTheFolder->storage() ))->getFolder();
04323   }
04324 
04325   if ( didOpen )
04326     theFolder->close( "composer" );
04327   return sentOk;
04328 }
04329 
04330 void KMComposeWin::slotContinueDoSend( bool sentOk )
04331 {
04332   kdDebug(5006) << "KMComposeWin::slotContinueDoSend( " << sentOk << " )"
04333                 << endl;
04334   disconnect( this, SIGNAL( applyChangesDone( bool ) ),
04335               this, SLOT( slotContinueDoSend( bool ) ) );
04336 
04337   if ( !sentOk ) {
04338     mDisableBreaking = false;
04339     return;
04340   }
04341 
04342   for ( QValueVector<KMMessage*>::iterator it = mComposedMessages.begin() ; it != mComposedMessages.end() ; ++it ) {
04343 
04344     // remove fields that contain no data (e.g. an empty Cc: or Bcc:)
04345     (*it)->cleanupHeader();
04346 
04347     // needed for imap
04348     (*it)->setComplete( true );
04349 
04350     if ( mSaveIn==KMComposeWin::Drafts ) {
04351       sentOk = saveDraftOrTemplate( (*it)->drafts(), (*it) );
04352     } else if ( mSaveIn==KMComposeWin::Templates ) {
04353       sentOk = saveDraftOrTemplate( (*it)->templates(), (*it) );
04354     } else {
04355       (*it)->setTo( KMMessage::expandAliases( to() ));
04356       (*it)->setCc( KMMessage::expandAliases( cc() ));
04357       if( !mComposer->originalBCC().isEmpty() )
04358     (*it)->setBcc( KMMessage::expandAliases( mComposer->originalBCC() ));
04359       QString recips = (*it)->headerField( "X-KMail-Recipients" );
04360       if( !recips.isEmpty() ) {
04361     (*it)->setHeaderField( "X-KMail-Recipients", KMMessage::expandAliases( recips ), KMMessage::Address );
04362       }
04363       (*it)->cleanupHeader();
04364       sentOk = kmkernel->msgSender()->send((*it), mSendMethod);
04365     }
04366 
04367     if (!sentOk)
04368       return;
04369 
04370     *it = 0; // don't kill it later...
04371   }
04372 
04373   RecentAddresses::self( KMKernel::config() )->add( bcc() );
04374   RecentAddresses::self( KMKernel::config() )->add( cc() );
04375   RecentAddresses::self( KMKernel::config() )->add( to() );
04376 
04377   setModified( false );
04378   mAutoDeleteMsg = false;
04379   mFolder = 0;
04380   cleanupAutoSave();
04381   close();
04382   return;
04383 }
04384 
04385 bool KMComposeWin::checkTransport() const
04386 {
04387   if ( KMail::TransportManager::transportNames().isEmpty() ) {
04388     KMessageBox::information( mMainWidget,
04389                               i18n("Please create an account for sending and try again.") );
04390     return false;
04391   }
04392   return true;
04393 
04394 }
04395 
04396 //----------------------------------------------------------------------------
04397 void KMComposeWin::slotSendLater()
04398 {
04399   if ( !checkTransport() )
04400     return;
04401   if ( !checkRecipientNumber() )
04402     return;
04403   if ( mEditor->checkExternalEditorFinished() )
04404     doSend( KMail::MessageSender::SendLater );
04405 }
04406 
04407 
04408 //----------------------------------------------------------------------------
04409 void KMComposeWin::slotSaveDraft() {
04410   if ( mEditor->checkExternalEditorFinished() )
04411     doSend( KMail::MessageSender::SendLater, KMComposeWin::Drafts );
04412 }
04413 
04414 //----------------------------------------------------------------------------
04415 void KMComposeWin::slotSaveTemplate() {
04416   if ( mEditor->checkExternalEditorFinished() )
04417     doSend( KMail::MessageSender::SendLater, KMComposeWin::Templates );
04418 }
04419 
04420 //----------------------------------------------------------------------------
04421 void KMComposeWin::slotSendNowVia( int item )
04422 {
04423   QStringList availTransports= KMail::TransportManager::transportNames();
04424   QString customTransport = availTransports[ item ];
04425 
04426   mTransport->setCurrentText( customTransport );
04427   slotSendNow();
04428 }
04429 
04430 //----------------------------------------------------------------------------
04431 void KMComposeWin::slotSendLaterVia( int item )
04432 {
04433   QStringList availTransports= KMail::TransportManager::transportNames();
04434   QString customTransport = availTransports[ item ];
04435 
04436   mTransport->setCurrentText( customTransport );
04437   slotSendLater();
04438 }
04439 
04440 
04441 //----------------------------------------------------------------------------
04442 void KMComposeWin::slotSendNow() {
04443   if ( !mEditor->checkExternalEditorFinished() )
04444     return;
04445   if ( !checkTransport() )
04446     return;
04447   if ( !checkRecipientNumber() )
04448     return;
04449   if ( GlobalSettings::self()->confirmBeforeSend() )
04450   {
04451     int rc = KMessageBox::warningYesNoCancel( mMainWidget,
04452                                         i18n("About to send email..."),
04453                                         i18n("Send Confirmation"),
04454                                         i18n("&Send Now"),
04455                                         i18n("Send &Later") );
04456 
04457     if ( rc == KMessageBox::Yes )
04458       doSend( KMail::MessageSender::SendImmediate );
04459     else if ( rc == KMessageBox::No )
04460       doSend( KMail::MessageSender::SendLater );
04461   }
04462   else
04463     doSend( KMail::MessageSender::SendImmediate );
04464 }
04465 
04466 
04467 //----------------------------------------------------------------------------
04468 bool KMComposeWin::checkRecipientNumber() const
04469 {
04470   uint thresHold = GlobalSettings::self()->recipientThreshold();
04471   if ( mCheckForRecipients &&
04472        GlobalSettings::self()->tooManyRecipients() &&
04473        mRecipientsEditor->recipients().count() > thresHold ) {
04474     if ( KMessageBox::questionYesNo( mMainWidget,
04475                                i18n("You are trying to send the mail to more than %1 recipients. Send message anyway?").arg(thresHold),
04476                                i18n("Too many receipients"),
04477                                i18n("&Send as Is"),
04478                                i18n("&Edit Recipients")) == KMessageBox::No ) {
04479       return false;
04480     }
04481   }
04482   return true;
04483 }
04484 
04485 
04486 //----------------------------------------------------------------------------
04487 void KMComposeWin::slotAppendSignature()
04488 {
04489     insertSignature();
04490 }
04491 
04492 //----------------------------------------------------------------------------
04493 void KMComposeWin::slotPrependSignature()
04494 {
04495     insertSignature( Prepend );
04496 }
04497 
04498 //----------------------------------------------------------------------------
04499 void KMComposeWin::slotInsertSignatureAtCursor()
04500 {
04501     insertSignature( AtCursor );
04502 }
04503 
04504 //----------------------------------------------------------------------------
04505 void KMComposeWin::insertSignature( SignaturePlacement placement )
04506 {
04507    bool mod = mEditor->isModified();
04508 
04509    const KPIM::Identity &ident =
04510      kmkernel->identityManager()->
04511      identityForUoidOrDefault( mIdentity->currentIdentity() );
04512 
04513    mOldSigText = GlobalSettings::self()->prependSignature()? ident.signature().rawText() : ident.signatureText();
04514 
04515    if( !mOldSigText.isEmpty() )
04516    {
04517     mEditor->sync();
04518     int paragraph, index;
04519     mEditor->getCursorPosition( &paragraph, &index );
04520     index = mEditor->indexOfCurrentLineStart( paragraph, index );
04521 
04522     switch( placement ) {
04523       case Append:
04524         mEditor->setText( mEditor->text() + mOldSigText );
04525         break;
04526       case Prepend:
04527         mOldSigText = "\n\n" + mOldSigText + "\n";
04528         mEditor->insertAt( mOldSigText, paragraph, index );
04529         break;
04530       case AtCursor:
04531 
04532         // If there is text in the same line, add a newline so that the stuff in
04533         // the current line moves after the signature. Also remove a leading newline, it is not
04534         // needed here.
04535         if ( mEditor->paragraphLength( paragraph ) > 0 )
04536           mOldSigText = mOldSigText + "\n";
04537         if ( mOldSigText.startsWith( "\n" ) )
04538           mOldSigText = mOldSigText.remove( 0, 1 );
04539 
04540         // If we are inserting into a wordwrapped line, add a newline at the start to make
04541         // the text edit hard-wrap the line here
04542         if ( index != 0 )
04543           mOldSigText = "\n" + mOldSigText;
04544 
04545         mEditor->insertAt( mOldSigText, paragraph, index );
04546         break;
04547     }
04548     mEditor->update();
04549     mEditor->setModified(mod);
04550 
04551     if (  mPreserveUserCursorPosition ) {
04552       mEditor->setCursorPositionFromStart( (unsigned int) mMsg->getCursorPos() );
04553       // Only keep the cursor from the mMsg *once* based on the
04554       // preserve-cursor-position setting; this handles the case where
04555       // the message comes from a template with a specific cursor
04556       // position set and the signature is appended automatically.
04557       mPreserveUserCursorPosition = false;
04558     } else {
04559       // for append and prepend, move the cursor to 0,0, for insertAt,
04560       // keep it in the same row, but move to first column
04561       if ( index == 0 ) {
04562         mEditor->setCursorPosition( paragraph, 0 );
04563       } else {
04564         // For word-wrapped lines, we have created a new paragraph, so change to that one
04565         mEditor->setCursorPosition( paragraph + 1, 0 );
04566       }
04567       if ( placement == Prepend || placement == Append )
04568         mEditor->setContentsPos( 0, 0 );
04569     }
04570     mEditor->sync();
04571   }
04572 }
04573 
04574 //-----------------------------------------------------------------------------
04575 void KMComposeWin::slotHelp()
04576 {
04577   kapp->invokeHelp();
04578 }
04579 
04580 //-----------------------------------------------------------------------------
04581 void KMComposeWin::slotCleanSpace()
04582 {
04583   // Originally we simply used the KEdit::cleanWhiteSpace() method,
04584   // but that code doesn't handle quoted-lines or signatures, so instead
04585   // we now simply use regexp's to squeeze sequences of tabs and spaces
04586   // into a single space, and make sure all our lines are single-spaced.
04587   //
04588   // Yes, extra space in a quote string is squeezed.
04589   // Signatures are respected (i.e. not cleaned).
04590 
04591   QString s;
04592   if ( mEditor->hasMarkedText() ) {
04593     s = mEditor->markedText();
04594     if( s.isEmpty() )
04595       return;
04596   } else {
04597     s = mEditor->text();
04598   }
04599 
04600   // Remove the signature for now.
04601   QString sig;
04602   bool restore = false;
04603   const KPIM::Identity & ident =
04604     kmkernel->identityManager()->identityForUoid( mId );
04605   if ( !ident.isNull() ) {
04606     sig = ident.signatureText();
04607     if( !sig.isEmpty() ) {
04608       if( s.endsWith( sig ) ) {
04609         s.truncate( s.length() - sig.length() );
04610         restore = true;
04611       }
04612     }
04613   }
04614 
04615   // Squeeze tabs and spaces
04616   QRegExp squeeze( "[\t ]+" );
04617   s.replace( squeeze, QChar( ' ' ) );
04618 
04619   // Remove trailing whitespace
04620   QRegExp trailing( "\\s+$" );
04621   s.replace( trailing, QChar( '\n' ) );
04622 
04623   // Single space lines
04624   QRegExp singleSpace( "[\n]{2,}" );
04625   s.replace( singleSpace, QChar( '\n' ) );
04626 
04627   // Restore the signature
04628   if ( restore )
04629     s.append( sig );
04630 
04631   // Put the new text in place.
04632   // The lines below do not clear the undo history, but unfortuately cause
04633   // the side-effect that you need to press Ctrl-Z twice (first Ctrl-Z will
04634   // show cleared text area) to get back the original, pre-cleaned text.
04635   // If you use mEditor->setText( s ) then the undo history is cleared so
04636   // that isn't a good solution either.
04637   // TODO: is Qt4 better at handling the undo history??
04638   if ( !mEditor->hasMarkedText() )
04639     mEditor->clear();
04640   mEditor->insert( s );
04641 }
04642 
04643 //-----------------------------------------------------------------------------
04644 void KMComposeWin::slotToggleMarkup()
04645 {
04646  if ( markupAction->isChecked() ) {
04647     mHtmlMarkup = true;
04648     toolBar("htmlToolBar")->show();
04649    // markup will be toggled as soon as markup is actually used
04650    fontChanged( mEditor->currentFont() ); // set buttons in correct position
04651    mSaveFont = mEditor->currentFont();
04652  }
04653  else
04654    toggleMarkup(false);
04655 
04656 }
04657 //-----------------------------------------------------------------------------
04658 void KMComposeWin::toggleMarkup(bool markup)
04659 {
04660   if ( markup ) {
04661     if ( !mUseHTMLEditor ) {
04662       kdDebug(5006) << "setting RichText editor" << endl;
04663       mUseHTMLEditor = true; // set it directly to true. setColor hits another toggleMarkup
04664       mHtmlMarkup = true;
04665 
04666       // set all highlighted text caused by spelling back to black
04667       int paraFrom, indexFrom, paraTo, indexTo;
04668       mEditor->getSelection ( &paraFrom, &indexFrom, &paraTo, &indexTo);
04669       mEditor->selectAll();
04670       // save the buttonstates because setColor calls fontChanged
04671       bool _bold = textBoldAction->isChecked();
04672       bool _italic = textItalicAction->isChecked();
04673       mEditor->setColor(QColor(0,0,0));
04674       textBoldAction->setChecked(_bold);
04675       textItalicAction->setChecked(_italic);
04676       mEditor->setSelection ( paraFrom, indexFrom, paraTo, indexTo);
04677 
04678       mEditor->setTextFormat(Qt::RichText);
04679       mEditor->setModified(true);
04680       markupAction->setChecked(true);
04681       toolBar( "htmlToolBar" )->show();
04682       mEditor->deleteAutoSpellChecking();
04683       mAutoSpellCheckingAction->setChecked(false);
04684       slotAutoSpellCheckingToggled(false);
04685     }
04686   } else { // markup is to be turned off
04687     kdDebug(5006) << "setting PlainText editor" << endl;
04688     mHtmlMarkup = false;
04689     toolBar("htmlToolBar")->hide();
04690     if ( mUseHTMLEditor ) { // it was turned on
04691       mUseHTMLEditor = false;
04692       mEditor->setTextFormat(Qt::PlainText);
04693       QString text = mEditor->text();
04694       mEditor->setText(text); // otherwise the text still looks formatted
04695       mEditor->setModified(true);
04696       slotAutoSpellCheckingToggled(true);
04697     }
04698   }
04699 }
04700 
04701 void KMComposeWin::htmlToolBarVisibilityChanged( bool visible )
04702 {
04703   // disable markup if the user hides the HTML toolbar
04704   if ( !visible ) {
04705     markupAction->setChecked( false );
04706     toggleMarkup( false );
04707   }
04708 }
04709 
04710 void KMComposeWin::slotSubjectTextSpellChecked()
04711 {
04712   mSubjectTextWasSpellChecked = true;
04713 }
04714 
04715 //-----------------------------------------------------------------------------
04716 void KMComposeWin::slotAutoSpellCheckingToggled( bool on )
04717 {
04718   if ( mEditor->autoSpellChecking(on) == -1 ) {
04719     mAutoSpellCheckingAction->setChecked(false); // set it to false again
04720   }
04721 
04722   QString temp;
04723   if ( on )
04724     temp = i18n( "Spellcheck: on" );
04725   else
04726     temp = i18n( "Spellcheck: off" );
04727   statusBar()->changeItem( temp, 3 );
04728 }
04729 //-----------------------------------------------------------------------------
04730 void KMComposeWin::slotSpellcheck()
04731 {
04732   if (mSpellCheckInProgress) return;
04733   mSubjectTextWasSpellChecked = false;
04734   mSpellCheckInProgress=true;
04735   /*
04736     connect (mEditor, SIGNAL (spellcheck_progress (unsigned)),
04737     this, SLOT (spell_progress (unsigned)));
04738     */
04739 
04740   mEditor->spellcheck();
04741 }
04742 //-----------------------------------------------------------------------------
04743 void KMComposeWin::slotUpdateSignatureActions()
04744 {
04745   //Check if an identity has signature or not and turn on/off actions in the
04746   //edit menu accordingly.
04747   const KPIM::Identity & ident =
04748     kmkernel->identityManager()->identityForUoidOrDefault( mIdentity->currentIdentity() );
04749   QString sig = ident.signatureText();
04750 
04751   if ( sig.isEmpty() ) {
04752      mAppendSignatureAction->setEnabled( false );
04753      mPrependSignatureAction->setEnabled( false );
04754      mInsertSignatureAction->setEnabled( false );
04755   }
04756   else {
04757       mAppendSignatureAction->setEnabled( true );
04758       mPrependSignatureAction->setEnabled( true );
04759       mInsertSignatureAction->setEnabled( true );
04760   }
04761 }
04762 
04763 void KMComposeWin::polish()
04764 {
04765   // Ensure the html toolbar is appropriately shown/hidden
04766   markupAction->setChecked(mHtmlMarkup);
04767   if (mHtmlMarkup)
04768     toolBar("htmlToolBar")->show();
04769   else
04770     toolBar("htmlToolBar")->hide();
04771   KMail::Composer::polish();
04772 }
04773 
04774 //-----------------------------------------------------------------------------
04775 void KMComposeWin::slotSpellcheckDone(int result)
04776 {
04777   kdDebug(5006) << "spell check complete: result = " << result << endl;
04778   mSpellCheckInProgress=false;
04779 
04780   switch( result )
04781   {
04782     case KS_CANCEL:
04783       statusBar()->changeItem(i18n(" Spell check canceled."),0);
04784       break;
04785     case KS_STOP:
04786       statusBar()->changeItem(i18n(" Spell check stopped."),0);
04787       break;
04788     default:
04789       statusBar()->changeItem(i18n(" Spell check complete."),0);
04790       break;
04791   }
04792   QTimer::singleShot( 2000, this, SLOT(slotSpellcheckDoneClearStatus()) );
04793 }
04794 
04795 void KMComposeWin::slotSpellcheckDoneClearStatus()
04796 {
04797   statusBar()->changeItem("", 0);
04798 }
04799 
04800 
04801 //-----------------------------------------------------------------------------
04802 void KMComposeWin::slotIdentityChanged( uint uoid )
04803 {
04804   const KPIM::Identity & ident =
04805     kmkernel->identityManager()->identityForUoid( uoid );
04806   if( ident.isNull() ) return;
04807 
04808   //Turn on/off signature actions if identity has no signature.
04809   slotUpdateSignatureActions();
04810 
04811   if( !ident.fullEmailAddr().isNull() )
04812     mEdtFrom->setText(ident.fullEmailAddr());
04813   // make sure the From field is shown if it does not contain a valid email address
04814   if ( KPIM::getFirstEmailAddress( from() ).isEmpty() )
04815     mShowHeaders |= HDR_FROM;
04816   if ( mEdtReplyTo ) mEdtReplyTo->setText(ident.replyToAddr());
04817 
04818   if ( mRecipientsEditor ) {
04819     // remove BCC of old identity and add BCC of new identity (if they differ)
04820     const KPIM::Identity & oldIdentity =
04821       kmkernel->identityManager()->identityForUoidOrDefault( mId );
04822     if ( oldIdentity.bcc() != ident.bcc() ) {
04823       mRecipientsEditor->removeRecipient( oldIdentity.bcc(), Recipient::Bcc );
04824       mRecipientsEditor->addRecipient( ident.bcc(), Recipient::Bcc );
04825       mRecipientsEditor->setFocusBottom();
04826     }
04827   }
04828 
04829   // don't overwrite the BCC field under certain circomstances
04830   // NOT edited and preset BCC from the identity
04831   if( mEdtBcc && !mEdtBcc->edited() && !ident.bcc().isEmpty() ) {
04832     // BCC NOT empty AND contains a diff adress then the preset BCC
04833     // of the new identity
04834     if( !mEdtBcc->text().isEmpty() && mEdtBcc->text() != ident.bcc() && !mEdtBcc->edited() ) {
04835       mEdtBcc->setText( ident.bcc() );
04836     } else {
04837       // user type into the editbox an address that != to the preset bcc
04838       // of the identity, we assume that since the user typed it
04839       // they want to keep it
04840       if ( mEdtBcc->text() != ident.bcc() && !mEdtBcc->text().isEmpty() ) {
04841         QString temp_string( mEdtBcc->text() + QString::fromLatin1(",") + ident.bcc() );
04842         mEdtBcc->setText( temp_string );
04843       } else {
04844         // if the user typed the same address as the preset BCC
04845         // from the identity we will overwrite it to avoid duplicates.
04846         mEdtBcc->setText( ident.bcc() );
04847       }
04848     }
04849   }
04850   // user edited the bcc box and has a preset bcc in the identity
04851   // we will append whatever the user typed to the preset address
04852   // allowing the user to keep all addresses
04853   if( mEdtBcc && mEdtBcc->edited() && !ident.bcc().isEmpty() ) {
04854     if( !mEdtBcc->text().isEmpty() ) {
04855       QString temp_string ( mEdtBcc->text() + QString::fromLatin1(",") + ident.bcc() );
04856       mEdtBcc->setText( temp_string );
04857     } else {
04858       mEdtBcc->setText( ident.bcc() );
04859     }
04860   }
04861   // user typed nothing and the identity does not have a preset bcc
04862   // we then reset the value to get rid of any previous
04863   // values if the user changed identity mid way through.
04864   if( mEdtBcc && !mEdtBcc->edited() && ident.bcc().isEmpty() ) {
04865     mEdtBcc->setText( ident.bcc() );
04866   }
04867   // make sure the BCC field is shown because else it's ignored
04868   if ( !ident.bcc().isEmpty() ) {
04869     mShowHeaders |= HDR_BCC;
04870   }
04871 
04872   if ( ident.organization().isEmpty() )
04873     mMsg->removeHeaderField("Organization");
04874   else
04875     mMsg->setHeaderField("Organization", ident.organization());
04876 
04877   if (!ident.isXFaceEnabled() || ident.xface().isEmpty())
04878     mMsg->removeHeaderField("X-Face");
04879   else
04880   {
04881     QString xface = ident.xface();
04882     if (!xface.isEmpty())
04883     {
04884       int numNL = ( xface.length() - 1 ) / 70;
04885       for ( int i = numNL; i > 0; --i )
04886         xface.insert( i*70, "\n\t" );
04887       mMsg->setHeaderField("X-Face", xface);
04888     }
04889   }
04890 
04891   if ( !mBtnTransport->isChecked() && !mIgnoreStickyFields ) {
04892     QString transp = ident.transport();
04893     if ( transp.isEmpty() )
04894     {
04895       mMsg->removeHeaderField("X-KMail-Transport");
04896       transp = GlobalSettings::self()->defaultTransport();
04897     }
04898     else
04899       mMsg->setHeaderField("X-KMail-Transport", transp);
04900     setTransport( transp );
04901   }
04902 
04903   if ( !mBtnDictionary->isChecked() && !mIgnoreStickyFields ) {
04904     mDictionaryCombo->setCurrentByDictionary( ident.dictionary() );
04905   }
04906 
04907   if ( !mBtnFcc->isChecked() && !mPreventFccOverwrite ) {
04908     setFcc( ident.fcc() );
04909   }
04910 
04911   QString edtText = mEditor->text();
04912 
04913   if ( mOldSigText.isEmpty() ) {
04914     const KPIM::Identity &id =
04915       kmkernel->
04916       identityManager()->
04917       identityForUoidOrDefault( mMsg->headerField( "X-KMail-Identity" ).
04918                                 stripWhiteSpace().toUInt() );
04919     mOldSigText = GlobalSettings::self()->prependSignature() ? id.signature().rawText() : id.signatureText();
04920   }
04921 
04922 
04923   if ( !GlobalSettings::prependSignature() ) {
04924     // try to truncate the old sig
04925     // First remove any trailing whitespace
04926     while ( !edtText.isEmpty() && edtText[edtText.length()-1].isSpace() )
04927       edtText.truncate( edtText.length() - 1 );
04928     // From the sig too, just in case
04929     while ( !mOldSigText.isEmpty() && mOldSigText[mOldSigText.length()-1].isSpace() )
04930       mOldSigText.truncate( mOldSigText.length() - 1 );
04931 
04932     if ( edtText.endsWith( mOldSigText ) )
04933       edtText.truncate( edtText.length() - mOldSigText.length() );
04934 
04935     // now append the new sig
04936     mOldSigText = ident.signatureText();
04937     if( ( !mOldSigText.isEmpty() ) &&
04938         ( GlobalSettings::self()->autoTextSignature() == "auto" ) ) {
04939       edtText.append( mOldSigText );
04940     }
04941     mEditor->setText( edtText );
04942   } else {
04943     const int pos = edtText.find( mOldSigText );
04944     if ( pos >= 0 && !mOldSigText.isEmpty() ) {
04945       const int oldLength = mOldSigText.length();
04946       mOldSigText = "\n\n"+ ident.signature().rawText() + "\n"; // see insertSignature()
04947       edtText = edtText.replace( pos, oldLength, mOldSigText );
04948       mEditor->setText( edtText );
04949     } else {
04950       insertSignature( Append );
04951     }
04952   }
04953 
04954   // disable certain actions if there is no PGP user identity set
04955   // for this profile
04956   bool bNewIdentityHasSigningKey = !ident.pgpSigningKey().isEmpty() || !ident.smimeSigningKey().isEmpty();
04957   bool bNewIdentityHasEncryptionKey = !ident.pgpSigningKey().isEmpty() || !ident.smimeSigningKey().isEmpty();
04958   mAttachMPK->setEnabled( Kleo::CryptoBackendFactory::instance()->openpgp() &&
04959               !ident.pgpEncryptionKey().isEmpty() );
04960   // save the state of the sign and encrypt button
04961   if ( !bNewIdentityHasEncryptionKey && mLastIdentityHasEncryptionKey ) {
04962     mLastEncryptActionState = mEncryptAction->isChecked();
04963     setEncryption( false );
04964   }
04965   if ( !bNewIdentityHasSigningKey && mLastIdentityHasSigningKey ) {
04966     mLastSignActionState = mSignAction->isChecked();
04967     setSigning( false );
04968   }
04969   // restore the last state of the sign and encrypt button
04970   if ( bNewIdentityHasEncryptionKey && !mLastIdentityHasEncryptionKey )
04971       setEncryption( mLastEncryptActionState );
04972   if ( bNewIdentityHasSigningKey && !mLastIdentityHasSigningKey )
04973     setSigning( mLastSignActionState );
04974 
04975   mLastIdentityHasSigningKey = bNewIdentityHasSigningKey;
04976   mLastIdentityHasEncryptionKey = bNewIdentityHasEncryptionKey;
04977 
04978   setModified( true );
04979   mId = uoid;
04980 
04981   // make sure the From and BCC fields are shown if necessary
04982   rethinkFields( false );
04983 }
04984 
04985 //-----------------------------------------------------------------------------
04986 void KMComposeWin::slotSpellcheckConfig()
04987 {
04988   KDialogBase dlg(KDialogBase::Plain, i18n("Spellchecker"),
04989                   KDialogBase::Ok|KDialogBase::Cancel, KDialogBase::Ok,
04990                   this, 0, true, true );
04991   KWin kwin;
04992   QTabDialog qtd (this, "tabdialog", true);
04993   KSpellConfig mKSpellConfig (&qtd);
04994   mKSpellConfig.layout()->setMargin( KDialog::marginHint() );
04995 
04996   qtd.addTab (&mKSpellConfig, i18n("Spellchecker"));
04997   qtd.setCancelButton ();
04998 
04999   kwin.setIcons (qtd.winId(), kapp->icon(), kapp->miniIcon());
05000   qtd.setCancelButton(KStdGuiItem::cancel().text());
05001   qtd.setOkButton(KStdGuiItem::ok().text());
05002 
05003   if (qtd.exec())
05004     mKSpellConfig.writeGlobalSettings();
05005 }
05006 
05007 //-----------------------------------------------------------------------------
05008 void KMComposeWin::slotStatusMessage(const QString &message)
05009 {
05010     statusBar()->changeItem( message, 0 );
05011 }
05012 
05013 void KMComposeWin::slotEditToolbars()
05014 {
05015   saveMainWindowSettings(KMKernel::config(), "Composer");
05016   KEditToolbar dlg(guiFactory(), this);
05017 
05018   connect( &dlg, SIGNAL(newToolbarConfig()),
05019            SLOT(slotUpdateToolbars()) );
05020 
05021   dlg.exec();
05022 }
05023 
05024 void KMComposeWin::slotUpdateToolbars()
05025 {
05026   createGUI("kmcomposerui.rc");
05027   applyMainWindowSettings(KMKernel::config(), "Composer");
05028 }
05029 
05030 void KMComposeWin::slotEditKeys()
05031 {
05032   KKeyDialog::configure( actionCollection(),
05033                          false /*don't allow one-letter shortcuts*/
05034                          );
05035 }
05036 
05037 void KMComposeWin::setReplyFocus( bool hasMessage )
05038 {
05039   mEditor->setFocus();
05040   if ( hasMessage ) {
05041     if( mMsg->getCursorPos() ) {
05042       mEditor->setCursorPositionFromStart( (unsigned int) mMsg->getCursorPos() );
05043     } else {
05044       mEditor->setCursorPosition( 1, 0 );
05045     }
05046   }
05047 }
05048 
05049 void KMComposeWin::setFocusToSubject()
05050 {
05051   mEdtSubject->setFocus();
05052 }
05053 
05054 int KMComposeWin::autoSaveInterval() const
05055 {
05056   return GlobalSettings::self()->autosaveInterval() * 1000 * 60;
05057 }
05058 
05059 void KMComposeWin::initAutoSave()
05060 {
05061   kdDebug(5006) << k_funcinfo << endl;
05062   // make sure the autosave folder exists
05063   KMFolderMaildir::createMaildirFolders( KMKernel::localDataPath() + "autosave" );
05064   if ( mAutoSaveFilename.isEmpty() ) {
05065     mAutoSaveFilename = KMFolderMaildir::constructValidFileName();
05066   }
05067 
05068   updateAutoSave();
05069 }
05070 
05071 void KMComposeWin::updateAutoSave()
05072 {
05073   if ( autoSaveInterval() == 0 ) {
05074     delete mAutoSaveTimer; mAutoSaveTimer = 0;
05075   }
05076   else {
05077     if ( !mAutoSaveTimer ) {
05078       mAutoSaveTimer = new QTimer( this, "mAutoSaveTimer" );
05079       connect( mAutoSaveTimer, SIGNAL( timeout() ),
05080                this, SLOT( autoSaveMessage() ) );
05081     }
05082     mAutoSaveTimer->start( autoSaveInterval() );
05083   }
05084 }
05085 
05086 void KMComposeWin::setAutoSaveFilename( const QString & filename )
05087 {
05088   mAutoSaveFilename = filename;
05089 }
05090 
05091 void KMComposeWin::cleanupAutoSave()
05092 {
05093   delete mAutoSaveTimer; mAutoSaveTimer = 0;
05094   if ( !mAutoSaveFilename.isEmpty() ) {
05095     kdDebug(5006) << k_funcinfo << "deleting autosave file "
05096                   << mAutoSaveFilename << endl;
05097     KMFolderMaildir::removeFile( KMKernel::localDataPath() + "autosave",
05098                                  mAutoSaveFilename );
05099     mAutoSaveFilename = QString();
05100   }
05101 }
05102 
05103 void KMComposeWin::slotCompletionModeChanged( KGlobalSettings::Completion mode)
05104 {
05105   GlobalSettings::self()->setCompletionMode( (int) mode );
05106 
05107   // sync all the lineedits to the same completion mode
05108   mEdtFrom->setCompletionMode( mode );
05109   mEdtReplyTo->setCompletionMode( mode );
05110   if ( mClassicalRecipients ) {
05111     mEdtTo->setCompletionMode( mode );
05112     mEdtCc->setCompletionMode( mode );
05113     mEdtBcc->setCompletionMode( mode );
05114   }else
05115     mRecipientsEditor->setCompletionMode( mode );
05116 }
05117 
05118 void KMComposeWin::slotConfigChanged()
05119 {
05120   readConfig( true /*reload*/);
05121   updateAutoSave();
05122   rethinkFields();
05123   slotWordWrapToggled( mWordWrapAction->isChecked() );
05124 }
05125 
05126 /*
05127 * checks if the drafts-folder has been deleted
05128 * that is not nice so we set the system-drafts-folder
05129 */
05130 void KMComposeWin::slotFolderRemoved(KMFolder* folder)
05131 {
05132   // TODO: need to handle templates here?
05133   if ( (mFolder) && (folder->idString() == mFolder->idString()) )
05134   {
05135     mFolder = kmkernel->draftsFolder();
05136     kdDebug(5006) << "restoring drafts to " << mFolder->idString() << endl;
05137   }
05138   if (mMsg) mMsg->setParent(0);
05139 }
05140 
05141 
05142 void KMComposeWin::editorFocusChanged(bool gained)
05143 {
05144   mPasteQuotation->setEnabled(gained);
05145 }
05146 
05147 void KMComposeWin::slotSetAlwaysSend( bool bAlways )
05148 {
05149     mAlwaysSend = bAlways;
05150 }
05151 
05152 void KMComposeWin::slotListAction( const QString& style )
05153 {
05154     toggleMarkup(true);
05155     if ( style == i18n( "Standard" ) )
05156        mEditor->setParagType( QStyleSheetItem::DisplayBlock, QStyleSheetItem::ListDisc );
05157     else if ( style == i18n( "Bulleted List (Disc)" ) )
05158        mEditor->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListDisc );
05159     else if ( style == i18n( "Bulleted List (Circle)" ) )
05160        mEditor->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListCircle );
05161     else if ( style == i18n( "Bulleted List (Square)" ) )
05162        mEditor->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListSquare );
05163     else if ( style == i18n( "Ordered List (Decimal)" ))
05164        mEditor->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListDecimal );
05165     else if ( style == i18n( "Ordered List (Alpha lower)" ) )
05166        mEditor->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListLowerAlpha );
05167     else if ( style == i18n( "Ordered List (Alpha upper)" ) )
05168        mEditor->setParagType( QStyleSheetItem::DisplayListItem, QStyleSheetItem::ListUpperAlpha );
05169     mEditor->viewport()->setFocus();
05170 }
05171 
05172 void KMComposeWin::slotFontAction( const QString& font)
05173 {
05174     toggleMarkup(true);
05175     mEditor->QTextEdit::setFamily( font );
05176     mEditor->viewport()->setFocus();
05177 }
05178 
05179 void KMComposeWin::slotSizeAction( int size )
05180 {
05181     toggleMarkup(true);
05182     mEditor->setPointSize( size );
05183     mEditor->viewport()->setFocus();
05184 }
05185 
05186 void KMComposeWin::slotAlignLeft()
05187 {
05188     toggleMarkup(true);
05189     mEditor->QTextEdit::setAlignment( AlignLeft );
05190 }
05191 
05192 void KMComposeWin::slotAlignCenter()
05193 {
05194     toggleMarkup(true);
05195     mEditor->QTextEdit::setAlignment( AlignHCenter );
05196 }
05197 
05198 void KMComposeWin::slotAlignRight()
05199 {
05200     toggleMarkup(true);
05201     mEditor->QTextEdit::setAlignment( AlignRight );
05202 }
05203 
05204 void KMComposeWin::slotTextBold()
05205 {
05206     toggleMarkup(true);
05207     mEditor->QTextEdit::setBold( textBoldAction->isChecked() );
05208 }
05209 
05210 void KMComposeWin::slotTextItalic()
05211 {
05212     toggleMarkup(true);
05213     mEditor->QTextEdit::setItalic( textItalicAction->isChecked() );
05214 }
05215 
05216 void KMComposeWin::slotTextUnder()
05217 {
05218     toggleMarkup(true);
05219     mEditor->QTextEdit::setUnderline( textUnderAction->isChecked() );
05220 }
05221 
05222 void KMComposeWin::slotFormatReset()
05223 {
05224   mEditor->setColor(mForeColor);
05225   mEditor->setCurrentFont( mSaveFont ); // fontChanged is called now
05226 }
05227 void KMComposeWin::slotTextColor()
05228 {
05229   QColor color = mEditor->color();
05230 
05231   if ( KColorDialog::getColor( color, this ) ) {
05232     toggleMarkup(true);
05233     mEditor->setColor( color );
05234   }
05235 }
05236 
05237 void KMComposeWin::fontChanged( const QFont &f )
05238 {
05239   QFont fontTemp = f;
05240   fontTemp.setBold( true );
05241   fontTemp.setItalic( true );
05242   QFontInfo fontInfo( fontTemp );
05243 
05244   if ( fontInfo.bold() ) {
05245     textBoldAction->setChecked( f.bold() );
05246     textBoldAction->setEnabled( true ) ;
05247   } else {
05248     textBoldAction->setEnabled( false );
05249   }
05250 
05251   if ( fontInfo.italic() ) {
05252     textItalicAction->setChecked( f.italic() );
05253     textItalicAction->setEnabled( true ) ;
05254   } else {
05255     textItalicAction->setEnabled( false );
05256   }
05257 
05258   textUnderAction->setChecked( f.underline() );
05259 
05260   fontAction->setFont( f.family() );
05261   fontSizeAction->setFontSize( f.pointSize() );
05262 }
05263 
05264 void KMComposeWin::alignmentChanged( int a )
05265 {
05266     //toggleMarkup();
05267     alignLeftAction->setChecked( ( a == AlignAuto ) || ( a & AlignLeft ) );
05268     alignCenterAction->setChecked( ( a & AlignHCenter ) );
05269     alignRightAction->setChecked( ( a & AlignRight ) );
05270 }
05271 
05272 namespace {
05273   class KToggleActionResetter {
05274     KToggleAction * mAction;
05275     bool mOn;
05276   public:
05277     KToggleActionResetter( KToggleAction * action, bool on )
05278       : mAction( action ),  mOn( on ) {}
05279     ~KToggleActionResetter() {
05280       if ( mAction )
05281         mAction->setChecked( mOn );
05282     }
05283     void disable() { mAction = 0; }
05284   };
05285 }
05286 
05287 void KMComposeWin::slotEncryptChiasmusToggled( bool on ) {
05288   mEncryptWithChiasmus = false;
05289 
05290   if ( !on )
05291     return;
05292 
05293   KToggleActionResetter resetter( mEncryptChiasmusAction, false );
05294 
05295   const Kleo::CryptoBackend::Protocol * chiasmus =
05296     Kleo::CryptoBackendFactory::instance()->protocol( "Chiasmus" );
05297 
05298   if ( !chiasmus ) {
05299     const QString msg = Kleo::CryptoBackendFactory::instance()->knowsAboutProtocol( "Chiasmus" )
05300       ? i18n( "Please configure a Crypto Backend to use for "
05301               "Chiasmus encryption first.\n"
05302               "You can do this in the Crypto Backends tab of "
05303               "the configure dialog's Security page." )
05304       : i18n( "It looks as though libkleopatra was compiled without "
05305               "Chiasmus support. You might want to recompile "
05306               "libkleopatra with --enable-chiasmus.");
05307     KMessageBox::information( this, msg, i18n("No Chiasmus Backend Configured" ) );
05308     return;
05309   }
05310 
05311   STD_NAMESPACE_PREFIX auto_ptr<Kleo::SpecialJob> job( chiasmus->specialJob( "x-obtain-keys", QMap<QString,QVariant>() ) );
05312   if ( !job.get() ) {
05313     const QString msg = i18n( "Chiasmus backend does not offer the "
05314                               "\"x-obtain-keys\" function. Please report this bug." );
05315     KMessageBox::error( this, msg, i18n( "Chiasmus Backend Error" ) );
05316     return;
05317   }
05318 
05319   if ( job->exec() ) {
05320     job->showErrorDialog( this, i18n( "Chiasmus Backend Error" ) );
05321     return;
05322   }
05323 
05324   const QVariant result = job->property( "result" );
05325   if ( result.type() != QVariant::StringList ) {
05326     const QString msg = i18n( "Unexpected return value from Chiasmus backend: "
05327                               "The \"x-obtain-keys\" function did not return a "
05328                               "string list. Please report this bug." );
05329     KMessageBox::error( this, msg, i18n( "Chiasmus Backend Error" ) );
05330     return;
05331   }
05332 
05333   const QStringList keys = result.toStringList();
05334   if ( keys.empty() ) {
05335     const QString msg = i18n( "No keys have been found. Please check that a "
05336                               "valid key path has been set in the Chiasmus "
05337                               "configuration." );
05338     KMessageBox::information( this, msg, i18n( "No Chiasmus Keys Found" ) );
05339     return;
05340   }
05341 
05342   ChiasmusKeySelector selectorDlg( this, i18n( "Chiasmus Encryption Key Selection" ),
05343                                    keys, GlobalSettings::chiasmusKey(),
05344                                    GlobalSettings::chiasmusOptions() );
05345   if ( selectorDlg.exec() != QDialog::Accepted )
05346     return;
05347 
05348   GlobalSettings::setChiasmusOptions( selectorDlg.options() );
05349   GlobalSettings::setChiasmusKey( selectorDlg.key() );
05350   assert( !GlobalSettings::chiasmusKey().isEmpty() );
05351   mEncryptWithChiasmus = true;
05352   resetter.disable();
05353 }
05354 
05355 void KMComposeWin::slotEditDone(KMail::EditorWatcher * watcher)
05356 {
05357   kdDebug(5006) << k_funcinfo << endl;
05358   KMMessagePart *part = mEditorMap[ watcher ];
05359   KTempFile *tf = mEditorTempFiles[ watcher ];
05360   mEditorMap.remove( watcher );
05361   mEditorTempFiles.remove( watcher );
05362   if ( !watcher->fileChanged() )
05363     return;
05364 
05365   tf->file()->reset();
05366   QByteArray data = tf->file()->readAll();
05367   part->setBodyEncodedBinary( data );
05368 }
05369 
05370 
05371 void KMComposeWin::slotUpdateSignatureAndEncrypionStateIndicators()
05372 {
05373     const bool showIndicatorsAlways = false; // FIXME config option?
05374     mSignatureStateIndicator->setText( mSignAction->isChecked()? i18n("Message will be signed") : i18n("Message will not be signed") );
05375     mEncryptionStateIndicator->setText( mEncryptAction->isChecked()? i18n("Message will be encrypted") : i18n("Message will not be encrypted") );
05376     if ( !showIndicatorsAlways ) {
05377       mSignatureStateIndicator->setShown( mSignAction->isChecked() );
05378       mEncryptionStateIndicator->setShown( mEncryptAction->isChecked() );
05379     }
05380 }
05381 
05382 void KMComposeWin::slotAttachmentDragStarted()
05383 {
05384   kdDebug(5006) << k_funcinfo << endl;
05385   int idx = 0;
05386   QStringList filenames;
05387   for ( QPtrListIterator<QListViewItem> it(mAtmItemList); *it; ++it, ++idx ) {
05388     if ( (*it)->isSelected() ) {
05389       KMMessagePart* msgPart = mAtmList.at(idx);
05390       KTempDir * tempDir = new KTempDir(); // will be deleted on composer close
05391       tempDir->setAutoDelete( true );
05392       mTempDirs.insert( tempDir );
05393       const QString fileName = tempDir->name() + msgPart->name();
05394       KPIM::kByteArrayToFile(msgPart->bodyDecodedBinary(),
05395                              fileName,
05396                              false, false, false);
05397       KPIM::checkAndCorrectPermissionsIfPossible( fileName, false, true, true );
05398       KURL url;
05399       url.setPath( fileName );
05400       filenames << url.path();
05401     }
05402   }
05403   if ( filenames.isEmpty() ) return;
05404 
05405   QUriDrag *drag  = new QUriDrag( mAtmListView );
05406   drag->setFileNames( filenames );
05407   drag->dragCopy();
05408 }
05409 
05410 void KMComposeWin::recipientEditorSizeHintChanged()
05411 {
05412   QTimer::singleShot( 1, this, SLOT(setMaximumHeaderSize()) );
05413 }
05414 
05415 void KMComposeWin::setMaximumHeaderSize()
05416 {
05417   mHeadersArea->setMaximumHeight( mHeadersArea->sizeHint().height() );
05418 }
05419 
KDE Home | KDE Accessibility Home | Description of Access Keys