kmail

kmcomposewin.cpp

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