diff --git a/src/core/widgets/category_list_widget.py b/src/core/widgets/category_list_widget.py index 87cc9ce..ca9958f 100644 --- a/src/core/widgets/category_list_widget.py +++ b/src/core/widgets/category_list_widget.py @@ -4,6 +4,8 @@ import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk +from gi.repository import GLib +from gi.repository import Gio from xdg.DesktopEntry import DesktopEntry @@ -18,8 +20,9 @@ class CategoryListWidget(Gtk.ButtonBox): super(CategoryListWidget, self).__init__() self.ctx = self.get_style_context() - self.active_category: str = 'Accessories' + self.dir_watchers: [] = [] + self.active_category: str = 'Accessories' self.category_dict: {} = { "Accessories": [], "Multimedia": [], @@ -48,11 +51,10 @@ class CategoryListWidget(Gtk.ButtonBox): self.ctx.add_class("category-list-widget") def _setup_signals(self): - event_system.subscribe("refresh-active-category", self.refresh_active_category) + self._load_dir_watchers() def _subscribe_to_events(self): - ... - + event_system.subscribe("refresh-active-category", self.refresh_active_category) def _load_widgets(self): for category in self.category_dict.keys(): @@ -62,8 +64,45 @@ class CategoryListWidget(Gtk.ButtonBox): self.add(button) - def set_active_category(self, button): - self.active_category = button.get_label() + def _reset_dir_watchers(self): + self._clear_dir_watchers() + self._load_dir_watchers() + + def _load_dir_watchers(self): + for dpath in settings_manager.settings.config.application_dirs: + dir_watcher = Gio.File.new_for_path(dpath).monitor_directory( + Gio.FileMonitorFlags.WATCH_MOVES, + Gio.Cancellable() + ) + + watch_id = dir_watcher.connect( + "changed", + self._dir_watch_updates + ) + + dir_watcher.watch_id = watch_id + self.dir_watchers.append(dir_watcher) + + def _clear_dir_watchers(self): + for watcher in self.dir_watchers: + watcher.cancel() + watcher.disconnect(watcher.watch_id) + watcher.run_dispose() + + self.dir_watchers.clear() + + def _dir_watch_updates(self, file_monitor, file, other_file = None, eve_type = None): + if eve_type in [Gio.FileMonitorEvent.CREATED, Gio.FileMonitorEvent.DELETED, + Gio.FileMonitorEvent.RENAMED, Gio.FileMonitorEvent.MOVED_IN, + Gio.FileMonitorEvent.MOVED_OUT]: + + GLib.idle_add(self._reload_meu) + + def _reload_meu(self): + for group in self.category_dict: + self.category_dict[group].clear() + + self.fill_menu_objects() event_system.emit( "load-active-category", (self.category_dict[ self.active_category ],) @@ -121,6 +160,13 @@ class CategoryListWidget(Gtk.ButtonBox): } ) + def set_active_category(self, button): + self.active_category = button.get_label() + event_system.emit( + "load-active-category", + (self.category_dict[ self.active_category ],) + ) + def refresh_active_category(self): event_system.emit( "load-active-category",