forked from geany/geany-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
treebrowser.c
2165 lines (1811 loc) · 66.3 KB
/
treebrowser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* treebrowser.c - v0.20
*
* Copyright 2010 Adrian Dimitrov <dimitrov.adrian@gmail.com>
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <gdk/gdkkeysyms.h>
#include "geany.h"
#include "geanyplugin.h"
#include <gio/gio.h>
#ifdef G_OS_WIN32
# include <windows.h>
#endif
/* These items are set by Geany before plugin_init() is called. */
GeanyPlugin *geany_plugin;
GeanyData *geany_data;
static gint page_number = 0;
static GtkTreeStore *treestore;
static GtkWidget *treeview;
static GtkWidget *sidebar_vbox;
static GtkWidget *sidebar_vbox_bars;
static GtkWidget *filter;
static GtkWidget *addressbar;
static gchar *addressbar_last_address = NULL;
static GtkTreeIter bookmarks_iter;
static gboolean bookmarks_expanded = FALSE;
static GtkTreeViewColumn *treeview_column_text;
static GtkCellRenderer *render_icon, *render_text;
/* ------------------
* FLAGS
* ------------------ */
static gboolean flag_on_expand_refresh = FALSE;
/* ------------------
* CONFIG VARS
* ------------------ */
#ifndef G_OS_WIN32
# define CONFIG_OPEN_EXTERNAL_CMD_DEFAULT "nautilus '%d'"
# define CONFIG_OPEN_TERMINAL_DEFAULT "xterm"
#else
# define CONFIG_OPEN_EXTERNAL_CMD_DEFAULT "explorer '%d'"
# define CONFIG_OPEN_TERMINAL_DEFAULT "cmd"
#endif
static gchar *CONFIG_FILE = NULL;
static gchar *CONFIG_OPEN_EXTERNAL_CMD = NULL;
static gchar *CONFIG_OPEN_TERMINAL = NULL;
static gboolean CONFIG_REVERSE_FILTER = FALSE;
static gboolean CONFIG_ONE_CLICK_CHDOC = FALSE;
static gboolean CONFIG_SHOW_HIDDEN_FILES = FALSE;
static gboolean CONFIG_HIDE_OBJECT_FILES = FALSE;
static gint CONFIG_SHOW_BARS = 1;
static gboolean CONFIG_CHROOT_ON_DCLICK = FALSE;
static gboolean CONFIG_FOLLOW_CURRENT_DOC = TRUE;
static gboolean CONFIG_ON_DELETE_CLOSE_FILE = TRUE;
static gboolean CONFIG_ON_OPEN_FOCUS_EDITOR = FALSE;
static gboolean CONFIG_SHOW_TREE_LINES = TRUE;
static gboolean CONFIG_SHOW_BOOKMARKS = FALSE;
static gint CONFIG_SHOW_ICONS = 2;
static gboolean CONFIG_OPEN_NEW_FILES = TRUE;
/* ------------------
* TREEVIEW STRUCT
* ------------------ */
enum
{
TREEBROWSER_COLUMNC = 4,
TREEBROWSER_COLUMN_ICON = 0,
TREEBROWSER_COLUMN_NAME = 1,
TREEBROWSER_COLUMN_URI = 2,
TREEBROWSER_COLUMN_FLAG = 3,
TREEBROWSER_RENDER_ICON = 0,
TREEBROWSER_RENDER_TEXT = 1,
TREEBROWSER_FLAGS_SEPARATOR = -1
};
/* Keybinding(s) */
enum
{
KB_FOCUS_FILE_LIST,
KB_FOCUS_PATH_ENTRY,
KB_RENAME_OBJECT,
KB_CREATE_FILE,
KB_CREATE_DIR,
KB_REFRESH,
KB_TRACK_CURRENT,
KB_COUNT
};
/* ------------------
* PLUGIN INFO
* ------------------ */
PLUGIN_VERSION_CHECK(224)
PLUGIN_SET_TRANSLATABLE_INFO(
LOCALEDIR,
GETTEXT_PACKAGE,
_("TreeBrowser"),
_("This plugin adds a tree browser to Geany, allowing the user to "
"browse files using a tree view of the directory being browsed."),
"1.32" ,
"Adrian Dimitrov (dimitrov.adrian@gmail.com)")
/* ------------------
* PREDEFINES
* ------------------ */
#define foreach_slist_free(node, list) for (node = list, list = NULL; g_slist_free_1(list), node != NULL; list = node, node = node->next)
/* ------------------
* PROTOTYPES
* ------------------ */
static void project_open_cb(G_GNUC_UNUSED GObject *obj, G_GNUC_UNUSED GKeyFile *config, G_GNUC_UNUSED gpointer data);
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);
/* ------------------
* PLUGIN CALLBACKS
* ------------------ */
PluginCallback plugin_callbacks[] =
{
{ "project-open", (GCallback) &project_open_cb, TRUE, NULL },
{ "document-activate", (GCallback) &treebrowser_track_current_cb, TRUE, NULL },
{ NULL, NULL, FALSE, NULL }
};
/* ------------------
* TREEBROWSER CORE FUNCTIONS
* ------------------ */
static gboolean
tree_view_row_expanded_iter(GtkTreeView *tree_view, GtkTreeIter *iter)
{
GtkTreePath *tree_path;
gboolean expanded;
tree_path = gtk_tree_model_get_path(gtk_tree_view_get_model(tree_view), iter);
expanded = gtk_tree_view_row_expanded(tree_view, tree_path);
gtk_tree_path_free(tree_path);
return expanded;
}
static GdkPixbuf *
utils_pixbuf_from_stock(const gchar *stock_id)
{
GtkIconSet *icon_set;
icon_set = gtk_icon_factory_lookup_default(stock_id);
if (icon_set)
return gtk_icon_set_render_icon(icon_set, gtk_widget_get_default_style(),
gtk_widget_get_default_direction(),
GTK_STATE_NORMAL, GTK_ICON_SIZE_MENU, NULL, NULL);
return NULL;
}
static GdkPixbuf *
utils_pixbuf_from_path(gchar *path)
{
GIcon *icon;
GdkPixbuf *ret = NULL;
GtkIconInfo *info;
gchar *ctype;
gint width;
ctype = g_content_type_guess(path, NULL, 0, NULL);
icon = g_content_type_get_icon(ctype);
g_free(ctype);
if (icon != NULL)
{
gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &width, NULL);
info = gtk_icon_theme_lookup_by_gicon(gtk_icon_theme_get_default(), icon, width, GTK_ICON_LOOKUP_USE_BUILTIN);
g_object_unref(icon);
if (!info)
{
icon = g_themed_icon_new("text-x-generic");
if (icon != NULL)
{
info = gtk_icon_theme_lookup_by_gicon(gtk_icon_theme_get_default(), icon, width, GTK_ICON_LOOKUP_USE_BUILTIN);
g_object_unref(icon);
}
}
if (!info)
return NULL;
ret = gtk_icon_info_load_icon (info, NULL);
gtk_icon_info_free(info);
}
return ret;
}
/* result must be freed */
static gchar*
path_is_in_dir(gchar* src, gchar* find)
{
guint i = 0;
gchar *diffed_path = NULL, *tmp = NULL;
gchar **src_segments = NULL, **find_segments = NULL;
guint src_segments_n = 0, find_segments_n = 0, n = 0;
src_segments = g_strsplit(src, G_DIR_SEPARATOR_S, 0);
find_segments = g_strsplit(find, G_DIR_SEPARATOR_S, 0);
src_segments_n = g_strv_length(src_segments);
find_segments_n = g_strv_length(find_segments);
n = src_segments_n;
if (find_segments_n < n)
n = find_segments_n;
for(i = 1; i<n; i++)
if (utils_str_equal(find_segments[i], src_segments[i]) != TRUE)
break;
else
{
tmp = g_strconcat(diffed_path == NULL ? "" : diffed_path,
G_DIR_SEPARATOR_S, find_segments[i], NULL);
g_free(diffed_path);
diffed_path = tmp;
}
g_strfreev(src_segments);
g_strfreev(find_segments);
return diffed_path;
}
/* Return: FALSE - if file is filtered and not shown, and TRUE - if file isn`t filtered, and have to be shown */
static gboolean
check_filtered(const gchar *base_name)
{
gchar **filters;
guint i;
gboolean temporary_reverse = FALSE;
const gchar *exts[] = {".o", ".obj", ".so", ".dll", ".a", ".lib", ".la", ".lo", ".pyc"};
guint exts_len;
const gchar *ext;
gboolean filtered;
if (CONFIG_HIDE_OBJECT_FILES)
{
exts_len = G_N_ELEMENTS(exts);
for (i = 0; i < exts_len; i++)
{
ext = exts[i];
if (g_str_has_suffix(base_name, ext))
return FALSE;
}
}
if (EMPTY(gtk_entry_get_text(GTK_ENTRY(filter))))
return TRUE;
filters = g_strsplit(gtk_entry_get_text(GTK_ENTRY(filter)), ";", 0);
if (utils_str_equal(filters[0], "!") == TRUE)
{
temporary_reverse = TRUE;
i = 1;
}
else
i = 0;
filtered = CONFIG_REVERSE_FILTER || temporary_reverse ? TRUE : FALSE;
for (; filters[i]; i++)
{
if (utils_str_equal(base_name, "*") || g_pattern_match_simple(filters[i], base_name))
{
filtered = CONFIG_REVERSE_FILTER || temporary_reverse ? FALSE : TRUE;
break;
}
}
g_strfreev(filters);
return filtered;
}
#ifdef G_OS_WIN32
static gboolean
win32_check_hidden(const gchar *filename)
{
DWORD attrs;
static wchar_t w_filename[MAX_PATH];
MultiByteToWideChar(CP_UTF8, 0, filename, -1, w_filename, sizeof(w_filename));
attrs = GetFileAttributesW(w_filename);
if (attrs != INVALID_FILE_ATTRIBUTES && attrs & FILE_ATTRIBUTE_HIDDEN)
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. */
static gboolean
check_hidden(const gchar *filename)
{
gsize len;
const gchar *base_name = NULL;
base_name = g_path_get_basename(filename);
if (EMPTY(base_name))
return FALSE;
if (CONFIG_SHOW_HIDDEN_FILES)
return FALSE;
#ifdef G_OS_WIN32
if (win32_check_hidden(filename))
return TRUE;
#else
if (base_name[0] == '.')
return TRUE;
#endif
len = strlen(base_name);
if (base_name[len - 1] == '~')
return TRUE;
return FALSE;
}
static gchar*
get_default_dir(void)
{
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;
dir_name = g_path_get_dirname(doc->file_name);
path = utils_get_locale_from_utf8(dir_name);
g_free(dir_name);
}
if (EMPTY(path))
{
if (project)
path = project->base_path;
else
path = geany->prefs->default_open_path;
}
if (EMPTY(path))
{
path = 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 *
get_terminal(void)
{
gchar *terminal;
#ifdef G_OS_WIN32
terminal = g_strdup("cmd");
#else
terminal = g_strdup(CONFIG_OPEN_TERMINAL);
#endif
return terminal;
}
static gboolean
treebrowser_checkdir(gchar *directory)
{
gboolean is_dir;
static const GdkColor red = {0, 0xffff, 0x6666, 0x6666};
static const GdkColor white = {0, 0xffff, 0xffff, 0xffff};
static gboolean old_value = TRUE;
is_dir = g_file_test(directory, G_FILE_TEST_IS_DIR);
if (old_value != is_dir)
{
gtk_widget_modify_base(GTK_WIDGET(addressbar), GTK_STATE_NORMAL, is_dir ? NULL : &red);
gtk_widget_modify_text(GTK_WIDGET(addressbar), GTK_STATE_NORMAL, is_dir ? NULL : &white);
old_value = is_dir;
}
if (!is_dir)
{
if (CONFIG_SHOW_BARS == 0)
dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("%s: no such directory."), directory);
return FALSE;
}
return is_dir;
}
static void
treebrowser_chroot(const gchar *dir)
{
gchar *directory;
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);
gtk_entry_set_text(GTK_ENTRY(addressbar), directory);
if (!directory || strlen(directory) == 0)
setptr(directory, g_strdup(G_DIR_SEPARATOR_S));
if (! treebrowser_checkdir(directory))
{
g_free(directory);
return;
}
treebrowser_bookmarks_set_state();
setptr(addressbar_last_address, directory);
treebrowser_browse(addressbar_last_address, NULL);
treebrowser_load_bookmarks();
}
static void
treebrowser_browse(const gchar *directory, gpointer parent)
{
GtkTreeIter iter, iter_empty, *last_dir_iter = NULL;
gboolean is_dir;
gboolean expanded = FALSE, has_parent;
gchar *utf8_name;
GSList *list, *node;
gchar *fname;
gchar *uri;
has_parent = parent ? gtk_tree_store_iter_is_valid(treestore, parent) : FALSE;
if (has_parent)
{
if (parent == &bookmarks_iter)
treebrowser_load_bookmarks();
}
else
parent = NULL;
if (has_parent && tree_view_row_expanded_iter(GTK_TREE_VIEW(treeview), parent))
{
expanded = TRUE;
treebrowser_bookmarks_set_state();
}
if (parent)
treebrowser_tree_store_iter_clear_nodes(parent, FALSE);
else
gtk_tree_store_clear(treestore);
list = utils_get_file_list(directory, NULL, NULL);
if (list != NULL)
{
foreach_slist_free(node, list)
{
fname = node->data;
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))
{
GdkPixbuf *icon = NULL;
if (is_dir)
{
if (last_dir_iter == NULL)
gtk_tree_store_prepend(treestore, &iter, parent);
else
{
gtk_tree_store_insert_after(treestore, &iter, parent, last_dir_iter);
gtk_tree_iter_free(last_dir_iter);
}
last_dir_iter = gtk_tree_iter_copy(&iter);
icon = CONFIG_SHOW_ICONS ? utils_pixbuf_from_stock(GTK_STOCK_DIRECTORY) : NULL;
gtk_tree_store_set(treestore, &iter,
TREEBROWSER_COLUMN_ICON, icon,
TREEBROWSER_COLUMN_NAME, fname,
TREEBROWSER_COLUMN_URI, uri,
-1);
gtk_tree_store_prepend(treestore, &iter_empty, &iter);
gtk_tree_store_set(treestore, &iter_empty,
TREEBROWSER_COLUMN_ICON, NULL,
TREEBROWSER_COLUMN_NAME, _("(Empty)"),
TREEBROWSER_COLUMN_URI, NULL,
-1);
}
else
{
if (check_filtered(utf8_name))
{
icon = CONFIG_SHOW_ICONS == 2
? utils_pixbuf_from_path(uri)
: CONFIG_SHOW_ICONS
? utils_pixbuf_from_stock(GTK_STOCK_FILE)
: NULL;
gtk_tree_store_append(treestore, &iter, parent);
gtk_tree_store_set(treestore, &iter,
TREEBROWSER_COLUMN_ICON, icon,
TREEBROWSER_COLUMN_NAME, fname,
TREEBROWSER_COLUMN_URI, uri,
-1);
}
}
if (icon)
g_object_unref(icon);
}
g_free(utf8_name);
g_free(uri);
g_free(fname);
}
}
else
{
gtk_tree_store_prepend(treestore, &iter_empty, parent);
gtk_tree_store_set(treestore, &iter_empty,
TREEBROWSER_COLUMN_ICON, NULL,
TREEBROWSER_COLUMN_NAME, _("(Empty)"),
TREEBROWSER_COLUMN_URI, NULL,
-1);
}
if (has_parent)
{
if (expanded)
gtk_tree_view_expand_row(GTK_TREE_VIEW(treeview), gtk_tree_model_get_path(GTK_TREE_MODEL(treestore), parent), FALSE);
}
else
treebrowser_load_bookmarks();
}
static void
treebrowser_bookmarks_set_state(void)
{
if (gtk_tree_store_iter_is_valid(treestore, &bookmarks_iter))
bookmarks_expanded = tree_view_row_expanded_iter(GTK_TREE_VIEW(treeview), &bookmarks_iter);
else
bookmarks_expanded = FALSE;
}
static void
treebrowser_load_bookmarks(void)
{
gchar *bookmarks;
gchar *contents, *path_full;
gchar **lines, **line;
GtkTreeIter iter;
gchar *pos;
GdkPixbuf *icon = NULL;
if (! CONFIG_SHOW_BOOKMARKS)
return;
bookmarks = g_build_filename(g_get_home_dir(), ".gtk-bookmarks", NULL);
if (g_file_get_contents(bookmarks, &contents, NULL, NULL))
{
if (gtk_tree_store_iter_is_valid(treestore, &bookmarks_iter))
{
bookmarks_expanded = tree_view_row_expanded_iter(GTK_TREE_VIEW(treeview), &bookmarks_iter);
treebrowser_tree_store_iter_clear_nodes(&bookmarks_iter, FALSE);
}
else
{
gtk_tree_store_prepend(treestore, &bookmarks_iter, NULL);
icon = CONFIG_SHOW_ICONS ? utils_pixbuf_from_stock(GTK_STOCK_HOME) : NULL;
gtk_tree_store_set(treestore, &bookmarks_iter,
TREEBROWSER_COLUMN_ICON, icon,
TREEBROWSER_COLUMN_NAME, _("Bookmarks"),
TREEBROWSER_COLUMN_URI, NULL,
-1);
if (icon)
g_object_unref(icon);
gtk_tree_store_insert_after(treestore, &iter, NULL, &bookmarks_iter);
gtk_tree_store_set(treestore, &iter,
TREEBROWSER_COLUMN_ICON, NULL,
TREEBROWSER_COLUMN_NAME, NULL,
TREEBROWSER_COLUMN_URI, NULL,
TREEBROWSER_COLUMN_FLAG, TREEBROWSER_FLAGS_SEPARATOR,
-1);
}
lines = g_strsplit (contents, "\n", 0);
for (line = lines; *line; ++line)
{
if (**line)
{
pos = g_utf8_strchr (*line, -1, ' ');
if (pos != NULL)
{
*pos = '\0';
}
}
path_full = g_filename_from_uri(*line, NULL, NULL);
if (path_full != NULL)
{
if (g_file_test(path_full, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
{
gchar *file_name = g_path_get_basename(path_full);
gtk_tree_store_append(treestore, &iter, &bookmarks_iter);
icon = CONFIG_SHOW_ICONS ? utils_pixbuf_from_stock(GTK_STOCK_DIRECTORY) : NULL;
gtk_tree_store_set(treestore, &iter,
TREEBROWSER_COLUMN_ICON, icon,
TREEBROWSER_COLUMN_NAME, file_name,
TREEBROWSER_COLUMN_URI, path_full,
-1);
g_free(file_name);
if (icon)
g_object_unref(icon);
gtk_tree_store_append(treestore, &iter, &iter);
gtk_tree_store_set(treestore, &iter,
TREEBROWSER_COLUMN_ICON, NULL,
TREEBROWSER_COLUMN_NAME, _("(Empty)"),
TREEBROWSER_COLUMN_URI, NULL,
-1);
}
g_free(path_full);
}
}
g_strfreev(lines);
g_free(contents);
if (bookmarks_expanded)
{
GtkTreePath *tree_path;
tree_path = gtk_tree_model_get_path(GTK_TREE_MODEL(treestore), &bookmarks_iter);
gtk_tree_view_expand_row(GTK_TREE_VIEW(treeview), tree_path, FALSE);
gtk_tree_path_free(tree_path);
}
}
g_free(bookmarks);
}
static gboolean
treebrowser_search(gchar *uri, gpointer parent)
{
GtkTreeIter iter;
GtkTreePath *path;
gchar *uri_current;
if (gtk_tree_model_iter_children(GTK_TREE_MODEL(treestore), &iter, parent))
{
do
{
if (gtk_tree_model_iter_has_child(GTK_TREE_MODEL(treestore), &iter))
if (treebrowser_search(uri, &iter))
return TRUE;
gtk_tree_model_get(GTK_TREE_MODEL(treestore), &iter, TREEBROWSER_COLUMN_URI, &uri_current, -1);
if (utils_str_equal(uri, uri_current) == TRUE)
{
path = gtk_tree_model_get_path(GTK_TREE_MODEL(treestore), &iter);
gtk_tree_view_expand_to_path(GTK_TREE_VIEW(treeview), path);
gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(treeview), path, NULL, FALSE, 0, 0);
gtk_tree_view_set_cursor(GTK_TREE_VIEW(treeview), path, treeview_column_text, FALSE);
gtk_tree_path_free(path);
g_free(uri_current);
return TRUE;
}
else
g_free(uri_current);
} while (gtk_tree_model_iter_next(GTK_TREE_MODEL(treestore), &iter));
}
return FALSE;
}
static void
fs_remove(gchar *root, gboolean delete_root)
{
gchar *path;
const gchar *name;
if (! g_file_test(root, G_FILE_TEST_EXISTS))
return;
if (g_file_test(root, G_FILE_TEST_IS_DIR))
{
GDir *dir;
dir = g_dir_open (root, 0, NULL);
if (!dir)
{
if (delete_root)
{
g_remove(root);
}
else return;
}
name = g_dir_read_name (dir);
while (name != NULL)
{
path = g_build_filename(root, name, NULL);
if (g_file_test(path, G_FILE_TEST_IS_DIR))
fs_remove(path, delete_root);
g_remove(path);
g_free(path);
name = g_dir_read_name(dir);
}
g_dir_close(dir);
}
else
delete_root = TRUE;
if (delete_root)
g_remove(root);
return;
}
static void
showbars(gboolean state)
{
if (state)
{
gtk_widget_show(sidebar_vbox_bars);
if (!CONFIG_SHOW_BARS)
CONFIG_SHOW_BARS = 1;
}
else
{
gtk_widget_hide(sidebar_vbox_bars);
CONFIG_SHOW_BARS = 0;
}
save_settings();
}
static void
treebrowser_tree_store_iter_clear_nodes(gpointer iter, gboolean delete_root)
{
GtkTreeIter i;
if (gtk_tree_model_iter_children(GTK_TREE_MODEL(treestore), &i, iter))
{
while (gtk_tree_store_remove(GTK_TREE_STORE(treestore), &i))
/* do nothing */;
}
if (delete_root)
gtk_tree_store_remove(GTK_TREE_STORE(treestore), iter);
}
static gboolean
treebrowser_expand_to_path(gchar* root, gchar* find)
{
guint i = 0;
gboolean found = FALSE, global_found = FALSE;
gchar *new = NULL, *new_root = NULL;
gchar **root_segments = NULL, **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);
find_segments_n = g_strv_length(find_segments)-1;
for (i = 0; i<=find_segments_n; i++)
{
new_root = g_build_filename(new ? new : G_DIR_SEPARATOR_S, find_segments[i], NULL);
SETPTR(new, new_root);
if (found)
{
if (treebrowser_search(new, NULL))
global_found = TRUE;
}
else
if (utils_str_equal(root, new) == TRUE)
found = TRUE;
}
g_free(new);
g_strfreev(root_segments);
g_strfreev(find_segments);
return global_found;
}
static gboolean
treebrowser_track_current(void)
{
GeanyDocument *doc = document_get_current();
gchar *path_current;
gchar **path_segments = NULL;
gchar *froot = NULL;
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 chrooting to the document`s nearles path
*/
froot = path_is_in_dir(addressbar_last_address, g_path_get_dirname(path_current));
if (froot == NULL)
froot = g_strdup(G_DIR_SEPARATOR_S);
if (utils_str_equal(froot, addressbar_last_address) != TRUE)
treebrowser_chroot(froot);
treebrowser_expand_to_path(froot, path_current);
}
g_strfreev(path_segments);
g_free(froot);
g_free(path_current);
return FALSE;
}
return FALSE;
}
static gboolean
treebrowser_iter_rename(gpointer iter)
{
GtkTreeViewColumn *column;
GtkCellRenderer *renderer;
GtkTreePath *path;
GList *renderers;
if (gtk_tree_store_iter_is_valid(treestore, iter))
{
path = gtk_tree_model_get_path(GTK_TREE_MODEL(treestore), iter);
if (G_LIKELY(path != NULL))
{
column = gtk_tree_view_get_column(GTK_TREE_VIEW(treeview), 0);
renderers = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
renderer = g_list_nth_data(renderers, TREEBROWSER_RENDER_TEXT);
g_object_set(G_OBJECT(renderer), "editable", TRUE, NULL);
gtk_tree_view_set_cursor_on_cell(GTK_TREE_VIEW(treeview), path, column, renderer, TRUE);
gtk_tree_path_free(path);
g_list_free(renderers);
return TRUE;
}
}
return FALSE;
}
static void
treebrowser_rename_current(void)
{
GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
GtkTreeIter iter;
GtkTreeModel *model;
if (gtk_tree_selection_get_selected(selection, &model, &iter))
{
treebrowser_iter_rename(&iter);
}
}
static void
treebrowser_create_new_current(const gchar *type)
{
on_menu_create_new_object(NULL, type);
}
/* ------------------
* RIGHTCLICK MENU EVENTS
* ------------------*/
static void
on_menu_go_up(GtkMenuItem *menuitem, gpointer *user_data)
{
gchar *uri;
uri = g_path_get_dirname(addressbar_last_address);
treebrowser_chroot(uri);
g_free(uri);
}
static void
on_menu_current_path(GtkMenuItem *menuitem, gpointer *user_data)
{
gchar *uri;
uri = get_default_dir();
treebrowser_chroot(uri);
g_free(uri);
}
static void
on_menu_open_externally(GtkMenuItem *menuitem, gchar *uri)
{
gchar *cmd, *locale_cmd, *dir, *c;
GString *cmd_str = g_string_new(CONFIG_OPEN_EXTERNAL_CMD);
GError *error = NULL;
dir = g_file_test(uri, G_FILE_TEST_IS_DIR) ? g_strdup(uri) : g_path_get_dirname(uri);
utils_string_replace_all(cmd_str, "%f", uri);
utils_string_replace_all(cmd_str, "%d", dir);
cmd = g_string_free(cmd_str, FALSE);
locale_cmd = utils_get_locale_from_utf8(cmd);
if (! g_spawn_command_line_async(locale_cmd, &error))
{
c = strchr(cmd, ' ');
if (c != NULL)
*c = '\0';
ui_set_statusbar(TRUE,
_("Could not execute configured external command '%s' (%s)."),
cmd, error->message);
g_error_free(error);
}
g_free(locale_cmd);
g_free(cmd);
g_free(dir);
}
static void
on_menu_open_terminal(GtkMenuItem *menuitem, gchar *uri)
{
gchar *argv[2] = {NULL, NULL};
argv[0] = get_terminal();