Add new styles supported: Robohash and Pagan
[claws.git] / src / plugins / libravatar / libravatar.c
index 7870bc94db42a802a11d4bf7930d5647922eab47..75ec065ffe3bf2fb799c4aed54b5a3c265da6427 100644 (file)
@@ -1,7 +1,6 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2014 Hiroyuki Yamamoto and the Claws Mail Team
- * Copyright (C) 2014 Ricardo Mones
+ * Copyright (C) 2014-2015 Ricardo Mones and the Claws Mail Team
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -30,6 +29,8 @@
 #include "version.h"
 #include "libravatar.h"
 #include "libravatar_prefs.h"
+#include "libravatar_cache.h"
+#include "libravatar_image.h"
 #include "libravatar_missing.h"
 #include "libravatar_federation.h"
 #include "prefs_common.h"
@@ -45,11 +46,13 @@ static const char *def_mode[] = {
        "identicon",
        "monsterid",
        "wavatar",
-       "retro"
+       "retro",
+       "robohash",
+       "pagan"
 };
 
-static guint update_hook_id;
-static guint render_hook_id;
+static gulong update_hook_id = HOOK_NONE;
+static gulong render_hook_id = HOOK_NONE;
 static gchar *cache_dir = NULL; /* dir-separator terminated */
 
 static gboolean libravatar_header_update_hook(gpointer source, gpointer data)
@@ -70,6 +73,7 @@ static gboolean libravatar_header_update_hook(gpointer source, gpointer data)
 
                debug_print("libravatar added '%s'\n", a);
                procmsg_msginfo_add_avatar(acd->msginfo, AVATAR_LIBRAVATAR, a);
+               g_free(a);
        }
 
        return FALSE; /* keep getting */
@@ -77,7 +81,7 @@ static gboolean libravatar_header_update_hook(gpointer source, gpointer data)
 
 static gchar *federated_base_url_from_address(const gchar *address)
 {
-#if (defined USE_GNUTLS && GLIB_CHECK_VERSION(2,22,0))
+#if defined USE_GNUTLS
        gchar *base_url = NULL;
 
        if (!libravatarprefs.allow_federated) {
@@ -95,9 +99,21 @@ default_url:
        return g_strdup(libravatarprefs.base_url);
 }
 
-static GtkWidget *image_widget_from_filename(const gchar *filename)
+static GtkWidget *image_widget_from_pixbuf(GdkPixbuf *picture)
 {
        GtkWidget *image = NULL;
+
+       if (picture) {
+               image = gtk_image_new_from_pixbuf(picture);
+               g_object_unref(picture);
+       } else
+               g_warning("null picture returns null widget");
+
+       return image;
+}
+
+static GtkWidget *image_widget_from_filename(const gchar *filename)
+{
        GdkPixbuf *picture = NULL;
        GError *error = NULL;
        gint w, h;
@@ -112,17 +128,12 @@ static GtkWidget *image_widget_from_filename(const gchar *filename)
                picture = gdk_pixbuf_new_from_file(filename, &error);
 
        if (error != NULL) {
-               g_warning("Failed to load image '%s': %s\n", filename, error->message);
+               g_warning("failed to load image '%s': %s", filename, error->message);
                g_error_free(error);
-       } else {
-               if (picture) {
-                       image = gtk_image_new_from_pixbuf(picture);
-                       g_object_unref(picture);
-               } else
-                       g_warning("Failed to load image '%s': no error returned!\n", filename);
+               return NULL;
        }
 
-       return image;
+       return image_widget_from_pixbuf(picture);
 }
 
 static gchar *cache_name_for_md5(const gchar *md5)
@@ -137,73 +148,27 @@ static gchar *cache_name_for_md5(const gchar *md5)
        return g_strconcat(cache_dir, md5, NULL);
 }
 
-static size_t write_image_data_cb(void *ptr, size_t size, size_t nmemb, void *stream)
-{
-       size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
-       debug_print("received %zu bytes from avatar server\n", written);
-
-       return written;
-}
-
 static GtkWidget *image_widget_from_url(const gchar *url, const gchar *md5)
 {
        GtkWidget *image = NULL;
-       gchar *filename;
-       FILE *file;
-       CURL *curl;
-
-       curl = curl_easy_init();
-       if (curl == NULL) {
-               g_warning("could not initialize curl to get image from url\n");
-               return NULL;
-       }
-       curl_easy_setopt(curl, CURLOPT_URL, url);
-       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_image_data_cb);
-       curl_easy_setopt(curl, CURLOPT_TIMEOUT, prefs_common_get_prefs()->io_timeout_secs);
-       curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
-
-       filename = cache_name_for_md5(md5);
-       file = fopen(filename, "wb");
-       if (file != NULL) {
-               long filesize;
-
-               if (libravatarprefs.allow_redirects) {
-                       long maxredirs = (libravatarprefs.default_mode == DEF_MODE_URL)? 3L
-                               : ((libravatarprefs.default_mode == DEF_MODE_MM)? 2L: 1L);
-
-                       curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
-                       curl_easy_setopt(curl, CURLOPT_MAXREDIRS, maxredirs);
-               }
-               curl_easy_setopt(curl, CURLOPT_FILE, file);
-               debug_print("retrieving URL to file: %s -> %s\n", url, filename);
-               curl_easy_perform(curl);
-               filesize = ftell(file);
-               fclose(file);
-
-               if (filesize < MIN_PNG_SIZE)
-                       debug_print("not enough data for an avatar image: %ld bytes\n", filesize);
-               else
-                       image = image_widget_from_filename(filename);
-
-               if (!libravatarprefs.cache_icons || filesize == 0) {
-                       if (g_unlink(filename) < 0)
-                               g_warning("failed to delete cache file %s\n", filename);
-               }
-
-               if (filesize == 0)
-                       missing_add_md5(libravatarmisses, md5);
-       } else {
-               g_warning("could not open '%s' for writting\n", filename);
+       AvatarImageFetch aif;
+
+       aif.url = url;
+       aif.md5 = md5;
+       aif.filename = cache_name_for_md5(md5);
+       libravatar_image_fetch(&aif);
+       if (aif.pixbuf) {
+               image = gtk_image_new_from_pixbuf(aif.pixbuf);
+               g_object_unref(aif.pixbuf);
        }
-       curl_easy_cleanup(curl);
-       g_free(filename);
+       g_free(aif.filename);
 
        return image;
 }
 
 static gboolean is_recent_enough(const gchar *filename)
 {
-       struct stat s;
+       GStatBuf s;
        time_t t;
 
        if (libravatarprefs.cache_icons) {
@@ -239,15 +204,17 @@ static gchar *libravatar_url_for_md5(const gchar *base, const gchar *md5)
                                base, md5, AVATAR_SIZE,
                                def_mode[libravatarprefs.default_mode - 10]);
        } else if (libravatarprefs.default_mode == DEF_MODE_URL) {
-               return g_strdup_printf("%s/%s?s=%u&d=%s",
-                               base, md5, AVATAR_SIZE,
-                               libravatarprefs.default_mode_url);
+               gchar *escaped = g_uri_escape_string(libravatarprefs.default_mode_url, "/", TRUE);
+               gchar *url = g_strdup_printf("%s/%s?s=%u&d=%s",
+                               base, md5, AVATAR_SIZE, escaped);
+               g_free(escaped);
+               return url;
        } else if (libravatarprefs.default_mode == DEF_MODE_NONE) {
-               return g_strdup_printf("%s/%s?s=%u",
+               return g_strdup_printf("%s/%s?s=%u&d=404",
                                base, md5, AVATAR_SIZE);
        }
 
-       g_warning("invalid libravatar default mode: %d\n", libravatarprefs.default_mode);
+       g_warning("invalid libravatar default mode: %d", libravatarprefs.default_mode);
        return NULL;
 }
 
@@ -279,7 +246,7 @@ static gboolean libravatar_image_render_hook(gpointer source, gpointer data)
                        return FALSE;
                }
                /* not cached copy: try network */
-               if (prefs_common.work_offline) {
+               if (prefs_common_get_prefs()->work_offline) {
                        debug_print("working off-line: libravatar network retrieval skipped\n");
                        return FALSE;
                }
@@ -305,29 +272,8 @@ static gboolean libravatar_image_render_hook(gpointer source, gpointer data)
 
 static gint cache_dir_init()
 {
-       gchar *subdir;
-       int i;
-
-       cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
-                               LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S,
-                               NULL);
-       if (!is_dir_exist(cache_dir)) {
-               if (make_dir(cache_dir) < 0) {
-                       g_free(cache_dir);
-                       return -1;
-               }
-       }
-       for (i = DEF_MODE_MM; i <= DEF_MODE_RETRO; ++i) {
-               subdir = g_strconcat(cache_dir, def_mode[i - 10], NULL);
-               if (!is_dir_exist(subdir)) {
-                       if (make_dir(subdir) < 0) {
-                               g_warning("cannot create directory %s\n", subdir);
-                               g_free(subdir);
-                               return -1;
-                       }
-               }
-               g_free(subdir);
-       }
+       cache_dir = libravatar_cache_init(def_mode, DEF_MODE_MM - 10, DEF_MODE_RETRO - 10);
+       cm_return_val_if_fail (cache_dir != NULL, -1);
 
        return 0;
 }
@@ -361,6 +307,20 @@ static void missing_cache_done()
        }
 }
 
+static void unregister_hooks()
+{
+       if (render_hook_id != HOOK_NONE) {
+               hooks_unregister_hook(AVATAR_IMAGE_RENDER_HOOKLIST,
+                                     render_hook_id);
+               render_hook_id = HOOK_NONE;
+       }
+       if (update_hook_id != HOOK_NONE) {
+               hooks_unregister_hook(AVATAR_HEADER_UPDATE_HOOKLIST,
+                                     update_hook_id);
+               update_hook_id = HOOK_NONE;
+       }
+}
+
 /**
  * Initialize plugin.
  *
@@ -377,7 +337,7 @@ gint plugin_init(gchar **error)
        update_hook_id = hooks_register_hook(AVATAR_HEADER_UPDATE_HOOKLIST,
                                             libravatar_header_update_hook,
                                             NULL);
-       if (update_hook_id == -1) {
+       if (update_hook_id == HOOK_NONE) {
                *error = g_strdup(_("Failed to register avatar header update hook"));
                return -1;
        }
@@ -385,12 +345,14 @@ gint plugin_init(gchar **error)
        render_hook_id = hooks_register_hook(AVATAR_IMAGE_RENDER_HOOKLIST,
                                             libravatar_image_render_hook,
                                             NULL);
-       if (render_hook_id == -1) {
+       if (render_hook_id == HOOK_NONE) {
+               unregister_hooks();
                *error = g_strdup(_("Failed to register avatar image render hook"));
                return -1;
        }
        /* cache dir */
        if (cache_dir_init() == -1) {
+               unregister_hooks();
                *error = g_strdup(_("Failed to create avatar image cache directory"));
                return -1;
        }
@@ -400,6 +362,7 @@ gint plugin_init(gchar **error)
        curl_global_init(CURL_GLOBAL_DEFAULT);
        /* missing cache */
        if (missing_cache_init() == -1) {
+               unregister_hooks();
                *error = g_strdup(_("Failed to load missing items cache"));
                return -1;
        }
@@ -416,16 +379,7 @@ gint plugin_init(gchar **error)
  */
 gboolean plugin_done(void)
 {
-       if (render_hook_id != -1) {
-               hooks_unregister_hook(AVATAR_IMAGE_RENDER_HOOKLIST,
-                                     render_hook_id);
-               render_hook_id = -1;
-       }
-       if (update_hook_id != -1) {
-               hooks_unregister_hook(AVATAR_HEADER_UPDATE_HOOKLIST,
-                                     update_hook_id);
-               update_hook_id = -1;
-       }
+       unregister_hooks();
        libravatar_prefs_done();
        missing_cache_done();
        if (cache_dir != NULL)