示例#1
0
文件: search.py 项目: krig/jamaendo
class SearchWindow(hildon.StackableWindow):
    def __init__(self):
        hildon.StackableWindow.__init__(self)
        self.set_title("Search")
        self.idmap = {}

        vbox = gtk.VBox(False, 0)

        self.fetcher = None
        self.connect('destroy', self.on_destroy)

        # Results list
        self.panarea = hildon.PannableArea()
        self.musiclist = MusicList()
        self.musiclist.loading_message = "Nothing found yet"
        self.musiclist.empty_message = "No matching results"
        self.musiclist.connect('row-activated', self.row_activated)
        self.panarea.add(self.musiclist)
        vbox.pack_start(self.panarea, True, True, 0)


        # Create selector for search mode
        self.mode_selector = hildon.TouchSelector(text=True)

        self.mode_selector.append_text("Artists")
        self.mode_selector.append_text("Albums")
        self.mode_selector.append_text("Tracks")
        self.mode = hildon.PickerButton(gtk.HILDON_SIZE_FINGER_HEIGHT,
                                        hildon.BUTTON_ARRANGEMENT_VERTICAL)
        self.mode.set_title("Search for")
        self.mode.set_selector(self.mode_selector)
        self.mode_selector.connect("changed", self.mode_changed)
        #vbox.pack_start(self.mode, False)
        self.mode.set_active(1)


        # Search box
        hbox = gtk.HBox(False, 0)
        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(">>")
        btn.connect('clicked', self.on_search)
        hbox.pack_start(self.mode, False)
        hbox.pack_start(self.entry, True, True, 0)
        hbox.pack_start(btn, False)
        vbox.pack_start(hbox, False)
        self.add(vbox)

        self.create_menu()

    def create_menu(self):
        def on_player(*args):
            from playerwindow import open_playerwindow
            open_playerwindow()
        self.menu = hildon.AppMenu()
        player = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
        player.set_label("Open player")
        player.connect("clicked", on_player)
        self.menu.append(player)
        self.menu.show_all()
        self.set_app_menu(self.menu)

    def on_destroy(self, wnd):
        if self.fetcher:
            self.fetcher.stop()
            self.fetcher = None

    def mode_changed(self, selector, user_data):
        pass
        #current_selection = selector.get_current_text()

    def on_search(self, w):
        mode = self.mode.get_active()
        txt = self.entry.get_text()
        self.musiclist.set_loading(False)
        self.musiclist.empty_message = "Searching..."
        self.musiclist.get_model().clear()

        if self.fetcher:
            self.fetcher.stop()
            self.fetcher = None

        itemgen = None
        if mode == 0:
            itemgen = lambda: jamaendo.search_artists(query=txt)
        elif mode == 1:
            itemgen = lambda: jamaendo.search_albums(query=txt)
        elif mode == 2:
            itemgen = lambda: jamaendo.search_tracks(query=txt)
        else:
            return

        self.fetcher = Fetcher(itemgen, self,
                               on_item = self.on_add_result,
                               on_ok = self.on_add_complete,
                               on_fail = self.on_add_complete)
        self.fetcher.start()
        '''
        try:
            if mode == 0:
                items = jamaendo.search_artists(query=txt)
            elif mode == 1:
                items = jamaendo.search_albums(query=txt)
            elif mode == 2:
                items = jamaendo.search_tracks(query=txt)

            for item in items:
                self.idmap[item.ID] = item

            self.musiclist.add_items(items)
        except jamaendo.JamaendoAPIException:
            # nothing found, force redraw
            self.musiclist.queue_draw()
        '''

    def on_add_result(self, wnd, item):
        if wnd is self:
            self.musiclist.add_items([item])
            self.idmap[item.ID] = item

    def on_add_complete(self, wnd, error=None):
        if wnd is self:
            self.musiclist.empty_message = "No matching results"
            self.musiclist.queue_draw()
            self.fetcher.stop()
            self.fetcher = None

    def row_activated(self, treeview, path, view_column):
        _id = self.musiclist.get_item_id(path)
        item = self.idmap[_id]
        self.open_item(item)

    def open_item(self, item):
        if isinstance(item, jamaendo.Album):
            wnd = ShowAlbum(item)
            wnd.show_all()
        elif isinstance(item, jamaendo.Artist):
            wnd = ShowArtist(item)
            wnd.show_all()
        elif isinstance(item, jamaendo.Track):
            wnd = open_playerwindow()
            wnd.play_tracks([item])
示例#2
0
文件: featured.py 项目: krig/jamaendo
class FeaturedWindow(hildon.StackableWindow):
    features = (
        ("New releases",jamaendo.new_releases),
        ("Top albums today", lambda: jamaendo.top_albums(order='ratingday_desc')),
        ("Top tracks today", lambda: jamaendo.top_tracks(order='ratingday_desc')),
        ("Albums of the week",jamaendo.albums_of_the_week),
        ("Tracks of the week",jamaendo.tracks_of_the_week),
        ("Top 50 tags", lambda: jamaendo.top_tags(count=50)),
        ("Top 50 artists", lambda: jamaendo.top_artists(count=50)),
        ("Top 50 albums", lambda: jamaendo.top_albums(count=50)),
        ("Top 50 tracks", lambda: jamaendo.top_tracks(count=50)),
        )

    def __init__(self, feature):
        hildon.StackableWindow.__init__(self)
        self.set_title(feature)

        self.fetcher = None
        self.connect('destroy', self.on_destroy)
        self.featurefn = _alist(self.features, feature)

        # Results list
        self.panarea = hildon.PannableArea()
        self.musiclist = MusicList()
        self.musiclist.connect('row-activated', self.row_activated)
        self.panarea.add(self.musiclist)

        self.idmap = {}
        self.items = []

        self.add(self.panarea)

        self.create_menu()

        self.start_feature_fetcher()

    def start_feature_fetcher(self):
        if self.fetcher:
            self.fetcher.stop()
            self.fetcher = None
        self.fetcher = Fetcher(self.featurefn, self,
                               on_item = self.on_feature_result,
                               on_ok = self.on_feature_complete,
                               on_fail = self.on_feature_complete)
        self.fetcher.start()

    def on_feature_result(self, wnd, item):
        if wnd is self:
            self.musiclist.add_items([item])
            self.idmap[item.ID] = item
            self.items.append(item)

    def on_feature_complete(self, wnd, error=None):
        if wnd is self:
            if error:
                banner = hildon.hildon_banner_show_information(self, '', "Unable to get list")
                banner.set_timeout(2000)
            self.fetcher.stop()
            self.fetcher = None

    def create_menu(self):
        def on_player(*args):
            from playerwindow import open_playerwindow
            open_playerwindow()
        self.menu = hildon.AppMenu()
        player = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
        player.set_label("Open player")
        player.connect("clicked", on_player)
        self.menu.append(player)
        self.menu.show_all()
        self.set_app_menu(self.menu)

    def on_destroy(self, wnd):
        if self.fetcher:
            self.fetcher.stop()
            self.fetcher = None

    def row_activated(self, treeview, path, view_column):
        _id = self.musiclist.get_item_id(path)
        item = self.idmap[_id]
        self.open_item(item)

    def open_item(self, item):
        if isinstance(item, jamaendo.Album):
            wnd = ShowAlbum(item)
            wnd.show_all()
        elif isinstance(item, jamaendo.Artist):
            wnd = ShowArtist(item)
            wnd.show_all()
        elif isinstance(item, jamaendo.Track):
            playlist = Playlist(self.items)
            playlist.jump_to(item.ID)
            wnd = open_playerwindow()
            wnd.play_tracks(playlist)
        elif isinstance(item, jamaendo.Tag):
            self.start_tag_fetcher(item.ID)

    def start_tag_fetcher(self, item_id):
        if self.fetcher:
            self.fetcher.stop()
            self.fetcher = None
        self.fetcher = Fetcher(lambda: jamaendo.get_tag_tracks(item_id),
                               self,
                               on_item = self.on_tag_result,
                               on_ok = self.on_tag_complete,
                               on_fail = self.on_tag_complete)
        self.fetcher.taglist = []
        self.fetcher.start()
        banner = hildon.hildon_banner_show_information(self, '', "Getting tracks for tag")
        banner.set_timeout(2000)

    def on_tag_result(self, wnd, item):
        if wnd is self:
            self.fetcher.taglist.append(item)

    def on_tag_complete(self, wnd, error=None):
        if wnd is self:
            taglist = self.fetcher.taglist
            self.fetcher.stop()
            if not error:
                wnd = open_playerwindow()
                wnd.play_tracks(taglist)
            else:
                banner = hildon.hildon_banner_show_information(self, '', "Error getting tracks")
                banner.set_timeout(2000)
            self.fetcher = None
示例#3
0
class FavoritesWindow(hildon.StackableWindow):
    def __init__(self):
        hildon.StackableWindow.__init__(self)
        self.set_title("Favorites")
        self.connect('destroy', self.on_destroy)
        self.fetcher = None
        self.idmap = {}

        self.panarea = hildon.PannableArea()
        self.favorites = MusicList()
        self.favorites.connect('row-activated', self.row_activated)
        self.panarea.add(self.favorites)
        self.add(self.panarea)

        if not settings.user:
            self.favorites.loading_message = """give your username
to the settings dialog
favorites appear
"""
        else:
            self.favorites.loading_message = """Loading favorites"""

        self.start_favorites_fetcher()

    def on_destroy(self, wnd):
        if self.fetcher:
            self.fetcher.stop()
            self.fetcher = None

    def start_favorites_fetcher(self):
        if self.fetcher:
            self.fetcher.stop()
            self.fetcher = None

        def gen():
            generated = []
            for item in jamaendo.favorite_albums(settings.user):
                generated.append(item.ID)
                yield item
            fav = [f[1] for f in settings.favorites \
                       if isinstance(f, tuple) and \
                       len(f) == 2 and \
                       f[0] == 'album' and \
                       f[1] not in generated]
            for item in jamaendo.get_albums(fav):
                yield item

        self.fetcher = Fetcher(gen,
                               self,
                               on_item = self.on_favorites_result,
                               on_ok = self.on_favorites_complete,
                               on_fail = self.on_favorites_complete)
        self.fetcher.start()

    def on_favorites_result(self, wnd, item):
        if wnd is self:
            if item.ID not in self.idmap:
                self.idmap[item.ID] = item
                self.favorites.add_items([item])

    def on_favorites_complete(self, wnd, error=None):
        if wnd is self:
            self.fetcher.stop()
            self.fetcher = None

    def get_item_text(self, item):
        if isinstance(item, jamaendo.Album):
            return "%s - %s" % (item.artist_name, item.name)
        elif isinstance(item, jamaendo.Track):
            return "%s - %s" % (item.artist_name, item.name)
        else:
            return item.name

    def row_activated(self, treeview, path, view_column):
        _id = self.favorites.get_item_id(path)
        item = self.idmap.get(_id)
        if item:
            self.open_item(item)

    def open_item(self, item):
        if isinstance(item, jamaendo.Album):
            wnd = ShowAlbum(item)
            wnd.show_all()
        elif isinstance(item, jamaendo.Artist):
            wnd = ShowArtist(item)
            wnd.show_all()
        elif isinstance(item, jamaendo.Track):
            wnd = open_playerwindow()
            wnd.play_tracks([item])