[gnome-desktop] idle-monitor: Make per-device monitor fallible



commit 5ca4cfcec81f711252def5860e945ef2af7d4b64
Author: Bastien Nocera <hadess hadess net>
Date:   Sat Mar 23 09:50:10 2013 +0100

    idle-monitor: Make per-device monitor fallible
    
    https://bugzilla.gnome.org/show_bug.cgi?id=696118

 libgnome-desktop/gnome-idle-monitor.c |   50 ++++++++++++++++++++++++++------
 libgnome-desktop/gnome-idle-monitor.h |    3 +-
 libgnome-desktop/test-idle-monitor.c  |    8 ++++-
 3 files changed, 49 insertions(+), 12 deletions(-)
---
diff --git a/libgnome-desktop/gnome-idle-monitor.c b/libgnome-desktop/gnome-idle-monitor.c
index 761ffa9..3a942ad 100644
--- a/libgnome-desktop/gnome-idle-monitor.c
+++ b/libgnome-desktop/gnome-idle-monitor.c
@@ -70,7 +70,11 @@ enum
 
 static GParamSpec *obj_props[PROP_LAST];
 
-G_DEFINE_TYPE (GnomeIdleMonitor, gnome_idle_monitor, G_TYPE_OBJECT)
+static void gnome_idle_monitor_initable_iface_init (GInitableIface *iface);
+
+G_DEFINE_TYPE_WITH_CODE (GnomeIdleMonitor, gnome_idle_monitor, G_TYPE_OBJECT,
+                        G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
+                                               gnome_idle_monitor_initable_iface_init))
 
 static gint64
 _xsyncvalue_to_int64 (XSyncValue value)
@@ -279,10 +283,9 @@ init_xsync (GnomeIdleMonitor *monitor)
        }
 
        monitor->priv->counter = find_idletime_counter (monitor);
-       if (monitor->priv->counter == None) {
-               g_warning ("GnomeIdleMonitor: IDLETIME counter not found");
+       /* IDLETIME counter not found? */
+       if (monitor->priv->counter == None)
                return;
-       }
 
        monitor->priv->user_active_alarm = _xsync_alarm_set (monitor, XSyncNegativeTransition, 1, FALSE);
 
@@ -352,6 +355,30 @@ gnome_idle_monitor_constructed (GObject *object)
        init_xsync (monitor);
 }
 
+static gboolean
+gnome_idle_monitor_initable_init (GInitable     *initable,
+                                 GCancellable  *cancellable,
+                                 GError       **error)
+{
+       GnomeIdleMonitor *monitor;
+
+       monitor = GNOME_IDLE_MONITOR (initable);
+
+       if (monitor->priv->counter == None) {
+               g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
+                                    "Per-device idletime is not supported");
+               return FALSE;
+       }
+
+       return TRUE;
+}
+
+static void
+gnome_idle_monitor_initable_iface_init (GInitableIface *iface)
+{
+       iface->init = gnome_idle_monitor_initable_init;
+}
+
 static void
 gnome_idle_monitor_class_init (GnomeIdleMonitorClass *klass)
 {
@@ -399,22 +426,25 @@ gnome_idle_monitor_init (GnomeIdleMonitor *monitor)
 GnomeIdleMonitor *
 gnome_idle_monitor_new (void)
 {
-       return GNOME_IDLE_MONITOR (g_object_new (GNOME_TYPE_IDLE_MONITOR, NULL));
+       return GNOME_IDLE_MONITOR (g_initable_new (GNOME_TYPE_IDLE_MONITOR, NULL, NULL, NULL));
 }
 
 /**
  * gnome_idle_monitor_new_for_device:
  * @device: A #GdkDevice to get the idle time for.
+ * @error: A pointer to a #GError or %NULL.
  *
  * Returns: a new #GnomeIdleMonitor that tracks the device-specific
- * idletime for @device. To track server-global idletime for all
- * devices, use gnome_idle_monitor_new().
+ * idletime for @device. If device-specific idletime is not available,
+ * %NULL is returned, and @error is set. To track server-global
+ * idletime for all devices, use gnome_idle_monitor_new().
  */
 GnomeIdleMonitor *
-gnome_idle_monitor_new_for_device (GdkDevice *device)
+gnome_idle_monitor_new_for_device (GdkDevice  *device,
+                                  GError    **error)
 {
-       return GNOME_IDLE_MONITOR (g_object_new (GNOME_TYPE_IDLE_MONITOR,
-                                                "device", device, NULL));
+       return GNOME_IDLE_MONITOR (g_initable_new (GNOME_TYPE_IDLE_MONITOR, NULL, error,
+                                                  "device", device, NULL));
 }
 
 /**
diff --git a/libgnome-desktop/gnome-idle-monitor.h b/libgnome-desktop/gnome-idle-monitor.h
index 523a62f..bedc319 100644
--- a/libgnome-desktop/gnome-idle-monitor.h
+++ b/libgnome-desktop/gnome-idle-monitor.h
@@ -62,7 +62,8 @@ typedef void (*GnomeIdleMonitorWatchFunc) (GnomeIdleMonitor      *monitor,
 GType              gnome_idle_monitor_get_type     (void);
 
 GnomeIdleMonitor * gnome_idle_monitor_new          (void);
-GnomeIdleMonitor * gnome_idle_monitor_new_for_device (GdkDevice *device);
+GnomeIdleMonitor * gnome_idle_monitor_new_for_device (GdkDevice  *device,
+                                                     GError    **error);
 
 guint              gnome_idle_monitor_add_idle_watch    (GnomeIdleMonitor         *monitor,
                                                         guint64                   interval_msec,
diff --git a/libgnome-desktop/test-idle-monitor.c b/libgnome-desktop/test-idle-monitor.c
index 1b808ff..268a4c1 100644
--- a/libgnome-desktop/test-idle-monitor.c
+++ b/libgnome-desktop/test-idle-monitor.c
@@ -70,9 +70,15 @@ device_added_cb (GdkDeviceManager *manager,
        GnomeIdleMonitor *monitor;
        guint watch_id;
        int device_id;
+       GError *error = NULL;
 
        device_id = gdk_x11_device_get_id (device);
-       monitor = gnome_idle_monitor_new_for_device (device);
+       monitor = gnome_idle_monitor_new_for_device (device, &error);
+       if (!monitor) {
+               g_warning ("Per-device idletime monitor not available: %s", error->message);
+               g_error_free (error);
+               return;
+       }
 
        watch_id = gnome_idle_monitor_add_idle_watch (monitor,
                                                      IDLE_TIME,


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