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