2022-09-03 05:43:25 +00:00
|
|
|
# Python imports
|
2022-11-29 04:34:13 +00:00
|
|
|
import os
|
|
|
|
import json
|
2022-09-03 05:43:25 +00:00
|
|
|
|
|
|
|
# Lib imports
|
|
|
|
import gi
|
|
|
|
gi.require_version('Gtk', '3.0')
|
|
|
|
from gi.repository import Gtk
|
|
|
|
|
|
|
|
# Application imports
|
2022-09-06 02:21:04 +00:00
|
|
|
from plugins.plugin_base import PluginBase
|
2022-09-03 05:43:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-09-06 02:21:04 +00:00
|
|
|
class Plugin(PluginBase):
|
2022-09-03 05:43:25 +00:00
|
|
|
def __init__(self):
|
2022-09-06 02:21:04 +00:00
|
|
|
super().__init__()
|
|
|
|
|
2022-09-05 03:35:30 +00:00
|
|
|
self.name = "Favorites" # NOTE: Need to remove after establishing private bidirectional 1-1 message bus
|
2022-10-27 22:23:27 +00:00
|
|
|
# where self.name should not be needed for message comms
|
2022-09-03 05:43:25 +00:00
|
|
|
self.path = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
self._GLADE_FILE = f"{self.path}/favorites.glade"
|
|
|
|
self._FAVORITES_FILE = f"{self.path}/favorites.json"
|
|
|
|
|
|
|
|
self._favorites_dialog = None
|
|
|
|
self._favorites_store = None
|
|
|
|
self._favorites = None
|
|
|
|
self._selected = None
|
|
|
|
|
|
|
|
|
|
|
|
def run(self):
|
2023-02-21 01:18:45 +00:00
|
|
|
self._builder = Gtk.Builder()
|
2022-09-03 05:43:25 +00:00
|
|
|
self._builder.add_from_file(self._GLADE_FILE)
|
2023-02-21 01:18:45 +00:00
|
|
|
self._connect_builder_signals(self, self._builder)
|
2022-09-03 05:43:25 +00:00
|
|
|
|
|
|
|
self._favorites_dialog = self._builder.get_object("favorites_dialog")
|
|
|
|
self._favorites_store = self._builder.get_object("favorites_store")
|
|
|
|
self._current_dir_lbl = self._builder.get_object("current_dir_lbl")
|
|
|
|
|
|
|
|
if os.path.exists(self._FAVORITES_FILE):
|
|
|
|
with open(self._FAVORITES_FILE) as f:
|
|
|
|
self._favorites = json.load(f)
|
|
|
|
for favorite in self._favorites:
|
2022-11-26 05:12:43 +00:00
|
|
|
display, path = favorite
|
|
|
|
self._favorites_store.append([display, path])
|
2022-09-03 05:43:25 +00:00
|
|
|
else:
|
|
|
|
with open(self._FAVORITES_FILE, 'a') as f:
|
|
|
|
f.write('[]')
|
|
|
|
|
2022-10-27 22:23:27 +00:00
|
|
|
self._event_system.subscribe("show_favorites_menu", self._show_favorites_menu)
|
|
|
|
|
2022-09-03 05:43:25 +00:00
|
|
|
|
2022-10-10 01:59:44 +00:00
|
|
|
def generate_reference_ui_element(self):
|
|
|
|
button = Gtk.Button(label=self.name)
|
|
|
|
button.connect("button-release-event", self._show_favorites_menu)
|
|
|
|
return button
|
|
|
|
|
2022-09-03 05:43:25 +00:00
|
|
|
def _get_state(self, widget=None, eve=None):
|
2022-10-10 01:59:44 +00:00
|
|
|
self._event_system.emit("get_current_state")
|
2022-09-03 05:43:25 +00:00
|
|
|
|
|
|
|
def _set_current_dir_lbl(self, widget=None, eve=None):
|
2022-09-29 22:22:33 +00:00
|
|
|
self._current_dir_lbl.set_label(f"Current Directory:\n{self._fm_state.tab.get_current_directory()}")
|
2022-09-03 05:43:25 +00:00
|
|
|
|
|
|
|
def _add_to_favorite(self, state):
|
2022-11-26 05:12:43 +00:00
|
|
|
path = self._fm_state.tab.get_current_directory()
|
|
|
|
parts = path.split("/")
|
|
|
|
display = '/'.join(parts[-3:]) if len(parts) > 3 else path
|
|
|
|
|
|
|
|
self._favorites_store.append([display, path])
|
|
|
|
self._favorites.append([display, path])
|
2022-09-03 05:43:25 +00:00
|
|
|
self._save_favorites()
|
|
|
|
|
|
|
|
def _remove_from_favorite(self, state):
|
2022-11-26 05:12:43 +00:00
|
|
|
path = self._favorites_store.get_value(self._selected, 1)
|
2022-09-03 05:43:25 +00:00
|
|
|
self._favorites_store.remove(self._selected)
|
2022-11-26 05:12:43 +00:00
|
|
|
|
|
|
|
for i, f in enumerate(self._favorites):
|
|
|
|
if f[1] == path:
|
|
|
|
self._favorites.remove( self._favorites[i] )
|
|
|
|
|
2022-09-03 05:43:25 +00:00
|
|
|
self._save_favorites()
|
|
|
|
|
|
|
|
def _save_favorites(self):
|
|
|
|
with open(self._FAVORITES_FILE, 'w') as outfile:
|
|
|
|
json.dump(self._favorites, outfile, separators=(',', ':'), indent=4)
|
|
|
|
|
|
|
|
def _set_selected_path(self, widget=None, eve=None):
|
2022-11-26 05:12:43 +00:00
|
|
|
path = self._favorites_store.get_value(self._selected, 1)
|
2022-09-03 05:43:25 +00:00
|
|
|
self._ui_objects[0].set_text(path)
|
2022-09-29 22:22:33 +00:00
|
|
|
self._set_current_dir_lbl()
|
2022-09-03 05:43:25 +00:00
|
|
|
|
|
|
|
def _show_favorites_menu(self, widget=None, eve=None):
|
2022-09-29 22:22:33 +00:00
|
|
|
self._fm_state = None
|
2022-09-03 05:43:25 +00:00
|
|
|
self._get_state()
|
|
|
|
self._set_current_dir_lbl()
|
|
|
|
self._favorites_dialog.run()
|
|
|
|
|
|
|
|
def _hide_favorites_menu(self, widget=None, eve=None):
|
|
|
|
self._favorites_dialog.hide()
|
|
|
|
|
|
|
|
def _set_selected(self, user_data):
|
|
|
|
selected = user_data.get_selected()[1]
|
2022-11-26 05:12:43 +00:00
|
|
|
if selected and not self._selected == selected:
|
2022-09-03 05:43:25 +00:00
|
|
|
self._selected = selected
|