From 5222fc9258a2b3eb43f836bda3eb6e56b581de33 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Tue, 8 Dec 2020 08:30:50 +0100
Subject: Simplified the code running the scripts panel.

---
 plugins/python/scripting/Makefile.am |  1 -
 plugins/python/scripting/core.py     | 23 ++++++++++++----
 plugins/python/scripting/manager.py  | 53 ------------------------------------
 plugins/python/scripting/panel.py    | 16 +++++++----
 4 files changed, 28 insertions(+), 65 deletions(-)
 delete mode 100644 plugins/python/scripting/manager.py

diff --git a/plugins/python/scripting/Makefile.am b/plugins/python/scripting/Makefile.am
index 3c44f1f..c53be23 100644
--- a/plugins/python/scripting/Makefile.am
+++ b/plugins/python/scripting/Makefile.am
@@ -4,7 +4,6 @@ scriptingdir = $(pluginsdatadir)/python/scripting
 scripting_DATA = 							\
 	__init__.py								\
 	core.py									\
-	manager.py								\
 	panel.py								\
 	panel.ui								\
 	python-script-icon-16x16.png
diff --git a/plugins/python/scripting/core.py b/plugins/python/scripting/core.py
index 3e96c8e..c46ef07 100644
--- a/plugins/python/scripting/core.py
+++ b/plugins/python/scripting/core.py
@@ -1,5 +1,6 @@
 
-from gi.repository import Gtk
+from gi.repository import GLib, Gtk
+import os
 
 from pychrysalide import PluginModule
 from pychrysalide import core
@@ -7,7 +8,6 @@ from pychrysalide.gui import core as gcore
 from pychrysalide.gui import MenuBar
 from pychrysalide.gtkext import EasyGtk
 
-from .manager import get_recent_python_script_manager, remember_python_script
 from .panel import ScriptPanel
 
 
@@ -21,6 +21,8 @@ class ScriptingEngine(PluginModule):
 
     _actions = ( PluginModule.PluginAction.PLUGINS_LOADED, PluginModule.PluginAction.PANEL_CREATION )
 
+    _manager = None
+
 
     def __init__(self):
         """Initialize the plugin for Chrysalide."""
@@ -67,9 +69,9 @@ class ScriptingEngine(PluginModule):
 
         if action == PluginModule.PluginAction.PLUGINS_LOADED:
 
-            filename = self.build_config_filename('recents.xbel', True)
+            xbel = self.build_config_filename('recents.xbel', True)
 
-            get_recent_python_script_manager(filename)
+            ScriptingEngine._manager = Gtk.RecentManager(filename=xbel)
 
 
     def _on_panel_creation(self, action, item):
@@ -77,6 +79,8 @@ class ScriptingEngine(PluginModule):
 
         if type(item) == ScriptPanel:
 
+            item.attach_manager(ScriptingEngine._manager)
+
             item.connect('run-requested', self._on_run_requested)
             item.connect('ask-for-new-script', lambda x: self._on_file_run_script_activate(None))
 
@@ -116,7 +120,16 @@ class ScriptingEngine(PluginModule):
 
         self.log_message(core.LogMessageType.INFO, 'Execute the script file \'%s\'' % filename)
 
-        remember_python_script(filename)
+        uri = GLib.filename_to_uri(filename)
+
+        recent_data = Gtk.RecentData()
+        recent_data.app_name = 'Chrysalide Python plugin'
+        recent_data.app_exec = 'chrysalide'
+        recent_data.display_name = os.path.basename(filename)
+        recent_data.description = 'Python script run inside Chrysalide'
+        recent_data.mime_type = 'text/x-python'
+
+        ScriptingEngine._manager.add_full(uri, recent_data)
 
         try:
             with open(filename, 'r') as fd:
diff --git a/plugins/python/scripting/manager.py b/plugins/python/scripting/manager.py
deleted file mode 100644
index 6b27b48..0000000
--- a/plugins/python/scripting/manager.py
+++ /dev/null
@@ -1,53 +0,0 @@
-
-import os
-
-from gi.repository import GLib, Gtk
-
-
-_manager = None
-
-
-def get_recent_python_script_manager(xbel = None):
-    """Provide the manager for the recently run Python scripts."""
-
-    global _manager
-
-    # As a first panel creation is forced by the Chrysalide core to register
-    # its final GType, xbel is not defined at the first call of this function.
-    # Thus relying on the definition of xbel is a better filter than relying
-    # on the existence of _manager.
-    #
-    # In that special initial case, result is None
-
-    if not(xbel is None):
-
-        assert(_manager is None)
-
-        _manager = Gtk.RecentManager(filename=xbel)
-
-    return _manager
-
-
-def remember_python_script(filename):
-    """Register a Python script into the recents list."""
-
-    uri = GLib.filename_to_uri(filename)
-
-    recent_data = Gtk.RecentData()
-    recent_data.app_name = 'Chrysalide Python plugin'
-    recent_data.app_exec = 'chrysalide'
-    recent_data.display_name = os.path.basename(filename)
-    recent_data.description = 'Python script run inside Chrysalide'
-    recent_data.mime_type = 'text/x-python'
-
-    manager = get_recent_python_script_manager()
-    manager.add_full(uri, recent_data)
-
-
-def forget_python_script(filename):
-    """Unregister a Python script from the recents list."""
-
-    uri = GLib.filename_to_uri(filename)
-
-    manager = get_recent_python_script_manager()
-    manager.remove_item(uri)
diff --git a/plugins/python/scripting/panel.py b/plugins/python/scripting/panel.py
index 75b50e3..9c5d153 100644
--- a/plugins/python/scripting/panel.py
+++ b/plugins/python/scripting/panel.py
@@ -5,7 +5,6 @@ from pychrysalide.gtkext import BuiltNamedWidget
 from pychrysalide.gui import core
 from pychrysalide.gui import PanelItem
 
-from .manager import get_recent_python_script_manager, forget_python_script
 
 
 class ScriptPanel(PanelItem):
@@ -35,6 +34,7 @@ class ScriptPanel(PanelItem):
                                GObject.TYPE_NONE, ())
 
         self._last_selected = None
+        self._manager = None
 
         builder = self.named_widget.builder
 
@@ -43,13 +43,15 @@ class ScriptPanel(PanelItem):
 
         builder.connect_signals(self)
 
-        manager = get_recent_python_script_manager()
 
-        if manager:
+    def attach_manager(self, manager):
+        """Assign the recent manager to the panel."""
 
-            manager.connect("changed", self._on_recent_list_changed)
+        self._manager = manager
 
-            self._on_recent_list_changed(manager)
+        manager.connect("changed", self._on_recent_list_changed)
+
+        self._on_recent_list_changed(manager)
 
 
     def _on_row_activated(self, treeview, path, column):
@@ -84,7 +86,9 @@ class ScriptPanel(PanelItem):
 
             if treeiter:
 
-                forget_python_script(model[treeiter][3])
+                uri = GLib.filename_to_uri(model[treeiter][3])
+
+                self._manager.remove_item(uri)
 
         elif event.keyval == Gdk.KEY_Insert:
 
-- 
cgit v0.11.2-87-g4458