2022-08-11 08:16:44 +00:00
|
|
|
# plugins_list.py
|
|
|
|
#
|
|
|
|
# Change the look of Adwaita, with ease
|
2022-08-15 23:23:21 +00:00
|
|
|
# Copyright (C) 2022 Gradience Team
|
2022-08-11 08:16:44 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2022-08-11 16:39:52 +00:00
|
|
|
#
|
2022-08-11 08:16:44 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2022-08-11 16:39:52 +00:00
|
|
|
#
|
2022-08-11 08:16:44 +00:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2022-08-10 16:24:28 +00:00
|
|
|
import os
|
2022-08-23 17:38:09 +00:00
|
|
|
|
2022-09-10 13:19:16 +00:00
|
|
|
from gi.repository import Adw, GLib
|
2022-09-04 09:08:32 +00:00
|
|
|
from yapsy.PluginManager import PluginManager
|
2022-08-23 17:38:09 +00:00
|
|
|
from .plugin_row import GradiencePluginRow
|
2022-09-15 16:49:43 +00:00
|
|
|
from .constants import pkgdatadir
|
2022-08-17 21:35:59 +00:00
|
|
|
|
2022-09-07 16:14:08 +00:00
|
|
|
USER_PLUGIN_DIR = os.path.join(
|
2022-09-09 16:17:42 +00:00
|
|
|
os.environ.get("XDG_DATA_HOME", os.environ["HOME"] + "/.local/share"),
|
2022-09-07 16:14:08 +00:00
|
|
|
"gradience",
|
|
|
|
"plugins",
|
|
|
|
)
|
|
|
|
|
2022-09-15 16:49:43 +00:00
|
|
|
SYSTEM_PLUGIN_DIR = os.path.join(
|
|
|
|
pkgdatadir,
|
|
|
|
"plugins",
|
|
|
|
)
|
2022-09-07 16:14:08 +00:00
|
|
|
|
2022-09-15 16:50:20 +00:00
|
|
|
|
2022-08-19 12:32:02 +00:00
|
|
|
class GradiencePluginsList:
|
2022-09-07 16:16:58 +00:00
|
|
|
"""Represent the plugin group in Advanced"""
|
|
|
|
|
2022-09-01 14:06:04 +00:00
|
|
|
def __init__(self, win):
|
|
|
|
|
|
|
|
self.win = win
|
2022-09-07 16:16:58 +00:00
|
|
|
|
2022-09-07 16:16:44 +00:00
|
|
|
self.check_if_plugin_dir_exists()
|
2022-09-01 14:06:04 +00:00
|
|
|
|
2022-09-10 13:39:54 +00:00
|
|
|
self.app = self.win.get_application()
|
2022-09-10 13:43:03 +00:00
|
|
|
self.enabled_plugins = set(
|
2022-09-10 13:44:53 +00:00
|
|
|
self.app.settings.get_value("enabled-plugins").unpack()
|
|
|
|
)
|
2022-09-10 13:39:54 +00:00
|
|
|
self.rows = {}
|
2022-09-10 13:43:03 +00:00
|
|
|
|
2022-09-10 13:39:54 +00:00
|
|
|
self.reload()
|
2022-09-10 13:43:03 +00:00
|
|
|
|
2022-09-10 13:39:54 +00:00
|
|
|
def reload(self):
|
2022-09-04 09:08:32 +00:00
|
|
|
self.pm = PluginManager()
|
2022-09-15 16:50:20 +00:00
|
|
|
self.pm.setPluginPlaces([USER_PLUGIN_DIR, SYSTEM_PLUGIN_DIR])
|
2022-09-04 09:08:32 +00:00
|
|
|
self.pm.collectPlugins()
|
|
|
|
for pluginInfo in self.pm.getAllPlugins():
|
|
|
|
pluginInfo.plugin_object.activate()
|
2022-09-10 10:38:58 +00:00
|
|
|
|
2022-09-10 10:37:08 +00:00
|
|
|
self.app = self.win.get_application()
|
2022-09-10 13:19:47 +00:00
|
|
|
self.enabled_plugins = set(
|
|
|
|
self.app.settings.get_value("enabled-plugins").unpack()
|
|
|
|
)
|
|
|
|
|
2022-09-11 09:20:27 +00:00
|
|
|
def save_enabled_plugins(self):
|
2022-09-10 13:19:16 +00:00
|
|
|
self.app.settings.set_value(
|
|
|
|
"enabled-plugins", GLib.Variant("as", list(self.enabled_plugins))
|
|
|
|
)
|
2022-09-10 13:19:47 +00:00
|
|
|
|
2022-09-10 13:19:16 +00:00
|
|
|
def enable_plugin(self, plugin_id):
|
|
|
|
self.enabled_plugins.add(plugin_id)
|
|
|
|
self.save_enabled_plugins()
|
2022-09-10 13:19:47 +00:00
|
|
|
|
2022-09-10 13:19:16 +00:00
|
|
|
def disable_plugin(self, plugin_id):
|
|
|
|
self.enabled_plugins.remove(plugin_id)
|
|
|
|
self.save_enabled_plugins()
|
2022-09-10 13:19:47 +00:00
|
|
|
|
2022-09-07 16:16:44 +00:00
|
|
|
@staticmethod
|
|
|
|
def check_if_plugin_dir_exists():
|
2022-09-07 16:16:58 +00:00
|
|
|
"""Check if the plugin directory exists, if not, create it"""
|
2022-09-07 16:16:44 +00:00
|
|
|
if not os.path.exists(USER_PLUGIN_DIR):
|
|
|
|
os.makedirs(USER_PLUGIN_DIR)
|
|
|
|
return False
|
|
|
|
return True
|
2022-09-01 14:11:14 +00:00
|
|
|
|
2022-08-19 12:44:02 +00:00
|
|
|
def to_group(self):
|
2022-09-10 10:38:58 +00:00
|
|
|
preset = {
|
|
|
|
"variables": self.app.variables,
|
|
|
|
"palette": self.app.palette,
|
|
|
|
"custom_css": self.app.custom_css,
|
|
|
|
}
|
2022-08-19 12:44:02 +00:00
|
|
|
group = Adw.PreferencesGroup()
|
|
|
|
group.set_title(_("Plugins"))
|
2022-08-19 12:45:50 +00:00
|
|
|
group.set_description(
|
2022-09-07 15:51:36 +00:00
|
|
|
_(
|
|
|
|
"Plugins add additional features to Gradience, plugins are made by Gradience community and can make issues."
|
|
|
|
)
|
|
|
|
)
|
2022-09-14 13:40:35 +00:00
|
|
|
empty = True
|
|
|
|
for pluginInfo in self.pm.getAllPlugins():
|
2022-09-14 13:40:49 +00:00
|
|
|
row = GradiencePluginRow(pluginInfo.plugin_object, preset, self)
|
2022-09-14 13:40:35 +00:00
|
|
|
self.rows[pluginInfo.plugin_object.plugin_id] = row
|
|
|
|
group.add(row)
|
|
|
|
empty = False
|
|
|
|
if empty:
|
2022-08-19 12:44:02 +00:00
|
|
|
row = Adw.ActionRow()
|
|
|
|
row.set_title(_("No plugins found"))
|
|
|
|
group.add(row)
|
|
|
|
return group
|
2022-09-04 09:43:52 +00:00
|
|
|
|
2022-09-04 09:08:32 +00:00
|
|
|
def save(self):
|
|
|
|
saved = {}
|
|
|
|
for pluginInfo in self.pm.getAllPlugins():
|
|
|
|
saved[pluginInfo.plugin_object.plugin_id] = pluginInfo.plugin_object.save()
|
|
|
|
return saved
|
2022-09-04 09:43:52 +00:00
|
|
|
|
|
|
|
def validate(self):
|
|
|
|
errors = []
|
|
|
|
for pluginInfo in self.pm.getAllPlugins():
|
|
|
|
error, detail = pluginInfo.plugin_object.validate()
|
|
|
|
if error:
|
2022-09-07 15:51:36 +00:00
|
|
|
errors.append(detail)
|
2022-09-04 09:43:52 +00:00
|
|
|
return errors
|
2022-09-10 08:38:16 +00:00
|
|
|
|
|
|
|
def apply(self):
|
|
|
|
for pluginInfo in self.pm.getAllPlugins():
|
2022-09-10 13:19:16 +00:00
|
|
|
if pluginInfo.plugin_object.plugin_id in self.enabled_plugins:
|
2022-09-10 13:19:47 +00:00
|
|
|
pluginInfo.plugin_object.apply()
|