aboutsummaryrefslogtreecommitdiff
path: root/src/timezone-file.c
blob: c7c2cf689d231239fe3a9a178d185fc2d088634c (plain)
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
/*
 * 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 "config.h"

#include <glib.h>
#include <glib/gi18n-lib.h>
#include <gio/gio.h>

#include "timezone-file.h"

enum
{
  PROP_0,
  PROP_FILENAME,
  PROP_LAST
};

static GParamSpec * properties[PROP_LAST] = { 0 };

struct _IndicatorDatetimeTimezoneFilePriv
{
  gchar * filename;
  GFile * file;
  GFileMonitor * monitor;
  gchar * timezone;
};

typedef IndicatorDatetimeTimezoneFilePriv priv_t;

G_DEFINE_TYPE (IndicatorDatetimeTimezoneFile,
               indicator_datetime_timezone_file,
               INDICATOR_TYPE_DATETIME_TIMEZONE)

/***
****
***/

static void
reload (IndicatorDatetimeTimezoneFile * self)
{
  priv_t * p = self->priv;

  GError * err = NULL;
  gchar * new_timezone = NULL;

  if (!g_file_get_contents (p->filename, &new_timezone, NULL, &err))
    {
      g_warning ("Unable to read timezone file '%s': %s", p->filename, err->message);
      g_error_free (err);
    }
  else
    {
      g_strstrip (new_timezone);
      g_free (p->timezone);
      p->timezone = new_timezone;
      indicator_datetime_timezone_notify_timezone (INDICATOR_DATETIME_TIMEZONE(self));
    }
}

static void
set_filename (IndicatorDatetimeTimezoneFile * self, const char * filename)
{
  GError * err;
  priv_t * p = self->priv;

  g_clear_object (&p->monitor);
  g_clear_object (&p->file);
  g_free (p->filename);

  p->filename = g_strdup (filename);
  p->file = g_file_new_for_path (p->filename);

  err = NULL;
  p->monitor = g_file_monitor_file (p->file, G_FILE_MONITOR_NONE, NULL, &err);
  if (err != NULL)
    {
      g_warning ("Unable to monitor timezone file '%s': %s", TIMEZONE_FILE, err->message);
      g_error_free (err);
    }
  else
    {
      g_signal_connect_swapped (p->monitor, "changed", G_CALLBACK(reload), self);
      g_debug ("Monitoring timezone file '%s'", p->filename);
    }

  reload (self);
}

/***
**** IndicatorDatetimeTimezoneClass funcs
***/

static const char *
my_get_timezone (IndicatorDatetimeTimezone * self)
{
  return INDICATOR_DATETIME_TIMEZONE_FILE(self)->priv->timezone;
}

/***
**** GObjectClass funcs
***/

static void
my_get_property (GObject     * o,
                 guint         property_id,
                 GValue      * value,
                 GParamSpec  * pspec)
{
  IndicatorDatetimeTimezoneFile * self = INDICATOR_DATETIME_TIMEZONE_FILE (o);

  switch (property_id)
    {
      case PROP_FILENAME:
        g_value_set_string (value, self->priv->filename);
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (o, property_id, pspec);
    }
}

static void
my_set_property (GObject       * o,
                 guint           property_id,
                 const GValue  * value,
                 GParamSpec    * pspec)
{
  IndicatorDatetimeTimezoneFile * self = INDICATOR_DATETIME_TIMEZONE_FILE (o);

  switch (property_id)
    {
      case PROP_FILENAME:
        set_filename (self, g_value_get_string (value));
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (o, property_id, pspec);
    }
}
  
static void
my_dispose (GObject * o)
{
  IndicatorDatetimeTimezoneFile * self = INDICATOR_DATETIME_TIMEZONE_FILE (o);
  priv_t * p = self->priv;

  g_clear_object (&p->monitor);
  g_clear_object (&p->file);

  G_OBJECT_CLASS (indicator_datetime_timezone_file_parent_class)->dispose (o);
}

static void
my_finalize (GObject * o)
{
  IndicatorDatetimeTimezoneFile * self = INDICATOR_DATETIME_TIMEZONE_FILE (o);
  priv_t * p = self->priv;

  g_free (p->filename);
  g_free (p->timezone);

  G_OBJECT_CLASS (indicator_datetime_timezone_file_parent_class)->finalize (o);
}

/***
****
***/

static void
indicator_datetime_timezone_file_class_init (IndicatorDatetimeTimezoneFileClass * klass)
{
  GObjectClass * object_class;
  IndicatorDatetimeTimezoneClass * location_class;
  const GParamFlags flags = G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS;

  object_class = G_OBJECT_CLASS (klass);
  object_class->dispose = my_dispose;
  object_class->finalize = my_finalize;
  object_class->set_property = my_set_property;
  object_class->get_property = my_get_property;

  location_class = INDICATOR_DATETIME_TIMEZONE_CLASS (klass);
  location_class->get_timezone = my_get_timezone;

  g_type_class_add_private (klass, sizeof (IndicatorDatetimeTimezoneFilePriv));

  /* install properties */

  properties[PROP_0] = NULL;

  properties[PROP_FILENAME] = g_param_spec_string ("filename",
                                                   "Filename",
                                                   "Filename to monitor for TZ changes",
                                                   "",
                                                   flags);

  g_object_class_install_properties (object_class, PROP_LAST, properties);
}

static void
indicator_datetime_timezone_file_init (IndicatorDatetimeTimezoneFile * self)
{
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
                                            INDICATOR_TYPE_DATETIME_TIMEZONE_FILE,
                                            IndicatorDatetimeTimezoneFilePriv);
}

/***
****  Public
***/

IndicatorDatetimeTimezone *
indicator_datetime_timezone_file_new (const char * filename)
{
  gpointer o = g_object_new (INDICATOR_TYPE_DATETIME_TIMEZONE_FILE, "filename", filename, NULL);

  return INDICATOR_DATETIME_TIMEZONE (o);
}