Gradience/gradience/plugins_list.py

131 lines
4 KiB
Python
Raw Normal View History

2022-08-11 08:16:44 +00:00
# plugins_list.py
#
# Change the look of Adwaita, with ease
# 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-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
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
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"),
"gradience",
"plugins",
)
2022-09-15 16:49:43 +00:00
SYSTEM_PLUGIN_DIR = os.path.join(
pkgdatadir,
"plugins",
)
2022-08-19 12:32:02 +00:00
class GradiencePluginsList:
"""Represent the plugin group in Advanced"""
2022-09-01 14:06:04 +00:00
def __init__(self, win):
self.win = win
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()
self.enabled_plugins = set(
self.app.settings.get_value("enabled-plugins").unpack()
)
2022-09-10 13:39:54 +00:00
self.rows = {}
2022-09-10 13:39:54 +00:00
self.reload()
2022-09-10 13:39:54 +00:00
def reload(self):
2022-09-04 09:08:32 +00:00
self.pm = PluginManager()
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:37:08 +00:00
self.app = self.win.get_application()
self.enabled_plugins = set(
self.app.settings.get_value("enabled-plugins").unpack()
)
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:16 +00:00
def enable_plugin(self, plugin_id):
self.enabled_plugins.add(plugin_id)
self.save_enabled_plugins()
2022-09-10 13:19:16 +00:00
def disable_plugin(self, plugin_id):
self.enabled_plugins.remove(plugin_id)
self.save_enabled_plugins()
@staticmethod
def check_if_plugin_dir_exists():
"""Check if the plugin directory exists, if not, create it"""
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):
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(
_(
"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():
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:
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:
pluginInfo.plugin_object.apply()