Added 'helldon' to transparently port the thing to the desktop :P
[jamaendo] / jamaui / playlists.py
1 #!/usr/bin/env python
2 #
3 # This file is part of Jamaendo.
4 # Copyright (c) 2010 Kristoffer Gronlund
5 #
6 # Jamaendo is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Jamaendo is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Jamaendo.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Player code heavily based on http://thpinfo.com/2008/panucci/:
20 #  A resuming media player for Podcasts and Audiobooks
21 #  Copyright (c) 2008-05-26 Thomas Perl <thpinfo.com>
22 #  (based on http://pygstdocs.berlios.de/pygst-tutorial/seeking.html)
23 #
24 import gtk
25 try:
26     import hildon
27 except:
28     import helldon as hildon
29 import jamaendo
30 from settings import settings
31 import logging
32
33 log = logging.getLogger(__name__)
34
35 def _alist(l, match):
36     for key, value in l:
37         if key == match:
38             return value
39     return None
40
41 def _show_banner(parent, message, timeout = 2000):
42     banner = hildon.hildon_banner_show_information(parent, '', message)
43     banner.set_timeout(2000)
44
45 from listbox import ListDialog
46
47 def add_to_playlist(wnd, track):
48     if not track:
49         _show_banner(wnd, "Nothing to add")
50         return
51
52     dialog = ListDialog('Add to playlist', wnd)
53     for name,_ in settings.playlists.iteritems():
54         dialog.listbox.append(name)
55     dialog.listbox.append("New...")
56     try:
57         dialog.show_all()
58         if dialog.run() == gtk.RESPONSE_OK:
59             selected_playlist = dialog.selected
60             if selected_playlist == "New...":
61                 dialog.hide()
62                 selected_playlist = create_new_playlist(wnd)
63             if track and selected_playlist:
64                 if isinstance(track, (list, tuple)):
65                     for t in track:
66                         settings.add_to_playlist(selected_playlist, {'id':t.ID, 'data':t.get_data()})
67                 else:
68                     settings.add_to_playlist(selected_playlist, {'id':track.ID, 'data':track.get_data()})
69                 settings.save()
70                 _show_banner(wnd, "Added to playlist '%s'" % (selected_playlist))
71     finally:
72         dialog.destroy()
73
74 def create_new_playlist(wnd):
75     dia_name = gtk.Dialog()
76     dia_name.set_title("New playlist")
77     dia_name.add_button( gtk.STOCK_OK, gtk.RESPONSE_OK )
78     entry = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
79     entry.set_placeholder("Enter name")
80     entry.set_max_length(32)
81     entry.connect('activate', lambda entry, dialog: dialog.response(gtk.RESPONSE_OK), dia_name)
82     dia_name.vbox.pack_start(entry, True, True, 0)
83     dia_name.show_all()
84     if dia_name.run() != gtk.RESPONSE_OK:
85         return False
86     selected_playlist = entry.get_text()
87     dia_name.destroy()
88     if selected_playlist == '' or selected_playlist == 'New...':
89         return False
90     elif settings.get_playlist(selected_playlist):
91         _show_banner(wnd, "Playlist '%s' already exists!" % (selected_playlist))
92         return False
93     return selected_playlist
94
95
96 class PlaylistsWindow(hildon.StackableWindow):
97     def __init__(self):
98         hildon.StackableWindow.__init__(self)
99         self.set_title("Playlists")
100
101         self.panarea = hildon.PannableArea()
102
103         (self.COL_NAME, self.COL_INFO) = range(2)
104         self.store = gtk.ListStore(str, str)
105         self.treeview = gtk.TreeView()
106         self.treeview.set_model(self.store)
107
108         col = gtk.TreeViewColumn('Name')
109         self.treeview.append_column(col)
110         cell = gtk.CellRendererText()
111         col.pack_start(cell, True)
112         col.add_attribute(cell, 'text', self.COL_NAME)
113         self.treeview.set_search_column(self.COL_NAME)
114         col.set_sort_column_id(self.COL_NAME)
115
116         col = gtk.TreeViewColumn('Info')
117         self.treeview.append_column(col)
118         cell = gtk.CellRendererText()
119         cell.set_property('xalign', 1.0)
120         col.pack_start(cell, True)
121         col.add_attribute(cell, 'text', self.COL_INFO)
122
123         self.treeview.connect('row-activated', self.row_activated)
124
125         self.panarea.add(self.treeview)
126
127         self.add(self.panarea)
128
129         def trackcount(lst):
130             ln = len(lst)
131             if ln > 1:
132                 return "(%d tracks)"%(ln)
133             elif ln == 1:
134                 return "(1 track)"
135             return "(empty)"
136
137         for key, lst in sorted(list(settings.playlists.iteritems())):
138             self.store.append([key, trackcount(lst)])
139
140     def row_activated(self, treeview, path, view_column):
141         name = self.store.get(self.store.get_iter(path), self.COL_NAME)[0]
142         pl = settings.get_playlist(name)
143         if pl:
144             from playerwindow import open_playerwindow
145             wnd = open_playerwindow()
146             wnd.play_tracks(pl)