90a351bc1c7f3b296004b6205573b3842d929f38
[lxde/liblxqt.git] / lxqtsingleapplication.cpp
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"
30 #include <KWindowSystem/KWindowSystem>
31 #include <KWindowSystem/NETWM>
32 #include <QDBusMessage>
33 #include <QWidget>
34 #include <QDebug>
35 #include <QTimer>
36
37 using namespace LXQt;
38
39 SingleApplication::SingleApplication(int &argc, char **argv, StartOptions options)
40 : Application(argc, argv),
41 mActivationWindow(0)
42 {
43 QString service =
44 QString::fromLatin1("org.lxqt.%1").arg(QApplication::applicationName());
45
46 SingleApplicationAdaptor *mAdaptor = new SingleApplicationAdaptor(this);
47 QDBusConnection bus = QDBusConnection::sessionBus();
48
49 if (!bus.isConnected()) {
50 QLatin1String errorMessage("Can't connect to the D-Bus session bus\n"
51 "Make sure the D-Bus daemon is running");
52
53 /* ExitOnDBusFailure is the default. Any value other than
54 NoExitOnDBusFailure will be taken as ExitOnDBusFailure (the default).
55 */
56 if (options == NoExitOnDBusFailure) {
57 qDebug() << Q_FUNC_INFO << errorMessage;
58 return;
59 } else {
60 qCritical() << Q_FUNC_INFO << errorMessage;
61 QTimer::singleShot(0, [this] { exit(1); });
62 }
63 }
64
65 bool registered = (bus.registerService(service) ==
66 QDBusConnectionInterface::ServiceRegistered);
67 if (registered) { // We are the primary instance
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
78 QTimer::singleShot(0, [this] { exit(0); });
79 }
80 }
81
82 SingleApplication::~SingleApplication()
83 {
84 }
85
86 void SingleApplication::setActivationWindow(QWidget *w)
87 {
88 mActivationWindow = w;
89 }
90
91 QWidget *SingleApplication::activationWindow() const
92 {
93 return mActivationWindow;
94 }
95
96 void SingleApplication::activateWindow()
97 {
98 if (mActivationWindow) {
99 mActivationWindow->show();
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);
107 KWindowSystem::activateWindow(window);
108 } else {
109 qDebug() << Q_FUNC_INFO << "activationWindow not set or null";
110 }
111 }