Ejemplo n.º 1
0
    def __init__(self):
        gtk.HBox.__init__(self)
        self.connect("expose-event", self.on_page_expose_event)

        self.aibizhi_cache_page = CachePage(Aibizhi())
        self.bizhi360_cache_page = CachePage(Bizhi360())
        self.aibizhi_cache_page.cache_view.try_to_fetch()
        self.bizhi360_cache_page.cache_view.try_to_fetch()

        self.favorites_page = FavoritePage(get_favorite_dir())
        self.pictures_page = LocalPicturePage(get_download_wallpaper_dir())

        self.add_item = AddItem()
        self.pictures_page.select_view.add_items([self.add_item])

        self.task_page = TaskPage()

        self.switch_page = gtk.VBox()
        self.__init_navigatebar()

        self.pack_start(self.navigatebar, False, True)
        self.pack_start(self.switch_page, True, True)

        event_manager.add_callback("switch-to-local-pictures",
                                   self.switch_to_local_pictures)
    def __init__(self, padding_x=8, padding_y=10):
        IconView.__init__(self, padding_x=padding_x, padding_y=padding_y)

        self.add_item = AddItem()
        self.add_items([self.add_item])

        self.connect("right-click-item", self.__on_right_click_item)

        event_manager.add_callback("add-wallpapers", self.on_add_wallpapers)
        event_manager.add_callback("add-download-wallpapers",
                                   self.on_add_wallpapers)
        event_manager.add_callback("wallpapers-deleted",
                                   self.on_wallpapers_deleted)
        event_manager.add_callback("select-wallpaper",
                                   self.on_wallpaper_select)
        event_manager.add_callback("apply-wallpaper", self.on_wallpaper_apply)
        event_manager.add_callback("apply-download-wallpaper",
                                   self.on_download_wallpaper_apply)
        event_manager.add_callback("delete-wallpaper-link",
                                   self.__on_delete_wallpaper_link)
        self.theme = None
    def __init__(self, padding_x=8, padding_y=10):
        IconView.__init__(self, padding_x=padding_x, padding_y=padding_y)
        
        self.add_item = AddItem()
        self.add_items([self.add_item])

        self.connect("right-click-item", self.__on_right_click_item)
        
        event_manager.add_callback("add-wallpapers", self.on_add_wallpapers)
        event_manager.add_callback("add-download-wallpapers", self.on_add_wallpapers)
        event_manager.add_callback("wallpapers-deleted", self.on_wallpapers_deleted)
        event_manager.add_callback("select-wallpaper", self.on_wallpaper_select)
        event_manager.add_callback("apply-wallpaper", self.on_wallpaper_apply)
        event_manager.add_callback("apply-download-wallpaper", self.on_download_wallpaper_apply)
        event_manager.add_callback("delete-wallpaper-link", self.__on_delete_wallpaper_link)
        self.theme = None
class WallpaperView(IconView):
    def __init__(self, padding_x=8, padding_y=10):
        IconView.__init__(self, padding_x=padding_x, padding_y=padding_y)

        self.add_item = AddItem()
        self.add_items([self.add_item])

        self.connect("right-click-item", self.__on_right_click_item)

        event_manager.add_callback("add-wallpapers", self.on_add_wallpapers)
        event_manager.add_callback("add-download-wallpapers",
                                   self.on_add_wallpapers)
        event_manager.add_callback("wallpapers-deleted",
                                   self.on_wallpapers_deleted)
        event_manager.add_callback("select-wallpaper",
                                   self.on_wallpaper_select)
        event_manager.add_callback("apply-wallpaper", self.on_wallpaper_apply)
        event_manager.add_callback("apply-download-wallpaper",
                                   self.on_download_wallpaper_apply)
        event_manager.add_callback("delete-wallpaper-link",
                                   self.__on_delete_wallpaper_link)
        self.theme = None

    def __on_delete_wallpaper_link(self, name, obj, data):
        items = filter(lambda item: item.image_path == data, self.items)
        if items:
            self.delete_items(items)

    def __on_right_click_item(self, widget, item, x, y):
        menu_items = [(None, _("Apply Wallpaper"),
                       lambda: item.do_apply_wallpaper())]
        Menu(menu_items, True).show((int(x), int(y)))

    def set_theme(self, theme):
        self.theme = theme
        self.clear()
        self.add_item.set_theme(self.theme)

        self.add_items([self.add_item])

        self.add_system_wallpapers(self.theme.get_system_wallpapers())
        self.add_user_wallpapers(self.theme.get_user_wallpapers())

    def is_editable(self):
        if self.theme.is_system_theme:
            if len(self.theme.get_user_wallpapers()) == 0:
                return False

        return True

    def is_exists(self, image):
        if self.theme == None:
            return False

        if image in self.theme.get_user_wallpapers():
            return True
        return False

    def add_user_wallpapers(self, image_paths, save=False):
        self.add_images(image_paths, readonly=False)
        if save:
            if self.theme == None:
                return

            self.theme.add_user_wallpapers(image_paths)

    def add_system_wallpapers(self, image_paths):
        self.add_images(image_paths, readonly=True)

    def add_images(self, images, readonly=False):
        images = list(set(images))
        items = map(
            lambda image: WallpaperItem(image, readonly, self.theme,
                                        background_gsettings), images)
        self.add_items(items, insert_pos=-1)

    def on_wallpaper_select(self, name, obj, select_item):
        image_uris = [
            "file://%s" % item.image_path for item in self.items
            if item.is_tick
        ]
        self.apply_wallpapers(image_uris)

    def is_deletable(self):
        for item in self.items:
            if not item.is_tick:
                return True

        return False

    def delete_wallpaper(self):
        for item in self.items:
            if not item.is_tick:
                self.theme.remove_option("user_wallpaper", item.image_path)

        self.theme.save()
        self.set_theme(self.theme, True)

    def is_randomable(self):
        i = 0

        for item in self.items:
            if item.is_tick:
                i += 1
        if i < 2:
            return False

        return True

    def is_select_all(self):
        for item in self.items:
            if item.__class__.__name__ == "AddItem":
                continue

            if not item.is_tick:
                return False

        return True

    def select_all(self):
        is_select_all = self.is_select_all()

        for item in self.items:
            if is_select_all:
                item.untick()
            else:
                item.tick()

        image_uris = [
            "file://%s" % item.image_path for item in self.items
            if item.is_tick
        ]
        self.apply_wallpapers(image_uris)

    def on_download_wallpaper_apply(self, name, obj, image_path):
        image_uris = ["file://%s" % image_path]
        self.apply_wallpapers(image_uris)
        if self.is_randomable():
            self.items[-2].tick()

    def on_wallpaper_apply(self, name, obj, apply_item):
        [item.untick() for item in self.items if item != apply_item]
        image_uris = ["file://%s" % apply_item.image_path]
        self.apply_wallpapers(image_uris)

    def apply_wallpapers(self, image_paths):
        image_path_string = ";".join(image_paths)
        background_gsettings.set_string("picture-uris", image_path_string)
        if self.theme:
            self.theme.save()

    def on_add_wallpapers(self, name, obj, image_paths):
        filter_images = filter(lambda image: not self.is_exists(image),
                               image_paths)
        if filter_images:
            self.add_user_wallpapers(filter_images, save=True)
        event_manager.emit("update-theme", self.theme)

    def on_wallpapers_deleted(self, name, obj, image_paths):
        items = filter(lambda item: item.image_path in image_paths, self.items)
        if items:
            self.delete_items(items)

    def get_scrolled_window(self):
        scrolled_window = ScrolledWindow()
        scrolled_window.add_child(self)
        return scrolled_window

    def draw_mask(self, cr, x, y, w, h):
        cr.set_source_rgb(1, 1, 1)
        cr.rectangle(x, y, w, h)
        cr.fill()
class WallpaperView(IconView):
    
    def __init__(self, padding_x=8, padding_y=10):
        IconView.__init__(self, padding_x=padding_x, padding_y=padding_y)
        
        self.add_item = AddItem()
        self.add_items([self.add_item])

        self.connect("right-click-item", self.__on_right_click_item)
        
        event_manager.add_callback("add-wallpapers", self.on_add_wallpapers)
        event_manager.add_callback("add-download-wallpapers", self.on_add_wallpapers)
        event_manager.add_callback("wallpapers-deleted", self.on_wallpapers_deleted)
        event_manager.add_callback("select-wallpaper", self.on_wallpaper_select)
        event_manager.add_callback("apply-wallpaper", self.on_wallpaper_apply)
        event_manager.add_callback("apply-download-wallpaper", self.on_download_wallpaper_apply)
        event_manager.add_callback("delete-wallpaper-link", self.__on_delete_wallpaper_link)
        self.theme = None

    def __on_delete_wallpaper_link(self, name, obj, data):
        items = filter(lambda item: item.image_path == data, self.items) 
        if items:                                                               
            self.delete_items(items) 

    def __on_right_click_item(self, widget, item, x, y):                        
        menu_items = [(None, _("Apply Wallpaper"), lambda : item.do_apply_wallpaper())]
        Menu(menu_items, True).show((int(x), int(y)))

    def set_theme(self, theme):    
        self.theme = theme
        self.clear()
        self.add_item.set_theme(self.theme)
        
        self.add_items([self.add_item])
        
        self.add_system_wallpapers(self.theme.get_system_wallpapers())        
        self.add_user_wallpapers(self.theme.get_user_wallpapers())

    def is_editable(self):
        if self.theme.is_system_theme:
            if len(self.theme.get_user_wallpapers()) == 0:
                return False

        return True

    def is_exists(self, image):    
        if self.theme == None:
            return False

        if image in self.theme.get_user_wallpapers():
            return True
        return False
    
    def add_user_wallpapers(self, image_paths, save=False):
        self.add_images(image_paths, readonly=False)
        if save:
            if self.theme == None:
                return
            
            self.theme.add_user_wallpapers(image_paths)        
    
    def add_system_wallpapers(self, image_paths):
        self.add_images(image_paths, readonly=True)

    def add_images(self, images, readonly=False):
        images = list(set(images))
        items = map(lambda image: WallpaperItem(image, readonly, self.theme, background_gsettings), images)
        self.add_items(items, insert_pos=-1)
        
    def on_wallpaper_select(self, name, obj, select_item):    
        image_uris = [ "file://%s" % item.image_path for item in self.items if item.is_tick]
        self.apply_wallpapers(image_uris)

    def is_deletable(self):
        for item in self.items:
            if not item.is_tick:
                return True

        return False

    def delete_wallpaper(self):
        for item in self.items:
            if not item.is_tick:
                self.theme.remove_option("user_wallpaper", item.image_path)

        self.theme.save()
        self.set_theme(self.theme, True)

    def is_randomable(self):
        i = 0

        for item in self.items:
            if item.is_tick:
                i += 1
        if i < 2:
            return False

        return True

    def is_select_all(self):
        for item in self.items:
            if item.__class__.__name__ == "AddItem":
                continue

            if not item.is_tick:
                return False

        return True

    def select_all(self):
        is_select_all = self.is_select_all()

        for item in self.items:
            if is_select_all:
                item.untick()
            else:
                item.tick()
        
        image_uris = [ "file://%s" % item.image_path for item in self.items if item.is_tick]
        self.apply_wallpapers(image_uris)

    def on_download_wallpaper_apply(self, name, obj, image_path):
        image_uris = ["file://%s" % image_path]
        self.apply_wallpapers(image_uris)
        if self.is_randomable():
            self.items[-2].tick()
    
    def on_wallpaper_apply(self, name, obj, apply_item):
        [ item.untick() for item in self.items if item != apply_item]
        image_uris = ["file://%s" % apply_item.image_path]
        self.apply_wallpapers(image_uris)
        
    def apply_wallpapers(self, image_paths):
        image_path_string = ";".join(image_paths)
        background_gsettings.set_string("picture-uris", image_path_string)        
        if self.theme:
            self.theme.save()        
                
    def on_add_wallpapers(self, name, obj, image_paths):    
        filter_images = filter(lambda image: not self.is_exists(image), image_paths)        
        if filter_images:
            self.add_user_wallpapers(filter_images, save=True)
        event_manager.emit("update-theme", self.theme)
            
    def on_wallpapers_deleted(self, name, obj, image_paths):        
        items = filter(lambda item: item.image_path in image_paths, self.items)
        if items:        
            self.delete_items(items)
        
    def get_scrolled_window(self):    
        scrolled_window = ScrolledWindow()
        scrolled_window.add_child(self)
        return scrolled_window
    
    def draw_mask(self, cr, x, y, w, h):
        cr.set_source_rgb(1, 1, 1)
        cr.rectangle(x, y, w, h)
        cr.fill()