From a3021efe5f0b9b1a03d7e0e3f1ab1aeaebcf28ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Wed, 30 Sep 2015 01:57:40 +0200 Subject: GLibDBusNameWatcher: new wrapper class for monitoring DBus names (bzr r4008.7.10) --- UnityCore/CMakeLists.txt | 2 ++ UnityCore/GLibDBusNameWatcher.cpp | 44 +++++++++++++++++++++++++++++++ UnityCore/GLibDBusNameWatcher.h | 54 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 UnityCore/GLibDBusNameWatcher.cpp create mode 100644 UnityCore/GLibDBusNameWatcher.h (limited to 'UnityCore') 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 . + * + * Authored by: Marco Trevisan (Treviño) + */ + +#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(self)->appeared.emit(gchar_to_string(name), gchar_to_string(name_owner)); + }, + [] (GDBusConnection *connection, const gchar *name, gpointer self) { + static_cast(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 . + * + * Authored by: Marco Trevisan (Treviño) + */ + +#ifndef UNITY_GLIB_DBUS_NAME_WATCHER_H +#define UNITY_GLIB_DBUS_NAME_WATCHER_H + +#include +#include +#include + +namespace unity +{ +namespace glib +{ + +class DBusNameWatcher +{ +public: + typedef std::shared_ptr 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 appeared; + sigc::signal 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 -- cgit v1.2.3