[latexila/wip/templates-revamp] Personal templates: implement the delete action



commit fc3ee1f3b1b42c64b039fea2ac0e3d53a5a04f71
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed Jul 8 16:38:19 2015 +0200

    Personal templates: implement the delete action

 src/liblatexila/latexila-templates-manage-dialog.c |   65 +++++++++++++++++++-
 src/liblatexila/latexila-templates-personal.c      |   30 ++++++++-
 src/liblatexila/latexila-templates-personal.h      |    2 +-
 3 files changed, 90 insertions(+), 7 deletions(-)
---
diff --git a/src/liblatexila/latexila-templates-manage-dialog.c 
b/src/liblatexila/latexila-templates-manage-dialog.c
index 815d6f9..10a323a 100644
--- a/src/liblatexila/latexila-templates-manage-dialog.c
+++ b/src/liblatexila/latexila-templates-manage-dialog.c
@@ -23,8 +23,63 @@
 #include "latexila-templates-common.h"
 #include "latexila-utils.h"
 
+static void
+delete_button_clicked_cb (GtkToolButton *delete_button,
+                          GtkTreeView   *templates_view)
+{
+  GtkTreeSelection *selection;
+  GtkTreeModel *model;
+  LatexilaTemplatesPersonal *templates_store;
+  GtkTreeIter iter;
+  gchar *name;
+  GtkDialog *confirm_dialog;
+  gint response;
+  GError *error = NULL;
+
+  selection = gtk_tree_view_get_selection (templates_view);
+
+  if (!gtk_tree_selection_get_selected (selection, &model, &iter))
+    g_return_if_reached ();
+
+  templates_store = latexila_templates_personal_get_instance ();
+  g_return_if_fail (GTK_TREE_MODEL (templates_store) == model);
+
+  gtk_tree_model_get (model, &iter,
+                      LATEXILA_TEMPLATES_COLUMN_NAME, &name,
+                      -1);
+
+  confirm_dialog = GTK_DIALOG (gtk_message_dialog_new (manage_dialog,
+                                                       GTK_DIALOG_MODAL |
+                                                       GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                       GTK_MESSAGE_QUESTION,
+                                                       GTK_BUTTONS_NONE,
+                                                       _("Do you really want to delete the template “%s”?"),
+                                                       name));
+
+  g_free (name);
+
+  gtk_dialog_add_buttons (confirm_dialog,
+                          _("_Cancel"), GTK_RESPONSE_CANCEL,
+                          _("_Delete"), GTK_RESPONSE_YES,
+                          NULL);
+
+  response = gtk_dialog_run (confirm_dialog);
+  gtk_widget_destroy (GTK_WIDGET (confirm_dialog));
+
+  if (response != GTK_RESPONSE_YES)
+    return;
+
+  latexila_templates_personal_delete (templates_store, &iter, &error);
+
+  if (error != NULL)
+    {
+      g_warning ("%s", error->message);
+      g_error_free (error);
+    }
+}
+
 static GtkWidget *
-get_toolbar (void)
+get_toolbar (GtkTreeView *templates_view)
 {
   GtkToolbar *toolbar;
   GtkStyleContext *context;
@@ -37,12 +92,18 @@ get_toolbar (void)
   context = gtk_widget_get_style_context (GTK_WIDGET (toolbar));
   gtk_style_context_add_class (context, GTK_STYLE_CLASS_INLINE_TOOLBAR);
 
+  /* Delete */
   delete_button = GTK_TOOL_BUTTON (gtk_tool_button_new (NULL, NULL));
   gtk_tool_button_set_icon_name (delete_button, "list-remove-symbolic");
   gtk_widget_set_tooltip_text (GTK_WIDGET (delete_button), _("Delete"));
 
   gtk_toolbar_insert (toolbar, GTK_TOOL_ITEM (delete_button), -1);
 
+  g_signal_connect (delete_button,
+                    "clicked",
+                    G_CALLBACK (delete_button_clicked_cb),
+                    templates_view);
+
   return GTK_WIDGET (toolbar);
 }
 
@@ -65,7 +126,7 @@ get_dialog_content (void)
   gtk_container_add (GTK_CONTAINER (scrolled_window),
                      GTK_WIDGET (templates_view));
 
-  toolbar = get_toolbar ();
+  toolbar = get_toolbar (templates_view);
 
   return latexila_utils_join_widgets (scrolled_window, toolbar);
 }
diff --git a/src/liblatexila/latexila-templates-personal.c b/src/liblatexila/latexila-templates-personal.c
index 8a63670..3e2030b 100644
--- a/src/liblatexila/latexila-templates-personal.c
+++ b/src/liblatexila/latexila-templates-personal.c
@@ -509,7 +509,7 @@ out:
 /**
  * latexila_templates_personal_delete:
  * @templates: the #LatexilaTemplatesPersonal instance.
- * @path: the #GtkTreePath of a personal template.
+ * @iter: a valid #GtkTreeIter.
  * @error: (out) (optional): a location to a %NULL #GError, or %NULL.
  *
  * Deletes a personal template.
@@ -518,12 +518,34 @@ out:
  */
 gboolean
 latexila_templates_personal_delete (LatexilaTemplatesPersonal  *templates,
-                                    GtkTreePath                *path,
+                                    GtkTreeIter                *iter,
                                     GError                    **error)
 {
+  GFile *file = NULL;
+  gboolean success = FALSE;
+
   g_return_val_if_fail (LATEXILA_IS_TEMPLATES_PERSONAL (templates), FALSE);
-  g_return_val_if_fail (path != NULL, NULL);
+  g_return_val_if_fail (iter != NULL, FALSE);
   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
-  return TRUE;
+  gtk_tree_model_get (GTK_TREE_MODEL (templates),
+                      iter,
+                      LATEXILA_TEMPLATES_COLUMN_FILE, &file,
+                      -1);
+
+  g_return_val_if_fail (G_IS_FILE (file), FALSE);
+
+  gtk_list_store_remove (GTK_LIST_STORE (templates), iter);
+
+  if (!save_rc_file (templates, error))
+    goto out;
+
+  if (!g_file_delete (file, NULL, error))
+    goto out;
+
+  success = TRUE;
+
+out:
+  g_clear_object (&file);
+  return success;
 }
diff --git a/src/liblatexila/latexila-templates-personal.h b/src/liblatexila/latexila-templates-personal.h
index e635ee7..4851d10 100644
--- a/src/liblatexila/latexila-templates-personal.h
+++ b/src/liblatexila/latexila-templates-personal.h
@@ -41,7 +41,7 @@ gboolean      latexila_templates_personal_create                (LatexilaTemplat
                                                                  GError                   **error);
 
 gboolean      latexila_templates_personal_delete                (LatexilaTemplatesPersonal  *templates,
-                                                                 GtkTreePath                *path,
+                                                                 GtkTreeIter                *iter,
                                                                  GError                    **error);
 
 G_END_DECLS


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