File: oobs-timeconfig.c

package info (click to toggle)
liboobs 3.0.0-4.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,284 kB
  • sloc: sh: 10,131; ansic: 9,920; makefile: 176; xml: 1
file content (595 lines) | stat: -rw-r--r-- 15,015 bytes parent folder | download | duplicates (2)
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
/* Copyright (C) 2005 Carlos Garnacho
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 * Authors: Carlos Garnacho Parro  <carlosg@gnome.org>
 */

#include <dbus/dbus.h>
#include <glib-object.h>
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>

#include "oobs-object.h"
#include "oobs-object-private.h"
#include "oobs-timeconfig.h"
#include "utils.h"

/**
 * SECTION:oobs-timeconfig
 * @title: OobsTimeConfig
 * @short_description: Object that represents time configuration
 * @see_also: #OobsNTPConfig, #OobsNTPServer
 **/

#define TIME_CONFIG_REMOTE_OBJECT "TimeConfig"
#define OOBS_TIME_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), OOBS_TYPE_TIME_CONFIG, OobsTimeConfigPrivate))

typedef struct _OobsTimeConfigPrivate OobsTimeConfigPrivate;

struct _OobsTimeConfigPrivate
{
  gboolean  time_is_set;
  GTimeVal  time;
  gchar    *timezone;
};

static void oobs_time_config_class_init (OobsTimeConfigClass *class);
static void oobs_time_config_init       (OobsTimeConfig      *config);
static void oobs_time_config_finalize   (GObject             *object);

static void oobs_time_config_set_property (GObject      *object,
					   guint         prop_id,
					   const GValue *value,
					   GParamSpec   *pspec);
static void oobs_time_config_get_property (GObject      *object,
					   guint         prop_id,
					   GValue       *value,
					   GParamSpec   *pspec);

static void oobs_time_config_update     (OobsObject   *object);
static void oobs_time_config_commit     (OobsObject   *object);

enum
{
  PROP_0,
  PROP_UNIX_TIME,
  PROP_TIMEZONE
};

G_DEFINE_TYPE (OobsTimeConfig, oobs_time_config, OOBS_TYPE_OBJECT);

static void
oobs_time_config_class_init (OobsTimeConfigClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  OobsObjectClass *oobs_object_class = OOBS_OBJECT_CLASS (class);

  object_class->set_property = oobs_time_config_set_property;
  object_class->get_property = oobs_time_config_get_property;
  object_class->finalize     = oobs_time_config_finalize;

  oobs_object_class->commit  = oobs_time_config_commit;
  oobs_object_class->update  = oobs_time_config_update;

  g_object_class_install_property (object_class,
				   PROP_UNIX_TIME,
				   g_param_spec_long ("unix-time",
						      "Unix time",
						      "Current unix time for the computer",
						      0,
						      G_MAXLONG,
						      0,
						      G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_TIMEZONE,
				   g_param_spec_string ("timezone",
							"Timezone",
							"Timezone for the computer",
							NULL,
							G_PARAM_READWRITE));
  g_type_class_add_private (object_class,
			    sizeof (OobsTimeConfigPrivate));
}

static void
oobs_time_config_init (OobsTimeConfig *config)
{
  OobsTimeConfigPrivate *priv;

  g_return_if_fail (OOBS_IS_TIME_CONFIG (config));

  priv = OOBS_TIME_CONFIG_GET_PRIVATE (config);

  priv->time_is_set = FALSE;
  config->_priv = priv;
}

static void
oobs_time_config_finalize (GObject *object)
{
  OobsTimeConfigPrivate *priv;

  g_return_if_fail (OOBS_IS_TIME_CONFIG (object));

  priv = OOBS_TIME_CONFIG (object)->_priv;

  if (priv && priv->timezone)
    g_free (priv->timezone);

  if (G_OBJECT_CLASS (oobs_time_config_parent_class)->finalize)
    (* G_OBJECT_CLASS (oobs_time_config_parent_class)->finalize) (object);
}

static void
oobs_time_config_set_property (GObject      *object,
			       guint         prop_id,
			       const GValue *value,
			       GParamSpec   *pspec)
{
  OobsTimeConfigPrivate *priv;

  g_return_if_fail (OOBS_IS_TIME_CONFIG (object));

  priv = OOBS_TIME_CONFIG (object)->_priv;

  switch (prop_id)
    {
    case PROP_UNIX_TIME:
      priv->time.tv_sec  = g_value_get_long (value);
      priv->time.tv_usec = 0;
      priv->time_is_set  = TRUE;
      break;
    case PROP_TIMEZONE:
      g_free (priv->timezone);
      priv->timezone = g_value_dup_string (value);
      break;
    }
}

static void
oobs_time_config_get_property (GObject    *object,
			       guint       prop_id,
			       GValue     *value,
			       GParamSpec *pspec)
{
  OobsTimeConfigPrivate *priv;
  GTimeVal tv;

  g_return_if_fail (OOBS_IS_TIME_CONFIG (object));

  priv = OOBS_TIME_CONFIG (object)->_priv;

  switch (prop_id)
    {
    case PROP_UNIX_TIME:
      if (!priv->time_is_set)
	{
	  /* return the current time */
	  /* FIXME: using local information instead
	     of backend data, this avoids latency, but
	     won't work when remote configuration is
	     possible */
	  g_get_current_time (&tv);
	}
      else
	{
	  /* once set, the time configuration will
	     stop getting the current time */
	  tv.tv_sec  = priv->time.tv_sec;
	  tv.tv_usec = priv->time.tv_usec;
	}

      g_value_set_long (value, tv.tv_sec);
      break;
    case PROP_TIMEZONE:
      g_value_set_string (value, priv->timezone);
      break;
    }
}

static void
oobs_time_config_update (OobsObject *object)
{
  OobsTimeConfigPrivate *priv;
  DBusMessage *reply;
  DBusMessageIter iter;
  const gchar *timezone;

  priv  = OOBS_TIME_CONFIG (object)->_priv;
  reply = _oobs_object_get_dbus_message (object);

  /* First of all, free the previous config */
  g_free (priv->timezone);

  /* FIXME: skip time & date settings, at the moment
   * we rely on the local configuration, this has
   * to change if/when we allow remote configuration
   */
  dbus_message_iter_init (reply, &iter);
  dbus_message_iter_next (&iter);
  dbus_message_iter_next (&iter);
  dbus_message_iter_next (&iter);
  dbus_message_iter_next (&iter);
  dbus_message_iter_next (&iter);

  dbus_message_iter_next (&iter);
  timezone = utils_get_string (&iter);
  priv->timezone = (timezone) ? g_strdup (timezone) : NULL;
}

static void
oobs_time_config_commit (OobsObject *object)
{
  OobsTimeConfigPrivate *priv;
  DBusMessage *message;
  DBusMessageIter iter;
  gint year, month, day, hour, minute, second;

  priv = OOBS_TIME_CONFIG (object)->_priv;
  message = _oobs_object_get_dbus_message (object);

  oobs_time_config_get_utc_time (OOBS_TIME_CONFIG (object),
				 &year, &month,  &day,
				 &hour, &minute, &second);

  dbus_message_iter_init_append (message, &iter);
  utils_append_int (&iter, year);
  utils_append_int (&iter, month);
  utils_append_int (&iter, day);
  utils_append_int (&iter, hour);
  utils_append_int (&iter, minute);
  utils_append_int (&iter, second);

  utils_append_string (&iter, priv->timezone);

  /* we've just synchronized with the system
   * date, so we don't need this anymore
   */
  priv->time_is_set = FALSE;
}

/**
 * oobs_time_config_get:
 * 
 * Returns the #OobsTimeConfig singleton, this object
 * represents the date and time configuration.
 * 
 * Return Value: the singleton #OobsTimeConfig.
 **/
OobsObject*
oobs_time_config_get (void)
{
  static OobsObject *the_object = NULL;

  if (!the_object)
    the_object = g_object_new (OOBS_TYPE_TIME_CONFIG,
                               "remote-object", TIME_CONFIG_REMOTE_OBJECT,
                               NULL);

  return the_object;
}

static gboolean 
is_leap_year (gint year)
{
  return ((((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0));
}

static gboolean
date_is_sane (gint year,
	      gint month,
	      gint day,
	      gint hour,
	      gint minute,
	      gint second)
{
  static const gint month_lengths[2][12] = {
    { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
    { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
  };

  gint month_days;
  gboolean is_leap;

  g_return_val_if_fail ((year >= 1), FALSE);
  g_return_val_if_fail ((month >= 0 && month <= 11), FALSE);

  is_leap = is_leap_year (year);
  month_days = month_lengths [is_leap][month];

  g_return_val_if_fail ((day >= 1 && day <= month_days), FALSE);
  g_return_val_if_fail ((hour >= 0 && hour <= 23), FALSE);
  g_return_val_if_fail ((minute >= 0 && minute <= 59), FALSE);
  g_return_val_if_fail ((second >= 0 && second <= 59), FALSE);

  return TRUE;
}

/**
 * oobs_time_config_get_unix_time:
 * @config: An #OobsTimeConfig.
 * 
 * Returns the time, measured in seconds, since the "epoch" (1970-01-01T00:00:00Z).
 * 
 * Return Value: The unix time of the system.
 **/
time_t
oobs_time_config_get_unix_time (OobsTimeConfig *config)
{
  time_t unix_time;

  g_return_val_if_fail (OOBS_IS_TIME_CONFIG (config), 0);

  g_object_get (G_OBJECT (config), "unix-time", &unix_time, NULL);

  return unix_time;
}

/**
 * oobs_time_config_set_unix_time:
 * @config: An #OobsTimeConfig.
 * @unix_time: new Unix time.
 * 
 * This function sets the #config time to be #unix_time. #unix_time
 * is measured in seconds, since the "epoch" (1970-01-01T00:00:00Z).
 **/
void
oobs_time_config_set_unix_time (OobsTimeConfig *config, time_t unix_time)
{
  g_return_if_fail (OOBS_IS_TIME_CONFIG (config));

  g_object_set (G_OBJECT (config), "unix-time", unix_time, NULL);
}

static void
real_get_time (OobsTimeConfig *config,
	       gboolean        use_utc,
	       gint           *year,
	       gint           *month,
	       gint           *day,
	       gint           *hour,
	       gint           *minute,
	       gint           *second)
{
  time_t unix_time;
  struct tm *tm;

  g_return_if_fail (OOBS_IS_TIME_CONFIG (config));

  unix_time = oobs_time_config_get_unix_time (config);

  if (use_utc)
    tm = gmtime (&unix_time);
  else
    tm = localtime (&unix_time);

  if (year)
    *year = tm->tm_year + 1900;

  if (month)
    *month = tm->tm_mon;

  if (day)
    *day = tm->tm_mday;

  if (hour)
    *hour = tm->tm_hour;

  if (minute)
    *minute = tm->tm_min;

  if (second)
    *second = tm->tm_sec;
}


/**
 * oobs_time_config_get_time:
 * @config: An #OobsTimeConfig.
 * @year: gint pointer to store the year, or #NULL.
 * @month: gint pointer to store the month, or #NULL.
 * @day: gint pointer to store the day, or #NULL.
 * @hour: gint pointer to store the hour, or #NULL.
 * @minute: gint pointer to store the minute, or #NULL.
 * @second: gint pointer to store the second, or #NULL.
 * 
 * Gets the system time and date in human readable values.
 **/
void
oobs_time_config_get_time (OobsTimeConfig *config,
			   gint           *year,
			   gint           *month,
			   gint           *day,
			   gint           *hour,
			   gint           *minute,
			   gint           *second)
{
  real_get_time (config, FALSE,
		 year, month,  day,
		 hour, minute, second);
}

/**
 * oobs_time_config_get_utc_time:
 * @config: An #OobsTimeConfig.
 * @year: gint pointer to store the year, or #NULL.
 * @month: gint pointer to store the month, or #NULL.
 * @day: gint pointer to store the day, or #NULL.
 * @hour: gint pointer to store the hour, or #NULL.
 * @minute: gint pointer to store the minute, or #NULL.
 * @second: gint pointer to store the second, or #NULL.
 * 
 * Gets the system time and date in human readable values (UTC).
 **/
void
oobs_time_config_get_utc_time (OobsTimeConfig *config,
			       gint           *year,
			       gint           *month,
			       gint           *day,
			       gint           *hour,
			       gint           *minute,
			       gint           *second)
{
  real_get_time (config, TRUE,
		 year, month,  day,
		 hour, minute, second);
}

static time_t
get_utc_unix_time (struct tm *tm)
{
  gchar *tz;
  time_t unix_time;

  tz = getenv ("TZ");
  setenv ("TZ", "", 1);
  tzset ();

  unix_time = mktime (tm);

  if (tz)
    setenv ("TZ", tz, 1);
  else
    unsetenv ("TZ");

  tzset ();

  return unix_time;
}

static void
real_set_time (OobsTimeConfig *config,
	       gboolean        use_utc,
	       gint            year,
	       gint            month,
	       gint            day,
	       gint            hour,
	       gint            minute,
	       gint            second)
{
  struct tm tm = { 0, };
  time_t unix_time;

  g_return_if_fail (OOBS_IS_TIME_CONFIG (config));
  g_return_if_fail (date_is_sane (year, month, day, hour, minute, second));

  tm.tm_year  = year - 1900;
  tm.tm_mon   = month;
  tm.tm_mday  = day;
  tm.tm_hour  = hour;
  tm.tm_min   = minute;
  tm.tm_sec   = second;
  tm.tm_isdst = -1;

  if (use_utc)
    unix_time = get_utc_unix_time (&tm);
  else
    unix_time = mktime (&tm);

  oobs_time_config_set_unix_time (config, unix_time);
}

/**
 * oobs_time_config_set_time:
 * @config: An #OobsTimeConfig.
 * @year: year.
 * @month: month.
 * @day: day.
 * @hour: hour.
 * @minute: minute.
 * @second: second.
 * 
 * Sets the time and date of #config to be the specified in the parameters.
 **/
void
oobs_time_config_set_time (OobsTimeConfig *config,
			   gint            year,
			   gint            month,
			   gint            day,
			   gint            hour,
			   gint            minute,
			   gint            second)
{
  real_set_time (config, FALSE,
		 year, month,  day,
		 hour, minute, second);
}

/**
 * oobs_time_config_set_utc_time:
 * @config: An #OobsTimeConfig.
 * @year: year.
 * @month: month.
 * @day: day.
 * @hour: hour.
 * @minute: minute.
 * @second: second.
 * 
 * Sets the time and date of @config to be the specified in the parameters (assuming they're in UTC).
 **/
void
oobs_time_config_set_utc_time (OobsTimeConfig *config,
			       gint            year,
			       gint            month,
			       gint            day,
			       gint            hour,
			       gint            minute,
			       gint            second)
{
  real_set_time (config, TRUE,
		 year, month,  day,
		 hour, minute, second);
}

/**
 * oobs_time_config_get_timezone:
 * @config: An #OobsTimeConfig.
 * 
 * Returns the timezone set for #config.
 * 
 * Return Value: A pointer to the timezone as a string or #NULL.
 *               This string must not be freed, modified or stored.
 **/
G_CONST_RETURN gchar*
oobs_time_config_get_timezone (OobsTimeConfig *config)
{
  OobsTimeConfigPrivate *priv;

  g_return_val_if_fail (OOBS_IS_TIME_CONFIG (config), NULL);

  priv = config->_priv;

  return priv->timezone;
}

/**
 * oobs_time_config_set_timezone:
 * @config: An #OobsTimeConfig.
 * @timezone: A new timezone for #config.
 * 
 * Sets the timezone of #config to be #timezone,
 * overwriting the previous one.
 **/
void
oobs_time_config_set_timezone (OobsTimeConfig *config,
			       const gchar    *timezone)
{
  g_return_if_fail (OOBS_IS_TIME_CONFIG (config));

  g_object_set (G_OBJECT (config), "timezone", timezone, NULL);
}