e7311b70bfa71f8f5bd5cc75cd0aa103677d767b
[jamaendo] / jamaui / ui.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 # Jamaendo jamendo.com API wrapper licensed under the New BSD License;
25 # see module for details.
26 #
27
28 import os, sys
29 import gtk
30 import gobject
31 import util
32 import logging
33 from settings import settings
34
35 import ossohelper
36
37 gobject.threads_init()
38
39 log = logging.getLogger(__name__)
40
41 VERSION = '1'
42
43 try:
44     import hildon
45 except:
46     if util.platform == 'maemo':
47         log.critical('Using GTK widgets, install "python2.5-hildon" '
48             'for this to work properly.')
49     else:
50         log.critical('This ui only works in maemo')
51         sys.exit(1)
52
53 from dbus.mainloop.glib import DBusGMainLoop
54
55 DBusGMainLoop(set_as_default=True)
56
57 import jamaendo
58
59 from playerwindow import open_playerwindow
60 from search import SearchWindow
61 from featured import FeaturedWindow
62 from radios import RadiosWindow
63 from favorites import FavoritesWindow
64
65 class PlaylistsWindow(hildon.StackableWindow):
66     def __init__(self):
67         hildon.StackableWindow.__init__(self)
68         self.set_title("Playlists")
69
70         label = gtk.Label("Playlists")
71         vbox = gtk.VBox(False, 0)
72         vbox.pack_start(label, True, True, 0)
73         self.add(vbox)
74
75 class Jamaui(object):
76     def __init__(self):
77         self.app = None
78         self.menu = None
79         self.window = None
80
81     def create_window(self):
82         self.app = hildon.Program()
83         self.window = hildon.StackableWindow()
84         self.app.add_window(self.window)
85
86         self.window.set_title("jamaendo")
87         self.window.connect("destroy", self.destroy)
88
89         self.CONFDIR = os.path.expanduser('~/MyDocs/.jamaendo')
90         jamaendo.set_cache_dir(self.CONFDIR)
91         settings.set_filename(os.path.join(self.CONFDIR, 'ui_settings'))
92         settings.load()
93
94     def save_settings(self):
95         settings.save()
96
97     def create_menu(self):
98         self.menu = hildon.AppMenu()
99
100         #search = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
101         #search.set_label("Search")
102         #search.connect("clicked", self.on_search)
103         #self.menu.append(search)
104
105         #player = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
106         #player.set_label("Open player")
107         #player.connect("clicked", self.on_player)
108         #self.menu.append(player)
109
110         player = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
111         player.set_label("Favorites")
112         player.connect("clicked", self.on_favorites)
113         self.menu.append(player)
114
115         #player = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
116         #player.set_label("Playlists")
117         #player.connect("clicked", self.on_playlists)
118         #self.menu.append(player)
119
120         player = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
121         player.set_label("Settings")
122         player.connect("clicked", self.on_settings)
123         self.menu.append(player)
124
125
126         # Don't use localdb ATM
127         #refresh = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
128         #refresh.set_label("Refresh")
129         #refresh.connect("clicked", self.on_refresh)
130         #self.menu.append(refresh)
131
132         menu_about = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
133         menu_about.set_label("About")
134         menu_about.connect("clicked", self.show_about, self.window)
135         self.menu.append(menu_about)
136         gtk.about_dialog_set_url_hook(self.open_link, None)
137
138         self.menu.show_all()
139         self.window.set_app_menu(self.menu)
140
141
142     def setup_widgets(self):
143         bgimg = util.find_resource('bg.png')
144         if bgimg:
145             background, mask = gtk.gdk.pixbuf_new_from_file(bgimg).render_pixmap_and_mask()
146             self.window.realize()
147             self.window.window.set_back_pixmap(background, False)
148
149         bbox = gtk.HButtonBox()
150         alignment = gtk.Alignment(xalign=0.2, yalign=0.925, xscale=1.0)
151         alignment.add(bbox)
152         bbox.set_property('layout-style', gtk.BUTTONBOX_SPREAD)
153         self.bbox = bbox
154         self.window.add(alignment)
155
156         self.add_mainscreen_button("Featured", "Most listened to", self.on_featured)
157         self.add_mainscreen_button("Radios", "The best in free music", self.on_radios)
158         self.add_mainscreen_button("Search", "Search for artists/albums", self.on_search)
159
160         self.window.show_all()
161
162     def add_mainscreen_button(self, title, subtitle, callback):
163         btn = hildon.Button(gtk.HILDON_SIZE_THUMB_HEIGHT,
164                             hildon.BUTTON_ARRANGEMENT_VERTICAL)
165         btn.set_text(title, subtitle)
166         btn.set_property('width-request', 225)
167         btn.connect('clicked', callback)
168         self.bbox.add(btn)
169
170     #def add_featured_button(self):
171     #    self.featured_sel = hildon.TouchSelector(text=True)
172     #    self.featured_sel.append_text("Albums of the week")
173     #    self.featured_sel.append_text("Tracks of the week")
174     #    self.featured_sel.append_text("New releases")
175     #    btn = hildon.PickerButton(gtk.HILDON_SIZE_THUMB_HEIGHT,
176     #                              hildon.BUTTON_ARRANGEMENT_VERTICAL)
177     #    btn.set_text("Featured", "Most listened to")
178     #    btn.set_property('width-request', 225)
179     #    btn.set_selector(self.featured_sel)
180     #    self.featured_btn = btn
181     #    self.bbox.add(btn)
182
183     def destroy(self, widget):
184         gtk.main_quit()
185
186     def show_about(self, w, win):
187         dialog = gtk.AboutDialog()
188         dialog.set_program_name("jamaendo")
189         dialog.set_website("http://github.com/krig")
190         dialog.set_website_label("http://github.com/krig")
191         dialog.set_version(VERSION)
192         dialog.set_license("GNU GPL v3")
193         dialog.set_authors(("Kristoffer Gronlund <kristoffer.gronlund@purplescout.se>",
194                             "Based on Panucci and gPodder, by",
195                             "Thomas Perl <thpinfo.com>"))
196         dialog.set_comments("""Jamaendo plays music from the music catalog of JAMENDO.
197
198 JAMENDO is an online platform that distributes musical works under Creative Commons licenses.""")
199         gtk.about_dialog_set_email_hook(self.open_link, dialog)
200         gtk.about_dialog_set_url_hook(self.open_link, dialog)
201         dialog.connect( 'response', lambda dlg, response: dlg.destroy())
202         for parent in dialog.vbox.get_children():
203             for child in parent.get_children():
204                 if isinstance(child, gtk.Label):
205                     child.set_selectable(False)
206                     child.set_alignment(0.0, 0.5)
207         dialog.run()
208         dialog.destroy()
209
210     def open_link(self, d, url, data):
211         print "url: %s" % (url)
212         import webbrowser
213         webbrowser.open_new(url)
214
215
216     #def on_refresh(self, button):
217     #    dialog = RefreshDialog()
218     #    dialog.show_all()
219     #    dialog.run()
220     #    dialog.hide()
221
222     def on_featured(self, button):
223         dialog = hildon.PickerDialog(self.window)
224         sel = hildon.TouchSelector(text=True)
225         for feature, _ in FeaturedWindow.features:
226             sel.append_text(feature)
227         dialog.set_selector(sel)
228         dialog.set_title("Featured")
229         if dialog.run() == gtk.RESPONSE_OK:
230             txt = sel.get_current_text()
231             self.featuredwnd = FeaturedWindow(txt)
232             self.featuredwnd.show_all()
233         dialog.destroy()
234
235     def on_radios(self, button):
236         self.radioswnd = RadiosWindow()
237         self.radioswnd.show_all()
238
239     def on_search(self, button):
240         self.searchwnd = SearchWindow()
241         self.searchwnd.show_all()
242
243     def on_playlists(self, button):
244         self.playlistswnd = PlaylistsWindow()
245         self.playlistswnd.show_all()
246
247     def on_settings(self, button):
248         dialog = gtk.Dialog()
249         dialog.set_title("Settings")
250         dialog.add_button( gtk.STOCK_OK, gtk.RESPONSE_OK )
251         vbox = dialog.vbox
252         tbl = gtk.Table(1, 2)
253         tbl.attach(gtk.Label("Username:"), 0, 1, 0, 1)
254         entry = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
255         entry.set_placeholder("jamendo.com username")
256         if settings.user:
257             entry.set_text(settings.user)
258         tbl.attach(entry, 1, 2, 0, 1)
259         vbox.pack_start(tbl, True, True, 2)
260         dialog.show_all()
261         result = dialog.run()
262         val = entry.get_text()
263         dialog.destroy()
264         print val, result
265         if val and result == gtk.RESPONSE_OK:
266             print "new user name:", val
267             settings.user = val
268             self.save_settings()
269
270
271     def on_favorites(self, button):
272         self.favoriteswnd = FavoritesWindow()
273         self.favoriteswnd.show_all()
274
275     def on_player(self, button):
276         open_playerwindow([])
277
278     '''
279     def on_search(self, button):
280         if self.searchbar:
281             self.searchbar.show()
282         else:
283             self.searchstore = gtk.ListStore(gobject.TYPE_STRING)
284             iter = self.searchstore.append()
285             self.searchstore.set(iter, 0, "Test1")
286             iter = self.searchstore.append()
287             self.searchstore.set(iter, 0, "Test2")
288             self.searchbar = hildon.FindToolbar("Search", self.searchstore, 0)
289             self.searchbar.set_active(0)
290             self.window.add_toolbar(self.searchbar)
291             self.searchbar.show()
292     '''
293
294     def run(self):
295         ossohelper.application_init('org.jamaendo', '0.1')
296         self.create_window()
297         self.create_menu()
298         self.setup_widgets()
299         self.window.show_all()
300         gtk.main()
301         ossohelper.application_exit()
302
303 if __name__=="__main__":
304     ui = Jamaui()
305     ui.run()
306