Blame SOURCES/0011-Tie-session-selector-to-properties-dialog.patch

f47161
From 7a80fca7e35d35eda415d4823ce54a4d45afb136 Mon Sep 17 00:00:00 2001
b31790
From: Ray Strode <rstrode@redhat.com>
b31790
Date: Fri, 20 Dec 2013 11:28:53 -0500
b31790
Subject: [PATCH 11/19] Tie session selector to properties dialog
b31790
b31790
---
f47161
 capplet/gsm-properties-dialog.c |  30 ++++-
b31790
 data/session-selector.ui        |   2 +-
f47161
 tools/gnome-session-selector.c  | 211 +++++++++++++++++++++++++-------
f47161
 tools/meson.build               |   3 +-
f47161
 4 files changed, 200 insertions(+), 46 deletions(-)
b31790
b31790
diff --git a/capplet/gsm-properties-dialog.c b/capplet/gsm-properties-dialog.c
b31790
index d2be778b..51fa5106 100644
b31790
--- a/capplet/gsm-properties-dialog.c
b31790
+++ b/capplet/gsm-properties-dialog.c
b31790
@@ -471,87 +471,113 @@ session_saved_message (GsmPropertiesDialog *dialog,
b31790
 {
b31790
         GtkLabel *label;
b31790
         gchar *markup;
b31790
         label = GTK_LABEL (gtk_builder_get_object (dialog->priv->xml, CAPPLET_SESSION_SAVED_WIDGET_NAME));
b31790
         if (is_error)
b31790
                 markup = g_markup_printf_escaped ("%s", msg);
b31790
         else
b31790
                 markup = g_markup_escape_text (msg, -1);
b31790
         gtk_label_set_markup (label, markup);
b31790
         g_free (markup);
b31790
 }
b31790
 
b31790
 static void
b31790
 session_saved_cb (DBusGProxy *proxy,
b31790
                   DBusGProxyCall *call_id,
b31790
                   void *user_data)
b31790
 {
b31790
         gboolean res;
b31790
         GsmPropertiesDialog *dialog = user_data;
b31790
 
b31790
         res = dbus_g_proxy_end_call (proxy, call_id, NULL, G_TYPE_INVALID);
b31790
         if (res)
b31790
                 session_saved_message (dialog, _("Your session has been saved."), FALSE);
b31790
         else
b31790
                 session_saved_message (dialog, _("Failed to save session"), TRUE);
b31790
 
b31790
         g_object_unref (proxy);
b31790
 }
b31790
 
b31790
 static void
b31790
-on_save_session_clicked (GtkWidget           *widget,
b31790
-                         GsmPropertiesDialog *dialog)
b31790
+save_session_directly (GsmPropertiesDialog *dialog)
b31790
 {
b31790
         DBusGConnection *conn;
b31790
         DBusGProxy *proxy;
b31790
         DBusGProxyCall *call;
b31790
 
b31790
         conn = dbus_g_bus_get (DBUS_BUS_SESSION, NULL);
b31790
         if (conn == NULL) {
b31790
                 session_saved_message (dialog, _("Could not connect to the session bus"), TRUE);
b31790
                 return;
b31790
         }
b31790
 
b31790
         proxy = dbus_g_proxy_new_for_name (conn, GSM_SERVICE_DBUS, GSM_PATH_DBUS, GSM_INTERFACE_DBUS);
b31790
         if (proxy == NULL) {
b31790
                 session_saved_message (dialog, _("Could not connect to the session manager"), TRUE);
b31790
                 return;
b31790
         }
b31790
 
b31790
         call = dbus_g_proxy_begin_call (proxy, "SaveSession", session_saved_cb, dialog, NULL, G_TYPE_INVALID);
b31790
         if (call == NULL) {
b31790
                 session_saved_message (dialog, _("Failed to save session"), TRUE);
b31790
                 g_object_unref (proxy);
b31790
                 return;
b31790
         }
b31790
 }
b31790
 
b31790
+static void
b31790
+save_session_from_selector (GsmPropertiesDialog *dialog,
b31790
+                            const char          *program_path)
b31790
+{
b31790
+        char *command_line = g_strdup_printf ("%s --action save", program_path);
b31790
+
b31790
+        g_spawn_command_line_sync (command_line, NULL, NULL, NULL, NULL);
b31790
+
b31790
+        g_free (command_line);
b31790
+}
b31790
+
b31790
+static void
b31790
+on_save_session_clicked (GtkWidget           *widget,
b31790
+                         GsmPropertiesDialog *dialog)
b31790
+{
b31790
+        char *program_path;
b31790
+
b31790
+        program_path = g_find_program_in_path ("gnome-session-selector");
b31790
+
b31790
+        if (program_path != NULL) {
b31790
+                save_session_from_selector (dialog, program_path);
b31790
+                g_free (program_path);
b31790
+        } else {
b31790
+                save_session_directly (dialog);
b31790
+        }
b31790
+}
b31790
+
b31790
 static void
b31790
 setup_dialog (GsmPropertiesDialog *dialog)
b31790
 {
b31790
         GtkTreeView       *treeview;
b31790
         GtkWidget         *button;
b31790
         GtkTreeModel      *tree_filter;
b31790
         GtkTreeViewColumn *column;
b31790
         GtkCellRenderer   *renderer;
b31790
         GtkTreeSelection  *selection;
b31790
         GtkTargetList     *targetlist;
b31790
 
b31790
         gtk_dialog_add_buttons (GTK_DIALOG (dialog),
b31790
                                 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
b31790
                                 NULL);
b31790
 
b31790
         dialog->priv->list_store = gtk_list_store_new (NUMBER_OF_COLUMNS,
b31790
                                                        G_TYPE_BOOLEAN,
b31790
                                                        G_TYPE_BOOLEAN,
b31790
                                                        G_TYPE_ICON,
b31790
                                                        G_TYPE_STRING,
b31790
                                                        G_TYPE_OBJECT,
b31790
                                                        G_TYPE_STRING);
b31790
         tree_filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (dialog->priv->list_store),
b31790
                                                  NULL);
b31790
         g_object_unref (dialog->priv->list_store);
b31790
         dialog->priv->tree_filter = tree_filter;
b31790
 
b31790
         gtk_tree_model_filter_set_visible_column (GTK_TREE_MODEL_FILTER (tree_filter),
b31790
                                                   STORE_COL_VISIBLE);
b31790
 
b31790
diff --git a/data/session-selector.ui b/data/session-selector.ui
b31790
index 1c55712d..1534a746 100644
b31790
--- a/data/session-selector.ui
b31790
+++ b/data/session-selector.ui
b31790
@@ -20,61 +20,61 @@
b31790
     <child>
b31790
       <object class="GtkFrame" id="frame1">
b31790
         <property name="visible">True</property>
b31790
         <property name="label_xalign">0.5</property>
b31790
         <property name="shadow_type">out</property>
b31790
         <child>
b31790
           <object class="GtkAlignment" id="alignment3">
b31790
             <property name="visible">True</property>
b31790
             <property name="border_width">12</property>
b31790
             <child>
b31790
               <object class="GtkVBox" id="vbox3">
b31790
                 <property name="visible">True</property>
b31790
                 <property name="orientation">vertical</property>
b31790
                 <property name="spacing">6</property>
b31790
 
b31790
                 <child>
b31790
                   <object class="GtkInfoBar" id="info-bar">
b31790
                     <property name="visible">True</property>
b31790
                     <property name="message-type">other</property>
b31790
 
b31790
                     <child internal-child="content_area">
b31790
                       <object class="GtkHBox" id="info-bar-content_area">
b31790
                         <property name="visible">True</property>
b31790
                         <property name="orientation">vertical</property>
b31790
                         <property name="spacing">0</property>
b31790
                         <child>
b31790
                           <object class="GtkLabel" id="info-label">
b31790
                             <property name="visible">True</property>
b31790
                             <property name="xalign">0.0</property>
b31790
                             <property name="yalign">0.5</property>
b31790
-                            <property name="label" translatable="yes">Please select a custom session to run</property>
b31790
+                            <property name="label" translatable="yes">Please select a custom session to use</property>
b31790
                           </object>
b31790
                           <packing>
b31790
                             <property name="expand">True</property>
b31790
                             <property name="fill">True</property>
b31790
                             <property name="position">0</property>
b31790
                           </packing>
b31790
                         </child>
b31790
                       </object>
b31790
                     </child>
b31790
                   </object>
b31790
                   <packing>
b31790
                     <property name="expand">False</property>
b31790
                     <property name="fill">True</property>
b31790
                     <property name="position">0</property>
b31790
                   </packing>
b31790
                 </child>
b31790
                 <child>
b31790
                   <object class="GtkVBox" id="vbox4">
b31790
                     <property name="visible">True</property>
b31790
                     <property name="orientation">vertical</property>
b31790
                     <property name="spacing">12</property>
b31790
                     <child>
b31790
                       <object class="GtkHBox" id="hbox3">
b31790
                         <property name="visible">True</property>
b31790
                         <property name="spacing">12</property>
b31790
                         <child>
b31790
                           <object class="GtkScrolledWindow" id="scrolledwindow2">
b31790
                             <property name="visible">True</property>
b31790
                             <property name="can_focus">True</property>
b31790
                             <property name="hscrollbar_policy">never</property>
b31790
diff --git a/tools/gnome-session-selector.c b/tools/gnome-session-selector.c
b31790
index 71892c43..53822f6c 100644
b31790
--- a/tools/gnome-session-selector.c
b31790
+++ b/tools/gnome-session-selector.c
b31790
@@ -7,126 +7,133 @@
b31790
  * the Free Software Foundation; either version 2 of the License, or
b31790
  * (at your option) any later version.
b31790
  *
b31790
  * This program is distributed in the hope that it will be useful,
b31790
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
b31790
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
b31790
  * GNU General Public License for more details.
b31790
  *
b31790
  * You should have received a copy of the GNU General Public License
b31790
  * along with this program; if not, see <http://www.gnu.org/licenses/>.
b31790
  *
b31790
  * Written by: Matthias Clasen <mclasen@redhat.com>
b31790
  */
b31790
 
b31790
 #include "config.h"
b31790
 
b31790
 #include <fcntl.h>
b31790
 #include <stdlib.h>
b31790
 #include <string.h>
b31790
 #include <sys/types.h>
b31790
 #include <sys/stat.h>
b31790
 #include <unistd.h>
b31790
 
b31790
 #include <glib.h>
b31790
 #include <gtk/gtk.h>
b31790
 #include <gio/gio.h>
b31790
 
b31790
 #include <glib/gi18n.h>
b31790
 #include <glib/gstdio.h>
b31790
 
b31790
+#include <dbus/dbus-glib.h>
b31790
+#include <dbus/dbus-glib-lowlevel.h>
b31790
+
b31790
+#define GSM_SERVICE_DBUS   "org.gnome.SessionManager"
b31790
+#define GSM_PATH_DBUS      "/org/gnome/SessionManager"
b31790
+#define GSM_INTERFACE_DBUS "org.gnome.SessionManager"
b31790
+
b31790
 #define GSM_MANAGER_SCHEMA        "org.gnome.SessionManager"
b31790
 #define KEY_AUTOSAVE_ONE_SHOT     "auto-save-session-one-shot"
b31790
 
b31790
 static GtkBuilder *builder;
b31790
 static GtkWidget *session_list;
b31790
 static GtkListStore *store;
b31790
 static GtkTreeModelSort *sort_model;
b31790
+static char *info_text;
b31790
 
b31790
 static void select_session (const char *name);
b31790
+static gboolean make_session_current (const char *name);
b31790
 
b31790
 static char *
b31790
 get_session_path (const char *name)
b31790
 {
b31790
         return g_build_filename (g_get_user_config_dir (), "gnome-session", name, NULL);
b31790
 }
b31790
 
b31790
 static char *
b31790
 find_new_session_name (void)
b31790
 {
b31790
         char *name;
b31790
         char *path;
b31790
         int i;
b31790
 
b31790
         for (i = 1; i < 20; i++) {
b31790
                 name = g_strdup_printf (_("Session %d"), i);
b31790
                 path = get_session_path (name);
b31790
                 if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
b31790
                         g_free (path);
b31790
                         return name;
b31790
                 }
b31790
                 g_free (path);
b31790
                 g_free (name);
b31790
         }
b31790
 
b31790
         return NULL;
b31790
 }
b31790
 
b31790
 static gboolean
b31790
 is_valid_session_name (const char *name)
b31790
 {
b31790
         GtkTreeIter iter;
b31790
         char *n;
b31790
-        const char *info_text;
b31790
         char *warning_text;
b31790
         gboolean user_tried_dot;
b31790
         gboolean user_tried_slash;
b31790
         GtkWidget *info_bar;
b31790
         GtkWidget *label;
b31790
 
b31790
         if (name[0] == 0) {
b31790
                 return FALSE;
b31790
         }
b31790
 
b31790
         if (name[0] == '.') {
b31790
             user_tried_dot = TRUE;
b31790
         } else {
b31790
             user_tried_dot = FALSE;
b31790
         }
b31790
 
b31790
         if (strchr (name, '/') != NULL) {
b31790
             user_tried_slash = TRUE;
b31790
         } else {
b31790
             user_tried_slash = FALSE;
b31790
         }
b31790
 
b31790
-        info_text = _("Please select a custom session to run");
b31790
         warning_text = NULL;
b31790
         if (user_tried_dot && user_tried_slash) {
b31790
             warning_text = g_strdup_printf ("%s\n<small>Note: %s</small>",
b31790
                                             info_text,
b31790
                                             _("Session names are not allowed to start with “.” or contain “/” characters"));
b31790
         } else if (user_tried_dot) {
b31790
             warning_text = g_strdup_printf ("%s\n<small>Note: %s</small>",
b31790
                                             info_text,
b31790
                                             _("Session names are not allowed to start with “.”"));
b31790
         } else if (user_tried_slash) {
b31790
             warning_text = g_strdup_printf ("%s\n<small>Note: %s</small>",
b31790
                                             info_text,
b31790
                                             _("Session names are not allowed to contain “/” characters"));
b31790
         }
b31790
 
b31790
         gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter);
b31790
         do {
b31790
                 gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, 0, &n, -1);
b31790
                 if (strcmp (n, name) == 0) {
b31790
                         char *message;
b31790
                         message = g_strdup_printf (_("A session named “%s” already exists"), name);
b31790
                         warning_text = g_strdup_printf ("%s\n<small>Note: %s</small>", info_text, message);
b31790
                         g_free (message);
b31790
                         g_free (n);
b31790
                         break;
b31790
                 }
b31790
                 g_free (n);
b31790
         } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter));
b31790
 
b31790
         info_bar = (GtkWidget *) gtk_builder_get_object (builder, "info-bar");
b31790
@@ -181,518 +188,638 @@ populate_session_list (GtkWidget *session_list)
b31790
 
b31790
         default_name = NULL;
b31790
         if (readlink (saved_session, last_session, PATH_MAX - 1) > 0) {
b31790
                 default_name = g_path_get_basename (last_session);
b31790
         }
b31790
 
b31790
         while ((name = g_dir_read_name (dir)) != NULL) {
b31790
                 if (strcmp (name, "saved-session") == 0)
b31790
                         continue;
b31790
 
b31790
                 gtk_list_store_insert_with_values (store, &iter, 100, 0, name, -1);
b31790
 
b31790
                 if (g_strcmp0 (default_name, name) == 0) {
b31790
                         GtkTreeSelection *selection;
b31790
                         GtkTreeIter child_iter;
b31790
 
b31790
                         gtk_tree_model_sort_convert_child_iter_to_iter (GTK_TREE_MODEL_SORT (sort_model), &child_iter, &iter);
b31790
                         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (session_list));
b31790
                         gtk_tree_selection_select_iter (selection, &child_iter);
b31790
                 }
b31790
         }
b31790
 
b31790
         g_free (default_name);
b31790
         g_dir_close (dir);
b31790
 
b31790
  out:
b31790
         g_free (saved_session);
b31790
         g_free (path);
b31790
 }
b31790
 
b31790
+static char *
b31790
+get_last_session (void)
b31790
+{
b31790
+        char *saved_session;
b31790
+        char last_session[PATH_MAX] = "";
b31790
+        char *name = NULL;
b31790
+
b31790
+        saved_session = get_session_path ("saved-session");
b31790
+
b31790
+        if (readlink (saved_session, last_session, PATH_MAX - 1) > 0) {
b31790
+                name = g_path_get_basename (last_session);
b31790
+        }
b31790
+
b31790
+        g_free (saved_session);
b31790
+
b31790
+        return name;
b31790
+}
b31790
+
b31790
 static char *
b31790
 get_selected_session (void)
b31790
 {
b31790
         GtkTreeSelection *selection;
b31790
         GtkTreeModel *model;
b31790
         GtkTreeIter iter;
b31790
         gchar *name;
b31790
 
b31790
         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (session_list));
b31790
         if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
b31790
                 gtk_tree_model_get (model, &iter, 0, &name, -1);
b31790
                 return name;
b31790
         }
b31790
 
b31790
         return NULL;
b31790
 }
b31790
 
b31790
 static void
b31790
 remove_session (const char *name)
b31790
 {
b31790
         char *path1, *path2;
b31790
         char *n, *path;
b31790
         const char *d;
b31790
         GDir *dir;
b31790
         GError *error;
b31790
 
b31790
         path1 = get_session_path ("saved-session");
b31790
         path2 = get_session_path (name);
b31790
 
b31790
         error = NULL;
b31790
         n = g_file_read_link (path1, &error);
b31790
         if (n == NULL) {
b31790
                 g_warning ("Failed to read link: %s", error->message);
b31790
                 g_error_free (error);
b31790
         }
b31790
         else if (strcmp (n, name) == 0) {
b31790
                 unlink (path1);
b31790
         }
b31790
         g_free (n);
b31790
 
b31790
         dir = g_dir_open (path2, 0, NULL);
b31790
         while ((d = g_dir_read_name (dir)) != NULL) {
b31790
                 path = g_build_filename (path2, d, NULL);
b31790
                 unlink (path);
b31790
                 g_free (path);
b31790
         }
b31790
         g_dir_close (dir);
b31790
 
b31790
         remove (path2);
b31790
 
b31790
         g_free (path1);
b31790
         g_free (path2);
b31790
 }
b31790
 
b31790
+static gboolean
b31790
+make_session_current (const char *name)
b31790
+{
b31790
+        char *path1;
b31790
+        gboolean ret = TRUE;
b31790
+
b31790
+        path1 = g_build_filename (g_get_user_config_dir (), "gnome-session", "saved-session", NULL);
b31790
+
b31790
+        unlink (path1);
b31790
+        if (symlink (name, path1) < 0) {
b31790
+                g_warning ("Failed to make session '%s' current", name);
b31790
+                ret = FALSE;
b31790
+        }
b31790
+
b31790
+        g_free (path1);
b31790
+
b31790
+        return ret;
b31790
+}
b31790
+
b31790
 static void
b31790
 on_remove_session_clicked (GtkButton *button,
b31790
                            gpointer   data)
b31790
 {
b31790
         GtkTreeSelection *selection;
b31790
         GtkTreeModel *model;
b31790
         GtkTreeIter iter;
b31790
         char *name;
b31790
 
b31790
         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (session_list));
b31790
         if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
b31790
                 GtkTreeIter child_iter;
b31790
                 gtk_tree_model_get (model, &iter, 0, &name, -1);
b31790
                 remove_session (name);
b31790
                 g_free (name);
b31790
 
b31790
                 gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (model), &child_iter, &iter);
b31790
                 gtk_list_store_remove (GTK_LIST_STORE (store), &child_iter);
b31790
 
b31790
                 if (!gtk_tree_selection_get_selected (selection, NULL, NULL)) {
b31790
                         gtk_tree_model_get_iter_first (model, &iter);
b31790
                         gtk_tree_model_get (model, &iter, 0, &name, -1);
b31790
                         select_session (name);
b31790
+                        make_session_current (name);
b31790
                         g_free (name);
b31790
                 }
b31790
         }
b31790
 }
b31790
 
b31790
 static void
b31790
 begin_rename (void)
b31790
 {
b31790
         GtkTreePath *path;
b31790
         GtkTreeViewColumn *column;
b31790
         GList *cells;
b31790
 
b31790
         gtk_widget_grab_focus (session_list);
b31790
 
b31790
         gtk_tree_view_get_cursor (GTK_TREE_VIEW (session_list),
b31790
                                   &path, &column);
b31790
 
b31790
         cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
b31790
 
b31790
         if (cells != NULL) {
b31790
             GtkCellRenderer *cell;
b31790
 
b31790
             cell = (GtkCellRenderer *) cells->data;
b31790
             g_list_free (cells);
b31790
 
b31790
             g_object_set (cell, "editable", TRUE, NULL);
b31790
             gtk_tree_view_set_cursor_on_cell (GTK_TREE_VIEW (session_list), path,
b31790
                                               column, cell, TRUE);
b31790
         }
b31790
         gtk_tree_path_free (path);
b31790
 }
b31790
 
b31790
 static void
b31790
 on_rename_session_clicked (GtkButton *button,
b31790
                            gpointer   data)
b31790
 {
b31790
     begin_rename ();
b31790
 }
b31790
 
b31790
 static void
b31790
 on_continue_clicked (GtkButton *button,
b31790
                      gpointer    data)
b31790
 {
b31790
         char *name;
b31790
 
b31790
         name = get_selected_session ();
b31790
         g_free (name);
b31790
 
b31790
         gtk_main_quit ();
b31790
 }
b31790
 
b31790
 static void
b31790
 create_session (const char *name)
b31790
 {
b31790
         char *path;
b31790
         GtkTreeIter iter;
b31790
 
b31790
         path = get_session_path (name);
b31790
 
b31790
-        if (mkdir (path, 0755) < 0) {
b31790
+        if (g_mkdir_with_parents (path, 0755) < 0) {
b31790
                 g_warning ("Failed to create directory %s", path);
b31790
         }
b31790
         else {
b31790
                 char *marker;
b31790
 
b31790
                 gtk_list_store_insert_with_values (store, &iter, 100, 0, name, -1);
b31790
 
b31790
                 marker = g_build_filename (path, ".new-session", NULL);
b31790
                 creat (marker, 0600);
b31790
                 g_free (marker);
b31790
         }
b31790
 
b31790
         g_free (path);
b31790
 }
b31790
 
b31790
 static gboolean
b31790
 rename_session (const char *old_name,
b31790
                 const char *new_name)
b31790
 {
b31790
         char *old_path, *new_path;
b31790
         int result;
b31790
 
b31790
         if (g_strcmp0 (old_name, new_name) == 0) {
b31790
                 return TRUE;
b31790
         }
b31790
 
b31790
         if (!is_valid_session_name (new_name)) {
b31790
                return FALSE;
b31790
         }
b31790
 
b31790
         old_path = get_session_path (old_name);
b31790
         new_path = get_session_path (new_name);
b31790
 
b31790
         result = g_rename (old_path, new_path);
b31790
 
b31790
         if (result < 0) {
b31790
                 g_warning ("Failed to rename session from '%s' to '%s': %m", old_name, new_name);
b31790
+        } else {
b31790
+                char *last_session;
b31790
+                last_session = get_last_session ();
b31790
+                if (g_strcmp0 (old_name, last_session) == 0) {
b31790
+                        make_session_current (new_name);
b31790
+                }
b31790
+                g_free (last_session);
b31790
         }
b31790
 
b31790
         g_free (old_path);
b31790
         g_free (new_path);
b31790
-
b31790
         return result == 0;
b31790
 }
b31790
 
b31790
-static gboolean
b31790
-make_session_current (const char *name)
b31790
-{
b31790
-        char *path1;
b31790
-        gboolean ret = TRUE;
b31790
-
b31790
-        path1 = g_build_filename (g_get_user_config_dir (), "gnome-session", "saved-session", NULL);
b31790
-
b31790
-        unlink (path1);
b31790
-        if (symlink (name, path1) < 0) {
b31790
-                g_warning ("Failed to make session '%s' current", name);
b31790
-                ret = FALSE;
b31790
-        }
b31790
-
b31790
-        g_free (path1);
b31790
-
b31790
-        return ret;
b31790
-}
b31790
-
b31790
 static gboolean
b31790
 create_and_select_session (const char *name)
b31790
 {
b31790
         gchar *path;
b31790
 
b31790
         if (name[0] == 0 || name[0] == '.' || strchr (name, '/')) {
b31790
                 g_warning ("Invalid session name");
b31790
                 return FALSE;
b31790
         }
b31790
 
b31790
         path = g_build_filename (g_get_user_config_dir (), "gnome-session", name, NULL);
b31790
         if (!g_file_test (path, G_FILE_TEST_IS_DIR)) {
b31790
-                if (mkdir (path, 0755) < 0) {
b31790
+                if (g_mkdir_with_parents (path, 0755) < 0) {
b31790
                         g_warning ("Failed to create directory %s", path);
b31790
                         g_free (path);
b31790
                         return FALSE;
b31790
                 }
b31790
         }
b31790
 
b31790
         g_free (path);
b31790
 
b31790
         return make_session_current (name);
b31790
 }
b31790
 
b31790
 static void
b31790
 select_session (const char *name)
b31790
 {
b31790
         GtkTreeIter iter;
b31790
         char *n;
b31790
 
b31790
-        make_session_current (name);
b31790
-
b31790
         /* now select it in the list */
b31790
         gtk_tree_model_get_iter_first (GTK_TREE_MODEL (sort_model), &iter);
b31790
         do {
b31790
                 gtk_tree_model_get (GTK_TREE_MODEL (sort_model), &iter, 0, &n, -1);
b31790
                 if (strcmp (n, name) == 0) {
b31790
                         GtkTreePath *path;
b31790
 
b31790
                         path = gtk_tree_model_get_path (GTK_TREE_MODEL (sort_model), &iter);
b31790
                         gtk_tree_view_set_cursor (GTK_TREE_VIEW (session_list), path, NULL, FALSE);
b31790
                         gtk_tree_path_free (path);
b31790
                         g_free (n);
b31790
                         break;
b31790
                 }
b31790
                 g_free (n);
b31790
         } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (sort_model), &iter));
b31790
 }
b31790
 
b31790
 static void
b31790
-on_new_session_clicked (GtkButton *button,
b31790
-                        gpointer   data)
b31790
+create_session_and_begin_rename (void)
b31790
 {
b31790
         gchar *name;
b31790
 
b31790
         name = find_new_session_name ();
b31790
         create_session (name);
b31790
         select_session (name);
b31790
 
b31790
         begin_rename ();
b31790
 }
b31790
 
b31790
+static void
b31790
+on_new_session_clicked (GtkButton *button,
b31790
+                        gpointer   data)
b31790
+{
b31790
+	create_session_and_begin_rename ();
b31790
+}
b31790
+
b31790
 static void
b31790
 on_selection_changed (GtkTreeSelection *selection,
b31790
                       gpointer          data)
b31790
 {
b31790
         char *name;
b31790
 
b31790
         name = get_selected_session ();
b31790
 
b31790
         if (name == NULL) {
b31790
                 return;
b31790
         }
b31790
 
b31790
-        make_session_current (name);
b31790
-
b31790
         g_free (name);
b31790
 }
b31790
 
b31790
 static void
b31790
 update_remove_button (void)
b31790
 {
b31790
         GtkWidget *button;
b31790
 
b31790
         button = (GtkWidget *)gtk_builder_get_object (builder, "remove-session");
b31790
         if (gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL) > 1) {
b31790
                 gtk_widget_set_sensitive (button, TRUE);
b31790
         } else {
b31790
                 gtk_widget_set_sensitive (button, FALSE);
b31790
         }
b31790
 }
b31790
 
b31790
 static void
b31790
 on_row_edited (GtkCellRendererText *cell,
b31790
                const char          *path_string,
b31790
                const char          *new_name,
b31790
                gpointer             data)
b31790
 {
b31790
         GtkTreePath *path;
b31790
         GtkTreeIter  sort_iter, items_iter;
b31790
         char        *old_name;
b31790
         gboolean     was_renamed;
b31790
 
b31790
         path = gtk_tree_path_new_from_string (path_string);
b31790
         gtk_tree_model_get_iter (GTK_TREE_MODEL (sort_model), &sort_iter, path);
b31790
 
b31790
         gtk_tree_model_get (GTK_TREE_MODEL (sort_model), &sort_iter, 0, &old_name, -1);
b31790
 
b31790
         was_renamed = rename_session (old_name, new_name);
b31790
 
b31790
         if (was_renamed) {
b31790
                 gtk_tree_model_sort_convert_iter_to_child_iter (sort_model, &items_iter, &sort_iter);
b31790
 
b31790
                 gtk_list_store_set (store, &items_iter, 0, g_strdup (new_name), -1);
b31790
                 g_free (old_name);
b31790
-                make_session_current (new_name);
b31790
         } else {
b31790
                 begin_rename ();
b31790
         }
b31790
 
b31790
         gtk_tree_path_free (path);
b31790
 
b31790
         g_object_set (cell, "editable", FALSE, NULL);
b31790
 }
b31790
 
b31790
 static void
b31790
 on_row_deleted (GtkTreeModel *model,
b31790
                 GtkTreePath  *path,
b31790
                 gpointer      data)
b31790
 {
b31790
         update_remove_button ();
b31790
 }
b31790
 
b31790
 static void
b31790
 on_row_inserted (GtkTreeModel *model,
b31790
                  GtkTreePath  *path,
b31790
                  GtkTreeIter  *iter,
b31790
                  gpointer      data)
b31790
 {
b31790
         update_remove_button ();
b31790
 }
b31790
 
b31790
 static void
b31790
 on_row_activated (GtkTreeView       *tree_view,
b31790
                   GtkTreePath       *path,
b31790
                   GtkTreeViewColumn *column,
b31790
                   gpointer           data)
b31790
 {
b31790
-        char *name;
b31790
-
b31790
-        name = get_selected_session ();
b31790
-        g_free (name);
b31790
-
b31790
         gtk_main_quit ();
b31790
 }
b31790
 
b31790
 static void
b31790
 auto_save_next_session (void)
b31790
 {
b31790
         GSettings *settings;
b31790
 
b31790
         settings = g_settings_new (GSM_MANAGER_SCHEMA);
b31790
         g_settings_set_boolean (settings, KEY_AUTOSAVE_ONE_SHOT, TRUE);
b31790
         g_object_unref (settings);
b31790
 }
b31790
 
b31790
 static void
b31790
 auto_save_next_session_if_needed (void)
b31790
 {
b31790
         char *marker;
b31790
 
b31790
         marker = g_build_filename (g_get_user_config_dir (),
b31790
                                    "gnome-session", "saved-session",
b31790
                                    ".new-session", NULL);
b31790
 
b31790
         if (g_file_test (marker, G_FILE_TEST_EXISTS)) {
b31790
                 auto_save_next_session ();
b31790
                 unlink (marker);
b31790
         }
b31790
         g_free (marker);
b31790
 }
b31790
 
b31790
+static void
b31790
+save_session (void)
b31790
+{
b31790
+        DBusGConnection *conn;
b31790
+        DBusGProxy *proxy;
b31790
+        GError *error;
b31790
+
b31790
+        conn = dbus_g_bus_get (DBUS_BUS_SESSION, NULL);
b31790
+        if (conn == NULL) {
b31790
+                g_warning ("Could not connect to the session bus");
b31790
+                return;
b31790
+        }
b31790
+
b31790
+        proxy = dbus_g_proxy_new_for_name (conn, GSM_SERVICE_DBUS, GSM_PATH_DBUS, GSM_INTERFACE_DBUS);
b31790
+        if (proxy == NULL) {
b31790
+                g_warning ("Could not connect to the session manager");
b31790
+                return;
b31790
+        }
b31790
+
b31790
+        error = NULL;
b31790
+        if (!dbus_g_proxy_call (proxy, "SaveSession", &error, G_TYPE_INVALID, G_TYPE_INVALID)) {
b31790
+                g_warning ("Failed to save session: %s", error->message);
b31790
+                g_error_free (error);
b31790
+                return;
b31790
+        }
b31790
+
b31790
+        g_object_unref (proxy);
b31790
+}
b31790
+
b31790
 static int
b31790
 compare_sessions (GtkTreeModel *model,
b31790
                   GtkTreeIter  *a,
b31790
                   GtkTreeIter  *b,
b31790
                   gpointer      data)
b31790
 {
b31790
     char *name_a, *name_b;
b31790
     int result;
b31790
 
b31790
     gtk_tree_model_get (model, a, 0, &name_a, -1);
b31790
     gtk_tree_model_get (model, b, 0, &name_b, -1);
b31790
 
b31790
     result = g_utf8_collate (name_a, name_b);
b31790
 
b31790
     g_free (name_a);
b31790
     g_free (name_b);
b31790
 
b31790
     return result;
b31790
 }
b31790
 
b31790
 static void
b31790
 on_map (GtkWidget *widget,
b31790
         gpointer   data)
b31790
 {
b31790
         gdk_window_focus (gtk_widget_get_window (widget), GDK_CURRENT_TIME);
b31790
 }
b31790
 
b31790
 int
b31790
 main (int argc, char *argv[])
b31790
 {
b31790
         GtkWidget *window;
b31790
         GtkWidget *widget;
b31790
+        GtkWidget *label;
b31790
         GtkCellRenderer *cell;
b31790
         GtkTreeViewColumn *column;
b31790
         GtkTreeSelection *selection;
b31790
         GError *error;
b31790
+        char *selected_session;
b31790
+
b31790
+        static char *action = NULL;
b31790
+        static char **remaining_args = NULL;
b31790
+        static GOptionEntry entries[] = {
b31790
+                {"action", '\0', 0, G_OPTION_ARG_STRING, &action, N_("What to do with session selection (save|load|print)"), NULL},
b31790
+{ G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &remaining_args, N_("[session-name]"), NULL}
b31790
+        };
b31790
+
b31790
+        if (action == NULL) {
b31790
+            if (getenv ("SESSION_MANAGER") != NULL)
b31790
+                action = "print";
b31790
+            else
b31790
+                action = "load";
b31790
+        }
b31790
 
b31790
-        if (getenv ("SESSION_MANAGER") != NULL)
b31790
+        if (getenv ("SESSION_MANAGER") != NULL && strcmp (action, "load") == 0) {
b31790
+            g_warning ("Cannot load new session when session currently loaded");
b31790
             return 1;
b31790
+        }
b31790
+
b31790
+        if (getenv ("SESSION_MANAGER") == NULL && strcmp (action, "save") == 0) {
b31790
+            g_warning ("Can only save session when session loaded");
b31790
+            return 1;
b31790
+        }
b31790
+
b31790
+        if (strcmp (action, "load") != 0 && strcmp (action, "save") != 0 && strcmp (action, "print") != 0) {
b31790
+            g_warning ("'%s' is not a supported action.  Supported actions are load, save, and print.\n", action);
b31790
+            return 1;
b31790
+        }
b31790
 
b31790
-        gtk_init (&argc, &argv);
b31790
-        if (argc > 1) {
b31790
-                g_print ("create and select session\n");
b31790
-                if (!create_and_select_session (argv[1]))
b31790
+        error = NULL;
b31790
+        gtk_init_with_args (&argc, &argv,
b31790
+                            NULL, entries, GETTEXT_PACKAGE, &error);
b31790
+
b31790
+        if (remaining_args != NULL) {
b31790
+                if (g_strv_length (remaining_args) > 1) {
b31790
+                        g_warning ("gnome-session-selector takes at most one session argument");
b31790
+                        return 1;
b31790
+                }
b31790
+
b31790
+                if (!create_and_select_session (remaining_args[0]))
b31790
                         return 1;
b31790
                 else
b31790
                         return 0;
b31790
         }
b31790
 
b31790
         builder = gtk_builder_new ();
b31790
         gtk_builder_set_translation_domain (builder, GETTEXT_PACKAGE);
b31790
 
b31790
         error = NULL;
b31790
         if (!gtk_builder_add_from_file (builder, GTKBUILDER_DIR "/" "session-selector.ui",  &error)) {
b31790
                 g_warning ("Could not load file 'session-selector.ui': %s", error->message);
b31790
                 exit (1);
b31790
         }
b31790
 
b31790
         window = (GtkWidget *) gtk_builder_get_object (builder, "main-window");
b31790
 
b31790
         store = (GtkListStore *) gtk_builder_get_object (builder, "session-store");
b31790
         sort_model = (GtkTreeModelSort *) gtk_builder_get_object (builder, "sort-model");
b31790
 
b31790
         gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (sort_model),
b31790
                                          0, compare_sessions, NULL, NULL);
b31790
         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (sort_model),
b31790
                                               0, GTK_SORT_ASCENDING);
b31790
         g_signal_connect (store, "row-deleted", G_CALLBACK (on_row_deleted), NULL);
b31790
         g_signal_connect (store, "row-inserted", G_CALLBACK (on_row_inserted), NULL);
b31790
         session_list = (GtkWidget *) gtk_builder_get_object (builder, "session-list");
b31790
 
b31790
         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (session_list));
b31790
         gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
b31790
 
b31790
         populate_session_list (session_list);
b31790
 
b31790
         cell = gtk_cell_renderer_text_new ();
b31790
         g_signal_connect (cell, "edited", G_CALLBACK (on_row_edited), NULL);
b31790
 
b31790
         column = gtk_tree_view_column_new_with_attributes ("", cell, "text", 0, NULL);
b31790
         gtk_tree_view_append_column (GTK_TREE_VIEW (session_list), GTK_TREE_VIEW_COLUMN (column));
b31790
 
b31790
         g_signal_connect (session_list, "row-activated", G_CALLBACK (on_row_activated), NULL);
b31790
 
b31790
         g_signal_connect (selection, "changed",
b31790
                           G_CALLBACK (on_selection_changed), NULL);
b31790
 
b31790
         widget = (GtkWidget *) gtk_builder_get_object (builder, "new-session");
b31790
         g_signal_connect (widget, "clicked", G_CALLBACK (on_new_session_clicked), NULL);
b31790
         widget = (GtkWidget *) gtk_builder_get_object (builder, "remove-session");
b31790
         g_signal_connect (widget, "clicked", G_CALLBACK (on_remove_session_clicked), NULL);
b31790
         widget = (GtkWidget *) gtk_builder_get_object (builder, "rename-session");
b31790
         g_signal_connect (widget, "clicked", G_CALLBACK (on_rename_session_clicked), NULL);
b31790
         widget = (GtkWidget *) gtk_builder_get_object (builder, "continue-button");
b31790
         g_signal_connect (widget, "clicked", G_CALLBACK (on_continue_clicked), NULL);
b31790
 
b31790
         g_signal_connect (window, "map", G_CALLBACK (on_map), NULL);
b31790
         gtk_widget_show (window);
b31790
 
b31790
+        if (g_strcmp0 (action, "load") == 0) {
b31790
+            info_text = _("Please select a custom session to run");
b31790
+        } else if (g_strcmp0 (action, "print") == 0) {
b31790
+            info_text = _("Please select a session to use");
b31790
+        } else if (g_strcmp0 (action, "save") == 0) {
b31790
+            info_text = _("Please select a session to save to");
b31790
+        }
b31790
+
b31790
+        label = (GtkWidget*) gtk_builder_get_object (builder, "info-label");
b31790
+        gtk_label_set_markup (GTK_LABEL (label), info_text);
b31790
+
b31790
+        selected_session = get_selected_session ();
b31790
+
b31790
+        if (selected_session == NULL) {
b31790
+		create_session_and_begin_rename ();
b31790
+	} else {
b31790
+		g_free (selected_session);
b31790
+        }
b31790
+
b31790
         gtk_main ();
b31790
 
b31790
-        auto_save_next_session_if_needed ();
b31790
+        selected_session = get_selected_session ();
b31790
+
b31790
+        if (g_strcmp0 (action, "load") == 0) {
b31790
+                make_session_current (selected_session);
b31790
+                auto_save_next_session_if_needed ();
b31790
+        } else if (g_strcmp0 (action, "save") == 0) {
b31790
+                char *last_session;
b31790
+
b31790
+                last_session = get_last_session ();
b31790
+                make_session_current (selected_session);
b31790
+                save_session ();
b31790
+                if (last_session != NULL)
b31790
+                    make_session_current (last_session);
b31790
+        } else if (g_strcmp0 (action, "print") == 0) {
b31790
+                g_print ("%s\n", selected_session);
b31790
+        }
b31790
+        g_free (selected_session);
b31790
 
b31790
         return 0;
b31790
 }
f47161
diff --git a/tools/meson.build b/tools/meson.build
f47161
index 10ee918c..4bd24220 100644
f47161
--- a/tools/meson.build
f47161
+++ b/tools/meson.build
f47161
@@ -1,52 +1,53 @@
f47161
 install_data(
f47161
   'gnome-session-custom-session',
f47161
   install_dir: session_bindir
f47161
 )
f47161
 
f47161
 deps = session_deps + [
f47161
   sm_dep,
f47161
   ice_dep
f47161
 ]
f47161
 
f47161
 cflags = ['-DLOCALE_DIR="@0@"'.format(session_localedir)]
f47161
 
f47161
 programs = [
f47161
   # name, deps, cflags, install_dir
f47161
   ['gnome-session-quit', deps, cflags, session_bindir],
f47161
   ['gnome-session-inhibit', session_deps, cflags, session_bindir]
f47161
 ]
f47161
 
f47161
 if enable_session_selector
f47161
   deps = [
f47161
     glib_dep,
f47161
-    gtk_dep
f47161
+    gtk_dep,
f47161
+    dbus_glib_dep
f47161
   ]
f47161
 
f47161
   cflags += '-DGTKBUILDER_DIR="@0@"'.format(session_pkgdatadir)
f47161
 
f47161
   programs += [['gnome-session-selector', deps, cflags, session_bindir]]
f47161
 endif
f47161
 
f47161
 deps = [
f47161
   gtk_dep,
f47161
   x11_dep,
f47161
   dependency('egl'),
f47161
   dependency('glesv2')
f47161
 ]
f47161
 
f47161
 cflags = '-DPKGDATADIR="@0@"'.format(session_pkgdatadir)
f47161
 
f47161
 programs += [['gnome-session-check-accelerated-gles-helper', deps, cflags, session_libexecdir]]
f47161
 
f47161
 deps = [
f47161
   glib_dep,
f47161
   x11_dep,
f47161
   dependency('gl'),
f47161
   dependency('epoxy'),
f47161
   dependency('xcomposite')
f47161
 ]
f47161
 
f47161
 programs += [['gnome-session-check-accelerated-gl-helper', deps, cflags, session_libexecdir]]
f47161
 
f47161
 deps += [gtk_dep]
f47161
 
b31790
-- 
f47161
2.17.0
b31790