~ubuntu-branches/debian/sid/geany-plugins/sid

« back to all changes in this revision

Viewing changes to treebrowser/src/treebrowser.c

  • Committer: Package Import Robot
  • Author(s): Evgeni Golov, Evgeni Golov
  • Date: 2011-11-17 20:03:24 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111117200324-8qmu6tuwjtfpv3n7
Tags: 0.21.1-1
[ Evgeni Golov ]
* [f4007f2] Imported Upstream version 0.21.1
* [5ff622d] install plugins from/to /usr/lib/<arch-triplet>/geany
* [75411d7] bump build-dep to geany 0.21
* [db92155] depend on the virtual geany-abi-XX package
* [dd84769] switch to 3.0 (quilt)
* [3ab76d7] drop readme.source
* [233d44c] refresh patch against 0.21.1
* [030bec1] add lintian override for
            documentation-package-not-architecture-independent
* [aba43d7] use debhelper compat level 9

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 *      Copyright 2010 Adrian Dimitrov <dimitrov.adrian@gmail.com>
5
5
 */
6
6
 
 
7
#ifdef HAVE_CONFIG_H
 
8
# include "config.h"
 
9
#endif
 
10
 
7
11
#include <sys/types.h>
8
12
#include <sys/stat.h>
9
13
#include <string.h>
18
22
# include <gio/gio.h>
19
23
#endif
20
24
 
 
25
#ifdef G_OS_WIN32
 
26
# include <windows.h>
 
27
#endif
21
28
 
22
29
/* These items are set by Geany before plugin_init() is called. */
23
30
GeanyPlugin                             *geany_plugin;
50
57
 * ------------------ */
51
58
 
52
59
static gchar                            *CONFIG_FILE                            = NULL;
 
60
#ifdef G_OS_WIN32
53
61
static gchar                            *CONFIG_OPEN_EXTERNAL_CMD       = "nautilus '%d'";
 
62
#else
 
63
static gchar                            *CONFIG_OPEN_EXTERNAL_CMD       = "explorer '%d'";
 
64
#endif
54
65
static gboolean                         CONFIG_REVERSE_FILTER           = FALSE;
55
66
static gboolean                         CONFIG_ONE_CLICK_CHDOC          = FALSE;
56
67
static gboolean                         CONFIG_SHOW_HIDDEN_FILES        = FALSE;
156
167
 * TREEBROWSER CORE FUNCTIONS
157
168
 * ------------------ */
158
169
 
159
 
 
160
170
static gboolean
161
171
tree_view_row_expanded_iter(GtkTreeView *tree_view, GtkTreeIter *iter)
162
172
{
216
226
#endif
217
227
}
218
228
 
 
229
 
 
230
/* result must be freed */
 
231
static gchar*
 
232
path_is_in_dir(gchar* src, gchar* find)
 
233
{
 
234
        int i = 0;
 
235
 
 
236
        gchar *diffed_path = NULL, *tmp = NULL;
 
237
        gchar **src_segments = NULL, **find_segments = NULL;
 
238
        guint src_segments_n = 0, find_segments_n = 0, n = 0;
 
239
 
 
240
        src_segments = g_strsplit(src, G_DIR_SEPARATOR_S, 0);
 
241
        find_segments = g_strsplit(find, G_DIR_SEPARATOR_S, 0);
 
242
 
 
243
        src_segments_n = g_strv_length(src_segments);
 
244
        find_segments_n = g_strv_length(find_segments);
 
245
 
 
246
        n = src_segments_n;
 
247
        if (find_segments_n < n)
 
248
                n = find_segments_n;
 
249
 
 
250
        for(i = 1; i<n; i++)
 
251
                if (utils_str_equal(find_segments[i], src_segments[i]) != TRUE)
 
252
                        break;
 
253
                else
 
254
                {
 
255
                        tmp = g_strconcat(diffed_path == NULL ? "" : diffed_path,
 
256
                                                                G_DIR_SEPARATOR_S, find_segments[i], NULL);
 
257
                        g_free(diffed_path);
 
258
                        diffed_path = tmp;
 
259
                }
 
260
 
 
261
        g_strfreev(src_segments);
 
262
        g_strfreev(find_segments);
 
263
 
 
264
        return diffed_path;
 
265
}
 
266
 
 
267
/* Return: FALSE - if file is filtered and not shown, and TRUE - if file isn`t filtered, and have to be shown */
219
268
static gboolean
220
269
check_filtered(const gchar *base_name)
221
270
{
222
271
        gchar           **filters;
223
272
        guint           i;
224
273
        gboolean        temporary_reverse       = FALSE;
225
 
        const gchar *exts[]                     = {".o", ".obj", ".so", ".dll", ".a", ".lib"};
 
274
        const gchar *exts[]                     = {".o", ".obj", ".so", ".dll", ".a", ".lib", ".la", ".lo", ".pyc"};
226
275
        guint exts_len;
227
276
        const gchar *ext;
228
277
        gboolean        filtered;
265
314
        return filtered;
266
315
}
267
316
 
268
 
static gboolean
269
 
check_hidden(const gchar *uri)
270
 
{
271
 
        gboolean is_visible = TRUE;
272
 
        gchar *base_name;
273
 
 
274
 
#ifdef G_OS_WIN32
275
 
# ifdef HAVE_GIO
276
 
        GFile *file;
277
 
        GFileInfo *info;
278
 
 
279
 
        if (CONFIG_SHOW_HIDDEN_FILES)
280
 
        {
281
 
                g_object_unref(info);
282
 
                g_object_unref(file);
283
 
                g_free(base_name);
284
 
                return TRUE;
285
 
        }
286
 
 
287
 
        if (uri[strlen(uri) - 1] == '~')
288
 
        {
289
 
                g_object_unref(info);
290
 
                g_object_unref(file);
291
 
                g_free(base_name);
292
 
                return FALSE;
293
 
        }
294
 
 
295
 
        file = g_file_new_for_path(uri);
296
 
        info = g_file_query_info(file, G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN, 0, NULL, NULL);
297
 
        if (info)
298
 
        {
299
 
                if (g_file_info_get_is_hidden(info))
300
 
                        is_visible = FALSE;
301
 
                g_object_unref(info);
302
 
        }
303
 
        g_object_unref(file);
304
 
# endif /* HAVE_GIO */
305
 
#else /* G_OS_WIN32 */
306
 
 
307
 
        if (CONFIG_SHOW_HIDDEN_FILES)
308
 
        {
309
 
                g_free(base_name);
310
 
                return TRUE;
311
 
        }
312
 
 
313
 
        if (uri[strlen(uri) - 1] == '~')
314
 
        {
315
 
                g_free(base_name);
316
 
                return FALSE;
317
 
        }
318
 
 
319
 
        base_name = g_path_get_basename(uri);
 
317
#ifdef G_OS_WIN32
 
318
static gboolean
 
319
win32_check_hidden(const gchar *filename)
 
320
{
 
321
        DWORD attrs;
 
322
        static wchar_t w_filename[MAX_PATH];
 
323
        MultiByteToWideChar(CP_UTF8, 0, filename, -1, w_filename, sizeof(w_filename));
 
324
        attrs = GetFileAttributesW(w_filename);
 
325
        if (attrs != INVALID_FILE_ATTRIBUTES && attrs & FILE_ATTRIBUTE_HIDDEN)
 
326
                return TRUE;
 
327
        return FALSE;
 
328
}
 
329
#endif
 
330
 
 
331
/* Returns: whether name should be hidden. */
 
332
static gboolean
 
333
check_hidden(const gchar *filename)
 
334
{
 
335
        const gchar *base_name = NULL;
 
336
        base_name = g_path_get_basename(filename);
 
337
        gsize len;
 
338
 
 
339
        if (! NZV(base_name))
 
340
                return FALSE;
 
341
 
 
342
#ifdef G_OS_WIN32
 
343
        if (win32_check_hidden(filename))
 
344
                return TRUE;
 
345
#else
320
346
        if (base_name[0] == '.')
321
 
                is_visible = FALSE;
322
 
        g_free(base_name);
 
347
                return TRUE;
323
348
#endif
324
349
 
325
 
        return is_visible;
 
350
        len = strlen(base_name);
 
351
        if (base_name[len - 1] == '~')
 
352
                return TRUE;
 
353
 
 
354
        return FALSE;
326
355
}
327
356
 
328
357
static gchar*
359
388
get_terminal()
360
389
{
361
390
        gchar           *terminal;
 
391
#ifdef G_OS_WIN32
 
392
        terminal = g_strdup("cmd");
 
393
#else
362
394
        const gchar *term = g_getenv("TERM");
363
 
 
364
395
        if (term != NULL)
365
396
                terminal = g_strdup(term);
366
397
        else
367
398
                terminal = g_strdup("xterm");
 
399
#endif
368
400
        return terminal;
369
401
}
370
402
 
399
431
static void
400
432
treebrowser_chroot(gchar *directory)
401
433
{
402
 
        if (g_str_has_suffix(directory, "/"))
 
434
        if (g_str_has_suffix(directory, G_DIR_SEPARATOR_S))
403
435
                g_strlcpy(directory, directory, strlen(directory));
404
436
 
405
437
        gtk_entry_set_text(GTK_ENTRY(addressbar), directory);
406
438
 
407
439
        if (!directory || strlen(directory) == 0)
408
 
                directory = "/";
 
440
                directory = G_DIR_SEPARATOR_S;
409
441
 
410
442
        if (! treebrowser_checkdir(directory))
411
443
                return;
460
492
                        is_dir          = g_file_test (uri, G_FILE_TEST_IS_DIR);
461
493
                        utf8_name       = utils_get_utf8_from_locale(fname);
462
494
 
463
 
                        if (check_hidden(uri))
 
495
                        if (!check_hidden(uri))
464
496
                        {
465
497
                                GdkPixbuf *icon = NULL;
466
498
 
749
781
}
750
782
 
751
783
static gboolean
 
784
treebrowser_expand_to_path(gchar* root, gchar* find)
 
785
{
 
786
        int i = 0, j = 0;
 
787
        gboolean founded = FALSE, global_founded = FALSE;
 
788
        gchar *new = "";
 
789
        gchar **root_segments = NULL, **find_segments = NULL;
 
790
        guint root_segments_n = 0, find_segments_n = 0;
 
791
 
 
792
        root_segments = g_strsplit(root, G_DIR_SEPARATOR_S, 0);
 
793
        find_segments = g_strsplit(find, G_DIR_SEPARATOR_S, 0);
 
794
 
 
795
        root_segments_n = g_strv_length(root_segments)-1;
 
796
        find_segments_n = g_strv_length(find_segments)-1;
 
797
 
 
798
 
 
799
        for (i = 1; i<=find_segments_n; i++)
 
800
        {
 
801
                new = g_strconcat(new, G_DIR_SEPARATOR_S, find_segments[i], NULL);
 
802
 
 
803
                if (founded)
 
804
                {
 
805
                        if (treebrowser_search(new, NULL))
 
806
                                global_founded = TRUE;
 
807
                }
 
808
                else
 
809
                        if (utils_str_equal(root, new) == TRUE)
 
810
                                founded = TRUE;
 
811
        }
 
812
 
 
813
        g_free(new);
 
814
        g_strfreev(root_segments);
 
815
        g_strfreev(find_segments);
 
816
 
 
817
        return global_founded;
 
818
}
 
819
 
 
820
static gboolean
752
821
treebrowser_track_current()
753
822
{
754
823
 
755
824
        GeanyDocument   *doc            = document_get_current();
756
825
        gchar                   *path_current;
757
 
        gchar                   **path_segments;
 
826
        gchar                   **path_segments = NULL;
 
827
        gchar                   *froot = NULL;
758
828
 
759
829
        if (doc != NULL && doc->file_name != NULL && g_path_is_absolute(doc->file_name))
760
830
        {
761
831
                path_current = utils_get_locale_from_utf8(doc->file_name);
762
832
 
763
 
                path_segments = g_strsplit(path_current, G_DIR_SEPARATOR_S, 0);
764
 
 
765
 
                treebrowser_search(path_current, NULL);
766
833
                /*
767
 
                 * NEED TO REWORK THE CONCEPT
 
834
                 * Checking if the document is in the expanded or collapsed files
768
835
                 */
 
836
                if (! treebrowser_search(path_current, NULL))
 
837
                {
 
838
                        /*
 
839
                         * Else we have to chroting to the document`s nearles path
 
840
                         */
 
841
 
 
842
                        froot = path_is_in_dir(addressbar_last_address, g_path_get_dirname(path_current));
 
843
 
 
844
                        if (froot == NULL)
 
845
                                froot = g_strdup(G_DIR_SEPARATOR_S);
 
846
 
 
847
                        if (utils_str_equal(froot, addressbar_last_address) != TRUE)
 
848
                                treebrowser_chroot(froot);
 
849
 
 
850
                        treebrowser_expand_to_path(froot, path_current);
 
851
                }
769
852
 
770
853
                g_strfreev(path_segments);
 
854
                g_free(froot);
771
855
                g_free(path_current);
772
856
 
773
857
                return FALSE;
893
977
}
894
978
 
895
979
static void
 
980
on_menu_find_in_files(GtkMenuItem *menuitem, gchar *uri)
 
981
{
 
982
        search_show_find_in_files_dialog(uri);
 
983
}
 
984
 
 
985
static void
896
986
on_menu_create_new_object(GtkMenuItem *menuitem, gchar *type)
897
987
{
898
988
        GtkTreeSelection        *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
1036
1126
}
1037
1127
 
1038
1128
static void
 
1129
on_menu_close_children(GtkMenuItem *menuitem, gchar *uri)
 
1130
{
 
1131
        guint i;
 
1132
        int uri_len = strlen(uri);
 
1133
        for (i=0; i < GEANY(documents_array)->len; i++)
 
1134
        {
 
1135
                if (documents[i]->is_valid)
 
1136
                {
 
1137
                        /* the docuemnt filename shoudl always be longer than the uri when closing children
 
1138
                         * Compare the beginingin of the filename string to see if it matchs the uri*/
 
1139
                        if (strlen(documents[i]->file_name) > uri_len )
 
1140
                        {
 
1141
                                if (strncmp(uri,documents[i]->file_name, uri_len)==0)
 
1142
                                        document_close(documents[i]);
 
1143
                        }
 
1144
                }
 
1145
        }
 
1146
}
 
1147
 
 
1148
static void
1039
1149
on_menu_copy_uri(GtkMenuItem *menuitem, gchar *uri)
1040
1150
{
1041
1151
        GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1099
1209
        gtk_container_add(GTK_CONTAINER(menu), item);
1100
1210
        g_signal_connect(item, "activate", G_CALLBACK(on_menu_refresh), NULL);
1101
1211
 
 
1212
        item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find in Files"));
 
1213
        gtk_container_add(GTK_CONTAINER(menu), item);
 
1214
        g_signal_connect(item, "activate", G_CALLBACK(on_menu_find_in_files), uri);
 
1215
        gtk_widget_set_sensitive(item, is_dir);
 
1216
 
1102
1217
        item = gtk_separator_menu_item_new();
1103
1218
        gtk_container_add(GTK_CONTAINER(menu), item);
1104
1219
 
1128
1243
        g_signal_connect(item, "activate", G_CALLBACK(on_menu_close), uri);
1129
1244
        gtk_widget_set_sensitive(item, is_document);
1130
1245
 
 
1246
        item = ui_image_menu_item_new(GTK_STOCK_CLOSE, g_strdup_printf(_("Close Child Documents ")));
 
1247
        gtk_container_add(GTK_CONTAINER(menu), item);
 
1248
        g_signal_connect(item, "activate", G_CALLBACK(on_menu_close_children), uri);
 
1249
        gtk_widget_set_sensitive(item, is_dir);
 
1250
 
1131
1251
        item = ui_image_menu_item_new(GTK_STOCK_COPY, _("Copy full path to clipboard"));
1132
1252
        gtk_container_add(GTK_CONTAINER(menu), item);
1133
1253
        g_signal_connect(item, "activate", G_CALLBACK(on_menu_copy_uri), uri);