Fix bug #3741: Deleting an account doesn't delete imapcache
authorRicardo Mones <ricardo@mones.org>
Tue, 21 Feb 2017 18:05:19 +0000 (19:05 +0100)
committerRicardo Mones <ricardo@mones.org>
Fri, 24 Feb 2017 22:54:27 +0000 (23:54 +0100)
Based on initial patch by Paul and in exchange of a reviewed
patch for 3582 ;-)

src/account.c
src/prefs_account.c
src/prefs_account.h

index 5ff5d64..9625056 100644 (file)
@@ -1059,6 +1059,27 @@ static void account_clone(GtkWidget *widget, gpointer data)
 #undef ACP_FDUP
 #undef ACP_FASSIGN
 
 #undef ACP_FDUP
 #undef ACP_FASSIGN
 
+static void account_empty_cache(PrefsAccount *ac_prefs)
+{
+       gchar *cache_dir;
+
+       cache_dir = prefs_account_cache_dir(ac_prefs, FALSE);
+       if (cache_dir == NULL)
+               return; /* no cache dir, nothing to do */
+
+       if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0) {
+               g_warning("can't remove directory '%s'", cache_dir);
+       } else {
+               gchar *server_dir =  prefs_account_cache_dir(ac_prefs, TRUE);
+               if (g_rmdir(server_dir) == 0)
+                       debug_print("Removed empty cache server directory\n");
+               else
+                       debug_print("Cache server directory not empty: not removed\n");
+               g_free(server_dir);
+       }
+       g_free(cache_dir);
+}
+
 static void account_delete(GtkWidget *widget, gpointer data)
 {
        PrefsAccount *ac_prefs;
 static void account_delete(GtkWidget *widget, gpointer data)
 {
        PrefsAccount *ac_prefs;
@@ -1066,7 +1087,7 @@ static void account_delete(GtkWidget *widget, gpointer data)
        GList *list;
        Folder *folder;
        GSList *cur;
        GList *list;
        Folder *folder;
        GSList *cur;
+
        ac_prefs = account_list_view_get_selected_account(edit_account.list_view);
        if (ac_prefs == NULL)
                return;
        ac_prefs = account_list_view_get_selected_account(edit_account.list_view);
        if (ac_prefs == NULL)
                return;
@@ -1123,6 +1144,10 @@ static void account_delete(GtkWidget *widget, gpointer data)
                        cur = g_slist_next(cur);
                }
        }
                        cur = g_slist_next(cur);
                }
        }
+
+       debug_print("Removing cache directory of this account...\n");
+       account_empty_cache(ac_prefs);
+
        folder_write_list();
 }
 
        folder_write_list();
 }
 
index 9fc9616..185e24d 100644 (file)
@@ -4962,3 +4962,44 @@ void prefs_account_unregister_page(PrefsPage *page)
 {
        prefs_pages = g_slist_remove(prefs_pages, page);
 }
 {
        prefs_pages = g_slist_remove(prefs_pages, page);
 }
+
+gchar *prefs_account_cache_dir(PrefsAccount *ac_prefs, gboolean for_server)
+{
+       gchar *dir = NULL;
+#ifdef G_OS_WIN32
+       gchar *sanitized_server;
+#endif
+
+       if (ac_prefs->protocol == A_IMAP4) {
+#ifdef G_OS_WIN32
+               sanitized_server = g_strdup(ac_prefs->recv_server);
+               g_strdelimit(sanitized_server, ":", ',');
+#endif
+               if (for_server) {
+                       dir = g_strconcat(get_imap_cache_dir(),
+                                         G_DIR_SEPARATOR_S,
+#ifdef G_OS_WIN32
+                                         sanitized_server,
+#else
+                                         ac_prefs->recv_server,
+#endif
+                                         NULL);
+               } else {
+                       dir = g_strconcat(get_imap_cache_dir(),
+                                         G_DIR_SEPARATOR_S,
+#ifdef G_OS_WIN32
+                                         sanitized_server,
+#else
+                                         ac_prefs->recv_server,
+#endif
+                                         G_DIR_SEPARATOR_S,
+                                         ac_prefs->userid,
+                                         NULL);
+               }
+#ifdef G_OS_WIN32
+               g_free(sanitized_server);
+#endif
+       }
+
+       return dir;
+}
index f7feb13..83f6efa 100644 (file)
@@ -228,4 +228,6 @@ gchar *prefs_account_generate_msgid(PrefsAccount *account);
 void prefs_account_register_page       (PrefsPage      *page);
 void prefs_account_unregister_page     (PrefsPage      *page);
 
 void prefs_account_register_page       (PrefsPage      *page);
 void prefs_account_unregister_page     (PrefsPage      *page);
 
+gchar *prefs_account_cache_dir         (PrefsAccount   *ac_prefs, gboolean for_server);
+
 #endif /* PREFS_ACCOUNT_H */
 #endif /* PREFS_ACCOUNT_H */