Ejemplo n.º 1
0
class SkinConfig(gobject.GObject):
    '''
    SkinConfig class.

    @undocumented: update_image_size
    @undocumented: get_skin_file_path
    @undocumented: is_skin_exist
    @undocumented: get_default_skin
    @undocumented: get_skin_dir
    @undocumented: save_skin_name
    @undocumented: reload_skin
    @undocumented: load_skin
    @undocumented: save_skin
    @undocumented: change_theme
    @undocumented: apply_skin
    @undocumented: add_theme
    @undocumented: remove_theme
    @undocumented: wrap_skin_window
    @undocumented: add_skin_window
    @undocumented: remove_skin_window
    @undocumented: reset
    @undocumented: auto_resize
    @undocumented: vertical_mirror_background
    @undocumented: horizontal_mirror_background
    @undocumented: render_background
    @undocumented: export_skin
    @undocumented: load_skin_from_image
    @undocumented: load_skin_from_package
    '''

    __gsignals__ = {
        "theme-changed" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)),
    }

    def __init__(self):
        '''
        Initialize SkinConfig class.
        '''
        # Init.
        gobject.GObject.__init__(self)
        self.cache_pixbuf = CachePixbuf()

        self.theme_list = []
        self.window_list = []

    def set_application_window_size(self, app_window_width, app_window_height):
        '''
        Set application window with given size.

        @param app_window_width: Application window width.
        @param app_window_height: Application window height.
        '''
        self.app_window_width = app_window_width
        self.app_window_height = app_window_height

    def update_image_size(self, x, y, scale_x, scale_y):
        '''
        Internal function to update image size.
        '''
        self.x = x
        self.y = y
        self.scale_x = scale_x
        self.scale_y = scale_y

    def get_skin_file_path(self, filename):
        '''
        Internal function to get skin file path.
        '''
        skin_file_dir = None
        for skin_dir in [self.system_skin_dir, self.user_skin_dir]:
            if os.path.exists(skin_dir):
                if self.skin_name in os.listdir(os.path.expanduser(skin_dir)):
                    skin_file_dir = skin_dir
                    break

        if skin_file_dir:
            return os.path.join(skin_file_dir, self.skin_name, filename)
        else:
            return None

    def is_skin_exist(self, skin_name, system_skin_dir, user_skin_dir):
        '''
        Internal function to is skin exist in skin directories.
        '''
        for skin_dir in [system_skin_dir, user_skin_dir]:
            if os.path.exists(skin_dir):
                if skin_name in os.listdir(os.path.expanduser(skin_dir)):
                    return True

        return False

    def get_default_skin(self, system_skin_dir, user_skin_dir):
        '''
        Internal function to get default skin.
        '''
        for skin_dir in [system_skin_dir, user_skin_dir]:
            if os.path.exists(skin_dir):
                skin_list = os.listdir(os.path.expanduser(skin_dir))
                if len(skin_list) > 0:
                    return skin_list[0]

        return None

    def get_skin_dir(self):
        '''
        Internal function to get skin dir.
        '''
        for skin_dir in [self.system_skin_dir, self.user_skin_dir]:
            if os.path.exists(skin_dir):
                if self.skin_name in os.listdir(os.path.expanduser(skin_dir)):
                    return os.path.join(skin_dir, self.skin_name)

        return None

    def init_skin(self,
                  skin_name,
                  system_skin_dir,
                  user_skin_dir,
                  skin_config_file,
                  app_given_id,
                  app_given_version):
        '''
        Init skin.

        @param skin_name: Skin name.
        @param system_skin_dir: Default skin directory.
        @param user_skin_dir: User's skin directory, generic use ~/.config/project-name/skin
        @param skin_config_file: Skin's config filepath, generic use ~/.config/project-name/skin_config.ini
        @param app_given_id: Project name.
        @param app_given_version: Project version.
        '''
        self.skin_config_file = skin_config_file
        if os.path.exists(skin_config_file):
            # Read skin name from config file.
            skin_config = Config(skin_config_file)
            skin_config.load()

            # Load skin.
            init_skin_name = skin_config.get("skin", "skin_name")
        else:
            # Create skin config if it not exists.
            touch_file(self.skin_config_file)

            init_skin_name = skin_name

        if self.is_skin_exist(init_skin_name, system_skin_dir, user_skin_dir):
            self.load_skin(init_skin_name, system_skin_dir, user_skin_dir)
        else:
            # Try load default skin if user's select skin not exists.
            default_skin_name = self.get_default_skin(system_skin_dir, user_skin_dir)
            assert(default_skin_name != None)
            self.load_skin(default_skin_name, system_skin_dir, user_skin_dir)

        self.app_given_id = app_given_id
        self.app_given_version = app_given_version

    def save_skin_name(self):
        '''
        Internal function to save skin name.
        '''
        skin_config = Config(self.skin_config_file)
        skin_config.load()
        if skin_config.get("skin", "skin_name") != self.skin_name:
            skin_config.set("skin", "skin_name", self.skin_name)
            skin_config.write(self.skin_config_file)

    def reload_skin(self, skin_name=None):
        '''
        Internal function to reload skin.
        '''
        if skin_name:
            return self.load_skin(skin_name)
        else:
            return self.load_skin(self.skin_name)

    def load_skin(self, skin_name, system_skin_dir=None, user_skin_dir=None):
        '''
        Internal function to Load skin.

        @return: Return True if load finish, otherwise return False.
        '''
        try:
            # Save skin dir.
            self.skin_name = skin_name

            if system_skin_dir:
                self.system_skin_dir = system_skin_dir
                create_directory(self.system_skin_dir)

            if user_skin_dir:
                self.user_skin_dir = user_skin_dir
                create_directory(self.user_skin_dir)

            self.skin_dir = self.get_skin_dir()

            # Load config file.
            self.config = Config(self.get_skin_file_path("config.ini"))
            self.config.load()

            # Get theme config.
            self.theme_name = self.config.get("theme", "theme_name")

            # Get application config.
            self.app_id = self.config.get("application", "app_id")
            self.app_version = self.config.getfloat("application", "app_version")

            # Get background config.
            self.image = self.config.get("background", "image")
            self.x = self.config.getfloat("background", "x")
            self.y = self.config.getfloat("background", "y")
            self.scale_x = self.config.getfloat("background", "scale_x")
            self.scale_y = self.config.getfloat("background", "scale_y")
            self.dominant_color = self.config.get("background", "dominant_color")

            # Get action config.
            self.deletable = self.config.getboolean("action", "deletable")
            self.editable = self.config.getboolean("action", "editable")
            self.vertical_mirror = self.config.getboolean("action", "vertical_mirror")
            self.horizontal_mirror = self.config.getboolean("action", "horizontal_mirror")

            # Generate background pixbuf.
            self.background_pixbuf = gtk.gdk.pixbuf_new_from_file(self.get_skin_file_path(self.image))

            # Save skin name.
            self.save_skin_name()

            return True
        except Exception, e:
            print "function load_skin got error: %s" % (e)
            traceback.print_exc(file=sys.stdout)

            return False