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