kmail

kmcomposewin.cpp

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