tweakmodel.py 4.36 KB
Newer Older
1
# Copyright (c) 2011 John Stowers
2 3
# SPDX-License-Identifier: GPL-3.0+
# License-Filename: LICENSES/GPL-3.0
4

5
import logging
John Stowers's avatar
John Stowers committed
6 7 8
import glob
import os.path

9
import gtweak
10
from gtweak.utils import SchemaList, LogoutNotification, Notification
11
from gi.repository import Gtk, GLib
John Stowers's avatar
John Stowers committed
12

13 14
def N_(x): return x

15 16
LOG = logging.getLogger(__name__)

17 18 19
def string_for_search(s):
    return GLib.utf8_casefold(GLib.utf8_normalize(s, -1, GLib.NormalizeMode.ALL), -1)

20

21 22
class Tweak(object):

23
    main_window = None
24
    widget_for_size_group = None
Alex Muñoz's avatar
Alex Muñoz committed
25
    extra_info = ""
26

27
    def __init__(self, name, description, **options):
28 29
        self.name = name or ""
        self.description = description or ""
30
        self.uid = options.get("uid", self.__class__.__name__)
31
        self.group_name = options.get("group_name", _("Miscellaneous"))
32
        self.loaded = options.get("loaded", True)
33
        self.widget_sort_hint = None
34

John Stowers's avatar
John Stowers committed
35
        self._search_cache = None
36

John Stowers's avatar
John Stowers committed
37
    def search_matches(self, txt):
38
        if self._search_cache is None:
39 40
            self._search_cache = string_for_search(self.name) + " " + \
				 string_for_search(self.description)
Alex Muñoz's avatar
Alex Muñoz committed
41
            try:
42
                self._search_cache += " " + string_for_search(self.extra_info)
Alex Muñoz's avatar
Alex Muñoz committed
43 44
            except:
                LOG.warning("Error adding search info", exc_info=True)
45
        return txt in self._search_cache
John Stowers's avatar
John Stowers committed
46

47 48
    def notify_logout(self):
        self._logoutnotification = LogoutNotification()
49

50 51
    def notify_information(self, summary, desc=""):
        self._notification = Notification(summary, desc)
52

53

54
class TweakGroup(object):
55 56 57

    main_window = None

58
    def __init__(self, name, *tweaks, **options):
John Stowers's avatar
John Stowers committed
59
        self.name = name
60
        self.titlebar_widget = None
61
        self.tweaks = [t for t in tweaks if t.loaded]
62
        self.uid = options.get('uid', self.__class__.__name__)
63

64 65 66 67 68
    def add_tweak(self, tweak):
        if tweak.loaded:
            self.tweaks.append(tweak)
            return True

69

John Stowers's avatar
John Stowers committed
70 71
class TweakModel(Gtk.ListStore):
    (COLUMN_NAME,
72
     COLUMN_TWEAK) = list(range(2))
John Stowers's avatar
John Stowers committed
73 74 75

    def __init__(self):
        super(TweakModel, self).__init__(str, object)
76 77
        self._tweak_dir = gtweak.TWEAK_DIR
        assert(os.path.exists(self._tweak_dir))
John Stowers's avatar
John Stowers committed
78

79 80
        self.set_sort_column_id(self.COLUMN_NAME, Gtk.SortType.ASCENDING)

81 82
        # map of tweakgroup.name -> tweakgroup
        self._tweak_group_names = {}
John Stowers's avatar
John Stowers committed
83
        self._tweak_group_iters = {}
84

85 86
    @property
    def tweaks(self):
87
        return (t for row in self for t in row[TweakModel.COLUMN_TWEAK].tweaks)
88 89 90

    @property
    def tweak_groups(self):
91
        return (row[TweakModel.COLUMN_TWEAK] for row in self)
92

93
    def load_tweaks(self, main_window):
94 95 96
        tweak_files = [
                os.path.splitext(os.path.split(f)[-1])[0]
                    for f in glob.glob(os.path.join(self._tweak_dir, "tweak_group_*.py"))]
97 98 99

        if not gtweak.ENABLE_TEST:
            try:
100
                tweak_files.remove("tweak_group_test")
101 102
            except ValueError:
                pass
103

104 105 106
        groups = []
        tweaks = []

John Stowers's avatar
John Stowers committed
107 108
        mods = __import__("gtweak.tweaks", globals(), locals(), tweak_files, 0)
        for mod in [getattr(mods, file_name) for file_name in tweak_files]:
109
            groups.extend( getattr(mod, "TWEAK_GROUPS", []))
110 111 112

        schemas = SchemaList()

113
        for g in groups:
114
            g.main_window = main_window
115 116
            if g.tweaks:
                self.add_tweak_group(g)
Alex Muñoz's avatar
Alex Muñoz committed
117
                for i in g.tweaks:
118
                    i.main_window = main_window
Alex Muñoz's avatar
Alex Muñoz committed
119 120 121 122
                    try:
                        schemas.insert(i.key_name, i.schema_name)
                    except:
                        pass
John Stowers's avatar
John Stowers committed
123 124

    def add_tweak_group(self, tweakgroup):
125 126 127 128
        if tweakgroup.name in self._tweak_group_names:
            LOG.critical("Tweak group named: %s already exists" % tweakgroup.name)
            return

129
        _iter = self.append([gettext(tweakgroup.name), tweakgroup])
130
        self._tweak_group_names[tweakgroup.name] = tweakgroup
John Stowers's avatar
John Stowers committed
131
        self._tweak_group_iters[tweakgroup.name] = _iter
132

133
    def search_matches(self, txt):
134 135 136
        tweaks = []
        groups = []

Alex Muñoz's avatar
Alex Muñoz committed
137
        for g in self.tweak_groups:
138
            for t in g.tweaks:
139
                if t.search_matches(txt):
Alex Muñoz's avatar
Alex Muñoz committed
140
                    tweaks.append(t)
141
                    if g.name not in groups:
Alex Muñoz's avatar
Alex Muñoz committed
142
                        groups.append(g.name)
143
        return tweaks, groups
John Stowers's avatar
John Stowers committed
144 145 146

    def get_tweakgroup_iter(self, name):
        return self._tweak_group_iters[name]