-
-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathFileView.vala
More file actions
289 lines (241 loc) · 9.63 KB
/
Copy pathFileView.vala
File metadata and controls
289 lines (241 loc) · 9.63 KB
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
/*-
* Copyright (c) 2017 elementary LLC. (https://elementary.io),
* 2013 Julien Spautz <spautz.julien@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* as published by the Free Software Foundation, either version 3 of the
* License, 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 warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, 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, see <http://www.gnu.org/licenses/>.
*
* Authored by: Julien Spautz <spautz.julien@gmail.com>, Andrei-Costin Zisu <matzipan@gmail.com>
*/
/**
* SourceList that displays folders and their contents.
*/
public class Scratch.FolderManager.FileView : Granite.Widgets.SourceList, Code.PaneSwitcher {
private GLib.Settings settings;
public signal void select (string file);
public signal void close_all_docs_from_path (string path);
// This is a workaround for SourceList silliness: you cannot remove an item
// without it automatically selecting another one.
public bool ignore_next_select { get; set; default = false; }
public string icon_name { get; set; }
public string title { get; set; }
construct {
width_request = 180;
icon_name = "folder-symbolic";
title = _("Folders");
item_selected.connect (on_item_selected);
settings = new GLib.Settings ("io.elementary.code.folder-manager");
}
private void on_item_selected (Granite.Widgets.SourceList.Item? item) {
// This is a workaround for SourceList silliness: you cannot remove an item
// without it automatically selecting another one.
if (ignore_next_select) {
ignore_next_select = false;
return;
}
if (item is FileItem) {
select (((FileItem) item).file.path);
}
}
public void restore_saved_state () {
foreach (unowned string path in settings.get_strv ("opened-folders")) {
add_folder (new File (path), false);
}
}
public void open_folder (File folder) {
if (is_open (folder)) {
var existing = find_path (root, folder.path);
if (existing is Granite.Widgets.SourceList.ExpandableItem) {
((Granite.Widgets.SourceList.ExpandableItem)existing).expanded = true;
}
return;
}
add_folder (folder, true);
}
public void collapse_all () {
foreach (var child in root.children) {
((ProjectFolderItem) child).collapse_all ();
}
}
public void order_folders () {
var list = new Gee.ArrayList<ProjectFolderItem> ();
foreach (var child in root.children) {
root.remove (child as ProjectFolderItem);
list.add (child as ProjectFolderItem);
}
list.sort ( (a, b) => {
return a.name.down () > b.name.down () ? 0 : -1;
});
foreach (var item in list) {
root.add (item);
}
}
public void select_path (string path) {
item_selected.disconnect (on_item_selected);
selected = find_path (root, path);
item_selected.connect (on_item_selected);
}
private unowned Granite.Widgets.SourceList.Item? find_path (Granite.Widgets.SourceList.ExpandableItem list,
string path,
bool expand = false) {
foreach (var item in list.children) {
if (item is Item) {
var code_item = (Item)item;
if (code_item.path == path) {
return (!)item;
}
if (item is Granite.Widgets.SourceList.ExpandableItem) {
var expander = item as Granite.Widgets.SourceList.ExpandableItem;
if (!path.has_prefix (code_item.path)) {
continue;
}
if (!expander.expanded) {
if (expand) {
expander.expanded = true;
} else {
continue;
}
}
unowned var recurse_item = find_path (expander, path, expand);
if (recurse_item != null) {
return recurse_item;
}
}
}
}
return null;
}
public ProjectFolderItem? get_project_for_file (GLib.File file) {
foreach (var item in root.children) {
if (item is ProjectFolderItem) {
var folder = (ProjectFolderItem)item;
if (folder.is_git_repo && folder.contains_file (file)) {
return folder;
}
}
}
return null;
}
public unowned Granite.Widgets.SourceList.Item? expand_to_path (string path) {
return find_path (root, path, true);
}
/* Do global search on project containing the file path supplied in parameter */
public void search_global (string path, string? term = null) {
var item_for_path = (Item?)(expand_to_path (path));
if (item_for_path != null) {
var search_root = item_for_path.get_root_folder ();
if (search_root is ProjectFolderItem) {
search_root.global_search (search_root.file.file, term);
}
}
}
public void clear_badges () {
foreach (var child in root.children) {
if (child is ProjectFolderItem) {
((FolderItem)child).remove_all_badges ();
}
}
}
public void new_branch (string active_project_path) {
unowned var active_project = (ProjectFolderItem)(find_path (root, active_project_path));
if (active_project == null || !active_project.is_git_repo) {
Gdk.beep ();
return;
}
string? branch_name = null;
var dialog = new Dialogs.NewBranchDialog (active_project);
dialog.show_all ();
if (dialog.run () == Gtk.ResponseType.APPLY) {
branch_name = dialog.new_branch_name;
}
dialog.destroy ();
if (branch_name != null) {
active_project.new_branch (branch_name);
}
}
private void rename_items_with_same_name (Item item) {
string item_name = item.file.name;
foreach (var child in this.root.children) {
string new_other_item_name, new_item_name;
var other_item = child as ProjectFolderItem;
if (Utils.find_unique_path (item.file.file, other_item.file.file, out new_item_name, out new_other_item_name)) {
if (item_name.length < new_item_name.length) {
item_name = new_item_name;
}
if (other_item.name.length < new_other_item_name.length) {
other_item.name = new_other_item_name;
}
}
}
item.name = item_name;
}
private void add_folder (File folder, bool expand) {
if (is_open (folder)) {
warning ("Folder '%s' is already open.", folder.path);
return;
} else if (!folder.is_valid_directory (true)) { // Allow hidden top-level folders
warning ("Cannot open invalid directory.");
return;
}
var folder_root = new ProjectFolderItem (folder, this); // Constructor adds project to GitManager
this.root.add (folder_root);
rename_items_with_same_name (folder_root);
folder_root.expanded = expand;
folder_root.closed.connect (() => {
close_all_docs_from_path (folder_root.file.path);
root.remove (folder_root);
foreach (var child in root.children) {
var child_folder = (ProjectFolderItem) child;
if (child_folder.name != child_folder.file.name) {
rename_items_with_same_name (child_folder);
}
}
Scratch.Services.GitManager.get_instance ().remove_project (folder_root);
write_settings ();
});
folder_root.close_all_except.connect (() => {
foreach (var child in root.children) {
var project_folder_item = (ProjectFolderItem)child;
if (project_folder_item != folder_root) {
root.remove (project_folder_item);
Scratch.Services.GitManager.get_instance ().remove_project (project_folder_item);
}
}
write_settings ();
});
write_settings ();
}
private bool is_open (File folder) {
foreach (var child in root.children)
if (folder.path == ((Item) child).path)
return true;
return false;
}
private void write_settings () {
string[] to_save = {};
foreach (var main_folder in root.children) {
var saved = false;
var folder_path = ((Item) main_folder).path;
foreach (var saved_folder in to_save) {
if (folder_path == saved_folder) {
saved = true;
break;
}
}
if (!saved) {
to_save += folder_path;
}
}
settings.set_strv ("opened-folders", to_save);
}
}