[Xfce4-commits] <xfce4-session:master> Add support for editing autostart applications (bug #2380).

Nick Schermer noreply at xfce.org
Sun Feb 7 13:18:01 CET 2010


Updating branch refs/heads/master
         to a9f4b964819703abfab5e9b7964b98b6136ac676 (commit)
       from e298788c0dbd902f153c78ed0742a5353c97de3f (commit)

commit a9f4b964819703abfab5e9b7964b98b6136ac676
Author: Nick Schermer <nick at xfce.org>
Date:   Sun Feb 7 13:14:26 2010 +0100

    Add support for editing autostart applications (bug #2380).
    
    Based one the patch of Laurent Meunier with the required
    changes for the changes since the integration of the
    dialog in the main settings dialog.
    
    Also ask for confirmation before removing an item.

 settings/xfae-dialog.c |   20 ++++++--
 settings/xfae-dialog.h |    4 +-
 settings/xfae-model.c  |  126 ++++++++++++++++++++++++++++++++++++++++++++++++
 settings/xfae-model.h  |   14 +++++
 settings/xfae-window.c |  104 +++++++++++++++++++++++++++++++++++++---
 5 files changed, 256 insertions(+), 12 deletions(-)

diff --git a/settings/xfae-dialog.c b/settings/xfae-dialog.c
index e98a009..83a2bf8 100644
--- a/settings/xfae-dialog.c
+++ b/settings/xfae-dialog.c
@@ -229,15 +229,29 @@ xfae_dialog_browse (XfaeDialog *dialog)
 
 /**
  * xfae_dialog_new:
+ * @name    : initial name or %NULL.
+ * @descr   : initial description or %NULL..
+ * @command : initial command or %NULL..
  *
  * Allocates a new #XfaeDialog instance.
  *
  * Return value: the newly allocated #XfaeDialog.
  **/
 GtkWidget*
-xfae_dialog_new (void)
+xfae_dialog_new (const gchar *name,
+                 const gchar *descr,
+                 const gchar *command)
 {
-  return g_object_new (XFAE_TYPE_DIALOG, NULL);
+  XfaeDialog *dialog = g_object_new (XFAE_TYPE_DIALOG, NULL);
+
+  if (name)
+    gtk_entry_set_text (GTK_ENTRY (dialog->name_entry), name);
+  if (descr)
+    gtk_entry_set_text (GTK_ENTRY (dialog->descr_entry), descr );
+  if (command)
+    gtk_entry_set_text (GTK_ENTRY (dialog->command_entry), command);
+
+  return GTK_WIDGET (dialog);
 }
 
 
@@ -271,5 +285,3 @@ xfae_dialog_get (XfaeDialog *dialog,
   g_strstrip (*descr);
   g_strstrip (*command);
 }
-
-
diff --git a/settings/xfae-dialog.h b/settings/xfae-dialog.h
index 9be885d..5e362a7 100644
--- a/settings/xfae-dialog.h
+++ b/settings/xfae-dialog.h
@@ -38,7 +38,9 @@ typedef struct _XfaeDialog      XfaeDialog;
 
 GType      xfae_dialog_get_type (void) G_GNUC_CONST;
 
-GtkWidget *xfae_dialog_new      (void);
+GtkWidget *xfae_dialog_new      (const gchar *name,
+                                 const gchar *descr,
+                                 const gchar *command);
 
 void       xfae_dialog_get      (XfaeDialog *dialog,
                                  gchar     **name,
diff --git a/settings/xfae-model.c b/settings/xfae-model.c
index 0161216..e7a4b85 100644
--- a/settings/xfae-model.c
+++ b/settings/xfae-model.c
@@ -691,6 +691,68 @@ xfae_model_add (XfaeModel   *model,
 
 
 /**
+ * xfae_model_get:
+ * @model       : a #XfaeModel.
+ * @name        : the user visible name of the new item.
+ * @description : the description for the new item.
+ * @command     : the command for the new item.
+ * @error       : return locations for errors or %NULL.
+ *
+ * Attempts to add a new item with the given parameters
+ * to @model.
+ *
+ * Return value: %TRUE if successfull, else %FALSE.
+ **/
+gboolean
+xfae_model_get (XfaeModel    *model,
+                GtkTreeIter  *iter,
+                gchar       **name,
+                gchar       **description,
+                gchar       **command,
+                GError      **error)
+{
+  XfaeItem    *item;
+  GList       *lp;
+  XfceRc      *rc;
+  const gchar *value;
+
+  g_return_val_if_fail (XFAE_IS_MODEL (model), FALSE);
+  g_return_val_if_fail (iter->stamp == model->stamp, FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+  lp = iter->user_data;
+  item = lp->data;
+
+  /* try to open the resource config */
+  rc = xfce_rc_config_open (XFCE_RESOURCE_CONFIG, item->relpath, FALSE);
+  if (G_UNLIKELY (rc == NULL))
+    {
+      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (EIO),
+                   _("Failed to open %s for reading"), item->relpath);
+      return FALSE;
+    }
+
+  /* read the resource config */
+  value = xfce_rc_read_entry (rc, "Name", NULL);
+  if (name != NULL)
+   *name = g_strdup (value);
+
+  value = xfce_rc_read_entry (rc, "Comment", NULL);
+  if (description != NULL)
+    *description = g_strdup (value);
+
+  value = xfce_rc_read_entry (rc, "Exec", NULL);
+  if (command != NULL)
+    *command = g_strdup (value);
+
+  xfce_rc_close (rc);
+
+  return TRUE;
+}
+
+
+
+/**
  * xfae_model_remove:
  * @model : a #XfaeModel.
  * @iter  : the #GtkTreeIter referring to the item that should be removed.
@@ -737,6 +799,70 @@ xfae_model_remove (XfaeModel   *model,
 
 
 /**
+ * xfae_model_edit:
+ * @model       : a #XfaeModel.
+ * @iter        : the #GtkTreeIter referring to the item that should be removed.
+ * @name        : the user visible name of the new item.
+ * @description : the description for the new item.
+ * @command     : the command for the new item.
+ * @error       : return locations for errors or %NULL.
+ *
+ * Attempts to edit an item with the given parameters
+ * to @model.
+ *
+ * Return value: %TRUE if successfull, else %FALSE.
+ **/
+gboolean
+xfae_model_edit (XfaeModel   *model,
+                 GtkTreeIter *iter,
+                 const gchar *name,
+                 const gchar *description,
+                 const gchar *command,
+                 GError     **error)
+{
+  GtkTreePath *path;
+  XfaeItem    *item;
+  XfceRc      *rc;
+  GList       *lp;
+
+  g_return_val_if_fail (XFAE_IS_MODEL (model), FALSE);
+  g_return_val_if_fail (iter->stamp == model->stamp, FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+  lp = iter->user_data;
+  item = lp->data;
+
+  /* try to open the resource config */
+  rc = xfce_rc_config_open (XFCE_RESOURCE_CONFIG, item->relpath, FALSE);
+  if (G_UNLIKELY (rc == NULL))
+    {
+      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (EIO),
+                   _("Failed to open %s for writing"), item->relpath);
+      return FALSE;
+    }
+
+  item->name = g_strdup (name);
+  item->comment = g_strdup (description);
+
+  /* write the result */
+  xfce_rc_set_group (rc, "Desktop Entry");
+  xfce_rc_write_entry (rc, "Name", name);
+  xfce_rc_write_entry (rc, "Comment", description);
+  xfce_rc_write_entry (rc, "Exec", command);
+  xfce_rc_close (rc);
+
+  /* tell the view that we have most probably a new state */
+  path = gtk_tree_path_new_from_indices (g_list_position (model->items, lp), -1);
+  gtk_tree_model_row_changed (GTK_TREE_MODEL (model), path, iter);
+  gtk_tree_path_free (path);
+
+  return TRUE;
+}
+
+
+
+
+/**
  * xfae_model_toggle:
  * @model : a #XfaeModel.
  * @iter  : the #GtkTreeIter referring to the item that should be toggled.
diff --git a/settings/xfae-model.h b/settings/xfae-model.h
index 99b4e2e..a9d94e4 100644
--- a/settings/xfae-model.h
+++ b/settings/xfae-model.h
@@ -61,10 +61,24 @@ gboolean      xfae_model_add      (XfaeModel   *model,
                                    const gchar *command,
                                    GError     **error);
 
+gboolean      xfae_model_get      (XfaeModel   *model,
+                                   GtkTreeIter *iter,
+                                   gchar      **name,
+                                   gchar      **description,
+                                   gchar      **command,
+                                   GError     **error);
+
 gboolean      xfae_model_remove   (XfaeModel   *model,
                                    GtkTreeIter *iter,
                                    GError     **error);
 
+gboolean      xfae_model_edit     (XfaeModel   *model,
+                                   GtkTreeIter *iter,
+                                   const gchar *name,
+                                   const gchar *description,
+                                   const gchar *command,
+                                   GError     **error);
+
 gboolean      xfae_model_toggle   (XfaeModel   *model,
                                    GtkTreeIter *iter,
                                    GError     **error);
diff --git a/settings/xfae-window.c b/settings/xfae-window.c
index 48c9edc..f47d3e1 100644
--- a/settings/xfae-window.c
+++ b/settings/xfae-window.c
@@ -32,6 +32,7 @@
 
 static void     xfae_window_add                 (XfaeWindow       *window);
 static void     xfae_window_remove              (XfaeWindow       *window);
+static void     xfae_window_edit                (XfaeWindow       *window);
 static gboolean xfae_window_button_press_event  (GtkWidget        *treeview,
                                                  GdkEventButton   *event,
                                                  XfaeWindow       *window);
@@ -177,11 +178,22 @@ xfae_window_init (XfaeWindow *window)
   button = gtk_button_new_from_stock (GTK_STOCK_REMOVE);
   g_signal_connect_swapped (G_OBJECT (button), "clicked",
                             G_CALLBACK (xfae_window_remove), window);
+  gtk_box_pack_start (GTK_BOX (bbox), button, FALSE, FALSE, 0);
+  gtk_widget_show (button);
+
   g_signal_connect (G_OBJECT (window->selection), "changed",
                     G_CALLBACK (xfae_window_selection_changed), button);
   xfae_window_selection_changed (window->selection, button);
+
+  button = gtk_button_new_from_stock (GTK_STOCK_EDIT);
+  g_signal_connect_swapped (G_OBJECT (button), "clicked",
+                            G_CALLBACK (xfae_window_edit), window);
   gtk_box_pack_start (GTK_BOX (bbox), button, FALSE, FALSE, 0);
   gtk_widget_show (button);
+
+  g_signal_connect (G_OBJECT (window->selection), "changed",
+                    G_CALLBACK (xfae_window_selection_changed), button);
+  xfae_window_selection_changed (window->selection, button);
 }
 
 
@@ -262,7 +274,7 @@ xfae_window_add (XfaeWindow *window)
   gchar        *descr;
   gchar        *command;
 
-  dialog = xfae_dialog_new ();
+  dialog = xfae_dialog_new (NULL, NULL, NULL);
   parent = gtk_widget_get_toplevel (GTK_WIDGET (window));
   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent));
   if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK)
@@ -274,7 +286,7 @@ xfae_window_add (XfaeWindow *window)
       model = gtk_tree_view_get_model (GTK_TREE_VIEW (window->treeview));
       if (!xfae_model_add (XFAE_MODEL (model), name, descr, command, &error))
         {
-          xfce_dialog_show_error (NULL, error, _("Failed adding \"%s\""), name);
+          xfce_dialog_show_error (GTK_WINDOW (parent), error, _("Failed adding \"%s\""), name);
           g_error_free (error);
         }
 
@@ -294,13 +306,32 @@ xfae_window_remove (XfaeWindow *window)
   GtkTreeModel     *model;
   GtkTreeIter       iter;
   GError           *error = NULL;
+  GtkWidget        *parent;
+  gchar            *name;
+  gboolean          remove_item;
+
+  parent = gtk_widget_get_toplevel (GTK_WIDGET (window));
 
   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (window->treeview));
   if (gtk_tree_selection_get_selected (selection, &model, &iter))
     {
-      if (!xfae_model_remove (XFAE_MODEL (model), &iter, &error))
+      if (!xfae_model_get (XFAE_MODEL (model), &iter, &name, NULL, NULL, &error))
         {
-          xfce_dialog_show_error (NULL, error, _("Failed to remove item"));
+          xfce_dialog_show_error (GTK_WINDOW (parent), error, _("Failed to remove item"));
+          g_error_free (error);
+          return;
+        }
+
+      remove_item = xfce_dialog_confirm (GTK_WINDOW (parent), GTK_STOCK_REMOVE, NULL,
+                                         _("This will permanently remove the application "
+                                           "from the list of automatically started applications"),
+                                         _("Are you sure you want to remove \"%s\""), name);
+
+      g_free (name);
+
+      if (remove_item && !xfae_model_remove (XFAE_MODEL (model), &iter, &error))
+        {
+          xfce_dialog_show_error (GTK_WINDOW (parent), error, _("Failed to remove item"));
           g_error_free (error);
         }
     }
@@ -309,6 +340,59 @@ xfae_window_remove (XfaeWindow *window)
 
 
 static void
+xfae_window_edit (XfaeWindow *window)
+{
+  GtkTreeSelection *selection;
+  GtkTreeModel     *model;
+  GtkTreeIter       iter;
+  GError           *error = NULL;
+  gchar            *name;
+  gchar            *descr;
+  gchar            *command;
+  GtkWidget        *parent;
+  GtkWidget        *dialog;
+
+  parent = gtk_widget_get_toplevel (GTK_WIDGET (window));
+
+  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (window->treeview));
+  if (gtk_tree_selection_get_selected (selection, &model, &iter))
+    {
+      if (!xfae_model_get (XFAE_MODEL (model), &iter, &name, &descr, &command, &error))
+        {
+          xfce_dialog_show_error (GTK_WINDOW (parent), error, _("Failed to edit item"));
+          g_error_free (error);
+          return;
+        }
+
+      dialog = xfae_dialog_new (name, descr, command);
+      gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent));
+
+      g_free (command);
+      g_free (descr);
+      g_free (name);
+
+      if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK)
+        {
+	  gtk_widget_hide (dialog);
+
+	  xfae_dialog_get (XFAE_DIALOG (dialog), &name, &descr, &command);
+
+          if (!xfae_model_edit (XFAE_MODEL (model), &iter, name, descr, command, &error))
+            {
+              xfce_dialog_show_error (GTK_WINDOW (parent), error, _("Failed to edit item \"%s\""), name);
+              g_error_free (error);
+            }
+
+          g_free (command);
+          g_free (descr);
+          g_free (name);
+        }
+      gtk_widget_destroy (dialog);
+    }
+}
+
+
+static void
 xfae_window_item_toggled (XfaeWindow *window,
                           gchar      *path_string)
 {
@@ -406,12 +490,18 @@ xfae_window_create_plug_child (XfaeWindow *window)
   button = gtk_button_new_from_stock (GTK_STOCK_REMOVE);
   g_signal_connect_swapped (G_OBJECT (button), "clicked",
                             G_CALLBACK (xfae_window_remove), window);
-  g_signal_connect (G_OBJECT (window->selection), "changed",
-                    G_CALLBACK (xfae_window_selection_changed), button);
-  xfae_window_selection_changed (window->selection, button);
   gtk_box_pack_start (GTK_BOX (bbox), button, FALSE, FALSE, 0);
   gtk_widget_show (button);
 
+  button = gtk_button_new_from_stock (GTK_STOCK_EDIT);
+  g_signal_connect_swapped (G_OBJECT (button), "clicked",
+                            G_CALLBACK (xfae_window_edit), window);
+  gtk_box_pack_start (GTK_BOX (bbox), button, FALSE, FALSE, 0);
+  gtk_widget_show (button);
+
+  g_signal_connect (G_OBJECT (window->selection), "changed",
+                    G_CALLBACK (xfae_window_selection_changed), button);
+  xfae_window_selection_changed (window->selection, button);
   return vbox;
 }
 #endif



More information about the Xfce4-commits mailing list