Libravatar: clean cache button
[claws.git] / src / plugins / libravatar / libravatar_prefs.c
index f3c3be427aa7293a0bd9d70b9cb4a9fa4d907fdb..9e0e6c9995f8cf740754c9f79fed36c81dc9f5de 100644 (file)
@@ -1,7 +1,6 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2015 Hiroyuki Yamamoto and the Claws Mail Team
- * Copyright (C) 2015 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
 
 #include "defs.h"
 #include "libravatar_prefs.h"
+#include "libravatar_cache.h"
 #include "prefs_common.h"
 #include "prefs_gtk.h"
+#include "alertpanel.h"
 
 #define PREFS_BLOCK_NAME "Libravatar"
 #define NUM_DEF_BUTTONS 7
@@ -129,10 +130,83 @@ static GtkWidget *labeled_spinner_box(gchar *label, GtkWidget *spinner, gchar *u
        return hbox;
 }
 
+static gchar *avatar_stats_label_markup(AvatarCacheStats *stats)
+{
+       if (stats == NULL)
+               return g_strdup(
+                       _("<span color=\"red\">Error reading cache stats</span>"));
+
+       if (stats->errors > 0)
+               return g_markup_printf_escaped(
+                       _("<span color=\"red\">Using %s in %d files, %d "
+                       "directories, %d others and %d errors</span>"),
+                       to_human_readable((goffset) stats->bytes),
+                       stats->files,
+                       stats->dirs,
+                       stats->others,
+                       stats->errors);
+
+       return g_strdup_printf(
+               _("Using %s in %d files, %d directories and %d others"),
+               to_human_readable((goffset) stats->bytes),
+               stats->files,
+               stats->dirs,
+               stats->others);
+}
+
+static void cache_clean_button_clicked_cb(GtkButton *button, gpointer data)
+{
+       GtkLabel *label = (GtkLabel *) data;
+       gint val = 0;
+       AvatarCleanupResult *acr;
+       guint misses;
+
+       val = alertpanel_full(_("Clear icon cache"),
+                       _("Are you sure you want to remove all cached avatar icons?"),
+                       GTK_STOCK_NO, GTK_STOCK_YES, NULL, FALSE,
+                       NULL, ALERT_WARNING, G_ALERTDEFAULT);
+       if (val != G_ALERTALTERNATE)
+               return;
+
+       debug_print("cleaning missing cache");
+       misses = g_hash_table_size(libravatarmisses);
+       g_hash_table_remove_all(libravatarmisses);
+
+       debug_print("cleaning disk cache");
+       acr = libravatar_cache_clean();
+       if (acr == NULL) {
+               alertpanel_error(_("Not enough memory for operation"));
+               return;
+       }
+
+       if (acr->e_stat == 0 && acr->e_unlink == 0) {
+               alertpanel_notice(_("Icon cache successfully cleared:\n"
+                       "• %u missing entries removed.\n"
+                       "• %u files removed."),
+                       misses, acr->removed);
+               gtk_label_set_markup(label,
+                       _("<span color=\"#006400\">Icon cache succesfully cleared!</span>"));
+       }
+       else {
+               alertpanel_warning(_("Errors clearing icon cache:\n"
+                       "• %u missing entries removed.\n"
+                       "• %u files removed.\n"
+                       "• %u files failed to be read.\n"
+                       "• %u files couldn't be removed."),
+                       misses, acr->removed, acr->e_stat, acr->e_unlink);
+               gtk_label_set_markup(label,
+                       _("<span color=\"red\">Error clearing icon cache.</span>"));
+       }
+       gtk_widget_set_sensitive(GTK_WIDGET(button), FALSE);
+       g_free(acr);
+}
+
 static GtkWidget *p_create_frame_cache(struct LibravatarPrefsPage *page)
 {
-       GtkWidget *vbox, *checkbox, *spinner, *hbox;
+       GtkWidget *vbox, *checkbox, *spinner, *hbox, *label, *button;
        GtkAdjustment *adj;
+       AvatarCacheStats *stats;
+       gchar *markup;
 
        vbox =  gtk_vbox_new(FALSE, 6);
 
@@ -158,6 +232,29 @@ static GtkWidget *p_create_frame_cache(struct LibravatarPrefsPage *page)
        gtk_box_pack_start(GTK_BOX(vbox), checkbox, FALSE, FALSE, 0);
        gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
 
+       label = gtk_label_new(NULL);
+       gtk_widget_show(label);
+       stats = libravatar_cache_stats();
+       markup = avatar_stats_label_markup(stats);
+       gtk_label_set_markup(GTK_LABEL(label), markup);
+       g_free(markup);
+       gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
+
+       button = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
+       gtk_widget_show(button);
+       g_signal_connect(button, "clicked",
+               G_CALLBACK(cache_clean_button_clicked_cb), label);
+       gtk_widget_set_sensitive(button, (stats != NULL && stats->bytes > 0));
+
+       hbox = gtk_hbox_new(FALSE, 6);
+       gtk_widget_show(hbox);
+       gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
+
+       if (stats != NULL)
+               g_free(stats);
+
        return vbox;
 }
 
@@ -324,6 +421,7 @@ static GtkWidget *p_create_frame_network(struct LibravatarPrefsPage *page)
   ┌─Icon cache───────────────────────────────────────────┐
   │ [✔] Use cached icons                                 │
   │ Cache refresh interval [ 24 |⬘] hours                │
+  │ Using X KB in Y files and Z directories [Clear]      │
   └──────────────────────────────────────────────────────┘
   ┌─Default missing icon mode────────────────────────────┐
   │ (•) None                                             │
@@ -389,7 +487,7 @@ static void libravatar_save_config(void)
                return;
 
        if (prefs_write_param(param, pfile->fp) < 0) {
-               g_warning("Failed to write Libravatar configuration to file\n");
+               g_warning("failed to write Libravatar configuration to file");
                prefs_file_close_revert(pfile);
                return;
        }
@@ -457,6 +555,7 @@ void libravatar_prefs_init(void)
        libravatarprefs_page.page.create_widget = libravatar_prefs_create_widget_func;
        libravatarprefs_page.page.destroy_widget = libravatar_prefs_destroy_widget_func;
        libravatarprefs_page.page.save_page = libravatar_prefs_save_func;
+       libravatarprefs_page.page.weight = 40.0;
 
        prefs_gtk_register_page((PrefsPage *) &libravatarprefs_page);
 }