1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
/*
* Copyright 2013 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 warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, 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/>.
*
* Authors:
* Charles Kerr <charles.kerr@canonical.com>
*/
#include <datetime/clock.h>
#include <datetime/dbus-shared.h>
#include <glib.h>
#include <gio/gio.h>
#include <map>
#include <set>
#include <string>
#include <vector>
namespace unity {
namespace indicator {
namespace datetime {
/***
****
***/
class Clock::Impl
{
public:
Impl(Clock& owner):
m_owner(owner),
m_cancellable(g_cancellable_new())
{
g_bus_get(G_BUS_TYPE_SYSTEM, m_cancellable, on_bus_ready, this);
}
~Impl()
{
g_cancellable_cancel(m_cancellable);
g_object_unref(m_cancellable);
for(const auto& tag : m_watched_names)
g_bus_unwatch_name(tag);
}
private:
static void on_bus_ready(GObject * /*source_object*/,
GAsyncResult * res,
gpointer gself)
{
GError * error = NULL;
GDBusConnection * bus;
if ((bus = g_bus_get_finish(res, &error)))
{
auto self = static_cast<Impl*>(gself);
auto tag = g_bus_watch_name_on_connection(bus,
"org.freedesktop.login1",
G_BUS_NAME_WATCHER_FLAGS_NONE,
on_login1_appeared,
on_login1_vanished,
gself, nullptr);
self->m_watched_names.insert(tag);
tag = g_bus_watch_name_on_connection(bus,
BUS_POWERD_NAME,
G_BUS_NAME_WATCHER_FLAGS_NONE,
on_powerd_appeared,
on_powerd_vanished,
gself, nullptr);
self->m_watched_names.insert(tag);
g_object_unref(bus);
}
else if (error != nullptr)
{
if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
g_warning("%s Couldn't get system bus: %s", G_STRLOC, error->message);
g_error_free(error);
}
}
void remember_subscription(const std::string & name,
GDBusConnection * bus,
guint tag)
{
g_object_ref(bus);
auto deleter = [tag](GDBusConnection* bus){
g_dbus_connection_signal_unsubscribe(bus, tag);
g_object_unref(G_OBJECT(bus));
};
m_subscriptions[name].push_back(std::shared_ptr<GDBusConnection>(bus, deleter));
}
/**
*** DBus Chatter: org.freedesktop.login1
***
*** Fire Clock::minute_changed() signal on login1's PrepareForSleep signal
**/
static void on_login1_appeared(GDBusConnection * bus,
const gchar * name,
const gchar * name_owner,
gpointer gself)
{
auto tag = g_dbus_connection_signal_subscribe(bus,
name_owner,
"org.freedesktop.login1.Manager", // interface
"PrepareForSleep", // signal name
"/org/freedesktop/login1", // object path
nullptr, // arg0
G_DBUS_SIGNAL_FLAGS_NONE,
on_prepare_for_sleep,
gself,
nullptr);
static_cast<Impl*>(gself)->remember_subscription(name, bus, tag);
}
static void on_login1_vanished(GDBusConnection * /*system_bus*/,
const gchar * name,
gpointer gself)
{
static_cast<Impl*>(gself)->m_subscriptions[name].clear();
}
static void on_prepare_for_sleep(GDBusConnection* /*connection*/,
const gchar* /*sender_name*/,
const gchar* /*object_path*/,
const gchar* /*interface_name*/,
const gchar* /*signal_name*/,
GVariant* /*parameters*/,
gpointer gself)
{
g_debug("firing clock.minute_changed() due to PrepareForSleep");
static_cast<Impl*>(gself)->m_owner.minute_changed();
}
/**
*** DBus Chatter: com.canonical.powerd
***
*** Fire Clock::minute_changed() signal when powerd says the system's
*** has awoken from sleep -- the old timestamp is likely out-of-date
**/
static void on_powerd_appeared(GDBusConnection * bus,
const gchar * name,
const gchar * name_owner,
gpointer gself)
{
auto tag = g_dbus_connection_signal_subscribe(bus,
name_owner,
BUS_POWERD_INTERFACE,
"SysPowerStateChange",
BUS_POWERD_PATH,
nullptr, // arg0
G_DBUS_SIGNAL_FLAGS_NONE,
on_sys_power_state_change,
gself, // user_data
nullptr); // user_data closure
static_cast<Impl*>(gself)->remember_subscription(name, bus, tag);
}
static void on_powerd_vanished(GDBusConnection * /*bus*/,
const gchar * name,
gpointer gself)
{
static_cast<Impl*>(gself)->m_subscriptions[name].clear();
}
static void on_sys_power_state_change(GDBusConnection* /*connection*/,
const gchar* /*sender_name*/,
const gchar* /*object_path*/,
const gchar* /*interface_name*/,
const gchar* /*signal_name*/,
GVariant* /*parameters*/,
gpointer gself)
{
g_debug("firing clock.minute_changed() due to state change");
static_cast<Impl*>(gself)->m_owner.minute_changed();
}
/***
****
***/
Clock& m_owner;
GCancellable * m_cancellable = nullptr;
std::set<guint> m_watched_names;
std::map<std::string,std::vector<std::shared_ptr<GDBusConnection>>> m_subscriptions;
};
/***
****
***/
Clock::Clock():
m_impl(new Impl{*this})
{
}
Clock::~Clock()
{
}
/***
****
***/
} // namespace datetime
} // namespace indicator
} // namespace unity
|