Fix wrong connect
[kdepim.git] / kmail / main.cpp
blobeb6a6115d0f97d8aef2b9712697b3710df0907a3
1 /*
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"
36 #ifdef Q_OS_WIN
37 #include <unistd.h>
38 #include <windows.h>
39 #endif
41 #include <QDir>
42 #include <QApplication>
43 #include <QSessionManager>
45 //-----------------------------------------------------------------------------
47 class KMailApplication : public KontactInterface::PimUniqueApplication
49 public:
50 KMailApplication(int &argc, char **argv[])
51 : KontactInterface::PimUniqueApplication(argc, argv)
52 , mDelayedInstanceCreation(false)
53 , mEventLoopReached(false)
54 { }
56 int activate(const QStringList &args) Q_DECL_OVERRIDE;
57 void commitData(QSessionManager &sm);
58 void setEventLoopReached();
59 void delayedInstanceCreation(const QStringList &args);
60 protected:
61 bool mDelayedInstanceCreation;
62 bool mEventLoopReached;
66 void KMailApplication::commitData(QSessionManager &sm)
68 kmkernel->dumpDeadLetters();
69 kmkernel->setShuttingDown(true); // Prevent further dumpDeadLetters calls
70 #warning KF5 PORTME
71 //KApplication::commitData(sm)
74 void KMailApplication::setEventLoopReached()
76 mEventLoopReached = true;
79 int KMailApplication::activate(const QStringList &args)
81 qCDebug(KMAIL_LOG);
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;
89 return 0;
92 if (!kmkernel) {
93 return 0;
96 if (!kmkernel->firstInstance() || !qApp->isSessionRestored()) {
97 kmkernel->handleCommandLine(true, args);
99 kmkernel->setFirstInstance(false);
100 return 0;
103 void KMailApplication::delayedInstanceCreation(const QStringList &args)
105 if (mDelayedInstanceCreation) {
106 activate(args);
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
116 // typing "run".
117 #if 0 // for testing KUniqueAppliaction on Windows
118 MessageBoxA(NULL,
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);
121 #endif
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";
134 return 0;
137 KMMigrateApplication migrate;
138 migrate.migrate();
141 // import i18n data and icons from libraries:
142 KMail::insertLibraryCataloguesAndIcons();
144 //local, do the init
145 KMKernel kmailKernel;
146 kmailKernel.init();
148 // and session management
149 kmailKernel.doSessionManagement();
151 // any dead letters?
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);
160 // Go!
161 int ret = qApp->exec();
162 // clean up
163 kmailKernel.cleanup();
164 return ret;