Add GTimeZoneMonitor

Monitors /etc/localtime for changes and instructs GTimeZone to drop its
cache.  Also has a signal for interested 3rd parties.
This commit is contained in:
Ryan Lortie 2011-03-18 22:00:14 -04:00
parent de3a3b181a
commit e9ce8f2374
8 changed files with 224 additions and 0 deletions

View File

@ -31,6 +31,7 @@
<chapter id="file_mon">
<title>File System Monitoring</title>
<xi:include href="xml/gfilemonitor.xml"/>
<xi:include href="xml/gtimezonemonitor.xml"/>
</chapter>
<chapter id="utils">
<title>File-related Utilities</title>

View File

@ -3106,3 +3106,16 @@ G_TYPE_TLS_SERVER_CONNECTION
<SUBSECTION Private>
g_tls_server_connection_get_type
</SECTION>
<SECTION>
<FILE>gtimezonemonitor</FILE>
<TITLE>GTimeZoneMonitor</TITLE>
GTimeZoneMonitor
g_time_zone_monitor_get
<SUBSECTION Standard>
G_IS_TIME_ZONE_MONITOR
G_TIME_ZONE_MONITOR
G_TYPE_TIME_ZONE_MONITOR
<SUBSECTION Private>
g_time_zone_monitor_get_type
</SECTION>

View File

@ -136,3 +136,4 @@ g_dbus_server_get_type
g_dbus_auth_observer_get_type
g_credentials_get_type
g_unix_credentials_message_get_type
g_time_zone_monitor_get_type

View File

@ -379,6 +379,7 @@ libgio_2_0_la_SOURCES = \
gthemedicon.c \
gthreadedresolver.c \
gthreadedresolver.h \
gtimezonemonitor.c \
gtlsbackend.c \
gtlscertificate.c \
gtlsclientconnection.c \
@ -532,6 +533,7 @@ gio_headers = \
gtcpwrapperconnection.h \
gthreadedsocketservice.h\
gthemedicon.h \
gtimezonemonitor.h \
gtlsbackend.h \
gtlscertificate.h \
gtlsclientconnection.h \

View File

@ -119,6 +119,7 @@
#include <gio/gtcpwrapperconnection.h>
#include <gio/gthemedicon.h>
#include <gio/gthreadedsocketservice.h>
#include <gio/gtimezonemonitor.h>
#include <gio/gtlsbackend.h>
#include <gio/gtlscertificate.h>
#include <gio/gtlsclientconnection.h>

156
gio/gtimezonemonitor.c Normal file
View File

@ -0,0 +1,156 @@
/*
* Copyright © 2011 Codethink Limited
*
* 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 licence or (at
* your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authors: Ryan Lortie <desrt@desrt.ca>
*/
#include "gtimezonemonitor.h"
#include "gfile.h"
/**
* SECTION:gtimezonemonitor
* @title: GTimeZoneMonitor
* @short_description: Monitor the local timezone
*
* #GTimeZoneMonitor is a utility class to monitor the local timezone for
* changes (ie: in response to the user manually changing the timezone
* to that of a different locale).
*
* You must use this class in order for your program to notice changes
* to the local timezone. It works by monitoring the /etc/localtime
* file. When the timezone is found to have changed,
* g_time_zone_refresh_local() is called and the "changed" signal is
* emitted on the #GTimeZoneMonitor (in that order).
*
* Windows support is not presently working.
**/
/**
* GTimeZoneMonitor:
*
* This is an opaque structure type.
**/
typedef GObjectClass GTimeZoneMonitorClass;
struct _GTimeZoneMonitor
{
GObject parent_instance;
GFileMonitor *monitor;
};
G_DEFINE_TYPE (GTimeZoneMonitor, g_time_zone_monitor, G_TYPE_OBJECT)
static guint g_time_zone_monitor_changed_signal;
static void
etc_localtime_changed (GFileMonitor *monitor,
GFile *file,
GFile *other_file,
GFileMonitorEvent event_type,
gpointer user_data)
{
GTimeZoneMonitor *tzm = user_data;
if (event_type != G_FILE_MONITOR_EVENT_CREATED)
return;
g_time_zone_refresh_local ();
g_signal_emit (tzm, g_time_zone_monitor_changed_signal, 0);
}
static void
g_time_zone_monitor_finalize (GObject *object)
{
g_assert_not_reached ();
}
static void
g_time_zone_monitor_init (GTimeZoneMonitor *tzm)
{
GFile *etc_localtime;
etc_localtime = g_file_new_for_path ("/etc/localtime");
tzm->monitor = g_file_monitor_file (etc_localtime, 0, NULL, NULL);
g_object_unref (etc_localtime);
g_signal_connect (tzm->monitor, "changed",
G_CALLBACK (etc_localtime_changed), tzm);
}
static void
g_time_zone_monitor_class_init (GTimeZoneMonitorClass *class)
{
class->finalize = g_time_zone_monitor_finalize;
/**
* GTimeZoneMonitor::changed
* @monitor: the #GTimeZoneMonitor
*
* Indicates that the local timezone has changed.
*
* The g_time_zone_refresh_local() function is called just before this
* signal is emitted, so any new #GTimeZone or #GDateTime instances
* created from signal handlers will be as per the new timezone.
*
* Note that this signal is not emitted in response to entering or
* exiting daylight savings time within a given timezone. It's only
* for when the user has changed the timezone to that of a different
* location.
**/
g_time_zone_monitor_changed_signal =
g_signal_new ("changed", G_TYPE_TIME_ZONE_MONITOR,
G_SIGNAL_RUN_FIRST, 0, NULL, NULL,
g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}
/**
* g_time_zone_monitor_get:
*
* Gets the singleton instance of the #GTimeZoneMonitor class, creating
* it if required.
*
* You should call g_object_unref() on the result when you no longer
* need it. Be aware, though, that this will not destroy the instance,
* so if you connected to the changed signal, you are required to
* disconnect from it for yourself.
*
* There is only one instance of #GTimeZoneMonitor and it dispatches its
* signals via the default #GMainContext. There is no way to create an
* instance that will dispatch signals using a different context.
*
* Returns: a reference to the #GTimeZoneMonitor.
**/
GTimeZoneMonitor *
g_time_zone_monitor_get (void)
{
static gsize instance;
if (g_once_init_enter (&instance))
{
GTimeZoneMonitor *monitor;
monitor = g_object_new (G_TYPE_TIME_ZONE_MONITOR, NULL);
g_once_init_leave (&instance, (gsize) monitor);
}
return g_object_ref ((void *) instance);
}

47
gio/gtimezonemonitor.h Normal file
View File

@ -0,0 +1,47 @@
/*
* Copyright © 2011 Codethink Limited
*
* 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 licence or (at
* your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authors: Ryan Lortie <desrt@desrt.ca>
*/
#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
#error "Only <gio/gio.h> can be included directly."
#endif
#ifndef __G_TIME_ZONE_MONITOR_H__
#define __G_TIME_ZONE_MONITOR_H__
#include "gactiongroup.h"
G_BEGIN_DECLS
#define G_TYPE_TIME_ZONE_MONITOR (g_time_zone_monitor_get_type ())
#define G_TIME_ZONE_MONITOR(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), \
G_TYPE_TIME_ZONE_MONITOR, GTimeZoneMonitor))
#define G_IS_TIME_ZONE_MONITOR(inst) (G_TYPE_CHECK_INSTANCE_TYPE ((inst), \
G_TYPE_TIME_ZONE_MONITOR))
typedef struct _GTimeZoneMonitor GTimeZoneMonitor;
GType g_time_zone_monitor_get_type (void) G_GNUC_CONST;
GTimeZoneMonitor * g_time_zone_monitor_get (void);
G_END_DECLS
#endif /* __G_TIME_ZONE_MONITOR_H__ */

View File

@ -492,6 +492,9 @@ g_time_zone_new_local (void)
*
* #GTimeZone does no monitoring of the local timezone on its own, which
* is why you have to call this function to notify it of the change.
*
* If you use #GTimeZoneMonitor to watch for changes then this function
* will automatically be called for you.
**/
void
g_time_zone_refresh_local (void)