kmail

kmreadermainwin.cpp

00001 /*
00002     This file is part of KMail, the KDE mail client.
00003     Copyright (c) 2002 Don Sanders <sanders@kde.org>
00004 
00005     KMail is free software; you can redistribute it and/or modify it
00006     under the terms of the GNU General Public License, version 2, as
00007     published by the Free Software Foundation.
00008 
00009     KMail is distributed in the hope that it will be useful, but
00010     WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program; if not, write to the Free Software
00016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00017 */
00018 //
00019 // A toplevel KMainWindow derived class for displaying
00020 // single messages or single message parts.
00021 //
00022 // Could be extended to include support for normal main window
00023 // widgets like a toolbar.
00024 
00025 #ifdef HAVE_CONFIG_H
00026 #include <config.h>
00027 #endif
00028 
00029 #include <qaccel.h>
00030 #include <kapplication.h>
00031 #include <kedittoolbar.h>
00032 #include <klocale.h>
00033 #include <kstdaccel.h>
00034 #include <kwin.h>
00035 #include <kaction.h>
00036 #include <kiconloader.h>
00037 #include <kdebug.h>
00038 #include "kmcommands.h"
00039 #include "kmenubar.h"
00040 #include "kpopupmenu.h"
00041 #include "kmreaderwin.h"
00042 #include "kmfolder.h"
00043 #include "kmmainwidget.h"
00044 #include "kmfoldertree.h"
00045 #include "kmmsgdict.h"
00046 #include "csshelper.h"
00047 #include "messageactions.h"
00048 
00049 #include "globalsettings.h"
00050 
00051 #include "kmreadermainwin.h"
00052 
00053 KMReaderMainWin::KMReaderMainWin( bool htmlOverride, bool htmlLoadExtOverride,
00054                                   char *name )
00055   : KMail::SecondaryWindow( name ? name : "readerwindow#" ),
00056     mMsg( 0 )
00057 {
00058   mReaderWin = new KMReaderWin( this, this, actionCollection() );
00059   //mReaderWin->setShowCompleteMessage( true );
00060   mReaderWin->setAutoDelete( true );
00061   mReaderWin->setHtmlOverride( htmlOverride );
00062   mReaderWin->setHtmlLoadExtOverride( htmlLoadExtOverride );
00063   mReaderWin->setDecryptMessageOverwrite( true );
00064   mReaderWin->setShowSignatureDetails( false );
00065   initKMReaderMainWin();
00066 }
00067 
00068 
00069 //-----------------------------------------------------------------------------
00070 KMReaderMainWin::KMReaderMainWin( char *name )
00071   : KMail::SecondaryWindow( name ? name : "readerwindow#" ),
00072     mMsg( 0 )
00073 {
00074   mReaderWin = new KMReaderWin( this, this, actionCollection() );
00075   mReaderWin->setAutoDelete( true );
00076   initKMReaderMainWin();
00077 }
00078 
00079 
00080 //-----------------------------------------------------------------------------
00081 KMReaderMainWin::KMReaderMainWin(KMMessagePart* aMsgPart,
00082     bool aHTML, const QString& aFileName, const QString& pname,
00083     const QString & encoding, char *name )
00084   : KMail::SecondaryWindow( name ? name : "readerwindow#" ),
00085     mMsg( 0 )
00086 {
00087   mReaderWin = new KMReaderWin( this, this, actionCollection() );
00088   mReaderWin->setOverrideEncoding( encoding );
00089   mReaderWin->setMsgPart( aMsgPart, aHTML, aFileName, pname );
00090   initKMReaderMainWin();
00091 }
00092 
00093 
00094 //-----------------------------------------------------------------------------
00095 void KMReaderMainWin::initKMReaderMainWin() {
00096   setCentralWidget( mReaderWin );
00097   setupAccel();
00098   setupGUI( Keys | StatusBar | Create, "kmreadermainwin.rc" );
00099   setupForwardingActionsList();
00100   applyMainWindowSettings( KMKernel::config(), "Separate Reader Window" );
00101   if ( ! mReaderWin->message() ) {
00102     menuBar()->hide();
00103     toolBar( "mainToolBar" )->hide();
00104   }
00105 
00106   connect( kmkernel, SIGNAL( configChanged() ),
00107            this, SLOT( slotConfigChanged() ) );
00108 }
00109 
00110 void KMReaderMainWin::setupForwardingActionsList()
00111 {
00112   QPtrList<KAction> mForwardActionList;
00113   if ( GlobalSettings::self()->forwardingInlineByDefault() ) {
00114       unplugActionList( "forward_action_list" );
00115       mForwardActionList.append( mForwardInlineAction );
00116       mForwardActionList.append( mForwardAttachedAction );
00117       mForwardActionList.append( mForwardDigestAction );
00118       mForwardActionList.append( mRedirectAction );
00119       plugActionList( "forward_action_list", mForwardActionList );
00120   } else {
00121       unplugActionList( "forward_action_list" );
00122       mForwardActionList.append( mForwardAttachedAction );
00123       mForwardActionList.append( mForwardInlineAction );
00124       mForwardActionList.append( mForwardDigestAction );
00125       mForwardActionList.append( mRedirectAction );
00126       plugActionList( "forward_action_list", mForwardActionList );
00127   }
00128 }
00129 
00130 //-----------------------------------------------------------------------------
00131 KMReaderMainWin::~KMReaderMainWin()
00132 {
00133   saveMainWindowSettings( KMKernel::config(), "Separate Reader Window" );
00134 }
00135 
00136 //-----------------------------------------------------------------------------
00137 void KMReaderMainWin::setUseFixedFont( bool useFixedFont )
00138 {
00139   mReaderWin->setUseFixedFont( useFixedFont );
00140 }
00141 
00142 //-----------------------------------------------------------------------------
00143 void KMReaderMainWin::showMsg( const QString & encoding, KMMessage *msg )
00144 {
00145   mReaderWin->setOverrideEncoding( encoding );
00146   mReaderWin->setMsg( msg, true );
00147   mReaderWin->slotTouchMessage();
00148   setCaption( msg->subject() );
00149   mMsg = msg;
00150   mMsgActions->setCurrentMessage( msg );
00151   menuBar()->show();
00152   toolBar( "mainToolBar" )->show();
00153 
00154   connect ( msg->parent(), SIGNAL( destroyed( QObject* ) ), this, SLOT( slotFolderRemoved( QObject* ) ) );
00155 
00156 }
00157 
00158 void KMReaderMainWin::slotFolderRemoved( QObject* folderPtr )
00159 {
00160   assert(mMsg);
00161   assert(folderPtr == mMsg->parent());
00162   if( mMsg && folderPtr == mMsg->parent() )
00163     mMsg->setParent( 0 );
00164 }
00165 
00166 //-----------------------------------------------------------------------------
00167 void KMReaderMainWin::slotTrashMsg()
00168 {
00169   if ( !mMsg )
00170     return;
00171   // find the real msg by its sernum
00172   KMFolder* parent;
00173   int index;
00174   KMMsgDict::instance()->getLocation( mMsg->getMsgSerNum(), &parent, &index );
00175   if ( parent && !parent->isTrash() ) {
00176     // open the folder (ref counted)
00177     parent->open("trashmsg");
00178     KMMessage *msg = parent->getMsg( index );
00179     if (msg) {
00180       KMDeleteMsgCommand *command = new KMDeleteMsgCommand( parent, msg );
00181       command->start();
00182     }
00183     parent->close("trashmsg");
00184   }
00185   close();
00186 }
00187 
00188 //-----------------------------------------------------------------------------
00189 void KMReaderMainWin::slotFind()
00190 {
00191   mReaderWin->slotFind();
00192 }
00193 
00194 void KMReaderMainWin::slotFindNext()
00195 {
00196   mReaderWin->slotFindNext();
00197 }
00198 
00199 //-----------------------------------------------------------------------------
00200 void KMReaderMainWin::slotCopy()
00201 {
00202   mReaderWin->slotCopySelectedText();
00203 }
00204 
00205 //-----------------------------------------------------------------------------
00206 void KMReaderMainWin::slotMarkAll()
00207 {
00208   mReaderWin->selectAll();
00209 }
00210 
00211 //-----------------------------------------------------------------------------
00212 void KMReaderMainWin::slotPrintMsg()
00213 {
00214   KMPrintCommand *command = new KMPrintCommand( this, mReaderWin->message(),
00215       mReaderWin->htmlOverride(), mReaderWin->htmlLoadExtOverride(),
00216       mReaderWin->isFixedFont(), mReaderWin->overrideEncoding() );
00217   command->setOverrideFont( mReaderWin->cssHelper()->bodyFont( mReaderWin->isFixedFont(), true /*printing*/ ) );
00218   command->start();
00219 }
00220 
00221 //-----------------------------------------------------------------------------
00222 void KMReaderMainWin::slotForwardInlineMsg()
00223 {
00224    KMCommand *command = 0;
00225    if ( mReaderWin->message() && mReaderWin->message()->parent() ) {
00226     command = new KMForwardInlineCommand( this, mReaderWin->message(),
00227         mReaderWin->message()->parent()->identity() );
00228    } else {
00229     command = new KMForwardInlineCommand( this, mReaderWin->message() );
00230    }
00231    command->start();
00232 }
00233 
00234 //-----------------------------------------------------------------------------
00235 void KMReaderMainWin::slotForwardAttachedMsg()
00236 {
00237    KMCommand *command = 0;
00238    if ( mReaderWin->message() && mReaderWin->message()->parent() ) {
00239      command = new KMForwardAttachedCommand( this, mReaderWin->message(),
00240         mReaderWin->message()->parent()->identity() );
00241    } else {
00242      command = new KMForwardAttachedCommand( this, mReaderWin->message() );
00243    }
00244    command->start();
00245 }
00246 
00247 //-----------------------------------------------------------------------------
00248 void KMReaderMainWin::slotForwardDigestMsg()
00249 {
00250    KMCommand *command = 0;
00251    if ( mReaderWin->message() && mReaderWin->message()->parent() ) {
00252      command = new KMForwardDigestCommand( this, mReaderWin->message(),
00253         mReaderWin->message()->parent()->identity() );
00254    } else {
00255      command = new KMForwardDigestCommand( this, mReaderWin->message() );
00256    }
00257    command->start();
00258 }
00259 
00260 //-----------------------------------------------------------------------------
00261 void KMReaderMainWin::slotRedirectMsg()
00262 {
00263   KMCommand *command = new KMRedirectCommand( this, mReaderWin->message() );
00264   command->start();
00265 }
00266 
00267 //-----------------------------------------------------------------------------
00268 void KMReaderMainWin::slotShowMsgSrc()
00269 {
00270   KMMessage *msg = mReaderWin->message();
00271   if ( !msg )
00272     return;
00273   KMCommand *command = new KMShowMsgSrcCommand( this, msg,
00274                                                 mReaderWin->isFixedFont() );
00275   command->start();
00276 }
00277 
00278 //-----------------------------------------------------------------------------
00279 void KMReaderMainWin::setupForwardActions()
00280 {
00281   disconnect( mForwardActionMenu, SIGNAL( activated() ), 0, 0 );
00282   mForwardActionMenu->remove( mForwardInlineAction );
00283   mForwardActionMenu->remove( mForwardAttachedAction );
00284 
00285   if ( GlobalSettings::self()->forwardingInlineByDefault() ) {
00286     mForwardActionMenu->insert( mForwardInlineAction, 0 );
00287     mForwardActionMenu->insert( mForwardAttachedAction, 1 );
00288     mForwardInlineAction->setShortcut( Key_F );
00289     mForwardAttachedAction->setShortcut( SHIFT+Key_F );
00290     connect( mForwardActionMenu, SIGNAL(activated()), this,
00291             SLOT(slotForwardInlineMsg()) );
00292 
00293   } else {
00294     mForwardActionMenu->insert( mForwardAttachedAction, 0 );
00295     mForwardActionMenu->insert( mForwardInlineAction, 1 );
00296     mForwardInlineAction->setShortcut( SHIFT+Key_F );
00297     mForwardAttachedAction->setShortcut( Key_F );
00298     connect( mForwardActionMenu, SIGNAL(activated()), this,
00299             SLOT(slotForwardAttachedMsg()) );
00300   }
00301 }
00302 
00303 //-----------------------------------------------------------------------------
00304 void KMReaderMainWin::slotConfigChanged()
00305 {
00306   //readConfig();
00307   setupForwardActions();
00308   setupForwardingActionsList();
00309 }
00310 
00311 void KMReaderMainWin::setupAccel()
00312 {
00313   if ( kmkernel->xmlGuiInstance() )
00314     setInstance( kmkernel->xmlGuiInstance() );
00315 
00316   mMsgActions = new KMail::MessageActions( actionCollection(), this );
00317   mMsgActions->setMessageView( mReaderWin );
00318   //----- File Menu
00319   //mOpenAction = KStdAction::open( this, SLOT( slotOpenMsg() ),
00320   //                                actionCollection() );
00321 
00322   //mSaveAsAction = new KAction( i18n("Save &As..."), "filesave",
00323   //                             KStdAccel::shortcut( KStdAccel::Save ),
00324   //                             this, SLOT( slotSaveMsg() ),
00325   //                             actionCollection(), "file_save_as" );
00326 
00327   mSaveAsAction = KStdAction::saveAs( mReaderWin, SLOT( slotSaveMsg() ),
00328                       actionCollection() );
00329   mSaveAsAction->setShortcut( KStdAccel::shortcut( KStdAccel::Save ) );
00330   mPrintAction = KStdAction::print( this, SLOT( slotPrintMsg() ),
00331                                     actionCollection() );
00332 
00333   KAction *closeAction = KStdAction::close( this, SLOT( close() ), actionCollection() );
00334   KShortcut closeShortcut = closeAction->shortcut();
00335   closeShortcut.append( KKey(Key_Escape));
00336   closeAction->setShortcut(closeShortcut);
00337 
00338   //----- Edit Menu
00339   KStdAction::copy( this, SLOT( slotCopy() ), actionCollection() );
00340   KStdAction::selectAll( this, SLOT( slotMarkAll() ), actionCollection() );
00341   KStdAction::find( this, SLOT(slotFind()), actionCollection() );
00342   KStdAction::findNext( this, SLOT( slotFindNext() ), actionCollection() );
00343   mTrashAction = new KAction( KGuiItem( i18n( "&Move to Trash" ), "edittrash",
00344                               i18n( "Move message to trashcan" ) ),
00345                               Key_Delete, this, SLOT( slotTrashMsg() ),
00346                               actionCollection(), "move_to_trash" );
00347 
00348   //----- View Menu
00349   mViewSourceAction = new KAction( i18n("&View Source"), Key_V, this,
00350                                    SLOT(slotShowMsgSrc()), actionCollection(),
00351                                    "view_source" );
00352 
00353 
00354   mForwardActionMenu = new KActionMenu( i18n("Message->","&Forward"),
00355                     "mail_forward", actionCollection(),
00356                     "message_forward" );
00357       mForwardInlineAction = new KAction( i18n("&Inline..."),
00358                                       "mail_forward", SHIFT+Key_F, this,
00359                                       SLOT(slotForwardInlineMsg()),
00360                                       actionCollection(),
00361                                       "message_forward_inline" );
00362 
00363       mForwardAttachedAction = new KAction( i18n("Message->Forward->","As &Attachment..."),
00364                                         "mail_forward", Key_F, this,
00365                                         SLOT(slotForwardAttachedMsg()),
00366                                         actionCollection(),
00367                                         "message_forward_as_attachment" );
00368 
00369       mForwardDigestAction = new KAction( i18n("Message->Forward->","As Di&gest..."),
00370                                       "mail_forward", 0, this,
00371                                       SLOT(slotForwardDigestMsg()),
00372                                       actionCollection(),
00373                                       "message_forward_as_digest" );
00374 
00375       mRedirectAction = new KAction( i18n("Message->Forward->","&Redirect..."),
00376                                  "mail_forward", Key_E, this,
00377                                  SLOT(slotRedirectMsg()),
00378                                  actionCollection(),
00379                                  "message_forward_redirect" );
00380 
00381   setupForwardActions();
00382 
00383   mForwardActionMenu->insert( mForwardDigestAction );
00384   mForwardActionMenu->insert( mRedirectAction );
00385 
00386   fontAction = new KFontAction( "Select Font", 0, actionCollection(),
00387                                "text_font" );
00388   fontAction->setFont( mReaderWin->cssHelper()->bodyFont().family() );
00389   connect( fontAction, SIGNAL( activated( const QString& ) ),
00390            SLOT( slotFontAction( const QString& ) ) );
00391   fontSizeAction = new KFontSizeAction( "Select Size", 0, actionCollection(),
00392                                        "text_size" );
00393   fontSizeAction->setFontSize( mReaderWin->cssHelper()->bodyFont().pointSize() );
00394   connect( fontSizeAction, SIGNAL( fontSizeChanged( int ) ),
00395            SLOT( slotSizeAction( int ) ) );
00396 
00397   QAccel *accel = new QAccel(mReaderWin, "showMsg()");
00398   accel->connectItem(accel->insertItem(Key_Up),
00399                      mReaderWin, SLOT(slotScrollUp()));
00400   accel->connectItem(accel->insertItem(Key_Down),
00401                      mReaderWin, SLOT(slotScrollDown()));
00402   accel->connectItem(accel->insertItem(Key_Prior),
00403                      mReaderWin, SLOT(slotScrollPrior()));
00404   accel->connectItem(accel->insertItem(Key_Next),
00405                      mReaderWin, SLOT(slotScrollNext()));
00406   accel->connectItem(accel->insertItem(KStdAccel::shortcut(KStdAccel::Copy)),
00407                      mReaderWin, SLOT(slotCopySelectedText()));
00408   connect( mReaderWin, SIGNAL(popupMenu(KMMessage&,const KURL&,const QPoint&)),
00409       this, SLOT(slotMsgPopup(KMMessage&,const KURL&,const QPoint&)));
00410   connect(mReaderWin, SIGNAL(urlClicked(const KURL&,int)),
00411       mReaderWin, SLOT(slotUrlClicked()));
00412 
00413   setStandardToolBarMenuEnabled(true);
00414   KStdAction::configureToolbars(this, SLOT(slotEditToolbars()), actionCollection());
00415 }
00416 
00417 
00418 void KMReaderMainWin::slotMsgPopup(KMMessage &aMsg, const KURL &aUrl, const QPoint& aPoint)
00419 {
00420   KPopupMenu * menu = new KPopupMenu;
00421   mUrl = aUrl;
00422   mMsg = &aMsg;
00423   bool urlMenuAdded=false;
00424 
00425   if (!aUrl.isEmpty())
00426   {
00427     if (aUrl.protocol() == "mailto") {
00428       // popup on a mailto URL
00429       mReaderWin->mailToComposeAction()->plug( menu );
00430       if ( mMsg ) {
00431         mReaderWin->mailToReplyAction()->plug( menu );
00432         mReaderWin->mailToForwardAction()->plug( menu );
00433         menu->insertSeparator();
00434       }
00435       mReaderWin->addAddrBookAction()->plug( menu );
00436       mReaderWin->openAddrBookAction()->plug( menu );
00437       mReaderWin->copyAction()->plug( menu );
00438     } else {
00439       // popup on a not-mailto URL
00440       mReaderWin->urlOpenAction()->plug( menu );
00441       mReaderWin->addBookmarksAction()->plug( menu );
00442       mReaderWin->urlSaveAsAction()->plug( menu );
00443       mReaderWin->copyURLAction()->plug( menu );
00444     }
00445     urlMenuAdded=true;
00446   }
00447   if(mReaderWin && !mReaderWin->copyText().isEmpty()) {
00448     if ( urlMenuAdded )
00449       menu->insertSeparator();
00450     mMsgActions->replyMenu()->plug( menu );
00451     menu->insertSeparator();
00452 
00453     mReaderWin->copyAction()->plug( menu );
00454     mReaderWin->selectAllAction()->plug( menu );
00455   } else if ( !urlMenuAdded )
00456   {
00457     // popup somewhere else (i.e., not a URL) on the message
00458 
00459     if (!mMsg) // no message
00460     {
00461       delete menu;
00462       return;
00463     }
00464 
00465     if ( ! ( aMsg.parent() && ( aMsg.parent()->isSent() ||
00466                                 aMsg.parent()->isDrafts() ||
00467                                 aMsg.parent()->isTemplates() ) ) ) {
00468       // add the reply and forward actions only if we are not in a sent-mail,
00469       // templates or drafts folder
00470       //
00471       // FIXME: needs custom templates added to menu
00472       // (see KMMainWidget::updateCustomTemplateMenus)
00473       mMsgActions->replyMenu()->plug( menu );
00474       mForwardActionMenu->plug( menu );
00475       menu->insertSeparator();
00476     }
00477 
00478     QPopupMenu* copyMenu = new QPopupMenu(menu);
00479     KMMainWidget* mainwin = kmkernel->getKMMainWidget();
00480     if ( mainwin )
00481       mainwin->folderTree()->folderToPopupMenu( KMFolderTree::CopyMessage, this,
00482           &mMenuToFolder, copyMenu );
00483     menu->insertItem( i18n("&Copy To" ), copyMenu );
00484     menu->insertSeparator();
00485     mViewSourceAction->plug( menu );
00486     mReaderWin->toggleFixFontAction()->plug( menu );
00487     menu->insertSeparator();
00488     mPrintAction->plug( menu );
00489     mSaveAsAction->plug( menu );
00490     menu->insertItem( i18n("Save Attachments..."), mReaderWin, SLOT(slotSaveAttachments()) );
00491     mMsgActions->createTodoAction()->plug( menu );
00492   }
00493   menu->exec(aPoint, 0);
00494   delete menu;
00495 }
00496 
00497 void KMReaderMainWin::copySelectedToFolder( int menuId )
00498 {
00499   if (!mMenuToFolder[menuId])
00500     return;
00501 
00502   KMCommand *command = new KMCopyCommand( mMenuToFolder[menuId], mMsg );
00503   command->start();
00504 }
00505 
00506 void KMReaderMainWin::slotFontAction( const QString& font)
00507 {
00508   QFont f( mReaderWin->cssHelper()->bodyFont() );
00509   f.setFamily( font );
00510   mReaderWin->cssHelper()->setBodyFont( f );
00511   mReaderWin->cssHelper()->setPrintFont( f );
00512   mReaderWin->saveRelativePosition();
00513   mReaderWin->update();
00514 }
00515 
00516 void KMReaderMainWin::slotSizeAction( int size )
00517 {
00518   QFont f( mReaderWin->cssHelper()->bodyFont() );
00519   f.setPointSize( size );
00520   mReaderWin->cssHelper()->setBodyFont( f );
00521   mReaderWin->cssHelper()->setPrintFont( f );
00522   mReaderWin->saveRelativePosition();
00523   mReaderWin->update();
00524 }
00525 
00526 void KMReaderMainWin::slotCreateTodo()
00527 {
00528   if ( !mMsg )
00529     return;
00530   KMCommand *command = new CreateTodoCommand( this, mMsg );
00531   command->start();
00532 }
00533 
00534 void KMReaderMainWin::slotEditToolbars()
00535 {
00536   saveMainWindowSettings( KMKernel::config(), "ReaderWindow" );
00537   KEditToolbar dlg( guiFactory(), this );
00538   connect( &dlg, SIGNAL(newToolbarConfig()), SLOT(slotUpdateToolbars()) );
00539   dlg.exec();
00540 }
00541 
00542 void KMReaderMainWin::slotUpdateToolbars()
00543 {
00544   createGUI("kmreadermainwin.rc");
00545   applyMainWindowSettings(KMKernel::config(), "ReaderWindow");
00546 }
00547 
00548 #include "kmreadermainwin.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys