kmail Library API Documentation

kmreaderwin.cpp

00001 // -*- mode: C++; c-file-style: "gnu" -*-
00002 // kmreaderwin.cpp
00003 // Author: Markus Wuebben <markus.wuebben@kde.org>
00004 
00005 // define this to copy all html that is written to the readerwindow to
00006 // filehtmlwriter.out in the current working directory
00007 //#define KMAIL_READER_HTML_DEBUG 1
00008 
00009 #include <config.h>
00010 
00011 #include "kmreaderwin.h"
00012 
00013 #include "globalsettings.h"
00014 #include "kmversion.h"
00015 #include "kmmainwidget.h"
00016 #include "kmreadermainwin.h"
00017 #include "kmailicalifaceimpl.h"
00018 #include <kpgpblock.h>
00019 #include <libkdepim/kfileio.h>
00020 #include "kmfolderindex.h"
00021 #include "kmcommands.h"
00022 #include "kmmsgpartdlg.h"
00023 #include "mailsourceviewer.h"
00024 using KMail::MailSourceViewer;
00025 #include "partNode.h"
00026 #include "kmmsgdict.h"
00027 #include "messagesender.h"
00028 #include "kcursorsaver.h"
00029 #include "kmkernel.h"
00030 #include "kmfolder.h"
00031 #include "vcardviewer.h"
00032 using KMail::VCardViewer;
00033 #include "objecttreeparser.h"
00034 using KMail::ObjectTreeParser;
00035 #include "partmetadata.h"
00036 using KMail::PartMetaData;
00037 #include "attachmentstrategy.h"
00038 using KMail::AttachmentStrategy;
00039 #include "headerstrategy.h"
00040 using KMail::HeaderStrategy;
00041 #include "headerstyle.h"
00042 using KMail::HeaderStyle;
00043 #include "khtmlparthtmlwriter.h"
00044 using KMail::HtmlWriter;
00045 using KMail::KHtmlPartHtmlWriter;
00046 #include "htmlstatusbar.h"
00047 using KMail::HtmlStatusBar;
00048 #include "folderjob.h"
00049 using KMail::FolderJob;
00050 #include "csshelper.h"
00051 using KMail::CSSHelper;
00052 #include "isubject.h"
00053 using KMail::ISubject;
00054 #include "urlhandlermanager.h"
00055 using KMail::URLHandlerManager;
00056 #include "interfaces/observable.h"
00057 
00058 #include "broadcaststatus.h"
00059 
00060 #include <kmime_mdn.h>
00061 using namespace KMime;
00062 #ifdef KMAIL_READER_HTML_DEBUG
00063 #include "filehtmlwriter.h"
00064 using KMail::FileHtmlWriter;
00065 #include "teehtmlwriter.h"
00066 using KMail::TeeHtmlWriter;
00067 #endif
00068 
00069 #include <mimelib/mimepp.h>
00070 #include <mimelib/body.h>
00071 #include <mimelib/utility.h>
00072 
00073 #ifdef KLEO_CHIASMUS
00074 #include <kleo/specialjob.h>
00075 #include <kleo/cryptobackend.h>
00076 #include <kleo/cryptobackendfactory.h>
00077 #endif
00078 
00079 // KABC includes
00080 #include <kabc/addressee.h>
00081 #include <kabc/vcardconverter.h>
00082 
00083 // khtml headers
00084 #include <khtml_part.h>
00085 #include <khtmlview.h> // So that we can get rid of the frames
00086 #include <dom/html_element.h>
00087 #include <dom/html_block.h>
00088 #include <dom/html_document.h>
00089 #include <dom/dom_string.h>
00090 
00091 #ifdef KLEO_CHIASMUS
00092 // KIO headers
00093 #include <kio/job.h>
00094 #include <kio/netaccess.h>
00095 #include <storedtransferjob.h>
00096 #endif
00097 
00098 #include <kapplication.h>
00099 // for the click on attachment stuff (dnaber):
00100 #include <kuserprofile.h>
00101 #include <kcharsets.h>
00102 #include <kpopupmenu.h>
00103 #include <kstandarddirs.h>  // Sven's : for access and getpid
00104 #include <kcursor.h>
00105 #include <kdebug.h>
00106 #include <kfiledialog.h>
00107 #include <klocale.h>
00108 #include <kmessagebox.h>
00109 #include <kglobalsettings.h>
00110 #include <krun.h>
00111 #include <ktempfile.h>
00112 #include <kprocess.h>
00113 #include <kdialog.h>
00114 #include <kaction.h>
00115 #include <kiconloader.h>
00116 #include <kmdcodec.h>
00117 #include <kinputdialog.h>
00118 
00119 #include <qclipboard.h>
00120 #include <qhbox.h>
00121 #include <qtextcodec.h>
00122 #include <qpaintdevicemetrics.h>
00123 #include <qlayout.h>
00124 #include <qlabel.h>
00125 #include <qsplitter.h>
00126 #include <qstyle.h>
00127 #include <qvariant.h>
00128 #include <qstringlist.h>
00129 #include <qfile.h>
00130 
00131 // X headers...
00132 #undef Never
00133 #undef Always
00134 
00135 #include <memory>
00136 
00137 #include <unistd.h>
00138 #include <stdlib.h>
00139 #include <sys/stat.h>
00140 #include <errno.h>
00141 #include <stdio.h>
00142 #include <ctype.h>
00143 #include <string.h>
00144 
00145 #ifdef HAVE_PATHS_H
00146 #include <paths.h>
00147 #endif
00148 
00149 class NewByteArray : public QByteArray
00150 {
00151 public:
00152     NewByteArray &appendNULL();
00153     NewByteArray &operator+=( const char * );
00154     NewByteArray &operator+=( const QByteArray & );
00155     NewByteArray &operator+=( const QCString & );
00156     QByteArray& qByteArray();
00157 };
00158 
00159 NewByteArray& NewByteArray::appendNULL()
00160 {
00161     QByteArray::detach();
00162     uint len1 = size();
00163     if ( !QByteArray::resize( len1 + 1 ) )
00164         return *this;
00165     *(data() + len1) = '\0';
00166     return *this;
00167 }
00168 NewByteArray& NewByteArray::operator+=( const char * newData )
00169 {
00170     if ( !newData )
00171         return *this;
00172     QByteArray::detach();
00173     uint len1 = size();
00174     uint len2 = qstrlen( newData );
00175     if ( !QByteArray::resize( len1 + len2 ) )
00176         return *this;
00177     memcpy( data() + len1, newData, len2 );
00178     return *this;
00179 }
00180 NewByteArray& NewByteArray::operator+=( const QByteArray & newData )
00181 {
00182     if ( newData.isNull() )
00183         return *this;
00184     QByteArray::detach();
00185     uint len1 = size();
00186     uint len2 = newData.size();
00187     if ( !QByteArray::resize( len1 + len2 ) )
00188         return *this;
00189     memcpy( data() + len1, newData.data(), len2 );
00190     return *this;
00191 }
00192 NewByteArray& NewByteArray::operator+=( const QCString & newData )
00193 {
00194     if ( newData.isEmpty() )
00195         return *this;
00196     QByteArray::detach();
00197     uint len1 = size();
00198     uint len2 = newData.length(); // forget about the trailing 0x00 !
00199     if ( !QByteArray::resize( len1 + len2 ) )
00200         return *this;
00201     memcpy( data() + len1, newData.data(), len2 );
00202     return *this;
00203 }
00204 QByteArray& NewByteArray::qByteArray()
00205 {
00206     return *((QByteArray*)this);
00207 }
00208 
00209 #ifdef KLEO_CHIASMUS
00210 
00211 #include <klineedit.h>
00212 #include <klistbox.h>
00213 
00214 ChiasmusKeySelector::ChiasmusKeySelector( QWidget* parent, const QString& caption,
00215                                           const QStringList& keys, const QString& currentKey,
00216                                           const QString& lastOptions )
00217   : KDialogBase( parent, "chiasmusKeySelector", true, caption, Ok|Cancel, Ok, true )
00218 {
00219   QWidget *page = makeMainWidget();
00220 
00221   QVBoxLayout *layout = new QVBoxLayout(page, KDialog::spacingHint());
00222 
00223   mLabel = new QLabel( i18n( "Please select the Chiasmus key file to use:" ), page );
00224   layout->addWidget( mLabel );
00225 
00226   mListBox = new KListBox( page );
00227   mListBox->insertStringList( keys );
00228   const int current = keys.findIndex( currentKey );
00229   mListBox->setSelected( QMAX( 0, current ), true );
00230   mListBox->ensureCurrentVisible();
00231   layout->addWidget( mListBox, 1 );
00232 
00233   QLabel* optionLabel = new QLabel( i18n( "Additional arguments for chiasmus:" ), page );
00234   layout->addWidget( optionLabel );
00235 
00236   mOptions = new KLineEdit( lastOptions, page );
00237   optionLabel->setBuddy( mOptions );
00238   layout->addWidget( mOptions );
00239 
00240   layout->addStretch();
00241 
00242   connect( mListBox, SIGNAL( doubleClicked( QListBoxItem * ) ), this, SLOT( slotOk() ) );
00243   connect( mListBox, SIGNAL( returnPressed( QListBoxItem * ) ), this, SLOT( slotOk() ) );
00244 
00245   mListBox->setFocus();
00246 }
00247 
00248 QString ChiasmusKeySelector::key() const
00249 {
00250   return mListBox->currentText();
00251 }
00252 
00253 QString ChiasmusKeySelector::options() const
00254 {
00255   return mOptions->text();
00256 }
00257 
00258 #endif // KLEO_CHIASMUS
00259 
00260 // This function returns the complete data that were in this
00261 // message parts - *after* all encryption has been removed that
00262 // could be removed.
00263 // - This is used to store the message in decrypted form.
00264 void KMReaderWin::objectTreeToDecryptedMsg( partNode* node,
00265                                             NewByteArray& resultingData,
00266                                             KMMessage& theMessage,
00267                                             bool weAreReplacingTheRootNode,
00268                                             int recCount )
00269 {
00270   kdDebug(5006) << QString("-------------------------------------------------" ) << endl;
00271   kdDebug(5006) << QString("KMReaderWin::objectTreeToDecryptedMsg( %1 )  START").arg( recCount ) << endl;
00272   if( node ) {
00273     partNode* curNode = node;
00274     partNode* dataNode = curNode;
00275     partNode * child = node->firstChild();
00276     bool bIsMultipart = false;
00277     bool bKeepPartAsIs= false;
00278 
00279     switch( curNode->type() ){
00280       case DwMime::kTypeText: {
00281 kdDebug(5006) << "* text *" << endl;
00282           switch( curNode->subType() ){
00283           case DwMime::kSubtypeHtml:
00284 kdDebug(5006) << "html" << endl;
00285             break;
00286           case DwMime::kSubtypeXVCard:
00287 kdDebug(5006) << "v-card" << endl;
00288             break;
00289           case DwMime::kSubtypeRichtext:
00290 kdDebug(5006) << "rich text" << endl;
00291             break;
00292           case DwMime::kSubtypeEnriched:
00293 kdDebug(5006) << "enriched " << endl;
00294             break;
00295           case DwMime::kSubtypePlain:
00296 kdDebug(5006) << "plain " << endl;
00297             break;
00298           default:
00299 kdDebug(5006) << "default " << endl;
00300             break;
00301           }
00302         }
00303         break;
00304       case DwMime::kTypeMultipart: {
00305 kdDebug(5006) << "* multipart *" << endl;
00306           bIsMultipart = true;
00307           switch( curNode->subType() ){
00308           case DwMime::kSubtypeMixed:
00309 kdDebug(5006) << "mixed" << endl;
00310             break;
00311           case DwMime::kSubtypeAlternative:
00312 kdDebug(5006) << "alternative" << endl;
00313             break;
00314           case DwMime::kSubtypeDigest:
00315 kdDebug(5006) << "digest" << endl;
00316             break;
00317           case DwMime::kSubtypeParallel:
00318 kdDebug(5006) << "parallel" << endl;
00319             break;
00320           case DwMime::kSubtypeSigned:
00321 kdDebug(5006) << "signed" << endl;
00322             break;
00323           case DwMime::kSubtypeEncrypted: {
00324 kdDebug(5006) << "encrypted" << endl;
00325               if ( child ) {
00326                 partNode* data =
00327                   child->findType( DwMime::kTypeApplication, DwMime::kSubtypeOctetStream, false, true );
00328                 if ( !data )
00329                   data = child->findType( DwMime::kTypeApplication, DwMime::kSubtypePkcs7Mime, false, true );
00330                 if ( data && data->firstChild() )
00331                   dataNode = data;
00332               }
00333             }
00334             break;
00335           default :
00336 kdDebug(5006) << "(  unknown subtype  )" << endl;
00337             break;
00338           }
00339         }
00340         break;
00341       case DwMime::kTypeMessage: {
00342 kdDebug(5006) << "* message *" << endl;
00343           switch( curNode->subType() ){
00344           case DwMime::kSubtypeRfc822: {
00345 kdDebug(5006) << "RfC 822" << endl;
00346               if ( child )
00347                 dataNode = child;
00348             }
00349             break;
00350           }
00351         }
00352         break;
00353       case DwMime::kTypeApplication: {
00354 kdDebug(5006) << "* application *" << endl;
00355           switch( curNode->subType() ){
00356           case DwMime::kSubtypePostscript:
00357 kdDebug(5006) << "postscript" << endl;
00358             break;
00359           case DwMime::kSubtypeOctetStream: {
00360 kdDebug(5006) << "octet stream" << endl;
00361               if ( child )
00362                 dataNode = child;
00363             }
00364             break;
00365           case DwMime::kSubtypePgpEncrypted:
00366 kdDebug(5006) << "pgp encrypted" << endl;
00367             break;
00368           case DwMime::kSubtypePgpSignature:
00369 kdDebug(5006) << "pgp signed" << endl;
00370             break;
00371           case DwMime::kSubtypePkcs7Mime: {
00372 kdDebug(5006) << "pkcs7 mime     (keeping part as is)" << endl;
00373               // note: subtype Pkcs7Mime can also be signed
00374               //       and we do NOT want to remove the signature!
00375               bKeepPartAsIs = true;
00376               if ( child && curNode->encryptionState() != KMMsgNotEncrypted ){
00377                 dataNode = child;
00378               }
00379             }
00380             break;
00381           case DwMime::kSubtypePkcs7Signature: {
00382 kdDebug(5006) << "pkcs7 signature" << endl;
00383               // note: subtype Pkcs7Signature specifies a signature part
00384               //       which we do NOT want to remove!
00385               bKeepPartAsIs = true;
00386             }
00387             break;
00388           }
00389         }
00390         break;
00391       case DwMime::kTypeImage: {
00392 kdDebug(5006) << "* image *" << endl;
00393           switch( curNode->subType() ){
00394           case DwMime::kSubtypeJpeg:
00395 kdDebug(5006) << "JPEG" << endl;
00396             break;
00397           case DwMime::kSubtypeGif:
00398 kdDebug(5006) << "GIF" << endl;
00399             break;
00400           }
00401         }
00402         break;
00403       case DwMime::kTypeAudio: {
00404 kdDebug(5006) << "* audio *" << endl;
00405           switch( curNode->subType() ){
00406           case DwMime::kSubtypeBasic:
00407 kdDebug(5006) << "basic" << endl;
00408             break;
00409           }
00410         }
00411         break;
00412       case DwMime::kTypeVideo: {
00413 kdDebug(5006) << "* video *" << endl;
00414           switch( curNode->subType() ){
00415           case DwMime::kSubtypeMpeg:
00416 kdDebug(5006) << "mpeg" << endl;
00417             break;
00418           }
00419         }
00420         break;
00421       case DwMime::kTypeModel:
00422 kdDebug(5006) << "* model *" << endl;
00423         break;
00424     }
00425 
00426 
00427     DwHeaders& rootHeaders( theMessage.headers() );
00428     DwBodyPart * part = dataNode->dwPart() ? dataNode->dwPart() : 0;
00429     DwHeaders * headers(
00430         (part && part->hasHeaders())
00431         ? &part->Headers()
00432         : (  (weAreReplacingTheRootNode || !dataNode->parentNode())
00433             ? &rootHeaders
00434             : 0 ) );
00435     if( dataNode == curNode ) {
00436 kdDebug(5006) << "dataNode == curNode:  Save curNode without replacing it." << endl;
00437 
00438       // A) Store the headers of this part IF curNode is not the root node
00439       //    AND we are not replacing a node that already *has* replaced
00440       //    the root node in previous recursion steps of this function...
00441       if( headers ) {
00442         if( dataNode->parentNode() && !weAreReplacingTheRootNode ) {
00443 kdDebug(5006) << "dataNode is NOT replacing the root node:  Store the headers." << endl;
00444           resultingData += headers->AsString().c_str();
00445         } else if( weAreReplacingTheRootNode && part->hasHeaders() ){
00446 kdDebug(5006) << "dataNode replace the root node:  Do NOT store the headers but change" << endl;
00447 kdDebug(5006) << "                                 the Message's headers accordingly." << endl;
00448 kdDebug(5006) << "              old Content-Type = " << rootHeaders.ContentType().AsString().c_str() << endl;
00449 kdDebug(5006) << "              new Content-Type = " << headers->ContentType(   ).AsString().c_str() << endl;
00450           rootHeaders.ContentType()             = headers->ContentType();
00451           theMessage.setContentTransferEncodingStr(
00452               headers->HasContentTransferEncoding()
00453             ? headers->ContentTransferEncoding().AsString().c_str()
00454             : "" );
00455           rootHeaders.ContentDescription() = headers->ContentDescription();
00456           rootHeaders.ContentDisposition() = headers->ContentDisposition();
00457           theMessage.setNeedsAssembly();
00458         }
00459       }
00460 
00461       // B) Store the body of this part.
00462       if( headers && bIsMultipart && dataNode->firstChild() )  {
00463 kdDebug(5006) << "is valid Multipart, processing children:" << endl;
00464         QCString boundary = headers->ContentType().Boundary().c_str();
00465         curNode = dataNode->firstChild();
00466         // store children of multipart
00467         while( curNode ) {
00468 kdDebug(5006) << "--boundary" << endl;
00469           if( resultingData.size() &&
00470               ( '\n' != resultingData.at( resultingData.size()-1 ) ) )
00471             resultingData += QCString( "\n" );
00472           resultingData += QCString( "\n" );
00473           resultingData += "--";
00474           resultingData += boundary;
00475           resultingData += "\n";
00476           // note: We are processing a harmless multipart that is *not*
00477           //       to be replaced by one of it's children, therefor
00478           //       we set their doStoreHeaders to true.
00479           objectTreeToDecryptedMsg( curNode,
00480                                     resultingData,
00481                                     theMessage,
00482                                     false,
00483                                     recCount + 1 );
00484           curNode = curNode->nextSibling();
00485         }
00486 kdDebug(5006) << "--boundary--" << endl;
00487         resultingData += "\n--";
00488         resultingData += boundary;
00489         resultingData += "--\n\n";
00490 kdDebug(5006) << "Multipart processing children - DONE" << endl;
00491       } else if( part ){
00492         // decrypt and store simple part
00493 kdDebug(5006) << "is Simple part or invalid Multipart, processing single body (if inline encrypted):" << endl;
00494         // Problem: body text may be inline PGP encrypted, so we can not just dump it.
00495 
00496 kdDebug(5006) << "\n\n\npart as is:\n" << part->Body().AsString().c_str() << endl;
00497 
00498         if( bKeepPartAsIs ){
00499 kdDebug(5006) << "bKeepPartAsIs == TRUE" << endl;
00500           resultingData += part->Body().AsString().c_str();
00501         }else{
00502 kdDebug(5006) << "bKeepPartAsIs == FALSE" << endl;
00503           // Note: parseObjectTree() does no inline PGP decrypting anymore.
00504           ObjectTreeParser otp( 0, 0, false, false, true );
00505           dataNode->setProcessed( false, true );
00506           otp.setKeepEncryptions( false );
00507           otp.parseObjectTree( curNode );
00508           //resultingData += otp.rawReplyString();  // re-enable this, once ObjectTreeParser is updated.
00509 
00510 
00511           // Temporary solution, to be replaced by a Kleo::CryptoBackend job inside ObjectTreeParser:
00512           bool bDecryptedInlinePGP = false;
00513           QPtrList<Kpgp::Block> pgpBlocks;
00514           QStrList nonPgpBlocks;
00515           if ( Kpgp::Module::prepareMessageForDecryption( otp.rawReplyString(),
00516                                                           pgpBlocks,
00517                                                           nonPgpBlocks ) ) {
00518             if ( pgpBlocks.count() == 1 ) {
00519               Kpgp::Block * block = pgpBlocks.first();
00520               if ( block->type() == Kpgp::PgpMessageBlock ) {
00521                 // try to decrypt this OpenPGP block
00522                 block->decrypt();
00523                 resultingData += nonPgpBlocks.first() + block->text() + nonPgpBlocks.last();
00524                 bDecryptedInlinePGP = true;
00525               }
00526             }
00527           }
00528           if( !bDecryptedInlinePGP ){
00529             // if this new line is omitted, signed text can not be verified later
00530             resultingData += "\n";
00531             resultingData += otp.rawReplyString();
00532           }
00533           // end of temporary solution.
00534 
00535 
00536         }
00537 kdDebug(5006) << "decrypting of single body - DONE" << endl;
00538       }
00539     } else {
00540 kdDebug(5006) << "dataNode != curNode:  Replace curNode by dataNode." << endl;
00541       bool rootNodeReplaceFlag = weAreReplacingTheRootNode || !curNode->parentNode();
00542       if( rootNodeReplaceFlag ) {
00543 kdDebug(5006) << "                      Root node will be replaced." << endl;
00544       } else {
00545 kdDebug(5006) << "                      Root node will NOT be replaced." << endl;
00546       }
00547       // store special data to replace the current part
00548       // (e.g. decrypted data or embedded RfC 822 data)
00549       objectTreeToDecryptedMsg( dataNode,
00550                                 resultingData,
00551                                 theMessage,
00552                                 rootNodeReplaceFlag,
00553                                 recCount + 1 );
00554     }
00555   }
00556   kdDebug(5006) << QString("\nKMReaderWin::objectTreeToDecryptedMsg( %1 )  END").arg( recCount ) << endl;
00557 }
00558 
00559 
00560 /*
00561  ===========================================================================
00562 
00563 
00564         E N D    O F     T E M P O R A R Y     M I M E     C O D E
00565 
00566 
00567  ===========================================================================
00568 */
00569 
00570 
00571 
00572 
00573 
00574 
00575 
00576 
00577 
00578 
00579 
00580 void KMReaderWin::createWidgets() {
00581   QVBoxLayout * vlay = new QVBoxLayout( this );
00582   mSplitter = new QSplitter( Qt::Vertical, this, "mSplitter" );
00583   vlay->addWidget( mSplitter );
00584   mMimePartTree = new KMMimePartTree( this, mSplitter, "mMimePartTree" );
00585   mBox = new QHBox( mSplitter, "mBox" );
00586   setStyleDependantFrameWidth();
00587   mBox->setFrameStyle( mMimePartTree->frameStyle() );
00588   mColorBar = new HtmlStatusBar( mBox, "mColorBar" );
00589   mViewer = new KHTMLPart( mBox, "mViewer" );
00590   mSplitter->setOpaqueResize( KGlobalSettings::opaqueResize() );
00591   mSplitter->setResizeMode( mMimePartTree, QSplitter::KeepSize );
00592 }
00593 
00594 const int KMReaderWin::delay = 150;
00595 
00596 //-----------------------------------------------------------------------------
00597 KMReaderWin::KMReaderWin(QWidget *aParent,
00598              QWidget *mainWindow,
00599              KActionCollection* actionCollection,
00600                          const char *aName,
00601                          int aFlags )
00602   : QWidget(aParent, aName, aFlags | Qt::WDestructiveClose),
00603     mAttachmentStrategy( 0 ),
00604     mHeaderStrategy( 0 ),
00605     mHeaderStyle( 0 ),
00606     mOldGlobalOverrideEncoding( "---" ), // init with dummy value
00607     mCSSHelper( 0 ),
00608     mRootNode( 0 ),
00609     mMainWindow( mainWindow ),
00610     mActionCollection( actionCollection ),
00611     mSelectEncodingAction( 0 ),
00612     mHtmlWriter( 0 )
00613 {
00614   mSplitterSizes << 180 << 100;
00615   mMimeTreeMode = 1;
00616   mMimeTreeAtBottom = true;
00617   mAutoDelete = false;
00618   mLastSerNum = 0;
00619   mWaitingForSerNum = 0;
00620   mMessage = 0;
00621   mLastStatus = KMMsgStatusUnknown;
00622   mMsgDisplay = true;
00623   mPrinting = false;
00624   mShowColorbar = false;
00625   mAtmUpdate = false;
00626 
00627   createWidgets();
00628   initHtmlWidget();
00629   readConfig();
00630 
00631   mHtmlOverride = false;
00632 
00633   connect( &updateReaderWinTimer, SIGNAL(timeout()),
00634        this, SLOT(updateReaderWin()) );
00635   connect( &mResizeTimer, SIGNAL(timeout()),
00636        this, SLOT(slotDelayedResize()) );
00637   connect( &mDelayedMarkTimer, SIGNAL(timeout()),
00638            this, SLOT(slotTouchMessage()) );
00639 
00640   createActions( actionCollection );
00641 }
00642 
00643 void KMReaderWin::createActions( KActionCollection * ac ) {
00644   if ( !ac )
00645       return;
00646 
00647   KRadioAction *raction = 0;
00648 
00649   // header style
00650   KActionMenu *headerMenu =
00651     new KActionMenu( i18n("View->", "&Headers"), ac, "view_headers" );
00652   headerMenu->setToolTip( i18n("Choose display style of message headers") );
00653 
00654   connect( headerMenu, SIGNAL(activated()),
00655            this, SLOT(slotCycleHeaderStyles()) );
00656 
00657   raction = new KRadioAction( i18n("View->headers->", "&Fancy Headers"), 0,
00658                               this, SLOT(slotFancyHeaders()),
00659                               ac, "view_headers_fancy" );
00660   raction->setToolTip( i18n("Show the list of headers in a fancy format") );
00661   raction->setExclusiveGroup( "view_headers_group" );
00662   headerMenu->insert( raction );
00663 
00664   raction = new KRadioAction( i18n("View->headers->", "&Brief Headers"), 0,
00665                               this, SLOT(slotBriefHeaders()),
00666                               ac, "view_headers_brief" );
00667   raction->setToolTip( i18n("Show brief list of message headers") );
00668   raction->setExclusiveGroup( "view_headers_group" );
00669   headerMenu->insert( raction );
00670 
00671   raction = new KRadioAction( i18n("View->headers->", "&Standard Headers"), 0,
00672                               this, SLOT(slotStandardHeaders()),
00673                               ac, "view_headers_standard" );
00674   raction->setToolTip( i18n("Show standard list of message headers") );
00675   raction->setExclusiveGroup( "view_headers_group" );
00676   headerMenu->insert( raction );
00677 
00678   raction = new KRadioAction( i18n("View->headers->", "&Long Headers"), 0,
00679                               this, SLOT(slotLongHeaders()),
00680                               ac, "view_headers_long" );
00681   raction->setToolTip( i18n("Show long list of message headers") );
00682   raction->setExclusiveGroup( "view_headers_group" );
00683   headerMenu->insert( raction );
00684 
00685   raction = new KRadioAction( i18n("View->headers->", "&Printing Headers"), 0, this,
00686       SLOT(slotMinimalHeaders()),
00687       ac, "view_headers_minimal" );
00688   raction->setToolTip( i18n("Show the headers for printing") );
00689   raction->setExclusiveGroup( "view_headers_group" );
00690   headerMenu->insert( raction );
00691 
00692   raction = new KRadioAction( i18n("View->headers->", "&All Headers"), 0,
00693                               this, SLOT(slotAllHeaders()),
00694                               ac, "view_headers_all" );
00695   raction->setToolTip( i18n("Show all message headers") );
00696   raction->setExclusiveGroup( "view_headers_group" );
00697   headerMenu->insert( raction );
00698 
00699 
00700   // Set Encoding submenu
00701   mSelectEncodingAction = new KSelectAction( i18n( "&Set Encoding" ), "charset", 0,
00702                                  this, SLOT( slotSetEncoding() ),
00703                                  ac, "encoding" );
00704   QStringList encodings = KMMsgBase::supportedEncodings( false );
00705   encodings.prepend( i18n( "Auto" ) );
00706   mSelectEncodingAction->setItems( encodings );
00707   mSelectEncodingAction->setCurrentItem( 0 );
00708 
00709 
00710   mMailToComposeAction = new KAction( i18n("New Message To..."), 0, this,
00711                     SLOT(slotMailtoCompose()), ac,
00712                     "mailto_compose" );
00713   mMailToReplyAction = new KAction( i18n("Reply To..."), 0, this,
00714                     SLOT(slotMailtoReply()), ac,
00715                     "mailto_reply" );
00716   mMailToForwardAction = new KAction( i18n("Forward To..."),
00717                     0, this, SLOT(slotMailtoForward()), ac,
00718                     "mailto_forward" );
00719   mAddAddrBookAction = new KAction( i18n("Add to Address Book"),
00720                     0, this, SLOT(slotMailtoAddAddrBook()),
00721                     ac, "add_addr_book" );
00722   mOpenAddrBookAction = new KAction( i18n("Open in Address Book"),
00723                     0, this, SLOT(slotMailtoOpenAddrBook()),
00724                     ac, "openin_addr_book" );
00725   mCopyAction = new KAction( i18n("Copy to Clipboard"), 0, this,
00726                  SLOT(slotUrlCopy()), ac, "copy_address" );
00727   mCopyURLAction = new KAction( i18n("Copy Link Address"), 0, this,
00728                 SLOT(slotUrlCopy()), ac, "copy_url" );
00729   mUrlOpenAction = new KAction( i18n("Open URL"), 0, this,
00730                  SLOT(slotUrlOpen()), ac, "open_url" );
00731   mAddBookmarksAction = new KAction( i18n("Bookmark This Link"),
00732                                      "bookmark_add",
00733                                      0, this, SLOT(slotAddBookmarks()),
00734                                      ac, "add_bookmarks" );
00735   mUrlSaveAsAction = new KAction( i18n("Save Link As..."), 0, this,
00736                  SLOT(slotUrlSave()), ac, "saveas_url" );
00737   mViewSourceAction = new KAction( i18n("&View Source"), Key_V, this,
00738               SLOT(slotShowMsgSrc()), ac, "view_source" );
00739 
00740   mToggleFixFontAction = new KToggleAction( i18n("Use Fi&xed Font"),
00741             Key_X, this, SLOT(slotToggleFixedFont()),
00742             ac, "toggle_fixedfont" );
00743 
00744   mStartIMChatAction = new KAction( i18n("Chat &With..."), 0, this,
00745                     SLOT(slotIMChat()), ac, "start_im_chat" );
00746 
00747 }
00748 
00749 //-----------------------------------------------------------------------------
00750 KMReaderWin::~KMReaderWin()
00751 {
00752   delete mHtmlWriter; mHtmlWriter = 0;
00753   if (mAutoDelete) delete message();
00754   delete mRootNode; mRootNode = 0;
00755   removeTempFiles();
00756 }
00757 
00758 
00759 //-----------------------------------------------------------------------------
00760 void KMReaderWin::slotMessageArrived( KMMessage *msg )
00761 {
00762   if (msg && ((KMMsgBase*)msg)->isMessage()) {
00763     if ( msg->getMsgSerNum() == mWaitingForSerNum ) {
00764       setMsg( msg, true );
00765     } else {
00766       kdDebug( 5006 ) <<  "KMReaderWin::slotMessageArrived - ignoring update" << endl;
00767     }
00768   }
00769 }
00770 
00771 //-----------------------------------------------------------------------------
00772 void KMReaderWin::update( KMail::Interface::Observable * observable ) {
00773   if ( !mAtmUpdate ) {
00774     kdDebug(5006) << "KMReaderWin::update - message" << endl;
00775     updateReaderWin();
00776     return;
00777   }
00778 
00779   if ( !mRootNode )
00780     return;
00781 
00782   kdDebug(5006) << "KMReaderWin::update - attachment " << mAtmCurrentName << endl;
00783   partNode * node = mRootNode->findId( mAtmCurrent );
00784   if ( !node ) {
00785     kdWarning(5006) << "KMReaderWin::update - Could not find node for attachment!" << endl;
00786     return;
00787   }
00788 
00789   assert( dynamic_cast<KMMessage*>( observable ) != 0 );
00790   // if the assert ever fails, this curious construction needs to
00791   // be rethought:
00792 
00793   // replace the dwpart of the node
00794   node->setDwPart( static_cast<KMMessage*>( observable )->lastUpdatedPart() );
00795   // update the tmp file
00796   // we have to set it writeable temporarily
00797   ::chmod( QFile::encodeName( mAtmCurrentName ), S_IRWXU );
00798   KPIM::kByteArrayToFile( node->msgPart().bodyDecodedBinary(), mAtmCurrentName,
00799             false, false, true );
00800   ::chmod( QFile::encodeName( mAtmCurrentName ), S_IRUSR );
00801 
00802   // no need to redisplay here as we only replaced the tmp file so that
00803   // the desired function (e.g. save) can work with it
00804 }
00805 
00806 //-----------------------------------------------------------------------------
00807 void KMReaderWin::removeTempFiles()
00808 {
00809   for (QStringList::Iterator it = mTempFiles.begin(); it != mTempFiles.end();
00810     it++)
00811   {
00812     QFile::remove(*it);
00813   }
00814   mTempFiles.clear();
00815   for (QStringList::Iterator it = mTempDirs.begin(); it != mTempDirs.end();
00816     it++)
00817   {
00818     QDir(*it).rmdir(*it);
00819   }
00820   mTempDirs.clear();
00821 }
00822 
00823 
00824 //-----------------------------------------------------------------------------
00825 bool KMReaderWin::event(QEvent *e)
00826 {
00827   if (e->type() == QEvent::ApplicationPaletteChange)
00828   {
00829     delete mCSSHelper;
00830     mCSSHelper = new CSSHelper( QPaintDeviceMetrics( mViewer->view() ), this );
00831     if (message())
00832       message()->readConfig();
00833     update( true ); // Force update
00834     return true;
00835   }
00836   return QWidget::event(e);
00837 }
00838 
00839 
00840 //-----------------------------------------------------------------------------
00841 void KMReaderWin::readConfig(void)
00842 {
00843   const KConfigGroup mdnGroup( KMKernel::config(), "MDN" );
00844   /*should be: const*/ KConfigGroup reader( KMKernel::config(), "Reader" );
00845 
00846   delete mCSSHelper;
00847   mCSSHelper = new CSSHelper( QPaintDeviceMetrics( mViewer->view() ), this );
00848 
00849   mNoMDNsWhenEncrypted = mdnGroup.readBoolEntry( "not-send-when-encrypted", true );
00850 
00851   // initialize useFixedFont from the saved value; the corresponding toggle
00852   // action is initialized in the main window
00853   mUseFixedFont = reader.readBoolEntry( "useFixedFont", false );
00854   mHtmlMail = reader.readBoolEntry( "htmlMail", false );
00855   setHeaderStyleAndStrategy( HeaderStyle::create( reader.readEntry( "header-style", "fancy" ) ),
00856                  HeaderStrategy::create( reader.readEntry( "header-set-displayed", "rich" ) ) );
00857 
00858   KRadioAction *raction = actionForHeaderStyle( headerStyle(), headerStrategy() );
00859   if ( raction )
00860     raction->setChecked( true );
00861 
00862   mAttachmentStrategy =
00863     AttachmentStrategy::create( reader.readEntry( "attachment-strategy" ) );
00864 
00865   mViewer->setOnlyLocalReferences( !reader.readBoolEntry( "htmlLoadExternal", false ) );
00866 
00867   // if the user uses OpenPGP then the color bar defaults to enabled
00868   // else it defaults to disabled
00869   mShowColorbar = reader.readBoolEntry( "showColorbar", Kpgp::Module::getKpgp()->usePGP() );
00870   // if the value defaults to enabled and KMail (with color bar) is used for
00871   // the first time the config dialog doesn't know this if we don't save the
00872   // value now
00873   reader.writeEntry( "showColorbar", mShowColorbar );
00874 
00875   mMimeTreeAtBottom = reader.readEntry( "MimeTreeLocation", "bottom" ) != "top";
00876   const QString s = reader.readEntry( "MimeTreeMode", "smart" );
00877   if ( s == "never" )
00878     mMimeTreeMode = 0;
00879   else if ( s == "always" )
00880     mMimeTreeMode = 2;
00881   else
00882     mMimeTreeMode = 1;
00883 
00884   const int mimeH = reader.readNumEntry( "MimePaneHeight", 100 );
00885   const int messageH = reader.readNumEntry( "MessagePaneHeight", 180 );
00886   mSplitterSizes.clear();
00887   if ( mMimeTreeAtBottom )
00888     mSplitterSizes << messageH << mimeH;
00889   else
00890     mSplitterSizes << mimeH << messageH;
00891 
00892 #ifdef KLEO_CHIASMUS
00893   mChiasmusKey = reader.readEntry( "chiasmus-key" );
00894   mChiasmusOptions = reader.readEntry( "chiasmus-options" );
00895 #endif
00896 
00897   adjustLayout();
00898 
00899   if (message())
00900     update();
00901   KMMessage::readConfig();
00902 }
00903 
00904 
00905 void KMReaderWin::adjustLayout() {
00906   if ( mMimeTreeAtBottom )
00907     mSplitter->moveToLast( mMimePartTree );
00908   else
00909     mSplitter->moveToFirst( mMimePartTree );
00910   mSplitter->setSizes( mSplitterSizes );
00911 
00912   if ( mMimeTreeMode == 2 && mMsgDisplay )
00913     mMimePartTree->show();
00914   else
00915     mMimePartTree->hide();
00916 
00917   if ( mShowColorbar && mMsgDisplay )
00918     mColorBar->show();
00919   else
00920     mColorBar->hide();
00921 }
00922 
00923 
00924 void KMReaderWin::saveSplitterSizes( KConfigBase & c ) const {
00925   if ( !mSplitter || !mMimePartTree )
00926     return;
00927   if ( mMimePartTree->isHidden() )
00928     return; // don't rely on QSplitter maintaining sizes for hidden widgets.
00929 
00930   c.writeEntry( "MimePaneHeight", mSplitter->sizes()[ mMimeTreeAtBottom ? 1 : 0 ] );
00931   c.writeEntry( "MessagePaneHeight", mSplitter->sizes()[ mMimeTreeAtBottom ? 0 : 1 ] );
00932 }
00933 
00934 //-----------------------------------------------------------------------------
00935 void KMReaderWin::writeConfig( bool sync ) const {
00936   KConfigGroup reader( KMKernel::config(), "Reader" );
00937 
00938   reader.writeEntry( "useFixedFont", mUseFixedFont );
00939   if ( headerStyle() )
00940     reader.writeEntry( "header-style", headerStyle()->name() );
00941   if ( headerStrategy() )
00942     reader.writeEntry( "header-set-displayed", headerStrategy()->name() );
00943   if ( attachmentStrategy() )
00944     reader.writeEntry( "attachment-strategy", attachmentStrategy()->name() );
00945 
00946 #ifdef KLEO_CHIASMUS
00947   reader.writeEntry( "chiasmus-key", mChiasmusKey );
00948   reader.writeEntry( "chiasmus-options", mChiasmusOptions );
00949 #endif
00950 
00951   saveSplitterSizes( reader );
00952 
00953   if ( sync )
00954     GlobalSettings::self()->requestSync();
00955 }
00956 
00957 //-----------------------------------------------------------------------------
00958 void KMReaderWin::initHtmlWidget(void)
00959 {
00960   mViewer->widget()->setFocusPolicy(WheelFocus);
00961   // Let's better be paranoid and disable plugins (it defaults to enabled):
00962   mViewer->setPluginsEnabled(false);
00963   mViewer->setJScriptEnabled(false); // just make this explicit
00964   mViewer->setJavaEnabled(false);    // just make this explicit
00965   mViewer->setMetaRefreshEnabled(false);
00966   mViewer->setURLCursor(KCursor::handCursor());
00967   // Espen 2000-05-14: Getting rid of thick ugly frames
00968   mViewer->view()->setLineWidth(0);
00969 
00970   if ( !htmlWriter() )
00971 #ifdef KMAIL_READER_HTML_DEBUG
00972     mHtmlWriter = new TeeHtmlWriter( new FileHtmlWriter( QString::null ),
00973                      new KHtmlPartHtmlWriter( mViewer, 0 ) );
00974 #else
00975     mHtmlWriter = new KHtmlPartHtmlWriter( mViewer, 0 );
00976 #endif
00977 
00978   connect(mViewer->browserExtension(),
00979           SIGNAL(openURLRequest(const KURL &, const KParts::URLArgs &)),this,
00980           SLOT(slotUrlOpen(const KURL &)));
00981   connect(mViewer->browserExtension(),
00982           SIGNAL(createNewWindow(const KURL &, const KParts::URLArgs &)),this,
00983           SLOT(slotUrlOpen(const KURL &)));
00984   connect(mViewer,SIGNAL(onURL(const QString &)),this,
00985           SLOT(slotUrlOn(const QString &)));
00986   connect(mViewer,SIGNAL(popupMenu(const QString &, const QPoint &)),
00987           SLOT(slotUrlPopup(const QString &, const QPoint &)));
00988   connect( kmkernel->imProxy(), SIGNAL( sigContactPresenceChanged( const QString & ) ),
00989           this, SLOT( contactStatusChanged( const QString & ) ) );
00990   connect( kmkernel->imProxy(), SIGNAL( sigPresenceInfoExpired() ),
00991           this, SLOT( updateReaderWin() ) );
00992 }
00993 
00994 void KMReaderWin::contactStatusChanged( const QString &uid)
00995 {
00996     kdDebug( 5006 ) << k_funcinfo << " got a presence change for " << uid << endl;
00997     // get the list of nodes for this contact from the htmlView
00998     DOM::NodeList presenceNodes = mViewer->htmlDocument()
00999         .getElementsByName( DOM::DOMString( QString::fromLatin1("presence-") + uid ) );
01000     for ( unsigned int i = 0; i < presenceNodes.length(); ++i )
01001     {
01002         DOM::Node n =  presenceNodes.item( i );
01003         kdDebug( 5006 ) << "name is " << n.nodeName().string() << endl;
01004         kdDebug( 5006 ) << "value of content was " << n.firstChild().nodeValue().string() << endl;
01005         QString newPresence = kmkernel->imProxy()->presenceString( uid );
01006         if ( newPresence.isNull() ) // KHTML crashes if you setNodeValue( QString::null )
01007             newPresence = QString::fromLatin1( "ENOIMRUNNING" );
01008         n.firstChild().setNodeValue( newPresence );
01009         kdDebug( 5006 ) << "value of content is now " << n.firstChild().nodeValue().string() << endl;
01010     }
01011     kdDebug( 5006 ) << "and we updated the above presence nodes" << uid << endl;
01012 }
01013 
01014 void KMReaderWin::setAttachmentStrategy( const AttachmentStrategy * strategy ) {
01015   mAttachmentStrategy = strategy ? strategy : AttachmentStrategy::smart() ;
01016   update( true );
01017 }
01018 
01019 void KMReaderWin::setHeaderStyleAndStrategy( const HeaderStyle * style,
01020                          const HeaderStrategy * strategy ) {
01021   mHeaderStyle = style ? style : HeaderStyle::fancy() ;
01022   mHeaderStrategy = strategy ? strategy : HeaderStrategy::rich() ;
01023   update( true );
01024 }
01025 
01026 //-----------------------------------------------------------------------------
01027 void KMReaderWin::setOverrideEncoding( const QString & encoding )
01028 {
01029   if ( encoding == mOverrideEncoding )
01030     return;
01031 
01032   mOverrideEncoding = encoding;
01033   if ( mSelectEncodingAction ) {
01034     if ( encoding.isEmpty() ) {
01035       mSelectEncodingAction->setCurrentItem( 0 );
01036     }
01037     else {
01038       QStringList encodings = mSelectEncodingAction->items();
01039       int i = 0;
01040       for ( QStringList::const_iterator it = encodings.begin(), end = encodings.end(); it != end; ++it, ++i ) {
01041         if ( KGlobal::charsets()->encodingForName( *it ) == encoding ) {
01042           mSelectEncodingAction->setCurrentItem( i );
01043           break;
01044         }
01045       }
01046     }
01047   }
01048   update( true );
01049 }
01050 
01051 //--------------------------------------------------------------------------
01052 void KMReaderWin::slotFancyHeaders() {
01053   setHeaderStyleAndStrategy( HeaderStyle::fancy(),
01054                                        HeaderStrategy::rich() );
01055 }
01056 
01057 void KMReaderWin::slotBriefHeaders() {
01058   setHeaderStyleAndStrategy( HeaderStyle::brief(),
01059                                        HeaderStrategy::brief() );
01060 }
01061 
01062 void KMReaderWin::slotStandardHeaders() {
01063   setHeaderStyleAndStrategy( HeaderStyle::plain(),
01064                                        HeaderStrategy::standard());
01065 }
01066 
01067 void KMReaderWin::slotLongHeaders() {
01068   setHeaderStyleAndStrategy( HeaderStyle::plain(),
01069                                        HeaderStrategy::rich() );
01070 }
01071 
01072 void KMReaderWin::slotMinimalHeaders() {
01073   setHeaderStyleAndStrategy( HeaderStyle::minimal(),
01074                                        HeaderStrategy::rich() );
01075 }
01076 
01077 void KMReaderWin::slotAllHeaders() {
01078   setHeaderStyleAndStrategy( HeaderStyle::plain(),
01079                                        HeaderStrategy::all() );
01080 }
01081 
01082 void KMReaderWin::slotCycleHeaderStyles() {
01083   const HeaderStrategy * strategy = headerStrategy();
01084   const HeaderStyle * style = headerStyle();
01085 
01086   const char * actionName = 0;
01087   if ( style == HeaderStyle::fancy() ) {
01088     slotBriefHeaders();
01089     actionName = "view_headers_brief";
01090   } else if ( style == HeaderStyle::brief() ) {
01091     slotStandardHeaders();
01092     actionName = "view_headers_standard";
01093   } else if ( style == HeaderStyle::minimal() ) {
01094     slotMinimalHeaders();
01095     actionName = "view_headers_minimal";
01096   } else if ( style == HeaderStyle::plain() ) {
01097     if ( strategy == HeaderStrategy::standard() ) {
01098       slotLongHeaders();
01099       actionName = "view_headers_long";
01100     } else if ( strategy == HeaderStrategy::rich() ) {
01101       slotAllHeaders();
01102       actionName = "view_headers_all";
01103     } else if ( strategy == HeaderStrategy::all() ) {
01104       slotFancyHeaders();
01105       actionName = "view_headers_fancy";
01106     }
01107   }
01108 
01109   if ( actionName )
01110     static_cast<KRadioAction*>( mActionCollection->action( actionName ) )->setChecked( true );
01111 }
01112 
01113 // little helper function
01114 KRadioAction * KMReaderWin::actionForHeaderStyle( const HeaderStyle * style, const HeaderStrategy * strategy ) {
01115   if ( !mActionCollection )
01116     return 0;
01117   const char * actionName = 0;
01118   if ( style == HeaderStyle::fancy() )
01119     actionName = "view_headers_fancy";
01120   else if ( style == HeaderStyle::brief() )
01121     actionName = "view_headers_brief";
01122   else if ( style == HeaderStyle::minimal() )
01123     actionName == "view_headers_minimal";
01124   else if ( style == HeaderStyle::plain() ) {
01125     if ( strategy == HeaderStrategy::standard() )
01126       actionName = "view_headers_standard";
01127     else if ( strategy == HeaderStrategy::rich() )
01128       actionName = "view_headers_long";
01129     else if ( strategy == HeaderStrategy::all() )
01130       actionName = "view_headers_all";
01131   }
01132   if ( actionName )
01133     return static_cast<KRadioAction*>(mActionCollection->action(actionName));
01134   else
01135     return 0;
01136 }
01137 
01138 //-----------------------------------------------------------------------------
01139 const QTextCodec * KMReaderWin::overrideCodec() const
01140 {
01141   kdDebug(5006) << k_funcinfo << " mOverrideEncoding == '" << mOverrideEncoding << "'" << endl;
01142   if ( mOverrideEncoding.isEmpty() || mOverrideEncoding == "Auto" ) // Auto
01143     return 0;
01144   else
01145     return KMMsgBase::codecForName( mOverrideEncoding.latin1() );
01146 }
01147 
01148 //-----------------------------------------------------------------------------
01149 void KMReaderWin::slotSetEncoding()
01150 {
01151   if ( mSelectEncodingAction->currentItem() == 0 ) // Auto
01152     mOverrideEncoding = QString();
01153   else
01154     mOverrideEncoding = KGlobal::charsets()->encodingForName( mSelectEncodingAction->currentText() );
01155   update( true );
01156 }
01157 
01158 //-----------------------------------------------------------------------------
01159 void KMReaderWin::readGlobalOverrideCodec()
01160 {
01161   // if the global character encoding wasn't changed then there's nothing to do
01162   if ( GlobalSettings::self()->overrideCharacterEncoding() == mOldGlobalOverrideEncoding )
01163     return;
01164 
01165   setOverrideEncoding( GlobalSettings::self()->overrideCharacterEncoding() );
01166   mOldGlobalOverrideEncoding = GlobalSettings::self()->overrideCharacterEncoding();
01167 }
01168 
01169 //-----------------------------------------------------------------------------
01170 void KMReaderWin::setMsg(KMMessage* aMsg, bool force)
01171 {
01172   if (aMsg)
01173       kdDebug(5006) << "(" << aMsg->getMsgSerNum() << ", last " << mLastSerNum << ") " << aMsg->subject() << " "
01174         << aMsg->fromStrip() << ", readyToShow " << (aMsg->readyToShow()) << endl;
01175 
01176   bool complete = true;
01177   if ( aMsg &&
01178        !aMsg->readyToShow() &&
01179        (aMsg->getMsgSerNum() != mLastSerNum) &&
01180        !aMsg->isComplete() )
01181     complete = false;
01182 
01183   // If not forced and there is aMsg and aMsg is same as mMsg then return
01184   if (!force && aMsg && mLastSerNum != 0 && aMsg->getMsgSerNum() == mLastSerNum)
01185     return;
01186 
01187   // (de)register as observer
01188   if (aMsg && message())
01189     message()->detach( this );
01190   if (aMsg)
01191     aMsg->attach( this );
01192   mAtmUpdate = false;
01193 
01194   // connect to the updates if we have hancy headers
01195 
01196   mDelayedMarkTimer.stop();
01197 
01198   mMessage = 0;
01199   if ( !aMsg ) {
01200     mWaitingForSerNum = 0; // otherwise it has been set
01201     mLastSerNum = 0;
01202   } else {
01203     mLastSerNum = aMsg->getMsgSerNum();
01204     // Check if the serial number can be used to find the assoc KMMessage
01205     // If so, keep only the serial number (and not mMessage), to avoid a dangling mMessage
01206     // when going to another message in the mainwindow.
01207     // Otherwise, keep only mMessage, this is fine for standalone KMReaderMainWins since
01208     // we're working on a copy of the KMMessage, which we own.
01209     if (message() != aMsg) {
01210       mMessage = aMsg;
01211       mLastSerNum = 0;
01212     }
01213   }
01214 
01215   if (aMsg) {
01216     aMsg->setOverrideCodec( overrideCodec() );
01217     aMsg->setDecodeHTML( htmlMail() );
01218     mLastStatus = aMsg->status();
01219     // FIXME: workaround to disable DND for IMAP load-on-demand
01220     if ( !aMsg->isComplete() )
01221       mViewer->setDNDEnabled( false );
01222     else
01223       mViewer->setDNDEnabled( true );
01224   } else {
01225     mLastStatus = KMMsgStatusUnknown;
01226   }
01227 
01228   // only display the msg if it is complete
01229   // otherwise we'll get flickering with progressively loaded messages
01230   if ( complete )
01231   {
01232     // Avoid flicker, somewhat of a cludge
01233     if (force) {
01234       // stop the timer to avoid calling updateReaderWin twice
01235       updateReaderWinTimer.stop();
01236       updateReaderWin();
01237     }
01238     else if (updateReaderWinTimer.isActive())
01239       updateReaderWinTimer.changeInterval( delay );
01240     else
01241       updateReaderWinTimer.start( 0, TRUE );
01242   }
01243 
01244   if ( aMsg && (aMsg->isUnread() || aMsg->isNew()) && GlobalSettings::self()->delayedMarkAsRead() ) {
01245     if ( GlobalSettings::self()->delayedMarkTime() != 0 )
01246       mDelayedMarkTimer.start( GlobalSettings::self()->delayedMarkTime() * 1000, TRUE );
01247     else
01248       slotTouchMessage();
01249   }
01250 }
01251 
01252 //-----------------------------------------------------------------------------
01253 void KMReaderWin::clearCache()
01254 {
01255   updateReaderWinTimer.stop();
01256   clear();
01257   mDelayedMarkTimer.stop();
01258   mLastSerNum = 0;
01259   mWaitingForSerNum = 0;
01260   mMessage = 0;
01261 }
01262 
01263 // enter items for the "Important changes" list here:
01264 static const char * const kmailChanges[] = {
01265   I18N_NOOP("Support for 3rd-party CryptPlugs has been discontinued. "
01266         "Support for the GnuPG cryptographic backend is now included "
01267         "directly in KMail.")
01268 };
01269 static const int numKMailChanges =
01270   sizeof kmailChanges / sizeof *kmailChanges;
01271 
01272 // enter items for the "new features" list here, so the main body of
01273 // the welcome page can be left untouched (probably much easier for
01274 // the translators). Note that the <li>...</li> tags are added
01275 // automatically below:
01276 static const char * const kmailNewFeatures[] = {
01277   I18N_NOOP( "Antispam wizard" ),
01278   I18N_NOOP( "Filter log" ),
01279   I18N_NOOP( "Quick search" ),
01280   I18N_NOOP( "Automatic mailing-list detection" ),
01281   I18N_NOOP( "View/open message files" ),
01282   I18N_NOOP( "HTML message composing" ),
01283   I18N_NOOP( "New filter criteria: in address book, in category, has attachment" ),
01284   I18N_NOOP("Cryptographic backend auto-configuration"),
01285   I18N_NOOP("Sign/encrypt key separation"),
01286   I18N_NOOP("Per-identity S/MIME key preselection"),
01287   I18N_NOOP("Per-identity cryptographic message format preselection"),
01288   I18N_NOOP("Per-contact crypto preferences"),
01289   I18N_NOOP("List only opened IMAP folders"),
01290 };
01291 static const int numKMailNewFeatures =
01292   sizeof kmailNewFeatures / sizeof *kmailNewFeatures;
01293 
01294 
01295 //-----------------------------------------------------------------------------
01296 //static
01297 QString KMReaderWin::newFeaturesMD5()
01298 {
01299   QCString str;
01300   for ( int i = 0 ; i < numKMailChanges ; ++i )
01301     str += kmailChanges[i];
01302   for ( int i = 0 ; i < numKMailNewFeatures ; ++i )
01303     str += kmailNewFeatures[i];
01304   KMD5 md5( str );
01305   return md5.base64Digest();
01306 }
01307 
01308 //-----------------------------------------------------------------------------
01309 void KMReaderWin::displayAboutPage()
01310 {
01311   mMsgDisplay = false;
01312   adjustLayout();
01313 
01314   QString location = locate("data", "kmail/about/main.html");
01315   QString content = KPIM::kFileToString(location);
01316   mViewer->begin(KURL( location ));
01317   QString info =
01318     i18n("%1: KMail version; %2: help:// URL; %3: homepage URL; "
01319      "%4: prior KMail version; %5: prior KDE version; "
01320      "%6: generated list of new features; "
01321      "%7: First-time user text (only shown on first start); "
01322      "%8: prior KMail version; "
01323          "%9: generated list of important changes; "
01324      "--- end of comment ---",
01325      "<h2>Welcome to KMail %1</h2><p>KMail is the email client for the K "
01326      "Desktop Environment. It is designed to be fully compatible with "
01327      "Internet mailing standards including MIME, SMTP, POP3 and IMAP."
01328      "</p>\n"
01329      "<ul><li>KMail has many powerful features which are described in the "
01330      "<a href=\"%2\">documentation</a></li>\n"
01331      "<li>The <a href=\"%3\">KMail homepage</A> offers information about "
01332      "new versions of KMail</li></ul>\n"
01333          "<p><span style='font-size:125%; font-weight:bold;'>"
01334          "Important changes</span> (compared to KMail %8):</p>\n"
01335      "<ul>\n%9</ul>\n"
01336      "<p>Some of the new features in this release of KMail include "
01337      "(compared to KMail %4, which is part of KDE %5):</p>\n"
01338      "<ul>\n%6</ul>\n"
01339      "%7\n"
01340      "<p>We hope that you will enjoy KMail.</p>\n"
01341      "<p>Thank you,</p>\n"
01342      "<p>&nbsp; &nbsp; The KMail Team</p>")
01343     .arg(KMAIL_VERSION) // KMail version
01344     .arg("help:/kmail/index.html") // KMail help:// URL
01345     .arg("http://kmail.kde.org/") // KMail homepage URL
01346     .arg("1.6").arg("3.2"); // prior KMail and KDE version
01347 
01348   QString featureItems;
01349   for ( int i = 0 ; i < numKMailNewFeatures ; i++ )
01350     featureItems += i18n("<li>%1</li>\n").arg( i18n( kmailNewFeatures[i] ) );
01351 
01352   info = info.arg( featureItems );
01353 
01354   if( kmkernel->firstStart() ) {
01355     info = info.arg( i18n("<p>Please take a moment to fill in the KMail "
01356               "configuration panel at Settings-&gt;Configure "
01357               "KMail.\n"
01358               "You need to create at least a default identity and "
01359               "an incoming as well as outgoing mail account."
01360               "</p>\n") );
01361   } else {
01362     info = info.arg( QString::null );
01363   }
01364 
01365   QString changesItems;
01366   for ( int i = 0 ; i < numKMailChanges ; i++ )
01367     changesItems += i18n("<li>%1</li>\n").arg( i18n( kmailChanges[i] ) );
01368 
01369   info = info.arg("1.6").arg( changesItems );
01370 
01371   mViewer->write(content.arg(pointsToPixel( mCSSHelper->bodyFont().pointSize() )).arg(info));
01372   mViewer->end();
01373 }
01374 
01375 void KMReaderWin::enableMsgDisplay() {
01376   mMsgDisplay = true;
01377   adjustLayout();
01378 }
01379 
01380 
01381 //-----------------------------------------------------------------------------
01382 
01383 void KMReaderWin::updateReaderWin()
01384 {
01385   if (!mMsgDisplay) return;
01386 
01387   htmlWriter()->reset();
01388 
01389   KMFolder* folder = 0;
01390   if (message(&folder))
01391   {
01392     if( !kmkernel->iCalIface().isResourceFolder( folder ) ){
01393       if ( mShowColorbar )
01394         mColorBar->show();
01395       else
01396         mColorBar->hide();
01397       displayMessage();
01398     }
01399   }
01400   else
01401   {
01402     mColorBar->hide();
01403     mMimePartTree->hide();
01404     mMimePartTree->clear();
01405     htmlWriter()->begin( mCSSHelper->cssDefinitions( isFixedFont() ) );
01406     htmlWriter()->write( mCSSHelper->htmlHead( isFixedFont() ) + "</body></html>" );
01407     htmlWriter()->end();
01408   }
01409 }
01410 
01411 //-----------------------------------------------------------------------------
01412 int KMReaderWin::pointsToPixel(int pointSize) const
01413 {
01414   const QPaintDeviceMetrics pdm(mViewer->view());
01415 
01416   return (pointSize * pdm.logicalDpiY() + 36) / 72;
01417 }
01418 
01419 //-----------------------------------------------------------------------------
01420 void KMReaderWin::showHideMimeTree( bool isPlainTextTopLevel ) {
01421   if ( mMimeTreeMode == 2 ||
01422        ( mMimeTreeMode == 1 && !isPlainTextTopLevel ) )
01423     mMimePartTree->show();
01424   else {
01425     // don't rely on QSplitter maintaining sizes for hidden widgets:
01426     KConfigGroup reader( KMKernel::config(), "Reader" );
01427     saveSplitterSizes( reader );
01428     mMimePartTree->hide();
01429   }
01430 }
01431 
01432 void KMReaderWin::displayMessage() {
01433   KMMessage * msg = message();
01434 
01435   mMimePartTree->clear();
01436   showHideMimeTree( !msg || // treat no message as "text/plain"
01437             ( msg->type() == DwMime::kTypeText
01438               && msg->subtype() == DwMime::kSubtypePlain ) );
01439 
01440   if ( !msg )
01441     return;
01442 
01443   msg->setOverrideCodec( overrideCodec() );
01444 
01445   htmlWriter()->begin( mCSSHelper->cssDefinitions( isFixedFont() ) );
01446   htmlWriter()->queue( mCSSHelper->htmlHead( isFixedFont() ) );
01447 
01448   if (!parent())
01449     setCaption(msg->subject());
01450 
01451   removeTempFiles();
01452 
01453   mColorBar->setNeutralMode();
01454 
01455   parseMsg(msg);
01456 
01457   if( mColorBar->isNeutral() )
01458     mColorBar->setNormalMode();
01459 
01460   htmlWriter()->queue("</body></html>");
01461   htmlWriter()->flush();
01462 }
01463 
01464 
01465 //-----------------------------------------------------------------------------
01466 void KMReaderWin::parseMsg(KMMessage* aMsg)
01467 {
01468 #ifndef NDEBUG
01469   kdDebug( 5006 )
01470     << "parseMsg(KMMessage* aMsg "
01471     << ( aMsg == message() ? "==" : "!=" )
01472     << " aMsg )" << endl;
01473 #endif
01474 
01475   KMMessagePart msgPart;
01476   QCString subtype, contDisp;
01477   QByteArray str;
01478 
01479   assert(aMsg!=0);
01480 
01481   delete mRootNode;
01482   mRootNode = partNode::fromMessage( aMsg );
01483   const QCString mainCntTypeStr = mRootNode->typeString() + '/' + mRootNode->subTypeString();
01484 
01485   QString cntDesc = aMsg->subject();
01486   if( cntDesc.isEmpty() )
01487     cntDesc = i18n("( body part )");
01488   KIO::filesize_t cntSize = aMsg->msgSize();
01489   QString cntEnc;
01490   if( aMsg->contentTransferEncodingStr().isEmpty() )
01491     cntEnc = "7bit";
01492   else
01493     cntEnc = aMsg->contentTransferEncodingStr();
01494 
01495   // fill the MIME part tree viewer
01496   mRootNode->fillMimePartTree( 0,
01497                    mMimePartTree,
01498                    cntDesc,
01499                    mainCntTypeStr,
01500                    cntEnc,
01501                    cntSize );
01502 
01503   partNode* vCardNode = mRootNode->findType( DwMime::kTypeText, DwMime::kSubtypeXVCard );
01504   bool hasVCard = false;
01505   if( vCardNode ) {
01506     // ### FIXME: We should only do this if the vCard belongs to the sender,
01507     // ### i.e. if the sender's email address is contained in the vCard.
01508     const QString vcard = vCardNode->msgPart().bodyToUnicode( overrideCodec() );
01509     KABC::VCardConverter t;
01510     if ( !t.parseVCards( vcard ).empty() ) {
01511       hasVCard = true;
01512       kdDebug(5006) << "FOUND A VALID VCARD" << endl;
01513       writeMessagePartToTempFile( &vCardNode->msgPart(), vCardNode->nodeId() );
01514     }
01515   }
01516   htmlWriter()->queue( writeMsgHeader(aMsg, hasVCard) );
01517 
01518   // show message content
01519   ObjectTreeParser otp( this );
01520   otp.parseObjectTree( mRootNode );
01521 
01522   // store encrypted/signed status information in the KMMessage
01523   //  - this can only be done *after* calling parseObjectTree()
01524   KMMsgEncryptionState encryptionState = mRootNode->overallEncryptionState();
01525   KMMsgSignatureState  signatureState  = mRootNode->overallSignatureState();
01526   aMsg->setEncryptionState( encryptionState );
01527   aMsg->setSignatureState(  signatureState  );
01528 
01529   bool emitReplaceMsgByUnencryptedVersion = false;
01530   const KConfigGroup reader( KMKernel::config(), "Reader" );
01531   if ( reader.readBoolEntry( "store-displayed-messages-unencrypted", false ) ) {
01532 
01533     // Hack to make sure the S/MIME CryptPlugs follows the strict requirement
01534     // of german government:
01535     // --> All received encrypted messages *must* be stored in unencrypted form
01536     //     after they have been decrypted once the user has read them.
01537     //     ( "Aufhebung der Verschluesselung nach dem Lesen" )
01538     //
01539     // note: Since there is no configuration option for this, we do that for
01540     //       all kinds of encryption now - *not* just for S/MIME.
01541     //       This could be changed in the objectTreeToDecryptedMsg() function
01542     //       by deciding when (or when not, resp.) to set the 'dataNode' to
01543     //       something different than 'curNode'.
01544     //
01545     // additional requirenment, according to AEgypten issue #167:
01546     //       There should be a dialog raising each time to ask whether
01547     //       the encryption should really be removed. There should be
01548     //       no option "don't show this request again".
01549 
01550 kdDebug(5006) << "\n\n\nKMReaderWin::parseMsg()  -  special post-encryption handling:\n1." << endl;
01551 /*
01552 kdDebug(5006) << "(aMsg == msg) = "                               << (aMsg == message()) << endl;
01553 kdDebug(5006) << "   (KMMsgStatusUnknown & mLastStatus) = "           << (KMMsgStatusUnknown & mLastStatus) << endl;
01554 kdDebug(5006) << "|| (KMMsgStatusNew     & mLastStatus) = "           << (KMMsgStatusNew     & mLastStatus) << endl;
01555 kdDebug(5006) << "|| (KMMsgStatusUnread  & mLastStatus) = "           << (KMMsgStatusUnread  & mLastStatus) << endl;
01556 kdDebug(5006) << "(mIdOfLastViewedMessage != aMsg->msgId()) = "    << (mIdOfLastViewedMessage != aMsg->msgId()) << endl;
01557 kdDebug(5006) << "   (KMMsgFullyEncrypted == encryptionState) = "     << (KMMsgFullyEncrypted == encryptionState) << endl;
01558 kdDebug(5006) << "|| (KMMsgPartiallyEncrypted == encryptionState) = " << (KMMsgPartiallyEncrypted == encryptionState) << endl;
01559 */
01560     // only proceed if we were called the normal way - not by
01561     // double click on the message (==not running in a separate window)
01562     if(    (aMsg == message())
01563         /*
01564            We remove this, because it is now required to ask EACH time :-)
01565 
01566           // only proceed if this message was not saved encryptedly before
01567           // to make sure only *new* messages are saved in decrypted form
01568         && ((KMMsgStatusUnknown | KMMsgStatusNew | KMMsgStatusUnread) & mLastStatus)
01569         */
01570 
01571           // avoid endless recursions
01572         && (mIdOfLastViewedMessage != aMsg->msgId())
01573           // only proceed if this message is (at least partially) encrypted
01574         && (    (KMMsgFullyEncrypted == encryptionState)
01575              || (KMMsgPartiallyEncrypted == encryptionState) ) ) {
01576 
01577       // ask if the user really wants to save in unencrypted state
01578       // (We do not show a "[ ] Do not ask me again" option here.)
01579       if ( KMessageBox::questionYesNo( this,
01580             i18n( "Should this message really be saved unencrypted?" ),
01581             i18n( "Save message in mail folder" ),
01582             KStdGuiItem::yes(), KStdGuiItem::no() ) // defaults
01583           == KMessageBox::Yes )
01584       {
01585 
01586 kdDebug(5006) << "KMReaderWin  -  calling objectTreeToDecryptedMsg()" << endl;
01587 
01588         NewByteArray decryptedData;
01589         // note: The following call may change the message's headers.
01590         objectTreeToDecryptedMsg( mRootNode, decryptedData, *aMsg );
01591         // add a \0 to the data
01592         decryptedData.appendNULL();
01593         QCString resultString( decryptedData.data() );
01594 kdDebug(5006) << "KMReaderWin  -  resulting data:" << resultString << endl;
01595 
01596         if( !resultString.isEmpty() ) {
01597 kdDebug(5006) << "KMReaderWin  -  composing unencrypted message" << endl;
01598           // try this:
01599           aMsg->setBody( resultString );
01600           KMMessage* unencryptedMessage = new KMMessage( *aMsg );
01601           unencryptedMessage->setParent( 0 );
01602           // because this did not work:
01603           /*
01604           DwMessage dwMsg( DwString( aMsg->asString() ) );
01605           dwMsg.Body() = DwBody( DwString( resultString.data() ) );
01606           dwMsg.Body().Parse();
01607           KMMessage* unencryptedMessage = new KMMessage( &dwMsg );
01608           */
01609 kdDebug(5006) << "KMReaderWin  -  resulting message:" << unencryptedMessage->asString() << endl;
01610 kdDebug(5006) << "KMReaderWin  -  attach unencrypted message to aMsg" << endl;
01611           aMsg->setUnencryptedMsg( unencryptedMessage );
01612           emitReplaceMsgByUnencryptedVersion = true;
01613         }
01614       }
01615     }
01616   }
01617 
01618   // save current main Content-Type before deleting mRootNode
01619   const int rootNodeCntType = mRootNode ? mRootNode->type() : DwMime::kTypeText;
01620   const int rootNodeCntSubtype = mRootNode ? mRootNode->subType() : DwMime::kSubtypePlain;
01621 
01622   // store message id to avoid endless recursions
01623   setIdOfLastViewedMessage( aMsg->msgId() );
01624 
01625   if( emitReplaceMsgByUnencryptedVersion ) {
01626     kdDebug(5006) << "KMReaderWin  -  invoce saving in decrypted form:" << endl;
01627     emit replaceMsgByUnencryptedVersion();
01628   } else {
01629     kdDebug(5006) << "KMReaderWin  -  finished parsing and displaying of message." << endl;
01630     showHideMimeTree( rootNodeCntType == DwMime::kTypeText &&
01631               rootNodeCntSubtype == DwMime::kSubtypePlain );
01632   }
01633 }
01634 
01635 
01636 //-----------------------------------------------------------------------------
01637 QString KMReaderWin::writeMsgHeader(KMMessage* aMsg, bool hasVCard)
01638 {
01639   kdFatal( !headerStyle(), 5006 )
01640     << "trying to writeMsgHeader() without a header style set!" << endl;
01641   kdFatal( !headerStrategy(), 5006 )
01642     << "trying to writeMsgHeader() without a header strategy set!" << endl;
01643   QString href;
01644   if (hasVCard)
01645     href = QString("file:") + KURL::encode_string( mTempFiles.last() );
01646 
01647   return headerStyle()->format( aMsg, headerStrategy(), href, mPrinting );
01648 }
01649 
01650 
01651 
01652 //-----------------------------------------------------------------------------
01653 QString KMReaderWin::writeMessagePartToTempFile( KMMessagePart* aMsgPart,
01654                                                  int aPartNum )
01655 {
01656   QString fileName = aMsgPart->fileName();
01657   if( fileName.isEmpty() )
01658     fileName = aMsgPart->name();
01659 
01660   //--- Sven's save attachments to /tmp start ---
01661   KTempFile *tempFile = new KTempFile( QString::null,
01662                                        "." + QString::number( aPartNum ) );
01663   tempFile->setAutoDelete( true );
01664   QString fname = tempFile->name();
01665   delete tempFile;
01666 
01667   if( ::access( QFile::encodeName( fname ), W_OK ) != 0 )
01668     // Not there or not writable
01669     if( ::mkdir( QFile::encodeName( fname ), 0 ) != 0
01670         || ::chmod( QFile::encodeName( fname ), S_IRWXU ) != 0 )
01671       return QString::null; //failed create
01672 
01673   assert( !fname.isNull() );
01674 
01675   mTempDirs.append( fname );
01676   // strip off a leading path
01677   int slashPos = fileName.findRev( '/' );
01678   if( -1 != slashPos )
01679     fileName = fileName.mid( slashPos + 1 );
01680   if( fileName.isEmpty() )
01681     fileName = "unnamed";
01682   fname += "/" + fileName;
01683 
01684   QByteArray data = aMsgPart->bodyDecodedBinary();
01685   size_t size = data.size();
01686   if ( aMsgPart->type() == DwMime::kTypeText && size) {
01687     // convert CRLF to LF before writing text attachments to disk
01688     size = KMFolder::crlf2lf( data.data(), size );
01689   }
01690   if( !KPIM::kBytesToFile( data.data(), size, fname, false, false, false ) )
01691     return QString::null;
01692 
01693   mTempFiles.append( fname );
01694   // make file read-only so that nobody gets the impression that he might
01695   // edit attached files (cf. bug #52813)
01696   ::chmod( QFile::encodeName( fname ), S_IRUSR );
01697 
01698   return fname;
01699 }
01700 
01701 
01702 //-----------------------------------------------------------------------------
01703 void KMReaderWin::showVCard( KMMessagePart * msgPart ) {
01704   const QString vCard = msgPart->bodyToUnicode( overrideCodec() );
01705 
01706   VCardViewer *vcv = new VCardViewer(this, vCard, "vCardDialog");
01707   vcv->show();
01708 }
01709 
01710 //-----------------------------------------------------------------------------
01711 void KMReaderWin::printMsg()
01712 {
01713   if (!message()) return;
01714   mViewer->view()->print();
01715 }
01716 
01717 
01718 //-----------------------------------------------------------------------------
01719 int KMReaderWin::msgPartFromUrl(const KURL &aUrl)
01720 {
01721   if (aUrl.isEmpty()) return -1;
01722 
01723   if (!aUrl.isLocalFile()) return -1;
01724 
01725   QString path = aUrl.path();
01726   uint right = path.findRev('/');
01727   uint left = path.findRev('.', right);
01728 
01729   bool ok;
01730   int res = path.mid(left + 1, right - left - 1).toInt(&ok);
01731   return (ok) ? res : -1;
01732 }
01733 
01734 
01735 //-----------------------------------------------------------------------------
01736 void KMReaderWin::resizeEvent(QResizeEvent *)
01737 {
01738   if( !mResizeTimer.isActive() )
01739   {
01740     //
01741     // Combine all resize operations that are requested as long a
01742     // the timer runs.
01743     //
01744     mResizeTimer.start( 100, true );
01745   }
01746 }
01747 
01748 
01749 //-----------------------------------------------------------------------------
01750 void KMReaderWin::slotDelayedResize()
01751 {
01752   mSplitter->setGeometry(0, 0, width(), height());
01753 }
01754 
01755 
01756 //-----------------------------------------------------------------------------
01757 void KMReaderWin::slotTouchMessage()
01758 {
01759   if ( !message() )
01760     return;
01761 
01762   if ( !message()->isNew() && !message()->isUnread() )
01763     return;
01764 
01765   SerNumList serNums;
01766   serNums.append( message()->getMsgSerNum() );
01767   KMCommand *command = new KMSetStatusCommand( KMMsgStatusRead, serNums );
01768   command->start();
01769   if ( mNoMDNsWhenEncrypted &&
01770        message()->encryptionState() != KMMsgNotEncrypted &&
01771        message()->encryptionState() != KMMsgEncryptionStateUnknown )
01772     return;
01773   if ( KMMessage * receipt = message()->createMDN( MDN::ManualAction,
01774                            MDN::Displayed,
01775                            true /* allow GUI */ ) )
01776     if ( !kmkernel->msgSender()->send( receipt ) ) // send or queue
01777       KMessageBox::error( this, i18n("Could not send MDN.") );
01778 }
01779 
01780 
01781 //-----------------------------------------------------------------------------
01782 void KMReaderWin::closeEvent(QCloseEvent *e)
01783 {
01784   QWidget::closeEvent(e);
01785   writeConfig();
01786 }
01787 
01788 
01789 bool foundSMIMEData( const QString aUrl,
01790                      QString& displayName,
01791                      QString& libName,
01792                      QString& keyId )
01793 {
01794   static QString showCertMan("showCertificate#");
01795   displayName = "";
01796   libName = "";
01797   keyId = "";
01798   int i1 = aUrl.find( showCertMan );
01799   if( -1 < i1 ) {
01800     i1 += showCertMan.length();
01801     int i2 = aUrl.find(" ### ", i1);
01802     if( i1 < i2 )
01803     {
01804       displayName = aUrl.mid( i1, i2-i1 );
01805       i1 = i2+5;
01806       i2 = aUrl.find(" ### ", i1);
01807       if( i1 < i2 )
01808       {
01809         libName = aUrl.mid( i1, i2-i1 );
01810         i2 += 5;
01811 
01812         keyId = aUrl.mid( i2 );
01813         /*
01814         int len = aUrl.length();
01815         if( len > i2+1 ) {
01816           keyId = aUrl.mid( i2, 2 );
01817           i2 += 2;
01818           while( len > i2+1 ) {
01819             keyId += ':';
01820             keyId += aUrl.mid( i2, 2 );
01821             i2 += 2;
01822           }
01823         }
01824         */
01825       }
01826     }
01827   }
01828   return !keyId.isEmpty();
01829 }
01830 
01831 
01832 //-----------------------------------------------------------------------------
01833 void KMReaderWin::slotUrlOn(const QString &aUrl)
01834 {
01835   if ( aUrl.stripWhiteSpace().isEmpty() ) {
01836     KPIM::BroadcastStatus::instance()->reset();
01837     return;
01838   }
01839 
01840   const KURL url(aUrl);
01841   mUrlClicked = url;
01842 
01843   const QString msg = URLHandlerManager::instance()->statusBarMessage( url, this );
01844 
01845   kdWarning( msg.isEmpty(), 5006 ) << "KMReaderWin::slotUrlOn(): Unhandled URL hover!" << endl;
01846   KPIM::BroadcastStatus::instance()->setTransientStatusMsg( msg );
01847 }
01848 
01849 
01850 //-----------------------------------------------------------------------------
01851 void KMReaderWin::slotUrlOpen(const KURL &aUrl, const KParts::URLArgs &)
01852 {
01853   mUrlClicked = aUrl;
01854 
01855   if ( URLHandlerManager::instance()->handleClick( aUrl, this ) )
01856     return;
01857 
01858   kdWarning( 5006 ) << "KMReaderWin::slotOpenUrl(): Unhandled URL click!" << endl;
01859   emit urlClicked( aUrl, Qt::LeftButton );
01860 }
01861 
01862 //-----------------------------------------------------------------------------
01863 void KMReaderWin::slotUrlPopup(const QString &aUrl, const QPoint& aPos)
01864 {
01865   const KURL url( aUrl );
01866   mUrlClicked = url;
01867 
01868   if ( URLHandlerManager::instance()->handleContextMenuRequest( url, aPos, this ) )
01869     return;
01870 
01871   if ( message() ) {
01872     kdWarning( 5006 ) << "KMReaderWin::slotUrlPopup(): Unhandled URL right-click!" << endl;
01873     emit popupMenu( *message(), url, aPos );
01874   }
01875 }
01876 
01877 void KMReaderWin::showAttachmentPopup( int id, const QString & name, const QPoint & p ) {
01878   mAtmCurrent = id;
01879   mAtmCurrentName = name;
01880   KPopupMenu menu;
01881   menu.insertItem(SmallIcon("fileopen"),i18n("Open"), 1);
01882   menu.insertItem(i18n("Open With..."), 2);
01883   menu.insertItem(i18n("to view something", "View"), 3);
01884   menu.insertItem(SmallIcon("filesaveas"),i18n("Save As..."), 4);
01885 #ifdef KLEO_CHIASMUS
01886   if ( name.endsWith( ".xia", false ) &&
01887        Kleo::CryptoBackendFactory::instance()->protocol( "Chiasmus" ) )
01888     menu.insertItem( i18n( "Decrypt With Chiasmus..." ), 6 );
01889 #endif
01890   menu.insertItem(i18n("Properties"), 5);
01891   connect(&menu, SIGNAL(activated(int)), this, SLOT(slotAtmLoadPart(int)));
01892   menu.exec( p ,0 );
01893 }
01894 
01895 //-----------------------------------------------------------------------------
01896 void KMReaderWin::setStyleDependantFrameWidth()
01897 {
01898   if ( !mBox )
01899     return;
01900   // set the width of the frame to a reasonable value for the current GUI style
01901   int frameWidth;
01902   if( style().isA("KeramikStyle") )
01903     frameWidth = style().pixelMetric( QStyle::PM_DefaultFrameWidth ) - 1;
01904   else
01905     frameWidth = style().pixelMetric( QStyle::PM_DefaultFrameWidth );
01906   if ( frameWidth < 0 )
01907     frameWidth = 0;
01908   if ( frameWidth != mBox->lineWidth() )
01909     mBox->setLineWidth( frameWidth );
01910 }
01911 
01912 //-----------------------------------------------------------------------------
01913 void KMReaderWin::styleChange( QStyle& oldStyle )
01914 {
01915   setStyleDependantFrameWidth();
01916   QWidget::styleChange( oldStyle );
01917 }
01918 
01919 //-----------------------------------------------------------------------------
01920 void KMReaderWin::slotAtmLoadPart( int choice )
01921 {
01922   mChoice = choice;
01923 
01924   partNode* node = mRootNode ? mRootNode->findId( mAtmCurrent ) : 0;
01925   if ( node && !node->msgPart().isComplete() )
01926   {
01927     // load the part
01928     mAtmUpdate = true;
01929     KMLoadPartsCommand *command = new KMLoadPartsCommand( node, message() );
01930     connect( command, SIGNAL( partsRetrieved() ),
01931         this, SLOT( slotAtmDistributeClick() ) );
01932     command->start();
01933   } else
01934     slotAtmDistributeClick();
01935 }
01936 
01937 //-----------------------------------------------------------------------------
01938 void KMReaderWin::slotAtmDistributeClick()
01939 {
01940   switch ( mChoice )
01941   {
01942     case 1:
01943       slotAtmOpen();
01944       break;
01945     case 2:
01946       slotAtmOpenWith();
01947       break;
01948     case 3:
01949       slotAtmView();
01950       break;
01951     case 4:
01952       slotAtmSave();
01953       break;
01954     case 5:
01955       slotAtmProperties();
01956       break;
01957 #ifdef KLEO_CHIASMUS
01958     case 6:
01959       slotAtmDecryptWithChiasmus();
01960       break;
01961 #endif
01962     default: kdWarning(5006) << "unknown menu item " << mChoice << endl;
01963   }
01964 }
01965 
01966 //-----------------------------------------------------------------------------
01967 void KMReaderWin::slotFind()
01968 {
01969   //dnaber:
01970   KAction *act = mViewer->actionCollection()->action("find");
01971   if( act )
01972     act->activate();
01973 }
01974 
01975 //-----------------------------------------------------------------------------
01976 void KMReaderWin::slotToggleFixedFont()
01977 {
01978   mUseFixedFont = !mUseFixedFont;
01979   update(true);
01980 }
01981 
01982 
01983 //-----------------------------------------------------------------------------
01984 void KMReaderWin::slotCopySelectedText()
01985 {
01986   kapp->clipboard()->setText( mViewer->selectedText() );
01987 }
01988 
01989 
01990 //-----------------------------------------------------------------------------
01991 void KMReaderWin::atmViewMsg(KMMessagePart* aMsgPart)
01992 {
01993   assert(aMsgPart!=0);
01994   partNode* node = mRootNode ? mRootNode->findId( mAtmCurrent ) : 0;
01995   KMMessage* msg = new KMMessage;
01996   msg->fromString(aMsgPart->bodyDecoded());
01997   assert(msg != 0);
01998   // some information that is needed for imap messages with LOD
01999   msg->setParent( message()->parent() );
02000   msg->setUID(message()->UID());
02001   msg->setComplete(true);
02002   msg->setReadyToShow(true);
02003   KMReaderMainWin *win = new KMReaderMainWin();
02004   win->showMsg( overrideEncoding(), msg );
02005   win->show();
02006 }
02007 
02008 
02009 void KMReaderWin::setMsgPart( partNode * node ) {
02010   htmlWriter()->reset();
02011   mColorBar->hide();
02012   htmlWriter()->begin( mCSSHelper->cssDefinitions( isFixedFont() ) );
02013   htmlWriter()->write( mCSSHelper->htmlHead( isFixedFont() ) );
02014   // end ###
02015   if ( node ) {
02016     ObjectTreeParser otp( this, 0, true );
02017     otp.parseObjectTree( node );
02018   }
02019   // ### this, too
02020   htmlWriter()->queue( "</body></html>" );
02021   htmlWriter()->flush();
02022 }
02023 
02024 //-----------------------------------------------------------------------------
02025 void KMReaderWin::setMsgPart( KMMessagePart* aMsgPart, bool aHTML,
02026                   const QString& aFileName, const QString& pname )
02027 {
02028   KCursorSaver busy(KBusyPtr::busy());
02029   if (qstricmp(aMsgPart->typeStr(), "message")==0) {
02030       // if called from compose win
02031       KMMessage* msg = new KMMessage;
02032       assert(aMsgPart!=0);
02033       msg->fromString(aMsgPart->bodyDecoded());
02034       mMainWindow->setCaption(msg->subject());
02035       setMsg(msg, true);
02036       setAutoDelete(true);
02037   } else if (qstricmp(aMsgPart->typeStr(), "text")==0) {
02038       if (qstricmp(aMsgPart->subtypeStr(), "x-vcard") == 0) {
02039         showVCard( aMsgPart );
02040     return;
02041       }
02042       htmlWriter()->begin( mCSSHelper->cssDefinitions( isFixedFont() ) );
02043       htmlWriter()->queue( mCSSHelper->htmlHead( isFixedFont() ) );
02044 
02045       if (aHTML && (qstricmp(aMsgPart->subtypeStr(), "html")==0)) { // HTML
02046     // ### this is broken. It doesn't stip off the HTML header and footer!
02047     htmlWriter()->queue( aMsgPart->bodyToUnicode( overrideCodec() ) );
02048     mColorBar->setHtmlMode();
02049       } else { // plain text
02050     const QCString str = aMsgPart->bodyDecoded();
02051     ObjectTreeParser otp( this );
02052     otp.writeBodyStr( str,
02053               overrideCodec() ? overrideCodec() : aMsgPart->codec(),
02054               message() ? message()->from() : QString::null );
02055       }
02056       htmlWriter()->queue("</body></html>");
02057       htmlWriter()->flush();
02058       mMainWindow->setCaption(i18n("View Attachment: %1").arg(pname));
02059   } else if (qstricmp(aMsgPart->typeStr(), "image")==0 ||
02060              (qstricmp(aMsgPart->typeStr(), "application")==0 &&
02061               qstricmp(aMsgPart->subtypeStr(), "postscript")==0))
02062   {
02063       if (aFileName.isEmpty()) return;  // prevent crash
02064       // Open the window with a size so the image fits in (if possible):
02065       QImageIO *iio = new QImageIO();
02066       iio->setFileName(aFileName);
02067       if( iio->read() ) {
02068           QImage img = iio->image();
02069           QRect desk = KGlobalSettings::desktopGeometry(mMainWindow);
02070           // determine a reasonable window size
02071           int width, height;
02072           if( img.width() < 50 )
02073               width = 70;
02074           else if( img.width()+20 < desk.width() )
02075               width = img.width()+20;
02076           else
02077               width = desk.width();
02078           if( img.height() < 50 )
02079               height = 70;
02080           else if( img.height()+20 < desk.height() )
02081               height = img.height()+20;
02082           else
02083               height = desk.height();
02084           mMainWindow->resize( width, height );
02085       }
02086       // Just write the img tag to HTML:
02087       htmlWriter()->begin( mCSSHelper->cssDefinitions( isFixedFont() ) );
02088       htmlWriter()->write( mCSSHelper->htmlHead( isFixedFont() ) );
02089       htmlWriter()->write( "<img src=\"file:" +
02090                KURL::encode_string( aFileName ) +
02091                "\" border=\"0\">\n"
02092                "</body></html>\n" );
02093       htmlWriter()->end();
02094       setCaption( i18n("View Attachment: %1").arg( pname ) );
02095       show();
02096   } else {
02097     htmlWriter()->begin( mCSSHelper->cssDefinitions( isFixedFont() ) );
02098     htmlWriter()->queue( mCSSHelper->htmlHead( isFixedFont() ) );
02099     htmlWriter()->queue( "<pre>" );
02100 
02101       QString str = aMsgPart->bodyDecoded();
02102       // A QString cannot handle binary data. So if it's shorter than the
02103       // attachment, we assume the attachment is binary:
02104       if( str.length() < (unsigned) aMsgPart->decodedSize() ) {
02105           str.prepend( i18n("[KMail: Attachment contains binary data. Trying to show first character.]",
02106                     "[KMail: Attachment contains binary data. Trying to show first %n characters.]",
02107           str.length()) + QChar('\n') );
02108       }
02109     htmlWriter()->queue( QStyleSheet::escape( str ) );
02110     htmlWriter()->queue( "</pre>" );
02111     htmlWriter()->queue("</body></html>");
02112     htmlWriter()->flush();
02113     mMainWindow->setCaption(i18n("View Attachment: %1").arg(pname));
02114   }
02115   // ---Sven's view text, html and image attachments in html widget end ---
02116 }
02117 
02118 
02119 //-----------------------------------------------------------------------------
02120 void KMReaderWin::slotAtmView()
02121 {
02122   partNode* node = mRootNode ? mRootNode->findId( mAtmCurrent ) : 0;
02123   if( node ) {
02124     KMMessagePart& msgPart = node->msgPart();
02125     QString pname = msgPart.fileName();
02126     if (pname.isEmpty()) pname=msgPart.name();
02127     if (pname.isEmpty()) pname=msgPart.contentDescription();
02128     if (pname.isEmpty()) pname="unnamed";
02129     // image Attachment is saved already
02130     if (qstricmp(msgPart.typeStr(), "message")==0) {
02131       atmViewMsg(&msgPart);
02132     } else if ((qstricmp(msgPart.typeStr(), "text")==0) &&
02133            (qstricmp(msgPart.subtypeStr(), "x-vcard")==0)) {
02134       setMsgPart( &msgPart, htmlMail(), mAtmCurrentName, pname );
02135     } else {
02136       KMReaderMainWin *win = new KMReaderMainWin(&msgPart, htmlMail(),
02137     mAtmCurrentName, pname, overrideEncoding() );
02138       win->show();
02139     }
02140   }
02141 }
02142 
02143 
02144 //-----------------------------------------------------------------------------
02145 void KMReaderWin::slotAtmOpen()
02146 {
02147   openAttachment( mAtmCurrent, mAtmCurrentName );
02148 }
02149 
02150 void KMReaderWin::openAttachment( int id, const QString & name ) {
02151   mAtmCurrentName = name;
02152   mAtmCurrent = id;
02153 
02154   QString str, pname, cmd, fileName;
02155 
02156   partNode* node = mRootNode ? mRootNode->findId( id ) : 0;
02157   if( !node ) {
02158     kdWarning(5006) << "KMReaderWin::openAttachment - could not find node " << id << endl;
02159     return;
02160   }
02161 
02162   KMMessagePart& msgPart = node->msgPart();
02163   if (qstricmp(msgPart.typeStr(), "message")==0)
02164   {
02165     atmViewMsg(&msgPart);
02166     return;
02167   }
02168 
02169   const QString contentTypeStr =
02170     ( msgPart.typeStr() + '/' + msgPart.subtypeStr() ).lower();
02171 
02172   if ( contentTypeStr == "text/x-vcard"  ) {
02173     showVCard( &msgPart );
02174     return;
02175   }
02176 
02177   // determine the MIME type of the attachment
02178   KMimeType::Ptr mimetype;
02179   // prefer the value of the Content-Type header
02180   mimetype = KMimeType::mimeType( contentTypeStr );
02181   if ( mimetype->name() == "application/octet-stream" ) {
02182     // consider the filename if Content-Type is application/octet-stream
02183     mimetype = KMimeType::findByPath( name, 0, true /* no disk access */ );
02184   }
02185   if ( ( mimetype->name() == "application/octet-stream" )
02186        && msgPart.isComplete() ) {
02187     // consider the attachment's contents if neither the Content-Type header
02188     // nor the filename give us a clue
02189     mimetype = KMimeType::findByFileContent( name );
02190   }
02191 
02192   KService::Ptr offer =
02193     KServiceTypeProfile::preferredService( mimetype->name(), "Application" );
02194 
02195   // remember for slotDoAtmOpen; FIXME, this is ugly
02196   mOffer = offer;
02197   QString open_text;
02198   QString filenameText = msgPart.fileName();
02199   if ( filenameText.isEmpty() )
02200     filenameText = msgPart.name();
02201   if ( offer ) {
02202     open_text = i18n("&Open with '%1'").arg( offer->name() );
02203   } else {
02204     open_text = i18n("&Open With...");
02205   }
02206   const QString text = i18n("Open attachment '%1'?\n"
02207                             "Note that opening an attachment may compromise "
02208                             "your system's security.")
02209                        .arg( filenameText );
02210   const int choice = KMessageBox::questionYesNoCancel( this, text,
02211       i18n("Open Attachment?"), KStdGuiItem::saveAs(), open_text,
02212       QString::fromLatin1("askSave") + mimetype->name() ); // dontAskAgainName
02213 
02214   if( choice == KMessageBox::Yes ) {        // Save
02215     slotAtmLoadPart( 4 );
02216   }
02217   else if( choice == KMessageBox::No ) {    // Open
02218     // this load-part is duplicated from slotAtmLoadPart but is needed here
02219     // to first display the choice before the attachment is actually downloaded
02220     if ( !msgPart.isComplete() ) {
02221       // load the part
02222       mAtmUpdate = true;
02223       KMLoadPartsCommand *command = new KMLoadPartsCommand( node, message() );
02224       connect( command, SIGNAL( partsRetrieved() ),
02225           this, SLOT( slotDoAtmOpen() ) );
02226       command->start();
02227     } else {
02228       slotDoAtmOpen();
02229     }
02230   } else {                  // Cancel
02231     kdDebug(5006) << "Canceled opening attachment" << endl;
02232   }
02233 }
02234 
02235 //-----------------------------------------------------------------------------
02236 void KMReaderWin::slotDoAtmOpen()
02237 {
02238   if ( !mOffer ) {
02239     slotAtmOpenWith();
02240     return;
02241   }
02242 
02243   KURL url;
02244   url.setPath( mAtmCurrentName );
02245   KURL::List lst;
02246   lst.append( url );
02247   KRun::run( *mOffer, lst );
02248 }
02249 
02250 //-----------------------------------------------------------------------------
02251 void KMReaderWin::slotAtmOpenWith()
02252 {
02253   // It makes sense to have an extra "Open with..." entry in the menu
02254   // so the user can change filetype associations.
02255 
02256     KURL::List lst;
02257     KURL url;
02258     url.setPath(mAtmCurrentName);
02259     lst.append(url);
02260     KRun::displayOpenWithDialog(lst);
02261 }
02262 
02263 
02264 //-----------------------------------------------------------------------------
02265 void KMReaderWin::slotAtmSave()
02266 {
02267   if ( !mRootNode )
02268     return;
02269 
02270   partNode * node = mRootNode->findId( mAtmCurrent );
02271   if ( !node ) {
02272     kdWarning(5006) << "KMReaderWin::slotAtmSave - could not find node " << mAtmCurrent << endl;
02273     return;
02274   }
02275 
02276   QPtrList<partNode> parts;
02277   parts.append( node );
02278   // save, do not leave encoded
02279   KMSaveAttachmentsCommand *command =
02280     new KMSaveAttachmentsCommand( this, parts, message(), false );
02281   command->start();
02282 }
02283 
02284 
02285 //-----------------------------------------------------------------------------
02286 void KMReaderWin::slotAtmProperties()
02287 {
02288     KMMsgPartDialogCompat dlg(0,TRUE);
02289 
02290     partNode* node = mRootNode ? mRootNode->findId( mAtmCurrent ) : 0;
02291     if( node ) {
02292         KMMessagePart& msgPart = node->msgPart();
02293 
02294         dlg.setMsgPart(&msgPart);
02295         dlg.exec();
02296     }
02297 }
02298 
02299 
02300 //-----------------------------------------------------------------------------
02301 void KMReaderWin::slotScrollUp()
02302 {
02303   static_cast<QScrollView *>(mViewer->widget())->scrollBy(0, -10);
02304 }
02305 
02306 
02307 //-----------------------------------------------------------------------------
02308 void KMReaderWin::slotScrollDown()
02309 {
02310   static_cast<QScrollView *>(mViewer->widget())->scrollBy(0, 10);
02311 }
02312 
02313 bool KMReaderWin::atBottom() const
02314 {
02315     const QScrollView *view = static_cast<const QScrollView *>(mViewer->widget());
02316     return view->contentsY() + view->visibleHeight() >= view->contentsHeight();
02317 }
02318 
02319 //-----------------------------------------------------------------------------
02320 void KMReaderWin::slotJumpDown()
02321 {
02322     QScrollView *view = static_cast<QScrollView *>(mViewer->widget());
02323     int offs = (view->clipper()->height() < 30) ? view->clipper()->height() : 30;
02324     view->scrollBy( 0, view->clipper()->height() - offs );
02325 }
02326 
02327 //-----------------------------------------------------------------------------
02328 void KMReaderWin::slotScrollPrior()
02329 {
02330   static_cast<QScrollView *>(mViewer->widget())->scrollBy(0, -(int)(height()*0.8));
02331 }
02332 
02333 
02334 //-----------------------------------------------------------------------------
02335 void KMReaderWin::slotScrollNext()
02336 {
02337   static_cast<QScrollView *>(mViewer->widget())->scrollBy(0, (int)(height()*0.8));
02338 }
02339 
02340 //-----------------------------------------------------------------------------
02341 void KMReaderWin::slotDocumentChanged()
02342 {
02343 
02344 }
02345 
02346 
02347 //-----------------------------------------------------------------------------
02348 void KMReaderWin::slotTextSelected(bool)
02349 {
02350   QString temp = mViewer->selectedText();
02351   kapp->clipboard()->setText(temp);
02352 }
02353 
02354 //-----------------------------------------------------------------------------
02355 void KMReaderWin::selectAll()
02356 {
02357   mViewer->selectAll();
02358 }
02359 
02360 //-----------------------------------------------------------------------------
02361 QString KMReaderWin::copyText()
02362 {
02363   QString temp = mViewer->selectedText();
02364   return temp;
02365 }
02366 
02367 
02368 //-----------------------------------------------------------------------------
02369 void KMReaderWin::slotDocumentDone()
02370 {
02371   // mSbVert->setValue(0);
02372 }
02373 
02374 
02375 //-----------------------------------------------------------------------------
02376 void KMReaderWin::setHtmlOverride(bool override)
02377 {
02378   mHtmlOverride = override;
02379   if (message())
02380       message()->setDecodeHTML(htmlMail());
02381 }
02382 
02383 
02384 //-----------------------------------------------------------------------------
02385 bool KMReaderWin::htmlMail()
02386 {
02387   return ((mHtmlMail && !mHtmlOverride) || (!mHtmlMail && mHtmlOverride));
02388 }
02389 
02390 
02391 //-----------------------------------------------------------------------------
02392 void KMReaderWin::update( bool force )
02393 {
02394   KMMessage* msg = message();
02395   if ( msg )
02396     setMsg( msg, force );
02397 }
02398 
02399 
02400 //-----------------------------------------------------------------------------
02401 KMMessage* KMReaderWin::message( KMFolder** aFolder ) const
02402 {
02403   KMFolder*  tmpFolder;
02404   KMFolder*& folder = aFolder ? *aFolder : tmpFolder;
02405   folder = 0;
02406   if (mMessage)
02407       return mMessage;
02408   if (mLastSerNum) {
02409     KMMessage *message = 0;
02410     int index;
02411     kmkernel->msgDict()->getLocation( mLastSerNum, &folder, &index );
02412     if (folder )
02413       message = folder->getMsg( index );
02414     if (!message)
02415       kdWarning(5006) << "Attempt to reference invalid serial number " << mLastSerNum << "\n" << endl;
02416     return message;
02417   }
02418   return 0;
02419 }
02420 
02421 
02422 
02423 //-----------------------------------------------------------------------------
02424 void KMReaderWin::slotUrlClicked()
02425 {
02426   KMMainWidget *mainWidget = dynamic_cast<KMMainWidget*>(mMainWindow);
02427   uint identity = 0;
02428   if ( message() && message()->parent() ) {
02429     identity = message()->parent()->identity();
02430   }
02431 
02432   KMCommand *command = new KMUrlClickedCommand( mUrlClicked, identity, this,
02433                         false, mainWidget );
02434   command->start();
02435 }
02436 
02437 //-----------------------------------------------------------------------------
02438 void KMReaderWin::slotMailtoCompose()
02439 {
02440   KMCommand *command = new KMMailtoComposeCommand( mUrlClicked, message() );
02441   command->start();
02442 }
02443 
02444 //-----------------------------------------------------------------------------
02445 void KMReaderWin::slotMailtoForward()
02446 {
02447   KMCommand *command = new KMMailtoForwardCommand( mMainWindow, mUrlClicked,
02448                            message() );
02449   command->start();
02450 }
02451 
02452 //-----------------------------------------------------------------------------
02453 void KMReaderWin::slotMailtoAddAddrBook()
02454 {
02455   KMCommand *command = new KMMailtoAddAddrBookCommand( mUrlClicked,
02456                                mMainWindow);
02457   command->start();
02458 }
02459 
02460 //-----------------------------------------------------------------------------
02461 void KMReaderWin::slotMailtoOpenAddrBook()
02462 {
02463   KMCommand *command = new KMMailtoOpenAddrBookCommand( mUrlClicked,
02464                             mMainWindow );
02465   command->start();
02466 }
02467 
02468 //-----------------------------------------------------------------------------
02469 void KMReaderWin::slotUrlCopy()
02470 {
02471   // we don't necessarily need a mainWidget for KMUrlCopyCommand so
02472   // it doesn't matter if the dynamic_cast fails.
02473   KMCommand *command =
02474     new KMUrlCopyCommand( mUrlClicked,
02475                           dynamic_cast<KMMainWidget*>( mMainWindow ) );
02476   command->start();
02477 }
02478 
02479 //-----------------------------------------------------------------------------
02480 void KMReaderWin::slotUrlOpen( const KURL &url )
02481 {
02482   if ( !url.isEmpty() )
02483     mUrlClicked = url;
02484   KMCommand *command = new KMUrlOpenCommand( mUrlClicked, this );
02485   command->start();
02486 }
02487 
02488 //-----------------------------------------------------------------------------
02489 void KMReaderWin::slotAddBookmarks()
02490 {
02491     KMCommand *command = new KMAddBookmarksCommand( mUrlClicked, this );
02492     command->start();
02493 }
02494 
02495 //-----------------------------------------------------------------------------
02496 void KMReaderWin::slotUrlSave()
02497 {
02498   KMCommand *command = new KMUrlSaveCommand( mUrlClicked, mMainWindow );
02499   command->start();
02500 }
02501 
02502 //-----------------------------------------------------------------------------
02503 void KMReaderWin::slotMailtoReply()
02504 {
02505   KMCommand *command = new KMMailtoReplyCommand( mMainWindow, mUrlClicked,
02506     message(), copyText() );
02507   command->start();
02508 }
02509 
02510 //-----------------------------------------------------------------------------
02511 void KMReaderWin::slotShowMsgSrc()
02512 {
02513   KMMessage *msg = message();
02514   if ( !msg )
02515     return;
02516   KMShowMsgSrcCommand *command = new KMShowMsgSrcCommand( msg, isFixedFont() );
02517   command->start();
02518 }
02519 
02520 //-----------------------------------------------------------------------------
02521 partNode * KMReaderWin::partNodeFromUrl( const KURL & url ) {
02522   return mRootNode ? mRootNode->findId( msgPartFromUrl( url ) ) : 0 ;
02523 }
02524 
02525 partNode * KMReaderWin::partNodeForId( int id ) {
02526   return mRootNode ? mRootNode->findId( id ) : 0 ;
02527 }
02528 
02529 //-----------------------------------------------------------------------------
02530 void KMReaderWin::slotSaveAttachments()
02531 {
02532   mAtmUpdate = true;
02533   KMSaveAttachmentsCommand *saveCommand = new KMSaveAttachmentsCommand( mMainWindow,
02534                                                                         message() );
02535   saveCommand->start();
02536 }
02537 
02538 //-----------------------------------------------------------------------------
02539 void KMReaderWin::slotSaveMsg()
02540 {
02541   KMSaveMsgCommand *saveCommand = new KMSaveMsgCommand( mMainWindow, message() );
02542 
02543   if (saveCommand->url().isEmpty())
02544     delete saveCommand;
02545   else
02546     saveCommand->start();
02547 }
02548 //-----------------------------------------------------------------------------
02549 void KMReaderWin::slotIMChat()
02550 {
02551   KMCommand *command = new KMIMChatCommand( mUrlClicked, message() );
02552   command->start();
02553 }
02554 
02555 void KMReaderWin::slotAtmDecryptWithChiasmus() {
02556 #ifdef KLEO_CHIASMUS
02557   const partNode * node = partNodeForId( mAtmCurrent );
02558   Q_ASSERT( node );
02559   if ( !node )
02560     return;
02561 
02562   // FIXME: better detection of mimetype??
02563   if ( !mAtmCurrentName.endsWith( ".xia", false ) )
02564     return;
02565 
02566   const Kleo::CryptoBackend::Protocol * chiasmus =
02567     Kleo::CryptoBackendFactory::instance()->protocol( "Chiasmus" );
02568   Q_ASSERT( chiasmus );
02569   if ( !chiasmus )
02570     return;
02571 
02572   const std::auto_ptr<Kleo::SpecialJob> listjob( chiasmus->specialJob( "x-obtain-keys", QMap<QString,QVariant>() ) );
02573   if ( !listjob.get() ) {
02574     const QString msg = i18n( "Chiasmus backend does not offer the "
02575                               "\"x-obtain-keys\" function. Please report this bug." );
02576     KMessageBox::error( this, msg, i18n( "Chiasmus Backend Error" ) );
02577     return;
02578   }
02579 
02580   if ( listjob->exec() ) {
02581     listjob->showErrorDialog( this, i18n( "Chiasmus Backend Error" ) );
02582     return;
02583   }
02584 
02585   const QVariant result = listjob->property( "result" );
02586   if ( result.type() != QVariant::StringList ) {
02587     const QString msg = i18n( "Unexpected return value from Chiasmus backend: "
02588                               "The \"x-obtain-keys\" function did not return a "
02589                               "string list. Please report this bug." );
02590     KMessageBox::error( this, msg, i18n( "Chiasmus Backend Error" ) );
02591     return;
02592   }
02593 
02594   const QStringList keys = result.toStringList();
02595   if ( keys.empty() ) {
02596     const QString msg = i18n( "No keys have been found. Please check that a "
02597                               "valid key path has been set in the Chiasmus "
02598                               "configuration." );
02599     KMessageBox::error( this, msg, i18n( "Chiasmus Backend Error" ) );
02600     return;
02601   }
02602 
02603   ChiasmusKeySelector selectorDlg( this, i18n( "Chiasmus Decryption Key Selection" ),
02604                                    keys, mChiasmusKey, mChiasmusOptions );
02605   if ( selectorDlg.exec() != QDialog::Accepted )
02606     return;
02607 
02608   mChiasmusOptions = selectorDlg.options();
02609   mChiasmusKey = selectorDlg.key();
02610   assert( !mChiasmusKey.isEmpty() );
02611 
02612   Kleo::SpecialJob * job = chiasmus->specialJob( "x-decrypt", QMap<QString,QVariant>() );
02613   if ( !job ) {
02614     const QString msg = i18n( "Chiasmus backend does not offer the "
02615                               "\"x-decrypt\" function. Please report this bug." );
02616     KMessageBox::error( this, msg, i18n( "Chiasmus Backend Error" ) );
02617     return;
02618   }
02619 
02620   const QByteArray input = node->msgPart().bodyDecodedBinary();
02621 
02622   if ( !job->setProperty( "key", mChiasmusKey ) ||
02623        !job->setProperty( "options", mChiasmusOptions ) ||
02624        !job->setProperty( "input", input ) ) {
02625     const QString msg = i18n( "The \"x-decrypt\" function does not accept "
02626                               "the expected parameters. Please report this bug." );
02627     KMessageBox::error( this, msg, i18n( "Chiasmus Backend Error" ) );
02628     return;
02629   }
02630 
02631   if ( job->start() ) {
02632     job->showErrorDialog( this, i18n( "Chiasmus Decryption Error" ) );
02633     return;
02634   }
02635 
02636   mJob = job;
02637   connect( job, SIGNAL(result(const GpgME::Error&,const QVariant&)),
02638            this, SLOT(slotAtmDecryptWithChiasmusResult(const GpgME::Error&,const QVariant&)) );
02639 #endif
02640 }
02641 
02642 #ifdef KLEO_CHIASMUS
02643 // return true if we should proceed, false if we should abort
02644 static bool checkOverwrite( const KURL& url, bool& overwrite, QWidget* w )
02645 {
02646   if ( KIO::NetAccess::exists( url, false /*dest*/, w ) ) {
02647     if ( KMessageBox::Cancel ==
02648          KMessageBox::warningContinueCancel(
02649                                             w,
02650                                             i18n( "A file named \"%1\" already exists. "
02651                                                   "Are you sure you want to overwrite it?" ).arg( url.prettyURL() ),
02652                                             i18n( "Overwrite File?" ),
02653                                             i18n( "&Overwrite" ) ) )
02654       return false;
02655     overwrite = true;
02656   }
02657   return true;
02658 }
02659 
02660 static const QString chomp( const QString & base, const QString & suffix, bool cs ) {
02661   return base.endsWith( suffix, cs ) ? base.left( base.length() - suffix.length() ) : base ;
02662 }
02663 #endif
02664 
02665 void KMReaderWin::slotAtmDecryptWithChiasmusResult( const GpgME::Error & err, const QVariant & result ) {
02666 #ifdef KLEO_CHIASMUS
02667   if ( !mJob )
02668     return;
02669   Q_ASSERT( mJob == sender() );
02670   if ( mJob != sender() )
02671     return;
02672   Kleo::Job * job = mJob;
02673   mJob = 0;
02674   if ( err.isCanceled() )
02675     return;
02676   if ( err ) {
02677     job->showErrorDialog( this, i18n( "Chiasmus Decryption Error" ) );
02678     return;
02679   }
02680 
02681   if ( result.type() != QVariant::ByteArray ) {
02682     const QString msg = i18n( "Unexpected return value from Chiasmus backend: "
02683                               "The \"x-decrypt\" function did not return a "
02684                               "byte array. Please report this bug." );
02685     KMessageBox::error( this, msg, i18n( "Chiasmus Backend Error" ) );
02686     return;
02687   }
02688 
02689   const KURL url = KFileDialog::getSaveURL( chomp( mUrlClicked.fileName(), ".xia", false ), QString::null, this );
02690   if ( url.isEmpty() )
02691     return;
02692 
02693   bool overwrite = false;
02694   if ( !checkOverwrite( url, overwrite, this ) )
02695     return;
02696 
02697   KIO::Job * uploadJob = KIOext::put( result.toByteArray(), url, -1, overwrite, false /*resume*/ );
02698   uploadJob->setWindow( this );
02699   connect( uploadJob, SIGNAL(result(KIO::Job*)),
02700            this, SLOT(slotAtmDecryptWithChiasmusUploadResult(KIO::Job*)) );
02701 #else
02702   Q_UNUSED( err );
02703   Q_UNUSED( result );
02704 #endif
02705 }
02706 
02707 void KMReaderWin::slotAtmDecryptWithChiasmusUploadResult( KIO::Job * job ) {
02708   if ( job->error() )
02709     job->showErrorDialog();
02710 }
02711 
02712 #if 1
02713 namespace {
02714   class SelectionSaver {
02715     KHTMLPart * const part;
02716     DOM::Range oldSelection;
02717   public:
02718     SelectionSaver( KHTMLPart * p )
02719       : part( p ),
02720     oldSelection( p->hasSelection() ? p->selection() : p->htmlDocument().createRange() ) {}
02721 
02722     ~SelectionSaver() {
02723       part->setSelection( oldSelection );
02724     }
02725   };
02726 }
02727 
02728 static QString selectedTextOrAll( KHTMLPart * part ) {
02729   SelectionSaver saver( part );
02730   part->selectAll();
02731   return part->selectedText();
02732 }
02733 #endif
02734 
02735 #if 0
02736 static QString bodyElementInnerText( KHTMLPart * part ) {
02737   const DOM::HTMLDocument doc = part->htmlDocument();
02738   if ( doc.isNull() )
02739     return QString::null;
02740   const DOM::HTMLElement body = doc.body();
02741   if ( body.isNull() )
02742     return QString::null;
02743   return body.innerText().string();
02744 }
02745 
02746 static QString selectedTextOrAll( KHTMLPart * part ) {
02747   if ( part->hasSelection() )
02748     return part->selectedText();
02749   else
02750     return bodyElementInnerText( part );
02751 }
02752 #endif
02753 
02754 void KMReaderWin::slotSaveTextAs() {
02755   ( new KMail::SaveTextAsCommand( selectedTextOrAll( mViewer ), this ) )->start();
02756 }
02757 
02758 #include "kmreaderwin.moc"
KDE Logo
This file is part of the documentation for kmail Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Dec 21 14:25:02 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003