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>
23 #include "kmkernel.h" //control center
24 #include "kmmainwidget.h"
25 #include "kmail_options.h"
28 #include <kmessagebox.h>
29 #undef Status // stupid X headers
31 #include "aboutdata.h"
33 #include "kmstartup.h"
40 //-----------------------------------------------------------------------------
42 class KMailApplication
: public KontactInterface::PimUniqueApplication
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();
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()
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;
84 if (!kmkernel
->firstInstance() || !kapp
->isSessionRestored())
85 kmkernel
->handleCommandLine( true );
86 kmkernel
->setFirstInstance(false);
90 void KMailApplication::delayedInstanceCreation() {
91 if ( mDelayedInstanceCreation
)
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
101 #if 0 // for testing KUniqueAppliaction on Windows
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
);
106 KMail::AboutData about
;
108 KCmdLineArgs::init(argc
, argv
, &about
);
109 KCmdLineArgs::addCmdLineOptions( kmail_options() ); // Add kmail options
110 if (!KMailApplication::start())
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
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();
128 KMKernel kmailKernel
;
131 // and session management
132 kmailKernel
.doSessionManagement();
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();
145 int ret
= qApp
->exec();
147 kmailKernel
.cleanup();
148 KMail::cleanup(); // pid file (see kmstartup.cpp)