From: Dirk-Jan C. Binnema Date: Fri, 3 Aug 2007 09:47:02 +0000 (+0000) Subject: * add a function to check for valid folder name; X-Git-Tag: git_migration_finished~2641 X-Git-Url: http://git.maemo.org/git/?p=modest;a=commitdiff_plain;h=048b382238390d0468241b324aecd0c668c19587;ds=inline * add a function to check for valid folder name; we do a comprehensive test, also disallowing names which are not valid on Windows filesystems (like AUX, or ':'); thus, it should also work on MMC, and also make sure that user-created local folders can be moved there. pmo-trunk-r2919 --- diff --git a/src/modest-text-utils.c b/src/modest-text-utils.c index 3c63385..da5859d 100644 --- a/src/modest-text-utils.c +++ b/src/modest-text-utils.c @@ -28,8 +28,14 @@ */ + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif /*_GNU_SOURCE*/ +#include /* for strcasestr */ + + #include -#include #include #include #include @@ -1071,6 +1077,54 @@ modest_text_utils_get_display_date (time_t date) gboolean +modest_text_utils_validate_folder_name (const gchar *folder_name) +{ + /* based on http://msdn2.microsoft.com/en-us/library/aa365247.aspx, + * with some extras */ + + guint len; + const gchar **cursor; + const gchar *forbidden_chars[] = { + "<", ">", ":", "\"", "/", "\\", "|", "?", "*", "^", "%", "$", NULL + }; + const gchar *forbidden_names[] = { /* windows does not like these */ + "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", + "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", + ".", "..", NULL + }; + + /* cannot be NULL */ + if (!folder_name) + return FALSE; + + /* cannot be empty */ + len = strlen(folder_name); + if (len == 0) + return FALSE; + + /* cannot start or end with a space */ + if (g_ascii_isspace(folder_name[0]) || g_ascii_isspace(folder_name[len - 1])) + return FALSE; + + /* cannot contain a forbidden char */ + for (cursor = forbidden_chars; cursor && *cursor; ++cursor) + if (strstr(folder_name, *cursor) != NULL) + return FALSE; + + /* cannot contain a forbidden word */ + if (len <= 4) { + for (cursor = forbidden_names; cursor && *cursor; ++cursor) { + g_warning ("%s", *cursor); + if (g_ascii_strcasecmp (folder_name, *cursor) == 0) + return FALSE; + } + } + return TRUE; /* it's valid! */ +} + + + +gboolean modest_text_utils_validate_domain_name (const gchar *domain) { gboolean valid = FALSE; diff --git a/src/modest-text-utils.h b/src/modest-text-utils.h index 5425d60..1c7fe41 100644 --- a/src/modest-text-utils.h +++ b/src/modest-text-utils.h @@ -292,6 +292,20 @@ gboolean modest_text_utils_validate_domain_name (const gchar *domain); gboolean modest_text_utils_validate_email_address (const gchar *email_address, const gchar **invalid_char_position); + +/** + * modest_text_utils_validate_folder_name: + * @folder_name: a string + * + * validates the folder name passed as argument. a 'valid folder name' + * is a name which should be valid on both Unix and Windows file systems. + * of course, this might be stricter than strictly needed in some cases, + * but it's better to err on the safe side. + * + * Returns: TRUE if the folder name is valid, FALSE otherwise + **/ +gboolean modest_text_utils_validate_folder_name (const gchar *folder_name); + /** * modest_text_utils_validate_recipient: * @recipient: a string