main.cpp 5.82 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
 * kmail: KDE mail client
 * Copyright (c) 1996-1998 Stefan Taferner <taferner@kde.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 19
 *
 */
20

Laurent Montel's avatar
Laurent Montel committed
21
#include <kontactinterface/pimuniqueapplication.h>
Laurent Montel's avatar
Laurent Montel committed
22

23
#include "kmkernel.h" //control center
24
#include "kmail_options.h"
25
#include "kmmigrateapplication.h"
26

Laurent Montel's avatar
Laurent Montel committed
27
#include "kmail_debug.h"
28
#undef Status // stupid X headers
29

Marc Mutz's avatar
Marc Mutz committed
30
#include "aboutdata.h"
31

32
#include <QDir>
33
#include <QApplication>
34
#include <QSessionManager>
Laurent Montel's avatar
Laurent Montel committed
35
#include <KCrash>
Laurent Montel's avatar
Laurent Montel committed
36
#include <QWebEngineUrlScheme>
37

Laurent Montel's avatar
Laurent Montel committed
38 39 40
#ifdef WITH_KUSERFEEDBACK
#include <KUserFeedback/Provider>
#include "userfeedback/kmailuserfeedbackprovider.h"
Laurent Montel's avatar
Laurent Montel committed
41

Laurent Montel's avatar
Laurent Montel committed
42 43
#endif

44
//-----------------------------------------------------------------------------
Stefan Taferner's avatar
Stefan Taferner committed
45

Volker Krause's avatar
Volker Krause committed
46
class KMailApplication : public KontactInterface::PimUniqueApplication
47 48
{
public:
49 50
    KMailApplication(int &argc, char **argv[])
        : KontactInterface::PimUniqueApplication(argc, argv)
51 52
        , mDelayedInstanceCreation(false)
        , mEventLoopReached(false)
Laurent Montel's avatar
Laurent Montel committed
53 54
    {
    }
55

David Faure's avatar
David Faure committed
56
    int activate(const QStringList &args, const QString &workingDir) override;
Laurent Montel's avatar
Laurent Montel committed
57
    void commitData(QSessionManager &sm);
Laurent Montel's avatar
Laurent Montel committed
58
    void setEventLoopReached();
David Faure's avatar
David Faure committed
59
    void delayedInstanceCreation(const QStringList &args, const QString &workingDir);
60 61 62 63

public Q_SLOTS:
    int newInstance(const QByteArray &startupId, const QStringList &arguments, const QString &workingDirectory) override;

64
protected:
Laurent Montel's avatar
Laurent Montel committed
65 66
    bool mDelayedInstanceCreation;
    bool mEventLoopReached;
67 68
};

Laurent Montel's avatar
Laurent Montel committed
69
void KMailApplication::commitData(QSessionManager &)
Laurent Montel's avatar
Laurent Montel committed
70
{
Laurent Montel's avatar
Laurent Montel committed
71
    kmkernel->dumpDeadLetters();
Laurent Montel's avatar
Laurent Montel committed
72
    kmkernel->setShuttingDown(true);   // Prevent further dumpDeadLetters calls
Don Sanders's avatar
Don Sanders committed
73 74
}

Laurent Montel's avatar
Laurent Montel committed
75 76
void KMailApplication::setEventLoopReached()
{
Laurent Montel's avatar
Laurent Montel committed
77
    mEventLoopReached = true;
78
}
Don Sanders's avatar
Don Sanders committed
79

80 81 82 83 84 85 86 87 88 89 90
int KMailApplication::newInstance(const QByteArray &startupId, const QStringList &arguments, const QString &workingDirectory)
{
    if (!kmkernel->firstInstance() && !arguments.isEmpty()) {
        // if we're going to create a new window (viewer or composer),
        // don't bring the mainwindow onto the current desktop
        return activate(arguments, workingDirectory);
    } else {
        return PimUniqueApplication::newInstance(startupId, arguments, workingDirectory);
    }
}

David Faure's avatar
David Faure committed
91
int KMailApplication::activate(const QStringList &args, const QString &workingDir)
92
{
Laurent Montel's avatar
Laurent Montel committed
93 94 95
    // If the event loop hasn't been reached yet, the kernel is probably not
    // fully initialized. Creating an instance would therefore fail, this is why
    // that is delayed until delayedInstanceCreation() is called.
Laurent Montel's avatar
Laurent Montel committed
96
    if (!mEventLoopReached) {
Laurent Montel's avatar
Laurent Montel committed
97
        qCDebug(KMAIL_LOG) << "Delaying instance creation.";
Laurent Montel's avatar
Laurent Montel committed
98 99 100 101
        mDelayedInstanceCreation = true;
        return 0;
    }

Laurent Montel's avatar
Laurent Montel committed
102
    if (!kmkernel) {
Laurent Montel's avatar
Laurent Montel committed
103
        return 0;
Laurent Montel's avatar
Laurent Montel committed
104
    }
Laurent Montel's avatar
Laurent Montel committed
105
    if (kmkernel->shuttingDown()) {
106 107 108
        qCDebug(KMAIL_LOG) << "KMail is in a shutdown mode.";
        return 0;
    }
Laurent Montel's avatar
Laurent Montel committed
109

110
    if (!kmkernel->firstInstance() || !qApp->isSessionRestored()) {
David Faure's avatar
David Faure committed
111
        kmkernel->handleCommandLine(true, args, workingDir);
Laurent Montel's avatar
Laurent Montel committed
112
    }
Laurent Montel's avatar
Laurent Montel committed
113
    kmkernel->setFirstInstance(false);
114
    return 0;
115 116
}

David Faure's avatar
David Faure committed
117
void KMailApplication::delayedInstanceCreation(const QStringList &args, const QString &workingDir)
Laurent Montel's avatar
Laurent Montel committed
118 119
{
    if (mDelayedInstanceCreation) {
David Faure's avatar
David Faure committed
120
        activate(args, workingDir);
Laurent Montel's avatar
Laurent Montel committed
121
    }
122 123
}

124
int main(int argc, char *argv[])
125
{
Laurent Montel's avatar
Laurent Montel committed
126 127 128
    QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true);
    QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, true);
Laurent Montel's avatar
Laurent Montel committed
129 130 131 132 133 134
    //Necessary for "cid" support in kmail.
    QWebEngineUrlScheme cidScheme("cid");
    cidScheme.setFlags(QWebEngineUrlScheme::SecureScheme | QWebEngineUrlScheme::ContentSecurityPolicyIgnored);
    cidScheme.setSyntax(QWebEngineUrlScheme::Syntax::Path);
    QWebEngineUrlScheme::registerScheme(cidScheme);

135
    KMailApplication app(argc, &argv);
Laurent Montel's avatar
Laurent Montel committed
136
    KLocalizedString::setApplicationDomain("kmail");
137
    app.setWindowIcon(QIcon::fromTheme(QStringLiteral("kmail")));
Laurent Montel's avatar
Laurent Montel committed
138
    app.setDesktopFileName(QStringLiteral("org.kde.kmail2"));
Laurent Montel's avatar
Laurent Montel committed
139
    KCrash::initialize();
Laurent Montel's avatar
Laurent Montel committed
140
    KMail::AboutData about;
141
    app.setAboutData(about);
142

143 144
    QCommandLineParser *cmdArgs = app.cmdArgs();
    kmail_options(cmdArgs);
145

146 147 148
    const QStringList args = QApplication::arguments();
    cmdArgs->process(args);
    about.processCommandLine(cmdArgs);
149

150
#ifdef WITH_KUSERFEEDBACK
Laurent Montel's avatar
Laurent Montel committed
151
    if (cmdArgs->isSet(QStringLiteral("feedback"))) {
Laurent Montel's avatar
Laurent Montel committed
152 153 154
        KMailUserFeedbackProvider *userFeedback = new KMailUserFeedbackProvider(nullptr);
        QTextStream(stdout) << userFeedback->describeDataSources() << '\n';
        delete userFeedback;
155 156 157 158
        return 0;
    }
#endif

159 160
    if (!KMailApplication::start(args)) {
        qCDebug(KMAIL_LOG) << "Another instance of KMail already running";
Laurent Montel's avatar
Laurent Montel committed
161
        return 0;
Laurent Montel's avatar
Laurent Montel committed
162
    }
163

164 165
    KMMigrateApplication migrate;
    migrate.migrate();
Laurent Montel's avatar
Laurent Montel committed
166

Laurent Montel's avatar
Laurent Montel committed
167 168 169
    //local, do the init
    KMKernel kmailKernel;
    kmailKernel.init();
170

Laurent Montel's avatar
Laurent Montel committed
171 172
    // and session management
    kmailKernel.doSessionManagement();
173

Laurent Montel's avatar
Laurent Montel committed
174 175
    // any dead letters?
    kmailKernel.recoverDeadLetters();
176

Laurent Montel's avatar
Laurent Montel committed
177
    kmkernel->setupDBus(); // Ok. We are ready for D-Bus requests.
178

Laurent Montel's avatar
Laurent Montel committed
179 180
    //If the instance hasn't been created yet, do that now
    app.setEventLoopReached();
181
    app.delayedInstanceCreation(args, QDir::currentPath());
182

Laurent Montel's avatar
Laurent Montel committed
183 184 185 186 187
    // Go!
    int ret = qApp->exec();
    // clean up
    kmailKernel.cleanup();
    return ret;
188
}