run-program: Remove trailing spaces
[anjuta.git] / plugins / snippets-manager / plugin.c
blobd56c42306ccfd0ffd5f777414e53971cc1759995
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 plugin.c
4 Copyright (C) Dragos Dena 2010
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301 USA
22 #include <string.h>
23 #include "plugin.h"
24 #include "snippet.h"
25 #include "snippets-import-export.h"
26 #include <libanjuta/interfaces/ianjuta-snippets-manager.h>
27 #include <libanjuta/interfaces/ianjuta-document-manager.h>
28 #include <libanjuta/interfaces/ianjuta-preferences.h>
29 #include <libanjuta/interfaces/ianjuta-editor-language.h>
30 #include <gio/gio.h>
31 #include <libanjuta/anjuta-shell.h>
32 #include <libanjuta/anjuta-debug.h>
33 #include <libanjuta/anjuta-utils.h>
35 #define ICON_FILE "anjuta-snippets-manager.png"
36 #define PREFERENCES_UI PACKAGE_DATA_DIR"/glade/snippets-manager-preferences.ui"
37 #define SNIPPETS_MANAGER_PREFERENCES_ROOT "snippets_preferences_root"
38 #define MENU_UI PACKAGE_DATA_DIR"/ui/snippets-manager-ui.xml"
40 #define GLOBAL_VAR_NEW_NAME "new_global_var_name"
41 #define GLOBAL_VAR_NEW_VALUE "new_global_var_value"
43 #define PREF_SCHEMA "org.gnome.anjuta.snippets"
45 static gpointer parent_class;
47 /* Menu callbacks and actions */
49 static void on_menu_trigger_insert_snippet (GtkAction *action,
50 SnippetsManagerPlugin *plugin);
51 static void on_menu_autocomplete_insert_snippet (GtkAction *action,
52 SnippetsManagerPlugin *plugin);
53 static void on_menu_import_snippets (GtkAction *action,
54 SnippetsManagerPlugin *plugin);
55 static void on_menu_export_snippets (GtkAction *action,
56 SnippetsManagerPlugin *plugin);
58 static GtkActionEntry actions_snippets[] = {
60 "ActionMenuEditSnippetsManager",
61 NULL,
62 N_("Snippets"),
63 NULL,
64 NULL,
65 NULL},
67 "ActionEditTriggerInsert",
68 NULL,
69 /* Translator: Appears in Edit->Snippets menu. It is used mainly for providing a
70 shortcut for the trigger-key based insertion of snippets. It's called like this
71 because you type the trigger-key in the editor, followed by Trigger Insert and
72 the snippet gets inserted. */
73 N_("_Trigger insert"),
74 "<control>e",
75 N_("Insert a snippet using the trigger-key"),
76 G_CALLBACK (on_menu_trigger_insert_snippet)},
78 "ActionEditAutoCompleteInsert",
79 NULL,
80 /* Translator: In a similar matter, it also appears in Edit->Snippets. It's another method
81 for inserting snippets. In this case, you call Auto complete insert, start
82 typing in the editor a string, the database is searched for that string and the
83 most relevant snippets are returned in a pop-up like the auto-complete one
84 (they are showed by their names). After selecting one of them, the snippet gets
85 inserted. */
86 N_("_Auto complete insert"),
87 "<control>r",
88 N_("Insert a snippet using auto-completion"),
89 G_CALLBACK (on_menu_autocomplete_insert_snippet)},
91 "ActionEditImportSnippets",
92 NULL,
93 N_("_Import snippets …"),
94 NULL,
95 N_("Import snippets to the database"),
96 G_CALLBACK (on_menu_import_snippets)},
98 "ActionEditExportSnippets",
99 NULL,
100 N_("_Export snippets …"),
101 NULL,
102 N_("Export snippets from the database"),
103 G_CALLBACK (on_menu_export_snippets)}
106 typedef struct _GlobalVariablesUpdateData
108 SnippetsDB *snippets_db;
109 GtkTreeView *global_vars_view;
110 } GlobalVariablesUpdateData;
112 gboolean
113 snippet_insert (SnippetsManagerPlugin * plugin,
114 const gchar *trigger,
115 gboolean editing_session)
117 AnjutaSnippet *requested_snippet = NULL;
118 SnippetsManagerPlugin *snippets_manager_plugin = NULL;
120 /* Assertions */
121 g_return_val_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (plugin),
122 FALSE);
123 snippets_manager_plugin = ANJUTA_PLUGIN_SNIPPETS_MANAGER (plugin);
125 /* Get the snippet from the database and check if it's not found */
126 requested_snippet = snippets_db_get_snippet (snippets_manager_plugin->snippets_db,
127 trigger,
128 NULL);
129 g_return_val_if_fail (ANJUTA_IS_SNIPPET (requested_snippet), FALSE);
131 /* Get the default content of the snippet */
132 snippets_interaction_insert_snippet (snippets_manager_plugin->snippets_interaction,
133 snippets_manager_plugin->snippets_db,
134 requested_snippet,
135 editing_session);
137 return TRUE;
140 static void
141 on_menu_trigger_insert_snippet (GtkAction *action,
142 SnippetsManagerPlugin *plugin)
144 /* Assertions */
145 g_return_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (plugin));
146 g_return_if_fail (ANJUTA_IS_SNIPPETS_DB (plugin->snippets_db));
147 g_return_if_fail (ANJUTA_IS_SNIPPETS_INTERACTION (plugin->snippets_interaction));
149 snippets_interaction_trigger_insert_request (plugin->snippets_interaction,
150 plugin->snippets_db);
154 static void
155 on_menu_autocomplete_insert_snippet (GtkAction *action,
156 SnippetsManagerPlugin *plugin)
158 /* Assertions */
159 g_return_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (plugin));
160 g_return_if_fail (ANJUTA_IS_SNIPPETS_PROVIDER (plugin->snippets_provider));
162 snippets_provider_request (plugin->snippets_provider);
165 static void
166 on_menu_import_snippets (GtkAction *action,
167 SnippetsManagerPlugin *plugin)
169 AnjutaPlugin *p = NULL;
171 /* Assertions */
172 g_return_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (plugin));
174 p = ANJUTA_PLUGIN (plugin);
175 snippets_manager_import_snippets (plugin->snippets_db, p->shell);
178 static void
179 on_menu_export_snippets (GtkAction *action,
180 SnippetsManagerPlugin *plugin)
182 AnjutaPlugin *p = NULL;
184 /* Assertions */
185 g_return_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (plugin));
187 p = ANJUTA_PLUGIN (plugin);
188 snippets_manager_export_snippets (plugin->snippets_db, p->shell);
191 static void
192 on_added_current_document (AnjutaPlugin *plugin,
193 const gchar *name,
194 const GValue *value,
195 gpointer data)
197 GObject *cur_editor = NULL;
198 SnippetsManagerPlugin *snippets_manager_plugin = NULL;
200 /* Assertions */
201 g_return_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (plugin));
202 snippets_manager_plugin = ANJUTA_PLUGIN_SNIPPETS_MANAGER (plugin);
204 /* Get the current document and test if it's an IAnjutaEditor */
205 cur_editor = g_value_get_object (value);
206 if (IANJUTA_IS_EDITOR (cur_editor))
207 snippets_interaction_set_editor (snippets_manager_plugin->snippets_interaction,
208 IANJUTA_EDITOR (cur_editor));
209 else
210 snippets_interaction_set_editor (snippets_manager_plugin->snippets_interaction,
211 NULL);
213 /* Refilter the snippets shown in the browser */
214 snippets_browser_refilter_snippets_view (snippets_manager_plugin->snippets_browser);
216 /* Load the provider if needed */
217 if (IANJUTA_IS_EDITOR_ASSIST (cur_editor))
218 snippets_provider_load (snippets_manager_plugin->snippets_provider,
219 IANJUTA_EDITOR_ASSIST (cur_editor));
223 static void
224 on_removed_current_document (AnjutaPlugin *plugin,
225 const char *name,
226 gpointer data)
228 SnippetsManagerPlugin *snippets_manager_plugin = NULL;
230 /* Assertions */
231 g_return_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (plugin));
232 snippets_manager_plugin = ANJUTA_PLUGIN_SNIPPETS_MANAGER (plugin);
234 /* Unload the provider */
235 snippets_provider_unload (snippets_manager_plugin->snippets_provider);
237 snippets_interaction_set_editor (snippets_manager_plugin->snippets_interaction,
238 NULL);
241 static void
242 on_snippets_browser_maximize_request (SnippetsBrowser *snippets_browser,
243 gpointer user_data)
245 SnippetsManagerPlugin *snippets_manager_plugin = NULL;
247 /* Assertions */
248 g_return_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (user_data));
249 snippets_manager_plugin = ANJUTA_PLUGIN_SNIPPETS_MANAGER (user_data);
251 if (snippets_manager_plugin->browser_maximized)
252 return;
254 anjuta_shell_maximize_widget (ANJUTA_PLUGIN (snippets_manager_plugin)->shell,
255 "snippets_browser", NULL);
256 snippets_browser_show_editor (snippets_browser);
258 snippets_manager_plugin->browser_maximized = TRUE;
261 static void
262 on_snippets_browser_unmaximize_request (SnippetsBrowser *snippets_browser,
263 gpointer user_data)
265 SnippetsManagerPlugin *snippets_manager_plugin = NULL;
267 /* Assertions */
268 g_return_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (user_data));
269 snippets_manager_plugin = ANJUTA_PLUGIN_SNIPPETS_MANAGER (user_data);
271 if (!snippets_manager_plugin->browser_maximized)
272 return;
274 anjuta_shell_unmaximize (ANJUTA_PLUGIN (snippets_manager_plugin)->shell,
275 NULL);
276 snippets_browser_hide_editor (snippets_browser);
278 snippets_manager_plugin->browser_maximized = FALSE;
281 static gboolean
282 snippets_manager_activate (AnjutaPlugin * plugin)
284 SnippetsManagerPlugin *snippets_manager_plugin = ANJUTA_PLUGIN_SNIPPETS_MANAGER (plugin);
285 AnjutaUI *anjuta_ui = NULL;
287 /* Assertions */
288 g_return_val_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (snippets_manager_plugin),
289 FALSE);
291 /* Link the AnjutaShell to the SnippetsDB and load the SnippetsDB*/
292 snippets_manager_plugin->snippets_db->anjuta_shell = plugin->shell;
293 snippets_db_load (snippets_manager_plugin->snippets_db);
295 /* Link the AnjutaShell to the SnippetsProvider and load if necessary */
296 snippets_manager_plugin->snippets_provider->anjuta_shell = plugin->shell;
298 /* Load the SnippetsBrowser with the snippets in the SnippetsDB */
299 snippets_manager_plugin->snippets_browser->anjuta_shell = plugin->shell;
300 snippets_browser_load (snippets_manager_plugin->snippets_browser,
301 snippets_manager_plugin->snippets_db,
302 snippets_manager_plugin->snippets_interaction);
303 gtk_widget_show_all (snippets_manager_plugin->snippets_browser);
304 anjuta_shell_add_widget_custom (plugin->shell,
305 GTK_WIDGET (snippets_manager_plugin->snippets_browser),
306 "snippets_browser",
307 _("Snippets"),
308 GTK_STOCK_FILE,
309 snippets_browser_get_grip (snippets_manager_plugin->snippets_browser),
310 ANJUTA_SHELL_PLACEMENT_LEFT,
311 NULL);
312 snippets_manager_plugin->browser_maximized = FALSE;
314 /* Initialize the Interaction Interpreter */
315 snippets_interaction_start (snippets_manager_plugin->snippets_interaction,
316 plugin->shell);
318 /* Add a watch for the current document */
319 snippets_manager_plugin->cur_editor_watch_id =
320 anjuta_plugin_add_watch (plugin,
321 IANJUTA_DOCUMENT_MANAGER_CURRENT_DOCUMENT,
322 on_added_current_document,
323 on_removed_current_document,
324 NULL);
326 /* Merge the Menu UI */
327 anjuta_ui = anjuta_shell_get_ui (plugin->shell, FALSE);
329 snippets_manager_plugin->action_group =
330 anjuta_ui_add_action_group_entries (anjuta_ui,
331 "ActionGroupSnippetsManager",
332 _("Snippets Manager actions"),
333 actions_snippets,
334 G_N_ELEMENTS (actions_snippets),
335 GETTEXT_PACKAGE,
336 TRUE,
337 snippets_manager_plugin);
339 snippets_manager_plugin->uiid = anjuta_ui_merge (anjuta_ui, MENU_UI);
341 DEBUG_PRINT ("%s", "SnippetsManager: Activating SnippetsManager plugin …");
343 return TRUE;
346 static gboolean
347 snippets_manager_deactivate (AnjutaPlugin *plugin)
349 SnippetsManagerPlugin *snippets_manager_plugin = NULL;
350 AnjutaUI *anjuta_ui = NULL;
352 /* Assertions */
353 g_return_val_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (plugin), FALSE);
354 snippets_manager_plugin = ANJUTA_PLUGIN_SNIPPETS_MANAGER (plugin);
356 DEBUG_PRINT ("%s", "SnippetsManager: Deactivating SnippetsManager plugin …");
358 anjuta_plugin_remove_watch (plugin,
359 snippets_manager_plugin->cur_editor_watch_id,
360 FALSE);
362 /* Remove the Menu UI */
363 anjuta_ui = anjuta_shell_get_ui (plugin->shell, NULL);
364 anjuta_ui_unmerge (anjuta_ui, snippets_manager_plugin->uiid);
365 anjuta_ui_remove_action_group (anjuta_ui, snippets_manager_plugin->action_group);
367 /* Unload the SnippetsBrowser */
368 if (snippets_manager_plugin->browser_maximized)
369 on_snippets_browser_unmaximize_request (snippets_manager_plugin->snippets_browser,
370 snippets_manager_plugin);
371 snippets_browser_unload (snippets_manager_plugin->snippets_browser);
372 g_object_ref (snippets_manager_plugin->snippets_browser);
373 anjuta_shell_remove_widget (plugin->shell,
374 GTK_WIDGET (snippets_manager_plugin->snippets_browser),
375 NULL);
377 /* Destroy the SnippetsDB */
378 snippets_db_close (snippets_manager_plugin->snippets_db);
380 /* Destroy the Interaction Interpreter */
381 snippets_interaction_destroy (snippets_manager_plugin->snippets_interaction);
383 /* Unload the SnippetsProvider */
384 snippets_provider_unload (snippets_manager_plugin->snippets_provider);
386 return TRUE;
389 static void
390 snippets_manager_finalize (GObject * obj)
393 G_OBJECT_CLASS (parent_class)->finalize (obj);
396 static void
397 snippets_manager_dispose (GObject * obj)
399 SnippetsManagerPlugin *snippets_manager = ANJUTA_PLUGIN_SNIPPETS_MANAGER (obj);
401 /* Assertions */
402 g_return_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (snippets_manager));
404 if (ANJUTA_IS_SNIPPETS_DB (snippets_manager->snippets_db))
405 g_object_unref (snippets_manager->snippets_db);
407 if (ANJUTA_IS_SNIPPETS_INTERACTION (snippets_manager->snippets_interaction))
408 g_object_unref (snippets_manager->snippets_interaction);
410 if (ANJUTA_IS_SNIPPETS_BROWSER (snippets_manager->snippets_browser))
411 g_object_unref (snippets_manager->snippets_browser);
413 if (ANJUTA_IS_SNIPPETS_PROVIDER (snippets_manager->snippets_provider))
414 g_object_unref (snippets_manager->snippets_provider);
416 g_object_unref (snippets_manager->settings);
418 G_OBJECT_CLASS (parent_class)->dispose (obj);
421 static void
422 snippets_manager_plugin_instance_init (GObject * obj)
424 SnippetsManagerPlugin *snippets_manager = ANJUTA_PLUGIN_SNIPPETS_MANAGER (obj);
426 snippets_manager->overwrite_on_conflict = FALSE;
427 snippets_manager->show_only_document_language_snippets = FALSE;
429 snippets_manager->cur_editor_watch_id = -1;
431 snippets_manager->action_group = NULL;
432 snippets_manager->uiid = -1;
434 snippets_manager->settings = g_settings_new (PREF_SCHEMA);
436 snippets_manager->snippets_db = snippets_db_new ();
437 snippets_manager->snippets_interaction = snippets_interaction_new ();
438 snippets_manager->snippets_browser = snippets_browser_new ();
439 snippets_manager->snippets_provider =
440 snippets_provider_new (snippets_manager->snippets_db,
441 snippets_manager->snippets_interaction);
443 g_signal_connect (G_OBJECT (snippets_manager->snippets_browser),
444 "maximize-request",
445 G_CALLBACK (on_snippets_browser_maximize_request),
446 snippets_manager);
447 g_signal_connect (G_OBJECT (snippets_manager->snippets_browser),
448 "unmaximize-request",
449 G_CALLBACK (on_snippets_browser_unmaximize_request),
450 snippets_manager);
454 static void
455 snippets_manager_plugin_class_init (GObjectClass * klass)
457 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
459 parent_class = g_type_class_peek_parent (klass);
461 plugin_class->activate = snippets_manager_activate;
462 plugin_class->deactivate = snippets_manager_deactivate;
463 klass->dispose = snippets_manager_dispose;
464 klass->finalize = snippets_manager_finalize;
469 /* IAnjutaSnippetsManager interface */
471 static gboolean
472 isnippets_manager_iface_insert (IAnjutaSnippetsManager* snippets_manager,
473 const gchar* key,
474 gboolean editing_session,
475 GError** err)
477 SnippetsManagerPlugin* plugin = ANJUTA_PLUGIN_SNIPPETS_MANAGER (snippets_manager);
478 snippet_insert (plugin, key, editing_session);
479 return TRUE;
482 static void
483 isnippets_manager_iface_init (IAnjutaSnippetsManagerIface *iface)
485 iface->insert = isnippets_manager_iface_insert;
490 /* IAnjutaPreferences interface */
492 static void
493 on_global_vars_name_changed (GtkCellRendererText *cell,
494 gchar *path_string,
495 gchar *new_text,
496 gpointer user_data)
498 GtkTreeModel *global_vars_model = NULL;
499 SnippetsDB *snippets_db = NULL;
500 GtkTreePath *path = NULL;
501 GtkTreeIter iter;
502 gchar *name = NULL;
504 /* Assertions */
505 g_return_if_fail (ANJUTA_IS_SNIPPETS_DB (user_data));
506 snippets_db = ANJUTA_SNIPPETS_DB (user_data);
507 global_vars_model = snippets_db_get_global_vars_model (snippets_db);
508 g_return_if_fail (GTK_IS_TREE_MODEL (global_vars_model));
510 /* Get the iter */
511 path = gtk_tree_path_new_from_string (path_string);
512 gtk_tree_model_get_iter (global_vars_model, &iter, path);
514 /* Get the current type and change it */
515 gtk_tree_model_get (global_vars_model, &iter,
516 GLOBAL_VARS_MODEL_COL_NAME, &name,
517 -1);
518 snippets_db_set_global_variable_name (snippets_db,
519 name,
520 new_text);
521 g_free (name);
523 /* Save the global variables */
524 snippets_db_save_global_vars (snippets_db);
527 static void
528 global_vars_view_name_data_func (GtkTreeViewColumn *col,
529 GtkCellRenderer *cell,
530 GtkTreeModel *global_vars_model,
531 GtkTreeIter *iter,
532 gpointer user_data)
534 gchar *name = NULL;
535 gboolean is_internal = FALSE;
537 /* Assertions */
538 g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (cell));
540 /* Get the name */
541 gtk_tree_model_get (global_vars_model, iter,
542 GLOBAL_VARS_MODEL_COL_NAME, &name,
543 -1);
545 /* Check if it's internal */
546 gtk_tree_model_get (global_vars_model, iter,
547 GLOBAL_VARS_MODEL_COL_IS_INTERNAL, &is_internal,
548 -1);
549 if (is_internal)
551 gchar *temp = NULL;
552 temp = g_strconcat ("<b>", name, "</b> <i>(Internal)</i>", NULL);
553 g_free (name);
554 name = temp;
555 g_object_set (cell, "sensitive", FALSE, NULL);
556 g_object_set (cell, "editable", FALSE, NULL);
558 else
560 gchar *temp = NULL;
561 temp = g_strconcat ("<b>", name, "</b>", NULL);
562 g_free (name);
563 name = temp;
564 g_object_set (cell, "sensitive", TRUE, NULL);
565 g_object_set (cell, "editable", TRUE, NULL);
568 g_object_set (cell, "markup", name, NULL);
569 g_free (name);
572 static void
573 on_global_vars_type_toggled (GtkCellRendererToggle *cell,
574 gchar *path_string,
575 gpointer user_data)
577 GtkTreeModel *global_vars_model = NULL;
578 SnippetsDB *snippets_db = NULL;
579 GtkTreePath *path = NULL;
580 GtkTreeIter iter;
581 gboolean is_command = FALSE;
582 gchar *name = NULL;
584 /* Assertions */
585 g_return_if_fail (ANJUTA_IS_SNIPPETS_DB (user_data));
586 snippets_db = ANJUTA_SNIPPETS_DB (user_data);
587 global_vars_model = snippets_db_get_global_vars_model (snippets_db);
588 g_return_if_fail (GTK_IS_TREE_MODEL (global_vars_model));
590 /* Get the iter */
591 path = gtk_tree_path_new_from_string (path_string);
592 gtk_tree_model_get_iter (global_vars_model, &iter, path);
594 /* Get the current type and change it */
595 gtk_tree_model_get (global_vars_model, &iter,
596 GLOBAL_VARS_MODEL_COL_IS_COMMAND, &is_command,
597 GLOBAL_VARS_MODEL_COL_NAME, &name,
598 -1);
599 snippets_db_set_global_variable_type (snippets_db,
600 name,
601 (is_command) ? FALSE : TRUE);
603 /* Save the global variables */
604 snippets_db_save_global_vars (snippets_db);
606 g_free (name);
610 static void
611 global_vars_view_type_data_func (GtkTreeViewColumn *col,
612 GtkCellRenderer *cell,
613 GtkTreeModel *global_vars_model,
614 GtkTreeIter *iter,
615 gpointer user_data)
617 gboolean is_command = FALSE, is_internal = TRUE;
619 /* Assertions */
620 g_return_if_fail (GTK_IS_CELL_RENDERER_TOGGLE (cell));
623 /* Check if it's internal */
624 gtk_tree_model_get (global_vars_model, iter,
625 GLOBAL_VARS_MODEL_COL_IS_INTERNAL, &is_internal,
626 -1);
627 if (is_internal)
629 g_object_set (cell, "sensitive", FALSE, NULL);
630 gtk_cell_renderer_toggle_set_activatable (GTK_CELL_RENDERER_TOGGLE (cell), FALSE);
631 gtk_cell_renderer_toggle_set_active (GTK_CELL_RENDERER_TOGGLE (cell), FALSE);
633 else
635 gtk_tree_model_get (global_vars_model, iter,
636 GLOBAL_VARS_MODEL_COL_IS_COMMAND, &is_command,
637 -1);
638 g_object_set (cell, "sensitive", TRUE, NULL);
639 gtk_cell_renderer_toggle_set_activatable (GTK_CELL_RENDERER_TOGGLE (cell), TRUE);
640 gtk_cell_renderer_toggle_set_active (GTK_CELL_RENDERER_TOGGLE (cell), is_command);
644 static void
645 on_global_vars_text_changed (GtkCellRendererText *cell,
646 gchar *path_string,
647 gchar *new_text,
648 gpointer user_data)
650 GtkTreeModel *global_vars_model = NULL;
651 SnippetsDB *snippets_db = NULL;
652 GtkTreePath *path = NULL;
653 GtkTreeIter iter;
654 gchar *name = NULL;
656 /* Assertions */
657 g_return_if_fail (ANJUTA_IS_SNIPPETS_DB (user_data));
658 snippets_db = ANJUTA_SNIPPETS_DB (user_data);
659 global_vars_model = snippets_db_get_global_vars_model (snippets_db);
660 g_return_if_fail (GTK_IS_TREE_MODEL (global_vars_model));
662 /* Get the iter */
663 path = gtk_tree_path_new_from_string (path_string);
664 gtk_tree_model_get_iter (global_vars_model, &iter, path);
666 /* Get the current type and change it */
667 gtk_tree_model_get (global_vars_model, &iter,
668 GLOBAL_VARS_MODEL_COL_NAME, &name,
669 -1);
670 snippets_db_set_global_variable_value (snippets_db,
671 name,
672 new_text);
673 g_free (name);
675 /* Save the global variables */
676 snippets_db_save_global_vars (snippets_db);
679 static void
680 global_vars_view_text_data_func (GtkTreeViewColumn *col,
681 GtkCellRenderer *cell,
682 GtkTreeModel *global_vars_model,
683 GtkTreeIter *iter,
684 gpointer user_data)
686 gchar *name = NULL, *text = NULL;
687 SnippetsDB *snippets_db = NULL;
688 gboolean is_internal = FALSE;
690 /* Assertions */
691 g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (cell));
692 g_return_if_fail (ANJUTA_IS_SNIPPETS_DB (user_data));
693 snippets_db = ANJUTA_SNIPPETS_DB (user_data);
695 /* Get the name */
696 gtk_tree_model_get (global_vars_model, iter,
697 GLOBAL_VARS_MODEL_COL_NAME, &name,
698 GLOBAL_VARS_MODEL_COL_IS_INTERNAL, &is_internal,
699 -1);
701 if (is_internal)
703 g_object_set (cell, "editable", FALSE, NULL);
705 else
707 g_object_set (cell, "editable", TRUE, NULL);
710 text = snippets_db_get_global_variable_text (snippets_db, name);
712 g_object_set (cell, "text", text, NULL);
713 g_free (name);
714 g_free (text);
717 static void
718 global_vars_view_value_data_func (GtkTreeViewColumn *col,
719 GtkCellRenderer *cell,
720 GtkTreeModel *global_vars_model,
721 GtkTreeIter *iter,
722 gpointer user_data)
724 gchar *name = NULL, *value = NULL;
725 SnippetsDB *snippets_db = NULL;
727 /* Assertions */
728 g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (cell));
729 g_return_if_fail (ANJUTA_IS_SNIPPETS_DB (user_data));
730 snippets_db = ANJUTA_SNIPPETS_DB (user_data);
732 /* Get the name */
733 gtk_tree_model_get (global_vars_model, iter,
734 GLOBAL_VARS_MODEL_COL_NAME, &name,
735 -1);
737 value = snippets_db_get_global_variable (snippets_db, name);
739 g_object_set (cell, "text", value, NULL);
740 g_free (name);
743 static void
744 set_up_global_variables_view (SnippetsManagerPlugin *snippets_manager_plugin,
745 GtkTreeView *global_vars_view)
747 GtkCellRenderer *cell = NULL;
748 GtkTreeViewColumn *col = NULL;
749 GtkTreeModel *global_vars_model = NULL;
751 /* Assertions */
752 global_vars_model = snippets_db_get_global_vars_model (snippets_manager_plugin->snippets_db);
753 g_return_if_fail (GTK_IS_TREE_MODEL (global_vars_model));
754 g_return_if_fail (GTK_IS_TREE_VIEW (global_vars_view));
756 /* Set up the model */
757 gtk_tree_view_set_model (global_vars_view,
758 global_vars_model);
760 /* Set up the name cell */
761 cell = gtk_cell_renderer_text_new ();
762 col = gtk_tree_view_column_new ();
763 gtk_tree_view_column_set_title (col, _("Name"));
764 gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
765 gtk_tree_view_column_set_resizable (col, TRUE);
766 gtk_tree_view_column_pack_start (col, cell, FALSE);
767 gtk_tree_view_column_set_cell_data_func (col, cell,
768 global_vars_view_name_data_func,
769 NULL, NULL);
770 gtk_tree_view_append_column (global_vars_view, col);
771 g_signal_connect (G_OBJECT (cell),
772 "edited",
773 G_CALLBACK (on_global_vars_name_changed),
774 snippets_manager_plugin->snippets_db);
776 /* Set up the type cell */
777 cell = gtk_cell_renderer_toggle_new ();
778 col = gtk_tree_view_column_new ();
779 gtk_tree_view_column_set_title (col, _("Command?"));
780 gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
781 gtk_tree_view_column_set_resizable (col, TRUE);
782 gtk_tree_view_column_pack_start (col, cell, FALSE);
783 gtk_tree_view_column_set_cell_data_func (col, cell,
784 global_vars_view_type_data_func,
785 NULL, NULL);
786 gtk_tree_view_append_column (global_vars_view, col);
787 g_signal_connect (G_OBJECT (cell),
788 "toggled",
789 G_CALLBACK (on_global_vars_type_toggled),
790 snippets_manager_plugin->snippets_db);
792 /* Set up the text cell */
793 cell = gtk_cell_renderer_text_new ();
794 col = gtk_tree_view_column_new ();
795 gtk_tree_view_column_set_title (col, _("Variable text"));
796 gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
797 gtk_tree_view_column_set_resizable (col, TRUE);
798 gtk_tree_view_column_pack_start (col, cell, FALSE);
799 gtk_tree_view_column_set_cell_data_func (col, cell,
800 global_vars_view_text_data_func,
801 snippets_manager_plugin->snippets_db,
802 NULL);
803 gtk_tree_view_append_column (global_vars_view, col);
804 g_signal_connect (G_OBJECT (cell),
805 "edited",
806 G_CALLBACK (on_global_vars_text_changed),
807 snippets_manager_plugin->snippets_db);
809 /* Set up the instant value cell */
810 cell = gtk_cell_renderer_text_new ();
811 g_object_set (cell, "editable", FALSE, NULL);
812 col = gtk_tree_view_column_new ();
813 gtk_tree_view_column_set_title (col, _("Instant value"));
814 gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
815 gtk_tree_view_column_set_resizable (col, TRUE);
816 gtk_tree_view_column_pack_start (col, cell, FALSE);
817 gtk_tree_view_column_set_cell_data_func (col, cell,
818 global_vars_view_value_data_func,
819 snippets_manager_plugin->snippets_db,
820 NULL);
821 gtk_tree_view_append_column (global_vars_view, col);
825 static void
826 on_add_variable_b_clicked (GtkButton *button,
827 gpointer user_data)
829 GlobalVariablesUpdateData *update_data = (GlobalVariablesUpdateData *)user_data;
830 GtkTreeView *global_vars_view = NULL;
831 GtkTreeModel *global_vars_model = NULL;
832 SnippetsDB *snippets_db = NULL;
833 GtkTreeIter iter;
834 gboolean iter_has_next = TRUE;
835 gchar *name = NULL;
837 /* Assertions */
838 g_return_if_fail (ANJUTA_IS_SNIPPETS_DB (update_data->snippets_db));
839 g_return_if_fail (GTK_IS_TREE_VIEW (update_data->global_vars_view));
840 snippets_db = ANJUTA_SNIPPETS_DB (update_data->snippets_db);
841 global_vars_view = GTK_TREE_VIEW (update_data->global_vars_view);
842 global_vars_model = snippets_db_get_global_vars_model (snippets_db);
844 /* Insert it into the SnippetsDB */
845 snippets_db_add_global_variable (snippets_db,
846 GLOBAL_VAR_NEW_NAME,
847 GLOBAL_VAR_NEW_VALUE,
848 FALSE, FALSE);
850 /* Get to the new inserted variable */
851 iter_has_next = gtk_tree_model_get_iter_first (global_vars_model, &iter);
852 while (iter_has_next)
854 gtk_tree_model_get (global_vars_model, &iter,
855 GLOBAL_VARS_MODEL_COL_NAME, &name,
856 -1);
857 if (!g_strcmp0 (name, GLOBAL_VAR_NEW_NAME))
859 GtkTreePath *path = gtk_tree_model_get_path (global_vars_model, &iter);
861 gtk_tree_view_set_cursor (global_vars_view,
862 path,
863 gtk_tree_view_get_column (global_vars_view, 0),
864 TRUE);
866 gtk_tree_path_free (path);
867 g_free (name);
868 return;
871 g_free (name);
872 iter_has_next = gtk_tree_model_iter_next (global_vars_model, &iter);
875 /* Save the global variables */
876 snippets_db_save_global_vars (snippets_db);
879 static void
880 on_delete_variable_b_clicked (GtkButton *button,
881 gpointer user_data)
883 GlobalVariablesUpdateData *update_data = (GlobalVariablesUpdateData *)user_data;
884 GtkTreeView *global_vars_view = NULL;
885 GtkTreeModel *global_vars_model = NULL;
886 SnippetsDB *snippets_db = NULL;
887 GtkTreeSelection *global_vars_selection = NULL;
888 gchar *name = NULL;
889 gboolean has_selection = FALSE;
890 GtkTreeIter iter;
892 /* Assertions */
893 g_return_if_fail (ANJUTA_IS_SNIPPETS_DB (update_data->snippets_db));
894 g_return_if_fail (GTK_IS_TREE_VIEW (update_data->global_vars_view));
895 snippets_db = ANJUTA_SNIPPETS_DB (update_data->snippets_db);
896 global_vars_view = GTK_TREE_VIEW (update_data->global_vars_view);
897 global_vars_model = snippets_db_get_global_vars_model (snippets_db);
898 global_vars_selection = gtk_tree_view_get_selection (global_vars_view);
900 /* Get the selected iter */
901 has_selection = gtk_tree_selection_get_selected (global_vars_selection,
902 &global_vars_model,
903 &iter);
905 /* If there is a selection delete the selected item */
906 if (has_selection)
908 gtk_tree_model_get (global_vars_model, &iter,
909 GLOBAL_VARS_MODEL_COL_NAME, &name,
910 -1);
911 snippets_db_remove_global_variable (snippets_db, name);
912 g_free (name);
916 /* Save the global variables */
917 snippets_db_save_global_vars (snippets_db);
920 static void
921 ipreferences_merge (IAnjutaPreferences* ipref,
922 AnjutaPreferences* prefs,
923 GError** e)
925 GError* error = NULL;
926 GtkBuilder* bxml = gtk_builder_new ();
927 GtkTreeView *global_vars_view = NULL;
928 GtkButton *add_variable_b = NULL, *delete_variable_b = NULL;
929 SnippetsManagerPlugin *snippets_manager_plugin = NULL;
930 GlobalVariablesUpdateData *global_vars_update_data = NULL;
932 /* Assertions */
933 snippets_manager_plugin = ANJUTA_PLUGIN_SNIPPETS_MANAGER (ipref);
934 g_return_if_fail (ANJUTA_IS_PLUGIN_SNIPPETS_MANAGER (snippets_manager_plugin));
936 if (!gtk_builder_add_from_file (bxml, PREFERENCES_UI, &error))
938 g_warning ("Couldn't load preferences ui file: %s", error->message);
939 g_error_free (error);
941 anjuta_preferences_add_from_builder (prefs, bxml, snippets_manager_plugin->settings,
942 SNIPPETS_MANAGER_PREFERENCES_ROOT, _("Code Snippets"),
943 ICON_FILE);
945 /* Get the Gtk objects */
946 global_vars_view = GTK_TREE_VIEW (gtk_builder_get_object (bxml, "global_vars_view"));
947 add_variable_b = GTK_BUTTON (gtk_builder_get_object (bxml, "add_var_button"));
948 delete_variable_b = GTK_BUTTON (gtk_builder_get_object (bxml, "delete_var_button"));
949 g_return_if_fail (GTK_IS_TREE_VIEW (global_vars_view));
950 g_return_if_fail (GTK_IS_BUTTON (add_variable_b));
951 g_return_if_fail (GTK_IS_BUTTON (delete_variable_b));
953 /* Set up the Global Variables GtkTreeView */
954 set_up_global_variables_view (snippets_manager_plugin, global_vars_view);
956 /* Connect the addition/deletion buttons */
957 global_vars_update_data = g_malloc (sizeof (GlobalVariablesUpdateData));
958 global_vars_update_data->snippets_db = snippets_manager_plugin->snippets_db;
959 global_vars_update_data->global_vars_view = global_vars_view;
961 g_signal_connect (G_OBJECT (add_variable_b),
962 "clicked",
963 G_CALLBACK (on_add_variable_b_clicked),
964 global_vars_update_data);
966 g_signal_connect (G_OBJECT (delete_variable_b),
967 "clicked",
968 G_CALLBACK (on_delete_variable_b_clicked),
969 global_vars_update_data);
971 g_object_unref (bxml);
974 static void
975 ipreferences_unmerge (IAnjutaPreferences* ipref,
976 AnjutaPreferences* prefs,
977 GError** e)
979 anjuta_preferences_remove_page (prefs, _("Code Snippets"));
982 static void
983 ipreferences_iface_init (IAnjutaPreferencesIface* iface)
985 iface->merge = ipreferences_merge;
986 iface->unmerge = ipreferences_unmerge;
990 ANJUTA_PLUGIN_BEGIN (SnippetsManagerPlugin, snippets_manager_plugin);
991 ANJUTA_PLUGIN_ADD_INTERFACE (isnippets_manager, IANJUTA_TYPE_SNIPPETS_MANAGER);
992 ANJUTA_PLUGIN_ADD_INTERFACE (ipreferences, IANJUTA_TYPE_PREFERENCES);
993 ANJUTA_PLUGIN_END
995 ANJUTA_SIMPLE_PLUGIN (SnippetsManagerPlugin, snippets_manager_plugin);