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 <klocale.h>
00032 #include <kstdaccel.h>
00033 #include <kwin.h>
00034 #include <kaction.h>
00035 #include <kiconloader.h>
00036 #include <kdebug.h>
00037 #include "kmcommands.h"
00038 #include "kmenubar.h"
00039 #include "kpopupmenu.h"
00040 #include "kmreaderwin.h"
00041 #include "kmfolder.h"
00042 #include "kmmainwidget.h"
00043 #include "kmfoldertree.h"
00044 #include "kmmsgdict.h"
00045 
00046 #include "kmreadermainwin.h"
00047 
00048 KMReaderMainWin::KMReaderMainWin( bool htmlOverride, bool htmlLoadExtOverride,
00049                                   char *name )
00050   : KMail::SecondaryWindow( name ? name : "readerwindow#" ),
00051     mMsg( 0 )
00052 {
00053   mReaderWin = new KMReaderWin( this, this, actionCollection() );
00054   //mReaderWin->setShowCompleteMessage( true );
00055   mReaderWin->setAutoDelete( true );
00056   mReaderWin->setHtmlOverride( htmlOverride );
00057   mReaderWin->setHtmlLoadExtOverride( htmlLoadExtOverride );
00058   initKMReaderMainWin();
00059 }
00060 
00061 
00062 //-----------------------------------------------------------------------------
00063 KMReaderMainWin::KMReaderMainWin( char *name )
00064   : KMail::SecondaryWindow( name ? name : "readerwindow#" ),
00065     mMsg( 0 )
00066 {
00067   mReaderWin = new KMReaderWin( this, this, actionCollection() );
00068   mReaderWin->setAutoDelete( true );
00069   initKMReaderMainWin();
00070 }
00071 
00072 
00073 //-----------------------------------------------------------------------------
00074 KMReaderMainWin::KMReaderMainWin(KMMessagePart* aMsgPart,
00075     bool aHTML, const QString& aFileName, const QString& pname,
00076     const QString & encoding, char *name )
00077   : KMail::SecondaryWindow( name ? name : "readerwindow#" ),
00078     mMsg( 0 )
00079 {
00080   mReaderWin = new KMReaderWin( this, this, actionCollection() );
00081   mReaderWin->setOverrideEncoding( encoding );
00082   mReaderWin->setMsgPart( aMsgPart, aHTML, aFileName, pname );
00083   initKMReaderMainWin();
00084 }
00085 
00086 
00087 //-----------------------------------------------------------------------------
00088 void KMReaderMainWin::initKMReaderMainWin() {
00089   setCentralWidget( mReaderWin );
00090   setupAccel();
00091   setupGUI( ToolBar | Keys | StatusBar | Create, "kmreadermainwin.rc" );
00092   applyMainWindowSettings( KMKernel::config(), "Separate Reader Window" );
00093   if ( ! mReaderWin->message() ) {
00094     menuBar()->hide();
00095     toolBar( "mainToolBar" )->hide();
00096   }
00097 
00098   connect( kmkernel, SIGNAL( configChanged() ),
00099            this, SLOT( slotConfigChanged() ) );
00100 }
00101 
00102 //-----------------------------------------------------------------------------
00103 KMReaderMainWin::~KMReaderMainWin()
00104 {
00105   saveMainWindowSettings( KMKernel::config(), "Separate Reader Window" );
00106 }
00107 
00108 //-----------------------------------------------------------------------------
00109 void KMReaderMainWin::setUseFixedFont( bool useFixedFont )
00110 {
00111   mReaderWin->setUseFixedFont( useFixedFont );
00112 }
00113 
00114 //-----------------------------------------------------------------------------
00115 void KMReaderMainWin::showMsg( const QString & encoding, KMMessage *msg )
00116 {
00117   mReaderWin->setOverrideEncoding( encoding );
00118   mReaderWin->setMsg( msg, true );
00119   mReaderWin->slotTouchMessage();
00120   setCaption( msg->subject() );
00121   mMsg = msg;
00122   menuBar()->show();
00123   toolBar( "mainToolBar" )->show();
00124 
00125   connect ( msg->parent(), SIGNAL( destroyed( QObject* ) ), this, SLOT( slotFolderRemoved( QObject* ) ) );
00126 
00127 }
00128 
00129 void KMReaderMainWin::slotFolderRemoved( QObject* folderPtr )
00130 {
00131   assert(mMsg);
00132   assert(folderPtr == mMsg->parent());
00133   if( mMsg && folderPtr == mMsg->parent() )
00134     mMsg->setParent( 0 );
00135 }
00136 
00137 //-----------------------------------------------------------------------------
00138 void KMReaderMainWin::slotTrashMsg()
00139 {
00140   // find the real msg by its sernum
00141   KMFolder* parent;
00142   int index;
00143   KMMsgDict::instance()->getLocation( mMsg->getMsgSerNum(), &parent, &index );
00144   if ( parent && !parent->isTrash() ) {
00145     // open the folder (ref counted)
00146     parent->open("trashmsg");
00147     KMMessage *msg = parent->getMsg( index );
00148     if (msg) {
00149       KMDeleteMsgCommand *command = new KMDeleteMsgCommand( parent, msg );
00150       command->start();
00151     }
00152     parent->close("trashmsg");
00153   }
00154   close();
00155 }
00156 
00157 //-----------------------------------------------------------------------------
00158 void KMReaderMainWin::slotFind()
00159 {
00160   mReaderWin->slotFind();
00161 }
00162 
00163 void KMReaderMainWin::slotFindNext()
00164 {
00165   mReaderWin->slotFindNext();
00166 }
00167 
00168 //-----------------------------------------------------------------------------
00169 void KMReaderMainWin::slotCopy()
00170 {
00171   mReaderWin->slotCopySelectedText();
00172 }
00173 
00174 //-----------------------------------------------------------------------------
00175 void KMReaderMainWin::slotMarkAll()
00176 {
00177   mReaderWin->selectAll();
00178 }
00179 
00180 //-----------------------------------------------------------------------------
00181 void KMReaderMainWin::slotPrintMsg()
00182 {
00183   KMCommand *command = new KMPrintCommand( this, mReaderWin->message(),
00184       mReaderWin->htmlOverride(), mReaderWin->htmlLoadExtOverride(),
00185       mReaderWin->isFixedFont(), mReaderWin->overrideEncoding() );
00186   command->start();
00187 }
00188 
00189 //-----------------------------------------------------------------------------
00190 void KMReaderMainWin::slotReplyToMsg()
00191 {
00192   KMCommand *command = new KMReplyToCommand( this, mReaderWin->message(),
00193       mReaderWin->copyText() );
00194   command->start();
00195 }
00196 
00197 
00198 //-----------------------------------------------------------------------------
00199 void KMReaderMainWin::slotReplyAuthorToMsg()
00200 {
00201   KMCommand *command = new KMReplyAuthorCommand( this, mReaderWin->message(),
00202       mReaderWin->copyText() );
00203   command->start();
00204 }
00205 
00206 //-----------------------------------------------------------------------------
00207 void KMReaderMainWin::slotReplyAllToMsg()
00208 {
00209   KMCommand *command = new KMReplyToAllCommand( this, mReaderWin->message(),
00210       mReaderWin->copyText() );
00211   command->start();
00212 }
00213 
00214 //-----------------------------------------------------------------------------
00215 void KMReaderMainWin::slotReplyListToMsg()
00216 {
00217   KMCommand *command = new KMReplyListCommand( this, mReaderWin->message(),
00218       mReaderWin->copyText() );
00219   command->start();
00220 }
00221 
00222 //-----------------------------------------------------------------------------
00223 void KMReaderMainWin::slotForwardInlineMsg()
00224 {
00225    KMCommand *command = 0;
00226    if ( mReaderWin->message() && mReaderWin->message()->parent() ) {
00227     command = new KMForwardInlineCommand( this, mReaderWin->message(),
00228         mReaderWin->message()->parent()->identity() );
00229    } else {
00230     command = new KMForwardInlineCommand( this, mReaderWin->message() );
00231    }
00232    command->start();
00233 }
00234 
00235 //-----------------------------------------------------------------------------
00236 void KMReaderMainWin::slotForwardAttachedMsg()
00237 {
00238    KMCommand *command = 0;
00239    if ( mReaderWin->message() && mReaderWin->message()->parent() ) {
00240      command = new KMForwardAttachedCommand( this, mReaderWin->message(),
00241         mReaderWin->message()->parent()->identity() );
00242    } else {
00243      command = new KMForwardAttachedCommand( this, mReaderWin->message() );
00244    }
00245    command->start();
00246 }
00247 
00248 //-----------------------------------------------------------------------------
00249 void KMReaderMainWin::slotForwardDigestMsg()
00250 {
00251    KMCommand *command = 0;
00252    if ( mReaderWin->message() && mReaderWin->message()->parent() ) {
00253      command = new KMForwardDigestCommand( this, mReaderWin->message(),
00254         mReaderWin->message()->parent()->identity() );
00255    } else {
00256      command = new KMForwardDigestCommand( this, mReaderWin->message() );
00257    }
00258    command->start();
00259 }
00260 
00261 //-----------------------------------------------------------------------------
00262 void KMReaderMainWin::slotRedirectMsg()
00263 {
00264   KMCommand *command = new KMRedirectCommand( this, mReaderWin->message() );
00265   command->start();
00266 }
00267 
00268 //-----------------------------------------------------------------------------
00269 void KMReaderMainWin::slotShowMsgSrc()
00270 {
00271   KMMessage *msg = mReaderWin->message();
00272   if ( !msg )
00273     return;
00274   KMCommand *command = new KMShowMsgSrcCommand( this, msg,
00275                                                 mReaderWin->isFixedFont() );
00276   command->start();
00277 }
00278 
00279 //-----------------------------------------------------------------------------
00280 void KMReaderMainWin::slotConfigChanged()
00281 {
00282   //readConfig();
00283 }
00284 
00285 void KMReaderMainWin::setupAccel()
00286 {
00287   if ( kmkernel->xmlGuiInstance() )
00288     setInstance( kmkernel->xmlGuiInstance() );
00289 
00290   //----- File Menu
00291   //mOpenAction = KStdAction::open( this, SLOT( slotOpenMsg() ),
00292   //                                actionCollection() );
00293 
00294   //mSaveAsAction = new KAction( i18n("Save &As..."), "filesave",
00295   //                             KStdAccel::shortcut( KStdAccel::Save ),
00296   //                             this, SLOT( slotSaveMsg() ),
00297   //                             actionCollection(), "file_save_as" );
00298 
00299   mSaveAsAction = KStdAction::saveAs( mReaderWin, SLOT( slotSaveMsg() ),
00300                       actionCollection() );
00301   mSaveAsAction->setShortcut( KStdAccel::shortcut( KStdAccel::Save ) );
00302   mPrintAction = KStdAction::print( this, SLOT( slotPrintMsg() ),
00303                                     actionCollection() );
00304 
00305   KAction *closeAction = KStdAction::close( this, SLOT( close() ), actionCollection() );
00306   KShortcut closeShortcut = closeAction->shortcut();
00307   closeShortcut.append( KKey(Key_Escape));
00308   closeAction->setShortcut(closeShortcut);
00309 
00310   //----- Edit Menu
00311   KStdAction::copy( this, SLOT( slotCopy() ), actionCollection() );
00312   KStdAction::selectAll( this, SLOT( slotMarkAll() ), actionCollection() );
00313   KStdAction::find( this, SLOT(slotFind()), actionCollection() );
00314   KStdAction::findNext( this, SLOT( slotFindNext() ), actionCollection() );
00315   mTrashAction = new KAction( KGuiItem( i18n( "&Move to Trash" ), "edittrash",
00316                               i18n( "Move message to trashcan" ) ),
00317                               Key_Delete, this, SLOT( slotTrashMsg() ),
00318                               actionCollection(), "move_to_trash" );
00319 
00320   //----- View Menu
00321   mViewSourceAction = new KAction( i18n("&View Source"), Key_V, this,
00322                                    SLOT(slotShowMsgSrc()), actionCollection(),
00323                                    "view_source" );
00324 
00325 
00326   mForwardActionMenu = new KActionMenu( i18n("Message->","&Forward"),
00327                     "mail_forward", actionCollection(),
00328                     "message_forward" );
00329   connect( mForwardActionMenu, SIGNAL( activated() ), this,
00330            SLOT( slotForwardInlineMsg() ) );
00331 
00332   mForwardAttachedAction = new KAction( i18n("Message->Forward->","As &Attachment..."),
00333                                         "mail_forward", Key_F, this,
00334                     SLOT(slotForwardAttachedMsg()),
00335                                         actionCollection(),
00336                     "message_forward_as_attachment" );
00337   mForwardActionMenu->insert( mForwardAttachedAction );
00338 
00339   mForwardInlineAction = new KAction( i18n("&Inline..."),
00340                                       "mail_forward", SHIFT+Key_F, this,
00341                                       SLOT(slotForwardInlineMsg()),
00342                                       actionCollection(),
00343                                       "message_forward_inline" );
00344   mForwardActionMenu->insert( mForwardInlineAction );
00345 
00346   mForwardDigestAction = new KAction( i18n("Message->Forward->","As Di&gest..."),
00347                                       "mail_forward", 0, this,
00348                                       SLOT(slotForwardDigestMsg()),
00349                                       actionCollection(),
00350                                       "message_forward_as_digest" );
00351   mForwardActionMenu->insert( mForwardDigestAction );
00352 
00353   mRedirectAction = new KAction( i18n("Message->Forward->","&Redirect..."),
00354                  "mail_forward", Key_E, this,
00355                                  SLOT(slotRedirectMsg()),
00356                  actionCollection(),
00357                                  "message_forward_redirect" );
00358   mForwardActionMenu->insert( mRedirectAction );
00359 
00360   mReplyActionMenu = new KActionMenu( i18n("Message->","&Reply"),
00361                                       "mail_reply", actionCollection(),
00362                                       "message_reply_menu" );
00363   connect( mReplyActionMenu, SIGNAL(activated()), this,
00364        SLOT(slotReplyToMsg()) );
00365 
00366   mReplyAction = new KAction( i18n("&Reply..."), "mail_reply", Key_R, this,
00367                   SLOT(slotReplyToMsg()), actionCollection(), "reply" );
00368   mReplyActionMenu->insert( mReplyAction );
00369 
00370   mReplyAuthorAction = new KAction( i18n("Reply to A&uthor..."), "mail_reply",
00371                                     SHIFT+Key_A, this,
00372                                     SLOT(slotReplyAuthorToMsg()),
00373                                     actionCollection(), "reply_author" );
00374   mReplyActionMenu->insert( mReplyAuthorAction );
00375 
00376   mReplyAllAction = new KAction( i18n("Reply to &All..."), "mail_replyall",
00377                  Key_A, this, SLOT(slotReplyAllToMsg()),
00378                  actionCollection(), "reply_all" );
00379   mReplyActionMenu->insert( mReplyAllAction );
00380 
00381   mReplyListAction = new KAction( i18n("Reply to Mailing-&List..."),
00382                   "mail_replylist", Key_L, this,
00383                   SLOT(slotReplyListToMsg()), actionCollection(),
00384                   "reply_list" );
00385   mReplyActionMenu->insert( mReplyListAction );
00386 
00387 
00388 
00389   QAccel *accel = new QAccel(mReaderWin, "showMsg()");
00390   accel->connectItem(accel->insertItem(Key_Up),
00391                      mReaderWin, SLOT(slotScrollUp()));
00392   accel->connectItem(accel->insertItem(Key_Down),
00393                      mReaderWin, SLOT(slotScrollDown()));
00394   accel->connectItem(accel->insertItem(Key_Prior),
00395                      mReaderWin, SLOT(slotScrollPrior()));
00396   accel->connectItem(accel->insertItem(Key_Next),
00397                      mReaderWin, SLOT(slotScrollNext()));
00398   accel->connectItem(accel->insertItem(KStdAccel::shortcut(KStdAccel::Copy)),
00399                      mReaderWin, SLOT(slotCopySelectedText()));
00400   connect( mReaderWin, SIGNAL(popupMenu(KMMessage&,const KURL&,const QPoint&)),
00401       this, SLOT(slotMsgPopup(KMMessage&,const KURL&,const QPoint&)));
00402   connect(mReaderWin, SIGNAL(urlClicked(const KURL&,int)),
00403       mReaderWin, SLOT(slotUrlClicked()));
00404 
00405 }
00406 
00407 
00408 void KMReaderMainWin::slotMsgPopup(KMMessage &aMsg, const KURL &aUrl, const QPoint& aPoint)
00409 {
00410   KPopupMenu * menu = new KPopupMenu;
00411   mUrl = aUrl;
00412   mMsg = &aMsg;
00413   bool urlMenuAdded=false;
00414 
00415   if (!aUrl.isEmpty())
00416   {
00417     if (aUrl.protocol() == "mailto") {
00418       // popup on a mailto URL
00419       mReaderWin->mailToComposeAction()->plug( menu );
00420       if ( mMsg ) {
00421         mReaderWin->mailToReplyAction()->plug( menu );
00422         mReaderWin->mailToForwardAction()->plug( menu );
00423         menu->insertSeparator();
00424       }
00425       mReaderWin->addAddrBookAction()->plug( menu );
00426       mReaderWin->openAddrBookAction()->plug( menu );
00427       mReaderWin->copyAction()->plug( menu );
00428     } else {
00429       // popup on a not-mailto URL
00430       mReaderWin->urlOpenAction()->plug( menu );
00431       mReaderWin->addBookmarksAction()->plug( menu );
00432       mReaderWin->urlSaveAsAction()->plug( menu );
00433       mReaderWin->copyURLAction()->plug( menu );
00434     }
00435     urlMenuAdded=true;
00436   }
00437   if(mReaderWin && !mReaderWin->copyText().isEmpty()) {
00438     if ( urlMenuAdded )
00439       menu->insertSeparator();
00440     mReplyActionMenu->plug( menu );
00441     menu->insertSeparator();
00442 
00443     mReaderWin->copyAction()->plug( menu );
00444     mReaderWin->selectAllAction()->plug( menu );
00445   } else if ( !urlMenuAdded )
00446   {
00447     // popup somewhere else (i.e., not a URL) on the message
00448 
00449     if (!mMsg) // no message
00450     {
00451       delete menu;
00452       return;
00453     }
00454 
00455     if ( ! ( aMsg.parent() && ( aMsg.parent()->isSent() ||
00456                                 aMsg.parent()->isDrafts() ||
00457                                 aMsg.parent()->isTemplates() ) ) ) {
00458       // add the reply and forward actions only if we are not in a sent-mail,
00459       // templates or drafts folder
00460       //
00461       // FIXME: needs custom templates added to menu
00462       // (see KMMainWidget::updateCustomTemplateMenus)
00463       mReplyActionMenu->plug( menu );
00464       mForwardActionMenu->plug( menu );
00465       menu->insertSeparator();
00466     }
00467 
00468     QPopupMenu* copyMenu = new QPopupMenu(menu);
00469     KMMainWidget* mainwin = kmkernel->getKMMainWidget();
00470     if ( mainwin )
00471       mainwin->folderTree()->folderToPopupMenu( KMFolderTree::CopyMessage, this,
00472           &mMenuToFolder, copyMenu );
00473     menu->insertItem( i18n("&Copy To" ), copyMenu );
00474     menu->insertSeparator();
00475     mViewSourceAction->plug( menu );
00476     mReaderWin->toggleFixFontAction()->plug( menu );
00477     menu->insertSeparator();
00478     mPrintAction->plug( menu );
00479     mSaveAsAction->plug( menu );
00480     menu->insertItem( i18n("Save Attachments..."), mReaderWin, SLOT(slotSaveAttachments()) );
00481   }
00482   menu->exec(aPoint, 0);
00483   delete menu;
00484 }
00485 
00486 void KMReaderMainWin::copySelectedToFolder( int menuId )
00487 {
00488   if (!mMenuToFolder[menuId])
00489     return;
00490 
00491   KMCommand *command = new KMCopyCommand( mMenuToFolder[menuId], mMsg );
00492   command->start();
00493 }
00494 
00495 #include "kmreadermainwin.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys