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