| 1 |
/* |
| 2 |
* Copyright © 2017 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software: you can redistribute it and/or modify it |
| 5 |
* under the terms of the GNU General Public License version 3, as published |
| 6 |
* by the Free Software Foundation. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, but |
| 9 |
* WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
* PURPOSE. See the GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License along |
| 14 |
* with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
* |
| 16 |
* Authors: |
| 17 |
* Pete Woods <[email protected]> |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <gio/gio.h> |
| 21 |
#include <memory> |
| 22 |
#include <lomiri/util/ResourcePtr.h> |
| 23 |
|
| 24 |
#pragma once |
| 25 |
#pragma GCC visibility push(default) |
| 26 |
|
| 27 |
namespace lomiri |
| 28 |
{ |
| 29 |
namespace app_launch |
| 30 |
{ |
| 31 |
|
| 32 |
struct DBusSignalUnsubscriber |
| 33 |
{ |
| 34 |
std::shared_ptr<GDBusConnection> bus_; |
| 35 |
|
| 36 |
void operator()(guint handle) |
| 37 |
{ |
| 38 |
if (handle != 0 && G_IS_OBJECT(bus_.get())) |
| 39 |
{ |
| 40 |
g_dbus_connection_signal_unsubscribe(bus_.get(), handle); |
| 41 |
} |
| 42 |
} |
| 43 |
}; |
| 44 |
|
| 45 |
typedef lomiri::util::ResourcePtr<guint, DBusSignalUnsubscriber> ManagedDBusSignalConnection; |
| 46 |
|
| 47 |
inline ManagedDBusSignalConnection managedDBusSignalConnection(guint id, std::shared_ptr<GDBusConnection> bus) |
| 48 |
{ |
| 49 |
return ManagedDBusSignalConnection(id, DBusSignalUnsubscriber{bus}); |
| 50 |
} |
| 51 |
|
| 52 |
template <typename T> |
| 53 |
struct SignalUnsubscriber |
| 54 |
{ |
| 55 |
std::shared_ptr<T> obj_; |
| 56 |
|
| 57 |
void operator()(gulong handle) |
| 58 |
{ |
| 59 |
if (handle != 0 && G_IS_OBJECT(obj_.get())) |
| 60 |
{ |
| 61 |
g_signal_handler_disconnect(obj_.get(), handle); |
| 62 |
} |
| 63 |
} |
| 64 |
}; |
| 65 |
|
| 66 |
template <typename T> |
| 67 |
using ManagedSignalConnection = lomiri::util::ResourcePtr<gulong, SignalUnsubscriber<T>>; |
| 68 |
|
| 69 |
template <typename T> |
| 70 |
inline ManagedSignalConnection<T> managedSignalConnection(gulong id, std::shared_ptr<T> obj) |
| 71 |
{ |
| 72 |
return ManagedSignalConnection<T>(id, SignalUnsubscriber<T>{obj}); |
| 73 |
} |
| 74 |
|
| 75 |
} // namespace app_launch |
| 76 |
} // namespace lomiri |
| 77 |
|
| 78 |
#pragma GCC visibility pop |