Line |
Branch |
Exec |
Source |
1 |
|
|
/* -*- mode: c; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 |
|
|
/* |
3 |
|
|
* Copyright 2022 Purism SPC |
4 |
|
|
* Copyright 2022 Mohammed Sadiq <sadiq@sadiqpk.org> |
5 |
|
|
* |
6 |
|
|
* This program is free software: you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation, either version 3 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 |
|
|
* |
19 |
|
|
* Author(s): |
20 |
|
|
* Mohammed Sadiq <sadiq@sadiqpk.org> |
21 |
|
|
* |
22 |
|
|
* SPDX-License-Identifier: GPL-3.0-or-later |
23 |
|
|
*/ |
24 |
|
|
|
25 |
|
|
#undef G_LOG_DOMAIN |
26 |
|
|
#define G_LOG_DOMAIN "cc-tz-item" |
27 |
|
|
|
28 |
|
|
#ifdef HAVE_CONFIG_H |
29 |
|
|
# include "config.h" |
30 |
|
|
#endif |
31 |
|
|
|
32 |
|
|
#include <glib/gi18n.h> |
33 |
|
|
#define GNOME_DESKTOP_USE_UNSTABLE_API |
34 |
|
|
#include <libgnome-desktop/gnome-languages.h> |
35 |
|
|
#include <libgnome-desktop/gnome-wall-clock.h> |
36 |
|
|
|
37 |
|
|
#include "cc-tz-item.h" |
38 |
|
|
|
39 |
|
|
#define DEFAULT_TZ "Europe/London" |
40 |
|
|
#define GETTEXT_PACKAGE_TIMEZONES GETTEXT_PACKAGE "-timezones" |
41 |
|
|
|
42 |
|
|
struct _CcTzItem |
43 |
|
|
{ |
44 |
|
|
GObject parent_instance; |
45 |
|
|
|
46 |
|
|
GSettings *desktop_settings; |
47 |
|
|
GTimeZone *tz; |
48 |
|
|
GnomeWallClock *wall_clock; |
49 |
|
|
|
50 |
|
|
TzLocation *tz_location; |
51 |
|
|
TzInfo *tz_info; |
52 |
|
|
|
53 |
|
|
char *name; |
54 |
|
|
char *country; |
55 |
|
|
char *time; |
56 |
|
|
char *offset; /* eg: UTC+530 */ |
57 |
|
|
char *zone; |
58 |
|
|
}; |
59 |
|
|
|
60 |
|
✗ |
G_DEFINE_TYPE (CcTzItem, cc_tz_item, G_TYPE_OBJECT) |
61 |
|
|
|
62 |
|
|
enum { |
63 |
|
|
PROP_0, |
64 |
|
|
PROP_COUNTRY, |
65 |
|
|
PROP_NAME, |
66 |
|
|
PROP_OFFSET, |
67 |
|
|
PROP_TIME, |
68 |
|
|
PROP_ZONE, |
69 |
|
|
N_PROPS |
70 |
|
|
}; |
71 |
|
|
|
72 |
|
|
static GParamSpec *properties[N_PROPS]; |
73 |
|
|
|
74 |
|
|
/* Adapted from cc-datetime-panel.c */ |
75 |
|
|
static void |
76 |
|
✗ |
generate_city_name (CcTzItem *self, |
77 |
|
|
TzLocation *loc) |
78 |
|
|
{ |
79 |
|
✗ |
g_auto(GStrv) split_translated = NULL; |
80 |
|
|
gint length; |
81 |
|
|
|
82 |
|
|
/* Load the translation for it */ |
83 |
|
✗ |
self->zone = g_strdup (dgettext (GETTEXT_PACKAGE_TIMEZONES, loc->zone)); |
84 |
|
✗ |
g_strdelimit (self->zone, "_", ' '); |
85 |
|
✗ |
split_translated = g_regex_split_simple ("[\\x{2044}\\x{2215}\\x{29f8}\\x{ff0f}/]", |
86 |
|
✗ |
self->zone, |
87 |
|
|
0, 0); |
88 |
|
|
|
89 |
|
✗ |
length = g_strv_length (split_translated); |
90 |
|
✗ |
self->country = gnome_get_country_from_code (loc->country, NULL); |
91 |
|
✗ |
self->name = g_strdup (split_translated[length-1]); |
92 |
|
✗ |
} |
93 |
|
|
|
94 |
|
|
static const char * |
95 |
|
✗ |
tz_item_get_time (CcTzItem *self) |
96 |
|
|
{ |
97 |
|
✗ |
g_autoptr(GDateTime) now = NULL; |
98 |
|
|
GDesktopClockFormat format; |
99 |
|
|
|
100 |
|
✗ |
g_assert (CC_IS_TZ_ITEM (self)); |
101 |
|
|
|
102 |
|
✗ |
if (self->time) |
103 |
|
✗ |
return self->time; |
104 |
|
|
|
105 |
|
✗ |
now = g_date_time_new_now (self->tz); |
106 |
|
✗ |
format = g_settings_get_enum (self->desktop_settings, "clock-format"); |
107 |
|
|
|
108 |
|
✗ |
self->time = gnome_wall_clock_string_for_datetime (self->wall_clock, now, format, TRUE, FALSE, FALSE); |
109 |
|
|
|
110 |
|
✗ |
return self->time; |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
static void |
114 |
|
✗ |
tz_item_clock_changed_cb (CcTzItem *self) |
115 |
|
|
{ |
116 |
|
|
gboolean had_time; |
117 |
|
|
|
118 |
|
✗ |
g_assert (CC_IS_TZ_ITEM (self)); |
119 |
|
|
|
120 |
|
✗ |
had_time = !!self->time; |
121 |
|
|
|
122 |
|
|
/* Clear the time, so that it'll be re-created when asked for one */ |
123 |
|
✗ |
g_clear_pointer (&self->time, g_free); |
124 |
|
|
|
125 |
|
✗ |
if (had_time) |
126 |
|
✗ |
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TIME]); |
127 |
|
✗ |
} |
128 |
|
|
|
129 |
|
|
static void |
130 |
|
✗ |
cc_tz_item_get_property (GObject *object, |
131 |
|
|
guint prop_id, |
132 |
|
|
GValue *value, |
133 |
|
|
GParamSpec *pspec) |
134 |
|
|
{ |
135 |
|
✗ |
CcTzItem *self = (CcTzItem *)object; |
136 |
|
|
|
137 |
|
✗ |
switch (prop_id) |
138 |
|
|
{ |
139 |
|
✗ |
case PROP_COUNTRY: |
140 |
|
✗ |
g_value_set_string (value, self->country); |
141 |
|
✗ |
break; |
142 |
|
|
|
143 |
|
✗ |
case PROP_NAME: |
144 |
|
✗ |
g_value_set_string (value, self->name); |
145 |
|
✗ |
break; |
146 |
|
|
|
147 |
|
✗ |
case PROP_OFFSET: |
148 |
|
✗ |
g_value_set_string (value, self->offset); |
149 |
|
✗ |
break; |
150 |
|
|
|
151 |
|
✗ |
case PROP_TIME: |
152 |
|
✗ |
g_value_set_string (value, tz_item_get_time (self)); |
153 |
|
✗ |
break; |
154 |
|
|
|
155 |
|
✗ |
case PROP_ZONE: |
156 |
|
✗ |
g_value_set_string (value, self->zone); |
157 |
|
✗ |
break; |
158 |
|
|
|
159 |
|
✗ |
default: |
160 |
|
✗ |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
161 |
|
|
} |
162 |
|
✗ |
} |
163 |
|
|
|
164 |
|
|
static void |
165 |
|
✗ |
cc_tz_item_finalize (GObject *object) |
166 |
|
|
{ |
167 |
|
✗ |
CcTzItem *self = (CcTzItem *)object; |
168 |
|
|
|
169 |
|
✗ |
g_clear_object (&self->desktop_settings); |
170 |
|
✗ |
g_clear_object (&self->wall_clock); |
171 |
|
|
|
172 |
|
✗ |
g_clear_pointer (&self->tz, g_time_zone_unref); |
173 |
|
✗ |
g_clear_pointer (&self->tz_info, tz_info_free); |
174 |
|
|
|
175 |
|
✗ |
g_clear_pointer (&self->name, g_free); |
176 |
|
✗ |
g_clear_pointer (&self->country, g_free); |
177 |
|
✗ |
g_clear_pointer (&self->time, g_free); |
178 |
|
✗ |
g_clear_pointer (&self->offset, g_free); |
179 |
|
✗ |
g_clear_pointer (&self->zone, g_free); |
180 |
|
|
|
181 |
|
✗ |
G_OBJECT_CLASS (cc_tz_item_parent_class)->finalize (object); |
182 |
|
✗ |
} |
183 |
|
|
|
184 |
|
|
static void |
185 |
|
✗ |
cc_tz_item_class_init (CcTzItemClass *klass) |
186 |
|
|
{ |
187 |
|
✗ |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
188 |
|
|
|
189 |
|
✗ |
object_class->get_property = cc_tz_item_get_property; |
190 |
|
✗ |
object_class->finalize = cc_tz_item_finalize; |
191 |
|
|
|
192 |
|
✗ |
properties[PROP_COUNTRY] = |
193 |
|
✗ |
g_param_spec_string ("country", |
194 |
|
|
"Timezone Country", |
195 |
|
|
"Timezone Country", |
196 |
|
|
NULL, |
197 |
|
|
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); |
198 |
|
|
|
199 |
|
✗ |
properties[PROP_NAME] = |
200 |
|
✗ |
g_param_spec_string ("name", |
201 |
|
|
"Timezone Name", |
202 |
|
|
"Timezone Name", |
203 |
|
|
NULL, |
204 |
|
|
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); |
205 |
|
|
|
206 |
|
✗ |
properties[PROP_OFFSET] = |
207 |
|
✗ |
g_param_spec_string ("offset", |
208 |
|
|
"Timezone offset", |
209 |
|
|
"Timezone offset", |
210 |
|
|
NULL, |
211 |
|
|
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); |
212 |
|
|
|
213 |
|
✗ |
properties[PROP_TIME] = |
214 |
|
✗ |
g_param_spec_string ("time", |
215 |
|
|
"Timezone time", |
216 |
|
|
"Timezone time", |
217 |
|
|
NULL, |
218 |
|
|
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); |
219 |
|
|
|
220 |
|
✗ |
properties[PROP_ZONE] = |
221 |
|
✗ |
g_param_spec_string ("zone", |
222 |
|
|
"Timezone zone", |
223 |
|
|
"Timezone zone", |
224 |
|
|
NULL, |
225 |
|
|
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); |
226 |
|
|
|
227 |
|
✗ |
g_object_class_install_properties (object_class, N_PROPS, properties); |
228 |
|
✗ |
} |
229 |
|
|
|
230 |
|
|
static void |
231 |
|
✗ |
cc_tz_item_init (CcTzItem *self) |
232 |
|
|
{ |
233 |
|
✗ |
self->desktop_settings = g_settings_new ("org.gnome.desktop.interface"); |
234 |
|
✗ |
self->wall_clock = gnome_wall_clock_new (); |
235 |
|
|
|
236 |
|
✗ |
g_signal_connect_object (self->wall_clock, "notify::clock", |
237 |
|
|
G_CALLBACK (tz_item_clock_changed_cb), |
238 |
|
|
self, |
239 |
|
|
G_CONNECT_SWAPPED); |
240 |
|
✗ |
g_signal_connect_object (self->desktop_settings, "changed::clock-format", |
241 |
|
|
G_CALLBACK (tz_item_clock_changed_cb), |
242 |
|
|
self, |
243 |
|
|
G_CONNECT_SWAPPED); |
244 |
|
✗ |
} |
245 |
|
|
|
246 |
|
|
CcTzItem * |
247 |
|
✗ |
cc_tz_item_new (TzLocation *location) |
248 |
|
|
{ |
249 |
|
|
CcTzItem *self; |
250 |
|
|
GString *offset; |
251 |
|
|
|
252 |
|
✗ |
g_return_val_if_fail (location, NULL); |
253 |
|
|
|
254 |
|
✗ |
self = g_object_new (CC_TYPE_TZ_ITEM, NULL); |
255 |
|
✗ |
self->tz_location = location; |
256 |
|
✗ |
self->tz_info = tz_info_from_location (location); |
257 |
|
✗ |
generate_city_name (self, location); |
258 |
|
|
|
259 |
|
✗ |
self->tz = g_time_zone_new_offset (self->tz_info->utc_offset); |
260 |
|
|
|
261 |
|
✗ |
offset = g_string_new (g_time_zone_get_identifier (self->tz)); |
262 |
|
|
/* Strip the seconds, eg: +05:30:00 -> +05:30 */ |
263 |
|
✗ |
g_string_set_size (offset, offset->len - 3); |
264 |
|
|
/* eg: +05:30 -> +0530*/ |
265 |
|
✗ |
g_string_replace (offset, ":", "", 0); |
266 |
|
|
|
267 |
|
|
/* If the timezone is UTC remove the time, which will always be [+]0000 */ |
268 |
|
✗ |
if (g_str_has_suffix (offset->str, "0000")) |
269 |
|
✗ |
g_string_set_size (offset, 0); |
270 |
|
|
|
271 |
|
|
/* eg: +0530 -> UTC+0530 */ |
272 |
|
✗ |
g_string_prepend (offset, "UTC"); |
273 |
|
|
|
274 |
|
✗ |
self->offset = g_string_free (offset, FALSE); |
275 |
|
|
|
276 |
|
✗ |
return self; |
277 |
|
|
} |
278 |
|
|
|
279 |
|
|
TzLocation * |
280 |
|
✗ |
cc_tz_item_get_location (CcTzItem *self) |
281 |
|
|
{ |
282 |
|
✗ |
g_return_val_if_fail (CC_IS_TZ_ITEM (self), NULL); |
283 |
|
|
|
284 |
|
✗ |
return self->tz_location; |
285 |
|
|
} |
286 |
|
|
|