[gedit] Bug 623064 - Return const directory strings



commit 4fbfac45514c6670e9d74a24410045537ea51661
Author: Garrett Regier <alias301 gmail com>
Date:   Mon Jun 28 14:58:10 2010 -0700

    Bug 623064 - Return const directory strings

 gedit/gedit-app.c                  |   16 +-
 gedit/gedit-commands-help.c        |    3 +-
 gedit/gedit-dirs.c                 |  367 ++++++++++++++----------------------
 gedit/gedit-dirs.h                 |   26 ++-
 gedit/gedit-plugins-engine.c       |   12 +-
 gedit/gedit-style-scheme-manager.c |    3 +-
 gedit/gedit.c                      |   10 +-
 7 files changed, 179 insertions(+), 258 deletions(-)
---
diff --git a/gedit/gedit-app.c b/gedit/gedit-app.c
index 93fddec..b59dcfb 100644
--- a/gedit/gedit-app.c
+++ b/gedit/gedit-app.c
@@ -276,7 +276,7 @@ gedit_app_class_init (GeditAppClass *klass)
 static gboolean
 ensure_user_config_dir (void)
 {
-	gchar *config_dir;
+	const gchar *config_dir;
 	gboolean ret = TRUE;
 	gint res;
 
@@ -294,43 +294,39 @@ ensure_user_config_dir (void)
 		ret = FALSE;
 	}
 
-	g_free (config_dir);
-
 	return ret;
 }
 
 static void
 load_accels (void)
 {
-	gchar *filename;
+	const gchar *filename;
 
 	filename = gedit_dirs_get_user_accels_file ();
 	if (filename != NULL)
 	{
 		gedit_debug_message (DEBUG_APP, "Loading keybindings from %s\n", filename);
 		gtk_accel_map_load (filename);
-		g_free (filename);
 	}
 }
 
 static void
 save_accels (void)
 {
-	gchar *filename;
+	const gchar *filename;
 
 	filename = gedit_dirs_get_user_accels_file ();
 	if (filename != NULL)
 	{
 		gedit_debug_message (DEBUG_APP, "Saving keybindings in %s\n", filename);
 		gtk_accel_map_save (filename);
-		g_free (filename);
 	}
 }
 
 static gchar *
 get_page_setup_file (void)
 {
-	gchar *config_dir;
+	const gchar *config_dir;
 	gchar *setup = NULL;
 
 	config_dir = gedit_dirs_get_user_config_dir ();
@@ -340,7 +336,6 @@ get_page_setup_file (void)
 		setup = g_build_filename (config_dir,
 					  GEDIT_PAGE_SETUP_FILE,
 					  NULL);
-		g_free (config_dir);
 	}
 
 	return setup;
@@ -403,7 +398,7 @@ save_page_setup (GeditApp *app)
 static gchar *
 get_print_settings_file (void)
 {
-	gchar *config_dir;
+	const gchar *config_dir;
 	gchar *settings = NULL;
 
 	config_dir = gedit_dirs_get_user_config_dir ();
@@ -413,7 +408,6 @@ get_print_settings_file (void)
 		settings = g_build_filename (config_dir,
 					     GEDIT_PRINT_SETTINGS_FILE,
 					     NULL);
-		g_free (config_dir);
 	}
 
 	return settings;
diff --git a/gedit/gedit-commands-help.c b/gedit/gedit-commands-help.c
index 97cc25a..57a7673 100644
--- a/gedit/gedit-commands-help.c
+++ b/gedit/gedit-commands-help.c
@@ -87,7 +87,7 @@ _gedit_cmd_help_about (GtkAction   *action,
 		   "GNOME Desktop");
 
 	GdkPixbuf *logo;
-	gchar *data_dir;
+	const gchar *data_dir;
 	gchar *logo_file;
 
 	gedit_debug (DEBUG_COMMANDS);
@@ -97,7 +97,6 @@ _gedit_cmd_help_about (GtkAction   *action,
 				      "logo",
 				      "gedit-logo.png",
 				      NULL);
-	g_free (data_dir);
 	logo = gdk_pixbuf_new_from_file (logo_file, NULL);
 	g_free (logo_file);
 
diff --git a/gedit/gedit-dirs.c b/gedit/gedit-dirs.c
index 4fee92f..8a45357 100644
--- a/gedit/gedit-dirs.c
+++ b/gedit/gedit-dirs.c
@@ -30,94 +30,97 @@
 #include <ige-mac-bundle.h>
 #endif
 
-gchar *
-gedit_dirs_get_user_config_dir (void)
+static gchar *user_config_dir        = NULL;
+static gchar *user_cache_dir         = NULL;
+static gchar *user_plugins_dir       = NULL;
+static gchar *user_accels_file       = NULL;
+static gchar *gedit_data_dir         = NULL;
+static gchar *gedit_locale_dir       = NULL;
+static gchar *gedit_lib_dir          = NULL;
+static gchar *gedit_plugins_dir      = NULL;
+static gchar *gedit_plugins_data_dir = NULL;
+
+void
+gedit_dirs_init ()
 {
-	gchar *config_dir = NULL;
-
-#ifndef G_OS_WIN32
-	const gchar *envvar;
-	const gchar *home;
-
-	/* Support old libgnome env var */
-	envvar = g_getenv ("GNOME22_USER_DIR");
-	if (envvar != NULL)
-	{
-		config_dir = g_build_filename (envvar,
-					       "gedit",
-					       NULL);
-
-	}
-	else
-	{
-		home = g_get_home_dir ();
-
-		if (home != NULL)
-		{
-			config_dir = g_build_filename (home,
-						       ".gnome2",
-						       "gedit",
-						       NULL);
-		}
-	}
-#else
-	config_dir = g_build_filename (g_get_user_config_dir (),
-				       "gedit",
-				       NULL);
-#endif
-
-	return config_dir;
-}
+	const gchar *g_config_dir = NULL;
 
-gchar *
-gedit_dirs_get_user_cache_dir (void)
-{
-	const gchar *cache_dir;
+	g_config_dir = g_get_user_config_dir ();
 
-	cache_dir = g_get_user_cache_dir ();
+#ifdef G_OS_WIN32
+	gchar *win32_dir;
 
-	return g_build_filename (cache_dir,
-				 "gedit",
-				 NULL);
-}
+	win32_dir = g_win32_get_package_installation_directory_of_module (NULL);
 
-gchar *
-gedit_dirs_get_user_plugins_dir (void)
-{
-	gchar *config_dir;
-	gchar *plugin_dir;
 
-	config_dir = gedit_dirs_get_user_config_dir ();
+	user_config_dir = g_build_filename (g_config_dir,
+					    "gedit",
+					    NULL);
+	user_accels_file = g_build_filename (user_config_dir,
+					     "accels",
+					     "gedit",
+					     NULL);
+	gedit_data_dir = g_build_filename (win32_dir,
+					   "share",
+					   "gedit",
+					   NULL);
+	gedit_locale_dir = g_build_filename (win32_dir,
+					     "share",
+					     "locale",
+					     NULL);
+	gedit_lib_dir = g_build_filename (win32_dir,
+					  "lib",
+					  "gedit",
+					  NULL);
 
-	plugin_dir = g_build_filename (config_dir,
-				       "plugins",
-				       NULL);
-	g_free (config_dir);
-	
-	return plugin_dir;
-}
+	g_free (win32_dir);
+#else /* !G_OS_WIN32 */
+#ifdef OS_OSX
+	IgeMacBundle *bundle = ige_mac_bundle_get_default ();
 
-gchar *
-gedit_dirs_get_user_accels_file (void)
-{
-	gchar *accels = NULL;
+	if (ige_mac_bundle_get_is_app_bundle (bundle))
+	{
+		const gchar *bundle_data_dir = ige_mac_bundle_get_datadir (bundle);
+		const gchar *bundle_resource_dir = ige_mac_bundle_get_resourcesdir (bundle);
 
-#ifndef G_OS_WIN32
+		gedit_data_dir = g_build_filename (bundle_data_dir,
+						   "gedit",
+						   NULL);
+		gedit_locale_dir = g_strdup (ige_mac_bundle_get_localedir (bundle));
+		gedit_lib_dir = g_build_filename (bundle_resource_dir,
+						  "lib",
+						  "gedit",
+						  NULL);
+	}
+#endif /* !OS_OSX */
 	const gchar *envvar;
 	const gchar *home;
 
-	/* on linux accels are stored in .gnome2/accels
-	 * for historic reasons (backward compat with the
-	 * old libgnome that took care of saving them */
+	if (gedit_data_dir == NULL)
+	{
+		gedit_data_dir = g_build_filename (DATADIR,
+						   "gedit",
+						   NULL);
+		gedit_locale_dir = g_build_filename (DATADIR,
+						     "locale",
+						     NULL);
+		gedit_lib_dir = g_build_filename (LIBDIR,
+						  "gedit",
+						  NULL);
+	}
 
 	/* Support old libgnome env var */
 	envvar = g_getenv ("GNOME22_USER_DIR");
 	if (envvar != NULL)
 	{
-		accels = g_build_filename (envvar,
-					   "accels",
-					   "gedit",
-					   NULL);
+		user_config_dir = g_build_filename (envvar,
+						    "gedit",
+						    NULL);
+		user_accels_file = g_build_filename (envvar,
+						     "accels",
+						     "gedit",
+						     NULL);
+
 	}
 	else
 	{
@@ -125,194 +128,115 @@ gedit_dirs_get_user_accels_file (void)
 
 		if (home != NULL)
 		{
-			accels = g_build_filename (home,
-						   ".gnome2",
-						   "accels",
-						   "gedit",
-						   NULL);
+			user_config_dir = g_build_filename (home,
+							    ".gnome2",
+							    "gedit",
+							    NULL);
+
+			/* on linux accels are stored in .gnome2/accels
+			 * for historic reasons (backward compat with the
+			 * old libgnome that took care of saving them */
+			user_accels_file = g_build_filename (home,
+							     ".gnome2",
+							     "accels",
+							     "gedit",
+							     NULL);
 		}
 	}
-#else
-	{
-		gchar *config_dir = NULL;
+#endif /* !G_OS_WIN32 */
 
-		config_dir = gedit_dirs_get_user_config_dir ();
-		accels = g_build_filename (config_dir,
-					   "accels",
+	user_cache_dir = g_build_filename (g_config_dir,
 					   "gedit",
 					   NULL);
-
-		g_free (config_dir);
-	}
-#endif
-
-	return accels;
+	user_plugins_dir = g_build_filename (user_config_dir,
+					     "plugins",
+					     NULL);
+	gedit_plugins_dir = g_build_filename (gedit_lib_dir,
+					      "plugins",
+					      NULL);
+	gedit_plugins_data_dir = g_build_filename (gedit_data_dir,
+						   "plugins",
+						   NULL);
 }
 
-gchar *
-gedit_dirs_get_gedit_data_dir (void)
+void
+gedit_dirs_shutdown ()
 {
-	gchar *data_dir;
+	g_free (user_config_dir);
+	g_free (user_cache_dir);
+	g_free (user_plugins_dir);
+	g_free (user_accels_file);
+	g_free (gedit_data_dir);
+	g_free (gedit_locale_dir);
+	g_free (gedit_lib_dir);
+	g_free (gedit_plugins_dir);
+	g_free (gedit_plugins_data_dir);
+}
 
-#ifdef G_OS_WIN32
-	gchar *win32_dir;
-	
-	win32_dir = g_win32_get_package_installation_directory_of_module (NULL);
+const gchar *
+gedit_dirs_get_user_config_dir (void)
+{
+	return user_config_dir;
+}
 
-	data_dir = g_build_filename (win32_dir,
-				     "share",
-				     "gedit",
-				     NULL);
-	
-	g_free (win32_dir);
-#elif defined (OS_OSX)
-	IgeMacBundle *bundle = ige_mac_bundle_get_default ();
+const gchar *
+gedit_dirs_get_user_cache_dir (void)
+{
+	return user_cache_dir;
+}
 
-	if (ige_mac_bundle_get_is_app_bundle (bundle))
-	{
-		const gchar *bundle_data_dir = ige_mac_bundle_get_datadir (bundle);
+const gchar *
+gedit_dirs_get_user_plugins_dir (void)
+{
+	return user_plugins_dir;
+}
 
-		data_dir = g_build_filename (bundle_data_dir,
-					     "gedit",
-					     NULL);
-	}
-	else
-	{
-		data_dir = g_build_filename (DATADIR,
-					     "gedit",
-					     NULL);
-	}
-#else
-	data_dir = g_build_filename (DATADIR,
-	                             "gedit",
-	                             NULL);
-#endif
+const gchar *
+gedit_dirs_get_user_accels_file (void)
+{
+	return user_accels_file;
+}
 
-	return data_dir;
+const gchar *
+gedit_dirs_get_gedit_data_dir (void)
+{
+	return gedit_data_dir;
 }
 
-gchar *
+const gchar *
 gedit_dirs_get_gedit_locale_dir (void)
 {
-	gchar *locale_dir;
-
-#ifdef G_OS_WIN32
-	gchar *win32_dir;
-	
-	win32_dir = g_win32_get_package_installation_directory_of_module (NULL);
-
-	locale_dir = g_build_filename (win32_dir,
-				       "share",
-				       "locale",
-				       NULL);
-	
-	g_free (win32_dir);
-#elif defined (OS_OSX)
-	IgeMacBundle *bundle = ige_mac_bundle_get_default ();
-
-	if (ige_mac_bundle_get_is_app_bundle (bundle))
-	{
-		locale_dir = g_strdup (ige_mac_bundle_get_localedir (bundle));
-	}
-	else
-	{
-		locale_dir = g_build_filename (DATADIR,
-		                               "locale",
-		                               NULL);
-	}
-#else
-	locale_dir = g_build_filename (DATADIR,
-				       "locale",
-				       NULL);
-#endif
-
-	return locale_dir;
+	return gedit_locale_dir;
 }
 
-gchar *
+const gchar *
 gedit_dirs_get_gedit_lib_dir (void)
 {
-	gchar *lib_dir;
-
-#ifdef G_OS_WIN32
-	gchar *win32_dir;
-	
-	win32_dir = g_win32_get_package_installation_directory_of_module (NULL);
-
-	lib_dir = g_build_filename (win32_dir,
-				    "lib",
-				    "gedit",
-				    NULL);
-	
-	g_free (win32_dir);
-#elif defined (OS_OSX)
-	IgeMacBundle *bundle = ige_mac_bundle_get_default ();
-
-	if (ige_mac_bundle_get_is_app_bundle (bundle))
-	{
- 		const gchar *path = ige_mac_bundle_get_resourcesdir (bundle);
-		lib_dir = g_build_filename (path,
-	                            	"lib",
-	                            	"gedit",
-	                            	NULL);
-	}
-	else
-	{
-		lib_dir = g_build_filename (LIBDIR,
-					    "gedit",
-					    NULL);
-	}
-#else
-	lib_dir = g_build_filename (LIBDIR,
-				    "gedit",
-				    NULL);
-#endif
-
-	return lib_dir;
+	return gedit_lib_dir;
 }
 
-gchar *
+const gchar *
 gedit_dirs_get_gedit_plugins_dir (void)
 {
-	gchar *lib_dir;
-	gchar *plugin_dir;
-	
-	lib_dir = gedit_dirs_get_gedit_lib_dir ();
-	
-	plugin_dir = g_build_filename (lib_dir,
-				       "plugins",
-				       NULL);
-	g_free (lib_dir);
-	
-	return plugin_dir;
+	return gedit_plugins_dir;
 }
 
-gchar *
+const gchar *
 gedit_dirs_get_gedit_plugins_data_dir (void)
 {
-	gchar *data_dir;
-	gchar *plugin_data_dir;
-
-	data_dir = gedit_dirs_get_gedit_data_dir ();
-
-	plugin_data_dir = g_build_filename (data_dir,
-					    "plugins",
-					    NULL);
-	g_free (data_dir);
-
-	return plugin_data_dir;
+	return gedit_plugins_data_dir;
 }
 
-gchar *
+const gchar *
 gedit_dirs_get_binding_modules_dir (void)
 {
-	return gedit_dirs_get_gedit_lib_dir ();
+	return gedit_lib_dir;
 }
 
 gchar *
 gedit_dirs_get_ui_file (const gchar *file)
 {
-	gchar *datadir;
+	const gchar *datadir;
 	gchar *ui_file;
 
 	g_return_val_if_fail (file != NULL, NULL);
@@ -322,7 +246,6 @@ gedit_dirs_get_ui_file (const gchar *file)
 				    "ui",
 				    file,
 				    NULL);
-	g_free (datadir);
 	
 	return ui_file;
 }
diff --git a/gedit/gedit-dirs.h b/gedit/gedit-dirs.h
index e0be2f9..88296c0 100644
--- a/gedit/gedit-dirs.h
+++ b/gedit/gedit-dirs.h
@@ -28,25 +28,31 @@
 
 G_BEGIN_DECLS
 
-gchar		*gedit_dirs_get_user_config_dir		(void);
+/* This function must be called before starting gedit */
+void		 gedit_dirs_init			(void);
+/* This function must be called before exiting gedit */
+void		 gedit_dirs_shutdown 			(void);
 
-gchar		*gedit_dirs_get_user_cache_dir		(void);
 
-gchar		*gedit_dirs_get_user_plugins_dir	(void);
+const gchar	*gedit_dirs_get_user_config_dir		(void);
 
-gchar		*gedit_dirs_get_user_accels_file	(void);
+const gchar	*gedit_dirs_get_user_cache_dir		(void);
 
-gchar		*gedit_dirs_get_gedit_data_dir		(void);
+const gchar	*gedit_dirs_get_user_plugins_dir	(void);
 
-gchar		*gedit_dirs_get_gedit_locale_dir	(void);
+const gchar	*gedit_dirs_get_user_accels_file	(void);
 
-gchar		*gedit_dirs_get_gedit_lib_dir		(void);
+const gchar	*gedit_dirs_get_gedit_data_dir		(void);
 
-gchar		*gedit_dirs_get_gedit_plugins_dir	(void);
+const gchar	*gedit_dirs_get_gedit_locale_dir	(void);
 
-gchar		*gedit_dirs_get_gedit_plugins_data_dir	(void);
+const gchar	*gedit_dirs_get_gedit_lib_dir		(void);
 
-gchar		*gedit_dirs_get_binding_modules_dir	(void);
+const gchar	*gedit_dirs_get_gedit_plugins_dir	(void);
+
+const gchar	*gedit_dirs_get_gedit_plugins_data_dir	(void);
+
+const gchar	*gedit_dirs_get_binding_modules_dir	(void);
 
 gchar		*gedit_dirs_get_ui_file			(const gchar *file);
 
diff --git a/gedit/gedit-plugins-engine.c b/gedit/gedit-plugins-engine.c
index 657ef78..2db87fc 100644
--- a/gedit/gedit-plugins-engine.c
+++ b/gedit/gedit-plugins-engine.c
@@ -132,7 +132,7 @@ gedit_plugins_engine_class_init (GeditPluginsEngineClass *klass)
 static void
 require_private_typelib (void)
 {
-	gchar *lib_dir;
+	const gchar *lib_dir;
 	gchar *filename;
 	GMappedFile *mfile;
 	GTypelib *typelib;
@@ -147,7 +147,6 @@ require_private_typelib (void)
 	mfile = g_mapped_file_new (filename, FALSE, NULL);
 
 	g_free (filename);
-	g_free (lib_dir);
 
 	if (mfile == NULL)
 	{
@@ -173,8 +172,8 @@ require_private_typelib (void)
 GeditPluginsEngine *
 gedit_plugins_engine_get_default (void)
 {
-	gchar *modules_dir;
-	gchar **search_paths;
+	const gchar *modules_dir;
+	const gchar **search_paths;
 
 	if (default_engine != NULL)
 	{
@@ -189,7 +188,7 @@ gedit_plugins_engine_get_default (void)
 	require_private_typelib ();
 
 	modules_dir = gedit_dirs_get_binding_modules_dir ();
-	search_paths = g_new (gchar *, 5);
+	search_paths = g_new (const gchar *, 5);
 	/* Add the user plugins dir in ~ */
 	search_paths[0] = gedit_dirs_get_user_plugins_dir ();
 	search_paths[1] = gedit_dirs_get_user_plugins_dir ();
@@ -205,8 +204,7 @@ gedit_plugins_engine_get_default (void)
 							     "search-paths", search_paths,
 							     NULL));
 
-	g_strfreev (search_paths);
-	g_free (modules_dir);
+	g_free (search_paths);
 
 	g_object_add_weak_pointer (G_OBJECT (default_engine),
 				   (gpointer) &default_engine);
diff --git a/gedit/gedit-style-scheme-manager.c b/gedit/gedit-style-scheme-manager.c
index 299b116..e45c5d4 100644
--- a/gedit/gedit-style-scheme-manager.c
+++ b/gedit/gedit-style-scheme-manager.c
@@ -42,7 +42,7 @@ static GtkSourceStyleSchemeManager *style_scheme_manager = NULL;
 static gchar *
 get_gedit_styles_path (void)
 {
-	gchar *config_dir;
+	const gchar *config_dir;
 	gchar *dir = NULL;
 
 	config_dir = gedit_dirs_get_user_config_dir ();
@@ -52,7 +52,6 @@ get_gedit_styles_path (void)
 		dir = g_build_filename (config_dir,
 					"styles",
 					NULL);
-		g_free (config_dir);
 	}
 	
 	return dir;
diff --git a/gedit/gedit.c b/gedit/gedit.c
index a72acb8..6c9b9cc 100644
--- a/gedit/gedit.c
+++ b/gedit/gedit.c
@@ -163,14 +163,13 @@ gedit_main (gboolean service)
 	GeditPluginsEngine *engine;
 	GeditApp *app;
 	gboolean restored = FALSE;
-	gchar *dir;
+	const gchar *dir;
 	gchar *icon_dir;
 
 	gedit_debug_message (DEBUG_APP, "Set icon");
 
 	dir = gedit_dirs_get_gedit_data_dir ();
 	icon_dir = g_build_filename (dir, "icons", NULL);
-	g_free (dir);
 
 	gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (), icon_dir);
 	g_free (icon_dir);
@@ -204,6 +203,8 @@ gedit_main (gboolean service)
 	g_object_unref (engine);
 	g_object_unref (app);
 
+	gedit_dirs_shutdown ();
+
 #ifndef ENABLE_GVFS_METADATA
 	gedit_metadata_manager_shutdown ();
 #endif
@@ -212,7 +213,7 @@ gedit_main (gboolean service)
 int
 main (int argc, char *argv[])
 {
-	gchar *dir;
+	const gchar *dir;
 	GeditCommandLine *command_line;
 	gboolean ret;
 	GeditDBus *dbus;
@@ -234,11 +235,12 @@ main (int argc, char *argv[])
 
 	dir = gedit_dirs_get_gedit_locale_dir ();
 	bindtextdomain (GETTEXT_PACKAGE, dir);
-	g_free (dir);
 
 	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 	textdomain (GETTEXT_PACKAGE);
 
+	gedit_dirs_init ();
+
 	/* Parse command line arguments */
 	command_line = gedit_command_line_get_default ();
 



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