-
Notifications
You must be signed in to change notification settings - Fork 2
/
PrefsPage.py
102 lines (87 loc) · 3.24 KB
/
PrefsPage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# rhythmbox-telegram
# Copyright (C) 2023-2024 Andrey Izman <izmanw@gmail.com>
#
# 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.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import gi
gi.require_version('Gtk', '3.0')
import json
from gi.repository import GObject, Gtk, Gio
def set_combo_text_column(combo, col):
if combo.get_has_entry():
combo.set_entry_text_column(col)
else:
text = Gtk.CellRendererText()
combo.pack_start(text, True)
combo.add_attribute(text, 'text', col)
class PrefsPage(GObject.GObject):
def __init__(self, prefs, name=None, ui_file=None, main_box=None):
self.box = Gtk.Box(hexpand=True)
# init vars
self._changes = {}
self.has_errors = []
# set custom values
self.prefs = prefs
self.plugin = prefs.plugin
self.account = prefs.account
self.settings = prefs.account.settings
if name is not None:
self.name = name
if ui_file is not None:
self.ui_file = ui_file
if main_box is not None:
self.main_box = main_box
# init UI
self.box.set_border_width(5) # noqa
self.ui = Gtk.Builder()
self.ui.add_from_file(prefs.find_plugin_file(self.ui_file))
self._init_widget()
self.register_signals()
def register_signals(self):
pass
def create_widget(self):
self._create_widget()
self.box.add(self.get_main_object()) # noqa
return self
def set_error(self, widget, is_error=True):
if is_error:
self.has_errors.append(widget)
widget.set_icon_from_icon_name(Gtk.EntryIconPosition.SECONDARY, 'error')
else:
widget.set_icon_from_stock(Gtk.EntryIconPosition.SECONDARY, None)
def clear_errors(self):
if self.has_errors:
for widget in self.has_errors:
self.set_error(widget, False)
self.has_errors.clear()
def on_change(self, name, value):
print('==ON_CHANGE %s %s' % (name, value))
txt = json.dumps(value)
reload = False
if name not in self._changes or self._changes[name] != txt:
self._changes[name] = txt
if name in ['connected', 'channels', 'page-group']:
reload = True
if reload:
print('===EMIT1.reload_sources')
self.prefs.plugin.emit('reload_sources')
def get_window(self):
return self.ui.get_object('window')
def get_main_object(self):
return self.ui.get_object(self.main_box)
def _init_widget(self):
pass
def _create_widget(self):
pass
def append_to(self, notebook):
notebook.append_page(self.box, Gtk.Label(self.name))