A thousand cuts..
[jamaendo] / jamaui / ui.py
index 17cf158..329963b 100644 (file)
@@ -1,23 +1,45 @@
-# debugging hack - add . to path
-import os, sys
-local_module_dir = os.path.join(os.path.dirname(sys.argv[0]), '..')
-if os.path.isdir(local_module_dir):
-    sys.path.append(local_module_dir)
+#!/usr/bin/env python
+#
+# This file is part of Jamaendo.
+# Copyright (c) 2010 Kristoffer Gronlund
+#
+# Jamaendo is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Jamaendo is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Jamaendo.  If not, see <http://www.gnu.org/licenses/>.
+#
+# Player code heavily based on http://thpinfo.com/2008/panucci/:
+#  A resuming media player for Podcasts and Audiobooks
+#  Copyright (c) 2008-05-26 Thomas Perl <thpinfo.com>
+#  (based on http://pygstdocs.berlios.de/pygst-tutorial/seeking.html)
+#
+# Jamaendo jamendo.com API wrapper licensed under the New BSD License;
+# see module for details.
+#
 
+import os, sys
 import gtk
 import gobject
 import util
 import logging
-import gobject
+from settings import settings
 
-# we don't use the local DB...
-from jamaendo.api import LocalDB, Query, Queries, refresh_dump
-from jamaui.player import Player, Playlist
+import ossohelper
 
 gobject.threads_init()
 
 log = logging.getLogger(__name__)
 
+VERSION = '0.1'
+
 try:
     import hildon
 except:
@@ -28,227 +50,56 @@ except:
         log.critical('This ui only works in maemo')
         sys.exit(1)
 
-class RefreshDialog(object):
-    def __init__(self):
-        self.notebook = gtk.Notebook()
-        info = gtk.VBox()
-        info.pack_start(gtk.Label("Downloading complete DB from jamendo.com."), True, False)
-        info.pack_start(gtk.Label("This will download approximately 8 MB."), True, False)
-        self.force = hildon.GtkToggleButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
-        self.force.set_label("Force refresh")
-
-        info.pack_start(self.force, True, False)
-        self.notebook.append_page(info)
-
-        pcont = gtk.VBox()
-        self.progress = gtk.ProgressBar()
-        pcont.pack_start(self.progress, True, False)
-        self.notebook.append_page(pcont,
-                                  gtk.Label("Updating Database"))
-        self.progress.set_fraction(0)
-        self.progress.set_orientation(gtk.PROGRESS_LEFT_TO_RIGHT)
-        self.progress.set_text("Downloading...")
-
-        self.notebook.append_page(gtk.Label("Database refreshed."))
-
-        self.dialog = hildon.WizardDialog(None, "Refresh", self.notebook)
-        self.notebook.connect("switch-page", self.on_switch)
-        self.dialog.set_forward_page_func(self.forward_func)
-
-        self.refresher = None
-
-    def on_complete(self, status):
-        hildon.hildon_gtk_window_set_progress_indicator(self.dialog, 0)
-        if status:
-            self.progress.set_fraction(1)
-            self.progress.set_text("DB up to date.")
-        else:
-            self.progress.set_fraction(0)
-            self.progress.set_text("Download failed.")
-
-    def on_progress(self, percent):
-        if percent < 100:
-            self.progress.set_text("Downloading...")
-        self.progress.set_fraction(percent/100.0)
-
-    def on_switch(self, notebook, page, num):
-        if num == 1:
-            hildon.hildon_gtk_window_set_progress_indicator(self.dialog, 1)
-            refresh_dump(self.on_complete, self.on_progress, force=self.force.get_active())
-        elif self.refresher:
-            # cancel download
-            pass
-        return True
-
-    def forward_func(self, notebook, current, userdata):
-        #page = notebook.get_nth_page(current)
-        if current == 0:
-            return True
-        else:
-            return False
-
-    def show_all(self):
-        self.dialog.show_all()
-
-    def run(self):
-        self.dialog.run()
-
-    def hide(self):
-        self.dialog.hide()
-
-class PlayerWindow(hildon.StackableWindow):
-    def __init__(self, playlist=None):
-        hildon.StackableWindow.__init__(self)
-        self.set_title("jamaendo")
-
-        self.playlist = Playlist(playlist)
-        self.player = Player()
-        #self.player.play(playlist)
-
-        vbox = gtk.VBox()
-
-        hbox = gtk.HBox()
-
-        cover = gtk.Image()
-
-        vbox2 = gtk.VBox()
-
-        playlist_pos = gtk.Label("0/0 songs")
-        track = gtk.Label("Track name")
-        progress = hildon.GtkHScale()
-        artist = gtk.Label("Artist")
-        album = gtk.Label("Album")
-
-        vbox2.pack_start(playlist_pos, False)
-        vbox2.pack_start(track, False)
-        vbox2.pack_start(progress, True, True)
-        vbox2.pack_start(artist, False)
-        vbox2.pack_start(album, False)
-
-        hbox.pack_start(cover, True, True, 0)
-        hbox.pack_start(vbox2, True, True, 0)
-
-        vbox.pack_start(hbox, True, True, 0)
-
-        btns = gtk.HButtonBox()
-        btns.set_property('layout-style', gtk.BUTTONBOX_SPREAD)
-
-        vbox.pack_end(btns, False, True, 0)
-
-        self.add_stock_button(btns, gtk.STOCK_MEDIA_PREVIOUS, self.on_prev)
-        self.add_stock_button(btns, gtk.STOCK_MEDIA_PLAY, self.on_play)
-        self.add_stock_button(btns, gtk.STOCK_MEDIA_PAUSE, self.on_pause)
-        self.add_stock_button(btns, gtk.STOCK_MEDIA_STOP, self.on_stop)
-        self.add_stock_button(btns, gtk.STOCK_MEDIA_NEXT, self.on_next)
-
-        self.add(vbox)
-
-    def add_stock_button(self, btns, stock, cb):
-        btn = hildon.GtkButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
-        btn.set_relief(gtk.RELIEF_NONE)
-        btn.set_image(gtk.image_new_from_stock(stock, gtk.ICON_SIZE_SMALL_TOOLBAR))
-        btn.connect('clicked', cb)
-        btns.add(btn)
-
-    def on_play(self, button):
-        self.player.play(self.playlist)
-    def on_pause(self, button):
-        self.player.pause()
-    def on_prev(self, button):
-        self.player.prev()
-    def on_next(self, button):
-        self.player.next()
-    def on_stop(self, button):
-        self.player.stop()
-
-class SearchWindow(hildon.StackableWindow):
-    def __init__(self):
-        hildon.StackableWindow.__init__(self)
-        self.set_title("Search")
-
-        vbox = gtk.VBox(False, 0)
+from dbus.mainloop.glib import DBusGMainLoop
 
-        hbox = gtk.HBox()
-        self.entry = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
-        self.entry.set_placeholder("Search")
-        self.entry.connect('activate', self.on_search)
-        btn = hildon.GtkButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
-        btn.set_label("Go")
-        btn.connect('clicked', self.on_search)
-        hbox.pack_start(self.entry, True, True, 0)
-        hbox.pack_start(btn, False)
+DBusGMainLoop(set_as_default=True)
 
-        self.results = hildon.TouchSelector(text=True)
-        self.results.connect("changed", self.selection_changed)
+import jamaendo
 
-        vbox.pack_start(hbox, False)
-        vbox.pack_start(self.results, True, True, 0)
-
-        self.add(vbox)
-
-        self.idmap = {}
-
-        self.pwnd = None
-
-    def on_search(self, w):
-        txt = self.entry.get_text()
-        print "Search for: %s" % (txt)
-        #db = LocalDB()
-        #db.connect()
-        for album in Queries.search_albums(query=txt):
-            title = "%s - %s" % (album['artist_name'], album['name'])
-            self.idmap[title] = album
-            print "Found %s" % (album)
-            self.results.append_text(title)
-
-    def selection_changed(self, results, userdata):
-        current_selection = results.get_current_text()
-
-        album = self.idmap[current_selection]
-        selected = [int(album['id'])]
-        print "Selected: %s" % (selected)
-        tracks = Queries.album_tracks(selected)
-        if tracks:
-            print "Playing: %s" % (tracks)
-            self.pwnd = PlayerWindow(tracks)
-            self.pwnd.show_all()
+from postoffice import postoffice
+from playerwindow import open_playerwindow
+from search import SearchWindow
+from featured import FeaturedWindow
+from radios import RadiosWindow
+from favorites import FavoritesWindow
 
 class PlaylistsWindow(hildon.StackableWindow):
     def __init__(self):
         hildon.StackableWindow.__init__(self)
         self.set_title("Playlists")
 
-        label = gtk.Label("playlists")
-        vbox = gtk.VBox(False, 0)
-        vbox.pack_start(label, True, True, 0)
-        self.add(vbox)
-
-class FavoritesWindow(hildon.StackableWindow):
-    def __init__(self):
-        hildon.StackableWindow.__init__(self)
-        self.set_title("Favorites")
-
-        label = gtk.Label("favorites")
+        label = gtk.Label("Playlists")
         vbox = gtk.VBox(False, 0)
         vbox.pack_start(label, True, True, 0)
         self.add(vbox)
 
 class Jamaui(object):
-    _BG = 'bg.png' # /opt/jamaendo/data/bg.png
-
     def __init__(self):
         self.app = None
         self.menu = None
         self.window = None
 
     def create_window(self):
+        log.debug("Creating main window...")
         self.app = hildon.Program()
         self.window = hildon.StackableWindow()
         self.app.add_window(self.window)
 
         self.window.set_title("jamaendo")
+
         self.window.connect("destroy", self.destroy)
 
+        self.CONFDIR = os.path.expanduser('~/MyDocs/.jamaendo')
+        jamaendo.set_cache_dir(self.CONFDIR)
+        settings.set_filename(os.path.join(self.CONFDIR, 'ui_settings'))
+        settings.load()
+
+        postoffice.connect('request-album-cover', self, self.on_request_cover)
+        log.debug("Created main window.")
+
+    def save_settings(self):
+        settings.save()
+
     def create_menu(self):
         self.menu = hildon.AppMenu()
 
@@ -257,11 +108,27 @@ class Jamaui(object):
         #search.connect("clicked", self.on_search)
         #self.menu.append(search)
 
+        #player = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
+        #player.set_label("Open player")
+        #player.connect("clicked", self.on_player)
+        #self.menu.append(player)
+
         player = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
-        player.set_label("Open player")
-        player.connect("clicked", self.on_player)
+        player.set_label("Favorites")
+        player.connect("clicked", self.on_favorites)
         self.menu.append(player)
 
+        #player = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
+        #player.set_label("Playlists")
+        #player.connect("clicked", self.on_playlists)
+        #self.menu.append(player)
+
+        player = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
+        player.set_label("Settings")
+        player.connect("clicked", self.on_settings)
+        self.menu.append(player)
+
+
         # Don't use localdb ATM
         #refresh = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
         #refresh.set_label("Refresh")
@@ -277,31 +144,24 @@ class Jamaui(object):
         self.menu.show_all()
         self.window.set_app_menu(self.menu)
 
-    def find_resource(self, name):
-        if os.path.isfile(os.path.join('data', name)):
-            return os.path.join('data', name)
-        elif os.path.isfile(os.path.join('/opt/jaemendo', name)):
-            return os.path.join('/opt/jaemendo', name)
-        elif os.path.isfile(os.path.join('/usr/share/jaemendo', name)):
-            return os.path.join('/usr/share/jaemendo', name)
-        else:
-            return name
 
     def setup_widgets(self):
-        background, mask = gtk.gdk.pixbuf_new_from_file(self.find_resource(self._BG)).render_pixmap_and_mask()
-        self.window.realize()
-        self.window.window.set_back_pixmap(background, False)
+        bgimg = util.find_resource('bg.png')
+        if bgimg:
+            background, mask = gtk.gdk.pixbuf_new_from_file(bgimg).render_pixmap_and_mask()
+            self.window.realize()
+            self.window.window.set_back_pixmap(background, False)
 
         bbox = gtk.HButtonBox()
-        alignment = gtk.Alignment(xalign=0.0, yalign=0.8, xscale=1.0)
+        alignment = gtk.Alignment(xalign=0.2, yalign=0.925, xscale=1.0)
         alignment.add(bbox)
         bbox.set_property('layout-style', gtk.BUTTONBOX_SPREAD)
         self.bbox = bbox
         self.window.add(alignment)
 
+        self.add_mainscreen_button("Featured", "Most listened to", self.on_featured)
+        self.add_mainscreen_button("Radios", "The best in free music", self.on_radios)
         self.add_mainscreen_button("Search", "Search for artists/albums", self.on_search)
-        self.add_mainscreen_button("Playlists", "Browse playlists", self.on_playlists)
-        self.add_mainscreen_button("Favorites", "Your favorite albums", self.on_favorites)
 
         self.window.show_all()
 
@@ -313,30 +173,104 @@ class Jamaui(object):
         btn.connect('clicked', callback)
         self.bbox.add(btn)
 
+    def on_request_cover(self, albumid, size):
+        jamaendo.get_album_cover_async(self.got_album_cover, int(albumid), size)
+
+    def got_album_cover(self, albumid, size, cover):
+        postoffice.notify('album-cover', albumid, size, cover)
+
+    #def add_featured_button(self):
+    #    self.featured_sel = hildon.TouchSelector(text=True)
+    #    self.featured_sel.append_text("Albums of the week")
+    #    self.featured_sel.append_text("Tracks of the week")
+    #    self.featured_sel.append_text("New releases")
+    #    btn = hildon.PickerButton(gtk.HILDON_SIZE_THUMB_HEIGHT,
+    #                              hildon.BUTTON_ARRANGEMENT_VERTICAL)
+    #    btn.set_text("Featured", "Most listened to")
+    #    btn.set_property('width-request', 225)
+    #    btn.set_selector(self.featured_sel)
+    #    self.featured_btn = btn
+    #    self.bbox.add(btn)
+
     def destroy(self, widget):
+        postoffice.disconnect('request-album-cover', self)
         gtk.main_quit()
 
     def show_about(self, w, win):
         dialog = gtk.AboutDialog()
+        dialog.set_program_name("jamaendo")
         dialog.set_website("http://github.com/krig")
         dialog.set_website_label("http://github.com/krig")
-        dialog.set_name("Jamaendo")
-        dialog.set_authors(("Kristoffer Gronlund (Purple Scout AB)",))
-        dialog.set_comments("Media player for jamendo.com")
-        dialog.set_version('')
+        dialog.set_version(VERSION)
+        dialog.set_license("""Copyright (c) 2010, Kristoffer Gronlund
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+     * Redistributions of source code must retain the above copyright
+       notice, this list of conditions and the following disclaimer.
+     * Redistributions in binary form must reproduce the above copyright
+       notice, this list of conditions and the following disclaimer in the
+       documentation and/or other materials provided with the distribution.
+     * Neither the name of Jamaendo nor the
+       names of its contributors may be used to endorse or promote products
+       derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+""")
+        dialog.set_authors(("Kristoffer Gronlund <kristoffer.gronlund@purplescout.se>",
+                            "Based on Panucci, written by Thomas Perl <thpinfo.com>"))
+        dialog.set_comments("""Jamaendo plays music from the music catalog of JAMENDO.
+
+JAMENDO is an online platform that distributes musical works under Creative Commons licenses.""")
+        gtk.about_dialog_set_email_hook(self.open_link, dialog)
+        gtk.about_dialog_set_url_hook(self.open_link, dialog)
+        dialog.connect( 'response', lambda dlg, response: dlg.destroy())
+        for parent in dialog.vbox.get_children():
+            for child in parent.get_children():
+                if isinstance(child, gtk.Label):
+                    child.set_selectable(False)
+                    child.set_alignment(0.0, 0.5)
         dialog.run()
         dialog.destroy()
 
     def open_link(self, d, url, data):
+        #print "url: %s" % (url)
         import webbrowser
         webbrowser.open_new(url)
 
 
-    def on_refresh(self, button):
-        dialog = RefreshDialog()
-        dialog.show_all()
-        dialog.run()
-        dialog.hide()
+    #def on_refresh(self, button):
+    #    dialog = RefreshDialog()
+    #    dialog.show_all()
+    #    dialog.run()
+    #    dialog.hide()
+
+    def on_featured(self, button):
+        dialog = hildon.PickerDialog(self.window)
+        sel = hildon.TouchSelector(text=True)
+        for feature, _ in FeaturedWindow.features:
+            sel.append_text(feature)
+        dialog.set_selector(sel)
+        dialog.set_title("Featured")
+        if dialog.run() == gtk.RESPONSE_OK:
+            txt = sel.get_current_text()
+            self.featuredwnd = FeaturedWindow(txt)
+            self.featuredwnd.show_all()
+        dialog.destroy()
+
+    def on_radios(self, button):
+        self.radioswnd = RadiosWindow()
+        self.radioswnd.show_all()
 
     def on_search(self, button):
         self.searchwnd = SearchWindow()
@@ -346,13 +280,36 @@ class Jamaui(object):
         self.playlistswnd = PlaylistsWindow()
         self.playlistswnd.show_all()
 
+    def on_settings(self, button):
+        dialog = gtk.Dialog()
+        dialog.set_title("Settings")
+        dialog.add_button( gtk.STOCK_OK, gtk.RESPONSE_OK )
+        vbox = dialog.vbox
+        tbl = gtk.Table(1, 2)
+        tbl.attach(gtk.Label("Username:"), 0, 1, 0, 1)
+        entry = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
+        entry.set_placeholder("jamendo.com username")
+        if settings.user:
+            entry.set_text(settings.user)
+        tbl.attach(entry, 1, 2, 0, 1)
+        vbox.pack_start(tbl, True, True, 2)
+        dialog.show_all()
+        result = dialog.run()
+        val = entry.get_text()
+        dialog.destroy()
+        #print val, result
+        if val and result == gtk.RESPONSE_OK:
+            #print "new user name:", val
+            settings.user = val
+            self.save_settings()
+
+
     def on_favorites(self, button):
         self.favoriteswnd = FavoritesWindow()
         self.favoriteswnd.show_all()
 
     def on_player(self, button):
-        self.playerwnd = PlayerWindow()
-        self.playerwnd.show_all()
+        open_playerwindow()
 
     '''
     def on_search(self, button):
@@ -371,11 +328,13 @@ class Jamaui(object):
     '''
 
     def run(self):
+        ossohelper.application_init('org.jamaendo', '0.1')
         self.create_window()
         self.create_menu()
         self.setup_widgets()
         self.window.show_all()
         gtk.main()
+        ossohelper.application_exit()
 
 if __name__=="__main__":
     ui = Jamaui()