00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 from math import floor
00034
00035 from time import strftime, gmtime, time, sleep
00036
00037
00038 import urllib
00039 import hashlib
00040
00041
00042 from threading import Thread
00043
00044 import traceback
00045 import os
00046 import sys
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 try:
00059 import clXMLRPC
00060 XMLRPC_AVAILABLE = True
00061 except:
00062 XMLRPC_AVAILABLE = False
00063 print ("Error import clXMLRPC")
00064
00065 try:
00066 import gnome.ui
00067 GNOME_UI_AVAILABLE = True
00068 except:
00069 GNOME_UI_AVAILABLE = False
00070
00071
00072 import re
00073
00074 import functions
00075 import clSettings
00076 import clJamendo
00077 if os.name == "posix" or "mac" or "OVERRIDE-PLAYER" in sys.argv:
00078 import clGstreamer010 as PLAYER
00079 else:
00080 import clPlayerForWin as PLAYER
00081
00082
00083 import dbthreaded
00084
00085
00086 import clThreadedDownload
00087 from clWidgets import *
00088 import clEntry
00089 import clPlugin, clEvent
00090 import notification
00091 import clBrowserInterface
00092 import download_db
00093 import clLayouts
00094 import clPreferences
00095 from extended_modules import clPlaylists
00096 from extended_modules import clBookmarks
00097
00098
00099 try:
00100 import download_db as update
00101 except:
00102 print ("Error importing update module")
00103
00104
00105 class main():
00106
00107
00108
00109
00110
00111
00112
00113 def __init__(self, parent, options):
00114
00115 self.verbose = options.verbose
00116
00117 self.debug = options.debug
00118
00119 self.debug_extreme = options.debug_extreme
00120
00121
00122
00123 self.version = functions.VERSION
00124 if GNOME_UI_AVAILABLE:
00125 gnome.init("Pyjama", self.version)
00126 print "PYJAMA FOUND IN %s" % functions.install_dir()
00127
00128
00129
00130
00131
00132 self.need_attention = False
00133
00134
00135 self.Events = clEvent.Events()
00136
00137 self.Events.add_event("pluginloaded")
00138 self.Events.add_event("nowplaying")
00139 self.Events.add_event("alldone")
00140 self.Events.add_event("showing_album_page")
00141 self.Events.add_event("showing_artist_page")
00142 self.Events.add_event("firstrun")
00143 self.Events.add_event("error")
00144 self.Events.add_event("info")
00145 self.Events.add_event("layout_changed")
00146 self.Events.add_event("populate_listmenu")
00147 self.Events.add_event("populate_playlistmenu")
00148 self.Events.add_event("scrolled_window_resized")
00149 self.Events.add_event("playlist_tooltip")
00150
00151 self.Events.add_event("albuminfo_created")
00152
00153
00154
00155
00156 self.Events.connect_event("pluginloaded", self.ev_plugin_loaded)
00157
00158 self.home_fkt = self.go_home_fkt
00159
00160
00161
00162 self.Events.connect_event("error", self.error)
00163 self.Events.connect_event("info", self.info)
00164
00165
00166
00167
00168 self.settings = clSettings.settings(self)
00169
00170
00171
00172
00173 self.settingsdb = dbthreaded.DB_Settings(self)
00174
00175
00176 self.db = dbthreaded.DB(self)
00177
00178
00179
00180 self.dump_tools = download_db.dump_tools(self)
00181
00182 self.window = parent
00183
00184 self.icon = notification.TrayIcon(self)
00185
00186 self.player = PLAYER.Player(self)
00187
00188 self.jamendo = clJamendo.Jamendo(self)
00189
00190
00191 self.browser = clBrowserInterface.Browser(self)
00192
00193
00194
00195 self.layouts = clLayouts.Layouts(self)
00196
00197 self.preferences = clPreferences.Preferences(self)
00198
00199 if XMLRPC_AVAILABLE:
00200 self.xmlrpc = clXMLRPC.XMLRPC(self)
00201
00202 for item in sys.argv:
00203 if item.endswith(".m3u"):
00204 if self.xmlrpc.role == "client":
00205 try:
00206 self.xmlrpc.server.test("test123")
00207 except Exception, inst:
00208 print ("Could not connect to server: %s" % inst)
00209 break
00210
00211
00212
00213 self.nocolor = options.theme != None or options.nocolor
00214
00215
00216
00217 self.allow_rearrange = True
00218
00219
00220 self.home = functions.preparedirs()
00221
00222 self.tvMarkedItem = None
00223
00224
00225
00226
00227
00228 self.playlist_to_load = None
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 self.download_database = self.simple_database_downloader
00239
00240
00241 self.pulsing = False
00242
00243
00244 self.historyBack = []
00245
00246 self.historyForward = []
00247
00248 self.historyCurrent = {}
00249
00250
00251
00252
00253
00254
00255
00256
00257 print ("Loading modules")
00258 self.playlists = clPlaylists.main(self)
00259 self.bookmarks = clBookmarks.main(self)
00260
00261
00262 if len(sys.argv)>1:
00263 if os.path.exists(sys.argv[1]):
00264 if self.check_another_instance_running() is True:
00265
00266 try:
00267 self.xmlrpc.send_playlist(sys.argv[1])
00268 sys.exit(0)
00269 except Exception, inst:
00270 print ("Error passing the playlist to the running instance - will no play it in this instance: %s" % inst)
00271
00272 self.playlist_to_load = sys.argv[1]
00273 else:
00274
00275
00276
00277 self.playlist_to_load = sys.argv[1]
00278
00279
00280
00281
00282 self.plugins = clPlugin.Plugins(self)
00283 if self.settings.get_value("PYJAMA", "FIRST_RUN", True) == True:
00284 self.Events.raise_event("firstrun")
00285 self.settings.set_value("PYJAMA", "FIRST_RUN", "False")
00286
00287
00288
00289
00290
00291 self.start_timer()
00292
00293
00294
00295
00296 if os.name != "posix" and os.name != "mac" and not "OVERRIDE-PLAYER" in sys.argv:
00297 self.Events.raise_event("info", text="You are running pyjama on windows. As far as I tested it, it worked fine except from the player support.\nFor this reason for windows the module <i>clPlayerForWin</i> was loaded for playback. This is only a hack for testing reasons, if you want to try out <i>clGstreamer010</i>, run pyjama with '<b>pyjama OVERRIDE-PLAYER</b>'")
00298
00299
00300
00301
00302 def go_home(self):
00303 self.jamendo.last_query_hack()
00304 self.home_fkt()
00305
00306
00307
00308
00309 def go_home_fkt(self):
00310 self.layouts.show_layout("top", 10, "ratingweek", 1, "all", who_called = "on_bHome_clicked")
00311
00312
00313
00314
00315 def set_home_fkt(self, fkt):
00316 self.home_fkt = fkt
00317
00318
00319
00320
00321
00322
00323 def read_playlist(self, playlist):
00324 if self.debug:
00325 print "Loading Playlist %s" % playlist
00326 fh = file(playlist, "r")
00327 if fh:
00328 lines = fh.readlines()
00329 fh.close()
00330 else:
00331 print ("Error reading %s") % playlist
00332 return
00333 if True:
00334 track_ids = []
00335 tracks = []
00336 rg1 = re.compile('.*?\\d+.*?(\\d+)',re.IGNORECASE|re.DOTALL)
00337 rg2 = re.compile('.*?(\/)(stream)(\/)(\d+)',re.IGNORECASE|re.DOTALL)
00338 for line in lines:
00339 if "jamendo.com/get2/stream/track/redirect/?id=" in line:
00340 m = rg1.search(line.strip())
00341 if m:
00342 track_id=m.group(1)
00343 track_ids.append(int(track_id))
00344 elif "jamendo.com/stream/" in line:
00345 m = rg2.search(line.strip())
00346 if m:
00347 track_id=m.group(4)
00348 track_ids.append(int(track_id))
00349 elif line.startswith("#"):
00350 pass
00351 else:
00352 print ("This is not a Jamendo track or not readable with pyjama: %s") % line.strip()
00353 return track_ids
00354
00355
00356
00357
00358
00359
00360
00361
00362 def simple_database_downloader(self, force_jamendo=False):
00363 dia = gtk.MessageDialog(self.window, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, 0, _("Downloading database dump from Jamendo right now. Please be patient.\n\nFor more information please run Pyjama from console.\n\nThis might take up to a minute"))
00364 dia.show()
00365 self.window.do_events()
00366 dump = update.dump_tools()
00367 dump.delete_db()
00368 dump.create_tables()
00369 ret = dump.create_db(force_jamendo)
00370 if ret == "nofile":
00371 if force_jamendo:
00372 self.Events.raise_event("error", None, "Error downloading the database dump from jamendo. Please try again later and notify me about this.")
00373 else:
00374 self.Events.raise_event("error", None, "Error downloading the database dump from the mirror.\nPlease try running 'pyjama --update-jamendo' in order to get the database right from jamendo.")
00375 dia.destroy()
00376 return
00377 dump.finish()
00378 dia.destroy()
00379
00380 self.db = dbthreaded.DB(self)
00381 self.window.sbStatus.set_text("Counter", _("Artists: %i, Albums: %i, Tracks: %i") % (self.db.artists, self.db.albums, self.db.tracks))
00382
00383
00384
00385
00386 def set_download_database_fkt(self, fkt):
00387 self.download_database = fkt
00388
00389
00390
00391
00392
00393 def show_plugins(self, event=None):
00394 clPlugin.ShowPluginsDialog(self)
00395
00396
00397
00398
00399
00400
00401 def show_preferences(self, widget=None, name=None):
00402 self.preferences.show_preferences(name)
00403
00404
00405
00406
00407 def check_another_instance_running(self):
00408 if XMLRPC_AVAILABLE:
00409 self.xmlrpc.test()
00410 if self.xmlrpc.role == "client":
00411 return True
00412 else:
00413 return None
00414 else:
00415 return None
00416
00417
00418
00419 def quit(self):
00420 self.settingsdb.quit()
00421 self.db.quit()
00422
00423
00424 self.settings.set_value("PYJAMA", "crashed", False)
00425
00426
00427 self.xmlrpc.quit()
00428
00429
00430
00431
00432
00433
00434
00435 def reload_current_page(self, ev=None, ignore_cache=False):
00436
00437 adjustment = self.window.scrolledwindow.get_vadjustment()
00438 self.window.scrolledwindow.hide()
00439
00440 if ignore_cache is True:
00441 self.jamendo.set_ignore_cache(True)
00442
00443
00444 data = self.historyCurrent
00445 self.layouts.show_layout(data['layout'], data['data1'], data['data2'], data['data3'], data['data4'], fromhistory=True, who_called='reload_current_page')
00446
00447 self.jamendo.set_ignore_cache(False)
00448
00449
00450 self.window.scrolledwindow.set_vadjustment(adjustment)
00451 self.window.scrolledwindow.show()
00452
00453
00454 def info(self, title="Information", text=""):
00455 dialog = MyDialog(title,self.window, \
00456 gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, \
00457 (gtk.STOCK_OK, gtk.RESPONSE_ACCEPT), gtk.STOCK_DIALOG_INFO, text)
00458 dialog.set_icon_from_file(os.path.join(functions.install_dir(), "images", "pyjama.png"))
00459 dialog.run()
00460 dialog.destroy()
00461
00462
00463
00464
00465
00466
00467
00468
00469 def error(self, error_inst, desc=False):
00470 print "An error occured: %s" % error_inst
00471 tb = traceback.format_exc()
00472 print "-"*60
00473 print tb
00474 print "-"*60
00475 if not desc:
00476 desc = error_inst
00477 print desc
00478 self.window.show_error_message(desc, tb)
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489 def ev_plugin_loaded(self, name, version, order, mod_name):
00490
00491 print ("Plugin '%s' V%s (%s) loaded") % (name, version, mod_name)
00492
00493
00494
00495
00496
00497 def set_allow_rearrange(self):
00498 self.allow_rearrange=True
00499 return False
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524 def appendtracks(self, tracks, play = False):
00525 cur = len(self.player.playlist)
00526 for track in tracks:
00527 track.uid = "%f%s" % (time(), track.uid)
00528 self.add2playlist(track)
00529 status = self.player.status
00530 if play:
00531 self.window.on_bStop_clicked(None)
00532 self.setplaylist(cur)
00533 self.window.on_bPlay_clicked(None)
00534
00535
00536
00537
00538
00539 def remove_item_from_playlist(self, item):
00540 if item < self.tvMarkedItem:
00541 self.tvMarkedItem -= 1
00542 elif item == self.tvMarkedItem:
00543 self.tvMarkedItem = None
00544 self.player.remove(item)
00545
00546
00547
00548
00549
00550
00551
00552 def move_playlist_item(self, s, d, before):
00553 c = self.tvMarkedItem
00554 if d > s:
00555 if before:
00556 bf = -1
00557 else:
00558 bf = 0
00559 else:
00560 if before:
00561 bf = 0
00562 else:
00563 bf = 1
00564 dest = d + bf
00565
00566
00567 self.tvMarkedItem = dest
00568 self.player.move_item(s, d, before)
00569
00570
00571
00572
00573
00574
00575 def add2playlist(self, track):
00576 self.player.add2playlist(track)
00577
00578 artist_name = track.artist_name
00579 track_name = track.name
00580 track_numalbum = track.numalbum
00581
00582
00583 markup = self.window.markupNormalEntry.replace("__ARTIST__", artist_name).replace("__TITLE__", track_name).replace("__NUM__", str(track_numalbum))
00584 tmpIter = self.window.liststore.append([markup])
00585 path = self.window.liststore.get_path(tmpIter)
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599 def showInfo(self, path=None):
00600 if path == None:
00601 if self.tvMarkedItem != None and self.tvMarkedItem < len(self.player.playlist):
00602 path = self.tvMarkedItem
00603 else:
00604 return None
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616 track = self.player.playlist[path]
00617
00618
00619 self.window.lbCoverArtist.set_text(track.artist_name)
00620 self.window.lbCoverAlbum.set_text(track.album_name)
00621
00622
00623 thr = Thread(target = self.__get_image_for_showInfo, args = ([track]))
00624 thr.start()
00625
00626
00627 def __get_image_for_showInfo(self, track):
00628 img = self.get_album_image(track.album_id)
00629 if not img:
00630 self.window.imgCover.set_from_stock(gtk.STOCK_MISSING_IMAGE, gtk.ICON_SIZE_DND)
00631 else:
00632 self.window.imgCover.set_from_file(img)
00633
00634
00635
00636
00637
00638 def setplaylist(self, activate_item):
00639 self.window.pbSong.set_fraction(0)
00640 self.window.pbSong.set_text("")
00641
00642 model = self.window.tvPlaylist.get_model()
00643 retIter = model.get_iter(activate_item)
00644 self.showInfo(activate_item)
00645
00646
00647 if self.tvMarkedItem != None and self.player.last_played != None and self.tvMarkedItem < len(self.player.playlist):
00648 track = self.player.last_played
00649 artist_name = track.artist_name
00650 track.name = track.name
00651 track.numalbum = str(track.numalbum)
00652 markup = self.window.markupNormalEntry.replace("__ARTIST__", artist_name).replace("__TITLE__", track.name).replace("__NUM__", track.numalbum)
00653 self.window.liststore.set(model.get_iter(self.tvMarkedItem), 0, markup)
00654
00655
00656
00657 self.window.tvSelection.select_iter(retIter)
00658 track = self.player.playlist[activate_item]
00659 artist_name = track.artist_name
00660 track.name = track.name
00661 track.numalbum = str(track.numalbum)
00662 markup = self.window.markupCurPlaying.replace("__ARTIST__", artist_name).replace("__TITLE__", track.name).replace("__NUM__", track.numalbum)
00663 self.window.liststore.set(retIter, 0, markup)
00664
00665 self.tvMarkedItem = activate_item
00666
00667
00668
00669
00670 def stop_timer(self):
00671 pass
00672
00673
00674
00675
00676
00677 def start_timer(self):
00678 gobject.timeout_add(200, self.timer_event)
00679
00680
00681
00682
00683
00684
00685
00686 def start_pulsing(self, text = ""):
00687 gobject.timeout_add(50, self.pulse)
00688 self.window.pbWait.set_fraction(0)
00689 self.window.pbWait.set_text(text)
00690 self.window.pbWait.show()
00691
00692
00693
00694
00695
00696
00697
00698 def pulse(self):
00699 self.window.pbWait.pulse()
00700 if self.pulsing:
00701 return True
00702 else:
00703 return False
00704
00705
00706
00707
00708 def stop_pulsing(self):
00709 self.pulsing = False
00710 self.window.pbWait.hide()
00711
00712
00713
00714
00715
00716
00717
00718 def timer_event(self):
00719
00720
00721
00722
00723
00724
00725
00726 if self.playlist_to_load is not None:
00727 if os.path.exists(self.playlist_to_load):
00728 self.playlists.open_playlist_from_file(self.playlist_to_load)
00729 self.playlist_to_load = None
00730
00731
00732
00733 self.player.check_status()
00734 status = self.player.status
00735 trans_stat = _(status)
00736 fehler = ["Error", "Stop", "End", None]
00737 self.setStatus( trans_stat )
00738 if status == "paused":
00739 self.window.bPlay.setimage(gtk.STOCK_MEDIA_PLAY)
00740 self.window.bPlay.tag = "play"
00741 if self.window.pbSong.counter > 2:
00742 if self.window.pbSong.get_text() == _("paused"):
00743 if self.window.pbSong.ShowRemainingTime:
00744 txt = "-%s" % functions.sec2time(self.player.togo*-1)
00745 else:
00746 txt = "%s/%s" % (functions.sec2time(self.player.cursec), functions.sec2time(self.player.duration))
00747 else:
00748 txt = _("paused")
00749 if "00:00:00" in txt or txt == "-00:00": txt = "buffering... %i%%" % self.player.buffer
00750 if self.window.pbSong.get_text != txt:
00751 self.window.pbSong.set_text(txt)
00752 self.window.pbSong.counter = 0
00753 self.window.pbSong.counter +=1
00754 return True
00755 elif not status in fehler and self.player.percentage != None:
00756 if self.window.bPlay.tag != "pause":
00757 self.window.bPlay.setimage(gtk.STOCK_MEDIA_PAUSE)
00758 self.window.bPlay.tag = "pause"
00759
00760 if self.player.duration > 0 and self.player.duration < 59*60:
00761 if self.window.pbSong.get_fraction != self.player.percentage:
00762 self.window.pbSong.set_fraction(self.player.percentage)
00763 if self.window.pbSong.ShowRemainingTime:
00764 txt = "-%s" % functions.sec2time(self.player.togo*-1)
00765 else:
00766 txt = "%s/%s" % (functions.sec2time(self.player.cursec), functions.sec2time(self.player.duration))
00767 else:
00768 self.window.pbSong.pulse()
00769 txt = "%s" % functions.sec2time(self.player.cursec)
00770 if "00:00:00" in txt or txt == "-00:00": txt = "buffering... %i%%" % self.player.buffer
00771 if self.window.pbSong.get_text != txt:
00772 self.window.pbSong.set_text(txt)
00773 return True
00774 elif status == "Stop" or status == "End":
00775
00776 self.window.bPlay.tag = "play"
00777 if self.icon.menu.play_button_status != "play":
00778 self.icon.menu.switch_play_button("play")
00779 if self.window.pbSong.get_fraction() != 0:
00780 self.window.pbSong.set_fraction(0)
00781 if self.window.pbSong.get_text()!= "":
00782 self.window.pbSong.set_text("")
00783
00784 return True
00785 else:
00786 self.window.bPlay.tag = "play"
00787 return True
00788
00789
00790
00791
00792
00793 def setInfo(self, text):
00794 self.window.sbStatus.set_text("VarInfo", text)
00795
00796
00797
00798
00799
00800 def setStatus(self, text):
00801 self.window.sbStatus.set_text("Player", text)
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812 def notification(self, caption, text, icon="pyjama", size = 100):
00813 self.icon.show_notification(caption, text, icon, size)
00814
00815
00816
00817
00818
00819
00820 def switch_window_state(self, ev1=None):
00821 if self.window.window.get_state() & gtk.gdk.WINDOW_STATE_ICONIFIED:
00822 self.window.deiconify()
00823 self.window.set_skip_taskbar_hint(False)
00824 else:
00825 self.window.set_skip_taskbar_hint(True)
00826 self.window.iconify()
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836 def get_album_image(self, album_id, size = 100):
00837 download_from = "http://imgjam.com/albums/%s/covers/1.%i.jpg" % (album_id, size)
00838 name = "http://api.jamendo.com/get2/image/album/redirect/?id=%s&imagesize=%i" % (album_id, size)
00839 md5hash = hashlib.md5(name).hexdigest()
00840 fh = os.path.join(self.home, "images", md5hash)
00841 if not os.path.exists(fh):
00842 try:
00843 print "download"
00844 urllib.urlretrieve(download_from, fh)
00845 except IOError:
00846 print ("Could not load image")
00847 return None
00848 return fh