Libravatar: remove hooks on failed init
[claws.git] / src / plugins / libravatar / libravatar.c
index 3088fa262ad7352b390fa8364c6401013e0ef05a..e36829e61c8387939cecbcdb776f1e89956bef53 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,7 @@
 #include "version.h"
 #include "libravatar.h"
 #include "libravatar_prefs.h"
+#include "libravatar_cache.h"
 #include "libravatar_missing.h"
 #include "libravatar_federation.h"
 #include "prefs_common.h"
@@ -113,14 +113,14 @@ 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);
+                       g_warning("failed to load image '%s': no error returned!", filename);
        }
 
        return image;
@@ -155,7 +155,7 @@ static GtkWidget *image_widget_from_url(const gchar *url, const gchar *md5)
 
        curl = curl_easy_init();
        if (curl == NULL) {
-               g_warning("could not initialize curl to get image from url\n");
+               g_warning("could not initialize curl to get image from URL");
                return NULL;
        }
        curl_easy_setopt(curl, CURLOPT_URL, url);
@@ -194,13 +194,13 @@ static GtkWidget *image_widget_from_url(const gchar *url, const gchar *md5)
 
                if (!libravatarprefs.cache_icons || filesize == 0) {
                        if (g_unlink(filename) < 0)
-                               g_warning("failed to delete cache file %s\n", filename);
+                               g_warning("failed to delete cache file '%s'", filename);
                }
 
                if (filesize == 0)
                        missing_add_md5(libravatarmisses, md5);
        } else {
-               g_warning("could not open '%s' for writing\n", filename);
+               g_warning("could not open '%s' for writing", filename);
        }
        curl_easy_cleanup(curl);
        g_free(filename);
@@ -254,7 +254,7 @@ static gchar *libravatar_url_for_md5(const gchar *base, const gchar *md5)
                                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;
 }
 
@@ -286,7 +286,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;
                }
@@ -312,29 +312,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;
 }
@@ -368,6 +347,20 @@ static void missing_cache_done()
        }
 }
 
+static void unregister_hooks()
+{
+       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;
+       }
+}
+
 /**
  * Initialize plugin.
  *
@@ -393,11 +386,13 @@ gint plugin_init(gchar **error)
                                             libravatar_image_render_hook,
                                             NULL);
        if (render_hook_id == -1) {
+               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;
        }
@@ -407,6 +402,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;
        }
@@ -423,16 +419,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)