>From f236253a2fdc2012c881edfc0c63b421d1977cfc Mon Sep 17 00:00:00 2001
From: Matthew Brush <mbrush@codebrainz.ca>
Date: Fri, 28 Oct 2011 15:39:28 -0700
Subject: [PATCH 11/11] treebrowser: Cleanup warnings

Fix a few logic and memory errors.
---
 geany-plugins/treebrowser/src/treebrowser.c |   95 ++++++++++++++-------------
 1 files changed, 50 insertions(+), 45 deletions(-)

diff --git a/geany-plugins/treebrowser/src/treebrowser.c b/geany-plugins/treebrowser/src/treebrowser.c
index 3ef56eb..b1e5aa9 100644
--- a/geany-plugins/treebrowser/src/treebrowser.c
+++ b/geany-plugins/treebrowser/src/treebrowser.c
@@ -57,10 +57,10 @@ static gboolean 			flag_on_expand_refresh 		= FALSE;
  * ------------------ */
 
 static gchar 				*CONFIG_FILE 				= NULL;
-#ifdef G_OS_WIN32
-static gchar 				*CONFIG_OPEN_EXTERNAL_CMD 	= "nautilus '%d'";
+#ifndef G_OS_WIN32
+static const gchar			*CONFIG_OPEN_EXTERNAL_CMD 	= "nautilus '%d'";
 #else
-static gchar 				*CONFIG_OPEN_EXTERNAL_CMD 	= "explorer '%d'";
+static const gchar			*CONFIG_OPEN_EXTERNAL_CMD 	= "explorer '%d'";
 #endif
 static gboolean 			CONFIG_REVERSE_FILTER 		= FALSE;
 static gboolean 			CONFIG_ONE_CLICK_CHDOC 		= FALSE;
@@ -429,23 +429,35 @@ treebrowser_checkdir(gchar *directory)
 }
 
 static void
-treebrowser_chroot(gchar *directory)
+treebrowser_chroot(const gchar *directory)
 {
-	if (g_str_has_suffix(directory, G_DIR_SEPARATOR_S))
-		g_strlcpy(directory, directory, strlen(directory));
+	gchar *dir;
+	
+	dir = directory ? g_strdup(directory) : g_strdup("");
+	
+	while (g_str_has_suffix(dir, G_DIR_SEPARATOR_S))
+		dir[strlen(dir)-1] = '\0';
 
-	gtk_entry_set_text(GTK_ENTRY(addressbar), directory);
+	gtk_entry_set_text(GTK_ENTRY(addressbar), dir);
 
-	if (!directory || strlen(directory) == 0)
-		directory = G_DIR_SEPARATOR_S;
+	if (!dir)
+		dir = g_strdup(G_DIR_SEPARATOR_S);
+	else if (strlen(dir) == 0)
+	{
+		g_free(dir);
+		dir = g_strdup(G_DIR_SEPARATOR_S);
+	}
 
-	if (! treebrowser_checkdir(directory))
+	if (! treebrowser_checkdir(dir))
+	{
+		g_free(dir);
 		return;
+	}
 
 	treebrowser_bookmarks_set_state();
 
 	gtk_tree_store_clear(treestore);
-	setptr(addressbar_last_address, g_strdup(directory));
+	setptr(addressbar_last_address, dir);
 
 	treebrowser_browse(addressbar_last_address, NULL);
 	treebrowser_load_bookmarks();
@@ -584,7 +596,6 @@ treebrowser_load_bookmarks()
 	gchar 		**lines, **line;
 	GtkTreeIter iter;
 	gchar 		*pos;
-	gchar 		*name;
 	GdkPixbuf 	*icon = NULL;
 
 	if (! CONFIG_SHOW_BOOKMARKS)
@@ -625,12 +636,7 @@ treebrowser_load_bookmarks()
 			{
 				pos = g_utf8_strchr (*line, -1, ' ');
 				if (pos != NULL)
-				{
 					*pos = '\0';
-					name = pos + 1;
-				}
-				else
-					name = NULL;
 			}
 			path_full = g_filename_from_uri(*line, NULL, NULL);
 			if (path_full != NULL)
@@ -781,40 +787,34 @@ gtk_tree_store_iter_clear_nodes(gpointer iter, gboolean delete_root)
 }
 
 static gboolean
-treebrowser_expand_to_path(gchar* root, gchar* find)
+treebrowser_expand_to_path(const gchar* root, const gchar* find)
 {
-	int i = 0, j = 0;
-	gboolean founded = FALSE, global_founded = FALSE;
-	gchar *new = "";
-	gchar **root_segments = NULL, **find_segments = NULL;
-	guint root_segments_n = 0, find_segments_n = 0;
+	int i = 0;
+	gboolean found = FALSE, global_found = FALSE;
+	gchar *new = NULL, *tmp;
+	gchar **find_segments = NULL;
+	guint find_segments_n = 0;
 
-	root_segments = g_strsplit(root, G_DIR_SEPARATOR_S, 0);
 	find_segments = g_strsplit(find, G_DIR_SEPARATOR_S, 0);
-
-	root_segments_n = g_strv_length(root_segments)-1;
-	find_segments_n = g_strv_length(find_segments)-1;
+	find_segments_n = g_strv_length(find_segments)-1; /* why -1 ? */
 
 
 	for (i = 1; i<=find_segments_n; i++)
 	{
-		new = g_strconcat(new, G_DIR_SEPARATOR_S, find_segments[i], NULL);
+		tmp = new;
+		new = g_build_filename(tmp ? tmp : "", find_segments[i], NULL);
+		g_free(tmp);
 
-		if (founded)
-		{
-			if (treebrowser_search(new, NULL))
-				global_founded = TRUE;
-		}
+		if (found)
+			global_found = treebrowser_search(new, NULL);
 		else
-			if (utils_str_equal(root, new) == TRUE)
-				founded = TRUE;
+			found = utils_str_equal(root, new);
 	}
 
 	g_free(new);
-	g_strfreev(root_segments);
 	g_strfreev(find_segments);
 
-	return global_founded;
+	return global_found;
 }
 
 static gboolean
@@ -1175,13 +1175,17 @@ on_menu_show_bars(GtkMenuItem *menuitem, gpointer *user_data)
 }
 
 static GtkWidget*
-create_popup_menu(gchar *name, gchar *uri)
+create_popup_menu(const gchar *name, const gchar *in_uri)
 {
 	GtkWidget *item, *menu = gtk_menu_new();
 
-	gboolean is_exists 		= g_file_test(uri, G_FILE_TEST_EXISTS);
-	gboolean is_dir 		= is_exists ? g_file_test(uri, G_FILE_TEST_IS_DIR) : FALSE;
-	gboolean is_document 	= document_find_by_filename(uri) != NULL ? TRUE : FALSE;
+	gboolean is_exists 		= g_file_test(in_uri, G_FILE_TEST_EXISTS);
+	gboolean is_dir 		= g_file_test(in_uri, G_FILE_TEST_IS_DIR);
+	gboolean is_document 	= document_find_by_filename(in_uri) != NULL;
+	static gchar *uri		= NULL;
+	
+	g_free(uri);
+	uri = g_strdup(in_uri);
 
 	item = ui_image_menu_item_new(GTK_STOCK_GO_UP, _("Go up"));
 	gtk_container_add(GTK_CONTAINER(menu), item);
@@ -1219,11 +1223,13 @@ create_popup_menu(gchar *name, gchar *uri)
 
 	item = ui_image_menu_item_new(GTK_STOCK_ADD, _("Create new directory"));
 	gtk_container_add(GTK_CONTAINER(menu), item);
-	g_signal_connect(item, "activate", G_CALLBACK(on_menu_create_new_object), "directory");
+	g_signal_connect_data(item, "activate", G_CALLBACK(on_menu_create_new_object), 
+		g_strdup("directory"), (GClosureNotify)g_free, 0);
 
 	item = ui_image_menu_item_new(GTK_STOCK_NEW, _("Create new file"));
 	gtk_container_add(GTK_CONTAINER(menu), item);
-	g_signal_connect(item, "activate", G_CALLBACK(on_menu_create_new_object), "file");
+	g_signal_connect_data(item, "activate", G_CALLBACK(on_menu_create_new_object), 
+		g_strdup("file"), (GClosureNotify)g_free, 0);
 
 	item = ui_image_menu_item_new(GTK_STOCK_SAVE_AS, _("Rename"));
 	gtk_container_add(GTK_CONTAINER(menu), item);
@@ -1365,7 +1371,7 @@ on_treeview_mouseclick(GtkWidget *widget, GdkEventButton *event, GtkTreeSelectio
 {
 	GtkTreeIter 	iter;
 	GtkTreeModel 	*model;
-	gchar 			*name = "", *uri = "";
+	const gchar 	*name = "", *uri = "";
 
 	if (gtk_tree_selection_get_selected(selection, &model, &iter))
 		/* FIXME: name and uri should be freed, but they are passed to create_popup_menu()
@@ -1992,6 +1998,5 @@ plugin_cleanup()
 {
 	g_free(addressbar_last_address);
 	g_free(CONFIG_FILE);
-	g_free(CONFIG_OPEN_EXTERNAL_CMD);
 	gtk_widget_destroy(sidebar_vbox);
 }
-- 
1.7.6.3

