def create_new(self):
        group_id = self.txtId.text()
        group_alias = self.txtAlias.text()
        group_icon = self.txtIcon.get_path()

        if not self.validate(group_id, group_alias, group_icon):
            return False

        if not self.check_existing_id(group_id):
            return False

        # set paths
        dir_path = path.join(extra_sources.USER_DIR_PATH, extra_sources.GROUPS_DIR_NAME, group_id)

        if path.exists(dir_path):
            salt = 0
            while path.exists(dir_path + str(salt)):
                salt += 1
            dir_path += str(salt)

        ini_path = path.join(dir_path, 'metadata.ini')

        ico_file_name = path.basename(group_icon)
        ico_path = path.join(dir_path, ico_file_name)

        # create dir
        os.mkdir(dir_path)

        # copy icon
        shutil.copy(group_icon, ico_path)

        # write config
        config = FixedConfigParser()

        config.add_section('general')
        config.add_section('ui')
        config.set('general', 'id', group_id)
        config.set('ui', 'alias', group_alias)
        config.set('ui', 'icon', ico_file_name)

        with codecs.open(ini_path, 'w', 'utf-8') as configfile:
            config.write(configfile)

        return True
    def save_existing(self):
        group_id = self.txtId.text()
        group_alias = self.txtAlias.text()
        group_icon = self.txtIcon.get_path()

        if not self.validate(group_id, group_alias, group_icon):
            return False

        if group_id != self.group_info.id and not self.check_existing_id(group_id):
            return False

        if group_id == self.group_info.id and \
           group_alias == self.group_info.alias and \
           is_same(group_icon, self.group_info.icon):
            return True

        # replace icon if need
        if not is_same(group_icon, self.group_info.icon):
            os.remove(self.group_info.icon)

            dir_path = os.path.dirname(self.group_info.file_path)

            ico_file_name = path.basename(group_icon)
            ico_path = path.join(dir_path, ico_file_name)

            shutil.copy(group_icon, ico_path)

        # write config
        config = FixedConfigParser()

        config.add_section('general')
        config.add_section('ui')
        config.set('general', 'id', group_id)
        config.set('ui', 'alias', group_alias)
        config.set('ui', 'icon', path.basename(group_icon))

        with codecs.open(self.group_info.file_path, 'w', 'utf-8') as configfile:
            config.write(configfile)

        return True
    def write_to_ini(cls, ds_info, ini_file_path):
        _to_utf = lambda x: x.encode("utf-8") if isinstance(x, unicode) else x
        config = FixedConfigParser()

        config.add_section("general")
        config.add_section("ui")
        config.add_section("license")
        config.add_section(ds_info.type.lower())

        # Required
        config.set("general", "id", ds_info.id)
        config.set("general", "type", ds_info.type)

        config.set("ui", "group", ds_info.group)
        config.set("ui", "alias", ds_info.alias)
        config.set("ui", "icon", ds_info.icon)

        # Lic & Terms
        config.set("license", "name", ds_info.lic_name)
        config.set("license", "link", ds_info.lic_link)
        config.set("license", "copyright_text", ds_info.copyright_text)
        config.set("license", "copyright_link", ds_info.copyright_link)
        config.set("license", "terms_of_use", ds_info.terms_of_use)

        if ds_info.type == KNOWN_DRIVERS.TMS:
            config.set("tms", "url", ds_info.tms_url)
            config.set("tms", "zmin", ds_info.tms_zmin)
            config.set("tms", "zmax", ds_info.tms_zmax)
            config.set("tms", "y_origin_top", ds_info.tms_y_origin_top)
            if ds_info.tms_epsg_crs_id:
                config.set("tms", "epsg_crs_id", ds_info.tms_epsg_crs_id)
            if ds_info.tms_postgis_crs_id:
                config.set("tms", "postgis_crs_id", ds_info.tms_postgis_crs_id)
            if ds_info.tms_custom_proj:
                config.set("tms", "custom_proj", ds_info.tms_custom_proj)

        if ds_info.type == KNOWN_DRIVERS.WMS:
            config.set("wms", "url", ds_info.wms_url)
            config.set("wms", "params", ds_info.wms_params)
            config.set("wms", "layers", ds_info.wms_layers)
            config.set("wms", "turn_over", ds_info.wms_turn_over)

        if ds_info.type == KNOWN_DRIVERS.GDAL:
            config.set("gdal", "source_file", os.path.basename(ds_info.gdal_source_file))

        if ds_info.type == KNOWN_DRIVERS.WFS:
            config.set("wfs", "url", ds_info.wfs_url)
            # config.set('wfs', 'layers', ds_info.wfs_layers)

        with codecs.open(ini_file_path, "w", "utf-8") as configfile:
            config.write(configfile)
    def write_to_ini(cls, ds_info, ini_file_path):
        _to_utf = lambda x: x.encode('utf-8') if isinstance(x, unicode) else x
        config = FixedConfigParser()

        config.add_section('general')
        config.add_section('ui')
        config.add_section('license')
        config.add_section(ds_info.type.lower())

        # Required
        config.set('general', 'id', ds_info.id)
        config.set('general', 'type', ds_info.type)

        config.set('ui', 'group', ds_info.group)
        config.set('ui', 'alias', ds_info.alias)
        config.set('ui', 'icon', ds_info.icon)

        # Lic & Terms
        config.set('license', 'name', ds_info.lic_name)
        config.set('license', 'link', ds_info.lic_link)
        config.set('license', 'copyright_text', ds_info.copyright_text)
        config.set('license', 'copyright_link', ds_info.copyright_link)
        config.set('license', 'terms_of_use', ds_info.terms_of_use)

        if ds_info.type == KNOWN_DRIVERS.TMS:
            config.set('tms', 'url', ds_info.tms_url)
            config.set('tms', 'zmin', ds_info.tms_zmin)
            config.set('tms', 'zmax', ds_info.tms_zmax)
            config.set('tms', 'y_origin_top', ds_info.tms_y_origin_top)
            if ds_info.tms_epsg_crs_id:
                config.set('tms', 'epsg_crs_id', ds_info.tms_epsg_crs_id)
            if ds_info.tms_postgis_crs_id:
                config.set('tms', 'postgis_crs_id', ds_info.tms_postgis_crs_id)
            if ds_info.tms_custom_proj:
                config.set('tms', 'custom_proj', ds_info.tms_custom_proj)

        if ds_info.type == KNOWN_DRIVERS.WMS:
            config.set('wms', 'url', ds_info.wms_url)
            config.set('wms', 'params', ds_info.wms_params)
            config.set('wms', 'layers', ds_info.wms_layers)
            config.set('wms', 'turn_over', ds_info.wms_turn_over)

        if ds_info.type == KNOWN_DRIVERS.GDAL:
            config.set('gdal', 'source_file',
                       os.path.basename(ds_info.gdal_source_file))

        if ds_info.type == KNOWN_DRIVERS.WFS:
            config.set('wfs', 'url', ds_info.wfs_url)
            # config.set('wfs', 'layers', ds_info.wfs_layers)

        with codecs.open(ini_file_path, 'w', 'utf-8') as configfile:
            config.write(configfile)
    def write_to_ini(cls, ds_info, ini_file_path):
        _to_utf = lambda x: x.encode('utf-8') if isinstance(x, unicode) else x
        config = FixedConfigParser()

        config.add_section('general')
        config.add_section('ui')
        config.add_section('license')
        config.add_section(ds_info.type.lower())

       # Required
        config.set('general', 'id', ds_info.id)
        config.set('general', 'type', ds_info.type)

        config.set('ui', 'group', ds_info.group)
        config.set('ui', 'alias', ds_info.alias)
        config.set('ui', 'icon', ds_info.icon)

        # Lic & Terms
        config.set('license', 'name', ds_info.lic_name)
        config.set('license', 'link', ds_info.lic_link)
        config.set('license', 'copyright_text', ds_info.copyright_text)
        config.set('license', 'copyright_link', ds_info.copyright_link)
        config.set('license', 'terms_of_use', ds_info.terms_of_use)

        if ds_info.type == KNOWN_DRIVERS.TMS:
            config.set('tms', 'url', ds_info.tms_url)
            config.set('tms', 'zmin', ds_info.tms_zmin)
            config.set('tms', 'zmax', ds_info.tms_zmax)
            config.set('tms', 'y_origin_top', ds_info.tms_y_origin_top)
            if ds_info.tms_epsg_crs_id:
                config.set('tms', 'epsg_crs_id', ds_info.tms_epsg_crs_id)
            if ds_info.tms_postgis_crs_id:
                config.set('tms', 'postgis_crs_id', ds_info.tms_postgis_crs_id)
            if ds_info.tms_custom_proj:
                config.set('tms', 'custom_proj', ds_info.tms_custom_proj)

        if ds_info.type == KNOWN_DRIVERS.WMS:
            config.set('wms', 'url', ds_info.wms_url)
            config.set('wms', 'params', ds_info.wms_params)
            config.set('wms', 'layers', ds_info.wms_layers)
            config.set('wms', 'turn_over', ds_info.wms_turn_over)

        if ds_info.type == KNOWN_DRIVERS.GDAL:
            config.set('gdal', 'source_file', os.path.basename(ds_info.gdal_source_file))


        with codecs.open(ini_file_path, 'w', 'utf-8') as configfile:
            config.write(configfile)