pcremote-client-n8x0 -> client sources
[remotepc] / pcremote-server-desktop-60 / debian / pcremote-server / usr / share / pcremote-server / players / amarok.py
1 # -*- coding: utf-8 -*-
2
3 #  ****************************************************************************
4 #  Copyright (c) 2008 INdT/Fucapi.
5 #  This program is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU Lesser General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU Lesser General Public License for more details.
14 #
15 #  You should have received a copy of the GNU Lesser General Public License
16 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 #  ============================================================================
19 #  Project Name : PC Remote
20 #  Author       : Jonatas Isvi
21 #  Email        : jonatas.nona@gmail.com
22 #  Reviewer     :
23 #  Email        : 
24 #  Version      : 1.0
25 #  Packge       : players
26 #  Description  : Amarok Player
27 #  ============================================================================
28
29 import os
30 import commands
31 import random
32 from playlist import Playlist
33 import pydcop
34
35 # command line
36 def shell(command):
37     return commands.getoutput(command)
38
39
40 # starts the amarok player application
41 def start():
42     os.popen('amarok')
43
44
45 # close the amarok player application
46 def shutdown():
47     shell('dcop amarok player stop')
48     pid = shell('pidof amarokapp')
49     shell('kill -9 %s' % pid)
50
51
52 # verifies if the amarok is running 
53 def isRunning():
54     pid = shell('pidof amarokapp')
55     if pid > 0:
56         return True
57     else:
58         return False
59
60 def send_file(addr, path):
61     shell("bluetooth-sendto --dest=%s %s" + (addr, path))
62
63 class AmarokPlayer():
64
65     """ Amarok
66     Define all states and functions of amarok player.
67     This class will build to support PCRemote Player,
68     receiving messages from any devices with a bluetooth 
69     connection.
70     """
71         
72     # some importants variables 
73     def __init__(self):
74         self.amarok = pydcop.anyAppCalled("amarok")
75         self.playlist = Playlist(self.amarok.playlist.saveCurrentPlaylist())
76         self.isPlaying()
77
78     # refresh playlist, accessing the Playlist class instance
79     def refresh_playlist(self):
80         self.playlist = Playlist(self.amarok.playlist.saveCurrentPlaylist())
81         self.isPlaying()
82
83     # get all songs of playlist
84     def song_list(self):
85         self.playlist.show()
86
87     # show current song, acessing the Playlist class instance
88     def current_song(self):
89         self.isPlaying()
90
91     # verifies if this amarok app is running
92     def isRunning(self):
93         aux = pydcop.anyAppCalled("amarok")
94         if aux:
95             return aux
96         else:
97             return None
98
99     # verifies if this amarok app is playing and update the 
100     # Playlist with current song
101     def isPlaying(self):
102         if not self.amarok.player.isPlaying() == 'true':
103             self.playlist.update(self.amarok.playlist.getActiveIndex(),\
104                                  self.amarok.player.title(),               \
105                                  self.amarok.player.artist(),              \
106                                  self.amarok.player.path(),                \
107                                  "." + self.amarok.player.type(),          \
108                                 )
109             return True
110         else:
111             return False        
112
113     # get the players name
114     def getName(self):
115         return "Amarok"
116
117     # send audio files to the N810 device
118     def file_properties(self, index=None):
119         track = self.amarok.playlist.getActiveIndex()
120         audiofile = self.playlist.song_properties(index=track, path=True)
121         #audiofile = (self.playlist.song_filename(index),\
122         #             self.playlist.song_size(index))
123         return audiofile
124
125     # next button and sets current song
126     def next(self):
127         self.amarok.player.next()
128         self.playlist.update(self.amarok.playlist.getActiveIndex(),\
129                              self.amarok.player.title(),               \
130                              self.amarok.player.artist(),              \
131                              self.amarok.player.path(),                \
132                              "." + self.amarok.player.type(),          \
133                              )
134         
135     # prev button and sets current song
136     def prev(self):
137         self.amarok.player.prev()               
138         self.playlist.update(self.amarok.playlist.getActiveIndex(),\
139                              self.amarok.player.title(),               \
140                              self.amarok.player.artist(),              \
141                              self.amarok.player.path(),                \
142                              "." + self.amarok.player.type(),          \
143                              )
144         
145     # play button and sets current song
146     #def play(self):
147         #self.amarok.player.play()
148         #self.playlist.update(self.amarok.playlist.getActiveIndex() + 1,\
149         #                    self.amarok.player.title(),               \
150         #                    self.amarok.player.artist(),              \
151         #                    self.amarok.player.path(),                \
152         #                    "." + self.amarok.player.type(),          \
153         #                    )
154
155     # play button and sets current song
156     # receive track or random form
157     # the argument track has intended to manipulate
158     # the playlist form when the user indicate a song track
159     # in client application
160     def play(self, track=-1, rdm=False):
161         if rdm:
162             index = random.randint(0, self.playlist.length() - 1)
163             self.amarok.playlist.playByIndex(index)
164             self.playlist.update(index + 1,                      \
165                                  self.amarok.player.title(),     \
166                                  self.amarok.player.artist(),    \
167                                  self.amarok.player.path(),      \
168                                  "." + self.amarok.player.type(),\
169                                  )
170         elif track != -1:
171             self.amarok.playlist.playByIndex(track)
172             self.playlist.update(track-1,                                \
173                                  self.amarok.player.title(),     \
174                                  self.amarok.player.artist(),    \
175                                  self.amarok.player.path(),      \
176                                  "." + self.amarok.player.type(),\
177                                  ) 
178         else:
179             self.amarok.player.play()
180             self.playlist.update(self.amarok.playlist.getActiveIndex() + 1,\
181                                  self.amarok.player.title(),               \
182                                  self.amarok.player.artist(),              \
183                                  self.amarok.player.path(),                \
184                                  "." + self.amarok.player.type(),          \
185                                  )
186         
187     # play button with index song and sets current song
188     #def play_track(self, index):
189     #   self.amarok.playlist.playByIndex(index-1)       
190     #   self.playlist.update(index,                          \
191     #                        self.amarok.player.title(),     \
192     #                        self.amarok.player.artist(),    \
193     #                        self.amarok.player.path(),      \
194     #                        "." + self.amarok.player.type(),\
195     #                        )
196
197     # random play songs
198     #def play_random(self):
199     #   index = random.randint(0, self.playlist.length() - 1)
200     #   self.amarok.playlist.playByIndex(index)
201     #   self.playlist.update(index+1,                        \
202     #                        self.amarok.player.title(),     \
203     #                        self.amarok.player.artist(),    \
204     #                        self.amarok.player.path(),      \
205     #                        "." + self.amarok.player.type(),\
206     #                        )
207                 
208     # pause button
209     def pause(self):
210         self.amarok.player.pause()
211
212     # mute button
213     def mute(self):
214         self.amarok.player.mute()
215
216     # stop button
217     def stop(self):
218         self.amarok.player.stop()
219
220     # get the current volume value
221     def get_volume(self):
222         return self.amarok.player.getVolume()
223
224     # set up volume
225     def volume_up(self, increase=1):
226         if (self.get_volume() + increase) <= 100:
227             up = self.get_volume() + increase
228             self.amarok.player.setVolume(up)
229         else:
230             print "erro!"
231
232     # set down volume
233     def volume_down(self, decrement=1):
234         if (self.get_volume() - decrement) >= 0:
235             down = self.get_volume() - decrement
236             self.amarok.player.setVolume(down)
237         else:
238             print "erro!"
239         
240     # set seek value 
241     def seek(self, value):
242         self.amarok.player.seek(value)