Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue737 treebrowser follow doc fix #796

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
98 changes: 60 additions & 38 deletions treebrowser/src/treebrowser.c
Expand Up @@ -141,14 +141,15 @@ PLUGIN_SET_TRANSLATABLE_INFO(
* ------------------ */

static void project_open_cb(G_GNUC_UNUSED GObject *obj, G_GNUC_UNUSED GKeyFile *config, G_GNUC_UNUSED gpointer data);
static void treebrowser_browse(gchar *directory, gpointer parent);
static void treebrowser_browse(const gchar *directory, gpointer parent);
static void treebrowser_bookmarks_set_state(void);
static void treebrowser_load_bookmarks(void);
static void treebrowser_tree_store_iter_clear_nodes(gpointer iter, gboolean delete_root);
static void treebrowser_rename_current(void);
static void on_menu_create_new_object(GtkMenuItem *menuitem, const gchar *type);
static void load_settings(void);
static gboolean save_settings(void);
static void treebrowser_track_current_cb(void);


/* ------------------
Expand All @@ -158,6 +159,7 @@ static gboolean save_settings(void);
PluginCallback plugin_callbacks[] =
{
{ "project-open", (GCallback) &project_open_cb, TRUE, NULL },
{ "document-activate", (GCallback) &treebrowser_track_current_cb, TRUE, NULL },
{ NULL, NULL, FALSE, NULL }
};

Expand Down Expand Up @@ -328,6 +330,21 @@ win32_check_hidden(const gchar *filename)
return TRUE;
return FALSE;
}

static gchar *
win32_strip_drive_letter(const gchar *path)
{
if (path != NULL && g_path_is_absolute(path))
{ /* HACK: Skip the drive letter on Windows, otherwise all further filename comparisons
* in this code will break. Though, without the drive letter multi drive support
* remains broken .*/
const gchar *path_relative = g_path_skip_root(path);
/* add leading backslash to make the path absolute again, i.e. absolute on the current drive */
gchar *new_path_current = g_build_filename(G_DIR_SEPARATOR_S, path_relative, NULL);
return new_path_current;
}
return g_strdup(path);
}
#endif

/* Returns: whether name should be hidden. */
Expand Down Expand Up @@ -362,31 +379,36 @@ check_hidden(const gchar *filename)
static gchar*
get_default_dir(void)
{
gchar *dir;
gchar *path = NULL;
GeanyProject *project = geany->app->project;
GeanyDocument *doc = document_get_current();

if (doc != NULL && doc->file_name != NULL && g_path_is_absolute(doc->file_name))
{
gchar *dir_name;
gchar *ret;

dir_name = g_path_get_dirname(doc->file_name);
ret = utils_get_locale_from_utf8(dir_name);
g_free (dir_name);

return ret;
path = utils_get_locale_from_utf8(dir_name);
g_free(dir_name);
}

if (project)
dir = project->base_path;
else
dir = geany->prefs->default_open_path;

if (! EMPTY(dir))
return utils_get_locale_from_utf8(dir);
if (EMPTY(path))
{
if (project)
path = project->base_path;
else
path = geany->prefs->default_open_path;
}
if (EMPTY(path))
{
path = g_get_current_dir();
}

return g_get_current_dir();
SETPTR(path, utils_get_locale_from_utf8(path));
#ifdef G_OS_WIN32
SETPTR(path, win32_strip_drive_letter(path));
#endif
return path;
}

static gchar *
Expand Down Expand Up @@ -434,7 +456,7 @@ treebrowser_chroot(const gchar *dir)
{
gchar *directory;

if (g_str_has_suffix(dir, G_DIR_SEPARATOR_S))
if (g_str_has_suffix(dir, G_DIR_SEPARATOR_S) && !utils_str_equal(dir, G_DIR_SEPARATOR_S))
directory = g_strndup(dir, strlen(dir)-1);
else
directory = g_strdup(dir);
Expand All @@ -459,7 +481,7 @@ treebrowser_chroot(const gchar *dir)
}

static void
treebrowser_browse(gchar *directory, gpointer parent)
treebrowser_browse(const gchar *directory, gpointer parent)
{
GtkTreeIter iter, iter_empty, *last_dir_iter = NULL;
gboolean is_dir;
Expand All @@ -470,8 +492,6 @@ treebrowser_browse(gchar *directory, gpointer parent)
gchar *fname;
gchar *uri;

directory = g_strconcat(directory, G_DIR_SEPARATOR_S, NULL);

has_parent = parent ? gtk_tree_store_iter_is_valid(treestore, parent) : FALSE;
if (has_parent)
{
Expand All @@ -498,9 +518,12 @@ treebrowser_browse(gchar *directory, gpointer parent)
foreach_slist_free(node, list)
{
fname = node->data;
uri = g_strconcat(directory, fname, NULL);
uri = g_build_filename(directory, fname, NULL);
is_dir = g_file_test (uri, G_FILE_TEST_IS_DIR);
utf8_name = utils_get_utf8_from_locale(fname);
#ifdef G_OS_WIN32
SETPTR(uri, win32_strip_drive_letter(uri));
#endif

if (!check_hidden(uri))
{
Expand Down Expand Up @@ -572,9 +595,6 @@ treebrowser_browse(gchar *directory, gpointer parent)
}
else
treebrowser_load_bookmarks();

g_free(directory);

}

static void
Expand Down Expand Up @@ -795,8 +815,8 @@ static gboolean
treebrowser_expand_to_path(gchar* root, gchar* find)
{
guint i = 0;
gboolean founded = FALSE, global_founded = FALSE;
gchar *new = NULL;
gboolean found = FALSE, global_found = FALSE;
gchar *new = NULL, *new_root = NULL;
gchar **root_segments = NULL, **find_segments = NULL;
guint find_segments_n = 0;

Expand All @@ -805,26 +825,26 @@ treebrowser_expand_to_path(gchar* root, gchar* find)

find_segments_n = g_strv_length(find_segments)-1;


for (i = 1; i<=find_segments_n; i++)
for (i = 0; i<=find_segments_n; i++)
{
new = g_strconcat(new ? new : "", G_DIR_SEPARATOR_S, find_segments[i], NULL);
new_root = g_build_filename(new ? new : G_DIR_SEPARATOR_S, find_segments[i], NULL);
SETPTR(new, new_root);

if (founded)
if (found)
{
if (treebrowser_search(new, NULL))
global_founded = TRUE;
global_found = TRUE;
}
else
if (utils_str_equal(root, new) == TRUE)
founded = TRUE;
found = TRUE;
}

g_free(new);
g_strfreev(root_segments);
g_strfreev(find_segments);

return global_founded;
return global_found;
}

static gboolean
Expand All @@ -839,14 +859,16 @@ treebrowser_track_current(void)
if (doc != NULL && doc->file_name != NULL && g_path_is_absolute(doc->file_name))
{
path_current = utils_get_locale_from_utf8(doc->file_name);

#ifdef G_OS_WIN32
SETPTR(path_current, win32_strip_drive_letter(path_current));
#endif
/*
* Checking if the document is in the expanded or collapsed files
*/
if (! treebrowser_search(path_current, NULL))
{
/*
* Else we have to chroting to the document`s nearles path
* Else we have to chrooting to the document`s nearles path
*/

froot = path_is_in_dir(addressbar_last_address, g_path_get_dirname(path_current));
Expand Down Expand Up @@ -2100,6 +2122,7 @@ void
plugin_init(GeanyData *data)
{
GeanyKeyGroup *key_group;
gchar *default_dir = NULL;

CONFIG_FILE = g_strconcat(geany->app->configdir, G_DIR_SEPARATOR_S, "plugins", G_DIR_SEPARATOR_S,
"treebrowser", G_DIR_SEPARATOR_S, "treebrowser.conf", NULL);
Expand All @@ -2108,7 +2131,9 @@ plugin_init(GeanyData *data)

load_settings();
create_sidebar();
treebrowser_chroot(get_default_dir());
default_dir = get_default_dir();
treebrowser_chroot(default_dir);
g_free(default_dir);

/* setup keybindings */
key_group = plugin_set_key_group(geany_plugin, "file_browser", KB_COUNT, NULL);
Expand All @@ -2127,9 +2152,6 @@ plugin_init(GeanyData *data)
0, 0, "rename_refresh", _("Refresh"), NULL);
keybindings_set_item(key_group, KB_TRACK_CURRENT, kb_activate,
0, 0, "track_current", _("Track Current"), NULL);

plugin_signal_connect(geany_plugin, NULL, "document-activate", TRUE,
(GCallback)&treebrowser_track_current_cb, NULL);
}

void
Expand Down