Debian Codegraph @

src/logic/ItemManager.vala

raw
1 /*
2 * Copyright (c) 2017-2019 José Amuedo (https://github.com/spheras)
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /**
19 * @class
20 * Item Manager that represents an Icon of a File or Folder
21 */
22 public class DesktopFolder.ItemManager : Object, DragnDrop.DndView, Clipboard.ClipboardFile {
23 /** file name associated with this item */
24 private string file_name;
25 /** the File object associated with the item */
26 private File file;
27 /** the folder manager where this item is inserted */
28 private FolderManager folder;
29 /** whether the item is selected */
30 private bool selected;
31 /** the view associated with this manager */
32 private ItemView view;
33 /** drag and drop behaviour for this folder */
34 private DragnDrop.DndBehaviour dnd_behaviour = null;
35 /** flag to indicate that the file doesn't exist.. and need to be rechecked */
36 private bool flag_dont_exist = false;
37
38 /**
39 * @constructor
40 * @param string file_name the name of the file/folder that this item represents
41 * @param File file the GLib File object for the file associated with this item
42 * @param FolderManager folder the folder parent
43 */
44 public ItemManager (string file_name, File file, FolderManager folder) {
45 this.file_name = file_name;
46 this.file = file;
47 // checking if the file still exists... we need to check following symlinks!
48 if (!GLib.FileUtils.test (file.get_path (), GLib.FileTest.EXISTS)) {
49 this.flag_dont_exist = true;
50 } else {
51 this.flag_dont_exist = false;
52 }
53 this.folder = folder;
54 this.selected = false;
55 this.view = new ItemView (this);
56 this.dnd_behaviour = new DragnDrop.DndBehaviour (this, true, this.is_folder ());
57 }
58
59 /**
60 * @name set_file
61 * @description set the file of the item
62 * @param Glib.File file the file associated with the item
63 */
64 public void set_file (File file) {
65 this.file = file;
66 }
67
68 /**
69 * @name is_selected
70 * @description check if the item is selected or not
71 * @return bool true->the item is selected
72 */
73 public bool is_selected () {
74 return this.selected;
75 }
76
77 /**
78 * @name check_exist_cached
79 * @description check if the file exist, but doesn't check fiscally, to avoid checking a lot of times
80 * @return {bool} true->, yes the file exist
81 */
82 public bool check_exist_cached () {
83 return !this.flag_dont_exist;
84 }
85
86 /**
87 * @name recheck_existence
88 * @description recheck if the file exist
89 */
90 public void recheck_existence () {
91 if (this.flag_dont_exist) {
92 // we need to recheck if the file exist now
93 // checking if the file still exists... we need to check following symlinks!
94 if (GLib.FileUtils.test (this.get_file ().get_path (), GLib.FileTest.EXISTS)) {
95 if (this.flag_dont_exist) {
96 this.flag_dont_exist = false;
97 this.view.refresh_icon ();
98 }
99 }
100 }
101 }
102
103 /**
104 * @name is_link
105 * @description check whether the item is a link to other folder/file or not
106 * @return {bool} true-> yes it is a link
107 */
108 public bool is_link () {
109 var file = this.get_file ();
110 var path = file.get_path ();
111 return FileUtils.test (path, FileTest.IS_SYMLINK);
112 }
113
114 /**
115 * @name is_executable
116 * @description check whether the item is a executable
117 * @return {bool} true-> yes it is a executable
118 */
119 public bool is_executable () {
120 var file = this.get_file ();
121 var path = file.get_path ();
122 if (!this.is_folder ()) {
123 return FileUtils.test (path, FileTest.IS_EXECUTABLE);
124 } else {
125 return false;
126 }
127 }
128
129 /**
130 * @name show_view
131 * @description show the icon
132 */
133 public void show_view () {
134 this.view.show_all ();
135 this.view.fade_in ();
136 }
137
138 /**
139 * @name hide_view
140 * @description hide the icon
141 */
142 public void hide_view () {
143 this.view.fade_out ();
144 Timeout.add (160, () => {
145 this.view.hide ();
146 return false;
147 });
148 }
149
150 /**
151 * @name select
152 * @description the item is selected
153 */
154 public void select_only () {
155 this.selected = true;
156 this.get_folder ().set_selected_item (this.view);
157 }
158
159 /**
160 * @name select_add
161 * @desription add this istem as selected
162 */
163 public void select_add () {
164 this.selected = true;
165 this.get_folder ().add_selected_item (this.view);
166 }
167
168 /**
169 * @name unselect
170 * @description unselect the item
171 */
172 public void unselect () {
173 this.selected = false;
174 this.get_folder ().remove_selected_item (this.view);
175 }
176
177 /**
178 * @name change_icon
179 * @description change the icon for the item
180 */
181 public void change_icon (string filename) {
182 ItemSettings is = this.folder.get_settings ().get_item (this.get_file_name ());
183 is.icon = filename;
184 this.folder.get_settings ().set_item (is);
185 this.folder.get_settings ().save ();
186 this.view.refresh_icon ();
187 }
188
189 /**
190 * @name get_settings
191 * @description return the settings of the item
192 * @return ItemSettings the settings
193 */
194 public ItemSettings get_settings () {
195 return this.folder.get_settings ().get_item (this.get_file_name ());
196 }
197
198 /**
199 * @name save_settings
200 * @description save the settings for the item
201 * @param {ItemSettings} is the new settings
202 */
203 public void save_settings (ItemSettings is) {
204 this.folder.get_settings ().set_item (is);
205 this.folder.get_settings ().save ();
206 }
207
208 /**
209 * @name rename
210 * @description rename the current item (file or folder)
211 * @param new_name string the new name for this item
212 * @return bool true->the operation was succesfull
213 */
214 public bool rename (string new_name) {
215 if (new_name.length <= 0) {
216 return false;
217 }
218 string old_name = this.file_name;
219 string old_path = this.get_absolute_path ();
220 this.file_name = new_name;
221 string new_path = this.folder.get_absolute_path () + "/" + new_name;
222
223 try {
224 this.folder.get_settings ().rename (old_name, new_name);
225 this.folder.get_settings ().save ();
226
227 FileUtils.rename (old_path, new_path);
228 this.file = File.new_for_path (new_path);
229
230 return true;
231 } catch (Error e) {
232 // we can't rename, undoing
233 this.file_name = old_name;
234 ItemSettings is = this.folder.get_settings ().get_item (new_name);
235 if (is == null) {
236 is = this.folder.get_settings ().get_item (old_name);
237 }
238 this.folder.get_settings ().set_item (is);
239 is.name = old_name;
240 this.folder.get_settings ().save ();
241
242 // showing the error
243 stderr.printf ("Error: %s\n", e.message);
244 Util.show_error_dialog ("Error", e.message);
245
246 return false;
247 }
248 }
249
250 /**
251 * @name save_position
252 * @description save a new position for the item icon at the folder settings
253 */
254 public void save_current_position () {
255 // the settings need to be modified
256 ItemSettings is = this.folder.get_settings ().get_item (this.file_name);
257 Gtk.Allocation allocation;
258 this.view.get_allocation (out allocation);
259 // HELP! don't know why these constants?? maybe padding??
260 is.x = allocation.x; // - ItemView.PADDING_X - ItemView.PADDING_X;
261 is.y = allocation.y; // - ItemView.PADDING_Y;
262 this.folder.get_settings ().set_item (is);
263 this.folder.get_settings ().save ();
264 }
265
266 /**
267 * @name is_desktop_file
268 * @description check whether the file is a desktop file or not
269 * @return true->yes, it is
270 */
271 public bool is_desktop_file () {
272 int index = this.file_name.index_of (".desktop", 0);
273 if (index > 0) {
274 return true;
275 }
276 return false;
277 }
278
279 /**
280 * @name is_openable_contenttype
281 * @description check whether the item file can be executable or not (if not, could be opened instead)
282 */
283 public bool is_openable_contenttype () {
284 bool uncertain = false;
285 string content_type = GLib.ContentType.guess (this.file_name, null, out uncertain);
286 bool executable = GLib.ContentType.can_be_executable (content_type);
287 debug ("content_type: %s -- %s", content_type, (executable ? "true" : "false"));
288 if (executable) {
289 return this.is_executable ();
290 } else if (content_type == "application/octet-stream") {
291 return this.is_executable ();
292 }
293 return false;
294 }
295
296 /**
297 * @name open_in_terminal
298 * @description open the folder item in a terminal (it is only called by folder items, see popup)
299 * @param string path the path of the folder to open
300 */
301 public void open_in_terminal (string path) {
302 try {
303 Environment.set_current_dir (path);
304 Process.spawn_command_line_async ("x-terminal-emulator --working-directory \"" + path + "\"");
305 } catch (Error e) {
306 stderr.printf ("Error: %s\n", e.message);
307 Util.show_error_dialog ("Error", e.message);
308 }
309 }
310
311 /**
312 * @name execute
313 * @description execute the file associated with this item
314 */
315 public void execute () {
316 // we must launch the file/folder
317 try {
318 if (this.is_desktop_file ()) {
319 GLib.DesktopAppInfo desktopApp = new GLib.DesktopAppInfo.from_filename (this.get_absolute_path ());
320 desktopApp.launch_uris (null, null);
321 } else if (this.is_openable_contenttype ()) {
322 var command = "\"" + this.get_absolute_path () + "\"";
323 var appinfo = AppInfo.create_from_commandline (command, null, AppInfoCreateFlags.NONE);
324 appinfo.launch_uris (null, null);
325 } else {
326 var command = "xdg-open \"" + this.folder.get_absolute_path () + "/" + this.file_name + "\"";
327 var appinfo = AppInfo.create_from_commandline (command, null, AppInfoCreateFlags.SUPPORTS_URIS);
328 appinfo.launch_uris (null, null);
329 }
330 } catch (Error e) {
331 stderr.printf ("Error: %s\n", e.message);
332 Util.show_error_dialog ("Error", e.message);
333 }
334 }
335
336 public void openwith (string filepath) {
337 // get content type
338 File file = File.new_for_path (filepath);
339 string file_content_type = "";
340 try {
341 file_content_type = file.query_info (
342 "*", FileQueryInfoFlags.NONE
343 ).get_content_type ();
344 } catch (Error e) {
345 file_content_type = "Unknown";
346 }
347 // open dialog
348 new DesktopFolder.Dialogs.OpenWith (
349 file_content_type, filepath
350 );
351 }
352
353 public void show_info (string filepath) {
354 string fname = get_file_name ();
355 string fpath = get_absolute_path ();
356 new DesktopFolder.Dialogs.ShowInfo (fpath, fname);
357 }
358
359 /**
360 * @name get_view
361 * @description return the view of this manager
362 * @return ItemView
363 */
364 public ItemView get_view () {
365 return this.view;
366 }
367
368 /**
369 * @name get_file_name
370 * @description return the file name associated with this item
371 * @return string the file name
372 */
373 public string get_file_name () {
374 return this.file_name;
375 }
376
377 /**
378 * @name get_file
379 * @description return the Glib.File associated
380 * @return File the file associated
381 */
382 public GLib.File get_file () {
383 return this.file;
384 }
385
386 /**
387 * @name get_absolute_path
388 * @description return the absolute path to this item
389 * @return string the absolute path
390 */
391 public string get_absolute_path () {
392 return this.get_file ().get_path ();
393 }
394
395 /**
396 * @name get_file_type
397 * @description return the FileType of this item
398 * @return FileType
399 */
400 public FileType get_file_type () {
401 File file = this.get_file ();
402 return file.query_file_type (FileQueryInfoFlags.NOFOLLOW_SYMLINKS);
403 }
404
405 /**
406 * @name is_folder
407 * @description check whether the item is a folder or not
408 * @return bool true->the item is a folder
409 */
410 public bool is_folder () {
411 return FileUtils.test (this.get_absolute_path (), FileTest.IS_DIR);
412 }
413
414 /**
415 * @name cut
416 * @description cut the file to the clipboard
417 */
418 public void cut () {
419 Clipboard.ClipboardManager cm = Clipboard.ClipboardManager.get_for_display ();
420 List <Clipboard.ClipboardFile> items = new List <Clipboard.ClipboardFile> ();
421 Gee.List <ItemView> selecteds = this.folder.get_selected_items ();
422 for (int i = 0; i < selecteds.size; i++) {
423 items.append (selecteds.@get (i).get_manager ());
424 }
425 cm.cut_files (items);
426 }
427
428 /**
429 * @name copy
430 * @description copy the file to the clipboard
431 */
432 public void copy () {
433 Clipboard.ClipboardManager cm = Clipboard.ClipboardManager.get_for_display ();
434 List <Clipboard.ClipboardFile> items = new List <Clipboard.ClipboardFile> ();
435 Gee.List <ItemView> selecteds = this.folder.get_selected_items ();
436 for (int i = 0; i < selecteds.size; i++) {
437 items.append (selecteds.@get (i).get_manager ());
438 }
439 cm.copy_files (items);
440 }
441
442 /**
443 * @name trash
444 * @description trash the file or folder associated
445 */
446 public void trash () {
447 if (this.folder.is_sync_running ()) {
448 return;
449 }
450
451 try {
452 this.unselect ();
453 if (this.is_folder ()) {
454 File file = File.new_for_path (this.get_absolute_path ());
455 file.trash ();
456 } else {
457 File file = File.new_for_path (this.get_absolute_path ());
458 file.trash ();
459 // FileUtils.remove(this.get_absolute_path());
460 }
461 this.on_delete ();
462 } catch (Error e) {
463 stderr.printf ("Error: %s\n", e.message);
464 Util.show_error_dialog ("Error", e.message);
465 }
466 }
467
468 /**
469 * @name trash_selected
470 * @descriptionsend to trash all the selected files
471 */
472 public void trash_selected () {
473 Gee.List <ItemView> selecteds = this.get_folder ().get_selected_items ();
474 ItemView[] to_delete = selecteds.to_array ();
475 for (int i = 0; i < to_delete.length; i++) {
476 ItemView view = to_delete[i];
477 if (view != null) {
478 view.get_manager ().trash ();
479 }
480 }
481 }
482
483 // ---------------------------------------------------------------------------------------
484 // ---------------------------DndView Implementation--------------------------------------
485 // ---------------------------------------------------------------------------------------
486
487 public void on_drag_motion () {
488 // we force the opened folder icon
489 this.view.force_opened_folder_icon ();
490 }
491
492 public void on_drag_leave () {
493 // restoring the normal icon
494 this.view.refresh_icon ();
495 }
496
497 /**
498 * @name get_widget
499 * @description return the widget associated with this view
500 * @return Widget the widget
501 */
502 public Gtk.Widget get_widget () {
503 return this.view;
504 }
505
506 /**
507 * @name get_application_window
508 * @description return the application window of this view, needed for drag operations
509 * @return ApplicationWindow
510 */
511 public Gtk.ApplicationWindow get_application_window () {
512 return this.folder.get_view ();
513 }
514
515 /**
516 * @name get_file_at
517 * @name get the file at the position x, y
518 * @return File
519 */
520 public GLib.File get_file_at (int x, int y) {
521 return this.get_file ();
522 }
523
524 /**
525 * @name is_writable
526 * @description indicates if the file linked by this view is writable or not
527 * @return bool
528 */
529 public bool is_writable () {
530 // TODO
531 return true;
532 }
533
534 /**
535 * @name get_target_location
536 * @description return the target File that represents this view
537 * @return File the file target of this view
538 */
539 public GLib.File get_target_location () {
540 return this.get_file ();
541 }
542
543 /**
544 * @name is_recent_uri_scheme
545 * @description check whether the File is a recent uri scheme?
546 * @return bool
547 */
548 public bool is_recent_uri_scheme () {
549 return true;
550 }
551
552 /**
553 * @name get_display_target_uri
554 * @description return the target uri of this view
555 * @return string the target uri
556 */
557 public string get_display_target_uri () {
558 return DragnDrop.Util.get_display_target_uri (this.get_file ());
559 }
560
561 /**
562 * @overrided
563 */
564 public Gtk.Image get_image () {
565 return this.view.get_image ();
566 }
567
568 /**
569 * @overrided
570 */
571 public void on_drag_end () {
572 this.view.on_drag_end ();
573 }
574
575 /**
576 * @overrided
577 */
578 public DragnDrop.DndView[] get_all_selected_views () {
579 Gee.List <ItemView> selected_views = this.get_folder ().get_selected_items ();
580 DragnDrop.DndView[] result = new DragnDrop.DndView[selected_views.size];
581 for (int i = 0; i < selected_views.size; i++) {
582 ItemView view = selected_views.@get (i);
583 if (view != null) {
584 result[i] = (DragnDrop.DndView)selected_views.@get (i).get_manager ();
585 }
586 }
587 return result;
588 }
589
590 // ---------------------------------------------------------------------------------------
591 // ---------------------------**********************--------------------------------------
592 // ---------------------------------------------------------------------------------------
593
594 /**
595 * @name get_folder
596 * @description return the folder that contains this item
597 * @return FolderManaget that contains the item.
598 */
599 public FolderManager get_folder () {
600 return this.folder;
601 }
602
603 }