[gnome-control-center] datetime: Add "using NTP" helpers



commit 9ee79e22154912de2d481f8f4a123f361f141804
Author: Bastien Nocera <hadess hadess net>
Date:   Thu Dec 9 12:48:41 2010 +0000

    datetime: Add "using NTP" helpers

 panels/datetime/set-timezone.c |  104 ++++++++++++++++++++++++++++++++++++++--
 panels/datetime/set-timezone.h |    8 +++
 2 files changed, 108 insertions(+), 4 deletions(-)
---
diff --git a/panels/datetime/set-timezone.c b/panels/datetime/set-timezone.c
index 74a339d..5aafced 100644
--- a/panels/datetime/set-timezone.c
+++ b/panels/datetime/set-timezone.c
@@ -144,11 +144,36 @@ can_set_system_time (void)
 	return settime_cache;
 }
 
+static gint   usingntp_cache = 0;
+static time_t usingntp_stamp = 0;
+
+static void
+update_can_usingntp (gint res)
+{
+	usingntp_cache = res;
+	time (&usingntp_stamp);
+}
+
+gint
+can_set_using_ntp (void)
+{
+	time_t now;
+
+	time (&now);
+	if (ABS (now - usingntp_stamp) > CACHE_VALIDITY_SEC) {
+		refresh_can_do ("CanSetUsingNtp", update_can_usingntp);
+		settime_stamp = now;
+	}
+
+	return usingntp_cache;
+}
+
 typedef struct {
 	gint ref_count;
         gchar *call;
 	gint64 time;
 	gchar *tz;
+	gboolean using_ntp;
 	GFunc callback;
 	gpointer data;
 	GDestroyNotify notify;
@@ -177,7 +202,7 @@ set_time_notify (DBusGProxy     *proxy,
 	GError *error = NULL;
 
 	if (dbus_g_proxy_end_call (proxy, call, &error, G_TYPE_INVALID)) {
-		if (data->callback) 
+		if (data->callback)
 			data->callback (data->data, NULL);
 	}
 	else {
@@ -224,7 +249,7 @@ set_time_async (SetTimeCallbackData *data)
 
 	data->ref_count++;
 	if (strcmp (data->call, "SetTime") == 0)
-		dbus_g_proxy_begin_call_with_timeout (proxy, 
+		dbus_g_proxy_begin_call_with_timeout (proxy,
 						      "SetTime",
 						      set_time_notify,
 						      data, free_data,
@@ -234,8 +259,8 @@ set_time_async (SetTimeCallbackData *data)
 						      G_TYPE_INVALID,
 						      /* return values: */
 						      G_TYPE_INVALID);
-	else 
-		dbus_g_proxy_begin_call_with_timeout (proxy, 
+	else if (strcmp (data->call, "SetTimezone") == 0)
+		dbus_g_proxy_begin_call_with_timeout (proxy,
 						      "SetTimezone",
 						      set_time_notify,
 						      data, free_data,
@@ -245,6 +270,17 @@ set_time_async (SetTimeCallbackData *data)
 						      G_TYPE_INVALID,
 						      /* return values: */
 						      G_TYPE_INVALID);
+	else if (strcmp (data->call, "SetUsingNtp") == 0)
+		dbus_g_proxy_begin_call_with_timeout (proxy,
+						      "SetUsingNtp",
+						      set_time_notify,
+						      data, free_data,
+						      INT_MAX,
+						      /* parameters: */
+						      G_TYPE_BOOLEAN, data->using_ntp,
+						      G_TYPE_INVALID,
+						      /* return values: */
+						      G_TYPE_INVALID);
 }
 
 void
@@ -383,3 +419,63 @@ get_system_timezone_async (GetTimezoneFunc callback,
 				 G_TYPE_INVALID);
 
 }
+
+gboolean
+get_using_ntp (void)
+{
+	static gboolean can_use_cache = FALSE;
+	static gboolean is_using_cache = FALSE;
+	static time_t   last_refreshed = 0;
+	time_t          now;
+        DBusGConnection *bus;
+        DBusGProxy      *proxy;
+
+	time (&now);
+	if (ABS (now - last_refreshed) > CACHE_VALIDITY_SEC) {
+		gboolean cu, iu;
+		bus = get_system_bus (NULL);
+		if (bus == NULL)
+			return FALSE;
+
+		proxy = dbus_g_proxy_new_for_name (bus,
+						   "org.gnome.SettingsDaemon.DateTimeMechanism",
+						   "/",
+						   "org.gnome.SettingsDaemon.DateTimeMechanism");
+
+
+		if (dbus_g_proxy_call (proxy,
+				       "GetUsingNtp",
+				       NULL,
+				       G_TYPE_INVALID,
+				       G_TYPE_BOOLEAN, &cu,
+				       G_TYPE_BOOLEAN, &iu,
+				       G_TYPE_INVALID)) {
+			can_use_cache = cu;
+			is_using_cache = iu;
+			last_refreshed = now;
+		}
+	}
+
+	return is_using_cache;
+}
+
+void
+set_using_ntp_async (gboolean        using_ntp,
+	             GFunc           callback,
+	             gpointer        d,
+	             GDestroyNotify  notify)
+{
+	SetTimeCallbackData *data;
+
+	data = g_new0 (SetTimeCallbackData, 1);
+	data->ref_count = 1;
+	data->call = "SetUsingNtp";
+	data->time = -1;
+	data->using_ntp = using_ntp;
+	data->callback = callback;
+	data->data = d;
+	data->notify = notify;
+
+	set_time_async (data);
+	free_data (data);
+}
diff --git a/panels/datetime/set-timezone.h b/panels/datetime/set-timezone.h
index 718aaad..5e657e3 100644
--- a/panels/datetime/set-timezone.h
+++ b/panels/datetime/set-timezone.h
@@ -36,6 +36,8 @@ gint     can_set_system_timezone (void);
 
 gint     can_set_system_time     (void);
 
+gint     can_set_using_ntp       (void);
+
 void     set_system_time_async   (gint64         time,
                                   GFunc          callback,
                                   gpointer       data,
@@ -46,4 +48,10 @@ void     set_system_timezone_async   (const gchar    *tz,
                                       gpointer        data,
                                       GDestroyNotify  notify);
 
+gboolean get_using_ntp               (void);
+
+void     set_using_ntp_async         (gboolean        using_ntp,
+                                      GFunc           callback,
+                                      gpointer        data,
+                                      GDestroyNotify  notify);
 #endif



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]