clazy: fix QString(QLatin1String(...))
[trojita.git] / src / IPC / DBusInterface.cpp
blob2e9772d4f5d9f776959971ba788a632403fdd73c
1 /* Copyright (C) 2013 Pali Rohár <pali.rohar@gmail.com>
3 This file is part of the Trojita Qt IMAP e-mail client,
4 http://trojita.flaska.net/
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of
9 the License or (at your option) version 3 or any later version
10 accepted by the membership of KDE e.V. (or its successor approved
11 by the membership of KDE e.V.), which shall act as a proxy
12 defined in Section 14 of version 3 of the license.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <QDBusConnection>
24 #include <QDBusConnectionInterface>
25 #include <QDBusInterface>
27 #include "MainWindowBridge.h"
29 #include "IPC.h"
31 namespace IPC {
33 static QString path = QStringLiteral("/Trojita");
35 QString service()
37 QString str = QStringLiteral("net.flaska.trojita");
38 QString profileName = QString::fromUtf8(qgetenv("TROJITA_PROFILE"));
39 return profileName.isEmpty() ? str : str + QLatin1Char('-') + profileName;
42 namespace Instance {
44 static QDBusInterface &interface()
46 static QDBusInterface iface(service(), path, QStringLiteral("net.flaska.trojita"));
47 return iface;
50 bool isRunning()
52 return QDBusConnection::sessionBus().isConnected() &&
53 QDBusConnection::sessionBus().interface()->isServiceRegistered(service());
56 void showMainWindow()
58 interface().call(QDBus::NoBlock, QStringLiteral("showMainWindow"));
61 void showAddressbookWindow()
63 interface().call(QDBus::NoBlock, QStringLiteral("showAddressbookWindow"));
66 void composeMail(const QString &url)
68 interface().call(QDBus::NoBlock, QStringLiteral("composeMail"), url);
71 } //namespace Instance
73 bool registerInstance(Gui::MainWindow *window, QString &error)
75 QDBusConnection conn = QDBusConnection::sessionBus();
76 if (!conn.isConnected()) {
77 error = conn.lastError().message();
78 return false;
81 bool serviceOk = conn.registerService(service());
82 if (!serviceOk) {
83 error = conn.lastError().message();
84 return false;
87 MainWindowBridge *object = new MainWindowBridge(window);
88 bool objectOk = conn.registerObject(path, object, QDBusConnection::ExportAllSlots);
89 if (!objectOk) {
90 error = conn.lastError().message();
91 delete object;
92 conn.unregisterService(service());
93 return false;
96 return true;
99 } //namespace IPC