Exemplo n.º 1
0
class SelectView(IconView):
    SHOW_ITEM_COUNT = 16

    __gsignals__ = {
        "loaded": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
    }

    def __init__(self,
                 monitor_dir,
                 padding_x=8,
                 padding_y=10,
                 filter_dir=None,
                 add_system=False):
        IconView.__init__(self, padding_x=padding_x, padding_y=padding_y)

        self.system_wallpapper_dir = get_system_wallpaper_dirs()[1]
        self.is_file_added = False
        self.monitor_dir = monitor_dir
        self.filter_dir = filter_dir
        self.library_monitor = LibraryMonitor(monitor_dir)
        self.library_monitor.set_property("monitored", True)
        self.library_monitor.connect("file-added", self.on_library_file_added)
        self.library_monitor.connect("folder-added",
                                     self.on_library_folder_added)
        self.library_monitor.connect("location-removed",
                                     self.on_library_location_removed)
        self.__image_index = 0
        self.__init_monitor_images(add_system)

    def on_library_file_added(self, obj, gfile):
        is_image_type = common.gfile_is_image(gfile)
        if is_image_type:
            image_path = gfile.get_path()
            if not self.is_exists(image_path):
                self.is_file_added = True
                self.add_items([SelectItem(image_path)], 1)

    def on_library_folder_added(self, obj, gfile):
        items = []
        for image_path in common.walk_images(gfile.get_path(),
                                             filter_dir=self.filter_dir):
            if not self.is_exists(image_path):
                items.append(SelectItem(image_path))
        if items:
            self.add_items(items)

    def on_library_location_removed(self, obj, gfile):
        file_path = gfile.get_path()
        items = filter(lambda item: item.image_path.startswith(file_path),
                       self.items)
        if items:
            event_manager.emit("wallpapers-deleted",
                               map(lambda item: item.image_path, items))
            self.delete_items(items)

    def is_exists(self, image_path):
        for item in self.items:
            if image_path == item.image_path:
                return True
        return False

    '''
    divide BIG images count into several small sections
    '''

    @common.threaded
    def __init_monitor_images(self, add_system=False):
        if self.is_file_added:
            return

        items = []
        image_paths = []
        i = 0

        self.set_loading(True)
        for image_path in common.walk_images(self.monitor_dir,
                                             filter_dir=self.filter_dir):
            image_paths.append(image_path)

        if add_system:
            system_deletes = get_system_deletes()
            for image_path in common.walk_images(self.system_wallpapper_dir):
                if not image_path in system_deletes:
                    image_paths.append(image_path)

        while self.__image_index < len(
                image_paths) and i < self.SHOW_ITEM_COUNT:
            items.append(SelectItem(image_paths[self.__image_index]))
            self.__image_index += 1
            i += 1

        if items and items not in self.items:
            self.add_items(items)
        self.set_loading(False)

    @common.threaded
    def __more_monitor_images(self):
        items = []
        i = 0

        self.set_loading(True)
        while self.__image_index < len(
                image_paths) and i < self.SHOW_ITEM_COUNT:
            items.append(SelectItem(image_paths[self.__image_index]))
            self.__image_index += 1
            i += 1

        if items:
            self.add_items(items)
        self.set_loading(False)

    def add_images(self, images):
        items = map(lambda image: SelectItem(image), images)
        self.add_items(items)

    def get_scrolled_window(self):
        scrolled_window = ScrolledWindow()
        scrolled_window.connect("vscrollbar-state-changed",
                                self.__on_vscrollbar_state_changed)
        scrolled_window.add_child(self)
        return scrolled_window

    def __on_vscrollbar_state_changed(self, widget, argv):
        if argv != "bottom":
            return

        self.__init_monitor_images()

    def draw_mask(self, cr, x, y, w, h):
        cr.set_source_rgb(1, 1, 1)
        cr.rectangle(x, y, w, h)
        cr.fill()

    def add_favorites(self):
        favorites_dir = get_favorite_dir()
        for item in self.items:
            if item.is_tick:
                new_path = os.path.join(favorites_dir,
                                        os.path.split(item.image_path)[1])
                shutil.copy2(item.image_path, new_path)

    def delete(self):
        for item in self.items:
            if item.is_tick:
                if item.image_path.startswith(self.system_wallpapper_dir):
                    self.delete_items([item])
                    add_system_deletes([item.image_path])
                else:
                    os.remove(item.image_path)
                event_manager.emit("delete-downloaded-wallpaper", item)

        self.queue_draw()

    def set_multi_wallpapper(self):
        uris = []
        for item in self.items:
            if item.is_tick:
                url = "file://%s" % item.image_path
                uris.append(url)
        image_path_string = ";".join(uris)
        background_gsettings.set_string("picture-uris", image_path_string)
        background_gsettings.set_int("background-duration", 600)

    def get_select_number(self):
        n = 0
        for item in self.items:
            if item.is_tick:
                n += 1
        return n

    def is_select_all(self):
        for item in self.items:
            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()

    def emit_add_wallpapers(self):
        tick_items = filter(lambda item: item.is_tick, self.items)
        if tick_items:
            image_paths = map(lambda item: item.image_path, tick_items)
            event_manager.emit("add-wallpapers", image_paths)
class SessionView(gtk.VBox):

    def __init__(self):
        gtk.VBox.__init__(self)
        self.open_dialog = False
        self.tmp_editing_session = None
        
        # UI style
        style.draw_background_color(self)
        self.tree = TreeView([],enable_drag_drop=False,
                             enable_hover=True,
                             enable_multiple_select=False,
                             )
        self.tree.set_expand_column(3)
        self.tree.set_column_titles((_("Active"), _("Application"), _("Description"), _("Exec")),)

        self.tree.set_size_request(800, -1)
        self.tree.connect("right-press-items", self.right_press_item)

        self.tree.draw_mask = self.draw_mask
        self.tree.add_items(self.get_list())
        align = gtk.Alignment(0, 0, 0, 1)
        align.set_padding(15, 0, 20, 20)
        align.add(self.tree)
        align.connect("expose-event", self.expose_line)


        add_button = Button(_("New"))
        self.delete_button = Button(_("Delete"))
        add_button.connect("clicked", self.add_autostart)
        self.delete_button.connect("clicked", self.delete_autostart)
        self.delete_button.set_sensitive(False)
        
        foot_box = FootBox(adjustment=15)
        foot_box.set_buttons([add_button, self.delete_button])
        self.pack_start(align, True, True)
        
        self.pack_end(foot_box, False, False)
        #self.pack_end(self.new_box, False, False)

        self.show_all()

        self._init_monitor()

    def disable_delete_button(self, value):
        self.delete_button.set_sensitive(not value)
    def _init_monitor(self):
        self.library_monitor = LibraryMonitor(get_user_config_dir())
        self.library_monitor.set_property("monitored", True)
        self.library_monitor.connect("file-added", self.refresh_list)
        self.library_monitor.connect("location-removed", self.refresh_list)

    def right_press_item(self, widget,  x_root, y_root, current_item, select_items):
        self.tmp_editing_session = current_item.item
        for item in select_items:
            item.unselect()
        if current_item != None:
            current_item.select()
            if self.open_dialog == False:
                dialog = NewSessionDialog(confirm_callback = self.edit_done, cancel_callback = self.cancel_callback)
                dialog.name_entry.set_text(current_item.item.name)
                dialog.exec_entry.set_text(current_item.item.exec_)
                dialog.desc_entry.set_text(current_item.item.comment)
                dialog.place_center()
                dialog.show_all()
                self.open_dialog = True

    def create_session_item(self, dialog):
        name = dialog.name_entry.get_text()
        exec_ = dialog.exec_entry.get_text()
        comment = dialog.desc_entry.get_text()
        session_manager.add(name, exec_, comment)
        self.open_dialog = False

    def expose_line(self, widget, event):
        cr = widget.window.cairo_create()
        rect = widget.allocation
        style.draw_out_line(cr, rect, exclude=["left", "right", "top"])
    
    def sort_method(self):
        pass

    def add_autostart(self, widget):
        if self.open_dialog == False:
            dialog = NewSessionDialog(confirm_callback= self.create_session_item, cancel_callback = self.cancel_callback)
            dialog.show_all()
            self.open_dialog = True

    def delete_autostart(self, widget):
        items = map(lambda row: self.tree.visible_items[row], self.tree.select_rows)
        item = items[0].item
        item.delete()
        self.tree.delete_select_items()
        if self.tree.visible_items == []:
            self.tree.add_items([NothingItem()])

    def edit_done(self, dialog):
        self.tmp_editing_session.set_name(dialog.name_entry.get_text())
        self.tmp_editing_session.set_exec(dialog.exec_entry.get_text())
        self.tmp_editing_session.set_comment(dialog.desc_entry.get_text())
        self.tmp_editing_session.save()
        self.tmp_editing_session = None
        items = map(lambda row: self.tree.visible_items[row], self.tree.select_rows)
        self.tree.redraw_request(items, True)
        self.open_dialog = False

    def cancel_callback(self):
        self.tmp_editing_session = None
        self.open_dialog = False

    def pack(self, parent, widget_list, expand=False, fill=False):
        for w in widget_list:
            parent.pack_start(w, expand, fill)

    def draw_mask(self, cr, x, y, w, h):
        cr.set_source_rgb(1, 1, 1)
        cr.rectangle(x, y, w, h)
        cr.fill()

    def get_list(self):
        usr_list = session_manager.list_autostart_items()
        if usr_list:
            return map(lambda w: SessionItem(self, w), usr_list)
        else:
            return [NothingItem()]

    def refresh_list(self, widget, gfile):
        self.tree.clear()
        self.tree.add_items(self.get_list())
        self.tree.show()
class SessionView(gtk.VBox):
    def __init__(self):
        gtk.VBox.__init__(self)
        self.open_dialog = False
        self.tmp_editing_session = None

        # UI style
        style.draw_background_color(self)
        self.tree = TreeView(
            [],
            enable_drag_drop=False,
            enable_hover=True,
            enable_multiple_select=False,
        )
        self.tree.set_expand_column(3)
        self.tree.set_column_titles(
            (_("Active"), _("Application"), _("Description"), _("Exec")), )

        self.tree.set_size_request(800, -1)
        self.tree.connect("right-press-items", self.right_press_item)

        self.tree.draw_mask = self.draw_mask
        self.tree.add_items(self.get_list())
        align = gtk.Alignment(0, 0, 0, 1)
        align.set_padding(15, 0, 20, 20)
        align.add(self.tree)
        align.connect("expose-event", self.expose_line)

        add_button = Button(_("New"))
        self.delete_button = Button(_("Delete"))
        add_button.connect("clicked", self.add_autostart)
        self.delete_button.connect("clicked", self.delete_autostart)
        self.delete_button.set_sensitive(False)

        foot_box = FootBox(adjustment=15)
        foot_box.set_buttons([add_button, self.delete_button])
        self.pack_start(align, True, True)

        self.pack_end(foot_box, False, False)
        #self.pack_end(self.new_box, False, False)

        self.show_all()

        self._init_monitor()

    def disable_delete_button(self, value):
        self.delete_button.set_sensitive(not value)

    def _init_monitor(self):
        self.library_monitor = LibraryMonitor(get_user_config_dir())
        self.library_monitor.set_property("monitored", True)
        self.library_monitor.connect("file-added", self.refresh_list)
        self.library_monitor.connect("location-removed", self.refresh_list)

    def right_press_item(self, widget, x_root, y_root, current_item,
                         select_items):
        self.tmp_editing_session = current_item.item
        for item in select_items:
            item.unselect()
        if current_item != None:
            current_item.select()
            if self.open_dialog == False:
                dialog = NewSessionDialog(confirm_callback=self.edit_done,
                                          cancel_callback=self.cancel_callback)
                dialog.name_entry.set_text(current_item.item.name)
                dialog.exec_entry.set_text(current_item.item.exec_)
                dialog.desc_entry.set_text(current_item.item.comment)
                dialog.place_center()
                dialog.show_all()
                self.open_dialog = True

    def create_session_item(self, dialog):
        name = dialog.name_entry.get_text()
        exec_ = dialog.exec_entry.get_text()
        comment = dialog.desc_entry.get_text()
        session_manager.add(name, exec_, comment)
        self.open_dialog = False

    def expose_line(self, widget, event):
        cr = widget.window.cairo_create()
        rect = widget.allocation
        style.draw_out_line(cr, rect, exclude=["left", "right", "top"])

    def sort_method(self):
        pass

    def add_autostart(self, widget):
        if self.open_dialog == False:
            dialog = NewSessionDialog(
                confirm_callback=self.create_session_item,
                cancel_callback=self.cancel_callback)
            dialog.show_all()
            self.open_dialog = True

    def delete_autostart(self, widget):
        items = map(lambda row: self.tree.visible_items[row],
                    self.tree.select_rows)
        item = items[0].item
        item.delete()
        self.tree.delete_select_items()
        if self.tree.visible_items == []:
            self.tree.add_items([NothingItem()])

    def edit_done(self, dialog):
        self.tmp_editing_session.set_name(dialog.name_entry.get_text())
        self.tmp_editing_session.set_exec(dialog.exec_entry.get_text())
        self.tmp_editing_session.set_comment(dialog.desc_entry.get_text())
        self.tmp_editing_session.save()
        self.tmp_editing_session = None
        items = map(lambda row: self.tree.visible_items[row],
                    self.tree.select_rows)
        self.tree.redraw_request(items, True)
        self.open_dialog = False

    def cancel_callback(self):
        self.tmp_editing_session = None
        self.open_dialog = False

    def pack(self, parent, widget_list, expand=False, fill=False):
        for w in widget_list:
            parent.pack_start(w, expand, fill)

    def draw_mask(self, cr, x, y, w, h):
        cr.set_source_rgb(1, 1, 1)
        cr.rectangle(x, y, w, h)
        cr.fill()

    def get_list(self):
        usr_list = session_manager.list_autostart_items()
        if usr_list:
            return map(lambda w: SessionItem(self, w), usr_list)
        else:
            return [NothingItem()]

    def refresh_list(self, widget, gfile):
        self.tree.clear()
        self.tree.add_items(self.get_list())
        self.tree.show()
class SelectView(IconView):
    SHOW_ITEM_COUNT = 16
    
    __gsignals__ = {                                                            
        "loaded" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),}

    def __init__(self, monitor_dir, padding_x=8, padding_y=10, filter_dir=None, add_system=False):
        IconView.__init__(self, padding_x=padding_x, padding_y=padding_y)
        
        self.system_wallpapper_dir = get_system_wallpaper_dirs()[1]
        self.is_file_added = False
        self.monitor_dir = monitor_dir
        self.filter_dir = filter_dir
        self.library_monitor = LibraryMonitor(monitor_dir)
        self.library_monitor.set_property("monitored", True)
        self.library_monitor.connect("file-added", self.on_library_file_added)
        self.library_monitor.connect("folder-added", self.on_library_folder_added)
        self.library_monitor.connect("location-removed", self.on_library_location_removed)
        self.__image_index = 0
        self.__init_monitor_images(add_system)

    def on_library_file_added(self, obj, gfile):    
        is_image_type = common.gfile_is_image(gfile)
        if is_image_type:
            image_path = gfile.get_path()
            if not self.is_exists(image_path):
                self.is_file_added = True
                self.add_items([SelectItem(image_path)], 1)
                
    def on_library_folder_added(self, obj, gfile):
        items = []
        for image_path in common.walk_images(gfile.get_path(), filter_dir = self.filter_dir):
            if not self.is_exists(image_path):
                items.append(SelectItem(image_path))
        if items:        
            self.add_items(items)
            
    def on_library_location_removed(self, obj, gfile):        
        file_path = gfile.get_path()
        items = filter(lambda item: item.image_path.startswith(file_path), self.items)
        if items:
            event_manager.emit("wallpapers-deleted", map(lambda item: item.image_path, items))            
            self.delete_items(items)
            
    def is_exists(self, image_path):
        for item in self.items:
            if image_path == item.image_path:
                return True
        return False    
    
    '''
    divide BIG images count into several small sections
    '''
    @common.threaded
    def __init_monitor_images(self, add_system=False):
        if self.is_file_added:
            return
        
        items = []
        image_paths = []
        i = 0
        
        self.set_loading(True)
        for image_path in common.walk_images(self.monitor_dir, filter_dir = self.filter_dir):
            image_paths.append(image_path)

        if add_system:
            system_deletes = get_system_deletes()
            for image_path in common.walk_images(self.system_wallpapper_dir):
                if not image_path in system_deletes:
                    image_paths.append(image_path)
        
        while self.__image_index < len(image_paths) and i < self.SHOW_ITEM_COUNT:
            items.append(SelectItem(image_paths[self.__image_index]))
            self.__image_index += 1
            i += 1

        if items and items not in self.items:    
            self.add_items(items)
        self.set_loading(False)

    @common.threaded
    def __more_monitor_images(self):
        items = []
        i = 0
        
        self.set_loading(True)
        while self.__image_index < len(image_paths) and i < self.SHOW_ITEM_COUNT:
            items.append(SelectItem(image_paths[self.__image_index]))              
            self.__image_index += 1
            i += 1
                                                                                   
        if items:                                                                  
            self.add_items(items)
        self.set_loading(False)

    def add_images(self, images):        
        items = map(lambda image: SelectItem(image), images)
        self.add_items(items)
        
    def get_scrolled_window(self):
        scrolled_window = ScrolledWindow()
        scrolled_window.connect("vscrollbar-state-changed", self.__on_vscrollbar_state_changed)
        scrolled_window.add_child(self)
        return scrolled_window

    def __on_vscrollbar_state_changed(self, widget, argv):                      
        if argv != "bottom":
            return

        self.__init_monitor_images()

    def draw_mask(self, cr, x, y, w, h):
        cr.set_source_rgb(1, 1, 1)
        cr.rectangle(x, y, w, h)
        cr.fill()

    def add_favorites(self):
        favorites_dir = get_favorite_dir()
        for item in self.items:
            if item.is_tick:
                new_path = os.path.join(favorites_dir, os.path.split(item.image_path)[1])
                shutil.copy2(item.image_path, new_path)

    def delete(self):
        for item in self.items:
            if item.is_tick:
                if item.image_path.startswith(self.system_wallpapper_dir):
                    self.delete_items([item])
                    add_system_deletes([item.image_path])
                else:
                    os.remove(item.image_path)
                event_manager.emit("delete-downloaded-wallpaper", item)

        self.queue_draw()

    def set_multi_wallpapper(self):
        uris = []
        for item in self.items:
            if item.is_tick:
                url = "file://%s" % item.image_path
                uris.append(url)
        image_path_string = ";".join(uris)
        background_gsettings.set_string("picture-uris", image_path_string)        
        background_gsettings.set_int("background-duration", 600)

    def get_select_number(self):
        n = 0
        for item in self.items:
            if item.is_tick:
                n += 1
        return n

    def is_select_all(self):
        for item in self.items:
            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()

    def emit_add_wallpapers(self):    
        tick_items = filter(lambda item : item.is_tick, self.items)
        if tick_items:
            image_paths = map(lambda item: item.image_path, tick_items)
            event_manager.emit("add-wallpapers", image_paths)