diff options
author | Matthew Lazarow <[email protected]> | 2018-08-13 15:56:01 -0400 |
---|---|---|
committer | Scott <[email protected]> | 2018-08-13 20:56:01 +0100 |
commit | a9daef1e18b93f793479155210110322234bb655 (patch) | |
tree | 0bd762a171c18d9999ec13dfc65f025f1e7db160 /gtk/src | |
parent | 223dc7bd007bfd665948f7e11a9f4681d580cb35 (diff) |
Autoname creation date
Adds {creation-date} and {creation-time} templates to autoname
preference option.
Diffstat (limited to 'gtk/src')
-rw-r--r-- | gtk/src/callbacks.c | 85 | ||||
-rw-r--r-- | gtk/src/ghb.m4 | 2 |
2 files changed, 86 insertions, 1 deletions
diff --git a/gtk/src/callbacks.c b/gtk/src/callbacks.c index bee41d115..bf00b7a38 100644 --- a/gtk/src/callbacks.c +++ b/gtk/src/callbacks.c @@ -50,6 +50,8 @@ #include <netinet/in.h> #include <netdb.h> +#include <regex.h> + #if !defined(_NO_UPDATE_CHECK) #if defined(_OLD_WEBKIT) #include <webkit.h> @@ -1050,6 +1052,71 @@ check_name_template(signal_user_data_t *ud, const char *str) return FALSE; } +static int +match_by_pattern(const char *string, const char *pattern) +{ + int status; + regex_t re; + if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) + { + return 0; + } + status = regexec(&re, string, (size_t) 0, NULL, 0); + regfree(&re); + if (status != 0) + { + return 0; + } + return 1; +} + +typedef struct { + const char *pattern; + const char *format; +} datemap; + +static int +parse_datestring(const char *src, struct tm *tm) +{ + datemap ymdThmsZ = {"[0-9]{4}-[0-1]?[0-9]-[0-3]?[0-9]T[0-9]{2}:[0-9]{2}:[0-9]{2}Z", "%Y-%m-%dT%H:%M:%SZ"}; + + datemap maps[1] = { ymdThmsZ }; + + for (int i = 0; i < sizeof(maps); i++) + { + if (match_by_pattern(src, maps[i].pattern)) + { + strptime(src, maps[i].format, tm); + return 1; + } + } + return 0; +} + +static char* +get_creation_date(const char *pattern, const char *metaValue, const char *file) +{ + char date[11] = ""; + if (metaValue != NULL && strlen(metaValue) > 1) + { + struct tm tm; + if (parse_datestring(metaValue, &tm)) + { + strftime(date, 11, pattern, &tm); + } + } + else + { + struct stat stbuf; + if (g_stat(file, &stbuf) == 0){ + struct tm *tm; + tm = localtime(&(stbuf.st_mtime)); + strftime(date, 11, pattern, tm); + } + } + return strdup(date); +} + static void set_destination_settings(signal_user_data_t *ud, GhbValue *settings) { @@ -1138,6 +1205,24 @@ set_destination_settings(signal_user_data_t *ud, GhbValue *settings) g_string_append_printf(str, "%s", dt); p += strlen("{date}"); } + else if (!strncmp(p, "{creation-date}", strlen("{creation-date}"))) + { + gchar *val; + const gchar *source = ghb_dict_get_string(ud->globals, "scan_source"); + val = get_creation_date("%Y-%m-%d", ghb_dict_get_string(settings, "MetaReleaseDate"), source); + g_string_append_printf(str, "%s", val); + p += strlen("{creation-date}"); + g_free(val); + } + else if (!strncmp(p, "{creation-time}", strlen("{creation-time}"))) + { + gchar *val; + const gchar *source = ghb_dict_get_string(ud->globals, "scan_source"); + val = get_creation_date("%H:%M", ghb_dict_get_string(settings, "MetaReleaseDate"), source); + g_string_append_printf(str, "%s", val); + p += strlen("{creation-time}"); + g_free(val); + } else if (!strncmp(p, "{quality}", strlen("{quality}"))) { if (ghb_dict_get_bool(settings, "vquality_type_constant")) diff --git a/gtk/src/ghb.m4 b/gtk/src/ghb.m4 index 26a0ed00a..226da35ca 100644 --- a/gtk/src/ghb.m4 +++ b/gtk/src/ghb.m4 @@ -7412,7 +7412,7 @@ filter_output([ </child> <child> <object class="GtkEntry" id="auto_name_template"> - <property name="tooltip_text" translatable="yes">Available Options: {source} {title} {preset} {chapters} {date} {time} {quality} {bitrate}</property> + <property name="tooltip_text" translatable="yes">Available Options: {source} {title} {preset} {chapters} {date} {time} {creation-date} {creation-time} {quality} {bitrate}</property> <property name="visible">True</property> <property name="can_focus">True</property> <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property> |