2 * Copyright © 2011 Codethink Limited
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.
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.
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.
19 * Authors: Ryan Lortie <desrt@desrt.ca>
22 #include "gtimezonemonitor.h"
26 * SECTION:gtimezonemonitor
27 * @title: GTimeZoneMonitor
28 * @short_description: Monitor the local timezone
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).
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).
40 * Windows support is not presently working.
46 * This is an opaque structure type.
49 typedef GObjectClass GTimeZoneMonitorClass;
51 struct _GTimeZoneMonitor
53 GObject parent_instance;
55 GFileMonitor *monitor;
58 G_DEFINE_TYPE (GTimeZoneMonitor, g_time_zone_monitor, G_TYPE_OBJECT)
60 static guint g_time_zone_monitor_changed_signal;
63 etc_localtime_changed (GFileMonitor *monitor,
66 GFileMonitorEvent event_type,
69 GTimeZoneMonitor *tzm = user_data;
71 if (event_type != G_FILE_MONITOR_EVENT_CREATED)
74 g_time_zone_refresh_local ();
76 g_signal_emit (tzm, g_time_zone_monitor_changed_signal, 0);
80 g_time_zone_monitor_finalize (GObject *object)
82 g_assert_not_reached ();
86 g_time_zone_monitor_init (GTimeZoneMonitor *tzm)
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);
94 g_signal_connect (tzm->monitor, "changed",
95 G_CALLBACK (etc_localtime_changed), tzm);
99 g_time_zone_monitor_class_init (GTimeZoneMonitorClass *class)
101 class->finalize = g_time_zone_monitor_finalize;
104 * GTimeZoneMonitor::changed
105 * @monitor: the #GTimeZoneMonitor
107 * Indicates that the local timezone has changed.
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.
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
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);
125 * g_time_zone_monitor_get:
127 * Gets the singleton instance of the #GTimeZoneMonitor class, creating
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.
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.
139 * Returns: a reference to the #GTimeZoneMonitor.
142 g_time_zone_monitor_get (void)
144 static gsize instance;
146 if (g_once_init_enter (&instance))
148 GTimeZoneMonitor *monitor;
150 monitor = g_object_new (G_TYPE_TIME_ZONE_MONITOR, NULL);
152 g_once_init_leave (&instance, (gsize) monitor);
155 return g_object_ref ((void *) instance);