Updated Russian translation
[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>
35
f05ba5af 36using namespace LXQt;
7e7ee62a 37
5ca9c65e 38SingleApplication::SingleApplication(int &argc, char **argv, StartOptions options)
7e7ee62a
LP
39 : Application(argc, argv),
40 mActivationWindow(0)
41{
42 QString service =
43 QString::fromLatin1("org.lxqt.%1").arg(QApplication::applicationName());
44
45 SingleApplicationAdaptor *mAdaptor = new SingleApplicationAdaptor(this);
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;
60 ::exit(1);
61 }
7e7ee62a
LP
62 }
63
64 bool registered = (bus.registerService(service) ==
65 QDBusConnectionInterface::ServiceRegistered);
66 if (registered) { // We are the primary instance
67 QLatin1String objectPath("/");
68 bus.registerObject(objectPath, mAdaptor,
69 QDBusConnection::ExportAllSlots);
70 } else { // We are the second outstance
71 QDBusMessage msg = QDBusMessage::createMethodCall(service,
72 QStringLiteral("/"),
73 QStringLiteral("org.lxqt.SingleApplication"),
74 QStringLiteral("activateWindow"));
75 QDBusConnection::sessionBus().send(msg);
76
77 ::exit(0);
78 }
79}
80
81SingleApplication::~SingleApplication()
82{
83}
84
85void SingleApplication::setActivationWindow(QWidget *w)
86{
87 mActivationWindow = w;
88}
89
90QWidget *SingleApplication::activationWindow() const
91{
92 return mActivationWindow;
93}
94
95void SingleApplication::activateWindow()
96{
97 if (mActivationWindow) {
98 WId window = mActivationWindow->effectiveWinId();
99
100 KWindowInfo info(window, NET::WMDesktop);
101 int windowDesktop = info.desktop();
102
103 if (windowDesktop != KWindowSystem::currentDesktop())
104 KWindowSystem::setCurrentDesktop(windowDesktop);
dac4466a 105 KWindowSystem::activateWindow(window);
7e7ee62a
LP
106 } else {
107 qDebug() << Q_FUNC_INFO << "activationWindow not set or null";
108 }
109}