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>
23 #include "kmkernel.h" //control center
24 #include "kmail_options.h"
28 #undef Status // stupid X headers
30 #include "aboutdata.h"
32 #include "kmstartup.h"
39 //-----------------------------------------------------------------------------
41 class KMailApplication
: public KPIM::PimApplication
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();
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()
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;
83 if (!kmkernel
->firstInstance() || !kapp
->isSessionRestored())
84 kmkernel
->handleCommandLine( true );
85 kmkernel
->setFirstInstance(false);
89 void KMailApplication::delayedInstanceCreation() {
90 if ( mDelayedInstanceCreation
)
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
100 #if 0 // for testing KUniqueAppliaction on Windows
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
);
105 KMail::AboutData about
;
107 KCmdLineArgs::init(argc
, argv
, &about
);
108 KCmdLineArgs::addCmdLineOptions( kmail_options() ); // Add kmail options
109 if (!KMailApplication::start())
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
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();
127 KMKernel kmailKernel
;
130 // and session management
131 kmailKernel
.doSessionManagement();
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();
146 int ret
= qApp
->exec();
148 kmailKernel
.cleanup();
150 KMail::cleanup(); // pid file (see kmstartup.cpp)