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"
26 #include "kmmigrateapplication.h"
28 #include "kmail_debug.h"
29 #include <kmessagebox.h>
30 #undef Status // stupid X headers
32 #include "aboutdata.h"
34 #include "kmstartup.h"
42 #include <QApplication>
43 #include <QSessionManager>
45 //-----------------------------------------------------------------------------
47 class KMailApplication
: public KontactInterface::PimUniqueApplication
50 KMailApplication(int &argc
, char **argv
[])
51 : KontactInterface::PimUniqueApplication(argc
, argv
)
52 , mDelayedInstanceCreation(false)
53 , mEventLoopReached(false)
56 int activate(const QStringList
&args
) Q_DECL_OVERRIDE
;
57 void commitData(QSessionManager
&sm
);
58 void setEventLoopReached();
59 void delayedInstanceCreation(const QStringList
&args
);
61 bool mDelayedInstanceCreation
;
62 bool mEventLoopReached
;
66 void KMailApplication::commitData(QSessionManager
&sm
)
68 kmkernel
->dumpDeadLetters();
69 kmkernel
->setShuttingDown(true); // Prevent further dumpDeadLetters calls
71 //KApplication::commitData(sm)
74 void KMailApplication::setEventLoopReached()
76 mEventLoopReached
= true;
79 int KMailApplication::activate(const QStringList
&args
)
83 // If the event loop hasn't been reached yet, the kernel is probably not
84 // fully initialized. Creating an instance would therefore fail, this is why
85 // that is delayed until delayedInstanceCreation() is called.
86 if (!mEventLoopReached
) {
87 qCDebug(KMAIL_LOG
) << "Delaying instance creation.";
88 mDelayedInstanceCreation
= true;
96 if (!kmkernel
->firstInstance() || !qApp
->isSessionRestored()) {
97 kmkernel
->handleCommandLine(true, args
);
99 kmkernel
->setFirstInstance(false);
103 void KMailApplication::delayedInstanceCreation(const QStringList
&args
)
105 if (mDelayedInstanceCreation
) {
110 int main(int argc
, char *argv
[])
112 KMailApplication
app(argc
, &argv
);
113 // WABA: KMail is a KUniqueApplication. Unfortunately this makes debugging
114 // a bit harder: You should pass --nofork as commandline argument when using
115 // a debugger. In gdb you can do this by typing "set args --nofork" before
117 #if 0 // for testing KUniqueAppliaction on Windows
119 QString("main() %1 pid=%2").arg(argv
[0]).arg(getpid()).toLatin1(),
120 QString("main() \"%1\"").arg(argv
[0]).toLatin1(), MB_OK
| MB_ICONINFORMATION
| MB_TASKMODAL
);
122 KMail::AboutData about
;
123 app
.setAboutData(about
);
125 QCommandLineParser
*cmdArgs
= app
.cmdArgs();
126 kmail_options(cmdArgs
);
128 const QStringList args
= QApplication::arguments();
129 cmdArgs
->process(args
);
130 about
.processCommandLine(cmdArgs
);
132 if (!KMailApplication::start(args
)) {
133 qCDebug(KMAIL_LOG
) << "Another instance of KMail already running";
137 KMMigrateApplication migrate
;
141 // import i18n data and icons from libraries:
142 KMail::insertLibraryCataloguesAndIcons();
145 KMKernel kmailKernel
;
148 // and session management
149 kmailKernel
.doSessionManagement();
152 kmailKernel
.recoverDeadLetters();
154 kmkernel
->setupDBus(); // Ok. We are ready for D-Bus requests.
156 //If the instance hasn't been created yet, do that now
157 app
.setEventLoopReached();
158 app
.delayedInstanceCreation(args
);
161 int ret
= qApp
->exec();
163 kmailKernel
.cleanup();