[anjuta] snippets-manager: export snippets from selection
- From: Johannes Schmid <jhs src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [anjuta] snippets-manager: export snippets from selection
- Date: Wed, 26 Oct 2011 08:35:19 +0000 (UTC)
commit 124ac6b82c3e2d840a6fe8652950be69effe84ea
Author: Mihai Bivol <mm bivol gmail com>
Date: Sat Mar 12 16:25:47 2011 -0800
snippets-manager: export snippets from selection
plugins/snippets-manager/snippets-import-export.c | 327 ++++++++++++++++-----
1 files changed, 254 insertions(+), 73 deletions(-)
---
diff --git a/plugins/snippets-manager/snippets-import-export.c b/plugins/snippets-manager/snippets-import-export.c
index 600d6d9..1ea67a9 100644
--- a/plugins/snippets-manager/snippets-import-export.c
+++ b/plugins/snippets-manager/snippets-import-export.c
@@ -27,6 +27,13 @@
#include <gtk/gtk.h>
#include <libanjuta/anjuta-utils.h>
+enum
+{
+ SNIPPETS_STORE_COL_SNIPPET_OBJECT = 0,
+ SNIPPETS_STORE_COL_ACTIVE,
+ SNIPPETS_STORE_COL_NO
+};
+
static void
add_or_update_snippet (SnippetsDB *snippets_db,
AnjutaSnippet *snippet,
@@ -180,94 +187,228 @@ snippets_manager_import_snippets (SnippetsDB *snippets_db,
}
static void
-snippets_view_name_text_data_func (GtkTreeViewColumn *column,
- GtkCellRenderer *renderer,
- GtkTreeModel *tree_model,
- GtkTreeIter *iter,
- gpointer user_data)
+snippets_view_trigger_data_func (GtkTreeViewColumn *column,
+ GtkCellRenderer *renderer,
+ GtkTreeModel *tree_model,
+ GtkTreeIter *iter,
+ gpointer user_data)
{
- gchar *name = NULL;
-
+ const gchar *trigger = NULL;
+ GObject *snippet = NULL;
+
/* Assertions */
g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
+
+ /* Get trigger key */
+ gtk_tree_model_get (tree_model, iter,
+ SNIPPETS_STORE_COL_SNIPPET_OBJECT, &snippet,
+ -1);
- /* Get the name */
- gtk_tree_model_get (tree_model, iter,
- SNIPPETS_DB_MODEL_COL_NAME, &name,
- -1);
-
- g_object_set (renderer, "text", name, NULL);
- g_free (name);
+ if (ANJUTA_IS_SNIPPET (snippet))
+ {
+ trigger = snippet_get_trigger_key (ANJUTA_SNIPPET (snippet));
+ g_object_set (renderer, "text", trigger, NULL);
+ }
+ else if (ANJUTA_IS_SNIPPETS_GROUP (snippet))
+ {
+ g_object_set (renderer, "text", "", NULL);
+ }
}
static void
-snippets_view_trigger_data_func (GtkTreeViewColumn *column,
- GtkCellRenderer *renderer,
- GtkTreeModel *tree_model,
- GtkTreeIter *iter,
- gpointer user_data)
-{
- gchar *trigger = NULL;
+snippets_view_languages_data_func (GtkTreeViewColumn *column,
+ GtkCellRenderer *renderer,
+ GtkTreeModel *tree_model,
+ GtkTreeIter *iter,
+ gpointer user_data)
+{ const gchar *languages = NULL;
+ GObject *snippet = NULL;
/* Assertions */
g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
- /* Get trigger key */
+ /* Get languages */
gtk_tree_model_get (tree_model, iter,
- SNIPPETS_DB_MODEL_COL_TRIGGER, &trigger,
+ SNIPPETS_STORE_COL_SNIPPET_OBJECT, &snippet,
-1);
- g_object_set (renderer, "text", trigger, NULL);
- g_free (trigger);
+ if (ANJUTA_IS_SNIPPET (snippet))
+ {
+ languages = snippet_get_languages_string (ANJUTA_SNIPPET (snippet));
+ g_object_set (renderer, "text", languages, NULL);
+ }
+ else if (ANJUTA_IS_SNIPPETS_GROUP (snippet))
+ {
+ g_object_set (renderer, "text", "", NULL);
+ }
+
}
static void
-snippets_view_languages_data_func (GtkTreeViewColumn *column,
- GtkCellRenderer *renderer,
- GtkTreeModel *tree_model,
- GtkTreeIter *iter,
- gpointer user_data)
+snippets_view_name_data_func (GtkTreeViewColumn *column,
+ GtkCellRenderer *renderer,
+ GtkTreeModel *tree_model,
+ GtkTreeIter *iter,
+ gpointer user_data)
{
- gchar *languages = NULL;
+ const gchar *name = NULL;
+ GObject *snippet = NULL;
/* Assertions */
g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
g_return_if_fail (GTK_IS_TREE_MODEL (tree_model));
- /* Get languages */
+ /* Get name */
gtk_tree_model_get (tree_model, iter,
- SNIPPETS_DB_MODEL_COL_LANGUAGES, &languages,
+ SNIPPETS_STORE_COL_SNIPPET_OBJECT, &snippet,
-1);
- g_object_set (renderer, "text", languages, NULL);
- g_free (languages);
+ if (ANJUTA_IS_SNIPPET (snippet))
+ {
+ name = snippet_get_name (ANJUTA_SNIPPET (snippet));
+ g_object_set (renderer, "text", name, NULL);
+ }
+ else if (ANJUTA_IS_SNIPPETS_GROUP (snippet))
+ {
+ name = snippets_group_get_name (ANJUTA_SNIPPETS_GROUP (snippet));
+ g_object_set (renderer, "text", name, NULL);
+ }
}
+
static void
handle_toggle (GtkCellRendererToggle *cell_renderer,
- gchar *path,
- gpointer user_data)
+ gchar *path,
+ gpointer snippet_tree_store)
{
- gboolean active = TRUE;
+ gboolean active;
+ GtkTreeIter iter, child_iter, parent_iter;
+ GObject *snippet;
/* Assertions */
g_return_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (cell_renderer));
+ g_return_if_fail (GTK_IS_TREE_STORE (snippet_tree_store));
+
+ /* Get the toggled object*/
+ gtk_tree_model_get_iter_from_string (GTK_TREE_MODEL (snippet_tree_store),
+ &iter, path);
+ gtk_tree_model_get (GTK_TREE_MODEL (snippet_tree_store), &iter,
+ SNIPPETS_STORE_COL_ACTIVE, &active,
+ SNIPPETS_STORE_COL_SNIPPET_OBJECT, &snippet,
+ -1);
+ if (active == TRUE)
+ active = FALSE;
+ else
+ active = TRUE;
+
+ /* If group toggled, toggle all the snippets */
+ if (ANJUTA_IS_SNIPPETS_GROUP (snippet))
+ {
+ if (gtk_tree_model_iter_children (GTK_TREE_MODEL (snippet_tree_store),
+ &child_iter, &iter))
+ {
+ do
+ {
+ gtk_tree_store_set (snippet_tree_store, &child_iter,
+ SNIPPETS_STORE_COL_ACTIVE, active,
+ -1);
+ }
+ while (gtk_tree_model_iter_next (GTK_TREE_MODEL (snippet_tree_store),
+ &child_iter));
+ }
+ }
+ /* If snippet toggled, select the snippets group */
+ if (ANJUTA_IS_SNIPPET (snippet))
+ {
+ if (gtk_tree_model_iter_parent (GTK_TREE_MODEL (snippet_tree_store),
+ &parent_iter, &iter) && active == TRUE)
+ {
+ gtk_tree_store_set (snippet_tree_store, &parent_iter,
+ SNIPPETS_STORE_COL_ACTIVE, TRUE,
+ -1);
+ }
+ }
+
+ gtk_tree_store_set (snippet_tree_store, &iter,
+ SNIPPETS_STORE_COL_ACTIVE, active,
+ -1);
+
g_print("%s\n", path);
}
+static gboolean
+model_foreach_set_store_func (GtkTreeModel *model,
+ GtkTreePath *path,
+ GtkTreeIter *iter,
+ gpointer store)
+{
+ GObject *obj = NULL;
+ GtkTreeIter store_iter;
+ GtkTreeStore *tree_store = NULL;
+ static GtkTreeIter group_iter;
+ /* Assertions */
+ g_return_val_if_fail (GTK_IS_TREE_STORE (store), TRUE);
+
+ tree_store = GTK_TREE_STORE (store);
+
+ gtk_tree_model_get (model, iter,
+ SNIPPETS_DB_MODEL_COL_CUR_OBJECT, &obj,
+ -1);
+
+ if (ANJUTA_IS_SNIPPETS_GROUP (obj))
+ {
+ gtk_tree_store_append (tree_store, &store_iter, NULL);
+ group_iter = store_iter;
+ gtk_tree_store_set (tree_store, &store_iter,
+ SNIPPETS_STORE_COL_SNIPPET_OBJECT, obj,
+ SNIPPETS_STORE_COL_ACTIVE, TRUE,
+ -1);
+ }
+ if (ANJUTA_IS_SNIPPET (obj))
+ {
+ gtk_tree_store_append (tree_store, &store_iter, &group_iter);
+ gtk_tree_store_set (tree_store, &store_iter,
+ SNIPPETS_STORE_COL_SNIPPET_OBJECT, obj,
+ SNIPPETS_STORE_COL_ACTIVE, TRUE,
+ -1);
+ }
+
+ return FALSE;
+}
+
+static GtkTreeStore*
+create_snippets_store (SnippetsDB *snippets_db)
+{
+ GtkTreeStore *store = NULL;
+ GtkTreeModel *model = NULL;
+ /* Assertions */
+ g_return_val_if_fail (ANJUTA_IS_SNIPPETS_DB (snippets_db), NULL);
+
+ model = gtk_tree_model_filter_new (GTK_TREE_MODEL(snippets_db), NULL);
+ store = gtk_tree_store_new (2, G_TYPE_OBJECT, G_TYPE_BOOLEAN);
+ gtk_tree_model_foreach (model, model_foreach_set_store_func, store);
+ return store;
+}
+
void snippets_manager_export_snippets (SnippetsDB *snippets_db,
AnjutaShell *anjuta_shell)
{
- GtkWidget *file_chooser = NULL;
- GtkFileFilter *filter = NULL;
- GtkWidget *snippets_chooser = NULL;
- GtkWidget *snippets_tree_view = NULL;
- GtkWidget *scrolled_box = NULL;
- GtkCellRenderer *text_renderer = NULL, *toggle_renderer = NULL;
- GtkTreeViewColumn *column = NULL;
- GtkTreeModel *model = NULL;
+ GtkWidget *file_chooser = NULL;
+ GtkFileFilter *filter = NULL;
+ GtkWidget *snippets_chooser = NULL;
+ GtkWidget *snippets_tree_view = NULL;
+ GtkWidget *scrolled_box = NULL;
+ GtkCellRenderer *text_renderer = NULL, *toggle_renderer = NULL;
+ GtkTreeViewColumn *column = NULL;
+ GtkTreeStore *snippet_tree_store = NULL;
+ GtkTreeIter iter, child_iter;
+ GList *snippets_group_list = NULL;
+ gboolean active;
+ AnjutaSnippet *snippet = NULL;
+ AnjutaSnippetsGroup *snippets_group = NULL;
+ GObject *object = NULL;
/* Assertions */
g_return_if_fail (ANJUTA_IS_SNIPPETS_DB (snippets_db));
@@ -275,63 +416,62 @@ void snippets_manager_export_snippets (SnippetsDB *snippets_db,
/* Create view and set model */
snippets_tree_view = gtk_tree_view_new();
- model = gtk_tree_model_filter_new (GTK_TREE_MODEL(snippets_db), NULL);
+ snippet_tree_store = create_snippets_store (snippets_db);
gtk_tree_view_set_model (GTK_TREE_VIEW (snippets_tree_view),
- model);
- g_object_unref (model);
+ GTK_TREE_MODEL (snippet_tree_store));
/* Column 1 -- Name */
- column = gtk_tree_view_column_new ();
+ column = gtk_tree_view_column_new ();
gtk_tree_view_column_set_title (column, "Name");
gtk_tree_view_append_column (GTK_TREE_VIEW (snippets_tree_view),
column);
toggle_renderer = gtk_cell_renderer_toggle_new();
- gtk_cell_renderer_toggle_set_active (GTK_CELL_RENDERER_TOGGLE (toggle_renderer), TRUE);
- /* TODO get some column to be some column in the model
- gtk_tree_view_column_add_attribute (column,
- toggle_renderer,
- "active", SOME_COLUMN);*/
- g_signal_connect (toggle_renderer, "toggled", G_CALLBACK (handle_toggle), NULL);
+
+ g_signal_connect (toggle_renderer, "toggled", G_CALLBACK (handle_toggle), snippet_tree_store);
gtk_tree_view_column_pack_start (column,
toggle_renderer,
- TRUE);
+ TRUE);
- text_renderer = gtk_cell_renderer_text_new();
+ gtk_tree_view_column_add_attribute (column,
+ toggle_renderer,
+ "active", SNIPPETS_STORE_COL_ACTIVE);
+
+ text_renderer = gtk_cell_renderer_text_new();
gtk_tree_view_column_pack_start (column,
text_renderer,
- TRUE);
+ TRUE);
gtk_tree_view_column_set_cell_data_func (column, text_renderer,
- snippets_view_name_text_data_func,
+ snippets_view_name_data_func,
snippets_tree_view, NULL);
- /* Column 2 -- Trigger Key */
+ /* Column 2 -- Trigger */
- column = gtk_tree_view_column_new ();
+ column = gtk_tree_view_column_new ();
gtk_tree_view_column_set_title (column, "Trigger");
gtk_tree_view_append_column (GTK_TREE_VIEW (snippets_tree_view),
column);
- text_renderer = gtk_cell_renderer_text_new();
+ text_renderer = gtk_cell_renderer_text_new();
gtk_tree_view_column_pack_start (column,
text_renderer,
- TRUE);
+ TRUE);
gtk_tree_view_column_set_cell_data_func (column, text_renderer,
snippets_view_trigger_data_func,
snippets_tree_view, NULL);
/* Column 3 -- Languages */
- column = gtk_tree_view_column_new ();
+ column = gtk_tree_view_column_new ();
gtk_tree_view_column_set_title (column, "Languages");
gtk_tree_view_append_column (GTK_TREE_VIEW (snippets_tree_view),
column);
- text_renderer = gtk_cell_renderer_text_new();
+ text_renderer = gtk_cell_renderer_text_new();
gtk_tree_view_column_pack_start (column,
text_renderer,
- TRUE);
+ TRUE);
gtk_tree_view_column_set_cell_data_func (column, text_renderer,
snippets_view_languages_data_func,
@@ -339,9 +479,9 @@ void snippets_manager_export_snippets (SnippetsDB *snippets_db,
/* Dialog for snippets chooser */
snippets_chooser = gtk_dialog_new_with_buttons ("Select Snippets",
- GTK_WINDOW (anjuta_shell),
- GTK_DIALOG_MODAL,
- NULL);
+ GTK_WINDOW (anjuta_shell),
+ GTK_DIALOG_MODAL,
+ NULL);
/* Add the tree-view to dialog */
scrolled_box = gtk_scrolled_window_new (NULL, NULL);
@@ -363,7 +503,6 @@ void snippets_manager_export_snippets (SnippetsDB *snippets_db,
/* Wait for the dialog to be closed */
if (gtk_dialog_run (GTK_DIALOG (snippets_chooser)) == GTK_RESPONSE_DELETE_EVENT)
{
- /*TODO select snippets for exporting*/
gtk_widget_destroy (snippets_chooser);
}
@@ -383,14 +522,56 @@ void snippets_manager_export_snippets (SnippetsDB *snippets_db,
{
gchar *uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (file_chooser)),
*path = anjuta_util_get_local_path_from_uri (uri);
+
+ /* Get the first iter from snippet_tree_store */
+ gtk_tree_model_get_iter_first (GTK_TREE_MODEL (snippet_tree_store),
+ &iter);
+ /* Select the snippets for exporting */
+ do
+ {
+ gtk_tree_model_get (GTK_TREE_MODEL (snippet_tree_store), &iter,
+ SNIPPETS_STORE_COL_ACTIVE, &active,
+ SNIPPETS_STORE_COL_SNIPPET_OBJECT, &object,
+ -1);
+
+ if (ANJUTA_IS_SNIPPETS_GROUP (object) && active)
+ {
+ snippets_group = snippets_group_new (snippets_group_get_name (ANJUTA_SNIPPETS_GROUP (object)));
+
+ if (gtk_tree_model_iter_children (GTK_TREE_MODEL (snippet_tree_store),
+ &child_iter, &iter))
+ {
+ /* Iterate through snippets from current group */
+ do
+ {
+ gtk_tree_model_get (GTK_TREE_MODEL (snippet_tree_store), &child_iter,
+ SNIPPETS_STORE_COL_ACTIVE, &active,
+ SNIPPETS_STORE_COL_SNIPPET_OBJECT, &snippet,
+ -1);
+ if (active)
+ {
+ snippets_group_add_snippet (snippets_group, snippet);
+ }
+ }
+ while (gtk_tree_model_iter_next (GTK_TREE_MODEL (snippet_tree_store),
+ &child_iter));
+ }
+
+ snippets_group_list = g_list_append (snippets_group_list, snippets_group);
+ }
+
+ }
+ while (gtk_tree_model_iter_next (GTK_TREE_MODEL (snippet_tree_store), &iter));
+
+ /* Save the snippets */
snippets_manager_save_snippets_xml_file (NATIVE_FORMAT,
- snippets_db_get_snippets_groups (snippets_db),
+ snippets_group_list,
path);
g_free (path);
g_free (uri);
}
-
+ g_object_unref (snippet_tree_store);
gtk_widget_destroy (file_chooser);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]