rimmed pushed a commit to branch master. http://git.enlightenment.org/tools/eflete.git/commit/?id=a6ca88e80ede26157af23d50ab4b5b961e0de913
commit a6ca88e80ede26157af23d50ab4b5b961e0de913 Author: Andrii Kroitor <an.kroi...@samsung.com> Date: Mon Oct 19 17:06:57 2015 +0300 validator: add resource name validator --- src/bin/Makefile.am | 1 + src/bin/common/validator.c | 113 +++++++++++++++++++++++++++++++++++++++++++++ src/bin/common/validator.h | 44 ++++++++++++++++++ 3 files changed, 158 insertions(+) diff --git a/src/bin/Makefile.am b/src/bin/Makefile.am index 0d5c4bf..281e744 100644 --- a/src/bin/Makefile.am +++ b/src/bin/Makefile.am @@ -8,6 +8,7 @@ noinst_LIBRARIES = libete.a libete_a_SOURCES = \ ../../src/bin/eflete.c \ ../../src/bin/common/string_common.c \ +../../src/bin/common/validator.c \ ../../src/bin/project_manager/group_manager.c \ ../../src/bin/project_manager/widget_manager.c \ ../../src/bin/project_manager/project_manager.c \ diff --git a/src/bin/common/validator.c b/src/bin/common/validator.c new file mode 100644 index 0000000..1044cca --- /dev/null +++ b/src/bin/common/validator.c @@ -0,0 +1,113 @@ +/* + * Edje Theme Editor + * Copyright (C) 2013-2015 Samsung Electronics. + * + * This file is part of Edje Theme Editor. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; If not, see www.gnu.org/licenses/lgpl.html. + */ +#include "validator.h" +#include <regex.h> + +struct _Resource_Name_Validator +{ + Eina_Stringshare *signal; + int status; + regex_t regex; + Eina_List **list; + Eina_Bool sorted; + Resource *res; +}; + +void +resource_name_validator_list_set(Resource_Name_Validator *validator, Eina_List **list, Eina_Bool sorted) +{ + assert(validator != NULL); + assert(list != NULL); + + validator->list = list; + validator->sorted = sorted; +} + +void +resource_name_validator_resource_set(Resource_Name_Validator *validator, Resource *resource) +{ + assert(validator != NULL); + + validator->res = resource; +} + +Resource_Name_Validator * +resource_name_validator_new(const char *pattern, const char *sig) +{ + Resource_Name_Validator *validator; + + assert(pattern != NULL); + + validator = calloc(1, sizeof(Resource_Name_Validator)); + validator->signal = eina_stringshare_add(sig ? sig : "default"); + validator->status = regcomp(&validator->regex, pattern, REG_EXTENDED | REG_NOSUB) ? ELM_REG_BADPAT : ELM_REG_NOERROR; + + return validator; +} + +void +resource_name_validator_free(Resource_Name_Validator *validator) +{ + assert(validator != NULL); + + eina_stringshare_del(validator->signal); + regfree(&validator->regex); + free(validator); +} + +Elm_Regexp_Status +resource_name_validator_status_get(Resource_Name_Validator *validator) +{ + assert(validator != NULL); + + return validator->status; +} + +Eina_Bool +resource_name_validator_helper(void *data, + Eo *obj EINA_UNUSED, + const Eo_Event_Description *desc EINA_UNUSED, + void *event_info) +{ + Resource *res; + Elm_Validate_Content *vc = event_info; + Resource_Name_Validator *validator = (Resource_Name_Validator *)data; + + assert(validator != NULL); + assert(validator->list != NULL); + + validator->status = regexec(&validator->regex, vc->text, (size_t)0, NULL, 0) ? ELM_REG_NOMATCH : ELM_REG_NOERROR; + if (validator->status == ELM_REG_NOERROR) /* new name match regex */ + { + /* check if resource with this name already exists in list */ + if (validator->sorted) + res = pm_resource_get(*validator->list, vc->text); + else + res = pm_resource_unsorted_get(*validator->list, vc->text); + + if (!res) /* name is free */ + validator->status = ELM_REG_NOERROR; + else if ((validator->res != NULL) && (validator->res == res)) /* name is taken by the same resource (it is not changed) */ + validator->status = ELM_REG_NOERROR; + else /* name is taken by another resource */ + validator->status = ELM_REG_NOMATCH; + } + vc->signal = validator->signal; + return validator->status ? EO_CALLBACK_STOP : EO_CALLBACK_CONTINUE; +} diff --git a/src/bin/common/validator.h b/src/bin/common/validator.h new file mode 100644 index 0000000..57eb2e9 --- /dev/null +++ b/src/bin/common/validator.h @@ -0,0 +1,44 @@ +/* + * Edje Theme Editor + * Copyright (C) 2013-2015 Samsung Electronics. + * + * This file is part of Edje Theme Editor. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; If not, see www.gnu.org/licenses/lgpl.html. + */ + +#ifndef VALIDATOR_H +#define VALIDATOR_H + +#include <Elementary.h> +#include "project_manager.h" + +typedef struct _Resource_Name_Validator Resource_Name_Validator; + +void +resource_name_validator_list_set(Resource_Name_Validator *validator, Eina_List **list, Eina_Bool sorted); +void +resource_name_validator_resource_set(Resource_Name_Validator *validator, Resource *resource); +Resource_Name_Validator * +resource_name_validator_new(const char *pattern, const char *sig); +void +resource_name_validator_free(Resource_Name_Validator *validator); +Elm_Regexp_Status +resource_name_validator_status_get(Resource_Name_Validator *validator); +Eina_Bool +resource_name_validator_helper(void *data, + Eo *obj EINA_UNUSED, + const Eo_Event_Description *desc EINA_UNUSED, + void *event_info); + +#endif /* VALIDATOR_H */ --