summaryrefslogtreecommitdiff
path: root/UnityCore
diff options
Diffstat (limited to 'UnityCore')
-rw-r--r--UnityCore/CMakeLists.txt2
-rw-r--r--UnityCore/GLibDBusNameWatcher.cpp44
-rw-r--r--UnityCore/GLibDBusNameWatcher.h54
3 files changed, 100 insertions, 0 deletions
diff --git a/UnityCore/CMakeLists.txt b/UnityCore/CMakeLists.txt
index 92191c0d5..f055790d3 100644
--- a/UnityCore/CMakeLists.txt
+++ b/UnityCore/CMakeLists.txt
@@ -25,6 +25,7 @@ set (CORE_HEADERS
Filter.h
Filters.h
GenericPreview.h
+ GLibDBusNameWatcher.h
GLibDBusProxy.h
GLibDBusServer.h
GLibSignal.h
@@ -80,6 +81,7 @@ set (CORE_SOURCES
Filter.cpp
Filters.cpp
GenericPreview.cpp
+ GLibDBusNameWatcher.cpp
GLibDBusProxy.cpp
GLibDBusServer.cpp
GLibSignal.cpp
diff --git a/UnityCore/GLibDBusNameWatcher.cpp b/UnityCore/GLibDBusNameWatcher.cpp
new file mode 100644
index 000000000..92f311210
--- /dev/null
+++ b/UnityCore/GLibDBusNameWatcher.cpp
@@ -0,0 +1,44 @@
+// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
+/*
+ * Copyright (C) 2015 Canonical Ltd
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authored by: Marco Trevisan (Treviño) <marco.trevisan@canonical.com>
+ */
+
+#include "GLibDBusNameWatcher.h"
+#include "GLibWrapper.h"
+
+namespace unity
+{
+namespace glib
+{
+
+DBusNameWatcher::DBusNameWatcher(std::string const& name, GBusType bus_type, GBusNameWatcherFlags flags)
+ : watcher_id_(g_bus_watch_name(bus_type, name.c_str(), flags,
+ [] (GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer self) {
+ static_cast<DBusNameWatcher*>(self)->appeared.emit(gchar_to_string(name), gchar_to_string(name_owner));
+ },
+ [] (GDBusConnection *connection, const gchar *name, gpointer self) {
+ static_cast<DBusNameWatcher*>(self)->vanished.emit(gchar_to_string(name));
+ }, this, nullptr))
+{}
+
+DBusNameWatcher::~DBusNameWatcher()
+{
+ g_bus_unwatch_name(watcher_id_);
+}
+
+} // namespace glib
+} // namespace unity
diff --git a/UnityCore/GLibDBusNameWatcher.h b/UnityCore/GLibDBusNameWatcher.h
new file mode 100644
index 000000000..b5901d3d4
--- /dev/null
+++ b/UnityCore/GLibDBusNameWatcher.h
@@ -0,0 +1,54 @@
+// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
+/*
+ * Copyright (C) 2015 Canonical Ltd
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authored by: Marco Trevisan (Treviño) <marco.trevisan@canonical.com>
+ */
+
+#ifndef UNITY_GLIB_DBUS_NAME_WATCHER_H
+#define UNITY_GLIB_DBUS_NAME_WATCHER_H
+
+#include <gio/gio.h>
+#include <memory>
+#include <sigc++/signal.h>
+
+namespace unity
+{
+namespace glib
+{
+
+class DBusNameWatcher
+{
+public:
+ typedef std::shared_ptr<DBusNameWatcher> Ptr;
+
+ DBusNameWatcher(std::string const& name, GBusType bus_type = G_BUS_TYPE_SESSION, GBusNameWatcherFlags flags = G_BUS_NAME_WATCHER_FLAGS_NONE);
+ virtual ~DBusNameWatcher();
+
+ sigc::signal<void, std::string const& /* name*/, std::string const& /* name owner*/> appeared;
+ sigc::signal<void, std::string const& /* name */> vanished;
+
+private:
+ // not copyable class
+ DBusNameWatcher(DBusNameWatcher const&) = delete;
+ DBusNameWatcher& operator=(DBusNameWatcher const&) = delete;
+
+ uint32_t watcher_id_;
+};
+
+} // namespace glib
+} // namespace unity
+
+#endif //UNITY_GLIB_DBUS_NAME_WATCHER_H