2021-11-22 22:24:44 +00:00
|
|
|
# Python imports
|
2021-11-26 22:50:58 +00:00
|
|
|
import os
|
2021-11-22 22:24:44 +00:00
|
|
|
|
|
|
|
# Lib imports
|
|
|
|
from gi.repository import GObject, Gio
|
|
|
|
|
|
|
|
# Application imports
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WidgetFileActionMixin:
|
|
|
|
def set_file_watcher(self, view):
|
|
|
|
if view.get_dir_watcher():
|
|
|
|
watcher = view.get_dir_watcher()
|
|
|
|
watcher.cancel()
|
|
|
|
if debug:
|
|
|
|
print(f"Watcher Is Cancelled: {watcher.is_cancelled()}")
|
|
|
|
|
|
|
|
dir_watcher = Gio.File.new_for_path(view.get_current_directory()) \
|
|
|
|
.monitor_directory(Gio.FileMonitorFlags.WATCH_MOVES,
|
|
|
|
Gio.Cancellable()
|
|
|
|
)
|
|
|
|
|
|
|
|
wid = view.get_wid()
|
|
|
|
tid = view.get_tab_id()
|
|
|
|
dir_watcher.connect("changed", self.dir_watch_updates, (f"{wid}|{tid}",))
|
|
|
|
view.set_dir_watcher(dir_watcher)
|
|
|
|
|
|
|
|
def dir_watch_updates(self, file_monitor, file, other_file=None, eve_type=None, data=None):
|
2021-11-30 02:42:14 +00:00
|
|
|
if eve_type in [Gio.FileMonitorEvent.CREATED, Gio.FileMonitorEvent.DELETED,
|
|
|
|
Gio.FileMonitorEvent.RENAMED, Gio.FileMonitorEvent.MOVED_IN,
|
|
|
|
Gio.FileMonitorEvent.MOVED_OUT]:
|
2021-11-22 22:24:44 +00:00
|
|
|
wid, tid = data[0].split("|")
|
|
|
|
notebook = self.builder.get_object(f"window_{wid}")
|
|
|
|
view = self.get_fm_window(wid).get_view_by_id(tid)
|
|
|
|
iconview = self.builder.get_object(f"{wid}|{tid}|iconview")
|
|
|
|
store = iconview.get_model()
|
|
|
|
_store, tab_label = self.get_store_and_label_from_notebook(notebook, f"{wid}|{tid}")
|
|
|
|
|
|
|
|
view.load_directory()
|
|
|
|
self.load_store(view, store)
|
|
|
|
tab_label.set_label(view.get_end_of_path())
|
2021-11-28 04:03:00 +00:00
|
|
|
self.set_bottom_labels(view)
|
2021-11-22 22:24:44 +00:00
|
|
|
|
2021-11-26 22:50:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-11-22 22:24:44 +00:00
|
|
|
def create_file(self):
|
2021-11-26 22:50:58 +00:00
|
|
|
fname_field = self.builder.get_object("context_menu_fname")
|
|
|
|
file_name = fname_field.get_text().strip()
|
|
|
|
type = self.builder.get_object("context_menu_type_toggle").get_state()
|
2021-11-22 22:24:44 +00:00
|
|
|
|
2021-11-26 22:50:58 +00:00
|
|
|
wid, tid = self.window_controller.get_active_data()
|
|
|
|
view = self.get_fm_window(wid).get_view_by_id(tid)
|
|
|
|
target = f"{view.get_current_directory()}"
|
2021-11-22 22:24:44 +00:00
|
|
|
|
2021-11-29 22:49:56 +00:00
|
|
|
if file_name:
|
2021-11-22 22:24:44 +00:00
|
|
|
file_name = "file://" + target + "/" + file_name
|
|
|
|
if type == True: # Create File
|
2021-11-29 22:49:56 +00:00
|
|
|
self.handle_file([file_name], "create_file")
|
2021-11-22 22:24:44 +00:00
|
|
|
else: # Create Folder
|
|
|
|
self.handle_file([file_name], "create_dir")
|
|
|
|
|
2021-11-26 22:50:58 +00:00
|
|
|
fname_field.set_text("")
|
2021-11-22 22:24:44 +00:00
|
|
|
|
2021-11-30 02:42:14 +00:00
|
|
|
|
|
|
|
def get_current_state(self):
|
|
|
|
wid, tid = self.window_controller.get_active_data()
|
|
|
|
view = self.get_fm_window(wid).get_view_by_id(tid)
|
|
|
|
iconview = self.builder.get_object(f"{wid}|{tid}|iconview")
|
|
|
|
store = iconview.get_model()
|
|
|
|
return wid, tid, view, iconview, store
|
|
|
|
|
2021-11-26 06:53:20 +00:00
|
|
|
def open_files(self):
|
2021-11-30 02:42:14 +00:00
|
|
|
wid, tid, view, iconview, store = self.get_current_state()
|
|
|
|
uris = self.format_to_uris(store, wid, tid, self.selected_files, True)
|
2021-11-26 06:53:20 +00:00
|
|
|
|
|
|
|
for file in uris:
|
|
|
|
view.open_file_locally(file)
|
|
|
|
|
2021-11-30 02:42:14 +00:00
|
|
|
|
2021-11-27 06:51:31 +00:00
|
|
|
def open_with_files(self, appchooser_widget):
|
2021-11-30 02:42:14 +00:00
|
|
|
wid, tid, view, iconview, store = self.get_current_state()
|
2021-11-27 06:51:31 +00:00
|
|
|
uris = self.format_to_uris(store, wid, tid, self.selected_files)
|
2021-11-29 22:49:56 +00:00
|
|
|
file = Gio.File.new_for_uri(uris[0])
|
2021-11-27 06:51:31 +00:00
|
|
|
app_info = appchooser_widget.get_app_info()
|
2021-11-30 02:42:14 +00:00
|
|
|
|
2021-11-29 22:49:56 +00:00
|
|
|
app_info.launch([file], None)
|
2021-11-26 22:50:58 +00:00
|
|
|
|
|
|
|
def rename_files(self):
|
|
|
|
rename_label = self.builder.get_object("file_to_rename_label")
|
|
|
|
rename_input = self.builder.get_object("new_rename_fname")
|
2021-11-30 02:42:14 +00:00
|
|
|
wid, tid, view, iconview, store = self.get_current_state()
|
2021-11-27 06:51:31 +00:00
|
|
|
uris = self.format_to_uris(store, wid, tid, self.to_rename_files)
|
2021-11-26 22:50:58 +00:00
|
|
|
|
2021-11-27 06:51:31 +00:00
|
|
|
# The rename button hides the rename dialog box which lets the loop continue.
|
2021-11-26 22:50:58 +00:00
|
|
|
# Weirdly, the show at the end is needed to flow through all the list properly
|
|
|
|
# than auto chosing the first rename entry you do.
|
|
|
|
for uri in uris:
|
|
|
|
entry = uri.split("/")[-1]
|
|
|
|
rename_label.set_label(entry)
|
|
|
|
rename_input.set_text(entry)
|
|
|
|
if self.skip_edit:
|
|
|
|
self.skip_edit = False
|
|
|
|
self.show_edit_file_menu()
|
|
|
|
|
2021-11-27 06:51:31 +00:00
|
|
|
# Yes...this step is required even with the above... =/
|
2021-11-26 22:50:58 +00:00
|
|
|
self.show_edit_file_menu()
|
|
|
|
|
|
|
|
if self.skip_edit:
|
|
|
|
continue
|
|
|
|
if self.cancel_edit:
|
|
|
|
break
|
|
|
|
|
|
|
|
rname_to = rename_input.get_text().strip()
|
|
|
|
target = f"file://{view.get_current_directory()}/{rname_to}"
|
2021-11-27 06:51:31 +00:00
|
|
|
self.handle_file([uri], "edit", target)
|
2021-11-26 22:50:58 +00:00
|
|
|
|
|
|
|
self.show_edit_file_menu()
|
|
|
|
|
|
|
|
self.skip_edit = False
|
|
|
|
self.cancel_edit = False
|
|
|
|
self.hide_new_file_menu()
|
|
|
|
self.to_rename_files.clear()
|
|
|
|
|
2021-11-29 08:01:51 +00:00
|
|
|
def archive_files(self, archiver_dialogue):
|
2021-11-30 02:42:14 +00:00
|
|
|
wid, tid, view, iconview, store = self.get_current_state()
|
|
|
|
paths = self.format_to_uris(store, wid, tid, self.selected_files)
|
2021-11-29 08:01:51 +00:00
|
|
|
|
|
|
|
save_target = archiver_dialogue.get_filename();
|
|
|
|
start_itr, end_itr = self.arc_command_buffer.get_bounds()
|
|
|
|
command = self.arc_command_buffer.get_text(start_itr, end_itr, False)
|
|
|
|
|
|
|
|
command = command.replace("%o", save_target)
|
|
|
|
command = command.replace("%N", ' '.join(paths))
|
|
|
|
final_command = f"terminator -e '{command}'"
|
|
|
|
self.execute(final_command, start_dir=None, use_os_system=True)
|
2021-11-26 22:50:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cut_files(self):
|
2021-11-30 02:42:14 +00:00
|
|
|
wid, tid, view, iconview, store = self.get_current_state()
|
2021-11-22 22:24:44 +00:00
|
|
|
uris = self.format_to_uris(store, wid, tid, self.selected_files)
|
2021-11-26 22:50:58 +00:00
|
|
|
self.to_cut_files = uris
|
2021-11-22 22:24:44 +00:00
|
|
|
|
2021-11-26 22:50:58 +00:00
|
|
|
def copy_files(self):
|
2021-11-30 02:42:14 +00:00
|
|
|
wid, tid, view, iconview, store = self.get_current_state()
|
2021-11-22 22:24:44 +00:00
|
|
|
uris = self.format_to_uris(store, wid, tid, self.selected_files)
|
2021-11-26 22:50:58 +00:00
|
|
|
self.to_copy_files = uris
|
2021-11-22 22:24:44 +00:00
|
|
|
|
|
|
|
def paste_files(self):
|
|
|
|
wid, tid = self.window_controller.get_active_data()
|
|
|
|
view = self.get_fm_window(wid).get_view_by_id(tid)
|
2021-11-29 22:49:56 +00:00
|
|
|
target = f"file://{view.get_current_directory()}"
|
2021-11-22 22:24:44 +00:00
|
|
|
|
|
|
|
if len(self.to_copy_files) > 0:
|
|
|
|
self.handle_file(self.to_copy_files, "copy", target)
|
|
|
|
elif len(self.to_cut_files) > 0:
|
|
|
|
self.handle_file(self.to_cut_files, "move", target)
|
|
|
|
|
|
|
|
|
2021-11-26 22:50:58 +00:00
|
|
|
|
|
|
|
|
2021-11-29 22:49:56 +00:00
|
|
|
def move_files(self, files, target):
|
|
|
|
self.handle_file(files, "move", target)
|
2021-11-22 22:24:44 +00:00
|
|
|
|
|
|
|
def delete_files(self):
|
2021-11-30 02:42:14 +00:00
|
|
|
wid, tid, view, iconview, store = self.get_current_state()
|
2021-11-22 22:24:44 +00:00
|
|
|
uris = self.format_to_uris(store, wid, tid, self.selected_files)
|
|
|
|
self.handle_file(uris, "delete")
|
|
|
|
|
|
|
|
def trash_files(self):
|
2021-11-30 02:42:14 +00:00
|
|
|
wid, tid, view, iconview, store = self.get_current_state()
|
2021-11-22 22:24:44 +00:00
|
|
|
uris = self.format_to_uris(store, wid, tid, self.selected_files)
|
|
|
|
self.handle_file(uris, "trash")
|
|
|
|
|
|
|
|
|
2021-11-26 22:50:58 +00:00
|
|
|
|
|
|
|
|
2021-11-29 22:49:56 +00:00
|
|
|
# NOTE: While not fully race condition proof, we happy path it first
|
|
|
|
# and then handle anything after as a conflict for renaming before
|
|
|
|
# copy, move, or edit.
|
2021-11-26 22:50:58 +00:00
|
|
|
def handle_file(self, paths, action, _target_path=None):
|
2021-11-29 22:49:56 +00:00
|
|
|
paths = self.preprocess_paths(paths)
|
|
|
|
target = None
|
2021-11-22 22:24:44 +00:00
|
|
|
|
|
|
|
for path in paths:
|
|
|
|
try:
|
2021-11-29 22:49:56 +00:00
|
|
|
file = Gio.File.new_for_uri(path)
|
|
|
|
|
|
|
|
if action == "trash":
|
|
|
|
file.trash(cancellable=None)
|
|
|
|
|
|
|
|
if action == "delete":
|
|
|
|
# TODO: Add proper confirmation prompt befor doing either
|
|
|
|
type = file.query_file_type(flags=Gio.FileQueryInfoFlags.NONE)
|
|
|
|
|
|
|
|
if type == Gio.FileType.DIRECTORY:
|
|
|
|
wid, tid = self.window_controller.get_active_data()
|
|
|
|
view = self.get_fm_window(wid).get_view_by_id(tid)
|
|
|
|
view.delete_file( file.get_path() )
|
|
|
|
else:
|
|
|
|
file.delete(cancellable=None)
|
2021-11-22 22:24:44 +00:00
|
|
|
|
2021-11-29 22:49:56 +00:00
|
|
|
if (action == "create_file" or action == "create_dir") and not file.query_exists():
|
|
|
|
if action == "create_file":
|
|
|
|
file.create(flags=Gio.FileCreateFlags.NONE, cancellable=None)
|
|
|
|
continue
|
|
|
|
if action == "create_dir":
|
|
|
|
file.make_directory(cancellable=None)
|
|
|
|
continue
|
2021-11-22 22:24:44 +00:00
|
|
|
|
|
|
|
|
2021-11-26 22:50:58 +00:00
|
|
|
if _target_path:
|
2021-11-29 22:49:56 +00:00
|
|
|
if os.path.isdir(_target_path.split("file://")[1]):
|
|
|
|
info = file.query_info("standard::display-name", 0, cancellable=None)
|
|
|
|
_target = f"{_target_path}/{info.get_display_name()}"
|
2021-11-27 06:51:31 +00:00
|
|
|
target = Gio.File.new_for_uri(_target)
|
2021-11-26 22:50:58 +00:00
|
|
|
else:
|
|
|
|
target = Gio.File.new_for_uri(_target_path)
|
2021-11-22 22:24:44 +00:00
|
|
|
|
2021-11-29 22:49:56 +00:00
|
|
|
if target and not target.query_exists():
|
|
|
|
type = file.query_file_type(flags=Gio.FileQueryInfoFlags.NONE)
|
|
|
|
|
|
|
|
if type == Gio.FileType.DIRECTORY:
|
|
|
|
wid, tid = self.window_controller.get_active_data()
|
|
|
|
view = self.get_fm_window(wid).get_view_by_id(tid)
|
|
|
|
fPath = file.get_path()
|
|
|
|
tPath = None
|
|
|
|
state = True
|
|
|
|
|
|
|
|
if action == "copy":
|
|
|
|
tPath = target.get_path()
|
|
|
|
view.copy_file(fPath, tPath)
|
|
|
|
if action == "move" or action == "edit":
|
|
|
|
tPath = target.get_parent().get_path()
|
|
|
|
view.move_file(fPath, tPath)
|
|
|
|
else:
|
|
|
|
if action == "copy":
|
|
|
|
file.copy(target, flags=Gio.FileCopyFlags.BACKUP, cancellable=None)
|
|
|
|
if action == "move" or action == "edit":
|
|
|
|
file.move(target, flags=Gio.FileCopyFlags.BACKUP, cancellable=None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# NOTE: Past here, we need to handle detected conflicts.
|
|
|
|
# Maybe create a collection of file and target pares
|
2021-11-29 23:11:37 +00:00
|
|
|
# that then get passed to a handler who calls show_exists_page?
|
2021-11-29 22:49:56 +00:00
|
|
|
# type = None
|
|
|
|
# gio_flag = None
|
|
|
|
# self.exists_alert.hide()
|
|
|
|
# if not state:
|
|
|
|
# raise GObject.GError("Failed to perform requested dir/file action!")
|
2021-11-22 22:24:44 +00:00
|
|
|
except GObject.GError as e:
|
2021-11-28 06:40:47 +00:00
|
|
|
raise OSError(e)
|
2021-11-22 22:24:44 +00:00
|
|
|
|
2021-11-29 22:49:56 +00:00
|
|
|
|
2021-11-22 22:24:44 +00:00
|
|
|
def preprocess_paths(self, paths):
|
|
|
|
if not isinstance(paths, list):
|
|
|
|
paths = [paths]
|
|
|
|
# Convert items such as pathlib paths to strings
|
|
|
|
paths = [path.__fspath__() if hasattr(path, "__fspath__") else path for path in paths]
|
|
|
|
return paths
|