[gnome-settings-daemon] datetime: Add code to set whether NTP should be used



commit 93a66a889ecec25dec203b1ba78d1b6a49206d18
Author: Bastien Nocera <hadess hadess net>
Date:   Wed Dec 8 18:22:22 2010 +0000

    datetime: Add code to set whether NTP should be used
    
    Spliced from:
    https://bugzilla.gnome.org/show_bug.cgi?id=533870
    
    Nothing but Fedora support in this right now.

 plugins/datetime/gsd-datetime-mechanism.c          |   97 ++++++++++++++++++++
 plugins/datetime/gsd-datetime-mechanism.h          |    9 ++
 plugins/datetime/gsd-datetime-mechanism.xml        |   36 +++++++
 ...nome.settingsdaemon.datetimemechanism.policy.in |    9 ++
 4 files changed, 151 insertions(+), 0 deletions(-)
---
diff --git a/plugins/datetime/gsd-datetime-mechanism.c b/plugins/datetime/gsd-datetime-mechanism.c
index 2959e39..f26baa1 100644
--- a/plugins/datetime/gsd-datetime-mechanism.c
+++ b/plugins/datetime/gsd-datetime-mechanism.c
@@ -694,6 +694,92 @@ gsd_datetime_mechanism_set_hardware_clock_using_utc (GsdDatetimeMechanism  *mech
         return TRUE;
 }
 
+gboolean
+gsd_datetime_mechanism_get_using_ntp  (GsdDatetimeMechanism    *mechanism,
+                                       DBusGMethodInvocation   *context)
+{
+        int exit_status;
+        GError *error = NULL;
+        gboolean can_use_ntp;
+        gboolean is_using_ntp;
+
+        if (g_file_test ("/etc/ntp.conf", G_FILE_TEST_EXISTS)) {
+                can_use_ntp = TRUE;
+                if (!g_spawn_command_line_sync ("/sbin/service ntpd status",
+                                                NULL, NULL, &exit_status, &error)) {
+                        GError *error2;
+                        error2 = g_error_new (GSD_DATETIME_MECHANISM_ERROR,
+                                              GSD_DATETIME_MECHANISM_ERROR_GENERAL,
+                                              "Error spawning /sbin/service: %s", error->message);
+                        g_error_free (error);
+                        dbus_g_method_return_error (context, error2);
+                        g_error_free (error2);
+                        return FALSE;
+                }
+                if (exit_status == 0)
+                        is_using_ntp = TRUE;
+                else
+                        is_using_ntp = FALSE;
+        }
+        else {
+                can_use_ntp = FALSE;
+                is_using_ntp = FALSE;
+        }
+
+        dbus_g_method_return (context, can_use_ntp, is_using_ntp);
+        return TRUE;
+}
+
+gboolean
+gsd_datetime_mechanism_set_using_ntp  (GsdDatetimeMechanism    *mechanism,
+                                       gboolean                 using_ntp,
+                                       DBusGMethodInvocation   *context)
+{
+        GError *error;
+        int exit_status;
+        char *cmd;
+
+        error = NULL;
+
+        if (!_check_polkit_for_action (mechanism, context, "org.gnome.clockapplet.mechanism.configurentp"))
+                return FALSE;
+
+        cmd = g_strconcat ("/sbin/chkconfig --level 2345 ntpd ", using_ntp ? "on" : "off", NULL);
+        if (!g_spawn_command_line_sync (cmd,
+                                        NULL, NULL, &exit_status, &error)) {
+                GError *error2;
+                error2 = g_error_new (GSD_DATETIME_MECHANISM_ERROR,
+                                      GSD_DATETIME_MECHANISM_ERROR_GENERAL,
+                                      "Error spawning /sbin/chkconfig: %s", error->message);
+                g_error_free (error);
+                dbus_g_method_return_error (context, error2);
+                g_error_free (error2);
+                g_free (cmd);
+                return FALSE;
+        }
+
+        g_free (cmd);
+
+        cmd = g_strconcat ("/sbin/service ntpd ", using_ntp ? "restart" : "stop", NULL);
+        if (!g_spawn_command_line_sync (cmd,
+                                        NULL, NULL, &exit_status, &error)) {
+                GError *error2;
+                error2 = g_error_new (GSD_DATETIME_MECHANISM_ERROR,
+                                      GSD_DATETIME_MECHANISM_ERROR_GENERAL,
+                                      "Error spawning /sbin/service: %s", error->message);
+                g_error_free (error);
+                dbus_g_method_return_error (context, error2);
+                g_error_free (error2);
+                g_free (cmd);
+                return FALSE;
+        }
+
+        g_free (cmd);
+
+        dbus_g_method_return (context);
+        return TRUE;
+}
+
 static void
 check_can_do (GsdDatetimeMechanism  *mechanism,
               const char            *action,
@@ -759,3 +845,14 @@ gsd_datetime_mechanism_can_set_timezone (GsdDatetimeMechanism  *mechanism,
 
         return TRUE;
 }
+
+gboolean
+gsd_datetime_mechanism_can_set_using_ntp (GsdDatetimeMechanism  *mechanism,
+                                          DBusGMethodInvocation *context)
+{
+        check_can_do (mechanism,
+                      "org.gnome.settingsdaemon.datetimemechanism.configurentp",
+                      context);
+
+        return TRUE;
+}
diff --git a/plugins/datetime/gsd-datetime-mechanism.h b/plugins/datetime/gsd-datetime-mechanism.h
index 13ca212..1c67cea 100644
--- a/plugins/datetime/gsd-datetime-mechanism.h
+++ b/plugins/datetime/gsd-datetime-mechanism.h
@@ -98,6 +98,15 @@ gboolean            gsd_datetime_mechanism_set_hardware_clock_using_utc  (GsdDat
                                                                           gboolean               using_utc,
                                                                           DBusGMethodInvocation *context);
 
+gboolean            gsd_datetime_mechanism_get_using_ntp  (GsdDatetimeMechanism    *mechanism,
+                                                           DBusGMethodInvocation   *context);
+
+gboolean            gsd_datetime_mechanism_set_using_ntp  (GsdDatetimeMechanism    *mechanism,
+                                                           gboolean                 using_ntp,
+                                                           DBusGMethodInvocation   *context);
+
+gboolean            gsd_datetime_mechanism_can_set_using_ntp (GsdDatetimeMechanism  *mechanism,
+                                                              DBusGMethodInvocation *context);
 G_END_DECLS
 
 #endif /* GSD_DATETIME_MECHANISM_H */
diff --git a/plugins/datetime/gsd-datetime-mechanism.xml b/plugins/datetime/gsd-datetime-mechanism.xml
index 8ac37f9..4313870 100644
--- a/plugins/datetime/gsd-datetime-mechanism.xml
+++ b/plugins/datetime/gsd-datetime-mechanism.xml
@@ -89,5 +89,41 @@
       <arg name="is_using_utc" direction="in" type="b"/>
     </method>
 
+    <method name="GetUsingNtp">
+      <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
+      <arg name="can_use_ntp" direction="out" type="b"/>
+      <arg name="is_using_ntp" direction="out" type="b"/>
+    </method>
+    <method name="SetUsingNtp">
+      <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
+      <arg name="is_using_ntp" direction="in" type="b"/>
+    </method>
+    <method name="CanSetUsingNtp">
+      <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
+      <arg name="value" direction="out" type="i">
+        <doc:doc>
+          <doc:summary>Whether the caller can set the "use NTP" setting</doc:summary>
+          <doc:description>
+            <doc:para>
+              The return value is not a boolean, but an integer with the following meaning:
+              <doc:list>
+                <doc:item>
+                  <doc:term>0</doc:term>
+                  <doc:definition>the caller cannot change the "use NTP" setting</doc:definition>
+                </doc:item>
+                <doc:item>
+                  <doc:term>1</doc:term>
+                  <doc:definition>the caller will be challenged before being able to change "use NTP" setting</doc:definition>
+                </doc:item>
+                <doc:item>
+                  <doc:term>2</doc:term>
+                  <doc:definition>the caller is authorized to change the "use NTP" setting</doc:definition>
+                </doc:item>
+              </doc:list>
+            </doc:para>
+          </doc:description>
+        </doc:doc>
+      </arg>
+    </method>
   </interface>
 </node>
diff --git a/plugins/datetime/org.gnome.settingsdaemon.datetimemechanism.policy.in b/plugins/datetime/org.gnome.settingsdaemon.datetimemechanism.policy.in
index 5fdb8d8..6a480ce 100644
--- a/plugins/datetime/org.gnome.settingsdaemon.datetimemechanism.policy.in
+++ b/plugins/datetime/org.gnome.settingsdaemon.datetimemechanism.policy.in
@@ -35,4 +35,13 @@
     </defaults>
   </action>
 
+  <action id="org.gnome.settingsdaemon.datetimemechanism.configurentp">
+    <_description>Configure network time</_description>
+    <_message>Privileges are required to configure network time.</_message>
+    <defaults>
+      <allow_inactive>no</allow_inactive>
+      <allow_active>auth_self_keep</allow_active>
+    </defaults>
+  </action>
+
 </policyconfig>



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