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



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

    Personal templates: implement the delete action

 docs/reference/latexila-docs.xml                   |    1 +
 docs/reference/latexila-sections.txt               |   10 ++-
 src/liblatexila/latexila-templates-manage-dialog.c |  147 +++++++++++++++-----
 src/liblatexila/latexila-templates-manage-dialog.h |   10 ++-
 src/liblatexila/latexila-templates-personal.c      |   30 ++++-
 src/liblatexila/latexila-templates-personal.h      |    2 +-
 src/main_window_file.vala                          |    4 +-
 7 files changed, 161 insertions(+), 43 deletions(-)
---
diff --git a/docs/reference/latexila-docs.xml b/docs/reference/latexila-docs.xml
index b45756a..17ac660 100644
--- a/docs/reference/latexila-docs.xml
+++ b/docs/reference/latexila-docs.xml
@@ -23,6 +23,7 @@
     <xi:include href="xml/post-processor-latexmk.xml"/>
     <xi:include href="xml/synctex.xml"/>
     <xi:include href="xml/templates-dialogs.xml"/>
+    <xi:include href="xml/templates-manage-dialog.xml"/>
     <xi:include href="xml/templates-default.xml"/>
     <xi:include href="xml/templates-personal.xml"/>
     <xi:include href="xml/utils.xml"/>
diff --git a/docs/reference/latexila-sections.txt b/docs/reference/latexila-sections.txt
index a3cf6c3..b416dcf 100644
--- a/docs/reference/latexila-sections.txt
+++ b/docs/reference/latexila-sections.txt
@@ -234,7 +234,15 @@ latexila_synctex_get_type
 <TITLE>LatexilaTemplatesDialogs</TITLE>
 latexila_templates_dialogs_open
 latexila_templates_dialogs_create_template
-latexila_templates_manage_dialog
+</SECTION>
+
+<SECTION>
+<FILE>templates-manage-dialog</FILE>
+<TITLE>LatexilaTemplatesManageDialog</TITLE>
+LatexilaTemplatesManageDialog
+latexila_templates_manage_dialog_new
+<SUBSECTION Standard>
+LATEXILA_TYPE_TEMPLATES_MANAGE_DIALOG
 </SECTION>
 
 <SECTION>
diff --git a/src/liblatexila/latexila-templates-manage-dialog.c 
b/src/liblatexila/latexila-templates-manage-dialog.c
index 815d6f9..bd1e38b 100644
--- a/src/liblatexila/latexila-templates-manage-dialog.c
+++ b/src/liblatexila/latexila-templates-manage-dialog.c
@@ -17,14 +17,88 @@
  * along with LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+/**
+ * SECTION:templates-manage-dialog
+ * @title: LatexilaTemplatesManageDialog
+ * @short_description: Dialog to manage personal templates
+ * @see_also: #LatexilaTemplatesPersonal
+ *
+ * #LatexilaTemplatesManageDialog is a #GtkDialog to manage personal templates.
+ * The implemented actions: delete.
+ */
+
 #include "latexila-templates-manage-dialog.h"
 #include <glib/gi18n.h>
 #include "latexila-templates-personal.h"
 #include "latexila-templates-common.h"
 #include "latexila-utils.h"
 
+struct _LatexilaTemplatesManageDialog
+{
+  GtkDialog parent;
+
+  GtkTreeView *templates_view;
+};
+
+G_DEFINE_TYPE (LatexilaTemplatesManageDialog, latexila_templates_manage_dialog, GTK_TYPE_DIALOG)
+
+static void
+delete_button_clicked_cb (GtkToolButton                 *delete_button,
+                          LatexilaTemplatesManageDialog *manage_dialog)
+{
+  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 (manage_dialog->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 (GTK_WINDOW (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 (LatexilaTemplatesManageDialog *manage_dialog)
 {
   GtkToolbar *toolbar;
   GtkStyleContext *context;
@@ -37,25 +111,37 @@ 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),
+                    manage_dialog);
+
   return GTK_WIDGET (toolbar);
 }
 
-static GtkWidget *
-get_dialog_content (void)
+static void
+latexila_templates_manage_dialog_class_init (LatexilaTemplatesManageDialogClass *klass)
+{
+}
+
+static void
+latexila_templates_manage_dialog_init (LatexilaTemplatesManageDialog *manage_dialog)
 {
   LatexilaTemplatesPersonal *templates_store;
-  GtkTreeView *templates_view;
   GtkWidget *scrolled_window;
   GtkWidget *toolbar;
+  GtkWidget *dialog_content;
+  GtkBox *content_area;
 
   templates_store = latexila_templates_personal_get_instance ();
-  templates_view = latexila_templates_get_view (GTK_LIST_STORE (templates_store));
+  manage_dialog->templates_view = latexila_templates_get_view (GTK_LIST_STORE (templates_store));
 
   scrolled_window = gtk_scrolled_window_new (NULL, NULL);
   gtk_widget_set_size_request (scrolled_window, 350, 300);
@@ -63,43 +149,34 @@ get_dialog_content (void)
                                        GTK_SHADOW_IN);
 
   gtk_container_add (GTK_CONTAINER (scrolled_window),
-                     GTK_WIDGET (templates_view));
+                     GTK_WIDGET (manage_dialog->templates_view));
+
+  toolbar = get_toolbar (manage_dialog);
 
-  toolbar = get_toolbar ();
+  dialog_content = latexila_utils_join_widgets (scrolled_window, toolbar);
 
-  return latexila_utils_join_widgets (scrolled_window, toolbar);
+  content_area = GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (manage_dialog)));
+  gtk_box_pack_start (content_area, dialog_content, TRUE, TRUE, 0);
+  gtk_widget_show_all (GTK_WIDGET (content_area));
 }
 
 /**
- * latexila_templates_manage_dialog:
+ * latexila_templates_manage_dialog_new:
  * @parent_window: transient parent window of the dialog.
  *
- * Runs a #GtkDialog to manage personal templates.
+ * Returns: a #GtkDialog to manage personal templates.
  */
-void
-latexila_templates_manage_dialog (GtkWindow *parent_window)
+GtkDialog *
+latexila_templates_manage_dialog_new (GtkWindow *parent_window)
 {
-  GtkDialog *dialog;
-  GtkWidget *dialog_content;
-  GtkBox *content_area;
-
-  g_return_if_fail (GTK_IS_WINDOW (parent_window));
-
-  dialog = GTK_DIALOG (gtk_dialog_new_with_buttons (_("Manage Personal Templates"),
-                                                    parent_window,
-                                                    GTK_DIALOG_MODAL |
-                                                    GTK_DIALOG_DESTROY_WITH_PARENT |
-                                                    GTK_DIALOG_USE_HEADER_BAR,
-                                                    NULL, NULL));
-
-  gtk_container_set_border_width (GTK_CONTAINER (dialog), 6);
-
-  dialog_content = get_dialog_content ();
-
-  content_area = GTK_BOX (gtk_dialog_get_content_area (dialog));
-  gtk_box_pack_start (content_area, dialog_content, TRUE, TRUE, 0);
-  gtk_widget_show_all (GTK_WIDGET (content_area));
-
-  gtk_dialog_run (dialog);
-  gtk_widget_destroy (GTK_WIDGET (dialog));
+  g_return_val_if_fail (GTK_IS_WINDOW (parent_window), NULL);
+
+  return g_object_new (LATEXILA_TYPE_TEMPLATES_MANAGE_DIALOG,
+                       "title", _("Manage Personal Templates"),
+                       "transient-for", parent_window,
+                       "modal", TRUE,
+                       "destroy-with-parent", TRUE,
+                       "use-header-bar", TRUE,
+                       "border-width", 6,
+                       NULL);
 }
diff --git a/src/liblatexila/latexila-templates-manage-dialog.h 
b/src/liblatexila/latexila-templates-manage-dialog.h
index 60f0216..147c782 100644
--- a/src/liblatexila/latexila-templates-manage-dialog.h
+++ b/src/liblatexila/latexila-templates-manage-dialog.h
@@ -24,7 +24,15 @@
 
 G_BEGIN_DECLS
 
-void      latexila_templates_manage_dialog        (GtkWindow *parent_window);
+#define LATEXILA_TYPE_TEMPLATES_MANAGE_DIALOG latexila_templates_manage_dialog_get_type ()
+
+G_DECLARE_FINAL_TYPE (LatexilaTemplatesManageDialog,
+                      latexila_templates_manage_dialog,
+                      LATEXILA,
+                      TEMPLATES_MANAGE_DIALOG,
+                      GtkDialog)
+
+GtkDialog *   latexila_templates_manage_dialog_new    (GtkWindow *parent_window);
 
 G_END_DECLS
 
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
diff --git a/src/main_window_file.vala b/src/main_window_file.vala
index 82065b0..0743e91 100644
--- a/src/main_window_file.vala
+++ b/src/main_window_file.vala
@@ -225,7 +225,9 @@ public class MainWindowFile
 
     public void on_manage_templates ()
     {
-        Latexila.templates_manage_dialog (_main_window);
+        Dialog dialog = new Latexila.TemplatesManageDialog (_main_window);
+        dialog.run ();
+        dialog.destroy ();
     }
 
     public void on_file_close ()


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