1# Copyright 2010 Christoph Reiter
2#           2016 Nick Boultbee
3#
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2 of the License, or
7# (at your option) any later version.
8
9from quodlibet.plugins.playorder import ShufflePlugin
10from quodlibet.order import OrderInOrder, OrderRemembered
11from quodlibet import _
12from quodlibet import app
13from quodlibet.qltk import Icons
14
15
16class FollowOrder(ShufflePlugin, OrderInOrder, OrderRemembered):
17    PLUGIN_ID = "follow"
18    PLUGIN_NAME = _("Follow Cursor")
19    PLUGIN_ICON = Icons.GO_JUMP
20    PLUGIN_DESC = _("Playback follows your selection, "
21                    "or the next song in the list once exhausted.")
22
23    __last_path = None
24
25    def next(self, playlist, iter):
26        next_fallback = OrderInOrder.next(self, playlist, iter)
27        OrderRemembered.next(self, playlist, iter)
28
29        selected = app.window.songlist.get_selected_songs()
30        if not selected:
31            return next_fallback
32
33        selected_iter = playlist.find(selected[0])
34        selected_path = playlist.get_path(selected_iter)
35        current_path = iter and playlist.get_path(iter)
36
37        if selected_path in (current_path, self.__last_path):
38            return next_fallback
39
40        self.__last_path = selected_path
41        return selected_iter
42
43    def previous(self, *args):
44        self.__last_path = None
45        return super(FollowOrder, self).previous(*args)
46
47    def set(self, playlist, iter):
48        if iter:
49            self.__last_path = playlist.get_path(iter)
50        return super(FollowOrder, self).set(playlist, iter)
51
52    def reset(self, playlist):
53        super(FollowOrder, self).reset(playlist)
54        self.__last_path = None
55