ece152712c68a921c782afa394cf17c06cfb8e5d
[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 import hildon
26 import jamaendo
27 from settings import settings
28 import logging
29
30 log = logging.getLogger(__name__)
31
32 def _alist(l, match):
33     for key, value in l:
34         if key == match:
35             return value
36     return None
37
38 def _show_banner(parent, message, timeout = 2000):
39     banner = hildon.hildon_banner_show_information(parent, '', message)
40     banner.set_timeout(2000)
41
42 from listbox import ListDialog
43
44 def add_to_playlist(wnd, track):
45     if not track:
46         _show_banner(wnd, "Nothing to add")
47         return
48
49     dialog = ListDialog('Add to playlist', wnd)
50     for name,_ in settings.playlists.iteritems():
51         dialog.listbox.append(name)
52     dialog.listbox.append("New...")
53     try:
54         dialog.show_all()
55         if dialog.run() == gtk.RESPONSE_OK:
56             selected_playlist = dialog.selected
57             if selected_playlist == "New...":
58                 dialog.hide()
59                 selected_playlist = create_new_playlist(wnd)
60             if track and selected_playlist:
61                 if isinstance(track, (list, tuple)):
62                     for t in track:
63                         settings.add_to_playlist(selected_playlist, {'id':t.ID, 'data':t.get_data()})
64                 else:
65                     settings.add_to_playlist(selected_playlist, {'id':track.ID, 'data':track.get_data()})
66                 settings.save()
67                 _show_banner(wnd, "Added to playlist '%s'" % (selected_playlist))
68     finally:
69         dialog.destroy()
70
71 def create_new_playlist(wnd):
72     dia_name = gtk.Dialog()
73     dia_name.set_title("New playlist")
74     dia_name.add_button( gtk.STOCK_OK, gtk.RESPONSE_OK )
75     entry = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
76     entry.set_placeholder("Enter name")
77     entry.set_max_length(32)
78     entry.connect('activate', lambda entry, dialog: dialog.response(gtk.RESPONSE_OK), dia_name)
79     dia_name.vbox.pack_start(entry, True, True, 0)
80     dia_name.show_all()
81     if dia_name.run() != gtk.RESPONSE_OK:
82         return False
83     selected_playlist = entry.get_text()
84     dia_name.destroy()
85     if selected_playlist == '' or selected_playlist == 'New...':
86         return False
87     elif settings.get_playlist(selected_playlist):
88         _show_banner(wnd, "Playlist '%s' already exists!" % (selected_playlist))
89         return False
90     return selected_playlist
91
92
93 class PlaylistsWindow(hildon.StackableWindow):
94     def __init__(self):
95         hildon.StackableWindow.__init__(self)
96         self.set_title("Playlists")
97
98         self.panarea = hildon.PannableArea()
99
100         (self.COL_NAME, self.COL_INFO) = range(2)
101         self.store = gtk.ListStore(str, str)
102         self.treeview = gtk.TreeView()
103         self.treeview.set_model(self.store)
104
105         col = gtk.TreeViewColumn('Name')
106         self.treeview.append_column(col)
107         cell = gtk.CellRendererText()
108         col.pack_start(cell, True)
109         col.add_attribute(cell, 'text', self.COL_NAME)
110         self.treeview.set_search_column(self.COL_NAME)
111         col.set_sort_column_id(self.COL_NAME)
112
113         col = gtk.TreeViewColumn('Info')
114         self.treeview.append_column(col)
115         cell = gtk.CellRendererText()
116         cell.set_property('xalign', 1.0)
117         col.pack_start(cell, True)
118         col.add_attribute(cell, 'text', self.COL_INFO)
119
120         self.treeview.connect('row-activated', self.row_activated)
121
122         self.panarea.add(self.treeview)
123
124         self.add(self.panarea)
125
126         def trackcount(lst):
127             ln = len(lst)
128             if ln > 1:
129                 return "(%d tracks)"%(ln)
130             elif ln == 1:
131                 return "(1 track)"
132             return "(empty)"
133
134         for key, lst in sorted(list(settings.playlists.iteritems())):
135             self.store.append([key, trackcount(lst)])
136
137     def row_activated(self, treeview, path, view_column):
138         name = self.store.get(self.store.get_iter(path), self.COL_NAME)[0]
139         pl = settings.get_playlist(name)
140         if pl:
141             from playerwindow import open_playerwindow
142             wnd = open_playerwindow()
143             wnd.play_tracks(pl)