Example #1
0
        def _on_items_changed(model, position, removed, added):
            if removed > 0:
                for i in list(range(removed)):
                    self._playlist_model.remove(position)

            if added > 0:
                for i in list(range(added)):
                    coresong = model[position + i]
                    song = CoreSong(coresong.props.media, self._coreselection,
                                    self._grilo)

                    self._playlist_model.insert(position + i, song)

                    song.bind_property("state", coresong, "state",
                                       GObject.BindingFlags.SYNC_CREATE)
                    song.bind_property("validation", coresong, "validation",
                                       GObject.BindingFlags.SYNC_CREATE)
Example #2
0
    def set_player_model(self, playlist_type, model):
        if model is self._previous_playlist_model:
            for song in self._playlist_model:
                if song.props.state == SongWidget.State.PLAYING:
                    song.props.state = SongWidget.State.PLAYED

            self.emit("playlist-loaded")
            return

        def _on_items_changed(model, position, removed, added):
            if removed > 0:
                for i in list(range(removed)):
                    self._playlist_model.remove(position)

            if added > 0:
                for i in list(range(added)):
                    coresong = model[position + i]
                    song = CoreSong(
                        coresong.props.media, self._coreselection,
                        self.props.grilo)

                    self._playlist_model.insert(position + i, song)

                    song.bind_property(
                        "state", coresong, "state",
                        GObject.BindingFlags.SYNC_CREATE)
                    coresong.bind_property(
                        "validation", song, "validation",
                        GObject.BindingFlags.BIDIRECTIONAL
                        | GObject.BindingFlags.SYNC_CREATE)

        with model.freeze_notify():
            self._playlist_model.remove_all()

            if playlist_type == PlayerPlaylist.Type.ALBUM:
                proxy_model = Gio.ListStore.new(Gio.ListModel)

                for disc in model:
                    proxy_model.append(disc.props.model)

                self._flatten_model = Gfm.FlattenListModel.new(
                    CoreSong, proxy_model)
                self._flatten_model.connect("items-changed", _on_items_changed)

                for model_song in self._flatten_model:
                    song = CoreSong(
                        model_song.props.media, self._coreselection,
                        self.props.grilo)

                    self._playlist_model.append(song)
                    song.bind_property(
                        "state", model_song, "state",
                        GObject.BindingFlags.SYNC_CREATE)
                    model_song.bind_property(
                        "validation", song, "validation",
                        GObject.BindingFlags.BIDIRECTIONAL
                        | GObject.BindingFlags.SYNC_CREATE)

                self.emit("playlist-loaded")
            elif playlist_type == PlayerPlaylist.Type.ARTIST:
                proxy_model = Gio.ListStore.new(Gio.ListModel)

                for artist_album in model:
                    for disc in artist_album.model:
                        proxy_model.append(disc.props.model)

                self._flatten_model = Gfm.FlattenListModel.new(
                    CoreSong, proxy_model)
                self._flatten_model.connect("items-changed", _on_items_changed)

                for model_song in self._flatten_model:
                    song = CoreSong(
                        model_song.props.media, self._coreselection,
                        self.props.grilo)

                    self._playlist_model.append(song)
                    song.bind_property(
                        "state", model_song, "state",
                        GObject.BindingFlags.SYNC_CREATE)
                    model_song.bind_property(
                        "validation", song, "validation",
                        GObject.BindingFlags.BIDIRECTIONAL
                        | GObject.BindingFlags.SYNC_CREATE)

                self.emit("playlist-loaded")
            elif playlist_type == PlayerPlaylist.Type.SONGS:
                if self._song_signal_id:
                    self._songliststore.props.model.disconnect(
                        self._song_signal_id)

                for song in self._songliststore.props.model:
                    self._playlist_model.append(song)

                    if song.props.state == SongWidget.State.PLAYING:
                        song.props.state = SongWidget.State.PLAYED

                self._song_signal_id = self._songliststore.props.model.connect(
                    "items-changed", _on_items_changed)

                self.emit("playlist-loaded")
            elif playlist_type == PlayerPlaylist.Type.SEARCH_RESULT:
                if self._search_signal_id:
                    self._song_search_flatten.disconnect(
                        self._search_signal_id)

                for song in self._song_search_flatten:
                    self._playlist_model.append(song)

                self._search_signal_id = self._song_search_flatten.connect(
                    "items-changed", _on_items_changed)

                self.emit("playlist-loaded")
            elif playlist_type == PlayerPlaylist.Type.PLAYLIST:
                if self._playlist_signal_id:
                    self._previous_playlist_model.disconnect(
                        self._playlist_signal_id)

                for model_song in model:
                    song = CoreSong(
                        model_song.props.media, self._coreselection,
                        self.props.grilo)

                    self._playlist_model.append(song)

                    song.bind_property(
                        "state", model_song, "state",
                        GObject.BindingFlags.SYNC_CREATE)
                    model_song.bind_property(
                        "validation", song, "validation",
                        GObject.BindingFlags.BIDIRECTIONAL
                        | GObject.BindingFlags.SYNC_CREATE)

                self._playlist_signal_id = model.connect(
                    "items-changed", _on_items_changed)

                self.emit("playlist-loaded")

        self._previous_playlist_model = model