This file is indexed.

/usr/lib/rhythmbox/plugins/alternative-toolbar/alternative-toolbar.py is in rhythmbox-plugin-alternative-toolbar 0.18.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# -*- Mode: python; coding: utf-8; tab-width: 4; indent-tabs-mode: nil; -*-
#
# Copyright (C) 2015 - 2018 - David Mohammed <fossfreedom@ubuntu.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, 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 rb
import gi
from gi.repository import GObject
from gi.repository import Gio
from gi.repository import Gtk
gi.require_version('Peas', '1.0')
from gi.repository import Peas
from gi.repository import RB

from alttoolbar_plugins import PluginDialog
from alttoolbar_preferences import CoverLocale
from alttoolbar_preferences import GSetting
from alttoolbar_preferences import Preferences
from alttoolbar_rb3compat import ActionGroup
from alttoolbar_rb3compat import ApplicationShell
from alttoolbar_rb3compat import gtk_version
from alttoolbar_type import AltToolbarCompact
from alttoolbar_type import AltToolbarHeaderBar
from alttoolbar_type import AltToolbarStandard

view_menu_ui = """
<ui>
  <menubar name="MenuBar">
    <menu name="ViewMenu" action="View">
        <menuitem name="Show Toolbar" action="ToggleToolbar" />
        <menuitem name="Show Source Toolbar"
        action="ToggleSourceMediaToolbar" />
    </menu>
  </menubar>
</ui>
"""

view_seek_menu_ui = """
<ui>
  <menubar name="MenuBar">
    <menu name="ViewMenu" action="View">
        <menuitem name="SeekBackward" action="SeekBackward" />
        <menuitem name="SeekForward" action="SeekForward" />
    </menu>
  </menubar>
</ui>
"""

seek_backward_time = 5
seek_forward_time = 10


class AltToolbarPlugin(GObject.Object, Peas.Activatable):
    """
    Main class of the plugin. Manages the activation and deactivation of the
    plugin.
    """
    __gtype_name = 'AltToolbarPlugin'
    object = GObject.property(type=GObject.Object)
    display_page_tree_visible = GObject.property(type=bool, default=False)
    show_album_art = GObject.property(type=bool, default=False)
    show_song_position_slider = GObject.property(type=bool, default=False)
    playing_label = GObject.property(type=bool, default=False)

    # signals
    # toolbar-visibility - bool parameter True = visible, False = not visible
    __gsignals__ = {
        'toolbar-visibility': (GObject.SIGNAL_RUN_LAST, None, (bool,))
    }

    def __init__(self):
        """
        Initialises the plugin object.
        """
        GObject.Object.__init__(self)
        self.appshell = None
        self.sh_psc = self.sh_op = self.sh_pc = None

    def do_activate(self):
        """
        Called by Rhythmbox when the plugin is activated. It creates the
        plugin's source and connects signals to manage the plugin's
        preferences.
        """

        self.shell = self.object
        self.db = self.shell.props.db
        self.shell_player = self.shell.props.shell_player

        # Prepare internal variables
        self.song_duration = 0
        self.entry = None
        self._plugin_dialog_width = 760
        self._plugin_dialog_height = 550

        # locale stuff
        cl = CoverLocale()
        cl.switch_locale(cl.Locale.LOCALE_DOMAIN)

        # for custom icons ensure we start looking in the plugin img folder
        # as a fallback
        theme = Gtk.IconTheme.get_default()
        theme.append_search_path(rb.find_plugin_file(self, 'img'))

        # Find the Rhythmbox Toolbar
        self.rb_toolbar = AltToolbarPlugin.find(self.shell.props.window,
                                                'main-toolbar', 'by_id')

        # get values from gsettings
        self.gs = GSetting()
        self.plugin_settings = self.gs.get_setting(self.gs.Path.PLUGIN)

        display_type = self.plugin_settings[self.gs.PluginKey.DISPLAY_TYPE]
        self.volume_control = self.plugin_settings[
            self.gs.PluginKey.VOLUME_CONTROL]
        self.show_compact_toolbar = self.plugin_settings[
            self.gs.PluginKey.SHOW_COMPACT]
        self.start_hidden = self.plugin_settings[
            self.gs.PluginKey.START_HIDDEN]
        self.inline_label = self.plugin_settings[
            self.gs.PluginKey.INLINE_LABEL]
        self.compact_progressbar = self.plugin_settings[
            self.gs.PluginKey.COMPACT_PROGRESSBAR]
        self.enhanced_sidebar = self.plugin_settings[
            self.gs.PluginKey.ENHANCED_SIDEBAR]
        self.show_tooltips = self.plugin_settings[
            self.gs.PluginKey.SHOW_TOOLTIPS]
        self.enhanced_plugins = self.plugin_settings[
            self.gs.PluginKey.ENHANCED_PLUGINS]
        self.horiz_categories = self.plugin_settings[
            self.gs.PluginKey.HORIZ_CATEGORIES]
        self.app_menu = self.plugin_settings[
            self.gs.PluginKey.APP_MENU]
        self.prefer_dark_theme = \
            self.plugin_settings[self.gs.PluginKey.DARK_THEME]

        # Add the various application view menus
        self.appshell = ApplicationShell(self.shell)
        self._add_menu_options()

        # Determine what type of toolbar is to be displayed
        default = Gtk.Settings.get_default()

        if display_type == 0:
            if (
                    not default.props.gtk_shell_shows_app_menu) or \
                    default.props.gtk_shell_shows_menubar:
                display_type = 2
            else:
                display_type = 1

        self.toolbar_type = None
        if display_type == 1:
            self.toolbar_type = AltToolbarHeaderBar()
        elif self.show_compact_toolbar:
            self.toolbar_type = AltToolbarCompact()
        else:
            self.toolbar_type = AltToolbarStandard()

        self.toolbar_type.initialise(self)
        self.toolbar_type.post_initialise()

        if self.enhanced_plugins:
            # redirect plugins action to our implementation

            action = Gio.SimpleAction.new('plugins', None)
            action.connect('activate', self._display_plugins)
            self.shell.props.application.add_action(action)

        self._connect_signals()
        self._connect_properties()

        # allow other plugins access to this toolbar
        self.shell.alternative_toolbar = self

        cl.switch_locale(cl.Locale.RB)

    def _display_plugins(self, *args):
        """
          display our implementation of the LibPeas Plugin window
        """

        has_headerbar = isinstance(self.toolbar_type, AltToolbarHeaderBar)

        if gtk_version() < 3.12:
            has_headerbar = False

        dlg = PluginDialog(self.shell.props.window, has_headerbar)
        response = 0
        dlg.set_default_size(self._plugin_dialog_width,
                             self._plugin_dialog_height)

        while response >= 0:
            response = dlg.run()
            print(response)

        self._plugin_dialog_width, self._plugin_dialog_height = dlg.get_size()
        dlg.destroy()

    def _add_menu_options(self):
        """
          add the various menu options to the application
        """
        self.seek_action_group = ActionGroup(self.shell,
                                             'AltToolbarPluginSeekActions')
        self.seek_action_group.add_action(func=self.on_skip_backward,
                                          action_name='SeekBackward',
                                          label=_("Seek Backward"),
                                          action_type='app', accel="<Alt>Left",
                                          tooltip=_(
                                              "Seek backward, in current "
                                              "track, by 5 seconds."))
        self.seek_action_group.add_action(func=self.on_skip_forward,
                                          action_name='SeekForward',
                                          label=_("Seek Forward"),
                                          action_type='app',
                                          accel="<Alt>Right",
                                          tooltip=_(
                                              "Seek forward, in current "
                                              "track, by 10 seconds."))

        self.appshell.insert_action_group(self.seek_action_group)
        self.appshell.add_app_menuitems(view_seek_menu_ui,
                                        'AltToolbarPluginSeekActions', 'view')

        self.toggle_action_group = ActionGroup(self.shell,
                                               'AltToolbarPluginActions')
        self.toggle_action_group.add_action(func=self.toggle_visibility,
                                            action_name='ToggleToolbar',
                                            label=_(
                                                "Show Play-Controls Toolbar"),
                                            action_state=ActionGroup.TOGGLE,
                                            action_type='app',
                                            tooltip=_(
                                                "Show or hide the "
                                                "play-controls toolbar"))
        self.toggle_action_group.add_action(
            func=self.toggle_sourcemedia_visibility,
            action_name='ToggleSourceMediaToolbar',
            label=_("Show Source Toolbar"),
            action_state=ActionGroup.TOGGLE,
            action_type='app', accel="<Ctrl>t",
            tooltip=_("Show or hide the source toolbar"))

        self.appshell.insert_action_group(self.toggle_action_group)
        self.appshell.add_app_menuitems(view_menu_ui,
                                        'AltToolbarPluginActions', 'view')

    def _connect_properties(self):
        """
          bind plugin properties to various gsettings that we dynamically
          interact with
        """
        self.plugin_settings.bind(self.gs.PluginKey.PLAYING_LABEL, self,
                                  'playing_label',
                                  Gio.SettingsBindFlags.GET)

    def _connect_signals(self):
        """
          connect to various rhythmbox signals that the toolbars need
        """
        self.sh_display_page_tree = self.shell.props.display_page_tree.connect(
            "selected", self.on_page_change
        )

        self.sh_psc = self.shell_player.connect("playing-song-changed",
                                                self._sh_on_song_change)

        self.sh_op = self.shell_player.connect("elapsed-changed",
                                               self._sh_on_playing)

        self.sh_pc = self.shell_player.connect("playing-changed",
                                               self._sh_on_playing_change)

        self.sh_pspc = self.shell_player.connect(
            "playing-song-property-changed",
            self._sh_on_song_property_changed)

        self.rb_settings = Gio.Settings.new('org.gnome.rhythmbox')

        self.rb_settings.bind('show-album-art', self, 'show_album_art',
                              Gio.SettingsBindFlags.GET)
        self.connect('notify::show-album-art',
                     self.show_album_art_settings_changed)
        self.show_album_art_settings_changed(None)

        self.rb_settings.bind('show-song-position-slider', self,
                              'show_song_position_slider',
                              Gio.SettingsBindFlags.GET)
        self.connect('notify::show-song-position-slider',
                     self.show_song_position_slider_settings_changed)
        self.show_song_position_slider_settings_changed(None)

    def _sh_on_song_property_changed(self, sp, uri, property, old, new):
        """
           shell-player "playing-song-property-changed" signal handler
        """
        if sp.get_playing() and property in \
                ('artist',
                 'album',
                 'title',
                 RB.RHYTHMDB_PROP_STREAM_SONG_ARTIST,
                 RB.RHYTHMDB_PROP_STREAM_SONG_ALBUM,
                 RB.RHYTHMDB_PROP_STREAM_SONG_TITLE):
            entry = sp.get_playing_entry()
            self.toolbar_type.display_song(entry)

    def _sh_on_playing_change(self, player, playing):
        """
           shell-player "playing-change" signal handler
        """
        self.toolbar_type.play_control_change(player, playing)
        if (self.song_duration != 0):
            self.toolbar_type.enable_slider(True)
        else:
            self.toolbar_type.enable_slider(False)
            if (hasattr(self.toolbar_type, "total_time_label")):
                label = ""
                self.toolbar_type.total_time_label.set_markup(label)

    def _sh_on_song_change(self, player, entry):
        """
           shell-player "playing-song-changed" signal handler
        """
        if (entry is not None):
            self.song_duration = entry.get_ulong(RB.RhythmDBPropType.DURATION)
        else:
            self.song_duration = 0

        self.toolbar_type.display_song(entry)

    def _sh_on_playing(self, player, second):
        """
           shell-player "elapsed-changed" signal handler
        """
        if not hasattr(self.toolbar_type, 'song_progress'):
            return

        if (self.song_duration != 0):
            self.toolbar_type.song_progress.progress = float(
                second) / self.song_duration

            print(self.toolbar_type.song_progress.progress)

            try:
                valid, time = player.get_playing_time()
                if not valid or time == 0:
                    return
            except:
                return

            m, s = divmod(time, 60)
            h, m = divmod(m, 60)

            tm, ts = divmod(self.song_duration, 60)
            th, tm = divmod(tm, 60)

            if th == 0:
                label = "<small>{time} / {ttime}</small>".format(
                    time="%02d:%02d" % (m, s),
                    ttime="%02d:%02d" % (tm, ts))
            else:
                label = "<small>{time}</small>".format(
                    time="%d:%02d:%02d" % (h, m, s))

            self.toolbar_type.total_time_label.set_markup(label)

    def on_skip_backward(self, *args):
        """
           keyboard seek backwards signal handler
        """
        sp = self.object.props.shell_player
        if (sp.get_playing()[1]):
            seek_time = sp.get_playing_time()[1] - seek_backward_time
            print(seek_time)
            if (seek_time < 0):
                seek_time = 0

            print(seek_time)
            sp.set_playing_time(seek_time)

    def on_skip_forward(self, *args):
        """
           keyboard seek forwards signal handler
        """
        sp = self.object.props.shell_player
        if (sp.get_playing()[1]):
            seek_time = sp.get_playing_time()[1] + seek_forward_time
            song_duration = sp.get_playing_song_duration()
            if (song_duration > 0):  # sanity check
                if (seek_time > song_duration):
                    seek_time = song_duration

                sp.set_playing_time(seek_time)

    def show_song_position_slider_settings_changed(self, *args):
        """
           rhythmbox show-slider signal handler
        """
        self.toolbar_type.show_slider(self.show_song_position_slider)

    def show_album_art_settings_changed(self, *args):
        """
           rhythmbox show-album-art signal handler
        """
        self.toolbar_type.show_cover(self.show_album_art)

    def on_page_change(self, display_page_tree, page):
        """
           sources display-tree signal handler
        """
        print("page changed", page)
        self.toolbar_type.reset_categories_pos(page)
        self.toolbar_type.reset_toolbar(page)
        self.toolbar_type.reset_entryview(page)

    @staticmethod
    def find(node, search_id, search_type, button_label=None):
        """
        find various GTK Widgets
        :param node: node is the starting container to find from
        :param search_id: search_id is the GtkWidget type string or
        GtkWidget name
        :param search_type: search_type is the type of search
                            "by_name" to search by the type of GtkWidget
                            e.g. GtkButton
                            "by_id" to search by the GtkWidget (glade name)
                            e.g. box_1
        :param button_label: button_label to find specific buttons where we
        cannot use by_id
        :return:N/A
        """

        # Couldn't find better way to find widgets than loop through them
        # print("by_name %s by_id %s" % (node.get_name(),
        # Gtk.Buildable.get_name(node)))

        def extract_label(button):
            label = button.get_label()
            if label:
                return label

            child = button.get_child()
            if child and child.get_name() == "GtkLabel":
                return child.get_text()

            return None

        if isinstance(node, Gtk.Buildable):
            if search_type == 'by_id':
                if Gtk.Buildable.get_name(node) == search_id:
                    if button_label is None or \
                            ('Button' in node.get_name()
                             and extract_label(node) == button_label):
                        return node
            elif search_type == 'by_name':
                if node.get_name() == search_id:
                    if button_label is None or \
                            ('Button' in node.get_name()
                             and extract_label(node) == button_label):
                        return node

        if isinstance(node, Gtk.Container):
            for child in node.get_children():
                ret = AltToolbarPlugin.find(child, search_id, search_type,
                                            button_label)
                if ret:
                    return ret

        return None

    def do_deactivate(self):
        """
        Called by Rhythmbox when the plugin is deactivated. It makes sure to
        free all the resources used by the plugin.
        """
        del self.db

        if self.sh_op:
            self.shell_player.disconnect(self.sh_op)
            self.shell_player.disconnect(self.sh_psc)
            self.shell_player.disconnect(self.sh_pc)
            self.shell_player.disconnect(self.sh_pspc)
            # self.disconnect(self.sh_display_page)
            self.shell.props.display_page_tree.disconnect(
                self.sh_display_page_tree)
            del self.shell_player

        if self.appshell:
            self.appshell.cleanup()

        self.rb_toolbar.set_visible(True)

        self.toolbar_type.cleanup()

        del self.shell

    def toggle_visibility(self, action, param=None, data=None):
        """
        Display or Hide PlayControls signal handler
        :param action:
        :param param:
        :param data:
        :return:
        """
        action = self.toggle_action_group.get_action('ToggleToolbar')

        self.toolbar_type.set_visible(action.get_active())

    def toggle_sourcemedia_visibility(self, action, param=None, data=None):
        """
        Display or Hide the source toolbar
        :param action:
        :param param:
        :param data:
        :return:
        """
        action = self.toggle_action_group.get_action(
            'ToggleSourceMediaToolbar')

        self.toolbar_type.source_toolbar_visibility(action.get_active())

    def _translation_helper(self):
        """
        a method just to help out with translation strings
        it is not meant to be called by itself
        """

        # define .plugin text strings used for translation
        plugin = _('Alternative Toolbar')
        plugin += "dummy"
        desc = _(
            'Replace the Rhythmbox large toolbar with a Client-Side '
            'Decorated or Compact Toolbar which can be hidden')

        desc += "dummy"
        # stop PyCharm removing the Preference import on optimisation
        pref = Preferences()
        return pref

    def get_toolbar(self, callback):
        """
        a method to return the toolbar itself
        :param callback: function callback - func(AT.ToolbarCallback)
        passed
        :return:
        """

        self.toolbar_type.setup_completed_async(callback)