[gnome-panel] libpanel-util: add PanelEndSessionDialog



commit 22e71c096139b06581ca339a1e1778a6521c2cff
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Thu Oct 23 02:28:36 2014 +0300

    libpanel-util: add PanelEndSessionDialog
    
    This will be used to request end session dialog to confirm or
    cancel hibernate, suspend ot hybrid sleep actions.

 gnome-panel/libpanel-util/Makefile.am              |   14 +
 .../org.freedesktop.login1.Manager.xml             |   35 +++
 .../libpanel-util/panel-end-session-dialog.c       |  292 ++++++++++++++++++++
 .../libpanel-util/panel-end-session-dialog.h       |   65 +++++
 4 files changed, 406 insertions(+), 0 deletions(-)
---
diff --git a/gnome-panel/libpanel-util/Makefile.am b/gnome-panel/libpanel-util/Makefile.am
index 20df2bc..16119e7 100644
--- a/gnome-panel/libpanel-util/Makefile.am
+++ b/gnome-panel/libpanel-util/Makefile.am
@@ -14,9 +14,18 @@ panel_util_enum_headers =    \
        panel-session-manager.h
 
 BUILT_SOURCES =                        \
+       dbus-login1-manager.c \
+       dbus-login1-manager.h \
        panel-util-types.c      \
        panel-util-types.h
 
+dbus-login1-manager.h:
+dbus-login1-manager.c: org.freedesktop.login1.Manager.xml Makefile.am
+       $(AM_V_GEN) gdbus-codegen \
+               --interface-prefix=org.freedesktop \
+               --generate-c-code dbus-login1-manager \
+               org.freedesktop.login1.Manager.xml
+
 panel-util-types.c: $(panel_util_enum_headers)
        $(AM_V_GEN)glib-mkenums \
                        --fhead "#include <glib-object.h>\n" \
@@ -51,6 +60,8 @@ libpanel_util_la_SOURCES =            \
        panel-cleanup.h                 \
        panel-dconf.c                   \
        panel-dconf.h                   \
+       panel-end-session-dialog.c \
+       panel-end-session-dialog.h \
        panel-color.c                   \
        panel-color.h                   \
        panel-error.c                   \
@@ -78,6 +89,9 @@ libpanel_util_la_SOURCES =            \
        panel-xdg.c                     \
        panel-xdg.h
 
+EXTRA_DIST = \
+       org.freedesktop.login1.Manager.xml
+
 CLEANFILES = \
        $(BUILT_SOURCES)
 
diff --git a/gnome-panel/libpanel-util/org.freedesktop.login1.Manager.xml 
b/gnome-panel/libpanel-util/org.freedesktop.login1.Manager.xml
new file mode 100644
index 0000000..0e4c3b8
--- /dev/null
+++ b/gnome-panel/libpanel-util/org.freedesktop.login1.Manager.xml
@@ -0,0 +1,35 @@
+<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" 
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd";>
+<node xmlns:doc="http://www.freedesktop.org/dbus/1.0/doc.dtd";>
+ <interface name="org.freedesktop.login1.Manager">
+  <method name="PowerOff">
+   <arg name="interactive" type="b" direction="in"/>
+  </method>
+  <method name="Reboot">
+   <arg name="interactive" type="b" direction="in"/>
+  </method>
+  <method name="Suspend">
+   <arg name="interactive" type="b" direction="in"/>
+  </method>
+  <method name="Hibernate">
+   <arg name="interactive" type="b" direction="in"/>
+  </method>
+  <method name="HybridSleep">
+   <arg name="interactive" type="b" direction="in"/>
+  </method>
+  <method name="CanPowerOff">
+   <arg name="result" type="s" direction="out"/>
+  </method>
+  <method name="CanReboot">
+   <arg name="result" type="s" direction="out"/>
+  </method>
+  <method name="CanSuspend">
+   <arg name="result" type="s" direction="out"/>
+  </method>
+  <method name="CanHibernate">
+   <arg name="result" type="s" direction="out"/>
+  </method>
+  <method name="CanHybridSleep">
+   <arg name="result" type="s" direction="out"/>
+  </method>
+ </interface>
+</node>
diff --git a/gnome-panel/libpanel-util/panel-end-session-dialog.c 
b/gnome-panel/libpanel-util/panel-end-session-dialog.c
new file mode 100644
index 0000000..85b8dbc
--- /dev/null
+++ b/gnome-panel/libpanel-util/panel-end-session-dialog.c
@@ -0,0 +1,292 @@
+/*
+ * panel-session.c:
+ *
+ * Copyright (C) 2008 Novell, Inc.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *     Vincent Untz <vuntz gnome org>
+ */
+
+#include <gio/gio.h>
+
+#include "dbus-login1-manager.h"
+#include "panel-cleanup.h"
+#include "panel-end-session-dialog.h"
+#include "panel-util-types.h"
+
+#define END_SESSION_DIALOG_NAME      "org.gnome.Shell"
+#define END_SESSION_DIALOG_PATH      "/org/gnome/SessionManager/EndSessionDialog"
+#define END_SESSION_DIALOG_INTERFACE "org.gnome.SessionManager.EndSessionDialog"
+
+#define AUTOMATIC_ACTION_TIMEOUT 60
+
+struct _PanelEndSessionDialogPrivate {
+       GDBusProxy    *dialog_proxy;
+       Login1Manager *login1_proxy;
+};
+
+/* Should match enum values with FlashbackLogoutAction in
+ * flashback-inhibit-dialog.h in gnome-flashback module */
+typedef enum {
+       REQUEST_TYPE_HIBERNATE    = 3,
+       REQUEST_TYPE_SUSPEND      = 4,
+       REQUEST_TYPE_HYBRID_SLEEP = 5
+} RequestType;
+
+G_DEFINE_TYPE_WITH_PRIVATE (PanelEndSessionDialog, panel_end_session_dialog, G_TYPE_OBJECT);
+
+static void
+open_ready_callback (GObject      *source_object,
+                     GAsyncResult *res,
+                     gpointer      user_data)
+{
+       PanelEndSessionDialog *dialog;
+       GError *error;
+       GVariant *ret;
+
+       dialog = PANEL_END_SESSION_DIALOG (user_data);
+       error = NULL;
+       ret = g_dbus_proxy_call_finish (dialog->priv->dialog_proxy, res, &error);
+
+       if (error) {
+               g_warning ("Unable to make Open call: %s", error->message);
+               g_error_free (error);
+               return;
+       }
+
+       g_variant_unref (ret);
+}
+
+static void
+panel_end_session_dialog_do_request (PanelEndSessionDialog *dialog,
+                                     RequestType            type)
+{
+       const gchar *inhibitors[] = { NULL };
+       GVariant *parameters;
+
+       g_return_if_fail (PANEL_IS_END_SESSION_DIALOG (dialog));
+
+       if (!dialog->priv->dialog_proxy) {
+               g_warning ("End session dialog is not available");
+               return;
+       }
+
+       parameters = g_variant_new ("(uuu^ao)",
+                                   (guint) type,
+                                   0,
+                                   AUTOMATIC_ACTION_TIMEOUT,
+                                   inhibitors);
+       g_dbus_proxy_call (dialog->priv->dialog_proxy,
+                          "Open",
+                          parameters,
+                          G_DBUS_CALL_FLAGS_NONE,
+                          -1,
+                          NULL,
+                          (GAsyncReadyCallback) open_ready_callback,
+                          dialog);
+}
+
+static void
+panel_end_session_dialog_on_signal (GDBusProxy *proxy,
+                                    gchar      *sender_name,
+                                    gchar      *signal_name,
+                                    GVariant   *parameters,
+                                    gpointer    user_data)
+{
+       PanelEndSessionDialog *dialog = PANEL_END_SESSION_DIALOG (user_data);
+
+       if (!dialog->priv->login1_proxy)
+               return;
+
+       if (g_str_equal ("ConfirmedHibernate", signal_name)) {
+               login1_manager_call_hibernate_sync (dialog->priv->login1_proxy,
+                                                   FALSE,
+                                                   NULL,
+                                                   NULL);
+       } else if (g_str_equal ("ConfirmedSuspend", signal_name)) {
+               login1_manager_call_suspend_sync (dialog->priv->login1_proxy,
+                                                 FALSE,
+                                                 NULL,
+                                                 NULL);
+       } else if (g_str_equal ("ConfirmedHybridSleep", signal_name)) {
+               login1_manager_call_hybrid_sleep_sync (dialog->priv->login1_proxy,
+                                                      FALSE,
+                                                      NULL,
+                                                      NULL);
+       }
+}
+
+static void
+panel_end_session_dialog_finalize (GObject *object)
+{
+       PanelEndSessionDialog *dialog = PANEL_END_SESSION_DIALOG (object);
+
+       g_clear_object (&dialog->priv->login1_proxy);
+       g_clear_object (&dialog->priv->dialog_proxy);
+
+       G_OBJECT_CLASS (panel_end_session_dialog_parent_class)->finalize (object);
+}
+
+static void
+panel_end_session_dialog_class_init (PanelEndSessionDialogClass *class)
+{
+       GObjectClass *gobject_class = G_OBJECT_CLASS (class);
+
+       gobject_class->finalize = panel_end_session_dialog_finalize;
+}
+
+static void
+panel_end_session_dialog_init (PanelEndSessionDialog *dialog)
+{
+       GError *error;
+
+       dialog->priv = panel_end_session_dialog_get_instance_private (dialog);
+
+       error = NULL;
+
+       dialog->priv->dialog_proxy =
+               g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
+                                              G_DBUS_PROXY_FLAGS_NONE,
+                                              NULL,
+                                              END_SESSION_DIALOG_NAME,
+                                              END_SESSION_DIALOG_PATH,
+                                              END_SESSION_DIALOG_INTERFACE,
+                                              NULL,
+                                              &error);
+
+       if (error) {
+               g_warning ("Could not connect to end session dialog: %s", error->message);
+               g_error_free (error);
+               return;
+       }
+
+       g_signal_connect (dialog->priv->dialog_proxy, "g-signal",
+                         G_CALLBACK (panel_end_session_dialog_on_signal), dialog);
+
+       dialog->priv->login1_proxy =
+               login1_manager_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
+                                                      G_DBUS_PROXY_FLAGS_NONE,
+                                                      "org.freedesktop.login1",
+                                                      "/org/freedesktop/login1",
+                                                      NULL,
+                                                      NULL);
+}
+
+PanelEndSessionDialog *
+panel_end_session_dialog_get (void)
+{
+       static PanelEndSessionDialog *dialog = NULL;
+
+       if (dialog == NULL) {
+               dialog = g_object_new (PANEL_TYPE_END_SESSION_DIALOG, NULL);
+               panel_cleanup_register (panel_cleanup_unref_and_nullify, &dialog);
+       }
+
+       return dialog;
+}
+
+gboolean
+panel_end_session_dialog_is_hibernate_available (PanelEndSessionDialog *dialog)
+{
+       gchar *result;
+       gboolean ret;
+
+       g_return_val_if_fail (PANEL_IS_END_SESSION_DIALOG (dialog), FALSE);
+
+       if (!dialog->priv->login1_proxy)
+               return FALSE;
+
+       login1_manager_call_can_hibernate_sync (dialog->priv->login1_proxy,
+                                               &result,
+                                               NULL,
+                                               NULL);
+
+       ret = FALSE;
+       if (g_str_equal ("yes", result))
+               ret = TRUE;
+
+       g_free (result);
+
+       return ret;
+}
+
+void
+panel_end_session_dialog_request_hibernate (PanelEndSessionDialog *dialog)
+{
+       panel_end_session_dialog_do_request (dialog, REQUEST_TYPE_HIBERNATE);
+}
+
+gboolean
+panel_end_session_dialog_is_suspend_available (PanelEndSessionDialog *dialog)
+{
+       gchar *result;
+       gboolean ret;
+
+       g_return_val_if_fail (PANEL_IS_END_SESSION_DIALOG (dialog), FALSE);
+
+       if (!dialog->priv->login1_proxy)
+               return FALSE;
+
+       login1_manager_call_can_suspend_sync (dialog->priv->login1_proxy,
+                                             &result,
+                                             NULL,
+                                             NULL);
+
+       ret = FALSE;
+       if (g_str_equal ("yes", result))
+               ret = TRUE;
+
+       g_free (result);
+
+       return ret;
+}
+
+void
+panel_end_session_dialog_request_suspend (PanelEndSessionDialog *dialog)
+{
+       panel_end_session_dialog_do_request (dialog, REQUEST_TYPE_SUSPEND);
+}
+
+gboolean
+panel_end_session_dialog_is_hybrid_sleep_available (PanelEndSessionDialog *dialog)
+{
+       gchar *result;
+       gboolean ret;
+
+       g_return_val_if_fail (PANEL_IS_END_SESSION_DIALOG (dialog), FALSE);
+
+       if (!dialog->priv->login1_proxy)
+               return FALSE;
+
+       login1_manager_call_can_hybrid_sleep_sync (dialog->priv->login1_proxy,
+                                                  &result,
+                                                  NULL,
+                                                  NULL);
+
+       ret = FALSE;
+       if (g_str_equal ("yes", result))
+               ret = TRUE;
+
+       g_free (result);
+
+       return ret;
+}
+
+void
+panel_end_session_dialog_request_hybrid_sleep (PanelEndSessionDialog *dialog)
+{
+       panel_end_session_dialog_do_request (dialog, REQUEST_TYPE_HYBRID_SLEEP);
+}
diff --git a/gnome-panel/libpanel-util/panel-end-session-dialog.h 
b/gnome-panel/libpanel-util/panel-end-session-dialog.h
new file mode 100644
index 0000000..8a284ee
--- /dev/null
+++ b/gnome-panel/libpanel-util/panel-end-session-dialog.h
@@ -0,0 +1,65 @@
+/*
+ * panel-end-session-dialog.h:
+ *
+ * Copyright (C) Alberts Muktupāvels
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *     Alberts Muktupāvels <alberts muktupavels gmail com>
+ */
+
+#ifndef PANEL_END_SESSION_DIALOG_H
+#define PANEL_END_SESSION_DIALOG_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define PANEL_TYPE_END_SESSION_DIALOG         (panel_end_session_dialog_get_type ())
+#define PANEL_END_SESSION_DIALOG(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), 
PANEL_TYPE_END_SESSION_DIALOG, PanelEndSessionDialog))
+#define PANEL_END_SESSION_DIALOG_CLASS(c)     (G_TYPE_CHECK_CLASS_CAST ((c),    
PANEL_TYPE_END_SESSION_DIALOG, PanelEndSessionDialogClass))
+#define PANEL_IS_END_SESSION_DIALOG(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), 
PANEL_TYPE_END_SESSION_DIALOG))
+#define PANEL_IS_END_SESSION_DIALOG_CLASS(c)  (G_TYPE_CHECK_CLASS_TYPE ((c),    
PANEL_TYPE_END_SESSION_DIALOG))
+#define PANEL_END_SESSION_DIALOG_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS((o),   
PANEL_TYPE_END_SESSION_DIALOG, PanelEndSessionDialogClass))
+
+typedef struct _PanelEndSessionDialog        PanelEndSessionDialog;
+typedef struct _PanelEndSessionDialogClass   PanelEndSessionDialogClass;
+typedef struct _PanelEndSessionDialogPrivate PanelEndSessionDialogPrivate;
+
+struct _PanelEndSessionDialog {
+       GObject                       parent;
+       PanelEndSessionDialogPrivate *priv;
+};
+
+struct _PanelEndSessionDialogClass {
+       GObjectClass parent_class;
+};
+
+GType                  panel_end_session_dialog_get_type                  (void);
+
+PanelEndSessionDialog *panel_end_session_dialog_get                       (void);
+
+gboolean               panel_end_session_dialog_is_hibernate_available    (PanelEndSessionDialog *dialog);
+void                   panel_end_session_dialog_request_hibernate         (PanelEndSessionDialog *dialog);
+
+gboolean               panel_end_session_dialog_is_suspend_available      (PanelEndSessionDialog *dialog);
+void                   panel_end_session_dialog_request_suspend           (PanelEndSessionDialog *dialog);
+
+gboolean               panel_end_session_dialog_is_hybrid_sleep_available (PanelEndSessionDialog *dialog);
+void                   panel_end_session_dialog_request_hybrid_sleep      (PanelEndSessionDialog *dialog);
+
+G_END_DECLS
+
+#endif /* PANEL_END_SESSION_DIALOG_H */


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