[anjuta] glade: bgo #700383 - Anjuta should automatically associate .ui and source files



commit 47a78d610e0ec6a8633652b4f69b1ad6bb7ae19a
Author: Marco Diego Aurélio Mesquita <marcodiegomesquita gmail com>
Date:   Wed May 29 22:36:42 2013 +0200

    glade: bgo #700383 - Anjuta should automatically associate .ui and source files

 libanjuta/interfaces/libanjuta.idl         |   17 +++-
 plugins/glade/anjuta-glade.plugin.in       |    2 +-
 plugins/glade/plugin.c                     |  147 ++++++++++++++++++++++++---
 plugins/language-support-cpp-java/plugin.c |   80 ++++++++++++++-
 4 files changed, 222 insertions(+), 24 deletions(-)
---
diff --git a/libanjuta/interfaces/libanjuta.idl b/libanjuta/interfaces/libanjuta.idl
index b2fcda2..e7c1dce 100644
--- a/libanjuta/interfaces/libanjuta.idl
+++ b/libanjuta/interfaces/libanjuta.idl
@@ -1249,6 +1249,22 @@ interface IAnjutaDocument
                void clear ();
 }
 
+
+interface IAnjutaGlade
+{
+       /**
+        * ianjuta_add_association:
+        * @obj: Self
+        * @master: ui file name.
+        * @slave: source code file name.
+        * @err: Error propagation and reporting.
+        *
+        * Adds an association of master ui file and slave source code file
+        *
+        */
+       void add_association (gchar *master, gchar *slave);
+}
+
 /**
  * SECTION:ianjuta-editor
  * @title: IAnjutaEditor
@@ -1946,7 +1962,6 @@ interface IAnjutaEditor
                 gboolean backward (const gchar* search, gboolean case_sensitive, IAnjutaEditorCell* start, 
IAnjutaEditorCell* end, IAnjutaEditorCell** result_start, IAnjutaEditorCell** result_end);
        }
 
-
        /**
         * SECTION:ianjuta-editor-convert
         * @title: IAnjutaEditorConvert
diff --git a/plugins/glade/anjuta-glade.plugin.in b/plugins/glade/anjuta-glade.plugin.in
index 8cb3480..5ca7b93 100644
--- a/plugins/glade/anjuta-glade.plugin.in
+++ b/plugins/glade/anjuta-glade.plugin.in
@@ -3,7 +3,7 @@ _Name=Glade interface designer
 _Description=Glade Plugin for Anjuta.
 Location=anjuta-glade:GladePlugin
 Icon=anjuta-glade-plugin-48.png
-Interfaces=IAnjutaFile,IAnjutaWizard
+Interfaces=IAnjutaFile,IAnjutaWizard, IAnjutaGlade
 Dependencies=anjuta-project-manager:ProjectManagerPlugin
 UserActivatable=no
 
diff --git a/plugins/glade/plugin.c b/plugins/glade/plugin.c
index c4a75ce..307cb7c 100644
--- a/plugins/glade/plugin.c
+++ b/plugins/glade/plugin.c
@@ -25,6 +25,7 @@
 #include <libanjuta/interfaces/ianjuta-wizard.h>
 #include <libanjuta/interfaces/ianjuta-help.h>
 #include <libanjuta/interfaces/ianjuta-document-manager.h>
+#include <libanjuta/interfaces/ianjuta-glade.h>
 
 #include "plugin.h"
 #include "anjuta-design-document.h"
@@ -64,6 +65,9 @@ struct _GladePluginPriv
 
        /* for status */
        gboolean add_ticks;
+
+       /* Association between ui and source files */
+       GHashTable *associations;
 };
 
 enum {
@@ -113,6 +117,81 @@ value_removed_current_editor (AnjutaPlugin *plugin,
 }
 
 static void
+glade_plugin_remove_associations (GladePlugin *plugin, gchar *master, GError **err)
+{
+       g_hash_table_remove (plugin->priv->associations, master);
+}
+
+static void
+_get_associated_with (gpointer key,
+                           gpointer value,
+                           gpointer user_data)
+{
+       struct struct_item {GList *list; gchar *search;} *item = user_data;
+
+       if (g_str_equal (value, item->search))
+               item->list = g_list_prepend (item->list, key);
+}
+
+static GList*
+glade_plugin_get_associated_with (GladePlugin *plugin, gchar *slave, GError **err)
+{
+       struct struct_item {GList *list; gchar *search;} item;
+       item.list = NULL;
+       item.search = slave;
+       g_hash_table_foreach (plugin->priv->associations, _get_associated_with, &item);
+       return item.list;
+}
+
+static void
+glade_plugin_remove_associated_with (GladePlugin *plugin, gchar *slave, GError **err)
+{
+       GList *associated = glade_plugin_get_associated_with (plugin, slave, err);
+
+       if (!associated)
+               return;
+
+       for (;associated; associated = associated->next)
+       {
+               glade_plugin_remove_associations (plugin, associated->data, err);
+       }
+
+       g_list_free (associated);
+}
+
+static void
+glade_plugin_add_association (GladePlugin *plugin, gchar *master, gchar *slave)
+{
+       g_hash_table_replace (plugin->priv->associations, master, slave);
+}
+
+static gchar*
+glade_plugin_get_association (GladePlugin *plugin, gchar *master)
+{
+       return g_hash_table_lookup (plugin->priv->associations, master);
+}
+
+static IAnjutaEditor*
+get_doc_with_associated_file (GladePlugin *plugin, IAnjutaDocument *doc)
+{
+       gchar *filename = ianjuta_document_get_filename (doc, NULL);
+       gchar *associated = glade_plugin_get_association (plugin, filename);
+
+       if (!associated)
+               return NULL;
+
+       IAnjutaDocumentManager *docman;
+
+    docman = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
+                                         IAnjutaDocumentManager, NULL);
+       GFile *target_file = ianjuta_document_manager_get_file (docman, associated, NULL);
+       IAnjutaDocument *doc_with_file =
+       ianjuta_document_manager_find_document_with_file (docman, target_file, NULL);
+       ianjuta_document_manager_set_current_document (docman, doc_with_file, NULL);
+       return IANJUTA_EDITOR (doc_with_file);
+}
+
+static void
 signal_editor_signal_activated_cb (GladeSignalEditor* seditor, 
                                    GladeSignal *signal,
                                    GladePlugin *plugin)
@@ -134,22 +213,21 @@ signal_editor_signal_activated_cb (GladeSignalEditor* seditor,
        if(!doc)
                return;
 
-    if (!IANJUTA_IS_EDITOR (doc))
-        return;
+       current_editor = IANJUTA_IS_EDITOR (doc) ? IANJUTA_EDITOR (doc)
+                                                                                        : 
get_doc_with_associated_file (plugin, doc);
 
-       current_editor = IANJUTA_EDITOR (doc);
+       if(!current_editor)
+           return;
 
-    if(!current_editor)
-        return;
+       g_signal_emit_by_name (G_OBJECT (current_editor), "glade-callback-add",
+                                                         G_OBJECT_TYPE_NAME (glade_widget_get_object 
(gwidget)),
+                                                         glade_signal_get_name (signal),
+                                                         glade_signal_get_handler (signal),
+                                                         glade_signal_get_userdata (signal),
+                                                         glade_signal_get_swapped (signal)?"1":"0",
+                                                         glade_signal_get_after (signal)?"1":"0",
+                                                         path);
 
-    g_signal_emit_by_name (G_OBJECT (current_editor), "glade-callback-add",
-                                                      G_OBJECT_TYPE_NAME (glade_widget_get_object (gwidget)),
-                                                      glade_signal_get_name (signal),
-                                                      glade_signal_get_handler (signal),
-                                                      glade_signal_get_userdata (signal),
-                                                      glade_signal_get_swapped (signal)?"1":"0",
-                                                      glade_signal_get_after (signal)?"1":"0",
-                                                      path);
 }
 
 static void
@@ -408,10 +486,8 @@ add_glade_member (GladeWidget               *widget,
        if(!doc)
                return;
 
-       if (!IANJUTA_IS_EDITOR (doc))
-               return;
-
-       current_editor = IANJUTA_EDITOR (doc);
+       current_editor = IANJUTA_IS_EDITOR (doc) ? IANJUTA_EDITOR (doc)
+                                                                                        : 
get_doc_with_associated_file (plugin, doc);
 
        if (!current_editor)
                return;
@@ -616,6 +692,21 @@ static GtkActionEntry actions_glade[] =
        }
 };
 
+static void
+on_document_removed(IAnjutaDocumentManager* docman, IAnjutaDocument* doc, AnjutaPlugin *plugin)
+{
+       GladePlugin *glade_plugin;
+       glade_plugin = ANJUTA_PLUGIN_GLADE (plugin);
+
+       gchar *filename = ianjuta_document_get_filename(doc, NULL);
+
+       if (filename)
+       {
+               glade_plugin_remove_associations (ANJUTA_PLUGIN_GLADE (plugin), filename, NULL);
+               glade_plugin_remove_associated_with (ANJUTA_PLUGIN_GLADE (plugin), filename, NULL);
+       }
+}
+
 static gboolean
 activate_plugin (AnjutaPlugin *plugin)
 {
@@ -744,6 +835,13 @@ activate_plugin (AnjutaPlugin *plugin)
        g_signal_connect (G_OBJECT (plugin->shell), "save_session",
                          G_CALLBACK (on_session_save), plugin);
 
+       /* Connecto to handle document close */
+       IAnjutaDocumentManager* docman = anjuta_shell_get_interface(ANJUTA_PLUGIN(plugin)->shell,
+                                                                                                             
                  IAnjutaDocumentManager,
+                                                                                           NULL);
+       g_signal_connect (G_OBJECT (docman), "document_removed",
+                         G_CALLBACK (on_document_removed), plugin);
+
        /* Watch documents */
        glade_plugin->priv->editor_watch_id =
                anjuta_plugin_add_watch (plugin, IANJUTA_DOCUMENT_MANAGER_CURRENT_DOCUMENT,
@@ -830,6 +928,8 @@ glade_plugin_instance_init (GObject *obj)
        priv->file_count = 0;
        priv->add_ticks = FALSE;
 
+       priv->associations = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_free);
+
        DEBUG_PRINT ("%s", "Intializing Glade plugin");
 }
 
@@ -968,9 +1068,22 @@ iwizard_iface_init(IAnjutaWizardIface *iface)
        iface->activate = iwizard_activate;
 }
 
+static void
+iglade_add_association (IAnjutaGlade *obj, gchar *master, gchar *slave, GError **err)
+{
+       glade_plugin_add_association (ANJUTA_PLUGIN_GLADE(obj), master, slave);
+}
+
+static void
+iglade_iface_init(IAnjutaGladeIface *iface)
+{
+       iface->add_association = iglade_add_association;
+}
+
 ANJUTA_PLUGIN_BEGIN (GladePlugin, glade_plugin);
 ANJUTA_PLUGIN_ADD_INTERFACE (ifile, IANJUTA_TYPE_FILE);
 ANJUTA_PLUGIN_ADD_INTERFACE (iwizard, IANJUTA_TYPE_WIZARD);
+ANJUTA_PLUGIN_ADD_INTERFACE (iglade, IANJUTA_TYPE_GLADE);
 ANJUTA_PLUGIN_END;
 
 ANJUTA_SIMPLE_PLUGIN (GladePlugin, glade_plugin);
diff --git a/plugins/language-support-cpp-java/plugin.c b/plugins/language-support-cpp-java/plugin.c
index 8f7227d..b771001 100644
--- a/plugins/language-support-cpp-java/plugin.c
+++ b/plugins/language-support-cpp-java/plugin.c
@@ -40,6 +40,7 @@
 #include <libanjuta/interfaces/ianjuta-symbol.h>
 #include <libanjuta/interfaces/ianjuta-symbol-manager.h>
 #include <libanjuta/interfaces/ianjuta-language.h>
+#include <libanjuta/interfaces/ianjuta-glade.h>
 
 #include "plugin.h"
 #include "cpp-packages.h"
@@ -71,6 +72,10 @@
 #define CHDR_BODY ";\n"
 #define CHDR_OFFSET 1
 
+/* Widgets marker */
+#define WIDGETS_DECLARATION_MARKER_PREFIX "/* ANJUTA: Widgets declaration for "
+#define WIDGETS_DECLARATION_MARKER_SUFFIX " - DO NOT REMOVE */"
+
 static gpointer parent_class;
 
 /* Glade support */
@@ -473,8 +478,10 @@ language_support_add_c_callback (CppJavaPlugin* lang_plugin,
 
     g_string_append (str, body);
 
+ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT(editor), NULL);
     ianjuta_editor_insert (editor, position,
                            str->str, -1, NULL);
+       ianjuta_document_end_undo_action (IANJUTA_DOCUMENT(editor), NULL);
 
     /* Code was inserted, we'll now check if we should add a prototype to the header */
     if (filetype == LS_FILE_C)
@@ -585,7 +592,8 @@ static gboolean insert_after_mark (IAnjutaEditor* editor, gchar* mark,
 static gchar*
 generate_widget_member_decl_marker (gchar* ui_filename)
 {
-    return g_strdup_printf ("/* ANJUTA: Widgets declaration for %s - DO NOT REMOVE */", ui_filename);
+    return g_strdup_printf (WIDGETS_DECLARATION_MARKER_PREFIX "%s" WIDGETS_DECLARATION_MARKER_SUFFIX,
+                                               ui_filename);
 }
 
 static gchar*
@@ -599,7 +607,7 @@ glade_widget_member_of_scope (gchar *widget_name, IAnjutaIterable *members)
 {
        do {
               IAnjutaSymbol *symbol = IANJUTA_SYMBOL (members);
-              const gchar *member_name = ianjuta_symbol_get_string (symbol, IANJUTA_SYMBOL_FIELD_NAME, NULL);
+              gchar *member_name = ianjuta_symbol_get_string (symbol, IANJUTA_SYMBOL_FIELD_NAME, NULL);
               /* Checks if member already exists... */
               if (g_strcmp0 (member_name, widget_name) == 0) {
                      return TRUE;
@@ -682,10 +690,15 @@ static void insert_member_decl_and_init (IAnjutaEditor* editor, gchar* widget_na
        status = anjuta_shell_get_status (ANJUTA_PLUGIN (lang_plugin)->shell, NULL);
 
        if (!glade_widget_already_in_scope (editor, widget_name, member_decl_marker, lang_plugin))
-       if (insert_after_mark (editor, member_decl_marker, member_decl, lang_plugin))
        {
-              insert_after_mark (editor, member_init_marker, member_init, lang_plugin);
-              anjuta_status_set (status, _("Code added for widget."));
+
+              ianjuta_document_begin_undo_action (IANJUTA_DOCUMENT(editor), NULL);
+              if (insert_after_mark (editor, member_decl_marker, member_decl, lang_plugin))
+              {
+                     insert_after_mark (editor, member_init_marker, member_init, lang_plugin);
+                     anjuta_status_set (status, _("Code added for widget."));
+              }
+              ianjuta_document_end_undo_action (IANJUTA_DOCUMENT(editor), NULL);
        }
 
        g_free (member_decl);
@@ -743,10 +756,67 @@ on_glade_callback_add (IAnjutaEditor* editor,
     g_free(mark);
 }
 
+static gchar*
+get_text_between (IAnjutaEditor *editor, gchar *prefix, gchar *suffix)
+{
+       IAnjutaEditorCell *search_start = IANJUTA_EDITOR_CELL (
+                                                                               
ianjuta_editor_get_start_position (editor, NULL));
+       IAnjutaEditorCell *search_end = IANJUTA_EDITOR_CELL (
+                                                                               
ianjuta_editor_get_end_position (editor, NULL));
+
+       IAnjutaEditorCell *result_start;
+       IAnjutaEditorCell *result_end = NULL;
+
+       IAnjutaEditorCell *prefix_end;
+       IAnjutaEditorCell *suffix_start;
+
+       ianjuta_editor_search_forward (IANJUTA_EDITOR_SEARCH (editor),
+                                                                  prefix, FALSE,
+                                                                  search_start, search_end,
+                                                                  &result_start,
+                                                                  &result_end, NULL);
+
+       if (!result_end)
+               return NULL;
+
+       prefix_end = result_end;
+
+       ianjuta_editor_search_forward (IANJUTA_EDITOR_SEARCH (editor),
+                                                                  suffix, FALSE,
+                                                                  result_end, search_end,
+                                                                  &result_start,
+                                                                  &result_end, NULL);
+
+       suffix_start = result_start;
+
+       if (!result_end)
+               return NULL;
+
+       return ianjuta_editor_get_text (editor, prefix_end, suffix_start, NULL);
+}
+
 /* Enable/Disable language-support */
 static void
 install_support (CppJavaPlugin *lang_plugin)
 {
+       /* Searching for association */
+       gchar *ui_filename = get_text_between (lang_plugin->current_editor,
+                                                                                  
WIDGETS_DECLARATION_MARKER_PREFIX,
+                                                                                  
WIDGETS_DECLARATION_MARKER_SUFFIX);
+
+       if (ui_filename)
+       {
+               GFile *file = ianjuta_file_get_file (IANJUTA_FILE (lang_plugin->current_editor), NULL);
+               gchar *filename = g_file_get_basename (file);
+
+               IAnjutaGlade *glade = anjuta_shell_get_interface (
+                                                                                 ANJUTA_PLUGIN 
(lang_plugin)->shell,
+                                                                                 IAnjutaGlade,
+                                                                                 NULL);
+
+               ianjuta_glade_add_association (glade, ui_filename, filename, NULL);
+       }
+
     IAnjutaLanguage* lang_manager =
         anjuta_shell_get_interface (ANJUTA_PLUGIN (lang_plugin)->shell,
                                     IAnjutaLanguage, NULL);


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