diff options
-rw-r--r-- | endless/Makefile.am | 2 | ||||
-rw-r--r-- | endless/endless.h | 1 | ||||
-rw-r--r-- | endless/eosattribution.c | 93 | ||||
-rw-r--r-- | endless/eoslicense.c | 209 | ||||
-rw-r--r-- | endless/eoslicense.h | 52 |
5 files changed, 280 insertions, 77 deletions
diff --git a/endless/Makefile.am b/endless/Makefile.am index 52f6f85..a39c1e6 100644 --- a/endless/Makefile.am +++ b/endless/Makefile.am @@ -29,6 +29,7 @@ endless_private_installed_headers = \ endless/eosapplication.h \ endless/eoscustomcontainer.h \ endless/eosenums.h \ + endless/eoslicense.h \ endless/eosmacros.h \ endless/eospagemanager.h \ endless/eostypes.h \ @@ -43,6 +44,7 @@ endless_library_sources = \ endless/eoscustomcontainer.c \ endless/eoshello.c \ endless/eosinit.c endless/eosinit-private.h \ + endless/eoslicense.c \ endless/eospagemanager.c \ endless/eosresource.c endless/eosresource-private.h \ endless/eostopbar.c endless/eostopbar-private.h \ diff --git a/endless/endless.h b/endless/endless.h index bfc8524..4e9d3f2 100644 --- a/endless/endless.h +++ b/endless/endless.h @@ -14,6 +14,7 @@ G_BEGIN_DECLS #include "eostypes.h" #include "eosapplication.h" #include "eosflexygrid.h" +#include "eoslicense.h" #include "eospagemanager.h" #include "eoswindow.h" #include "eoscustomcontainer.h" diff --git a/endless/eosattribution.c b/endless/eosattribution.c index 068c492..c930169 100644 --- a/endless/eosattribution.c +++ b/endless/eosattribution.c @@ -8,6 +8,7 @@ #include "eosattribution-private.h" #include "eoscellrendererpixbuflink-private.h" #include "eoscellrenderertextlink-private.h" +#include "eoslicense.h" typedef struct { @@ -43,69 +44,6 @@ enum static GParamSpec *eos_attribution_props[NPROPS] = { NULL, }; -/* These are the recognized string values for the "license" field. Any other -license must be clarified in the comments, or linked to with the "license_uri" -field. Make sure to add new values to the table "image-attribution-licenses" in -the documentation of EosApplication and to the two arrays below this one. */ -static gchar * const recognized_licenses[] = { - "Public domain", - "CC0 1.0", - "CC BY 2.0", - "CC BY 3.0", - "CC BY-SA 2.0", - "CC BY-SA 2.5", - "CC BY-SA 3.0", - "CC BY-SA 4.0", - "CC BY-NC 2.0", - "CC BY-NC 3.0", - "CC BY-NC-SA 2.0", - "CC BY-ND 2.0", - "CC BY-ND 3.0", - NULL -}; - -static gchar * const license_display_names[] = { - /* TRANSLATORS: These names should be translated as the official names of the - licenses in your language. Note: these names are markup, and so should not - contain any ampersands (&), less-than signs (<) or greater-than signs (>). */ - N_("Public domain"), - N_("CC0 1.0 (Public domain)"), - N_("Creative Commons Attribution 2.0"), - N_("Creative Commons Attribution 3.0"), - N_("Creative Commons Attribution-ShareAlike 2.0"), - N_("Creative Commons Attribution-ShareAlike 2.5 Generic"), - N_("Creative Commons Attribution-ShareAlike 3.0"), - N_("Creative Commons Attribution-ShareAlike 4.0 International"), - N_("Creative Commons Attribution-NonCommercial 2.0"), - N_("Creative Commons Attribution-NonCommercial 3.0"), - N_("Creative Commons Attribution-NonCommercial-ShareAlike 2.0 Generic"), - N_("Creative Commons Attribution-NoDerivs 2.0"), - N_("Creative Commons Attribution-NoDerivs 3.0"), - NULL -}; - -static gchar * const license_uris[] = { - NULL, - /* TRANSLATORS: These URIs should be translated as a link to the license page - in your language. For example, for Spanish, - "http://creativecommons.org/licenses/by/3.0/" should become - "http://creativecommons.org/licenses/by/3.0/deed.es". However, if no such page - exists, then leave these untranslated. */ - N_("http://creativecommons.org/publicdomain/zero/1.0/"), - N_("http://creativecommons.org/licenses/by/2.0/"), - N_("http://creativecommons.org/licenses/by/3.0/"), - N_("http://creativecommons.org/licenses/by-sa/2.0/"), - N_("http://creativecommons.org/licenses/by-sa/2.5/"), - N_("http://creativecommons.org/licenses/by-sa/3.0/"), - N_("http://creativecommons.org/licenses/by-sa/4.0/"), - N_("http://creativecommons.org/licenses/by-nc/2.0/"), - N_("http://creativecommons.org/licenses/by-nc/3.0/"), - N_("http://creativecommons.org/licenses/by-nc-sa/2.0/"), - N_("http://creativecommons.org/licenses/by-nd/2.0/"), - N_("http://creativecommons.org/licenses/by-nd/3.0/"), - NULL -}; - enum { COLUMN_PIXBUF, @@ -274,13 +212,13 @@ render_license_link (GtkTreeViewColumn *column, GtkTreeModel *model, GtkTreeIter *iter) { + gchar *license_code; gchar *license_uri; - gint license_index; gtk_tree_model_get (model, iter, - COLUMN_LICENSE, &license_index, + COLUMN_LICENSE, &license_code, COLUMN_LICENSE_URI, &license_uri, -1); - if (license_index != -1) + if (license_code != NULL) { /* TRANSLATORS: %s will be replaced with the name of an image license, such as "Public domain" or "Creative Commons Attribution". These names are @@ -288,7 +226,7 @@ render_license_link (GtkTreeViewColumn *column, string. Note: this string is markup, and so should not contain any ampersands (&), less-than signs (<), or greater-than signs (>). */ gchar *license_string = g_strdup_printf (_("%s."), - gettext (license_display_names[license_index])); + gettext (eos_get_license_display_name (license_code))); gboolean behave_like_link = (license_uri != NULL); g_object_set (renderer, "markup", license_string, @@ -312,6 +250,7 @@ render_license_link (GtkTreeViewColumn *column, g_object_set (renderer, "visible", FALSE, NULL); } + g_free (license_code); g_free (license_uri); } @@ -423,7 +362,7 @@ eos_attribution_init (EosAttribution *self) priv->model = gtk_list_store_new (NUM_MODEL_COLUMNS, GDK_TYPE_PIXBUF, G_TYPE_STRING, - G_TYPE_INT, + G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, @@ -615,18 +554,18 @@ eos_attribution_initable_init (GInitable *initable, "copyright_holder.", resource_path); continue; } - gint license_index = -1; + if (license != NULL) { - license_index = strv_index (recognized_licenses, license); - if (license_index == -1) + if (license_uri == NULL) { - g_warning ("License string for image %s not recognized: %s", - resource_path, license); - continue; + GFile *license_file = eos_get_license_file (license); + if (license_file != NULL) + { + license_uri = g_file_get_uri (license_file); + g_object_unref (license_file); + } } - if (license_uri == NULL) - license_uri = license_uris[license_index]; } /* Populate a row of the model */ @@ -649,7 +588,7 @@ eos_attribution_initable_init (GInitable *initable, gtk_list_store_set (priv->model, &new_row, COLUMN_PIXBUF, pixbuf, COLUMN_ORIGINAL_URI, original_uri, - COLUMN_LICENSE, license_index, + COLUMN_LICENSE, license, COLUMN_LICENSE_URI, license_uri, COLUMN_CREDIT, credit, COLUMN_CREDIT_CONTACT, credit_contact, diff --git a/endless/eoslicense.c b/endless/eoslicense.c new file mode 100644 index 0000000..fc18dbf --- /dev/null +++ b/endless/eoslicense.c @@ -0,0 +1,209 @@ +/* Copyright 2015 Endless Mobile, Inc. */ + +#include "config.h" +#include <gio/gio.h> +#include <glib.h> +#include <glib/gi18n-lib.h> +#include <string.h> + +#include "eoslicense.h" + +/** + * SECTION: + */ + +/* These are the recognized string values for the "license" field. Any other +license must be clarified in the comments, or linked to with the "license_uri" +field. Make sure to add new values to the table "image-attribution-licenses" in +the documentation of EosApplication and to the two arrays below this one. */ +gchar * const recognized_licenses[] = { + "Public domain", + "Owner permission", + "CC0 1.0", + "CC BY 2.0", + "CC BY 3.0", + "CC BY 4.0", + "CC BY-NC 2.0", + "CC BY-NC 3.0", + "CC BY-NC-SA 2.0", + "CC BY-ND 2.0", + "CC BY-ND 3.0", + "CC BY-SA 2.0", + "CC BY-SA 2.5", + "CC BY-SA 3.0", + "CC BY-SA 4.0", + NULL +}; + +/* These are the recognized display names for license names. There is a 1-to-1 +correspondence between `recognized_licenses` and this array */ +gchar * const recognized_licenses_display_names[] = { + /* TRANSLATORS: These names should be translated as the official names of the + licenses in your language. Note: these names are markup, and so should not + contain any ampersands (&), less-than signs (<) or greater-than signs (>). */ + N_("Public domain"), + N_("Owner permission"), + N_("CC0-1.0"), + N_("Creative Commons Attribution 2.0"), + N_("Creative Commons Attribution 3.0"), + N_("Creative Commons Attribution 4.0"), + N_("Creative Commons Attribution-NonCommercial 2.0"), + N_("Creative Commons Attribution-NonCommercial 3.0"), + N_("Creative Commons Attribution-NonCommercial-ShareAlike 2.0 Generic"), + N_("Creative Commons Attribution-NoDerivs 2.0"), + N_("Creative Commons Attribution-NoDerivs 3.0"), + N_("Creative Commons Attribution-ShareAlike 2.0"), + N_("Creative Commons Attribution-ShareAlike 2.5 Generic"), + N_("Creative Commons Attribution-ShareAlike 3.0"), + N_("Creative Commons Attribution-ShareAlike 4.0 International"), + NULL +}; + +/* These are the actual filenames for license files. There is a 1-to-1 +correspondence between `recognized_licenses` and this array. */ +static gchar * const recognized_licenses_filenames[] = { + NULL, + NULL, + "publicdomain/CC0-1.0.html", + "creativecommons/CC-BY-2.0.html", + "creativecommons/CC-BY-3.0.html", + "creativecommons/CC-BY-4.0.html", + "creativecommons/CC-BY-NC-2.0.html", + "creativecommons/CC-BY-NC-3.0.html", + "creativecommons/CC-BY-NC-SA-2.0.html", + "creativecommons/CC-BY-ND-2.0.html", + "creativecommons/CC-BY-ND-3.0.html", + "creativecommons/CC-BY-SA-2.0.html", + "creativecommons/CC-BY-SA-2.5.html", + "creativecommons/CC-BY-SA-3.0.html", + "creativecommons/CC-BY-SA-4.0.html", + NULL +}; + +static const char * +get_locale (GFile *cc_licenses_dir) +{ + static gchar * locale; + + if (g_once_init_enter (&locale)) + { + const gchar * const * languages = g_get_language_names (); + const gchar * const * iter; + for (iter = languages; *iter != NULL; iter++) + { + GFile *license_file = g_file_get_child (cc_licenses_dir, *iter); + + gboolean locale_file_exists = g_file_query_exists (license_file, NULL); + + g_object_unref (license_file); + + if (locale_file_exists) + break; + } + + /* Licenses will always be installed for at least one locale, which + may be the default C locale. */ + g_assert (*iter != NULL); + + g_once_init_leave (&locale, *iter); + } + + return locale; +} + +static gchar * +get_sanitized_license_code (const gchar *license) +{ + gchar *sanitized_license = g_strdup (license); + + /* Checks whether the prefix of license is "CC-BY", and if so, converts it + to "CC BY". */ + if (g_str_has_prefix (sanitized_license, "CC-BY")) + sanitized_license[2] = ' '; + return sanitized_license; +} + +static int +get_license_index (const gchar *license) +{ + int i; + for (i = 0; recognized_licenses[i] != NULL; i++) + { + if (strcmp (recognized_licenses[i], license) == 0) + return i; + } + + /* If no license was found, return -1. */ + return -1; +} + +/** + * eos_get_license_display_name: + * @license: The license name + * + * Returns: A string for the specified @license name and the + * current locale. + * Since: 0.4 + */ +const gchar * +eos_get_license_display_name (const gchar *license) +{ + /* Sanitize input */ + gchar *sanitized_license = get_sanitized_license_code (license); + /* Get index of valid license */ + int index = get_license_index (sanitized_license); + g_free (sanitized_license); + + /* If the array value is null, it means we don't have a license file for that + license name. */ + g_return_val_if_fail (recognized_licenses[index] != NULL, NULL); + + return recognized_licenses_display_names[index]; +} + +/** + * eos_get_license_file: + * @license: The license name + * + * Returns: (transfer full): A GFile for the specified @license name and the + * current locale. It returns NULL if the license + * is not found or does not have an associated file. + * Since: 0.4 + */ +GFile * +eos_get_license_file (const gchar *license) +{ + /* Sanitize input */ + gchar *sanitized_license = get_sanitized_license_code (license); + /* Get index of valid license */ + int index = get_license_index (sanitized_license); + g_free (sanitized_license); + + /* If the code array value is null, it means it is an unrecognized license code. */ + g_return_val_if_fail (recognized_licenses[index] != NULL, NULL); + + /* If the array value is null, it means we don't have a license file for that + license name. */ + if (recognized_licenses_filenames[index] == NULL) + return NULL; + + /* We need to split the recognized_licenses_filenames into the subdir and the filename */ + gchar **license_filename = g_strsplit (recognized_licenses_filenames[index], "/", 2); + + gchar *licenses_path = g_build_filename (DATADIR, "licenses", + license_filename[0], NULL); + GFile *cc_licenses_dir = g_file_new_for_path (licenses_path); + g_free (licenses_path); + + const char *locale = get_locale (cc_licenses_dir); + + GFile *license_locale_dir = g_file_get_child (cc_licenses_dir, locale); + GFile *license_file = g_file_get_child (license_locale_dir, + license_filename[1]); + + g_object_unref (cc_licenses_dir); + g_object_unref (license_locale_dir); + g_strfreev (license_filename); + + return license_file; +} diff --git a/endless/eoslicense.h b/endless/eoslicense.h new file mode 100644 index 0000000..7614da3 --- /dev/null +++ b/endless/eoslicense.h @@ -0,0 +1,52 @@ +/* Copyright 2015 Endless Mobile, Inc. */ + +#ifndef EOS_LICENSE_H +#define EOS_LICENSE_H + +#if !(defined(_EOS_SDK_INSIDE_ENDLESS_H) || defined(COMPILING_EOS_SDK)) +#error "Please do not include this header file directly." +#endif + +#include "eostypes.h" + +G_BEGIN_DECLS + +/** + * EOS_LICENSE_PUBLIC_DOMAN: + * + * A string constant that represents the "Public domain" attribution level. + * + * Since: 0.4 + */ +#define EOS_LICENSE_PUBLIC_DOMAN "Public domain" + +/** + * EOS_LICENSE_OWNER_PERMISSION: + * + * A string constant that represents the "Owner permission" attribution level. + * + * Since: 0.4 + */ +#define EOS_LICENSE_OWNER_PERMISSION "Owner permission" + +/** + * EOS_LICENSE_NO_LICENSE: + * + * A string constant that represents the "No license" attribution level. + * + * Since: 0.4 + */ +#define EOS_LICENSE_NO_LICENSE "No license" + +EOS_SDK_AVAILABLE_IN_0_4 +GFile * +eos_get_license_file (const gchar *license); + +EOS_SDK_AVAILABLE_IN_0_4 +const char * +eos_get_license_display_name (const gchar *license); + + +G_END_DECLS + +#endif /* EOS_LICENSE_H */ |