1 /* Test for system timezone handling
2 *
3 * Copyright (C) 2008-2010 Novell, Inc.
4 *
5 * Authors: Vincent Untz <vuntz@gnome.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22 #include <glib.h>
23 #include "system-timezone.h"
24
25 static void
timezone_print(void)26 timezone_print (void)
27 {
28 SystemTimezone *systz;
29
30 systz = system_timezone_new ();
31 g_print ("Current timezone: %s\n", system_timezone_get (systz));
32 g_object_unref (systz);
33 }
34
35 static int
timezone_set(const char * new_tz)36 timezone_set (const char *new_tz)
37 {
38 GError *error;
39
40 error = NULL;
41 if (!system_timezone_set (new_tz, &error)) {
42 g_printerr ("%s\n", error->message);
43 g_error_free (error);
44 return 1;
45 }
46
47 return 0;
48 }
49
50 static void
timezone_changed(SystemTimezone * systz,const char * new_tz,gpointer data)51 timezone_changed (SystemTimezone *systz,
52 const char *new_tz,
53 gpointer data)
54 {
55 g_print ("Timezone changed to: %s\n", new_tz);
56 }
57
58 static void
timezone_monitor(void)59 timezone_monitor (void)
60 {
61 SystemTimezone *systz;
62 GMainLoop *mainloop;
63
64 systz = system_timezone_new ();
65 g_signal_connect (systz, "changed",
66 G_CALLBACK (timezone_changed), NULL);
67
68 mainloop = g_main_loop_new (NULL, FALSE);
69 g_main_loop_run (mainloop);
70 g_main_loop_unref (mainloop);
71
72 g_object_unref (systz);
73 }
74
75 int
main(int argc,char ** argv)76 main (int argc,
77 char **argv)
78 {
79 int retval;
80
81 gboolean get = FALSE;
82 gboolean monitor = FALSE;
83 char *tz_set = NULL;
84
85 GError *error;
86 GOptionContext *context;
87 GOptionEntry options[] = {
88 { "get", 'g', 0, G_OPTION_ARG_NONE, &get, "Get the current timezone", NULL },
89 { "set", 's', 0, G_OPTION_ARG_STRING, &tz_set, "Set the timezone to TIMEZONE", "TIMEZONE" },
90 { "monitor", 'm', 0, G_OPTION_ARG_NONE, &monitor, "Monitor timezone changes", NULL },
91 { NULL, 0, 0, 0, NULL, NULL, NULL }
92 };
93
94 retval = 0;
95
96 context = g_option_context_new ("");
97 g_option_context_add_main_entries (context, options, NULL);
98
99 error = NULL;
100 if (!g_option_context_parse (context, &argc, &argv, &error)) {
101 g_printerr ("%s\n", error->message);
102 g_error_free (error);
103 g_option_context_free (context);
104
105 return 1;
106 }
107
108 g_option_context_free (context);
109
110 if (get || (!tz_set && !monitor))
111 timezone_print ();
112 else if (tz_set)
113 retval = timezone_set (tz_set);
114 else if (monitor)
115 timezone_monitor ();
116 else
117 g_assert_not_reached ();
118
119 return retval;
120 }
121