[anjuta] class-gen: bgo #654124 - Vala: add class wizard creates file twice



commit d989297c6b6122812f4716ab6036b3303fc7d255
Author: SÃbastien Granjoux <seb sfo free fr>
Date:   Thu Jul 7 23:04:48 2011 +0200

    class-gen: bgo #654124 - Vala: add class wizard creates file twice

 plugins/class-gen/generator.c |   27 ++++++++++++++++++------
 plugins/class-gen/plugin.c    |   44 ++++++++++++++++++----------------------
 plugins/class-gen/window.c    |   26 ++++++++++++------------
 3 files changed, 53 insertions(+), 44 deletions(-)
---
diff --git a/plugins/class-gen/generator.c b/plugins/class-gen/generator.c
index 27a2ab3..3e9ee52 100644
--- a/plugins/class-gen/generator.c
+++ b/plugins/class-gen/generator.c
@@ -99,8 +99,8 @@ cg_generator_autogen_source_func (NPWAutogen *autogen,
 	priv = CG_GENERATOR_PRIVATE (generator);
 
 	success = TRUE;
-	if (g_file_test (priv->header_destination,
-	                 G_FILE_TEST_IS_REGULAR) == FALSE)
+	if ((priv->header_destination != NULL) && (g_file_test (priv->header_destination,
+	                 G_FILE_TEST_IS_REGULAR) == FALSE))
 	{
 		if (g_file_test (priv->source_destination,
 		                 G_FILE_TEST_IS_REGULAR) == TRUE)
@@ -218,8 +218,9 @@ cg_generator_set_property (GObject *object,
 	{
 	case PROP_HEADER_TEMPLATE:
 		g_free (priv->header_template);
-		priv->header_template =
-			cg_generator_make_absolute (g_value_get_string (value));
+		priv->header_template = g_value_get_string (value) != NULL ?
+			cg_generator_make_absolute (g_value_get_string (value)) :
+			NULL;
 		break;
 	case PROP_SOURCE_TEMPLATE:
 		g_free (priv->source_template);
@@ -228,8 +229,9 @@ cg_generator_set_property (GObject *object,
 		break;
 	case PROP_HEADER_DESTINATION:
 		g_free (priv->header_destination);
-		priv->header_destination =
-			cg_generator_make_absolute (g_value_get_string (value));
+		priv->header_destination = g_value_get_string (value) != NULL ?
+			cg_generator_make_absolute (g_value_get_string (value)) :
+			NULL;
 		break;
 	case PROP_SOURCE_DESTINATION:
 		g_free (priv->source_destination);
@@ -404,7 +406,7 @@ cg_generator_run (CgGenerator *generator,
 
 		return FALSE;
 	}
-	else
+	else if ((priv->header_destination != NULL) && (priv->header_template != NULL))
 	{
 		npw_autogen_set_input_file (priv->autogen, priv->header_template,
 		                            NULL, NULL); /*"[+", "+]");*/
@@ -414,6 +416,17 @@ cg_generator_run (CgGenerator *generator,
 		                            cg_generator_autogen_header_func,
 		                            generator, error);
 	}
+	else
+	{
+		/* No header file, generate source file */
+		npw_autogen_set_input_file (priv->autogen, priv->source_template,
+		                            NULL, NULL); /*"[+", "+]");*/
+		npw_autogen_set_output_file (priv->autogen, priv->source_destination);
+
+		return npw_autogen_execute (priv->autogen,
+	                              cg_generator_autogen_source_func,
+	                              generator, error);
+	}
 }
 
 const gchar *
diff --git a/plugins/class-gen/plugin.c b/plugins/class-gen/plugin.c
index 1698bab..02fb618 100644
--- a/plugins/class-gen/plugin.c
+++ b/plugins/class-gen/plugin.c
@@ -176,7 +176,7 @@ cg_plugin_add_to_project (AnjutaClassGenPlugin *plugin,
                           gchar **new_source_file)
 {
 	IAnjutaProjectManager *manager;
-	GList *filenames;
+	GList *filenames = NULL;
 	GList *added_files;
 	GFile *file;
 	gboolean result;
@@ -187,7 +187,7 @@ cg_plugin_add_to_project (AnjutaClassGenPlugin *plugin,
 	if (manager == NULL) 
 		return FALSE;
 
-	filenames = g_list_append (NULL, g_path_get_basename (header_file));
+	if (header_file != NULL) filenames = g_list_append (filenames, g_path_get_basename (header_file));
 	filenames = g_list_append (filenames, g_path_get_basename (source_file));
 	file = g_file_new_for_path (plugin->top_dir);
 	added_files = ianjuta_project_manager_add_sources (manager, filenames,
@@ -196,16 +196,17 @@ cg_plugin_add_to_project (AnjutaClassGenPlugin *plugin,
 	g_list_foreach (filenames, (GFunc)g_free, NULL);
 	g_list_free (filenames);
 
-	result = g_list_length (added_files) == 2;
+	result = g_list_length (added_files) == ((header_file == NULL) ? 1 : 2);
 	if (result)
 	{
 		/*
 		 * Check if we're dealing with a programming language not having header
 		 * files.
 		 */
-		if (strcmp (header_file, source_file) == 0)
+		if (header_file == NULL)
 		{
-			*new_source_file = g_file_get_path((GFile *)g_list_next (added_files)->data);
+			*new_header_file = NULL;
+			*new_source_file = g_file_get_path((GFile *)added_files->data);
 		}
 		else
 		{
@@ -216,7 +217,7 @@ cg_plugin_add_to_project (AnjutaClassGenPlugin *plugin,
 
 	g_list_foreach (added_files, (GFunc)g_object_unref, NULL);
 	g_list_free (added_files);
-
+	
 	return result;
 }
 
@@ -233,7 +234,7 @@ cg_plugin_add_to_repository (AnjutaClassGenPlugin *plugin,
 	{
 		GList* files = NULL;
 		AnjutaAsyncNotify* notify = anjuta_async_notify_new ();
-		files = g_list_append (files, header_file);
+		if (header_file != NULL) files = g_list_append (files, header_file);
 		files = g_list_append (files, source_file);
 		ianjuta_vcs_add (vcs, files, notify, NULL);
 		g_list_free (files);
@@ -301,7 +302,6 @@ cg_plugin_generator_created_cb (CgGenerator *generator,
 	const gchar *header_file;
 	const gchar *source_file;
 	IAnjutaFileLoader *loader;
-        gboolean header_is_source = FALSE;
 
 	plugin = (AnjutaClassGenPlugin *) user_data;
 	header_file = cg_generator_get_header_destination (generator);
@@ -310,27 +310,23 @@ cg_plugin_generator_created_cb (CgGenerator *generator,
 	loader = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
 	                                     IAnjutaFileLoader, NULL);
 	
+	if (cg_window_get_add_to_project (plugin->window))
+	{
+		GFile* header = NULL;
+		GFile* source = g_file_new_for_path (source_file);
+		IAnjutaProjectManager *manager;
         /*
          * Check if we're workign with a programming language that
          * doesn't need header files.  If yes, don't create two tabs,
          * as the header and source will be the same file.
          */
-        if (g_strcmp0 (header_file, source_file) == 0)
-        {
-                header_is_source = TRUE;
-        }
-
-	if (cg_window_get_add_to_project (plugin->window))
-	{
-		GFile* header = g_file_new_for_path (header_file);
-		GFile* source = g_file_new_for_path (source_file);
-		IAnjutaProjectManager *manager;
-		if (header_is_source == TRUE)
+		if (header_file == NULL)
 		{
 			ianjuta_file_loader_load (loader, source, FALSE, NULL);
 		}
 		else
 		{
+			header = g_file_new_for_path (header_file);
 			ianjuta_file_loader_load (loader, header, FALSE, NULL);
 			ianjuta_file_loader_load (loader, source, FALSE, NULL);
 		}
@@ -343,7 +339,7 @@ cg_plugin_generator_created_cb (CgGenerator *generator,
 		manager = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell, IAnjutaProjectManager, NULL);
 		if (manager)
 		{
-			if (header_is_source == TRUE)
+			if (header == NULL)
 			{
 			    g_signal_emit_by_name (G_OBJECT (manager), "element_added", source);
 			}
@@ -355,12 +351,12 @@ cg_plugin_generator_created_cb (CgGenerator *generator,
 
 		}
 
-		g_object_unref (header);
+		if (header != NULL) g_object_unref (header);
 		g_object_unref (source);
 	}
 	else
 	{
-		if (header_is_source == TRUE)
+		if (header_file == NULL)
 		{
 			/* We do not just use ianjuta_file_leader_load here to ensure that
 			 * the new documents are flagged as changed and no path is
@@ -408,8 +404,8 @@ cg_plugin_window_response_cb (G_GNUC_UNUSED GtkDialog *dialog,
 		}
 		else
 		{
-			header_file = g_build_filename (g_get_tmp_dir (),
-				cg_window_get_header_file (plugin->window), NULL);
+			header_file = cg_window_get_header_file (plugin->window) != NULL ? g_build_filename (g_get_tmp_dir (),
+				cg_window_get_header_file (plugin->window), NULL) : NULL;
 			source_file = g_build_filename (g_get_tmp_dir (),
 				cg_window_get_source_file (plugin->window), NULL);
 
diff --git a/plugins/class-gen/window.c b/plugins/class-gen/window.c
index b8f89b5..fe4beec 100644
--- a/plugins/class-gen/window.c
+++ b/plugins/class-gen/window.c
@@ -28,19 +28,19 @@
 
 #include <ctype.h>
 
-#define BUILDER_FILE PACKAGE_DATA_DIR"/glade/anjuta-class-gen-plugin.ui"
+#define BUILDER_FILE PACKAGE_DATA_DIR "/glade/anjuta-class-gen-plugin.ui"
 
-#define CC_HEADER_TEMPLATE PACKAGE_DATA_DIR"/class-templates/cc-header.tpl"
-#define CC_SOURCE_TEMPLATE PACKAGE_DATA_DIR"/class-templates/cc-source.tpl"
+#define CC_HEADER_TEMPLATE PACKAGE_DATA_DIR "/class-templates/cc-header.tpl"
+#define CC_SOURCE_TEMPLATE PACKAGE_DATA_DIR "/class-templates/cc-source.tpl"
 
-#define GO_HEADER_TEMPLATE PACKAGE_DATA_DIR"/class-templates/go-header.tpl"
-#define GO_SOURCE_TEMPLATE PACKAGE_DATA_DIR"/class-templates/go-source.tpl"
+#define GO_HEADER_TEMPLATE PACKAGE_DATA_DIR "/class-templates/go-header.tpl"
+#define GO_SOURCE_TEMPLATE PACKAGE_DATA_DIR "/class-templates/go-source.tpl"
 
-#define PY_SOURCE_TEMPLATE PACKAGE_DATA_DIR"/class-templates/py-source.tpl"
+#define PY_SOURCE_TEMPLATE PACKAGE_DATA_DIR "/class-templates/py-source.tpl"
 
-#define JS_SOURCE_TEMPLATE PACKAGE_DATA_DIR"/class-templates/js-source.tpl"
+#define JS_SOURCE_TEMPLATE PACKAGE_DATA_DIR "/class-templates/js-source.tpl"
 
-#define VALA_SOURCE_TEMPLATE PACKAGE_DATA_DIR"/class-templates/vala-source.tpl"
+#define VALA_SOURCE_TEMPLATE PACKAGE_DATA_DIR "/class-templates/vala-source.tpl"
 
 typedef struct _CgWindowPrivate CgWindowPrivate;
 struct _CgWindowPrivate
@@ -1458,7 +1458,7 @@ cg_window_create_value_heap (CgWindow *window)
 	npw_value_set_value(value, LICENSES[license_index],
 	                         NPW_VALID_VALUE);
 
-	header_file = g_path_get_basename (cg_window_get_header_file (window));
+	header_file = cg_window_get_header_file (window) != NULL ? g_path_get_basename (cg_window_get_header_file (window)) : NULL;
 	source_file = g_path_get_basename (cg_window_get_source_file (window));
 	
 	value = npw_value_heap_find_value (values, "HeaderFile");
@@ -1492,11 +1492,11 @@ cg_window_get_header_template (CgWindow *window)
 	case 1:
 		return GO_HEADER_TEMPLATE;
 	case 2: /* Python */
-		return PY_SOURCE_TEMPLATE;
+		return NULL;
 	case 3: /* JavaScript */
-		return JS_SOURCE_TEMPLATE;
+		return NULL;
 	case 4: /* Vala */
-		return VALA_SOURCE_TEMPLATE;
+		return NULL;
 	default:
 		g_assert_not_reached ();
 		return NULL;
@@ -1513,7 +1513,7 @@ cg_window_get_header_file(CgWindow *window)
 	entry = GTK_ENTRY (gtk_builder_get_object (priv->bxml, "header_file"));
 	
 	g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
-	return gtk_entry_get_text (entry);
+	return gtk_widget_get_visible (GTK_WIDGET (entry)) == TRUE ? gtk_entry_get_text (entry) : NULL;
 }
 
 const gchar *



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