krop's commit fixes my problem in a better way, reverting
[kdepim.git] / kmail / main.cpp
blobe58dbe32d6de009e5b18a007782ad2d8174b701e
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 <libkdepim/pimapplication.h>
22 #include <kglobal.h>
23 #include "kmkernel.h" //control center
24 #include "kmail_options.h"
26 #include <kdebug.h>
28 #undef Status // stupid X headers
30 #include "aboutdata.h"
32 #include "kmstartup.h"
34 #ifdef Q_WS_WIN
35 #include <unistd.h>
36 #include <windows.h>
37 #endif
39 //-----------------------------------------------------------------------------
41 class KMailApplication : public KPIM::PimApplication
43 public:
44 KMailApplication() : KPIM::PimApplication(),
45 mDelayedInstanceCreation( false ),
46 mEventLoopReached( false ) { }
47 virtual int newInstance();
48 void commitData(QSessionManager& sm);
49 void setEventLoopReached();
50 void delayedInstanceCreation();
51 protected:
52 bool mDelayedInstanceCreation;
53 bool mEventLoopReached;
57 void KMailApplication::commitData(QSessionManager& sm) {
58 kmkernel->dumpDeadLetters();
59 kmkernel->setShuttingDown( true ); // Prevent further dumpDeadLetters calls
60 KApplication::commitData( sm );
63 void KMailApplication::setEventLoopReached() {
64 mEventLoopReached = true;
67 int KMailApplication::newInstance()
69 kDebug();
71 // If the event loop hasn't been reached yet, the kernel is probably not
72 // fully initialized. Creating an instance would therefore fail, this is why
73 // that is delayed until delayedInstanceCreation() is called.
74 if ( !mEventLoopReached ) {
75 kDebug() << "Delaying instance creation.";
76 mDelayedInstanceCreation = true;
77 return 0;
80 if (!kmkernel)
81 return 0;
83 if (!kmkernel->firstInstance() || !kapp->isSessionRestored())
84 kmkernel->handleCommandLine( true );
85 kmkernel->setFirstInstance(false);
86 return 0;
89 void KMailApplication::delayedInstanceCreation() {
90 if ( mDelayedInstanceCreation )
91 newInstance();
94 int main(int argc, char *argv[])
96 // WABA: KMail is a KUniqueApplication. Unfortunately this makes debugging
97 // a bit harder: You should pass --nofork as commandline argument when using
98 // a debugger. In gdb you can do this by typing "set args --nofork" before
99 // typing "run".
100 #if 0 // for testing KUniqueAppliaction on Windows
101 MessageBoxA(NULL,
102 QString("main() %1 pid=%2").arg(argv[0]).arg(getpid()).toLatin1(),
103 QString("main() \"%1\"").arg(argv[0]).toLatin1(), MB_OK|MB_ICONINFORMATION|MB_TASKMODAL);
104 #endif
105 KMail::AboutData about;
107 KCmdLineArgs::init(argc, argv, &about);
108 KCmdLineArgs::addCmdLineOptions( kmail_options() ); // Add kmail options
109 if (!KMailApplication::start())
110 return 0;
112 KMailApplication app;
114 // Qt doesn't treat the system tray as a window, and therefore Qt would quit
115 // the event loop when an error message is clicked away while KMail is in the
116 // tray.
117 // Rely on KGlobal::ref() and KGlobal::deref() instead, like we did in KDE3.
118 // See http://bugs.kde.org/show_bug.cgi?id=163479
119 QApplication::setQuitOnLastWindowClosed( false );
121 // import i18n data and icons from libraries:
122 KMail::insertLibraryCataloguesAndIcons();
124 KMail::lockOrDie();
126 //local, do the init
127 KMKernel kmailKernel;
128 kmailKernel.init();
130 // and session management
131 kmailKernel.doSessionManagement();
133 // any dead letters?
134 kmailKernel.recoverDeadLetters();
136 kmsetSignalHandler(kmsignalHandler);
138 kmkernel->setupDBus(); // Ok. We are ready for D-Bus requests.
139 kmkernel->setStartingUp( false ); // Starting up is finished
141 //If the instance hasn't been created yet, do that now
142 app.setEventLoopReached();
143 app.delayedInstanceCreation();
145 // Go!
146 int ret = qApp->exec();
147 // clean up
148 kmailKernel.cleanup();
150 KMail::cleanup(); // pid file (see kmstartup.cpp)
151 return ret;