~andrewsomething/exaile/karmic

« back to all changes in this revision

Viewing changes to xl/player/queue.py

  • Committer: Aren Olson
  • Date: 2009-09-12 00:36:59 UTC
  • Revision ID: reacocard@gmail.com-20090912003659-w373sg0n04uoa8op
remove useless files, add soem of the fixes from lp bug 420019

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008-2009 Adam Olsen 
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2, or (at your option)
6
 
# any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
 
 
17
 
 
18
 
from xl import playlist, settings, event, common
19
 
import logging, time
20
 
logger = logging.getLogger(__name__)
21
 
 
22
 
try:
23
 
    import cPickle as pickle
24
 
except:
25
 
    import pickle
26
 
 
27
 
class PlayQueue(playlist.Playlist):
28
 
 
29
 
    """
30
 
        Manages the queue of songs to be played
31
 
    """
32
 
 
33
 
    def __init__(self, player, location=None):
34
 
        self.current_playlist = None
35
 
        self.current_pl_track = None
36
 
        playlist.Playlist.__init__(self, name="Queue")
37
 
        self.player = player
38
 
        player._set_queue(self)
39
 
        self.stop_track = None
40
 
        if location is not None:
41
 
            self.load_from_location(location)
42
 
 
43
 
    def set_current_playlist(self, playlist):
44
 
        self.current_playlist = playlist
45
 
 
46
 
    def set_current_pl_track(self, track):
47
 
        self.current_pl_track = track
48
 
 
49
 
    def peek(self):
50
 
        track = playlist.Playlist.peek(self)
51
 
        if track == None:
52
 
            if self.current_playlist:
53
 
                track = self.current_playlist.peek()
54
 
        return track
55
 
 
56
 
    def next(self, player=True, track=None):
57
 
        """
58
 
            Goes to the next track, either in the queue, or in the current
59
 
            playlist.  If a track is passed in, that track is played
60
 
 
61
 
            :param player: play the track in addition to returning it
62
 
            :param track: if passed, play this track
63
 
        """
64
 
        if not track:
65
 
            if self.player.current and self.player.current == self.stop_track:
66
 
                self.player.stop()
67
 
                event.log_event('stop_track', self, self.stop_track)
68
 
                self.stop_track = None
69
 
                return None
70
 
 
71
 
            if not self.ordered_tracks:
72
 
                if self.current_playlist:
73
 
                    track = self.current_playlist.next()
74
 
                    self.current_playlist.current_playing = True
75
 
                    self.current_playing = False
76
 
            else:
77
 
                track = self.ordered_tracks.pop(0)
78
 
                self.current_pos = 0
79
 
                self.current_playing = True
80
 
                if self.current_playlist:
81
 
                    self.current_playlist.current_playing = False
82
 
        if player:
83
 
            if not track:
84
 
                self.player.stop()
85
 
                event.log_event("playback_playlist_end", self, 
86
 
                        self.current_playlist)
87
 
                return
88
 
            self.player.play(track)
89
 
        if not track:
90
 
            event.log_event("playback_playlist_end", self, 
91
 
                        self.current_playlist)
92
 
        return track
93
 
 
94
 
    def prev(self):
95
 
        track = None
96
 
        if self.player.current:
97
 
            if self.player.get_time() < 5:
98
 
                if self.current_playlist:
99
 
                    track = self.current_playlist.prev()
100
 
            else:
101
 
                track = self.player.current
102
 
        else:
103
 
            track = self.get_current()
104
 
        self.player.play(track)
105
 
        return track
106
 
 
107
 
    def get_current(self):
108
 
        if self.player.current and self.current_pos > 0:
109
 
            current = self.player.current
110
 
        else:
111
 
            current = playlist.Playlist.get_current(self)
112
 
            if current == None and self.current_playlist:
113
 
                current = self.current_playlist.get_current()
114
 
        return current
115
 
 
116
 
    def get_current_pos(self):
117
 
        return 0
118
 
 
119
 
    def play(self, track=None):
120
 
        """
121
 
            start playback, either from the passed track or from already 
122
 
            queued tracks
123
 
        """
124
 
        if self.player.is_playing() and not track:
125
 
            return
126
 
        if not track:
127
 
            track = self.get_current()
128
 
        if track:
129
 
            self.player.play(track)
130
 
            if track in self.ordered_tracks:
131
 
                self.ordered_tracks.remove(track)
132
 
        else:
133
 
            self.next()
134
 
 
135
 
    def _save_player_state(self, location):
136
 
        state = {}
137
 
        state['state'] = self.player.get_state()
138
 
        state['position'] = self.player.get_time()
139
 
        state['_playtime_stamp'] = self.player._playtime_stamp
140
 
        f = open(location, 'wb')
141
 
        pickle.dump(state, f, protocol = 2)
142
 
        f.close()
143
 
 
144
 
    @common.threaded
145
 
    def _restore_player_state(self, location):
146
 
        if not settings.get_option("player/resume_playback", True):
147
 
            return
148
 
 
149
 
        try:
150
 
            f = open(location, 'rb')
151
 
            state = pickle.load(f)
152
 
            f.close()
153
 
        except:
154
 
            return
155
 
 
156
 
        for req in ['state', 'position', '_playtime_stamp']:
157
 
            if req not in state:
158
 
                return
159
 
 
160
 
        if state['state'] in ['playing', 'paused']:
161
 
            vol = self.player._get_volume()
162
 
            self.player._set_volume(0)
163
 
            self.play()
164
 
            
165
 
            if not self.player.current:
166
 
                return
167
 
 
168
 
            self.player.seek(state['position'])
169
 
            if state['state'] == 'paused' or \
170
 
                    settings.get_option("player/resume_paused", False):
171
 
                self.player.toggle_pause()
172
 
            self.player._set_volume(vol)
173
 
            self.player._playtime_stamp = state['_playtime_stamp']