Unity 8
URLDispatcher.cpp
1 /*
2  * Copyright (C) 2016 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #include "URLDispatcher.h"
18 
19 #include <QDBusConnection>
20 
21 class URLDispatcherInterface : public QObject
22 {
23  Q_OBJECT
24  Q_CLASSINFO("D-Bus Interface", "com.canonical.URLDispatcher")
25 
26 public:
27  explicit URLDispatcherInterface(URLDispatcher *parent);
28 
29  Q_SCRIPTABLE void DispatchURL(const QString &url, const QString &package);
30 };
31 
32 URLDispatcherInterface::URLDispatcherInterface(URLDispatcher *parent)
33  : QObject(parent)
34 {
35 }
36 
37 void URLDispatcherInterface::DispatchURL(const QString &url, const QString &package)
38 {
39  Q_UNUSED(package);
40  Q_EMIT static_cast<URLDispatcher *>(parent())->urlRequested(url);
41 }
42 
43 URLDispatcher::URLDispatcher(QObject *parent)
44  : QObject(parent)
45  , m_dispatcher(nullptr)
46 {
47 }
48 
49 bool URLDispatcher::active() const
50 {
51  return m_dispatcher != nullptr;
52 }
53 
54 void URLDispatcher::setActive(bool value)
55 {
56  if (value == active())
57  return;
58 
59  QDBusConnection connection = QDBusConnection::sessionBus();
60 
61  if (value) {
62  URLDispatcherInterface *dispatcher = new URLDispatcherInterface(this);
63  connection.registerObject(QStringLiteral("/com/canonical/URLDispatcher"),
64  dispatcher,
65  QDBusConnection::ExportScriptableContents);
66  connection.registerService(QStringLiteral("com.canonical.URLDispatcher"));
67  m_dispatcher = dispatcher;
68  } else {
69  connection.unregisterService(QStringLiteral("com.canonical.URLDispatcher"));
70  delete m_dispatcher;
71  m_dispatcher = nullptr;
72  }
73 
74  Q_EMIT activeChanged();
75 }
76 
77 #include "URLDispatcher.moc"