Added UG translation
[platform/upstream/glib.git] / gio / gtimezonemonitor.c
1 /*
2  * Copyright © 2011 Codethink Limited
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2 of the licence or (at
7  * your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General
15  * Public License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17  * Boston, MA 02111-1307, USA.
18  *
19  * Authors: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include "gtimezonemonitor.h"
23 #include "gfile.h"
24
25 /**
26  * SECTION:gtimezonemonitor
27  * @title: GTimeZoneMonitor
28  * @short_description: Monitor the local timezone
29  *
30  * #GTimeZoneMonitor is a utility class to monitor the local timezone for
31  * changes (ie: in response to the user manually changing the timezone
32  * to that of a different locale).
33  *
34  * You must use this class in order for your program to notice changes
35  * to the local timezone.  It works by monitoring the /etc/localtime
36  * file.  When the timezone is found to have changed,
37  * g_time_zone_refresh_local() is called and the "changed" signal is
38  * emitted on the #GTimeZoneMonitor (in that order).
39  *
40  * Windows support is not presently working.
41  **/
42
43 /**
44  * GTimeZoneMonitor:
45  *
46  * This is an opaque structure type.
47  **/
48
49 typedef GObjectClass GTimeZoneMonitorClass;
50
51 struct _GTimeZoneMonitor
52 {
53   GObject parent_instance;
54
55   GFileMonitor *monitor;
56 };
57
58 G_DEFINE_TYPE (GTimeZoneMonitor, g_time_zone_monitor, G_TYPE_OBJECT)
59
60 static guint g_time_zone_monitor_changed_signal;
61
62 static void
63 etc_localtime_changed (GFileMonitor      *monitor,
64                        GFile             *file,
65                        GFile             *other_file,
66                        GFileMonitorEvent  event_type,
67                        gpointer           user_data)
68 {
69   GTimeZoneMonitor *tzm = user_data;
70
71   if (event_type != G_FILE_MONITOR_EVENT_CREATED)
72     return;
73
74   g_time_zone_refresh_local ();
75
76   g_signal_emit (tzm, g_time_zone_monitor_changed_signal, 0);
77 }
78
79 static void
80 g_time_zone_monitor_finalize (GObject *object)
81 {
82   g_assert_not_reached ();
83 }
84
85 static void
86 g_time_zone_monitor_init (GTimeZoneMonitor *tzm)
87 {
88   GFile *etc_localtime;
89
90   etc_localtime = g_file_new_for_path ("/etc/localtime");
91   tzm->monitor = g_file_monitor_file (etc_localtime, 0, NULL, NULL);
92   g_object_unref (etc_localtime);
93
94   g_signal_connect (tzm->monitor, "changed",
95                     G_CALLBACK (etc_localtime_changed), tzm);
96 }
97
98 static void
99 g_time_zone_monitor_class_init (GTimeZoneMonitorClass *class)
100 {
101   class->finalize = g_time_zone_monitor_finalize;
102
103   /**
104    * GTimeZoneMonitor::changed
105    * @monitor: the #GTimeZoneMonitor
106    *
107    * Indicates that the local timezone has changed.
108    *
109    * The g_time_zone_refresh_local() function is called just before this
110    * signal is emitted, so any new #GTimeZone or #GDateTime instances
111    * created from signal handlers will be as per the new timezone.
112    *
113    * Note that this signal is not emitted in response to entering or
114    * exiting daylight savings time within a given timezone.  It's only
115    * for when the user has changed the timezone to that of a different
116    * location.
117    **/
118   g_time_zone_monitor_changed_signal =
119     g_signal_new ("changed", G_TYPE_TIME_ZONE_MONITOR,
120                   G_SIGNAL_RUN_FIRST, 0, NULL, NULL,
121                   g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
122 }
123
124 /**
125  * g_time_zone_monitor_get:
126  *
127  * Gets the singleton instance of the #GTimeZoneMonitor class, creating
128  * it if required.
129  *
130  * You should call g_object_unref() on the result when you no longer
131  * need it.  Be aware, though, that this will not destroy the instance,
132  * so if you connected to the changed signal, you are required to
133  * disconnect from it for yourself.
134  *
135  * There is only one instance of #GTimeZoneMonitor and it dispatches its
136  * signals via the default #GMainContext.  There is no way to create an
137  * instance that will dispatch signals using a different context.
138  *
139  * Returns: a reference to the #GTimeZoneMonitor.
140  **/
141 GTimeZoneMonitor *
142 g_time_zone_monitor_get (void)
143 {
144   static gsize instance;
145
146   if (g_once_init_enter (&instance))
147     {
148       GTimeZoneMonitor *monitor;
149
150       monitor = g_object_new (G_TYPE_TIME_ZONE_MONITOR, NULL);
151
152       g_once_init_leave (&instance, (gsize) monitor);
153     }
154
155   return g_object_ref ((void *) instance);
156 }