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