backport 1145339: no error box spam
[kdepim.git] / kmail / main.cpp
blob00b6f210bb5c175f7a3bc92934b0a8f46355e03a
1 /* -*- mode: C++; c-file-style: "gnu" -*-
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 <kontactinterface/pimuniqueapplication.h>
22 #include <kglobal.h>
23 #include "kmkernel.h" //control center
24 #include "kmmainwidget.h"
25 #include "kmail_options.h"
27 #include <kdebug.h>
28 #include <kmessagebox.h>
29 #undef Status // stupid X headers
31 #include "aboutdata.h"
33 #include "kmstartup.h"
35 #ifdef Q_WS_WIN
36 #include <unistd.h>
37 #include <windows.h>
38 #endif
40 //-----------------------------------------------------------------------------
42 class KMailApplication : public KontactInterface::PimUniqueApplication
44 public:
45 KMailApplication() : KontactInterface::PimUniqueApplication(),
46 mDelayedInstanceCreation( false ),
47 mEventLoopReached( false ) { }
48 virtual int newInstance();
49 void commitData(QSessionManager& sm);
50 void setEventLoopReached();
51 void delayedInstanceCreation();
52 protected:
53 bool mDelayedInstanceCreation;
54 bool mEventLoopReached;
58 void KMailApplication::commitData(QSessionManager& sm) {
59 kmkernel->dumpDeadLetters();
60 kmkernel->setShuttingDown( true ); // Prevent further dumpDeadLetters calls
61 KApplication::commitData( sm );
64 void KMailApplication::setEventLoopReached() {
65 mEventLoopReached = true;
68 int KMailApplication::newInstance()
70 kDebug();
72 // If the event loop hasn't been reached yet, the kernel is probably not
73 // fully initialized. Creating an instance would therefore fail, this is why
74 // that is delayed until delayedInstanceCreation() is called.
75 if ( !mEventLoopReached ) {
76 kDebug() << "Delaying instance creation.";
77 mDelayedInstanceCreation = true;
78 return 0;
81 if (!kmkernel)
82 return 0;
84 if (!kmkernel->firstInstance() || !kapp->isSessionRestored())
85 kmkernel->handleCommandLine( true );
86 kmkernel->setFirstInstance(false);
87 return 0;
90 void KMailApplication::delayedInstanceCreation() {
91 if ( mDelayedInstanceCreation )
92 newInstance();
95 int main(int argc, char *argv[])
97 // WABA: KMail is a KUniqueApplication. Unfortunately this makes debugging
98 // a bit harder: You should pass --nofork as commandline argument when using
99 // a debugger. In gdb you can do this by typing "set args --nofork" before
100 // typing "run".
101 #if 0 // for testing KUniqueAppliaction on Windows
102 MessageBoxA(NULL,
103 QString("main() %1 pid=%2").arg(argv[0]).arg(getpid()).toLatin1(),
104 QString("main() \"%1\"").arg(argv[0]).toLatin1(), MB_OK|MB_ICONINFORMATION|MB_TASKMODAL);
105 #endif
106 KMail::AboutData about;
108 KCmdLineArgs::init(argc, argv, &about);
109 KCmdLineArgs::addCmdLineOptions( kmail_options() ); // Add kmail options
110 if (!KMailApplication::start())
111 return 0;
113 KMailApplication app;
115 // Qt doesn't treat the system tray as a window, and therefore Qt would quit
116 // the event loop when an error message is clicked away while KMail is in the
117 // tray.
118 // Rely on KGlobal::ref() and KGlobal::deref() instead, like we did in KDE3.
119 // See http://bugs.kde.org/show_bug.cgi?id=163479
120 QApplication::setQuitOnLastWindowClosed( false );
122 // import i18n data and icons from libraries:
123 KMail::insertLibraryCataloguesAndIcons();
125 KMail::lockOrDie();
127 //local, do the init
128 KMKernel kmailKernel;
129 kmailKernel.init();
131 // and session management
132 kmailKernel.doSessionManagement();
134 // any dead letters?
135 kmailKernel.recoverDeadLetters();
137 kmkernel->setupDBus(); // Ok. We are ready for D-Bus requests.
138 kmkernel->setStartingUp( false ); // Starting up is finished
140 //If the instance hasn't been created yet, do that now
141 app.setEventLoopReached();
142 app.delayedInstanceCreation();
144 // Go!
145 int ret = qApp->exec();
146 // clean up
147 kmailKernel.cleanup();
148 KMail::cleanup(); // pid file (see kmstartup.cpp)
149 return ret;