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