Beispiel #1
0
class MusicListItem(TreeItem):
    PLAYING_LIST_TYPE = 1
    FAVORITE_LIST_TYPE = 2
    CREATED_LIST_TYPE = 3
    COLLECTED_LIST_TYPE = 4
    LOGIN_LIST_TYPE = 5

    def __init__(self, list_data, is_online_list=False, is_login_item=False):
        TreeItem.__init__(self)

        self.column_index = 0
        self.side_padding = 5
        self.is_highlight = False
        self.padding_y = 0
        self.padding_x = 8

        if isinstance(list_data, basestring):
            self.title = list_data
            self.data = dict()
            self.list_type = self.PLAYING_LIST_TYPE
        else:
            self.title = list_data.get("name", "")
            self.data = list_data
            #self.list_id = list_data.get('id', '')

        self.data = list_data
        if is_online_list:
            if self.data['specialType'] == 5:
                self.list_type = self.FAVORITE_LIST_TYPE
            elif self.data['subscribed']:
                self.list_type = self.COLLECTED_LIST_TYPE
            else:
                self.list_type = self.CREATED_LIST_TYPE
        else:
            self.list_type = self.PLAYING_LIST_TYPE
        if is_login_item:
            self.list_type = self.LOGIN_LIST_TYPE
        self.has_separator = 1
        self.separator_height = 4
        self.item_width = CATEGROYLIST_WIDTH
        self.item_height = 26 + self.separator_height if self.has_separator else 26
        self.init_pixbufs()

        self.song_view = MusicView(data=self.data, view_type=self.list_type)
        self.song_view.set_size_request(PLAYLIST_WIDTH, -1)

        if is_login_item:
            self.login_box = LoginBox(lambda w: event_manager.emit("login"))

            event_manager.connect("login", self.login)

        #event_manager.connect("login-success", self.on_event_login_success)

        self.main_box = gtk.VBox()

    def login(self, args=None, *kwargs):
        username = self.login_box.username_entry.get_text()
        password = self.login_box.password_entry.get_text()
        utils.ThreadFetch(fetch_funcs=(nplayer.login_and_get_cookie,
                                       (username, password)),
                          success_funcs=(self.login_success, ())).start()

    def login_success(self, args, *kwargs):
        event_manager.emit('login-success')

    def init_pixbufs(self):
        if self.list_type == self.PLAYING_LIST_TYPE:
            normal_image_name = "playing_list.png"
            press_image_name = "playing_list_press.png"

        elif self.list_type == self.FAVORITE_LIST_TYPE:
            normal_image_name = "favorite_list.png"
            press_image_name = "favorite_list_press.png"

        elif self.list_type == self.CREATED_LIST_TYPE:
            normal_image_name = "created_list.png"
            press_image_name = "created_list_press.png"

        elif self.list_type == self.COLLECTED_LIST_TYPE:
            normal_image_name = "collected_list.png"
            press_image_name = "collected_list_press.png"
        else:
            normal_image_name = "marvin.png"
            press_image_name = "marvin.png"

        self.normal_pixbuf = gtk.gdk.pixbuf_new_from_file(
            get_image(normal_image_name))
        self.press_pixbuf = gtk.gdk.pixbuf_new_from_file(
            get_image(press_image_name))
        self.icon_width = self.normal_pixbuf.get_width()

    def get_height(self):
        return self.item_height

    def get_column_widths(self):
        return (self.item_width, )

    def get_column_renders(self):
        return (self.render_title, )

    def emit_redraw_request(self):
        if self.redraw_request_callback:
            self.redraw_request_callback(self)

    def set_title(self, title):
        self.title = title
        self.emit_redraw_request()

    def render_title(self, cr, rect):
        # Draw select background.

        rect.y += self.padding_y + 2
        # draw separator
        if self.has_separator:
            draw_separator(cr, rect.x, rect.y, rect.width, 1)
            rect.y += self.padding_y + self.separator_height - 2
            rect.height -= self.separator_height

        if self.is_highlight:
            draw_single_mask(cr, rect.x + 1, rect.y, rect.width - 2,
                             rect.height, "globalItemHighlight")
        elif self.is_hover:
            draw_single_mask(cr, rect.x + 1, rect.y, rect.width - 2,
                             rect.height, "globalItemHover")

        rect.x += self.padding_x
        rect.width -= self.padding_x * 2

        if self.is_highlight:
            pixbuf = self.press_pixbuf
        else:
            pixbuf = self.normal_pixbuf

        if pixbuf:
            icon_y = rect.y + (rect.height -
                               self.normal_pixbuf.get_height()) / 2

            draw_pixbuf(cr, pixbuf, rect.x, icon_y)
            rect.x += self.icon_width + self.padding_x
            rect.width -= self.icon_width - self.padding_x

        if self.is_highlight:
            text_color = "#FFFFFF"
        else:
            text_color = app_theme.get_color("labelText").get_color()

        draw_text(cr,
                  self.title,
                  rect.x,
                  rect.y,
                  rect.width,
                  rect.height,
                  text_size=10,
                  text_color=text_color,
                  alignment=pango.ALIGN_LEFT)

    def unselect(self):
        self.is_select = False
        self.emit_redraw_request()

    def select(self):
        self.is_select = True
        self.emit_redraw_request()

    def unhover(self, column, offset_x, offset_y):
        self.is_hover = False
        self.emit_redraw_request()

    def hover(self, column, offset_x, offset_y):
        self.is_hover = True
        self.emit_redraw_request()

    def highlight(self):
        self.is_highlight = True
        self.emit_redraw_request()

    def unhighlight(self):
        self.is_highlight = False
        self.emit_redraw_request()

    @property
    def list_widget(self):
        switch_tab(self.main_box, self.song_view)
        if not nplayer.is_login and self.list_type == MusicView.LOGIN_LIST_TYPE:
            switch_tab(self.main_box, self.login_box)

        return self.main_box

    def on_event_login_success(self, obj, data):
        self.song_view.load_online_playlists(clear=True)
        switch_tab(self.main_box, self.song_view)

        self.emit_redraw_request()

    def dump_list(self):
        songs = self.song_view.dump_songs()
        return (self.title, songs)

    get_songs = property(lambda self: self.song_view.get_songs)
    add_songs = property(lambda self: self.song_view.add_songs)
    refrush = property(lambda self: self.song_view.refrush)
    list_id = property(lambda self: self.song_view.list_id)
    current_song = property(lambda self: self.song_view.current_song)
    play_song = property(lambda self: self.song_view.play_song)
class MusicListItem(TreeItem):
    PLAYING_LIST_TYPE = 1
    FAVORITE_LIST_TYPE = 2
    CREATED_LIST_TYPE = 3
    COLLECTED_LIST_TYPE = 4
    LOGIN_LIST_TYPE = 5
    PERSONAL_FM_ITEM = 6

    def __init__(self, list_data, list_type, is_online_list=False,
            has_separator=True):
        TreeItem.__init__(self)

        self.column_index = 0
        self.side_padding = 5
        self.is_highlight = False
        self.padding_y = 0
        self.padding_x = 8

        if list_type and list_type in [self.PLAYING_LIST_TYPE,
                self.PERSONAL_FM_ITEM, self.LOGIN_LIST_TYPE]:
            self.title = list_data
        else:
            self.title = list_data.get("name", "")
        self.data = list_data
        self.list_type= list_type

        if is_online_list:
            if self.data['specialType'] == 5:
                self.list_type = self.FAVORITE_LIST_TYPE
            elif self.data['subscribed']:
                self.list_type = self.COLLECTED_LIST_TYPE
            else:
                self.list_type = self.CREATED_LIST_TYPE

        self.has_separator = has_separator
        self.separator_height = 4
        self.item_width = CATEGROYLIST_WIDTH
        self.item_height = 26 + self.separator_height if self.has_separator else 26
        self.init_pixbufs()

        self.song_view = MusicView(data=self.data, view_type=self.list_type)
        self.song_view.set_size_request(PLAYLIST_WIDTH, -1)

        if self.list_type == self.LOGIN_LIST_TYPE:
            self.login_box = LoginBox(self.login, self.login_with_sina_microblog_account)

            event_manager.connect("login", self.login)

        self.main_box = gtk.VBox()

    def login_with_sina_microblog_account(self, *kwargs):
        self.login_dialog = LoginDialog("http://music.163.com/api/sns/authorize?snsType=2&clientType=web2&callbackType=Login&forcelogin=true")
        self.login_dialog.show_window()

    def login(self, args=None, *kwargs):
        username = self.login_box.username_entry.get_text()
        password = self.login_box.password_entry.get_text()
        utils.ThreadFetch(
            fetch_funcs=(nplayer.login_and_get_cookie, (username,password)),
            success_funcs=(self.login_success, ())).start()

    def login_success(self, args, *kwargs):
        event_manager.emit('login-success')

    def init_pixbufs(self):
        if self.list_type == self.PLAYING_LIST_TYPE:
            normal_image_name = "playing_list.png"
            press_image_name = "playing_list_press.png"

        elif self.list_type == self.PERSONAL_FM_ITEM:
            normal_image_name = "personal_fm.png"
            press_image_name = "personal_fm_press.png"

        elif self.list_type == self.FAVORITE_LIST_TYPE:
            normal_image_name = "favorite_list.png"
            press_image_name = "favorite_list_press.png"

        elif self.list_type == self.CREATED_LIST_TYPE:
            normal_image_name = "created_list.png"
            press_image_name = "created_list_press.png"

        elif self.list_type == self.COLLECTED_LIST_TYPE:
            normal_image_name = "collected_list.png"
            press_image_name = "collected_list_press.png"
        else:
            normal_image_name = "marvin.png"
            press_image_name = "marvin.png"

        self.normal_pixbuf = gtk.gdk.pixbuf_new_from_file(
                                            get_image(normal_image_name))
        self.press_pixbuf = gtk.gdk.pixbuf_new_from_file(
                                            get_image(press_image_name))
        self.icon_width = self.normal_pixbuf.get_width()

    def get_height(self):
        return self.item_height

    def get_column_widths(self):
        return (self.item_width,)

    def get_column_renders(self):
        return (self.render_title,)

    def emit_redraw_request(self):
        if self.redraw_request_callback:
            self.redraw_request_callback(self)

    def set_title(self, title):
        self.title = title
        self.emit_redraw_request()

    def render_title(self, cr, rect):
        # Draw select background.

        rect.y += self.padding_y + 2
        # draw separator
        if self.has_separator:
            draw_separator(cr, rect.x, rect.y, rect.width, 1)
            rect.y += self.padding_y + self.separator_height - 2
            rect.height -= self.separator_height

        if self.is_highlight:
            draw_single_mask(cr, rect.x+1, rect.y, rect.width-2, rect.height,
                    "globalItemHighlight")
        elif self.is_hover:
            draw_single_mask(cr, rect.x+1, rect.y, rect.width-2, rect.height,
                    "globalItemHover")

        rect.x += self.padding_x
        rect.width -= self.padding_x * 2

        if self.is_highlight:
            pixbuf = self.press_pixbuf
        else:
            pixbuf = self.normal_pixbuf

        if pixbuf:
            icon_y = rect.y + (rect.height - self.normal_pixbuf.get_height())/2

            draw_pixbuf(cr, pixbuf, rect.x, icon_y)
            rect.x += self.icon_width + self.padding_x
            rect.width -= self.icon_width - self.padding_x

        if self.is_highlight:
            text_color = "#FFFFFF"
        else:
            text_color = app_theme.get_color("labelText").get_color()

        draw_text(cr, self.title, rect.x, rect.y, rect.width, rect.height,
            text_size=10, text_color=text_color, alignment=pango.ALIGN_LEFT)

    def unselect(self):
        self.is_select = False
        self.emit_redraw_request()

    def select(self):
        self.is_select = True
        self.emit_redraw_request()

    def unhover(self, column, offset_x, offset_y):
        self.is_hover = False
        self.emit_redraw_request()

    def hover(self, column, offset_x, offset_y):
        self.is_hover = True
        self.emit_redraw_request()

    def highlight(self):
        self.is_highlight = True
        self.emit_redraw_request()

    def unhighlight(self):
        self.is_highlight = False
        self.emit_redraw_request()

    @property
    def list_widget(self):
        switch_tab(self.main_box, self.song_view)
        if not nplayer.is_login and self.list_type == MusicView.LOGIN_LIST_TYPE:
            switch_tab(self.main_box, self.login_box)

        return self.main_box

    def dump_list(self):
        songs = self.song_view.dump_songs()
        return (self.title, songs)

    get_songs = property(lambda self: self.song_view.get_songs)
    add_songs = property(lambda self: self.song_view.add_songs)
    refrush = property(lambda self: self.song_view.refrush)
    list_id = property(lambda self: self.song_view.list_id)
    current_song = property(lambda self: self.song_view.current_song)
    play_song = property(lambda self: self.song_view.request_song)
class MusicListItem(TreeItem):
    PLAYING_LIST_TYPE = 1
    FAVORITE_LIST_TYPE = 2
    CREATED_LIST_TYPE = 3
    COLLECTED_LIST_TYPE = 4
    LOGIN_LIST_TYPE = 5
    PERSONAL_FM_ITEM = 6

    def __init__(self, list_data, list_type, is_online_list=False,
            has_separator=True):
        TreeItem.__init__(self)

        self.column_index = 0
        self.side_padding = 5
        self.is_highlight = False
        self.padding_y = 0
        self.padding_x = 8

        if list_type and list_type in [self.PLAYING_LIST_TYPE,
                self.PERSONAL_FM_ITEM, self.LOGIN_LIST_TYPE]:
            self.title = list_data
        else:
            self.title = list_data.get("name", "")
        self.title = gobject.markup_escape_text(self.title)
        self.data = list_data
        self.list_type= list_type

        if is_online_list:
            if self.data['specialType'] == 5:
                self.list_type = self.FAVORITE_LIST_TYPE
            elif self.data['subscribed']:
                self.list_type = self.COLLECTED_LIST_TYPE
            else:
                self.list_type = self.CREATED_LIST_TYPE

        self.has_separator = has_separator
        self.separator_height = 4
        self.item_width = CATEGROYLIST_WIDTH
        self.item_height = 26 + self.separator_height if self.has_separator else 26
        self.init_pixbufs()

        self.song_view = MusicView(data=self.data, view_type=self.list_type)
        self.song_view.set_size_request(PLAYLIST_WIDTH, -1)

        if self.list_type == self.LOGIN_LIST_TYPE:
            self.login_box = LoginBox(self.login, self.login_with_sina_microblog_account)

            event_manager.connect("login", self.login)
            self.login_box.password_entry.connect('activate', self.login)

        self.main_box = gtk.VBox()

    def login_with_sina_microblog_account(self, *kwargs):
        self.login_dialog = LoginDialog("http://music.163.com/api/sns/authorize?snsType=2&clientType=web2&callbackType=Login&forcelogin=true")
        self.login_dialog.show_window()

    def login(self, args=None, *kwargs):
        username = self.login_box.username_entry.get_text()
        password = self.login_box.password_entry.get_text()
        utils.ThreadFetch(
            fetch_funcs=(nplayer.login_and_get_cookie, (username,password)),
            success_funcs=(self.login_success, ())).start()

    def login_success(self, args, *kwargs):
        event_manager.emit('login-success')

    def init_pixbufs(self):
        if self.list_type == self.PLAYING_LIST_TYPE:
            normal_image_name = "playing_list.png"
            press_image_name = "playing_list_press.png"

        elif self.list_type == self.PERSONAL_FM_ITEM:
            normal_image_name = "personal_fm.png"
            press_image_name = "personal_fm_press.png"

        elif self.list_type == self.FAVORITE_LIST_TYPE:
            normal_image_name = "favorite_list.png"
            press_image_name = "favorite_list_press.png"

        elif self.list_type == self.CREATED_LIST_TYPE:
            normal_image_name = "created_list.png"
            press_image_name = "created_list_press.png"

        elif self.list_type == self.COLLECTED_LIST_TYPE:
            normal_image_name = "collected_list.png"
            press_image_name = "collected_list_press.png"
        else:
            normal_image_name = "marvin.png"
            press_image_name = "marvin.png"

        self.normal_pixbuf = gtk.gdk.pixbuf_new_from_file(
                                            get_image(normal_image_name))
        self.press_pixbuf = gtk.gdk.pixbuf_new_from_file(
                                            get_image(press_image_name))
        self.icon_width = self.normal_pixbuf.get_width()

    def get_height(self):
        return self.item_height

    def get_column_widths(self):
        return (self.item_width,)

    def get_column_renders(self):
        return (self.render_title,)

    def emit_redraw_request(self):
        if self.redraw_request_callback:
            self.redraw_request_callback(self)

    def set_title(self, title):
        self.title = gobject.markup_escape_text(title)
        self.emit_redraw_request()

    def render_title(self, cr, rect):
        # Draw select background.

        rect.y += self.padding_y + 2
        # draw separator
        if self.has_separator:
            draw_separator(cr, rect.x, rect.y, rect.width, 1)
            rect.y += self.padding_y + self.separator_height - 2
            rect.height -= self.separator_height

        if self.is_highlight:
            draw_single_mask(cr, rect.x+1, rect.y, rect.width-2, rect.height,
                    "globalItemHighlight")
        elif self.is_hover:
            draw_single_mask(cr, rect.x+1, rect.y, rect.width-2, rect.height,
                    "globalItemHover")

        rect.x += self.padding_x
        rect.width -= self.padding_x * 2

        if self.is_highlight:
            pixbuf = self.press_pixbuf
        else:
            pixbuf = self.normal_pixbuf

        if pixbuf:
            icon_y = rect.y + (rect.height - self.normal_pixbuf.get_height())/2

            draw_pixbuf(cr, pixbuf, rect.x, icon_y)
            rect.x += self.icon_width + self.padding_x
            rect.width -= self.icon_width - self.padding_x

        if self.is_highlight:
            text_color = "#FFFFFF"
        else:
            text_color = app_theme.get_color("labelText").get_color()

        draw_text(cr, self.title, rect.x, rect.y, rect.width, rect.height,
            text_size=10, text_color=text_color, alignment=pango.ALIGN_LEFT)

    def unselect(self):
        self.is_select = False
        self.emit_redraw_request()

    def select(self):
        self.is_select = True
        self.emit_redraw_request()

    def unhover(self, column, offset_x, offset_y):
        self.is_hover = False
        self.emit_redraw_request()

    def hover(self, column, offset_x, offset_y):
        event_manager.emit("update-playlist-tooltip",
                HTMLParser().unescape(self.title));
        self.is_hover = True
        self.emit_redraw_request()

    def highlight(self):
        self.is_highlight = True
        self.emit_redraw_request()

    def unhighlight(self):
        self.is_highlight = False
        self.emit_redraw_request()

    @property
    def list_widget(self):
        switch_tab(self.main_box, self.song_view)
        if not nplayer.is_login and self.list_type == MusicView.LOGIN_LIST_TYPE:
            switch_tab(self.main_box, self.login_box)

        return self.main_box

    def dump_list(self):
        songs = self.song_view.dump_songs()
        return (self.title, songs)

    get_songs = property(lambda self: self.song_view.get_songs)
    add_songs = property(lambda self: self.song_view.add_songs)
    refrush = property(lambda self: self.song_view.refrush)
    list_id = property(lambda self: self.song_view.list_id)
    current_song = property(lambda self: self.song_view.current_song)
    play_song = property(lambda self: self.song_view.request_song)