Merge lp:~matzipan/scratch/use-rename-event into lp:~elementary-apps/scratch/scratch

Proposed by Zisu Andrei
Status: Work in progress
Proposed branch: lp:~matzipan/scratch/use-rename-event
Merge into: lp:~elementary-apps/scratch/scratch
Diff against target: 1847 lines (+463/-1120)
16 files modified
plugins/CMakeLists.txt (+1/-2)
plugins/filemanager/CMakeLists.txt (+0/-29)
plugins/filemanager/File.vala (+0/-207)
plugins/filemanager/FileManagerPlugin.vala (+0/-113)
plugins/filemanager/FileView.vala (+0/-297)
plugins/filemanager/Settings.vala (+0/-36)
plugins/filemanager/filemanager.plugin (+0/-10)
plugins/folder-manager/CMakeLists.txt (+2/-0)
plugins/folder-manager/File.vala (+79/-101)
plugins/folder-manager/FileItem.vala (+39/-0)
plugins/folder-manager/FileView.vala (+92/-254)
plugins/folder-manager/FolderItem.vala (+196/-0)
plugins/folder-manager/FolderManagerPlugin.vala (+50/-56)
plugins/folder-manager/folder-manager.plugin (+4/-4)
schemas/CMakeLists.txt (+0/-1)
schemas/org.pantheon.scratch.plugins.file-manager.gschema.xml (+0/-10)
To merge this branch: bzr merge lp:~matzipan/scratch/use-rename-event
Reviewer Review Type Date Requested Status
elementary Apps team Pending
Review via email: mp+312213@code.launchpad.net

Description of the change

This branch is WIP.

Currently, if you rename a file in the sidebar, when you perform the action, the selected file cursor jumps to a different file. This is because the sidebar doesn't use the rename GIO event, but instead deletes and creates the row everytime this happens.

However, using the rename event doesn't fully work, because Granite.SourceList doesn't have a way to trigger re-sorting of the elements. It only sorts an element when it is first added.

Thus, I am pushing this now as it is, in the hopes that I will address this issue in my sidebar-widget branch in granite. Then it can be merged into scratch.

To post a comment you must log in.

Unmerged revisions

1780. By Zisu Andrei

Use rename monitor event instead of create/remove

1779. By Zisu Andrei

Simplify file/folder validity check. Move common construct/rename/trash code into superclass. GObject constructor for File

1778. By Zisu Andrei

Merge filemanager into folder-manager

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'plugins/CMakeLists.txt'
--- plugins/CMakeLists.txt 2016-09-03 10:30:53 +0000
+++ plugins/CMakeLists.txt 2016-11-30 23:31:32 +0000
@@ -6,8 +6,7 @@
6)6)
77
8add_subdirectory (contractor)8add_subdirectory (contractor)
9add_subdirectory (pastebin)9add_subdirectory (pastebin)
10add_subdirectory (filemanager)
11add_subdirectory (folder-manager)10add_subdirectory (folder-manager)
12add_subdirectory (terminal)11add_subdirectory (terminal)
13add_subdirectory (browser-preview)12add_subdirectory (browser-preview)
1413
=== removed directory 'plugins/filemanager'
=== removed file 'plugins/filemanager/CMakeLists.txt'
--- plugins/filemanager/CMakeLists.txt 2016-09-03 10:30:53 +0000
+++ plugins/filemanager/CMakeLists.txt 1970-01-01 00:00:00 +0000
@@ -1,29 +0,0 @@
1add_definitions(${NORMAL_CFLAGS})
2link_directories(${NORMAL_LINK_DIRS})
3
4set (PLUGIN_NAME "filemanager")
5
6vala_precompile(VALA_C ${PLUGIN_NAME}
7 FileManagerPlugin.vala
8 File.vala
9 Settings.vala
10 FileView.vala
11PACKAGES
12 gtk+-3.0
13 gee-0.8
14 granite
15 scratchcore
16 libpeas-1.0
17 gtksourceview-3.0
18 ${ZEITGEIST_DEPS}
19OPTIONS
20 ${DEFAULT_PLUGIN_OPTIONS}
21)
22
23add_library(${PLUGIN_NAME} MODULE ${VALA_C})
24add_dependencies(${PLUGIN_NAME} ${LIBNAME})
25
26install(TARGETS ${PLUGIN_NAME} DESTINATION ${PLUGINDIR}/${PLUGIN_NAME})
27install(FILES ${PLUGIN_NAME}.plugin DESTINATION ${PLUGINDIR}/${PLUGIN_NAME})
28
29message("-- File Manager plugin will be compiled")
300
=== removed file 'plugins/filemanager/File.vala'
--- plugins/filemanager/File.vala 2013-07-16 15:56:38 +0000
+++ plugins/filemanager/File.vala 1970-01-01 00:00:00 +0000
@@ -1,207 +0,0 @@
1// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
2/***
3 BEGIN LICENSE
4
5 Copyright (C) 2013 Julien Spautz <spautz.julien@gmail.com>
6 This program is free software: you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License version 3, as published
8 by the Free Software Foundation.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranties of
12 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
13 PURPOSE. See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program. If not, see <http://www.gnu.org/licenses/>
17
18 END LICENSE
19***/
20
21namespace Scratch.Plugins.FileManager {
22
23 /**
24 * Class for easily dealing with files.
25 */
26 internal class File : GLib.Object {
27
28 public GLib.File file;
29 private GLib.FileInfo info;
30
31 private enum Type {
32 VALID_FILE,
33 VALID_FOLDER,
34 UNKNOWN,
35 INVALID
36 }
37
38 public File (string path) {
39 file = GLib.File.new_for_path (path);
40
41 info = new FileInfo ();
42 try {
43 info = file.query_info (
44 GLib.FileAttribute.STANDARD_CONTENT_TYPE + "," +
45 GLib.FileAttribute.STANDARD_IS_BACKUP + "," +
46 GLib.FileAttribute.STANDARD_IS_HIDDEN + "," +
47 GLib.FileAttribute.STANDARD_DISPLAY_NAME + "," +
48 GLib.FileAttribute.STANDARD_TYPE,
49 FileQueryInfoFlags.NONE);
50 } catch (GLib.Error error) {
51 info = null;
52 warning (error.message);
53 }
54 }
55
56 // returns the path the file
57 string _path = null;
58 public string path {
59 get { return _path != null ? _path : _path = file.get_path (); }
60 }
61
62 // returns the basename of the file
63 string _name = null;
64 public string name {
65 get { return _name != null ? _name : _name = info.get_display_name (); }
66 }
67
68 // returns the icon of the file's content type
69 GLib.Icon _icon = null;
70 public GLib.Icon icon {
71 get {
72 if (_icon != null)
73 return _icon;
74 //var content_type = info.get_attribute_string (FileAttribute.STANDARD_FAST_CONTENT_TYPE);
75 var content_type = info.get_content_type ();
76 return _icon = GLib.ContentType.get_icon (content_type);
77 }
78 }
79
80 // checks if file exists
81 public bool exists {
82 get { return file.query_exists (); }
83 }
84
85 Type _type = Type.UNKNOWN;
86 // checks if we're dealing with a non-hidden, non-backup directory
87 public bool is_valid_directory {
88 get {
89 if (_type == Type.VALID_FILE)
90 return false;
91 if (_type == Type.VALID_FOLDER)
92 return true;
93 if (_type == Type.INVALID)
94 return false;
95
96 if (info.get_file_type () != FileType.DIRECTORY ||
97 info.get_is_hidden () || info.get_is_backup ()) {
98 return false;
99 }
100
101 bool has_valid_children = false;
102
103 foreach (var child in children) {
104 if (child.is_valid_textfile) {
105 _type = Type.VALID_FOLDER;
106 return has_valid_children = true;
107 }
108 }
109
110 foreach (var child in children) {
111 if (child.is_valid_directory) {
112 has_valid_children = true;
113 _type = Type.VALID_FOLDER;
114 return has_valid_children = true;
115 }
116 }
117
118 return false;
119 }
120 }
121
122 // checks if we're dealing with a textfile
123 public bool is_valid_textfile {
124 get {
125 if (_type == Type.VALID_FILE)
126 return true;
127 if (_type == Type.VALID_FOLDER)
128 return false;
129 if (_type == Type.INVALID)
130 return false;
131
132 if (info.get_file_type () == FileType.REGULAR) {
133 //var content_type = info.get_attribute_string (FileAttribute.STANDARD_FAST_CONTENT_TYPE);
134 var content_type = info.get_content_type ();
135 if (ContentType.is_a (content_type, "text/*") &&
136 !info.get_is_backup () &&
137 !info.get_is_hidden ()) {
138 _type = Type.VALID_FILE;
139 return true;
140 }
141 }
142
143 return false;
144 }
145 }
146
147 // returns a list of all children of a directory
148 GLib.List <File> _children = null;
149 public GLib.List <File> children {
150 get {
151 if (_children != null)
152 return _children;
153
154 var parent = GLib.File.new_for_path (file.get_path ());
155 try {
156 var enumerator = parent.enumerate_children (
157 GLib.FileAttribute.STANDARD_NAME,
158 FileQueryInfoFlags.NONE
159 );
160
161 var file_info = new FileInfo ();
162 while ((file_info = enumerator.next_file ()) != null) {
163 var child = parent.get_child (file_info.get_name ());
164 _children.append (new File (child.get_path ()));
165 }
166 } catch (GLib.Error error) {
167 warning (error.message);
168 }
169
170 return _children;
171 }
172 }
173
174 public void rename (string name) {
175 try {
176 file.set_display_name (name);
177 } catch (GLib.Error error) {
178 warning (error.message);
179 }
180 }
181
182 /*public void trash () {
183 try {
184 file.trash ();
185 } catch (GLib.Error error) {
186 warning (error.message);
187 }
188 }*/
189
190 public void reset_cache () {
191 _name = null;
192 _path = null;
193 _icon = null;
194 _children = null;
195 _type = Type.UNKNOWN;
196 }
197
198 public static int compare (File a, File b) {
199 if (a.is_valid_directory && b.is_valid_textfile)
200 return -1;
201 if (a.is_valid_textfile && b.is_valid_directory)
202 return 1;
203 return strcmp (a.path.collate_key_for_filename (),
204 b.path.collate_key_for_filename ());
205 }
206 }
207}
2080
=== removed file 'plugins/filemanager/FileManagerPlugin.vala'
--- plugins/filemanager/FileManagerPlugin.vala 2016-09-03 11:50:58 +0000
+++ plugins/filemanager/FileManagerPlugin.vala 1970-01-01 00:00:00 +0000
@@ -1,113 +0,0 @@
1// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
2/***
3 BEGIN LICENSE
4
5 Copyright (C) 2013 Julien Spautz <spautz.julien@gmail.com>
6 This program is free software: you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License version 3, as published
8 by the Free Software Foundation.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranties of
12 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
13 PURPOSE. See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program. If not, see <http://www.gnu.org/licenses/>
17
18 END LICENSE
19***/
20
21public const string NAME = _("Folder Manager");
22public const string DESCRIPTION = _("Basic folder manager with file browsing");
23
24namespace Scratch.Plugins {
25 public class FileManagerPlugin : Peas.ExtensionBase, Peas.Activatable {
26 public Scratch.Services.Interface plugins;
27
28 Gtk.Box box;
29 FileManager.FileView view;
30
31 public Object object { owned get; construct; }
32
33 public FileManagerPlugin () {
34 message ("Starting File Manager Plugin");
35 }
36
37 public void activate () {
38 plugins = (Scratch.Services.Interface) object;
39 plugins.hook_notebook_sidebar.connect (on_hook_sidebar);
40 }
41
42 public void deactivate () {
43 if (box != null)
44 box.destroy();
45 }
46
47 public void update_state () {
48
49 }
50
51 void on_hook_sidebar (Gtk.Notebook notebook) {
52 if (view != null)
53 return;
54
55 box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
56
57 // File View
58 view = new FileManager.FileView ();
59
60 view.select.connect ((a) => {
61 var file = GLib.File.new_for_path (a);
62 plugins.open_file (file);
63 });
64
65 // Toolbar
66 var toolbar = new Gtk.Toolbar ();
67 toolbar.set_icon_size (Gtk.IconSize.SMALL_TOOLBAR);
68 toolbar.get_style_context ().add_class ("inline-toolbar");
69
70 var parent = new Gtk.ToolButton (null, null);
71 parent.tooltip_text = _("Go to parent");
72 parent.icon_name = "go-up-symbolic";
73 parent.clicked.connect (() => {
74 view.open_parent ();
75 parent.sensitive = !(view.folder.file.file.get_path () == "/");
76 });
77
78 var spacer = new Gtk.ToolItem ();
79 spacer.set_expand (true);
80
81 var add = new Gtk.ToolButton (null, null);
82 add.tooltip_text = _("Add file");
83 add.icon_name = "list-add-symbolic";
84 add.clicked.connect (() => {
85 view.add_file ();
86 });
87
88 var remove = new Gtk.ToolButton (null, null);
89 remove.tooltip_text = _("Remove file");
90 remove.icon_name = "edit-delete-symbolic";
91 remove.clicked.connect (() => {
92 view.remove_file ();
93 });
94
95 toolbar.insert (parent, -1);
96 toolbar.insert (spacer, -1);
97 toolbar.insert (add, -1);
98 toolbar.insert (remove, -1);
99
100 box.pack_start (view, true, true, 0);
101 box.pack_start (toolbar, false, false, 0);
102 box.show_all ();
103
104 notebook.append_page (box, new Gtk.Label (_("File Manager")));
105 }
106 }
107}
108
109[ModuleInit]
110public void peas_register_types (GLib.TypeModule module) {
111 var objmodule = module as Peas.ObjectModule;
112 objmodule.register_extension_type (typeof (Peas.Activatable), typeof (Scratch.Plugins.FileManagerPlugin));
113}
1140
=== removed file 'plugins/filemanager/FileView.vala'
--- plugins/filemanager/FileView.vala 2016-09-03 11:50:58 +0000
+++ plugins/filemanager/FileView.vala 1970-01-01 00:00:00 +0000
@@ -1,297 +0,0 @@
1// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
2/***
3 BEGIN LICENSE
4
5 Copyright (C) 2013 Julien Spautz <spautz.julien@gmail.com>
6 This program is free software: you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License version 3, as published
8 by the Free Software Foundation.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranties of
12 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
13 PURPOSE. See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program. If not, see <http://www.gnu.org/licenses/>
17
18 END LICENSE
19***/
20
21namespace Scratch.Plugins.FileManager {
22 Settings settings;
23
24 /**
25 * SourceList that displays folders and their contents.
26 */
27 internal class FileView : Granite.Widgets.SourceList {
28 public FolderItem? folder = null;
29 public signal void select (string file);
30 public FileView () {
31 this.width_request = 180;
32
33 this.item_selected.connect ((item) => {
34 select ((item as FileItem).path);
35 });
36
37 this.root.child_removed.connect (() => {
38 this.selected = null;
39 });
40
41 settings = new Settings ();
42 restore_settings ();
43 }
44
45 public void open_parent () {
46 GLib.File parent = this.folder.file.file.get_parent ();
47 this.root.remove (this.folder);
48 open_folder (new File (parent.get_path ()));
49 }
50
51 public void open_folder (File folder, bool expand = true) {
52 if (is_open (folder)) {
53 warning ("Folder '%s' is already open.", folder.path);
54 return;
55 } else if (!folder.is_valid_directory) {
56 warning ("Cannot open invalid directory.");
57 return;
58 }
59
60 // Clean the SourceList before start adding something
61 if (this.folder != null) {
62 this.root.remove (this.folder);
63 }
64
65 this.folder = new FolderItem (folder, this);
66 this.root.add (this.folder);
67
68 this.folder.expanded = expand;
69 write_settings ();
70 }
71
72 public void add_file () {
73 string path = folder.file.file.get_path () + _("/New File");
74 var file = GLib.File.new_for_path (path);
75 int n = 1;
76 while (file.query_exists ()) {
77 file = GLib.File.new_for_path (path + n.to_string ());
78 n++;
79 }
80
81 try {
82 file.create (FileCreateFlags.NONE);
83 } catch (Error e) {
84 warning (e.message);
85 }
86
87 var item = new FileItem (new File (file.get_path ()));
88 this.folder.add (item);
89 }
90
91 public void remove_file () {
92 if (this.selected is FileItem) {
93 var file = GLib.File.new_for_path (((FileItem)selected).file.file.get_path ());
94 try {
95 file.delete ();
96 this.root.remove (selected);
97 } catch (Error e) {
98 warning (e.message);
99 }
100 }
101
102 this.selected = null;
103 }
104
105 private bool is_open (File folder) {
106 foreach (var child in root.children) {
107 if (folder.path == (child as Item).path) {
108 return true;
109 }
110 }
111
112 return false;
113 }
114
115 private void write_settings () {
116 settings.opened_folder = this.folder.file.file.get_path ();
117 }
118
119 private void restore_settings () {
120 var folder = new File (settings.opened_folder);
121 if (settings.opened_folder == "" || settings.opened_folder == null || !folder.is_valid_directory) {
122 settings.opened_folder = GLib.Environment.get_home_dir ();
123 }
124
125 open_folder (new File (settings.opened_folder));
126 }
127 }
128
129 /**
130 * Common abstract class for normal and expandable items.
131 */
132 internal class Item : Granite.Widgets.SourceList.ExpandableItem, Granite.Widgets.SourceListSortable {
133 public File file { get; construct; }
134 public string path { get { return file.path; } }
135
136 public int compare (Granite.Widgets.SourceList.Item a, Granite.Widgets.SourceList.Item b) {
137 if (a is FolderItem && b is FileItem) {
138 return -1;
139 } else if (a is FileItem && b is FolderItem) {
140 return 1;
141 }
142
143 return File.compare ((a as Item).file, (b as Item).file);
144 }
145
146 public bool allow_dnd_sorting () {
147 return false;
148 }
149 }
150
151 /**
152 * Normal item in the source list, represents a textfile.
153 * TODO Remove, Rename
154 */
155 internal class FileItem : Item {
156 //Gtk.Menu menu;
157 //Gtk.MenuItem item_trash;
158 public FileItem (File file) requires (file.is_valid_textfile) {
159 Object (file: file);
160
161 this.selectable = true;
162 this.editable = true;
163 this.name = file.name;
164 this.icon = file.icon;
165 }
166
167 public void rename (string new_name) {
168 string new_uri = file.file.get_parent ().get_uri () + "/" + new_name;
169 debug (new_uri);
170 file.rename (new_name);
171 }
172
173 /*public override Gtk.Menu? get_context_menu () {
174 menu = new Gtk.Menu ();
175 item_trash = new Gtk.MenuItem.with_label (_("Move to Trash"));
176 menu.append (item_trash);
177 item_trash.activate.connect (() => { file.trash (); });
178 menu.show_all ();
179 return menu;
180 }*/
181 }
182
183 /**
184 * Expandable item in the source list, represents a folder.
185 * Monitored for changes inside the directory.
186 * TODO remove, rename, create new file
187 */
188 internal class FolderItem : Item {
189 public signal void folder_open (GLib.File folder);
190 public FileView view { get; construct; }
191
192 private GLib.FileMonitor monitor;
193 private bool children_loaded = false;
194
195 //Gtk.Menu menu;
196 //Gtk.MenuItem item_trash;
197 //Gtk.MenuItem item_create;
198
199 public FolderItem (File file, FileView view) requires (file.is_valid_directory) {
200 Object (file: file, view: view);
201
202 this.editable = false;
203 this.selectable = false;
204 this.name = file.name;
205 this.icon = file.icon;
206
207 this.add (new Granite.Widgets.SourceList.Item ("")); // dummy
208 this.toggled.connect (() => {
209 if (this.expanded && this.n_children <= 1) {
210 this.clear ();
211 this.add_children ();
212 children_loaded = true;
213 }
214 });
215
216 try {
217 monitor = file.file.monitor_directory (GLib.FileMonitorFlags.NONE);
218 monitor.changed.connect ((s,d,e) => { on_changed (s,d,e); });
219 } catch (GLib.Error e) {
220 warning (e.message);
221 }
222 }
223
224 public override Gtk.Menu? get_context_menu () {
225 if (this == this.view.root.children.to_array ()[0]) {
226 return null;
227 }
228
229 var menu = new Gtk.Menu ();
230 var item = new Gtk.MenuItem.with_label (_("Open"));
231 item.activate.connect (() => { this.folder_open (this.file.file); });
232 menu.append (item);
233 menu.show_all ();
234 return menu;
235 }
236
237 internal void add_children () {
238 foreach (var child in file.children) {
239 if (child.is_valid_directory) {
240 var item = new FolderItem (child, view);
241 item.folder_open.connect (() => {
242 this.view.open_folder (child);
243 });
244
245 add (item);
246 } else if (child.is_valid_textfile) {
247 var item = new FileItem (child);
248 add (item);
249 item.edited.connect (item.rename);
250 }
251 }
252 }
253
254 private void on_changed (GLib.File source, GLib.File? dest, GLib.FileMonitorEvent event) {
255 if (!children_loaded) {
256 this.file.reset_cache ();
257 return;
258 }
259
260 switch (event) {
261 case GLib.FileMonitorEvent.DELETED:
262 var children_tmp = new Gee.ArrayList<Granite.Widgets.SourceList.Item> ();
263 children_tmp.add_all (children);
264 foreach (var item in children_tmp) {
265 if ((item as Item).path == source.get_path ()) {
266 remove (item);
267 }
268 }
269
270 break;
271 case GLib.FileMonitorEvent.CREATED:
272 if (source.query_exists () == false) {
273 return;
274 }
275
276 var file = new File (source.get_path ());
277 var exists = false;
278 foreach (var item in children) {
279 if ((item as Item).path == file.path) {
280 exists = true;
281 }
282 }
283
284 if (!exists) {
285 if (file.is_valid_textfile) {
286 this.add (new FileItem (file));
287 } else if (file.is_valid_directory) {
288 this.add (new FolderItem (file, view));
289 }
290 }
291
292 break;
293 }
294 }
295 }
296
297}
2980
=== removed file 'plugins/filemanager/Settings.vala'
--- plugins/filemanager/Settings.vala 2013-07-16 15:56:38 +0000
+++ plugins/filemanager/Settings.vala 1970-01-01 00:00:00 +0000
@@ -1,36 +0,0 @@
1// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
2/***
3 BEGIN LICENSE
4
5 Copyright (C) 2013 Julien Spautz <spautz.julien@gmail.com>
6 This program is free software: you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License version 3, as published
8 by the Free Software Foundation.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranties of
12 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
13 PURPOSE. See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program. If not, see <http://www.gnu.org/licenses/>
17
18 END LICENSE
19***/
20
21namespace Scratch.Plugins.FileManager {
22
23 /**
24 * Class for interacting with gsettings.
25 */
26 internal class Settings : Granite.Services.Settings {
27
28 private const string SCHEMA = "org.pantheon.scratch.plugins.file-manager";
29
30 public string opened_folder { get; set; }
31
32 public Settings () {
33 base (SCHEMA);
34 }
35 }
36}
370
=== removed file 'plugins/filemanager/filemanager.plugin'
--- plugins/filemanager/filemanager.plugin 2013-07-16 15:56:38 +0000
+++ plugins/filemanager/filemanager.plugin 1970-01-01 00:00:00 +0000
@@ -1,10 +0,0 @@
1[Plugin]
2Module=filemanager
3Loader=C
4IAge=2
5Name=File Manager
6Description=Browse files and directories
7Icon=system-file-manager
8Authors=Mario Guerriero
9Copyright=Copyright © 2013 Scratch Developers
10Website=http://launchpad.net/scratch
110
=== modified file 'plugins/folder-manager/CMakeLists.txt'
--- plugins/folder-manager/CMakeLists.txt 2016-09-03 10:30:53 +0000
+++ plugins/folder-manager/CMakeLists.txt 2016-11-30 23:31:32 +0000
@@ -8,6 +8,8 @@
8 File.vala8 File.vala
9 Settings.vala9 Settings.vala
10 FileView.vala10 FileView.vala
11 FileItem.vala
12 FolderItem.vala
11PACKAGES13PACKAGES
12 gtk+-3.014 gtk+-3.0
13 gee-0.815 gee-0.8
1416
=== modified file 'plugins/folder-manager/File.vala'
--- plugins/folder-manager/File.vala 2013-06-02 13:07:08 +0000
+++ plugins/folder-manager/File.vala 2016-11-30 23:31:32 +0000
@@ -1,77 +1,66 @@
1// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-1// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
2/***2/*-
3 BEGIN LICENSE3 * Copyright (c) 2016 elementary LLC. (https://elementary.io)
44 *
5 Copyright (C) 2013 Julien Spautz <spautz.julien@gmail.com>5 * This program is free software: you can redistribute it and/or modify
6 This program is free software: you can redistribute it and/or modify it6 * it under the terms of the GNU General Public License as published by
7 under the terms of the GNU Lesser General Public License version 3, as published7 * the Free Software Foundation, either version 3 of the License, or
8 by the Free Software Foundation.8 * (at your option) any later version.
99 *
10 This program is distributed in the hope that it will be useful, but10 * This program is distributed in the hope that it will be useful,
11 WITHOUT ANY WARRANTY; without even the implied warranties of11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 PURPOSE. See the GNU General Public License for more details.13 * GNU General Public License for more details.
1414 *
15 You should have received a copy of the GNU General Public License along15 * You should have received a copy of the GNU General Public License
16 with this program. If not, see <http://www.gnu.org/licenses/>16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1717 *
18 END LICENSE18 * Authored by: Julien Spautz <spautz.julien@gmail.com>, Andrei-Costin Zisu <matzipan@gmail.com>
19***/19 */
2020
21namespace Scratch.Plugins.FolderManager {21namespace Scratch.Plugins.FolderManager {
2222
23 /**23 /**
24 * Class for easily dealing with files.24 * Class for easily dealing with files.
25 */25 */
26 internal class File : GLib.Object {26 public class File : GLib.Object {
2727
28 public GLib.File file;28 public GLib.File file { get; private set; }
29 private GLib.FileInfo info;29 private GLib.FileInfo info;
3030
31 private enum Type {
32 VALID_FILE,
33 VALID_FOLDER,
34 UNKNOWN,
35 INVALID
36 }
37
38 public File (string path) {31 public File (string path) {
39 file = GLib.File.new_for_path (path);32 Object (path: path);
40
41 info = new FileInfo ();
42 try {
43 info = file.query_info (
44 GLib.FileAttribute.STANDARD_CONTENT_TYPE + "," +
45 GLib.FileAttribute.STANDARD_IS_BACKUP + "," +
46 GLib.FileAttribute.STANDARD_IS_HIDDEN + "," +
47 GLib.FileAttribute.STANDARD_DISPLAY_NAME + "," +
48 GLib.FileAttribute.STANDARD_TYPE,
49 FileQueryInfoFlags.NONE);
50 } catch (GLib.Error error) {
51 info = null;
52 warning (error.message);
53 }
54 }33 }
5534
56 // returns the path the file35 // returns the path the file
57 string _path = null;36 private string _path;
58 public string path {37 public string path {
59 get { return _path != null ? _path : _path = file.get_path (); }38 get {
39 _path = file.get_path ();
40 return _path;
41 }
42 set construct {
43 load_file_for_path (value);
44 }
60 }45 }
6146
62 // returns the basename of the file47 // returns the basename of the file
63 string _name = null;48 private string _name;
64 public string name {49 public string name {
65 get { return _name != null ? _name : _name = info.get_display_name (); }50 get {
51 _name = info.get_display_name ();
52 return _name;
53 }
66 }54 }
6755
68 // returns the icon of the file's content type56 // returns the icon of the file's content type
69 GLib.Icon _icon = null;57 GLib.Icon _icon = null;
70 public GLib.Icon icon {58 public GLib.Icon icon {
71 get {59 get {
72 if (_icon != null)60 if (_icon != null) {
73 return _icon;61 return _icon;
74 //var content_type = info.get_attribute_string (FileAttribute.STANDARD_FAST_CONTENT_TYPE);62 }
63
75 var content_type = info.get_content_type ();64 var content_type = info.get_content_type ();
76 return _icon = GLib.ContentType.get_icon (content_type);65 return _icon = GLib.ContentType.get_icon (content_type);
77 }66 }
@@ -82,37 +71,15 @@
82 get { return file.query_exists (); }71 get { return file.query_exists (); }
83 }72 }
8473
85 Type _type = Type.UNKNOWN;
86 // checks if we're dealing with a non-hidden, non-backup directory74 // checks if we're dealing with a non-hidden, non-backup directory
87 public bool is_valid_directory {75 public bool is_valid_directory {
88 get {76 get {
89 if (_type == Type.VALID_FILE)77 if (info.get_is_hidden () || info.get_is_backup ()) {
90 return false;78 return false;
91 if (_type == Type.VALID_FOLDER)79 }
80
81 if (info.get_file_type () == FileType.DIRECTORY) {
92 return true;82 return true;
93 if (_type == Type.INVALID)
94 return false;
95
96 if (info.get_file_type () != FileType.DIRECTORY ||
97 info.get_is_hidden () || info.get_is_backup ()) {
98 return false;
99 }
100
101 bool has_valid_children = false;
102
103 foreach (var child in children) {
104 if (child.is_valid_textfile) {
105 _type = Type.VALID_FOLDER;
106 return has_valid_children = true;
107 }
108 }
109
110 foreach (var child in children) {
111 if (child.is_valid_directory) {
112 has_valid_children = true;
113 _type = Type.VALID_FOLDER;
114 return has_valid_children = true;
115 }
116 }83 }
11784
118 return false;85 return false;
@@ -122,22 +89,13 @@
122 // checks if we're dealing with a textfile89 // checks if we're dealing with a textfile
123 public bool is_valid_textfile {90 public bool is_valid_textfile {
124 get {91 get {
125 if (_type == Type.VALID_FILE)92 if (info.get_is_hidden () || info.get_is_backup ()) {
93 return false;
94 }
95
96 if (info.get_file_type () == FileType.REGULAR &&
97 ContentType.is_a (info.get_content_type (), "text/*")) {
126 return true;98 return true;
127 if (_type == Type.VALID_FOLDER)
128 return false;
129 if (_type == Type.INVALID)
130 return false;
131
132 if (info.get_file_type () == FileType.REGULAR) {
133 //var content_type = info.get_attribute_string (FileAttribute.STANDARD_FAST_CONTENT_TYPE);
134 var content_type = info.get_content_type ();
135 if (ContentType.is_a (content_type, "text/*") &&
136 !info.get_is_backup () &&
137 !info.get_is_hidden ()) {
138 _type = Type.VALID_FILE;
139 return true;
140 }
141 }99 }
142100
143 return false;101 return false;
@@ -148,8 +106,9 @@
148 GLib.List <File> _children = null;106 GLib.List <File> _children = null;
149 public GLib.List <File> children {107 public GLib.List <File> children {
150 get {108 get {
151 if (_children != null)109 if (_children != null) {
152 return _children;110 return _children;
111 }
153112
154 var parent = GLib.File.new_for_path (file.get_path ());113 var parent = GLib.File.new_for_path (file.get_path ());
155 try {114 try {
@@ -171,9 +130,27 @@
171 }130 }
172 }131 }
173132
174 /*public void rename (string name) {133 private void load_file_for_path (string path) {
175 try {134 file = GLib.File.new_for_path (path);
176 file.set_display_name (name);135
136 info = new FileInfo ();
137 try {
138 info = file.query_info (
139 GLib.FileAttribute.STANDARD_CONTENT_TYPE + "," +
140 GLib.FileAttribute.STANDARD_IS_BACKUP + "," +
141 GLib.FileAttribute.STANDARD_IS_HIDDEN + "," +
142 GLib.FileAttribute.STANDARD_DISPLAY_NAME + "," +
143 GLib.FileAttribute.STANDARD_TYPE,
144 FileQueryInfoFlags.NONE);
145 } catch (GLib.Error error) {
146 info = null;
147 warning (error.message);
148 }
149 }
150
151 public void rename (string name) {
152 try {
153 file.set_display_name (name);
177 } catch (GLib.Error error) {154 } catch (GLib.Error error) {
178 warning (error.message);155 warning (error.message);
179 }156 }
@@ -185,21 +162,22 @@
185 } catch (GLib.Error error) {162 } catch (GLib.Error error) {
186 warning (error.message);163 warning (error.message);
187 }164 }
188 }*/165 }
189166
190 public void reset_cache () {167 private void reset_cache () {
191 _name = null;
192 _path = null;
193 _icon = null;168 _icon = null;
194 _children = null;169 _children = null;
195 _type = Type.UNKNOWN;
196 }170 }
197171
198 public static int compare (File a, File b) {172 public static int compare (File a, File b) {
199 if (a.is_valid_directory && b.is_valid_textfile)173 if (a.is_valid_directory && b.is_valid_textfile) {
200 return -1;174 return -1;
201 if (a.is_valid_textfile && b.is_valid_directory)175 }
176
177 if (a.is_valid_textfile && b.is_valid_directory) {
202 return 1;178 return 1;
179 }
180
203 return strcmp (a.path.collate_key_for_filename (),181 return strcmp (a.path.collate_key_for_filename (),
204 b.path.collate_key_for_filename ());182 b.path.collate_key_for_filename ());
205 }183 }
206184
=== added file 'plugins/folder-manager/FileItem.vala'
--- plugins/folder-manager/FileItem.vala 1970-01-01 00:00:00 +0000
+++ plugins/folder-manager/FileItem.vala 2016-11-30 23:31:32 +0000
@@ -0,0 +1,39 @@
1// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
2/*-
3 * Copyright (c) 2016 elementary LLC. (https://elementary.io)
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Authored by: Corentin Noël <corentin@elementary.io>, Andrei-Costin Zisu <matzipan@gmail.com>
19 */
20
21public class Scratch.Plugins.FolderManager.FileItem : Item {
22 public FileItem (Scratch.Plugins.FolderManager.File file, Scratch.Plugins.FolderManager.FileView view) requires (file.is_valid_textfile) {
23 Object (file: file, view: view);
24 }
25
26 public override Gtk.Menu? get_context_menu () {
27 var menu = new Gtk.Menu ();
28 var rename_item = new Gtk.MenuItem.with_label (_("Rename"));
29 rename_item.activate.connect (() => view.start_editing_item (this));
30 menu.append (rename_item);
31
32 var delete_item = new Gtk.MenuItem.with_label (_("Move to Trash"));
33 delete_item.activate.connect (() => do_remove ());
34 menu.append (delete_item);
35
36 menu.show_all ();
37 return menu;
38 }
39}
040
=== modified file 'plugins/folder-manager/FileView.vala'
--- plugins/folder-manager/FileView.vala 2015-09-16 01:11:55 +0000
+++ plugins/folder-manager/FileView.vala 2016-11-30 23:31:32 +0000
@@ -1,22 +1,22 @@
1// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-1// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
2/***2/*-
3 BEGIN LICENSE3 * Copyright (c) 2016 elementary LLC. (https://elementary.io)
44 *
5 Copyright (C) 2013 Julien Spautz <spautz.julien@gmail.com>5 * This program is free software: you can redistribute it and/or modify
6 This program is free software: you can redistribute it and/or modify it6 * it under the terms of the GNU General Public License as published by
7 under the terms of the GNU Lesser General Public License version 3, as published7 * the Free Software Foundation, either version 3 of the License, or
8 by the Free Software Foundation.8 * (at your option) any later version.
99 *
10 This program is distributed in the hope that it will be useful, but10 * This program is distributed in the hope that it will be useful,
11 WITHOUT ANY WARRANTY; without even the implied warranties of11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 PURPOSE. See the GNU General Public License for more details.13 * GNU General Public License for more details.
1414 *
15 You should have received a copy of the GNU General Public License along15 * You should have received a copy of the GNU General Public License
16 with this program. If not, see <http://www.gnu.org/licenses/>16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1717 *
18 END LICENSE18 * Authored by: Julien Spautz <spautz.julien@gmail.com>, Andrei-Costin Zisu <matzipan@gmail.com>
19***/19 */
2020
21namespace Scratch.Plugins.FolderManager {21namespace Scratch.Plugins.FolderManager {
22 Settings settings;22 Settings settings;
@@ -24,25 +24,31 @@
24 /**24 /**
25 * SourceList that displays folders and their contents.25 * SourceList that displays folders and their contents.
26 */26 */
27 internal class FileView : Granite.Widgets.SourceList {27 public class FileView : Granite.Widgets.SourceList {
2828 public signal void select (GLib.File file);
29 public signal void select (string file);29 public signal void welcome_visible (bool visible);
30
31 public FileView () {30 public FileView () {
32 this.width_request = 180;31 width_request = 180;
33 this.item_selected.connect ((item) => {32
34 select ((item as FileItem).path);33 item_selected.connect ((item) => {
34 if (item is FileItem) {
35 select ((item as FileItem).file.file);
36 }
37 });
38
39 root.child_removed.connect (() => {
40 this.selected = null;
41 if (root.n_children == 0) {
42 welcome_visible (true);
43 }
44
45 write_settings ();
35 });46 });
3647
37 settings = new Settings ();48 settings = new Settings ();
38 }49 }
3950
40 public void restore_saved_state () {51 public void open_folder (File folder, bool expand = true) {
41 foreach (var path in settings.opened_folders)
42 add_folder (new File (path), false);
43 }
44
45 public void open_folder (File folder) {
46 if (is_open (folder)) {52 if (is_open (folder)) {
47 warning ("Folder '%s' is already open.", folder.path);53 warning ("Folder '%s' is already open.", folder.path);
48 return;54 return;
@@ -51,64 +57,81 @@
51 return;57 return;
52 }58 }
5359
54 add_folder (folder, true);60 var folder_item = new FolderItem (folder, this);
61 root.add (folder_item);
62 welcome_visible (false);
63
64 folder_item.expanded = expand;
55 write_settings ();65 write_settings ();
56 }66 }
5767
58 private void add_folder (File folder, bool expand) {68 public void restore_settings () {
59 if (is_open (folder)) {69 foreach (var folder_path in settings.opened_folders) {
60 warning ("Folder '%s' is already open.", folder.path);70 if (folder_path == null) {
61 return;71 continue;
62 } else if (!folder.is_valid_directory) {72 }
63 warning ("Cannot open invalid directory.");73
64 return;74 var folder = new File (folder_path);
75 if (folder.is_valid_directory) {
76 open_folder (folder);
77 }
65 }78 }
66
67 var folder_root = new MainFolderItem (folder);
68 this.root.add (folder_root);
69
70 folder_root.expanded = expand;
71 folder_root.closed.connect (() => {
72 root.remove (folder_root);
73 write_settings ();
74 });
75 }79 }
7680
77 private bool is_open (File folder) {81 private bool is_open (File folder) {
78 foreach (var child in root.children)82 foreach (var child in root.children) {
79 if (folder.path == (child as Item).path)83 if (folder.path == (child as Item).path) {
80 return true;84 return true;
85 }
86 }
87
81 return false;88 return false;
82 }89 }
8390
84 private void write_settings () {91 private void write_settings () {
85 string[] to_save = {};92 string[] paths = {};
8693 foreach (var child in this.root.children) {
87 foreach (var main_folder in root.children) {94 if (child is FolderItem) {
88 var saved = false;95 paths += ((FolderItem) child).path;
89
90 foreach (var saved_folder in to_save) {
91 if ((main_folder as Item).path == saved_folder) {
92 saved = true;
93 break;
94 }
95 }
96
97 if (!saved) {
98 to_save += (main_folder as Item).path;
99 }96 }
100 }97 }
10198
102 settings.opened_folders = to_save;99 settings.opened_folders = paths;
103 }100 }
104 }101 }
105102
106 /**103 /**
107 * Common abstract class for file and filder items.104 * Common abstract class for normal and expandable items.
108 */105 */
109 internal class Item: Granite.Widgets.SourceList.ExpandableItem, Granite.Widgets.SourceListSortable {106 public class Item : Granite.Widgets.SourceList.ExpandableItem, Granite.Widgets.SourceListSortable {
110 public File file { get; construct; }107 public File file { get; construct; }
111 public string path { get { return file.path; } }108 public Scratch.Plugins.FolderManager.FileView view { get; construct; }
109 public string path {
110 get { return file.path; }
111 set { file.path = value; }
112 }
113
114 construct {
115 selectable = true;
116 editable = true;
117 update_item ();
118
119 edited.connect (rename);
120 file.notify["path"].connect(update_item);
121 }
122
123 protected void update_item () {
124 name = file.name;
125 icon = file.icon;
126 }
127
128 protected void rename (string new_name) {
129 file.rename (new_name);
130 }
131
132 protected void do_remove () {
133 file.trash ();
134 }
112135
113 public int compare (Granite.Widgets.SourceList.Item a, Granite.Widgets.SourceList.Item b) {136 public int compare (Granite.Widgets.SourceList.Item a, Granite.Widgets.SourceList.Item b) {
114 if (a is FolderItem && b is FileItem) {137 if (a is FolderItem && b is FileItem) {
@@ -120,193 +143,8 @@
120 return File.compare ((a as Item).file, (b as Item).file);143 return File.compare ((a as Item).file, (b as Item).file);
121 }144 }
122145
123 public bool allow_dnd_sorting () { 146 public bool allow_dnd_sorting () {
124 return false;147 return false;
125 }148 }
126 }149 }
127
128 /**
129 * Normal item in the source list, represents a textfile.
130 * TODO Remove, Rename
131 */
132 internal class FileItem : Item {
133
134 //Gtk.Menu menu;
135 //Gtk.MenuItem item_trash;
136
137 public FileItem (File file) requires (file.is_valid_textfile) {
138 Object (file: file);
139
140 this.selectable = true;
141 //this.editable = true;
142 this.name = file.name;
143 this.icon = file.icon;
144 }
145
146 /*public void rename (string new_name) {
147 file.rename (new_name);
148 }*/
149
150 /*public override Gtk.Menu? get_context_menu () {
151 menu = new Gtk.Menu ();
152 item_trash = new Gtk.MenuItem.with_label (_("Move to Trash"));
153 menu.append (item_trash);
154 item_trash.activate.connect (() => { file.trash (); });
155 menu.show_all ();
156 return menu;
157 }*/
158 }
159
160 /**
161 * Expandable item in the source list, represents a folder.
162 * Monitored for changes inside the directory.
163 * TODO remove, rename, create new file
164 */
165 internal class FolderItem : Item {
166
167 //Gtk.Menu menu;
168 //Gtk.MenuItem item_trash;
169 //Gtk.MenuItem item_create;
170
171 private GLib.FileMonitor monitor;
172 private bool children_loaded = false;
173
174 public FolderItem (File file) requires (file.is_valid_directory) {
175 Object (file: file);
176
177 this.editable = false;
178 this.selectable = false;
179 this.name = file.name;
180 this.icon = file.icon;
181
182 this.add (new Granite.Widgets.SourceList.Item ("")); // dummy
183 this.toggled.connect (() => {
184 if (this.expanded && this.n_children <= 1) {
185 this.clear ();
186 this.add_children ();
187 children_loaded = true;
188 }
189 });
190
191 try {
192 monitor = file.file.monitor_directory (GLib.FileMonitorFlags.NONE);
193 monitor.changed.connect ((s,d,e) => { on_changed (s,d,e); });
194 } catch (GLib.Error e) {
195 warning (e.message);
196 }
197 }
198
199 /*public override Gtk.Menu? get_context_menu () {
200 menu = new Gtk.Menu ();
201 item_trash = new Gtk.MenuItem.with_label (_("Move to Trash"));
202 item_create = new Gtk.MenuItem.with_label (_("Create new File"));
203 menu.append (item_trash);
204 menu.append (item_create);
205 item_trash.activate.connect (() => { file.trash (); });
206 item_create.activate.connect (() => {
207 var new_file = GLib.File.new_for_path (file.path + "/new File");
208
209 try {
210 FileOutputStream os = new_file.create (FileCreateFlags.NONE);
211 } catch (Error e) {
212 warning ("Error: %s\n", e.message);
213 }
214 });
215 menu.show_all ();
216 return menu;
217 }*/
218
219 internal void add_children () {
220 foreach (var child in file.children) {
221 if (child.is_valid_directory) {
222 var item = new FolderItem (child);
223 add (item);
224 } else if (child.is_valid_textfile) {
225 var item = new FileItem (child);
226 add (item);
227 //item.edited.connect (item.rename);
228 }
229 }
230 }
231
232 private void on_changed (GLib.File source, GLib.File? dest, GLib.FileMonitorEvent event) {
233
234 if (!children_loaded) {
235 this.file.reset_cache ();
236 return;
237 }
238
239 switch (event) {
240 case GLib.FileMonitorEvent.DELETED:
241 var children_tmp = new Gee.ArrayList<Granite.Widgets.SourceList.Item> ();
242 children_tmp.add_all (children);
243 foreach (var item in children_tmp) {
244 if ((item as Item).path == source.get_path ()) {
245 remove (item);
246 }
247 }
248
249 break;
250 case GLib.FileMonitorEvent.CREATED:
251 if (source.query_exists () == false) {
252 return;
253 }
254
255 var file = new File (source.get_path ());
256 var exists = false;
257 foreach (var item in children) {
258 if ((item as Item).path == file.path) {
259 exists = true;
260 break;
261 }
262 }
263
264 if (!exists) {
265 if (file.is_valid_textfile) {
266 this.add (new FileItem (file));
267 } else if (file.is_valid_directory) {
268 this.add (new FolderItem (file));
269 }
270 }
271
272 break;
273 }
274 }
275 }
276
277 /**
278 * Special root folder.
279 * TODO rename, create new file
280 */
281 internal class MainFolderItem : FolderItem {
282 public signal void closed ();
283
284 Gtk.Menu menu;
285 Gtk.MenuItem item_close;
286 //Gtk.MenuItem item_create;
287
288 public MainFolderItem (File file) requires (file.is_valid_directory) {
289 base (file);
290 }
291
292 public override Gtk.Menu? get_context_menu () {
293 menu = new Gtk.Menu ();
294 item_close = new Gtk.MenuItem.with_label (_("Close Folder"));
295 //item_create = new Gtk.MenuItem.with_label (_("Create new File"));
296 menu.append (item_close);
297 //menu.append (item_create);
298 item_close.activate.connect (() => { closed (); });
299 /*item_create.activate.connect (() => {
300 var new_file = GLib.File.new_for_path (file.path + "/new File");
301
302 try {
303 FileOutputStream os = new_file.create (FileCreateFlags.NONE);
304 } catch (Error e) {
305 warning ("Error: %s\n", e.message);
306 }
307 });*/
308 menu.show_all ();
309 return menu;
310 }
311 }
312}150}
313151
=== added file 'plugins/folder-manager/FolderItem.vala'
--- plugins/folder-manager/FolderItem.vala 1970-01-01 00:00:00 +0000
+++ plugins/folder-manager/FolderItem.vala 2016-11-30 23:31:32 +0000
@@ -0,0 +1,196 @@
1// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
2/*-
3 * Copyright (c) 2016 elementary LLC. (https://elementary.io)
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Authored by: Corentin Noël <corentin@elementary.io>, Andrei-Costin Zisu <matzipan@gmail.com>
19 */
20
21public class Scratch.Plugins.FolderManager.FolderItem : Item {
22 public signal void folder_open (GLib.File folder);
23
24 private GLib.FileMonitor monitor;
25 private bool children_loaded = false;
26
27 public FolderItem (Scratch.Plugins.FolderManager.File file, Scratch.Plugins.FolderManager.FileView view) requires (file.is_valid_directory) {
28 Object (file: file, view: view);
29 }
30
31 construct {
32 if (file.children.length () > 0) {
33 add (new Granite.Widgets.SourceList.Item ("")); // dummy
34 }
35
36 toggled.connect (() => {
37 if (expanded && n_children <= 1) {
38 clear ();
39 add_children ();
40 children_loaded = true;
41 }
42 });
43
44 try {
45 monitor = file.file.monitor_directory (GLib.FileMonitorFlags.WATCH_MOVES);
46 monitor.changed.connect (on_changed);
47 } catch (GLib.Error e) {
48 warning (e.message);
49 }
50 }
51
52 public override Gtk.Menu? get_context_menu () {
53 var menu = new Gtk.Menu ();
54 if (parent == view.root) {
55 var item = new Gtk.MenuItem.with_label (_("Close Folder"));
56 item.activate.connect (() => {
57 monitor.cancel ();
58 parent.remove (this);
59 });
60 menu.append (item);
61 } else {
62 var item = new Gtk.MenuItem.with_label (_("Open"));
63 item.activate.connect (() => folder_open (file.file));
64 menu.append (item);
65 }
66
67 var rename_item = new Gtk.MenuItem.with_label (_("Rename"));
68 rename_item.activate.connect (() => view.start_editing_item (this));
69 menu.append (rename_item);
70
71 var new_file_item = new Gtk.MenuItem.with_label (_("Add file"));
72 new_file_item.activate.connect (() => add_file ());
73 menu.append (new_file_item);
74
75 var new_folder_item = new Gtk.MenuItem.with_label (_("Add folder"));
76 new_folder_item.activate.connect(() => add_folder ());
77 menu.append (new_folder_item);
78
79 var delete_item = new Gtk.MenuItem.with_label (_("Move to Trash"));
80 delete_item.activate.connect (() => do_remove ());
81 menu.append (delete_item);
82
83 menu.show_all ();
84 return menu;
85 }
86
87 internal void add_children () {
88 foreach (var child in file.children) {
89 if (child.is_valid_directory) {
90 var item = new FolderItem (child, view);
91 item.folder_open.connect (() => {
92 view.open_folder (child);
93 });
94
95 add (item);
96 } else if (child.is_valid_textfile) {
97 var item = new FileItem (child, view);
98 add (item);
99 }
100 }
101 }
102
103 private void on_changed (GLib.File source, GLib.File? dest, GLib.FileMonitorEvent event) {
104 if (!children_loaded) {
105 return;
106 }
107
108 switch (event) {
109 case GLib.FileMonitorEvent.RENAMED:
110 foreach (var child in children) {
111 var item = child as Item;
112 if (item.path == source.get_path ()) {
113 item.path = dest.get_path ();
114 break;
115 }
116 }
117 break;
118 case GLib.FileMonitorEvent.DELETED:
119 var children_tmp = new Gee.ArrayList<Granite.Widgets.SourceList.Item> ();
120 children_tmp.add_all (children);
121 foreach (var item in children_tmp) {
122 if ((item as Item).path == source.get_path ()) {
123 remove (item);
124 }
125 }
126
127 break;
128 case GLib.FileMonitorEvent.CREATED:
129 if (source.query_exists () == false) {
130 return;
131 }
132
133 var file = new File (source.get_path ());
134 var exists = false;
135 foreach (var item in children) {
136 if ((item as Item).path == file.path) {
137 exists = true;
138 }
139 }
140
141 if (!exists) {
142 if (file.is_valid_textfile) {
143 add (new FileItem (file, view));
144 } else if (file.is_valid_directory) {
145 add (new FolderItem (file, view));
146 }
147 }
148
149 break;
150 }
151 }
152
153 private void add_folder () {
154 var child = file.file.get_child (_("New Folder"));
155
156 var n = 1;
157 while (child.query_exists ()) {
158 child = file.file.get_child (_("New Folder (%d)").printf (n));
159 n++;
160 }
161
162 try {
163 child.make_directory ();
164
165 if (children_loaded) {
166 var item = new FolderItem (new File (child.get_path ()), view);
167 add (item);
168 view.start_editing_item (item);
169 }
170 } catch (Error e) {
171 warning (e.message);
172 }
173 }
174
175 private void add_file () {
176 var child = file.file.get_child (_("New File"));
177
178 var n = 1;
179 while (child.query_exists ()) {
180 child = file.file.get_child (_("New File (%d)").printf (n));
181 n++;
182 }
183
184 try {
185 child.create (FileCreateFlags.NONE);
186
187 if (children_loaded) {
188 var item = new FileItem (new File (child.get_path ()), view);
189 add (item);
190 view.start_editing_item (item);
191 }
192 } catch (Error e) {
193 warning (e.message);
194 }
195 }
196}
0197
=== modified file 'plugins/folder-manager/FolderManagerPlugin.vala'
--- plugins/folder-manager/FolderManagerPlugin.vala 2015-12-01 11:37:53 +0000
+++ plugins/folder-manager/FolderManagerPlugin.vala 2016-11-30 23:31:32 +0000
@@ -23,13 +23,10 @@
2323
24namespace Scratch.Plugins {24namespace Scratch.Plugins {
25 public class FolderManagerPlugin : Peas.ExtensionBase, Peas.Activatable {25 public class FolderManagerPlugin : Peas.ExtensionBase, Peas.Activatable {
2626 public Scratch.Services.Interface plugins;
27 Gtk.Button button;
27 FolderManager.FileView view;28 FolderManager.FileView view;
28 Gtk.ToolButton tool_button;29
29
30 int index = 0;
31
32 Scratch.Services.Interface plugins;
33 public Object object { owned get; construct; }30 public Object object { owned get; construct; }
3431
35 public FolderManagerPlugin () {32 public FolderManagerPlugin () {
@@ -43,74 +40,71 @@
43 }40 }
4441
45 public void deactivate () {42 public void deactivate () {
46 if (view != null)43 if (view != null) {
47 view.destroy();44 view.destroy();
48 if (tool_button != null) {45 view = null;
49 //(tool_button.parent as Scratch.Widgets.Toolbar).open_button.visible = true;46 }
50 tool_button.destroy ();47
48 if (button != null) {
49 button.destroy();
50 button = null;
51 }51 }
52 }52 }
5353
54 public void update_state () {54 public void update_state () {
55
55 }56 }
5657
57 void on_hook_sidebar (Gtk.Notebook notebook) {58 void on_hook_sidebar (Gtk.Notebook notebook) {
58 if (view != null)59 if (view != null) {
59 return;60 return;
61 }
6062
63 // File View
61 view = new FolderManager.FileView ();64 view = new FolderManager.FileView ();
6265 view.select.connect ((file) => plugins.open_file (file));
63 view.select.connect ((a) => {66 view.welcome_visible.connect ((visible) => {
64 var file = GLib.File.new_for_path (a);67 if (visible) {
65 plugins.open_file (file);68 view.parent.remove (view);
66 });69 } else if (view.parent == null) {
6770 view.show_all ();
68 view.root.child_added.connect (() => {71 var icon = new Gtk.Image.from_icon_name ("folder-symbolic", Gtk.IconSize.MENU);
69 if (view.get_n_visible_children (view.root) == 0) {72 icon.tooltip_text = _("File Manager");
70 index = notebook.append_page (view, new Gtk.Label (_("Folders")));73 notebook.append_page (view , icon);
71 }74 }
72 });75 });
7376
74 view.root.child_removed.connect (() => {77 view.restore_settings ();
75 if (view.get_n_visible_children (view.root) == 1)
76 notebook.remove_page (index);
77 });
78
79 view.restore_saved_state ();
80 }78 }
8179
82 void on_hook_toolbar (Gtk.HeaderBar toolbar) {80 void on_hook_toolbar (Gtk.HeaderBar toolbar) {
83 if (tool_button != null)81 if (button != null) {
84 return;82 return;
8583 }
86 //(toolbar as Scratch.Widgets.Toolbar).open_button.visible = false;84
87 var icon = new Gtk.Image.from_icon_name ("folder-saved-search", Gtk.IconSize.LARGE_TOOLBAR);85 button = new Gtk.Button.from_icon_name ("folder-saved-search", Gtk.IconSize.LARGE_TOOLBAR);
88 tool_button = new Gtk.ToolButton (icon, _("Open a folder"));86 button.tooltip_text = _("Open a folder");
89 tool_button.tooltip_text = _("Open a folder");87 button.clicked.connect (() => open_dialog ());
90 tool_button.clicked.connect (() => {88 button.show_all ();
91 Gtk.Window window = plugins.manager.window;89 toolbar.pack_start (button);
92 Gtk.FileChooserDialog chooser = new Gtk.FileChooserDialog (90 }
93 "Select a folder.", window, Gtk.FileChooserAction.SELECT_FOLDER,91
94 _("_Cancel"), Gtk.ResponseType.CANCEL,92 private void open_dialog () {
95 _("_Open"), Gtk.ResponseType.ACCEPT);93 Gtk.Window window = plugins.manager.window;
96 chooser.select_multiple = true;94 Gtk.FileChooserDialog chooser = new Gtk.FileChooserDialog (
9795 "Select a folder.", window, Gtk.FileChooserAction.SELECT_FOLDER,
98 if (chooser.run () == Gtk.ResponseType.ACCEPT) {96 _("_Cancel"), Gtk.ResponseType.CANCEL,
99 SList<string> uris = chooser.get_uris ();97 _("_Open"), Gtk.ResponseType.ACCEPT);
100 foreach (unowned string uri in uris) {98 chooser.select_multiple = true;
101 var folder = new FolderManager.File (uri.replace ("file:///", "/"));99
102 view.open_folder (folder); // emit signal100 if (chooser.run () == Gtk.ResponseType.ACCEPT) {
103 }101 chooser.get_files ().foreach ((file) => {
104 }102 var folder = new Scratch.Plugins.FolderManager.File (file.get_path ());
105103 view.open_folder (folder);
106 chooser.close ();104 });
107 });105 }
108106
109 icon.show ();107 chooser.close ();
110 tool_button.show ();
111
112 toolbar.pack_start (tool_button);
113 //toolbar.insert (tool_button, 1);
114 }108 }
115 }109 }
116}110}
117111
=== modified file 'plugins/folder-manager/folder-manager.plugin'
--- plugins/folder-manager/folder-manager.plugin 2013-06-08 15:17:42 +0000
+++ plugins/folder-manager/folder-manager.plugin 2016-11-30 23:31:32 +0000
@@ -3,8 +3,8 @@
3Loader=C3Loader=C
4IAge=24IAge=2
5Name=Folder Manager5Name=Folder Manager
6Description=Basic folder manager with file browsing6Description=Browse files and directories
7Icon=folder-saved-search7Icon=system-file-manager
8Authors=Julien Spautz <spautz.julien@gmail.com>8Authors=Mario Guerriero, Julien Spautz, Corentin Noël, Andrei Zisu
9Copyright=Copyright © 2013 Scratch Developers9Copyright=Copyright © 2016 elementary LLC
10Website=http://launchpad.net/scratch10Website=http://launchpad.net/scratch
1111
=== modified file 'schemas/CMakeLists.txt'
--- schemas/CMakeLists.txt 2015-09-10 00:54:45 +0000
+++ schemas/CMakeLists.txt 2016-11-30 23:31:32 +0000
@@ -1,6 +1,5 @@
1include(GSettings)1include(GSettings)
2add_schema("org.pantheon.scratch.gschema.xml")2add_schema("org.pantheon.scratch.gschema.xml")
3add_schema("org.pantheon.scratch.plugins.folder-manager.gschema.xml")3add_schema("org.pantheon.scratch.plugins.folder-manager.gschema.xml")
4add_schema("org.pantheon.scratch.plugins.file-manager.gschema.xml")
5add_schema("org.pantheon.scratch.plugins.terminal.gschema.xml")4add_schema("org.pantheon.scratch.plugins.terminal.gschema.xml")
6add_schema("org.pantheon.scratch.plugins.spell.gschema.xml")5add_schema("org.pantheon.scratch.plugins.spell.gschema.xml")
76
=== removed file 'schemas/org.pantheon.scratch.plugins.file-manager.gschema.xml'
--- schemas/org.pantheon.scratch.plugins.file-manager.gschema.xml 2013-07-16 15:56:38 +0000
+++ schemas/org.pantheon.scratch.plugins.file-manager.gschema.xml 1970-01-01 00:00:00 +0000
@@ -1,10 +0,0 @@
1<?xml version="1.0" encoding="UTF-8"?>
2<schemalist>
3 <schema path="/org/pantheon/scratch/plugins/file-manager/" id="org.pantheon.scratch.plugins.file-manager" gettext-domain="scratch">
4 <key name="opened-folder" type="s">
5 <default>''</default>
6 <summary>Opened folder.</summary>
7 <description>Opened folder that should be restored in startup.</description>
8 </key>
9 </schema>
10</schemalist>

Subscribers

People subscribed via source and target branches