SingleApplication: Only create an DBus adaptor when needed
[lxde/liblxqt.git] / lxqtsingleapplication.cpp
CommitLineData
7e7ee62a
LP
1/* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 *
4 * LXQt - a lightweight, Qt based, desktop toolset
5 * http://lxqt.org
6 *
7 * Copyright: 2014 LXQt team
8 * Authors:
9 * Luís Pereira <luis.artur.pereira@gmail.com>
10 *
11 * This program or library is free software; you can redistribute it
12 * and/or modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20
21 * You should have received a copy of the GNU Lesser General
22 * Public License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301 USA
25 *
26 * END_COMMON_COPYRIGHT_HEADER */
27
28#include "lxqtsingleapplication.h"
29#include "singleapplicationadaptor.h"
a8942b21
HCC
30#include <KWindowSystem/KWindowSystem>
31#include <KWindowSystem/NETWM>
7e7ee62a
LP
32#include <QDBusMessage>
33#include <QWidget>
34#include <QDebug>
cd148a4a 35#include <QTimer>
7e7ee62a 36
f05ba5af 37using namespace LXQt;
7e7ee62a 38
5ca9c65e 39SingleApplication::SingleApplication(int &argc, char **argv, StartOptions options)
7e7ee62a
LP
40 : Application(argc, argv),
41 mActivationWindow(0)
42{
43 QString service =
44 QString::fromLatin1("org.lxqt.%1").arg(QApplication::applicationName());
45
7e7ee62a
LP
46 QDBusConnection bus = QDBusConnection::sessionBus();
47
48 if (!bus.isConnected()) {
49 QLatin1String errorMessage("Can't connect to the D-Bus session bus\n"
50 "Make sure the D-Bus daemon is running");
5ca9c65e
LP
51
52 /* ExitOnDBusFailure is the default. Any value other than
53 NoExitOnDBusFailure will be taken as ExitOnDBusFailure (the default).
54 */
55 if (options == NoExitOnDBusFailure) {
56 qDebug() << Q_FUNC_INFO << errorMessage;
57 return;
58 } else {
59 qCritical() << Q_FUNC_INFO << errorMessage;
cd148a4a 60 QTimer::singleShot(0, [this] { exit(1); });
5ca9c65e 61 }
7e7ee62a
LP
62 }
63
64 bool registered = (bus.registerService(service) ==
65 QDBusConnectionInterface::ServiceRegistered);
66 if (registered) { // We are the primary instance
add338c8 67 SingleApplicationAdaptor *mAdaptor = new SingleApplicationAdaptor(this);
7e7ee62a
LP
68 QLatin1String objectPath("/");
69 bus.registerObject(objectPath, mAdaptor,
70 QDBusConnection::ExportAllSlots);
71 } else { // We are the second outstance
72 QDBusMessage msg = QDBusMessage::createMethodCall(service,
73 QStringLiteral("/"),
74 QStringLiteral("org.lxqt.SingleApplication"),
75 QStringLiteral("activateWindow"));
76 QDBusConnection::sessionBus().send(msg);
77
cd148a4a 78 QTimer::singleShot(0, [this] { exit(0); });
7e7ee62a
LP
79 }
80}
81
82SingleApplication::~SingleApplication()
83{
84}
85
86void SingleApplication::setActivationWindow(QWidget *w)
87{
88 mActivationWindow = w;
89}
90
91QWidget *SingleApplication::activationWindow() const
92{
93 return mActivationWindow;
94}
95
96void SingleApplication::activateWindow()
97{
98 if (mActivationWindow) {
d5cd5a23 99 mActivationWindow->show();
7e7ee62a
LP
100 WId window = mActivationWindow->effectiveWinId();
101
102 KWindowInfo info(window, NET::WMDesktop);
103 int windowDesktop = info.desktop();
104
105 if (windowDesktop != KWindowSystem::currentDesktop())
106 KWindowSystem::setCurrentDesktop(windowDesktop);
dac4466a 107 KWindowSystem::activateWindow(window);
7e7ee62a
LP
108 } else {
109 qDebug() << Q_FUNC_INFO << "activationWindow not set or null";
110 }
111}