File: __init__.py

package info (click to toggle)
exaile 0.3.2.2-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,564 kB
  • sloc: python: 35,424; makefile: 265; sh: 58
file content (421 lines) | stat: -rw-r--r-- 13,928 bytes parent folder | download
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
# Copyright (C) 2009-2010 Mathias Brodala
#
# 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 2, 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., 675 Mass Ave, Cambridge, MA 02139, USA.

import cairo
import gobject
import gtk
import os

from xl import event, plugins, settings, xdg
from xl.formatter import TrackFormatter
from xl.nls import gettext as _
from xlgui import icons

import minimode_preferences, mmwidgets

MINIMODE = None

def __migrate_fixed_controls():
    """
        Makes sure fixed controls are selected,
        mostly for migration from older versions
    """
    option_name = 'plugin/minimode/selected_controls'

    if settings.MANAGER.has_option(option_name):
        selected_controls = settings.get_option(option_name)

        if not 'restore' in selected_controls:
            selected_controls += ['restore']
            settings.set_option(option_name, selected_controls)

def enable(exaile):
    """
        Enables the mini mode plugin
    """
    __migrate_fixed_controls()

    if exaile.loading:
        event.add_callback(_enable, 'gui_loaded')
    else:
        _enable(None, exaile, None)

def _enable(event, exaile, nothing):
    """
        Handles the deferred enable call
    """
    global MINIMODE
    MINIMODE = MiniMode(exaile)

def disable(exaile):
    """
        Disables the mini mode plugin
    """
    global MINIMODE
    MINIMODE.destroy()
    MINIMODE = None

def get_preferences_pane():
    return minimode_preferences

class MiniMode(gtk.Window):
    """
        A compact mode for Exaile
    """
    def __init__(self, exaile):
        """
            Sets up the mini mode main window and
            options to access it
        """
        gtk.Window.__init__(self)
        self.set_title('Exaile')
        self.set_resizable(False)

        self.exaile = exaile
        self._active = False
        self.defaults = self.exaile.plugins.get_plugin_default_preferences('minimode')
        self.defaults['plugin/minimode/horizontal_position'] = 10
        self.defaults['plugin/minimode/vertical_position'] = 10

        self.formatter = TrackFormatter(
            self.get_option('plugin/minimode/track_title_format')
        )

        self.box = mmwidgets.WidgetBox(spacing=3)
        self.border_frame = gtk.Frame()
        self.border_frame.add(self.box)
        self.add(self.border_frame)

        basedir = os.path.dirname(os.path.abspath(__file__))
        icons.MANAGER.add_stock_from_directory('exaile-minimode',
            os.path.join(basedir, 'icons'))

        self.menuitem = mmwidgets.MenuItem(self.on_menuitem_activate)
        self.exaile.gui.builder.get_object('view_menu').append(self.menuitem)
        self.menuitem.show()

        key, modifier = gtk.accelerator_parse('<Control><Alt>M')
        self.accel_group = gtk.AccelGroup()
        self.menuitem.add_accelerator('activate', self.accel_group,
            key, modifier, gtk.ACCEL_VISIBLE)
        self.exaile.gui.main.window.add_accel_group(self.accel_group)
        self.add_accel_group(self.accel_group)

        self.register_widgets()
        self.update_widgets(self.get_option('plugin/minimode/selected_controls'))
        self.update_position()

        self._configure_id = None
        self._main_visible_toggle_id = None

        self.connect('show', self.on_show)
        self.connect('expose-event', self.on_expose_event)
        self.connect('screen-changed', self.on_screen_changed)
        self.connect('delete-event', self.on_delete_event)
        self.exaile.gui.main.connect('main-visible-toggle',
            self.on_main_visible_toggle)

    def destroy(self):
        """
            Cleans up and hides
            the mini mode window
        """
        if self._configure_id is not None:
            self.disconnect(self._configure_id)
            self._configure_id = None
        if self._main_visible_toggle_id is not None:
            self.disconnect(self._main_visible_toggle_id)
            self._main_visible_toggle_id = None

        self.remove_accel_group(self.accel_group)
        self.exaile.gui.main.window.remove_accel_group(self.accel_group)
        self.exaile.gui.builder.get_object('view_menu').remove(self.menuitem)

        self._active = False
        self._hide()
        self.box.destroy()

        gtk.Window.destroy(self)

    def _hide(self):
        """
            Hides the mini mode window, shows the main window
        """
        if self._active:
            self.hide()
        else:
            if self._configure_id is not None:
                self.disconnect(self._configure_id)
                self._configure_id = None
            self.hide_all()

        self.exaile.gui.main.window.show()

    def _show(self):
        """
            Shows the mini mode window, hides the main window
        """
        self.exaile.gui.main.window.hide()

        if not self._active:
            self.update_window()

        self.show_all()
        if self._configure_id is None:
            self._configure_id = self.connect('configure-event',
                self.on_configure_event)

    def toggle_visible(self):
        """
            Toggles visibility of the mini mode window
        """
        if self.get_property('visible'):
            self._hide()
        else:
            self._show()

    def update_window(self):
        """
            Changes the appearance of the mini mode window
            based on user setting
        """
        for option in self.defaults.keys():
            value = self.get_option(option)

            if option == 'plugin/minimode/always_on_top':
                self.set_keep_above(value)
            elif option == 'plugin/minimode/show_in_panel':
                self.set_property('skip-taskbar-hint', not value)
            elif option == 'plugin/minimode/on_all_desktops':
                if value: self.stick()
                else: self.unstick()
            elif option == 'plugin/minimode/display_window_decorations':
                if value:
                    decoration_type = self.get_option('plugin/minimode/window_decoration_type')

                    if decoration_type == 'full':
                        self.set_decorated(True)
                        self.border_frame.set_shadow_type(gtk.SHADOW_NONE)
                    elif decoration_type == 'simple':
                        self.set_decorated(False)
                        self.border_frame.set_shadow_type(gtk.SHADOW_OUT)
                else:
                    self.set_decorated(False)
                    self.border_frame.set_shadow_type(gtk.SHADOW_NONE)
            elif option == 'plugin/minimode/use_alpha':
                self.unrealize()

                screen = self.get_screen()
                colormap = screen.get_rgba_colormap() or screen.get_rgb_colormap()
                self.set_colormap(colormap)
                self.set_app_paintable(value)

                self.realize()
            elif option == 'plugin/minimode/horizontal_position':
                self.update_position()
            elif option == 'plugin/minimode/vertical_position':
                self.update_position()
            elif option == 'plugin/minimode/selected_controls':
                self.update_widgets(value)
            elif option == 'plugin/minimode/track_title_format':
                self.formatter.set_property('format', value)

    def update_position(self):
        """
            Changes the position of the mini mode window
            based on user setting
        """
        x = self.get_option('plugin/minimode/horizontal_position')
        y = self.get_option('plugin/minimode/vertical_position')
        self.move(int(x), int(y))

    def register_widgets(self):
        """
            Registers all available widget types
        """
        widgets = {
            'previous': (mmwidgets.Button,
                [gtk.STOCK_MEDIA_PREVIOUS, _('Previous Track'),
                    self.on_previous_clicked]),
            'next': (mmwidgets.Button,
                [gtk.STOCK_MEDIA_NEXT, _('Next Track'),
                    self.on_next_clicked]),
            'play_pause': (mmwidgets.PlayPauseButton,
                [self.exaile.player, self.on_play_pause_clicked]),
            'stop': (mmwidgets.StopButton,
                [self.exaile.player, self.exaile.queue]),
            'volume': (mmwidgets.VolumeButton,
                [self.exaile.player, self.on_volume_changed]),
            'restore': (mmwidgets.RestoreButton,
                [self.accel_group, self.on_restore_clicked]),
            'progress_bar': (mmwidgets.ProgressBar,
                [self.exaile.player, self.on_seeked]),
            'track_selector': (mmwidgets.TrackSelector,
                [self.exaile.gui.main, self.exaile.queue, self.formatter,
                 self.on_track_change]),
            'playlist_button': (mmwidgets.PlaylistButton,
                [self.exaile.gui.main, self.exaile.player, self.exaile.queue,
                 self.formatter, self.on_track_change]),
            'progress_button': (mmwidgets.ProgressButton,
                [self.on_track_change, self.on_seeked]),
            'rating': (mmwidgets.RatingWidget, [])
        }

        self.box.register_widgets(widgets)

    def update_widgets(self, ids):
        """
            Adds and removes widgets
        """
        self.box.update_widgets(ids)

    def get_option(self, option):
        """
            Wrapper function, automatically inserts default values
        """
        return settings.get_option(option, self.defaults[option])

    def set_option(self, option, value):
        """
            Wrapper function, automatically inserts default values
            and sets value only if it has changed
        """
        oldvalue = self.get_option(option)
        if value != oldvalue:
            settings.set_option(option, value)

    def on_menuitem_activate(self, menuitem):
        """
            Shows mini mode on activation of a menu item
        """
        self.toggle_visible()
        self._active = not self._active

    def on_previous_clicked(self, button):
        """
            Jumps to the previous track
        """
        self.exaile.queue.prev()

    def on_next_clicked(self, button):
        """
            Jumps to the next track
        """
        self.exaile.queue.next()

    def on_play_pause_clicked(self, button):
        """
            Toggles between playback and pause mode
        """
        if self.exaile.player.is_paused() or self.exaile.player.is_playing():
            self.exaile.player.toggle_pause()
        else:
            self.exaile.queue.play()

    def on_restore_clicked(self, button):
        """
            Hides mini mode on button click
        """
        self.toggle_visible()
        self._active = False

    def on_track_change(self, sender, track):
        """
            Handles changes in track list controls
        """
        if track is not None:
            index = self.exaile.queue.current_playlist.index(track)
            self.exaile.queue.current_playlist.set_current_pos(index)
            self.exaile.queue.play(track)

    def on_seeked(self, progress_bar, position):
        """
            Handles seeking in the progress bar
        """
        self.exaile.player.set_progress(position)

    def on_volume_changed(self, volume_button, value):
        """
            Handles changes to the volume
        """
        settings.set_option('player/volume', value)

    def on_main_visible_toggle(self, main):
        """
            Handles tray icon toggles
        """
        if self._active:
            if self.get_property('visible'):
                self.hide()
            else:
                self.show()
            return True
        return False

    def on_configure_event(self, widget, event):
        """
            Handles movement of the window
        """
        x, y = self.get_position()
        self.set_option('plugin/minimode/horizontal_position', x)
        self.set_option('plugin/minimode/vertical_position', y)

    def on_show(self, widget):
        """
            Updates window size on exposure
        """
        self.resize(*self.size_request())
        self.queue_draw()

    def on_expose_event(self, widget, event):
        """
            Paints the window alpha transparency
        """
        context = widget.window.cairo_create()

        context.rectangle(event.area.x, event.area.y,
            event.area.width, event.area.height)
        context.clip()

        background = widget.style.bg[gtk.STATE_NORMAL]
        opacity = 1 - self.get_option('plugin/minimode/transparency')
        context.set_source_rgba(
            float(background.red) / 256**2,
            float(background.green) / 256**2,
            float(background.blue) / 256**2,
            opacity
        )
        context.set_operator(cairo.OPERATOR_SOURCE)

        context.paint()

    def on_screen_changed(self, widget, event):
        """
            Updates the colormap on screen change
        """
        screen = widget.get_screen()
        colormap = screen.get_rgba_colormap() or screen.get_rgb_colormap()
        self.window.set_colormap(rgbamap)

    def on_delete_event(self, widget, event):
        """
            Closes application on quit
        """
        self.hide()
        self.exaile.gui.main.quit()
        return True

# vim: et sts=4 sw=4