SolarFM/plugins/trasher/plugin.py

139 lines
4.8 KiB
Python
Raw Normal View History

2022-10-01 21:04:46 +00:00
# Python imports
import os
import threading
import subprocess
import inspect
2022-10-01 21:04:46 +00:00
# Lib imports
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GLib
from gi.repository import Gio
2022-10-01 21:04:46 +00:00
# Application imports
from plugins.plugin_base import PluginBase
from .xdgtrash import XDGTrash
# NOTE: Threads WILL NOT die with parent's destruction.
def threaded(fn):
def wrapper(*args, **kwargs):
threading.Thread(target=fn, args=args, kwargs=kwargs, daemon=False).start()
return wrapper
# NOTE: Threads WILL die with parent's destruction.
def daemon_threaded(fn):
def wrapper(*args, **kwargs):
threading.Thread(target=fn, args=args, kwargs=kwargs, daemon=True).start()
return wrapper
class Plugin(PluginBase):
def __init__(self):
super().__init__()
self.path = os.path.dirname(os.path.realpath(__file__))
self._GLADE_FILE = f"{self.path}/trasher.glade"
self.name = "Trasher" # NOTE: Need to remove after establishing private bidirectional 1-1 message bus
# where self.name should not be needed for message comms
self.trashman = XDGTrash()
self.trash_files_path = f"{GLib.get_user_data_dir()}/Trash/files"
self.trash_info_path = f"{GLib.get_user_data_dir()}/Trash/info"
self.trashman.regenerate()
def run(self):
self._event_system.subscribe("show_trash_buttons", self._show_trash_buttons)
self._event_system.subscribe("hide_trash_buttons", self._hide_trash_buttons)
self._event_system.subscribe("delete_files", self.delete_files)
self._event_system.subscribe("trash_files", self.trash_files)
def generate_reference_ui_element(self):
2022-10-26 04:27:21 +00:00
trash_a = Gtk.MenuItem("Trash Actions")
trash_menu = Gtk.Menu()
2022-10-01 21:04:46 +00:00
2022-10-26 04:27:21 +00:00
self.restore = Gtk.MenuItem("Restore From Trash")
self.restore.connect("activate", self.restore_trash_files)
2022-10-01 21:04:46 +00:00
2022-10-26 04:27:21 +00:00
self.empty = Gtk.MenuItem("Empty Trash")
self.empty.connect("activate", self.empty_trash)
2022-10-01 21:04:46 +00:00
2022-10-26 04:27:21 +00:00
trash = Gtk.ImageMenuItem("Trash")
trash.set_image( Gtk.Image.new_from_icon_name("user-trash", 16) )
trash.connect("activate", self.trash_files)
trash.set_always_show_image(True)
2022-10-01 21:04:46 +00:00
2022-10-26 04:27:21 +00:00
go_to = Gtk.ImageMenuItem("Go To Trash")
go_to.set_image( Gtk.Image.new_from_icon_name("user-trash", 16) )
go_to.connect("activate", self.go_to_trash)
go_to.set_always_show_image(True)
delete = Gtk.ImageMenuItem("Delete")
delete.set_image( Gtk.Image(stock=Gtk.STOCK_DELETE) )
delete.connect("activate", self.delete_files)
delete.set_always_show_image(True)
trash_a.set_submenu(trash_menu)
trash_a.show_all()
self._appen_menu_items(trash_menu, [self.restore, self.empty, trash, go_to, delete])
return trash_a
def _appen_menu_items(self, menu, items):
for item in items:
menu.append(item)
2022-10-01 21:04:46 +00:00
def _show_trash_buttons(self):
2022-10-26 04:27:21 +00:00
self.restore.show()
self.empty.show()
2022-10-01 21:04:46 +00:00
def _hide_trash_buttons(self):
2022-10-26 04:27:21 +00:00
self.restore.hide()
self.empty.hide()
2022-10-01 21:04:46 +00:00
def delete_files(self, widget = None, eve = None):
self._event_system.emit("get_current_state")
state = self._fm_state
2022-12-07 05:36:10 +00:00
uris = state.uris
2022-10-01 21:04:46 +00:00
response = None
2022-12-07 05:36:10 +00:00
state.message_dialog.format_secondary_text(f"Do you really want to delete the {len(uris)} file(s)?")
2022-10-01 21:04:46 +00:00
for uri in uris:
file = Gio.File.new_for_path(uri)
if not response:
response = state.message_dialog.run()
state.message_dialog.hide()
2022-10-01 21:04:46 +00:00
if response == Gtk.ResponseType.YES:
type = file.query_file_type(flags=Gio.FileQueryInfoFlags.NONE)
if type == Gio.FileType.DIRECTORY:
state.tab.delete_file( file.get_path() )
else:
file.delete(cancellable=None)
else:
break
def trash_files(self, widget = None, eve = None, verbocity = False):
self._event_system.emit("get_current_state")
state = self._fm_state
2022-12-07 05:36:10 +00:00
for uri in state.uris:
2022-10-01 21:04:46 +00:00
self.trashman.trash(uri, verbocity)
def restore_trash_files(self, widget = None, eve = None, verbocity = False):
self._event_system.emit("get_current_state")
state = self._fm_state
2022-12-07 05:36:10 +00:00
for uri in state.uris:
2022-10-01 21:04:46 +00:00
self.trashman.restore(filename=uri.split("/")[-1], verbose = verbocity)
def empty_trash(self, widget = None, eve = None, verbocity = False):
self.trashman.empty(verbose = verbocity)
def go_to_trash(self, widget = None, eve = None, verbocity = False):
self._event_system.emit("go_to_path", self.trash_files_path)