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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182 | /*
* lapiz-file-browser-plugin.c - Lapiz plugin providing easy file access
* from the sidepanel
*
* Copyright (C) 2006 - Jesse van den Kieboom <jesse@icecrew.nl>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <lapiz/lapiz-commands.h>
#include <lapiz/lapiz-utils.h>
#include <lapiz/lapiz-app.h>
#include <glib/gi18n-lib.h>
#include <lapiz/lapiz-window.h>
#include <lapiz/lapiz-debug.h>
#include <gio/gio.h>
#include <string.h>
#include <libbean/bean-activatable.h>
#include "lapiz-file-browser-enum-types.h"
#include "lapiz-file-browser-plugin.h"
#include "lapiz-file-browser-utils.h"
#include "lapiz-file-browser-error.h"
#include "lapiz-file-browser-widget.h"
#include "lapiz-file-browser-messages.h"
#define FILE_BROWSER_SCHEMA "org.cafe.lapiz.plugins.filebrowser"
#define FILE_BROWSER_ONLOAD_SCHEMA "org.cafe.lapiz.plugins.filebrowser.on-load"
#define BAUL_SCHEMA "org.cafe.baul.preferences"
#define BAUL_CLICK_POLICY_KEY "click-policy"
#define BAUL_ENABLE_DELETE_KEY "enable-delete"
#define BAUL_CONFIRM_TRASH_KEY "confirm-trash"
#define TERMINAL_SCHEMA "org.cafe.applications-terminal"
#define TERMINAL_EXEC_KEY "exec"
struct _LapizFileBrowserPluginPrivate
{
CtkWidget *window;
LapizFileBrowserWidget * tree_widget;
gulong merge_id;
CtkActionGroup * action_group;
CtkActionGroup * single_selection_action_group;
gboolean auto_root;
gulong end_loading_handle;
gboolean confirm_trash;
GSettings *settings;
GSettings *onload_settings;
GSettings *baul_settings;
GSettings *terminal_settings;
};
enum {
PROP_0,
PROP_OBJECT
};
static void on_uri_activated_cb (LapizFileBrowserWidget * widget,
gchar const *uri,
LapizWindow * window);
static void on_error_cb (LapizFileBrowserWidget * widget,
guint code,
gchar const *message,
LapizFileBrowserPluginPrivate * data);
static void on_model_set_cb (LapizFileBrowserView * widget,
GParamSpec *arg1,
LapizFileBrowserPluginPrivate * data);
static void on_virtual_root_changed_cb (LapizFileBrowserStore * model,
GParamSpec * param,
LapizFileBrowserPluginPrivate * data);
static void on_filter_mode_changed_cb (LapizFileBrowserStore * model,
GParamSpec * param,
LapizFileBrowserPluginPrivate * data);
static void on_rename_cb (LapizFileBrowserStore * model,
const gchar * olduri,
const gchar * newuri,
LapizWindow * window);
static void on_filter_pattern_changed_cb (LapizFileBrowserWidget * widget,
GParamSpec * param,
LapizFileBrowserPluginPrivate * data);
static void on_tab_added_cb (LapizWindow * window,
LapizTab * tab,
LapizFileBrowserPluginPrivate * data);
static gboolean on_confirm_delete_cb (LapizFileBrowserWidget * widget,
LapizFileBrowserStore * store,
GList * rows,
LapizFileBrowserPluginPrivate * data);
static gboolean on_confirm_no_trash_cb (LapizFileBrowserWidget * widget,
GList * files,
LapizWindow * window);
static void bean_activatable_iface_init (BeanActivatableInterface *iface);
G_DEFINE_DYNAMIC_TYPE_EXTENDED (LapizFileBrowserPlugin,<--- There is an unknown macro here somewhere. Configuration is required. If G_DEFINE_DYNAMIC_TYPE_EXTENDED is a macro then please configure it.
lapiz_file_browser_plugin,
BEAN_TYPE_EXTENSION_BASE,
0,
G_ADD_PRIVATE_DYNAMIC (LapizFileBrowserPlugin)
G_IMPLEMENT_INTERFACE_DYNAMIC (BEAN_TYPE_ACTIVATABLE,
bean_activatable_iface_init) \
\
lapiz_file_browser_enum_and_flag_register_type (type_module); \
_lapiz_file_browser_store_register_type (type_module); \
_lapiz_file_bookmarks_store_register_type (type_module); \
_lapiz_file_browser_view_register_type (type_module); \
_lapiz_file_browser_widget_register_type (type_module); \
)
static void
lapiz_file_browser_plugin_init (LapizFileBrowserPlugin * plugin)
{
plugin->priv = lapiz_file_browser_plugin_get_instance_private (plugin);
}
static void
lapiz_file_browser_plugin_dispose (GObject * object)
{
LapizFileBrowserPlugin *plugin = LAPIZ_FILE_BROWSER_PLUGIN (object);
if (plugin->priv->window != NULL)
{
g_object_unref (plugin->priv->window);
plugin->priv->window = NULL;
}
G_OBJECT_CLASS (lapiz_file_browser_plugin_parent_class)->dispose (object);
}
static void
lapiz_file_browser_plugin_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
LapizFileBrowserPlugin *plugin = LAPIZ_FILE_BROWSER_PLUGIN (object);
switch (prop_id)
{
case PROP_OBJECT:
plugin->priv->window = CTK_WIDGET (g_value_dup_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
lapiz_file_browser_plugin_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
LapizFileBrowserPlugin *plugin = LAPIZ_FILE_BROWSER_PLUGIN (object);
switch (prop_id)
{
case PROP_OBJECT:
g_value_set_object (value, plugin->priv->window);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
on_end_loading_cb (LapizFileBrowserStore *store,
CtkTreeIter *iter G_GNUC_UNUSED,
LapizFileBrowserPluginPrivate *data)
{
/* Disconnect the signal */
g_signal_handler_disconnect (store, data->end_loading_handle);
data->end_loading_handle = 0;
data->auto_root = FALSE;
}
static void
prepare_auto_root (LapizFileBrowserPluginPrivate *data)
{
LapizFileBrowserStore *store;
data->auto_root = TRUE;
store = lapiz_file_browser_widget_get_browser_store (data->tree_widget);
if (data->end_loading_handle != 0) {
g_signal_handler_disconnect (store, data->end_loading_handle);
data->end_loading_handle = 0;
}
data->end_loading_handle = g_signal_connect (store,
"end-loading",
G_CALLBACK (on_end_loading_cb),
data);
}
static void
restore_default_location (LapizFileBrowserPluginPrivate *data)
{
gchar * root;
gchar * virtual_root;
gboolean bookmarks;
gboolean remote;
bookmarks = !g_settings_get_boolean (data->onload_settings, "tree-view");
if (bookmarks) {
lapiz_file_browser_widget_show_bookmarks (data->tree_widget);
return;
}
root = g_settings_get_string (data->onload_settings, "root");
virtual_root = g_settings_get_string (data->onload_settings, "virtual-root");
remote = g_settings_get_boolean (data->onload_settings, "enable-remote");
if (root != NULL && *root != '\0') {
GFile *file;
file = g_file_new_for_uri (root);
if (remote || g_file_is_native (file)) {
if (virtual_root != NULL && *virtual_root != '\0') {
prepare_auto_root (data);
lapiz_file_browser_widget_set_root_and_virtual_root (data->tree_widget,
root,
virtual_root);
} else {
prepare_auto_root (data);
lapiz_file_browser_widget_set_root (data->tree_widget,
root,
TRUE);
}
}
g_object_unref (file);
}
g_free (root);
g_free (virtual_root);
}
static void
restore_filter (LapizFileBrowserPluginPrivate *data)
{
gchar *filter_mode;
LapizFileBrowserStoreFilterMode mode;
gchar *pattern;
/* Get filter_mode */
filter_mode = g_settings_get_string (data->settings, "filter-mode");
/* Filter mode */
mode = lapiz_file_browser_store_filter_mode_get_default ();
if (filter_mode != NULL) {
if (strcmp (filter_mode, "hidden") == 0) {
mode = LAPIZ_FILE_BROWSER_STORE_FILTER_MODE_HIDE_HIDDEN;
} else if (strcmp (filter_mode, "binary") == 0) {
mode = LAPIZ_FILE_BROWSER_STORE_FILTER_MODE_HIDE_BINARY;
} else if (strcmp (filter_mode, "hidden_and_binary") == 0 ||
strcmp (filter_mode, "binary_and_hidden") == 0) {
mode = LAPIZ_FILE_BROWSER_STORE_FILTER_MODE_HIDE_HIDDEN |
LAPIZ_FILE_BROWSER_STORE_FILTER_MODE_HIDE_BINARY;
} else if (strcmp (filter_mode, "none") == 0 ||
*filter_mode == '\0') {
mode = LAPIZ_FILE_BROWSER_STORE_FILTER_MODE_NONE;
}
}
/* Set the filter mode */
lapiz_file_browser_store_set_filter_mode (
lapiz_file_browser_widget_get_browser_store (data->tree_widget),
mode);
pattern = g_settings_get_string (data->settings, "filter-pattern");
lapiz_file_browser_widget_set_filter_pattern (data->tree_widget,
pattern);
g_free (filter_mode);
g_free (pattern);
}
static LapizFileBrowserViewClickPolicy
click_policy_from_string (gchar const *click_policy)
{
if (click_policy && strcmp (click_policy, "single") == 0)
return LAPIZ_FILE_BROWSER_VIEW_CLICK_POLICY_SINGLE;
else
return LAPIZ_FILE_BROWSER_VIEW_CLICK_POLICY_DOUBLE;
}
static void
on_click_policy_changed (GSettings *settings,
gchar *key,
gpointer user_data)
{
LapizFileBrowserPluginPrivate * data;
gchar *click_policy;
LapizFileBrowserViewClickPolicy policy = LAPIZ_FILE_BROWSER_VIEW_CLICK_POLICY_DOUBLE;
LapizFileBrowserView *view;
data = (LapizFileBrowserPluginPrivate *)(user_data);
click_policy = g_settings_get_string (settings, key);
policy = click_policy_from_string (click_policy);
view = lapiz_file_browser_widget_get_browser_view (data->tree_widget);
lapiz_file_browser_view_set_click_policy (view, policy);
g_free (click_policy);
}
static void
on_enable_delete_changed (GSettings *settings,
gchar *key,
gpointer user_data)
{
LapizFileBrowserPluginPrivate *data;
gboolean enable = FALSE;
data = (LapizFileBrowserPluginPrivate *)(user_data);
enable = g_settings_get_boolean (settings, key);
g_object_set (G_OBJECT (data->tree_widget), "enable-delete", enable, NULL);
}
static void
on_confirm_trash_changed (GSettings *settings,
gchar *key,
gpointer user_data)
{
LapizFileBrowserPluginPrivate *data;
gboolean enable = FALSE;
data = (LapizFileBrowserPluginPrivate *)(user_data);
enable = g_settings_get_boolean (settings, key);
data->confirm_trash = enable;
}
static gboolean
have_click_policy (void)
{
GSettings *settings = g_settings_new (BAUL_SCHEMA);
gchar *pref = g_settings_get_string (settings, BAUL_CLICK_POLICY_KEY);
gboolean result = (pref != NULL);
g_free (pref);
g_object_unref (settings);
return result;
}
static void
install_baul_prefs (LapizFileBrowserPluginPrivate *data)
{
gchar *pref;
gboolean prefb;
LapizFileBrowserViewClickPolicy policy;
LapizFileBrowserView *view;
if (have_click_policy ()) {
g_signal_connect (data->baul_settings,
"changed::" BAUL_CLICK_POLICY_KEY,
G_CALLBACK (on_click_policy_changed),
data);
}
g_signal_connect (data->baul_settings,
"changed::" BAUL_ENABLE_DELETE_KEY,
G_CALLBACK (on_enable_delete_changed),
data);
g_signal_connect (data->baul_settings,
"changed::" BAUL_CONFIRM_TRASH_KEY,
G_CALLBACK (on_confirm_trash_changed),
data);
pref = g_settings_get_string (data->baul_settings, BAUL_CLICK_POLICY_KEY);
policy = click_policy_from_string (pref);
g_free (pref);
view = lapiz_file_browser_widget_get_browser_view (data->tree_widget);
lapiz_file_browser_view_set_click_policy (view, policy);
prefb = g_settings_get_boolean (data->baul_settings, BAUL_ENABLE_DELETE_KEY);
g_object_set (G_OBJECT (data->tree_widget), "enable-delete", prefb, NULL);
prefb = g_settings_get_boolean (data->baul_settings, BAUL_CONFIRM_TRASH_KEY);
data->confirm_trash = prefb;
}
static void
set_root_from_doc (LapizFileBrowserPluginPrivate * data,
LapizDocument * doc)
{
GFile *file;
GFile *parent;
if (doc == NULL)
return;
file = lapiz_document_get_location (doc);
if (file == NULL)
return;
parent = g_file_get_parent (file);
if (parent != NULL) {
gchar * root;
root = g_file_get_uri (parent);
lapiz_file_browser_widget_set_root (data->tree_widget,
root,
TRUE);
g_object_unref (parent);
g_free (root);
}
g_object_unref (file);
}
static void
on_action_set_active_root (CtkAction *action G_GNUC_UNUSED,
LapizFileBrowserPluginPrivate *data)
{
set_root_from_doc (data,
lapiz_window_get_active_document (LAPIZ_WINDOW (data->window)));
}
static gchar *
get_terminal (LapizFileBrowserPluginPrivate * data)
{
gchar * terminal;
terminal = g_settings_get_string (data->terminal_settings,
TERMINAL_EXEC_KEY);
if (terminal == NULL) {
const gchar *term = g_getenv ("TERM");
if (term != NULL)
terminal = g_strdup (term);
else
terminal = g_strdup ("xterm");
}
return terminal;
}
static void
on_action_open_terminal (CtkAction *action G_GNUC_UNUSED,
LapizFileBrowserPluginPrivate *data)
{
gchar * terminal;
gchar * wd = NULL;
gchar * local;
gchar * argv[2];
GFile * file;
CtkTreeIter iter;
LapizFileBrowserStore * store;
/* Get the current directory */
if (!lapiz_file_browser_widget_get_selected_directory (data->tree_widget, &iter))
return;
store = lapiz_file_browser_widget_get_browser_store (data->tree_widget);
ctk_tree_model_get (CTK_TREE_MODEL (store),
&iter,
LAPIZ_FILE_BROWSER_STORE_COLUMN_URI,
&wd,
-1);
if (wd == NULL)
return;
terminal = get_terminal (data);
file = g_file_new_for_uri (wd);
local = g_file_get_path (file);
g_object_unref (file);
argv[0] = terminal;
argv[1] = NULL;
g_spawn_async (local,
argv,
NULL,
G_SPAWN_SEARCH_PATH,
NULL,
NULL,
NULL,
NULL);
g_free (terminal);
g_free (wd);
g_free (local);
}
static void
on_selection_changed_cb (CtkTreeSelection *selection G_GNUC_UNUSED,
LapizFileBrowserPluginPrivate *data)
{
CtkTreeView * tree_view;
CtkTreeModel * model;
CtkTreeIter iter;
gboolean sensitive;
gchar * uri;
tree_view = CTK_TREE_VIEW (lapiz_file_browser_widget_get_browser_view (data->tree_widget));
model = ctk_tree_view_get_model (tree_view);
if (!LAPIZ_IS_FILE_BROWSER_STORE (model))
return;
sensitive = lapiz_file_browser_widget_get_selected_directory (data->tree_widget, &iter);
if (sensitive) {
ctk_tree_model_get (model, &iter,
LAPIZ_FILE_BROWSER_STORE_COLUMN_URI,
&uri, -1);
sensitive = lapiz_utils_uri_has_file_scheme (uri);
g_free (uri);
}
ctk_action_set_sensitive (
ctk_action_group_get_action (data->single_selection_action_group,
"OpenTerminal"),
sensitive);
}
#define POPUP_UI "" \
"<ui>" \
" <popup name=\"FilePopup\">" \
" <placeholder name=\"FilePopup_Opt1\">" \
" <menuitem action=\"SetActiveRoot\"/>" \
" </placeholder>" \
" <placeholder name=\"FilePopup_Opt4\">" \
" <menuitem action=\"OpenTerminal\"/>" \
" </placeholder>" \
" </popup>" \
" <popup name=\"BookmarkPopup\">" \
" <placeholder name=\"BookmarkPopup_Opt1\">" \
" <menuitem action=\"SetActiveRoot\"/>" \
" </placeholder>" \
" </popup>" \
"</ui>"
static CtkActionEntry extra_actions[] =
{
{"SetActiveRoot", "go-jump", N_("_Set root to active document"),
NULL,
N_("Set the root to the active document location"),
G_CALLBACK (on_action_set_active_root)}
};
static CtkActionEntry extra_single_selection_actions[] = {
{"OpenTerminal", "utilities-terminal", N_("_Open terminal here"),
NULL,
N_("Open a terminal at the currently opened directory"),
G_CALLBACK (on_action_open_terminal)}
};
static void
add_popup_ui (LapizFileBrowserPluginPrivate *data)
{
CtkUIManager * manager;
CtkActionGroup * action_group;
GError * error = NULL;
manager = lapiz_file_browser_widget_get_ui_manager (data->tree_widget);
action_group = ctk_action_group_new ("FileBrowserPluginExtra");
ctk_action_group_set_translation_domain (action_group, NULL);
ctk_action_group_add_actions (action_group,
extra_actions,
G_N_ELEMENTS (extra_actions),
data);
ctk_ui_manager_insert_action_group (manager, action_group, 0);
data->action_group = action_group;
action_group = ctk_action_group_new ("FileBrowserPluginSingleSelectionExtra");
ctk_action_group_set_translation_domain (action_group, NULL);
ctk_action_group_add_actions (action_group,
extra_single_selection_actions,
G_N_ELEMENTS (extra_single_selection_actions),
data);
ctk_ui_manager_insert_action_group (manager, action_group, 0);
data->single_selection_action_group = action_group;
data->merge_id = ctk_ui_manager_add_ui_from_string (manager,
POPUP_UI,
-1,
&error);
if (data->merge_id == 0) {
g_warning("Unable to merge UI: %s", error->message);
g_error_free(error);
}
}
static void
remove_popup_ui (LapizFileBrowserPluginPrivate *data)
{
CtkUIManager * manager;
manager = lapiz_file_browser_widget_get_ui_manager (data->tree_widget);
ctk_ui_manager_remove_ui (manager, data->merge_id);
ctk_ui_manager_remove_action_group (manager, data->action_group);
g_object_unref (data->action_group);
ctk_ui_manager_remove_action_group (manager, data->single_selection_action_group);
g_object_unref (data->single_selection_action_group);
}
static void
lapiz_file_browser_plugin_update_state (BeanActivatable *activatable)
{
LapizFileBrowserPluginPrivate *data;
LapizDocument * doc;
data = LAPIZ_FILE_BROWSER_PLUGIN (activatable)->priv;
doc = lapiz_window_get_active_document (LAPIZ_WINDOW (data->window));
ctk_action_set_sensitive (ctk_action_group_get_action (data->action_group,
"SetActiveRoot"),
doc != NULL &&
!lapiz_document_is_untitled (doc));
}
static void
lapiz_file_browser_plugin_activate (BeanActivatable *activatable)
{
LapizFileBrowserPluginPrivate *data;
LapizWindow *window;
LapizPanel * panel;
CtkWidget * image;
GdkPixbuf * pixbuf;
LapizFileBrowserStore * store;
gchar *data_dir;
GSettingsSchemaSource *schema_source;
GSettingsSchema *schema;
data = LAPIZ_FILE_BROWSER_PLUGIN (activatable)->priv;
window = LAPIZ_WINDOW (data->window);
data_dir = bean_extension_base_get_data_dir (BEAN_EXTENSION_BASE (activatable));
data->tree_widget = LAPIZ_FILE_BROWSER_WIDGET (lapiz_file_browser_widget_new (data_dir));
g_free (data_dir);
data->settings = g_settings_new (FILE_BROWSER_SCHEMA);
data->onload_settings = g_settings_new (FILE_BROWSER_ONLOAD_SCHEMA);
data->terminal_settings = g_settings_new (TERMINAL_SCHEMA);
g_signal_connect (data->tree_widget,
"uri-activated",
G_CALLBACK (on_uri_activated_cb), window);
g_signal_connect (data->tree_widget,
"error", G_CALLBACK (on_error_cb), data);
g_signal_connect (data->tree_widget,
"notify::filter-pattern",
G_CALLBACK (on_filter_pattern_changed_cb),
data);
g_signal_connect (data->tree_widget,
"confirm-delete",
G_CALLBACK (on_confirm_delete_cb),
data);
g_signal_connect (data->tree_widget,
"confirm-no-trash",
G_CALLBACK (on_confirm_no_trash_cb),
window);
g_signal_connect (ctk_tree_view_get_selection (CTK_TREE_VIEW
(lapiz_file_browser_widget_get_browser_view
(data->tree_widget))),
"changed",
G_CALLBACK (on_selection_changed_cb),
data);
panel = lapiz_window_get_side_panel (window);
pixbuf = lapiz_file_browser_utils_pixbuf_from_theme("system-file-manager",
CTK_ICON_SIZE_MENU);
if (pixbuf) {
image = ctk_image_new_from_pixbuf(pixbuf);
g_object_unref(pixbuf);
} else {
image = ctk_image_new_from_icon_name("ctk-index", CTK_ICON_SIZE_MENU);
}
ctk_widget_show(image);
lapiz_panel_add_item (panel,
CTK_WIDGET (data->tree_widget),
_("File Browser"),
image);
ctk_widget_show (CTK_WIDGET (data->tree_widget));
add_popup_ui (data);
/* Restore filter options */
restore_filter (data);
/* Install baul preferences */
schema_source = g_settings_schema_source_get_default();
schema = g_settings_schema_source_lookup (schema_source, BAUL_SCHEMA, FALSE);
if (schema != NULL) {
data->baul_settings = g_settings_new (BAUL_SCHEMA);
install_baul_prefs (data);
g_settings_schema_unref (schema);
}
/* Connect signals to store the last visited location */
g_signal_connect (lapiz_file_browser_widget_get_browser_view (data->tree_widget),
"notify::model",
G_CALLBACK (on_model_set_cb),
data);
store = lapiz_file_browser_widget_get_browser_store (data->tree_widget);
g_signal_connect (store,
"notify::virtual-root",
G_CALLBACK (on_virtual_root_changed_cb),
data);
g_signal_connect (store,
"notify::filter-mode",
G_CALLBACK (on_filter_mode_changed_cb),
data);
g_signal_connect (store,
"rename",
G_CALLBACK (on_rename_cb),
window);
g_signal_connect (window,
"tab-added",
G_CALLBACK (on_tab_added_cb),
data);
/* Register messages on the bus */
lapiz_file_browser_messages_register (window, data->tree_widget);
lapiz_file_browser_plugin_update_state (activatable);
}
static void
lapiz_file_browser_plugin_deactivate (BeanActivatable *activatable)
{
LapizFileBrowserPluginPrivate *data;
LapizWindow *window;
LapizPanel * panel;
data = LAPIZ_FILE_BROWSER_PLUGIN (activatable)->priv;
window = LAPIZ_WINDOW (data->window);
/* Unregister messages from the bus */
lapiz_file_browser_messages_unregister (window);
/* Disconnect signals */
g_signal_handlers_disconnect_by_func (window,
G_CALLBACK (on_tab_added_cb),
data);
g_object_unref (data->settings);
g_object_unref (data->onload_settings);
g_object_unref (data->terminal_settings);
if (data->baul_settings)
g_object_unref (data->baul_settings);
remove_popup_ui (data);
panel = lapiz_window_get_side_panel (window);
lapiz_panel_remove_item (panel, CTK_WIDGET (data->tree_widget));
}
static void
lapiz_file_browser_plugin_class_init (LapizFileBrowserPluginClass * klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->dispose = lapiz_file_browser_plugin_dispose;
object_class->set_property = lapiz_file_browser_plugin_set_property;
object_class->get_property = lapiz_file_browser_plugin_get_property;
g_object_class_override_property (object_class, PROP_OBJECT, "object");
}
static void
lapiz_file_browser_plugin_class_finalize (LapizFileBrowserPluginClass *klass G_GNUC_UNUSED)
{
/* dummy function - used by G_DEFINE_DYNAMIC_TYPE_EXTENDED */
}
static void
bean_activatable_iface_init (BeanActivatableInterface *iface)
{
iface->activate = lapiz_file_browser_plugin_activate;
iface->deactivate = lapiz_file_browser_plugin_deactivate;
iface->update_state = lapiz_file_browser_plugin_update_state;
}
G_MODULE_EXPORT void
bean_register_types (BeanObjectModule *module)
{
lapiz_file_browser_plugin_register_type (G_TYPE_MODULE (module));
bean_object_module_register_extension_type (module,
BEAN_TYPE_ACTIVATABLE,
LAPIZ_TYPE_FILE_BROWSER_PLUGIN);
}
/* Callbacks */
static void
on_uri_activated_cb (LapizFileBrowserWidget *tree_widget G_GNUC_UNUSED,
gchar const *uri,
LapizWindow *window)
{
lapiz_commands_load_uri (window, uri, NULL, 0);
}
static void
on_error_cb (LapizFileBrowserWidget *tree_widget G_GNUC_UNUSED,
guint code,
gchar const *message,
LapizFileBrowserPluginPrivate *data)
{
gchar * title;
CtkWidget * dlg;
/* Do not show the error when the root has been set automatically */
if (data->auto_root && (code == LAPIZ_FILE_BROWSER_ERROR_SET_ROOT ||
code == LAPIZ_FILE_BROWSER_ERROR_LOAD_DIRECTORY))
{
/* Show bookmarks */
lapiz_file_browser_widget_show_bookmarks (data->tree_widget);
return;
}
switch (code) {
case LAPIZ_FILE_BROWSER_ERROR_NEW_DIRECTORY:
title =
_("An error occurred while creating a new directory");
break;
case LAPIZ_FILE_BROWSER_ERROR_NEW_FILE:
title = _("An error occurred while creating a new file");
break;
case LAPIZ_FILE_BROWSER_ERROR_RENAME:
title =
_
("An error occurred while renaming a file or directory");
break;
case LAPIZ_FILE_BROWSER_ERROR_DELETE:
title =
_
("An error occurred while deleting a file or directory");
break;
case LAPIZ_FILE_BROWSER_ERROR_OPEN_DIRECTORY:
title =
_
("An error occurred while opening a directory in the file manager");
break;
case LAPIZ_FILE_BROWSER_ERROR_SET_ROOT:
title =
_("An error occurred while setting a root directory");
break;
case LAPIZ_FILE_BROWSER_ERROR_LOAD_DIRECTORY:
title =
_("An error occurred while loading a directory");
break;
default:
title = _("An error occurred");
break;
}
dlg = ctk_message_dialog_new (CTK_WINDOW (data->window),
CTK_DIALOG_MODAL |
CTK_DIALOG_DESTROY_WITH_PARENT,
CTK_MESSAGE_ERROR, CTK_BUTTONS_OK,
"%s", title);
ctk_message_dialog_format_secondary_text (CTK_MESSAGE_DIALOG (dlg),
"%s", message);
ctk_dialog_run (CTK_DIALOG (dlg));
ctk_widget_destroy (dlg);
}
static void
on_model_set_cb (LapizFileBrowserView *widget G_GNUC_UNUSED,
GParamSpec *arg1 G_GNUC_UNUSED,
LapizFileBrowserPluginPrivate *data)
{
CtkTreeModel * model;
model = ctk_tree_view_get_model (CTK_TREE_VIEW (lapiz_file_browser_widget_get_browser_view (data->tree_widget)));
if (model == NULL)
return;
g_settings_set_boolean (data->onload_settings,
"tree-view",
LAPIZ_IS_FILE_BROWSER_STORE (model));
}
static void
on_filter_mode_changed_cb (LapizFileBrowserStore *model,
GParamSpec *param G_GNUC_UNUSED,
LapizFileBrowserPluginPrivate *data)
{
LapizFileBrowserStoreFilterMode mode;
mode = lapiz_file_browser_store_get_filter_mode (model);
if ((mode & LAPIZ_FILE_BROWSER_STORE_FILTER_MODE_HIDE_HIDDEN) &&
(mode & LAPIZ_FILE_BROWSER_STORE_FILTER_MODE_HIDE_BINARY)) {
g_settings_set_string (data->settings, "filter-mode", "hidden_and_binary");
} else if (mode & LAPIZ_FILE_BROWSER_STORE_FILTER_MODE_HIDE_HIDDEN) {
g_settings_set_string (data->settings, "filter-mode", "hidden");
} else if (mode & LAPIZ_FILE_BROWSER_STORE_FILTER_MODE_HIDE_BINARY) {
g_settings_set_string (data->settings, "filter-mode", "binary");
} else {
g_settings_set_string (data->settings, "filter-mode", "none");
}
}
static void
on_rename_cb (LapizFileBrowserStore *store G_GNUC_UNUSED,
const gchar *olduri,
const gchar *newuri,
LapizWindow *window G_GNUC_UNUSED)
{
LapizApp * app;
GList * documents;
GList * item;
LapizDocument * doc;
GFile * docfile;
GFile * oldfile;
GFile * newfile;
gchar * uri;
/* Find all documents and set its uri to newuri where it matches olduri */
app = lapiz_app_get_default ();
documents = lapiz_app_get_documents (app);
oldfile = g_file_new_for_uri (olduri);
newfile = g_file_new_for_uri (newuri);
for (item = documents; item; item = item->next) {
doc = LAPIZ_DOCUMENT (item->data);
uri = lapiz_document_get_uri (doc);
if (!uri)
continue;
docfile = g_file_new_for_uri (uri);
if (g_file_equal (docfile, oldfile)) {
lapiz_document_set_uri (doc, newuri);
} else {
gchar *relative;
relative = g_file_get_relative_path (oldfile, docfile);
if (relative) {
/* relative now contains the part in docfile without
the prefix oldfile */
g_object_unref (docfile);
g_free (uri);
docfile = g_file_get_child (newfile, relative);
uri = g_file_get_uri (docfile);
lapiz_document_set_uri (doc, uri);
}
g_free (relative);
}
g_free (uri);
g_object_unref (docfile);
}
g_object_unref (oldfile);
g_object_unref (newfile);
g_list_free (documents);
}
static void
on_filter_pattern_changed_cb (LapizFileBrowserWidget *widget,
GParamSpec *param G_GNUC_UNUSED,
LapizFileBrowserPluginPrivate *data)
{
gchar * pattern;
g_object_get (G_OBJECT (widget), "filter-pattern", &pattern, NULL);
if (pattern == NULL)
g_settings_set_string (data->settings, "filter-pattern", "");
else
g_settings_set_string (data->settings, "filter-pattern", pattern);
g_free (pattern);
}
static void
on_virtual_root_changed_cb (LapizFileBrowserStore *store,
GParamSpec *param G_GNUC_UNUSED,
LapizFileBrowserPluginPrivate *data)
{
gchar * root;
gchar * virtual_root;
root = lapiz_file_browser_store_get_root (store);
if (!root)
return;
g_settings_set_string (data->onload_settings, "root", root);
virtual_root = lapiz_file_browser_store_get_virtual_root (store);
if (!virtual_root) {
/* Set virtual to same as root then */
g_settings_set_string (data->onload_settings, "virtual-root", root);
} else {
g_settings_set_string (data->onload_settings, "virtual-root", virtual_root);
}
g_signal_handlers_disconnect_by_func (LAPIZ_WINDOW (data->window),
G_CALLBACK (on_tab_added_cb),
data);
g_free (root);
g_free (virtual_root);
}
static void
on_tab_added_cb (LapizWindow * window,
LapizTab * tab,
LapizFileBrowserPluginPrivate *data)
{
gboolean open;
gboolean load_default = TRUE;
open = g_settings_get_boolean (data->settings, "open-at-first-doc");
if (open) {
LapizDocument *doc;
gchar *uri;
doc = lapiz_tab_get_document (tab);
uri = lapiz_document_get_uri (doc);
if (uri != NULL && lapiz_utils_uri_has_file_scheme (uri)) {
prepare_auto_root (data);
set_root_from_doc (data, doc);
load_default = FALSE;
}
g_free (uri);
}
if (load_default)
restore_default_location (data);
/* Disconnect this signal, it's only called once */
g_signal_handlers_disconnect_by_func (window,
G_CALLBACK (on_tab_added_cb),
data);
}
static gchar *
get_filename_from_path (CtkTreeModel *model, CtkTreePath *path)
{
CtkTreeIter iter;
gchar *uri;
ctk_tree_model_get_iter (model, &iter, path);
ctk_tree_model_get (model, &iter,
LAPIZ_FILE_BROWSER_STORE_COLUMN_URI, &uri,
-1);
return lapiz_file_browser_utils_uri_basename (uri);
}
static gboolean
on_confirm_no_trash_cb (LapizFileBrowserWidget *widget G_GNUC_UNUSED,
GList *files,
LapizWindow *window)
{
gchar *normal;
gchar *message;
gchar *secondary;
gboolean result;
message = _("Cannot move file to trash, do you\nwant to delete permanently?");
if (files->next == NULL) {
normal = lapiz_file_browser_utils_file_basename (G_FILE (files->data));
secondary = g_strdup_printf (_("The file \"%s\" cannot be moved to the trash."), normal);
g_free (normal);
} else {
secondary = g_strdup (_("The selected files cannot be moved to the trash."));
}
result = lapiz_file_browser_utils_confirmation_dialog (window,
CTK_MESSAGE_QUESTION,
message,
secondary);
g_free (secondary);
return result;
}
static gboolean
on_confirm_delete_cb (LapizFileBrowserWidget *widget G_GNUC_UNUSED,
LapizFileBrowserStore *store,
GList *paths,
LapizFileBrowserPluginPrivate *data)
{
gchar *normal;
gchar *message;
gchar *secondary;
gboolean result;
if (!data->confirm_trash)
return TRUE;
if (paths->next == NULL) {
normal = get_filename_from_path (CTK_TREE_MODEL (store), (CtkTreePath *)(paths->data));
message = g_strdup_printf (_("Are you sure you want to permanently delete \"%s\"?"), normal);
g_free (normal);
} else {
message = g_strdup (_("Are you sure you want to permanently delete the selected files?"));
}
secondary = _("If you delete an item, it is permanently lost.");
result = lapiz_file_browser_utils_confirmation_dialog (LAPIZ_WINDOW (data->window),
CTK_MESSAGE_QUESTION,
message,
secondary);
g_free (message);
return result;
}
// ex:ts=8:noet:
|