kmail

kmmainwin.cpp

Go to the documentation of this file.
00001 
00002 #ifdef HAVE_CONFIG_H
00003 #include <config.h>
00004 #endif
00005 
00006 #include "kmmainwin.h"
00007 #include "kmmainwidget.h"
00008 #include "kstatusbar.h"
00009 #include "messagesender.h"
00010 #include "progressdialog.h"
00011 #include "statusbarprogresswidget.h"
00012 #include "accountwizard.h"
00013 #include "broadcaststatus.h"
00014 #include "accountmanager.h"
00015 #include "kmtransport.h"
00016 
00017 #include <kapplication.h>
00018 #include <klocale.h>
00019 #include <kedittoolbar.h>
00020 #include <kconfig.h>
00021 #include <kmessagebox.h>
00022 #include <kstringhandler.h>
00023 #include <kdebug.h>
00024 #include <ktip.h>
00025 
00026 #include "kmmainwin.moc"
00027 
00028 KMMainWin::KMMainWin(QWidget *)
00029     : KMainWindow( 0, "kmail-mainwindow#" ),
00030       mReallyClose( false )
00031 {
00032   // Set this to be the group leader for all subdialogs - this means
00033   // modal subdialogs will only affect this dialog, not the other windows
00034   setWFlags( getWFlags() | WGroupLeader );
00035 
00036   kapp->ref();
00037 
00038   (void) new KAction( i18n("New &Window"), "window_new", 0,
00039               this, SLOT(slotNewMailReader()),
00040               actionCollection(), "new_mail_client" );
00041 
00042   mKMMainWidget = new KMMainWidget( this, "KMMainWidget", this, actionCollection() );
00043   mKMMainWidget->resize( 725, 700 );
00044   setCentralWidget(mKMMainWidget);
00045   setupStatusBar();
00046   if (kmkernel->xmlGuiInstance())
00047     setInstance( kmkernel->xmlGuiInstance() );
00048 
00049   if ( kmkernel->firstInstance() )
00050     QTimer::singleShot( 200, this, SLOT(slotShowTipOnStart()) );
00051 
00052   setStandardToolBarMenuEnabled(true);
00053 
00054   KStdAction::configureToolbars(this, SLOT(slotEditToolbars()),
00055                 actionCollection());
00056 
00057   KStdAction::keyBindings(mKMMainWidget, SLOT(slotEditKeys()),
00058                           actionCollection());
00059 
00060   KStdAction::quit( this, SLOT(slotQuit()), actionCollection());
00061   createGUI( "kmmainwin.rc", false );
00062   // Don't use conserveMemory() because this renders dynamic plugging
00063   // of actions unusable!
00064 
00065   mKMMainWidget->setupForwardingActionsList();
00066 
00067   applyMainWindowSettings(KMKernel::config(), "Main Window");
00068 
00069   connect( KPIM::BroadcastStatus::instance(), SIGNAL( statusMsg( const QString& ) ),
00070            this, SLOT( displayStatusMsg(const QString&) ) );
00071 
00072   connect(kmkernel, SIGNAL(configChanged()),
00073     this, SLOT(slotConfigChanged()));
00074 
00075   connect(mKMMainWidget, SIGNAL(captionChangeRequest(const QString&)),
00076       SLOT(setCaption(const QString&)) );
00077 
00078   // Enable mail checks again (see destructor)
00079   kmkernel->enableMailCheck();
00080 
00081   if ( kmkernel->firstStart() )
00082     AccountWizard::start( kmkernel, this );
00083 }
00084 
00085 KMMainWin::~KMMainWin()
00086 {
00087   saveMainWindowSettings(KMKernel::config(), "Main Window");
00088   KMKernel::config()->sync();
00089   kapp->deref();
00090 
00091   if ( !kmkernel->haveSystemTrayApplet() ) {
00092     // Check if this was the last KMMainWin
00093     int not_withdrawn = 0;
00094     QPtrListIterator<KMainWindow> it(*KMainWindow::memberList);
00095     for (it.toFirst(); it.current(); ++it){
00096       if ( !it.current()->isHidden() &&
00097            it.current()->isTopLevel() &&
00098            it.current() != this &&
00099            ::qt_cast<KMMainWin *>( it.current() )
00100         )
00101         not_withdrawn++;
00102     }
00103 
00104     if ( not_withdrawn == 0 ) {
00105       kdDebug(5006) << "Closing last KMMainWin: stopping mail check" << endl;
00106       // Running KIO jobs prevent kapp from exiting, so we need to kill them
00107       // if they are only about checking mail (not important stuff like moving messages)
00108       kmkernel->abortMailCheck();
00109       kmkernel->acctMgr()->cancelMailCheck();
00110     }
00111   }
00112 }
00113 
00114 void KMMainWin::displayStatusMsg(const QString& aText)
00115 {
00116   if ( !statusBar() || !mLittleProgress) return;
00117   int statusWidth = statusBar()->width() - mLittleProgress->width()
00118                     - fontMetrics().maxWidth();
00119   QString text = KStringHandler::rPixelSqueeze( " " + aText, fontMetrics(),
00120                                                 statusWidth );
00121 
00122   // ### FIXME: We should disable richtext/HTML (to avoid possible denial of service attacks),
00123   // but this code would double the size of the satus bar if the user hovers
00124   // over an <foo@bar.com>-style email address :-(
00125 //  text.replace("&", "&amp;");
00126 //  text.replace("<", "&lt;");
00127 //  text.replace(">", "&gt;");
00128 
00129   statusBar()->changeItem(text, mMessageStatusId);
00130 }
00131 
00132 //-----------------------------------------------------------------------------
00133 void KMMainWin::slotNewMailReader()
00134 {
00135   KMMainWin *d;
00136 
00137   d = new KMMainWin();
00138   d->show();
00139   d->resize(d->size());
00140 }
00141 
00142 
00143 void KMMainWin::slotEditToolbars()
00144 {
00145   saveMainWindowSettings(KMKernel::config(), "Main Window");
00146   KEditToolbar dlg(actionCollection(), "kmmainwin.rc");
00147 
00148   connect( &dlg, SIGNAL(newToolbarConfig()),
00149        SLOT(slotUpdateToolbars()) );
00150 
00151   dlg.exec();
00152 }
00153 
00154 void KMMainWin::slotUpdateToolbars()
00155 {
00156   // remove dynamically created actions before editing
00157   mKMMainWidget->clearFilterActions();
00158 
00159   createGUI("kmmainwin.rc", false);
00160   applyMainWindowSettings(KMKernel::config(), "Main Window");
00161 
00162   // plug dynamically created actions again
00163   mKMMainWidget->initializeFilterActions();
00164 }
00165 
00166 void KMMainWin::setupStatusBar()
00167 {
00168   mMessageStatusId = 1;
00169 
00170   /* Create a progress dialog and hide it. */
00171   mProgressDialog = new KPIM::ProgressDialog( statusBar(), this );
00172   mProgressDialog->hide();
00173 
00174   mLittleProgress = new StatusbarProgressWidget( mProgressDialog, statusBar() );
00175   mLittleProgress->show();
00176 
00177   statusBar()->addWidget( mLittleProgress, 0 , true );
00178   statusBar()->insertItem(i18n(" Initializing..."), 1, 4 );
00179   statusBar()->setItemAlignment( 1, AlignLeft | AlignVCenter );
00180   statusBar()->addWidget( mKMMainWidget->vacationScriptIndicator(), 1 );
00181   mLittleProgress->show();
00182 }
00183 
00185 void KMMainWin::readConfig(void)
00186 {
00187 }
00188 
00190 void KMMainWin::writeConfig(void)
00191 {
00192   mKMMainWidget->writeConfig();
00193 }
00194 
00195 void KMMainWin::slotQuit()
00196 {
00197   mReallyClose = true;
00198   close();
00199 }
00200 
00201 void KMMainWin::slotConfigChanged()
00202 {
00203   readConfig();
00204 }
00205 
00206 //-----------------------------------------------------------------------------
00207 bool KMMainWin::queryClose()
00208 {
00209   if ( kapp->sessionSaving() )
00210     writeConfig();
00211 
00212   if ( kmkernel->shuttingDown() || kapp->sessionSaving() || mReallyClose )
00213     return true;
00214   return kmkernel->canQueryClose();
00215 }
00216 
00217 void KMMainWin::slotShowTipOnStart()
00218 {
00219   KTipDialog::showTip( this );
00220 }
00221 
00222