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