-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathalttoolbar_preferences.py
344 lines (282 loc) · 12.3 KB
/
alttoolbar_preferences.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# -*- Mode: python; coding: utf-8; tab-width: 4; indent-tabs-mode: nil; -*-
#
# Copyright (C) 2015 - 2020 David Mohammed <[email protected]>
#
# 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, 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
# define plugin
import gettext
import locale
import os
import shutil
import sys
import gi
import rb
from gi.repository import GObject
from gi.repository import Gio
from gi.repository import Gtk
gi.require_version('PeasGtk', '1.0')
from gi.repository import PeasGtk
from gi.repository import RB
class GSetting:
"""
This class manages the different settings that the plugin has to
access to read or write.
"""
# storage for the instance reference
__instance = None
class __impl:
""" Implementation of the singleton interface """
# below public variables and methods that can be called for GSetting
def __init__(self):
"""
Initializes the singleton interface, assigning all the constants
used to access the plugin's settings.
"""
self.Path = self._enum(
PLUGIN='org.gnome.rhythmbox.plugins.alternative_toolbar')
self.PluginKey = self._enum(
DISPLAY_TYPE='display-type',
START_HIDDEN='start-hidden',
SHOW_COMPACT='show-compact',
COMPACT_POS='compact-pos',
PLAYING_LABEL='playing-label',
VOLUME_CONTROL='volume-control',
INLINE_LABEL='inline-label',
ENHANCED_SIDEBAR='enhanced-sidebar',
EXPANDERS='expanders',
SHOW_TOOLTIPS='show-tooltips',
REPEAT_TYPE='repeat-type',
SOURCE_TOOLBAR='show-source-toolbar',
HORIZ_CATEGORIES='horiz-categories',
APP_MENU='app-menu-display',
DARK_THEME='dark-theme'
)
self.setting = {}
def get_setting(self, path):
"""
Return an instance of Gio.Settings pointing at the selected path.
"""
try:
setting = self.setting[path]
except:
self.setting[path] = Gio.Settings.new(path)
setting = self.setting[path]
return setting
def get_value(self, path, key):
"""
Return the value saved on key from the settings path.
"""
return self.get_setting(path)[key]
def set_value(self, path, key, value):
"""
Set the passed value to key in the settings path.
"""
self.get_setting(path)[key] = value
def _enum(self, **enums):
"""
Create an enumn.
"""
return type('Enum', (), enums)
def __init__(self):
""" Create singleton instance """
# Check whether we already have an instance
if GSetting.__instance is None:
# Create and remember instance
GSetting.__instance = GSetting.__impl()
# Store instance reference as the only member in the handle
self.__dict__['_GSetting__instance'] = GSetting.__instance
def __getattr__(self, attr):
""" Delegate access to implementation """
return getattr(self.__instance, attr)
def __setattr__(self, attr, value):
""" Delegate access to implementation """
return setattr(self.__instance, attr, value)
class CoverLocale:
"""
This class manages the locale
"""
# storage for the instance reference
__instance = None
class __impl:
""" Implementation of the singleton interface """
# below public variables and methods that can be called for CoverLocale
def __init__(self):
"""
Initializes the singleton interface, assigning all the constants
used to access the plugin's settings.
"""
self.Locale = self._enum(
RB='rhythmbox',
LOCALE_DOMAIN='alternative-toolbar')
def switch_locale(self, locale_type):
"""
Change the locale
"""
locale.setlocale(locale.LC_ALL, '')
locale.bindtextdomain(locale_type, RB.locale_dir())
locale.textdomain(locale_type)
gettext.bindtextdomain(locale_type, RB.locale_dir())
gettext.textdomain(locale_type)
gettext.install(locale_type)
def get_locale(self):
"""
return the string representation of the users locale
for example
en_US
"""
return locale.getdefaultlocale()[0]
def _enum(self, **enums):
"""
Create an enumn.
"""
return type('Enum', (), enums)
def get_translation(self, value):
"""
return the translated version of the string
"""
return gettext.gettext(value)
def __init__(self):
""" Create singleton instance """
# Check whether we already have an instance
if CoverLocale.__instance is None:
# Create and remember instance
CoverLocale.__instance = CoverLocale.__impl()
# Store instance reference as the only member in the handle
self.__dict__['_CoverLocale__instance'] = CoverLocale.__instance
def __getattr__(self, attr):
""" Delegate access to implementation """
return getattr(self.__instance, attr)
def __setattr__(self, attr, value):
""" Delegate access to implementation """
return setattr(self.__instance, attr, value)
class Preferences(GObject.Object, PeasGtk.Configurable):
"""
Preferences for the Plugins. It holds the settings for
the plugin and also is the responsible of creating the preferences dialog.
"""
__gtype_name__ = 'AlternativeToolbarPreferences'
object = GObject.property(type=GObject.Object)
def __init__(self):
"""
Initialises the preferences, getting an instance of the settings saved
by Gio.
"""
GObject.Object.__init__(self)
self.gs = GSetting()
self.plugin_settings = self.gs.get_setting(self.gs.Path.PLUGIN)
def do_create_configure_widget(self):
"""
Creates the plugin's preferences dialog
"""
print("DEBUG - create_display_contents")
# create the ui
self._first_run = True
cl = CoverLocale()
cl.switch_locale(cl.Locale.LOCALE_DOMAIN)
builder = Gtk.Builder()
builder.set_translation_domain(cl.Locale.LOCALE_DOMAIN)
builder.add_from_file(rb.find_plugin_file(self,
'ui/altpreferences.ui'))
builder.connect_signals(self)
# bind the toggles to the settings
start_hidden = builder.get_object('start_hidden_checkbox')
start_hidden.set_active(
not self.plugin_settings[self.gs.PluginKey.START_HIDDEN])
start_hidden.connect('toggled', self._start_hidden_checkbox_toggled)
self._show_compact = builder.get_object('show_compact_checkbox')
self.plugin_settings.bind(self.gs.PluginKey.SHOW_COMPACT,
self._show_compact, 'active',
Gio.SettingsBindFlags.DEFAULT)
self._show_compact.connect('toggled',
self._show_compact_checkbox_toggled)
self._compact_pos = builder.get_object('compact_pos_combobox')
self.plugin_settings.bind(self.gs.PluginKey.COMPACT_POS,
self._compact_pos, 'active',
Gio.SettingsBindFlags.DEFAULT)
self._playing_label = builder.get_object('playing_label_checkbox')
self.plugin_settings.bind(self.gs.PluginKey.PLAYING_LABEL,
self._playing_label, 'active',
Gio.SettingsBindFlags.DEFAULT)
self._inline_label = builder.get_object('inline_label_checkbox')
self.plugin_settings.bind(self.gs.PluginKey.INLINE_LABEL,
self._inline_label, 'active',
Gio.SettingsBindFlags.DEFAULT)
volume_control = builder.get_object('volume_control_checkbox')
self.plugin_settings.bind(self.gs.PluginKey.VOLUME_CONTROL,
volume_control, 'active',
Gio.SettingsBindFlags.DEFAULT)
self._enhanced_sidebar = builder.get_object(
'enhanced_sidebar_checkbox')
self.plugin_settings.bind(self.gs.PluginKey.ENHANCED_SIDEBAR,
self._enhanced_sidebar, 'active',
Gio.SettingsBindFlags.DEFAULT)
self._show_tooltips = builder.get_object('tooltips_checkbox')
self.plugin_settings.bind(self.gs.PluginKey.SHOW_TOOLTIPS,
self._show_tooltips, 'active',
Gio.SettingsBindFlags.DEFAULT)
self._dark_theme = \
builder.get_object('dark_theme_checkbox')
self.plugin_settings.bind(self.gs.PluginKey.DARK_THEME,
self._dark_theme, 'active',
Gio.SettingsBindFlags.DEFAULT)
modern_switch = builder.get_object('modern_switch')
# modern_switch.connect('state-set', self._modern_switch_state)
modern_switch.connect('notify', self._modern_switch_state)
# Determine what type of toolbar is to be displayed
default = Gtk.Settings.get_default()
display_type = self.plugin_settings[self.gs.PluginKey.DISPLAY_TYPE]
if display_type == 0:
if (not default.props.gtk_shell_shows_app_menu) or \
default.props.gtk_shell_shows_menubar:
modern_switch.set_active(False)
else:
modern_switch.set_active(True)
elif display_type == 1:
modern_switch.set_active(True)
else:
modern_switch.set_active(False)
if modern_switch.get_active():
self._show_compact.set_active(True)
self._show_compact_checkbox_toggled(self._show_compact)
infobar = builder.get_object('infobar')
button = infobar.add_button(_("Restart"), 1)
# restart_button = builder.get_object('restart_button')
button.connect('clicked', self._restart_button_clicked)
self._category_pos = builder.get_object('category_combobox')
self.plugin_settings.bind(self.gs.PluginKey.HORIZ_CATEGORIES,
self._category_pos, 'active',
Gio.SettingsBindFlags.DEFAULT)
self._first_run = False
return builder.get_object('preferences_box')
def _restart_button_clicked(self, *args):
exepath = shutil.which('rhythmbox')
os.execl(exepath, exepath, *sys.argv)
def _start_hidden_checkbox_toggled(self, toggle_button):
self.plugin_settings[self.gs.PluginKey.START_HIDDEN] = \
not toggle_button.get_active()
def _show_compact_checkbox_toggled(self, toggle_button):
enabled = toggle_button.get_active()
self._show_tooltips.set_sensitive(enabled)
self._inline_label.set_sensitive(enabled)
self._playing_label.set_sensitive(enabled)
def _modern_switch_state(self, switch, param):
state = switch.get_active()
self._show_compact.set_sensitive(not state)
self._compact_pos.set_sensitive(not state)
if state:
self._show_compact.set_active(True)
self.plugin_settings[self.gs.PluginKey.DISPLAY_TYPE] = 1
else:
self.plugin_settings[self.gs.PluginKey.DISPLAY_TYPE] = 2