backport 1145339: no error box spam
[kdepim.git] / kmail / kmmainwin.cpp
blobdd94383981bcd7415dd5ecf13fcc1e76960527ab
1 /*
2 * kmail: KDE mail client
3 * Copyright (c) 1996-1998 Stefan Taferner <taferner@kde.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "kmmainwin.h"
22 #include "kmmainwidget.h"
23 #include "kstatusbar.h"
24 #include "progressdialog.h"
25 #include "statusbarprogresswidget.h"
26 #include "broadcaststatus.h"
27 #include "util.h"
28 #include "tagactionmanager.h"
30 #include <kapplication.h>
31 #include <klocale.h>
32 #include <kedittoolbar.h>
33 #include <kconfig.h>
34 #include <kmessagebox.h>
35 #include <kstringhandler.h>
36 #include <kstandardaction.h>
37 #include <kdebug.h>
38 #include <ktip.h>
39 #include <kicon.h>
41 #include "messagecomposer/messagesender.h"
44 #include "kmmainwin.moc"
47 KMMainWin::KMMainWin(QWidget *)
48 : KXmlGuiWindow( 0 ),
49 mReallyClose( false )
51 setObjectName( "kmail-mainwindow#" );
52 // Set this to be the group leader for all subdialogs - this means
53 // modal subdialogs will only affect this dialog, not the other windows
54 setAttribute( Qt::WA_GroupLeader );
56 KAction *action = new KAction( KIcon("window-new"), i18n("New &Window"), this );
57 actionCollection()->addAction( "new_mail_client", action );
58 connect( action, SIGNAL( triggered(bool) ), SLOT( slotNewMailReader() ) );
60 resize( 700, 500 ); // The default size
62 mKMMainWidget = new KMMainWidget( this, this, actionCollection() );
63 setCentralWidget( mKMMainWidget );
64 setupStatusBar();
65 if ( kmkernel->xmlGuiInstance().isValid() )
66 setComponentData( kmkernel->xmlGuiInstance() );
68 setStandardToolBarMenuEnabled( true );
70 KStandardAction::configureToolbars( this, SLOT( slotEditToolbars() ),
71 actionCollection() );
73 KStandardAction::keyBindings( mKMMainWidget, SLOT( slotEditKeys() ),
74 actionCollection() );
76 KStandardAction::quit( this, SLOT( slotQuit() ), actionCollection() );
77 #ifdef Q_WS_MACX
78 // ### This quits the application prematurely, for example when the composer window
79 // ### is closed while the main application is minimized to the systray
80 connect( qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit()));
81 #endif
82 createGUI( "kmmainwin.rc" );
83 // Don't use conserveMemory() because this renders dynamic plugging
84 // of actions unusable!
86 //must be after createGUI, otherwise e.g toolbar settings are not loaded
87 applyMainWindowSettings( KMKernel::config()->group( "Main Window") );
89 connect( KPIM::BroadcastStatus::instance(), SIGNAL( statusMsg( const QString& ) ),
90 this, SLOT( displayStatusMsg(const QString&) ) );
92 connect( mKMMainWidget, SIGNAL( captionChangeRequest(const QString&) ),
93 SLOT( setCaption(const QString&) ) );
95 // Enable mail checks again (see destructor)
96 #if 0
97 kmkernel->enableMailCheck();
98 #else
99 kDebug() << "AKONADI PORT: Disabled code in " << Q_FUNC_INFO;
100 #endif
101 if ( kmkernel->firstStart() )
103 KMail::Util::launchAccountWizard( this );
105 if ( kmkernel->firstInstance() )
106 QTimer::singleShot( 200, this, SLOT( slotShowTipOnStart() ) );
109 KMMainWin::~KMMainWin()
111 saveMainWindowSettings( KMKernel::config()->group( "Main Window") );
112 KMKernel::config()->sync();
114 if ( mReallyClose ) {
115 // Check if this was the last KMMainWin
116 uint not_withdrawn = 0;
117 foreach ( KMainWindow* window, KMainWindow::memberList() ) {
118 if ( !window->isHidden() && window->isTopLevel() &&
119 window != this && ::qobject_cast<KMMainWin *>( window ) )
120 not_withdrawn++;
123 if ( not_withdrawn == 0 ) {
124 kDebug() << "Closing last KMMainWin: stopping mail check";
125 // Running KIO jobs prevent kapp from exiting, so we need to kill them
126 // if they are only about checking mail (not important stuff like moving messages)
127 #if 0
128 kmkernel->abortMailCheck();
129 //kmkernel->acctMgr()->cancelMailCheck();
130 #else
131 kDebug() << "AKONADI PORT: Disabled code in " << Q_FUNC_INFO;
132 #endif
137 void KMMainWin::displayStatusMsg( const QString& aText )
139 if ( !statusBar() || !mLittleProgress )
140 return;
141 int statusWidth = statusBar()->width() - mLittleProgress->width()
142 - fontMetrics().maxWidth();
144 QString text = fontMetrics().elidedText( ' ' + aText, Qt::ElideRight,
145 statusWidth );
147 // ### FIXME: We should disable richtext/HTML (to avoid possible denial of service attacks),
148 // but this code would double the size of the satus bar if the user hovers
149 // over an <foo@bar.com>-style email address :-(
150 // text.replace("&", "&amp;");
151 // text.replace("<", "&lt;");
152 // text.replace(">", "&gt;");
154 statusBar()->changeItem( text, mMessageStatusId );
157 //-----------------------------------------------------------------------------
158 void KMMainWin::slotNewMailReader()
160 KMMainWin *d;
162 d = new KMMainWin();
163 d->show();
164 d->resize( d->size() );
168 void KMMainWin::slotEditToolbars()
170 saveMainWindowSettings(KMKernel::config()->group( "Main Window") );
171 KEditToolBar dlg(actionCollection(), this);
172 dlg.setResourceFile( "kmmainwin.rc" );
174 connect( &dlg, SIGNAL(newToolbarConfig()), SLOT(slotUpdateToolbars()) );
176 dlg.exec();
179 void KMMainWin::slotUpdateToolbars()
181 // remove dynamically created actions before editing
182 mKMMainWidget->clearFilterActions();
183 mKMMainWidget->tagActionManager()->clearActions();
185 createGUI("kmmainwin.rc");
186 applyMainWindowSettings(KMKernel::config()->group( "Main Window") );
188 // plug dynamically created actions again
189 mKMMainWidget->initializeFilterActions();
190 mKMMainWidget->tagActionManager()->createActions();
193 void KMMainWin::setupStatusBar()
195 mMessageStatusId = 1;
197 /* Create a progress dialog and hide it. */
198 mProgressDialog = new KPIM::ProgressDialog( statusBar(), this );
199 mProgressDialog->hide();
201 mLittleProgress = new StatusbarProgressWidget( mProgressDialog, statusBar() );
202 mLittleProgress->show();
204 statusBar()->insertItem( i18n("Starting..."), 1, 4 );
205 QTimer::singleShot( 2000, KPIM::BroadcastStatus::instance(), SLOT( reset() ) );
206 statusBar()->setItemAlignment( 1, Qt::AlignLeft | Qt::AlignVCenter );
207 statusBar()->addPermanentWidget( mKMMainWidget->vacationScriptIndicator() );
208 statusBar()->addPermanentWidget( mLittleProgress );
209 mLittleProgress->show();
212 void KMMainWin::slotQuit()
214 mReallyClose = true;
215 close();
218 //-----------------------------------------------------------------------------
219 bool KMMainWin::restoreDockedState( int n )
221 // Default restore behavior is to show the window once it is restored.
222 // Override this if the main window was hidden in the system tray
223 // when the session was saved.
224 KConfigGroup config( kapp->sessionConfig(), QString::number( n ) );
225 bool show = !config.readEntry ("docked", false );
227 return KMainWindow::restore ( n, show );
230 void KMMainWin::saveProperties( KConfigGroup &config )
232 // This is called by the session manager on log-off
233 // Save the shown/hidden status so we can restore to the same state.
234 KMainWindow::saveProperties( config );
235 config.writeEntry( "docked", isHidden() );
238 bool KMMainWin::queryClose()
240 if ( kmkernel->shuttingDown() || kapp->sessionSaving() || mReallyClose )
241 return true;
242 return kmkernel->canQueryClose();
245 void KMMainWin::slotShowTipOnStart()
247 KTipDialog::showTip( this );