Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Owais Lone December 09, 2011
file 124 lines (102 sloc) 4.639 kb
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from gi.repository import GObject
from gi.repository import Peas
from gi.repository import RB
from gi.repository import Gio
from gi.repository.GLib import Variant


GSETTINGS_KEY = "org.gnome.rhythmbox.plugins.remember-the-rhythm"
KEY_PLAYBACK_TIME = 'playback-time'
KEY_LOCATION = 'last-entry-location'
KEY_PLAYLIST = 'playlist'
KEY_BROWSER_VALUES = 'browser-values'


class RememberTheRhythm(GObject.Object, Peas.Activatable):

    __gtype_name = 'RememberTheRhythm'
    object = GObject.property(type=GObject.Object)

    first_run = False

    def __init__(self):
        GObject.Object.__init__(self)
        self.settings = Gio.Settings.new(GSETTINGS_KEY)
        self.location = self.settings.get_string(KEY_LOCATION)
        self.playlist = self.settings.get_string(KEY_PLAYLIST)
        self.playback_time = self.settings.get_uint(KEY_PLAYBACK_TIME)
        self.browser_values_list = self.settings.get_value(KEY_BROWSER_VALUES)
        self.source = None

    def do_activate(self):
        self.shell = self.object
        self.library = self.shell.props.library_source
        self.shell_player = self.shell.props.shell_player
        self.playlist_manager = self.shell.props.playlist_manager
        self.db = self.shell.props.db
        self.backend_player = self.shell_player.props.player
        self.shell_player.connect('playing-changed', self.playing_changed)
        self.shell_player.connect('playing-source-changed', self.playing_source_changed)
        self.shell.props.db.connect('load-complete', self.load_complete)
        self.shell_player.connect('elapsed-changed', self.elapsed_changed)

    def do_deactivate(self):
        self.save_rhythm()

    def load_complete(self, *args, **kwargs):
        if self.location:
            entry = self.db.entry_lookup_by_location(self.location)
            if self.playlist:
                playlists = self.playlist_manager.get_playlists()
                for playlist in playlists:
                    if playlist.props.name == self.playlist:
                        self.source = playlist
                        break
            if not self.source:
                self.source = self.shell.guess_source_for_uri(self.location)
            self.shell_player.set_playing_source(self.source)
            self.shell_player.set_selected_source(self.source)
            self.shell_player.play_entry(entry, self.source)
            self.first_run = True

    def playing_source_changed(self, player, source, data=None):
        if source:
            self.source = source
            if self.source in self.playlist_manager.get_playlists():
                self.settings.set_string('playlist', self.source.props.name)
            else:
                self.settings.set_string('playlist', '')

    def playing_changed(self, player, playing, data=None):
        if self.first_run:
            self.first_run = False
            try:
                self.shell_player.set_playing_time(self.playback_time)
            except:
                pass
            GObject.idle_add(self.init_source)
            return

        try:
            entry = self.shell_player.get_playing_entry()
            self.location = entry.get_string(RB.RhythmDBPropType.LOCATION)
            GObject.idle_add(self.save_rhythm, 0)
        except:
            return


    def elapsed_changed(self, player, entry, data=None):
        try:
            self.playback_time = self.shell_player.get_playing_time()[1]
        except:
            pass

    def get_source_data(self):
        if self.source:
            views = self.source.get_property_views()
            browser_values_list = []
            for view in views:
                browser_values_list.append(view.get_selection())
            self.browser_values_list = Variant('aas', browser_values_list)
            self.settings.set_value(KEY_BROWSER_VALUES, self.browser_values_list)

    def init_source(self):
        if self.source:
            views = self.source.get_property_views()
            for i, view in enumerate(views):
                value = self.browser_values_list[i]
                if value:
                    view.set_selection(value)
            self.shell.props.display_page_tree.select(self.source)
            self.shell_player.jump_to_current()

    def save_rhythm(self, pb_time=None):
        if self.location:
            pb_time = pb_time == None and self.playback_time or pb_time
            self.settings.set_uint(KEY_PLAYBACK_TIME, pb_time)
            self.settings.set_string(KEY_LOCATION, self.location)
        GObject.idle_add(self.get_source_data)


Something went wrong with that request. Please try again.