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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/*
* Copyright 2013 Canonical Ltd.
*
* Authors:
* Charles Kerr <charles.kerr@canonical.com>
*
* 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/>.
*/
#include <glib.h>
#include <gio/gio.h>
#include "config.h"
#include "clock-live.h"
#include "settings-shared.h"
#include "timezone-file.h"
#include "timezone-geoclue.h"
/***
**** private struct
***/
struct _IndicatorDatetimeClockLivePriv
{
GSettings * settings;
IndicatorDatetimeTimezone * tz_file;
IndicatorDatetimeTimezone * tz_geoclue;
gchar ** timezones;
};
typedef IndicatorDatetimeClockLivePriv priv_t;
/***
**** GObject boilerplate
***/
static void indicator_datetime_clock_interface_init (
IndicatorDatetimeClockInterface * iface);
G_DEFINE_TYPE_WITH_CODE (
IndicatorDatetimeClockLive,
indicator_datetime_clock_live,
G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (INDICATOR_TYPE_DATETIME_CLOCK,
indicator_datetime_clock_interface_init));
/***
**** Timezones
***/
static void
on_current_timezone_changed (IndicatorDatetimeClockLive * self)
{
g_clear_pointer (&self->priv->timezones, g_strfreev);
indicator_datetime_clock_emit_changed (INDICATOR_DATETIME_CLOCK (self));
}
static void
set_detect_location_enabled (IndicatorDatetimeClockLive * self, gboolean enabled)
{
priv_t * p = self->priv;
gboolean changed = FALSE;
/* geoclue */
if (!p->tz_geoclue && enabled)
{
p->tz_geoclue = indicator_datetime_timezone_geoclue_new ();
g_signal_connect_swapped (p->tz_geoclue, "notify::timezone",
G_CALLBACK(on_current_timezone_changed),
self);
changed = TRUE;
}
else if (p->tz_geoclue && !enabled)
{
g_signal_handlers_disconnect_by_func (p->tz_geoclue,
on_current_timezone_changed,
self);
g_clear_object (&p->tz_geoclue);
changed = TRUE;
}
/* timezone file */
if (!p->tz_file && enabled)
{
p->tz_file = indicator_datetime_timezone_file_new (TIMEZONE_FILE);
g_signal_connect_swapped (p->tz_file, "notify::timezone",
G_CALLBACK(on_current_timezone_changed),
self);
changed = TRUE;
}
else if (p->tz_file && !enabled)
{
g_signal_handlers_disconnect_by_func (p->tz_file,
on_current_timezone_changed,
self);
g_clear_object (&p->tz_file);
changed = TRUE;
}
if (changed)
on_current_timezone_changed (self);
}
/* When the 'auto-detect timezone' boolean setting changes,
start or stop watching geoclue and /etc/timezone */
static void
on_detect_location_changed (IndicatorDatetimeClockLive * self)
{
const gboolean enabled = g_settings_get_boolean (self->priv->settings, SETTINGS_SHOW_DETECTED_S);
set_detect_location_enabled (self, enabled);
}
/***
**** IndicatorDatetimeClock virtual functions
***/
static void
rebuild_timezone_strv (IndicatorDatetimeClockLive * self)
{
priv_t * p;
GHashTable * hash;
int i;
GHashTableIter iter;
gpointer key;
p = self->priv;
hash = g_hash_table_new (g_str_hash, g_str_equal);
if (p->tz_file != NULL)
{
const gchar * tz = indicator_datetime_timezone_get_timezone (p->tz_file);
if (tz && *tz)
g_hash_table_add (hash, (gpointer) tz);
}
if (p->tz_geoclue != NULL)
{
const gchar * tz = indicator_datetime_timezone_get_timezone (p->tz_geoclue);
if (tz && *tz)
g_hash_table_add (hash, (gpointer) tz);
}
g_strfreev (p->timezones);
p->timezones = g_new0 (gchar*, g_hash_table_size(hash) + 1);
i = 0;
g_hash_table_iter_init (&iter, hash);
while (g_hash_table_iter_next (&iter, &key, NULL))
p->timezones[i++] = g_strdup (key);
g_hash_table_unref (hash);
}
static const gchar **
my_get_timezones (IndicatorDatetimeClock * clock)
{
IndicatorDatetimeClockLive * self = INDICATOR_DATETIME_CLOCK_LIVE (clock);
priv_t * p = self->priv;
if (p->timezones == NULL)
rebuild_timezone_strv (self);
return (const gchar **) p->timezones;
}
static GDateTime *
my_get_localtime (IndicatorDatetimeClock * clock)
{
const gchar ** zones = my_get_timezones (clock);
GTimeZone * zone = g_time_zone_new (zones ? zones[0] : NULL);
GDateTime * time = g_date_time_new_now (zone);
g_time_zone_unref (zone);
return time;
}
/***
**** GObject virtual functions
***/
static void
my_dispose (GObject * o)
{
IndicatorDatetimeClockLive * self;
priv_t * p;
self = INDICATOR_DATETIME_CLOCK_LIVE(o);
p = self->priv;
if (p->settings != NULL)
{
g_signal_handlers_disconnect_by_data (p->settings, self);
g_clear_object (&p->settings);
}
set_detect_location_enabled (self, FALSE);
G_OBJECT_CLASS (indicator_datetime_clock_live_parent_class)->dispose (o);
}
static void
my_finalize (GObject * o)
{
IndicatorDatetimeClockLive * self;
priv_t * p;
self = INDICATOR_DATETIME_CLOCK_LIVE(o);
p = self->priv;
g_strfreev (p->timezones);
G_OBJECT_CLASS (indicator_datetime_clock_live_parent_class)->dispose (o);
}
/***
**** Instantiation
***/
static void
indicator_datetime_clock_live_class_init (IndicatorDatetimeClockLiveClass * klass)
{
GObjectClass * object_class = G_OBJECT_CLASS (klass);
object_class->dispose = my_dispose;
object_class->finalize = my_finalize;
g_type_class_add_private (klass,
sizeof (IndicatorDatetimeClockLivePriv));
}
static void
indicator_datetime_clock_interface_init (IndicatorDatetimeClockInterface * iface)
{
iface->get_localtime = my_get_localtime;
iface->get_timezones = my_get_timezones;
}
static void
indicator_datetime_clock_live_init (IndicatorDatetimeClockLive * self)
{
IndicatorDatetimeClockLivePriv * p;
p = G_TYPE_INSTANCE_GET_PRIVATE (self,
INDICATOR_TYPE_DATETIME_CLOCK_LIVE,
IndicatorDatetimeClockLivePriv);
self->priv = p;
p->settings = g_settings_new (SETTINGS_INTERFACE);
g_signal_connect (p->settings, "changed::" SETTINGS_SHOW_DETECTED_S,
G_CALLBACK(on_detect_location_changed), self);
on_detect_location_changed (self);
}
/***
**** Public API
***/
IndicatorDatetimeClock *
indicator_datetime_clock_live_new (void)
{
gpointer o = g_object_new (INDICATOR_TYPE_DATETIME_CLOCK_LIVE, NULL);
return INDICATOR_DATETIME_CLOCK (o);
}
|