Better solution than a global function call, use our hook infrastructure
authorColin Leroy <colin@colino.net>
Thu, 22 May 2014 20:46:07 +0000 (22:46 +0200)
committerColin Leroy <colin@colino.net>
Thu, 22 May 2014 20:46:07 +0000 (22:46 +0200)
(with minor modifications for moving)

src/compose.c
src/compose.h
src/folder.c
src/folder.h
src/folderview.c
src/news_gtk.c

index d0123db..68f29a1 100644 (file)
@@ -2109,6 +2109,44 @@ static gboolean compose_is_sig_separator(Compose *compose, GtkTextBuffer *textbu
        return FALSE;
 }
 
+static gboolean compose_update_folder_hook(gpointer source, gpointer data)
+{
+       FolderUpdateData *hookdata = (FolderUpdateData *)source;
+       Compose *compose = (Compose *)data;
+       FolderItem *old_item = NULL;
+       FolderItem *new_item = NULL;
+       gchar *old_id, *new_id;
+
+       if (!(hookdata->update_flags & FOLDER_REMOVE_FOLDERITEM)
+        && !(hookdata->update_flags & FOLDER_MOVE_FOLDERITEM))
+               return FALSE;
+
+       old_item = hookdata->item;
+       new_item = hookdata->item2;
+
+       old_id = folder_item_get_identifier(old_item);
+       new_id = new_item ? folder_item_get_identifier(new_item) : g_strdup("NULL");
+
+       if (compose->targetinfo && compose->targetinfo->folder == old_item) {
+               debug_print("updating targetinfo folder: %s -> %s\n", old_id, new_id);
+               compose->targetinfo->folder = new_item;
+       }
+
+       if (compose->replyinfo && compose->replyinfo->folder == old_item) {
+               debug_print("updating replyinfo folder: %s -> %s\n", old_id, new_id);
+               compose->replyinfo->folder = new_item;
+       }
+
+       if (compose->fwdinfo && compose->fwdinfo->folder == old_item) {
+               debug_print("updating fwdinfo folder: %s -> %s\n", old_id, new_id);
+               compose->fwdinfo->folder = new_item;
+       }
+
+       g_free(old_id);
+       g_free(new_id);
+       return FALSE;
+}
+
 static void compose_colorize_signature(Compose *compose)
 {
        GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(compose->text));
@@ -5976,17 +6014,25 @@ static gint compose_queue_sub(Compose *compose, gint *msgnum, FolderItem **item,
        }
        /* Message-ID of message replying to */
        if ((compose->replyinfo != NULL) && (compose->replyinfo->msgid != NULL)) {
-               gchar *folderid;
-               
-               folderid = folder_item_get_identifier(compose->replyinfo->folder);
+               gchar *folderid = NULL;
+
+               if (compose->replyinfo->folder)
+                       folderid = folder_item_get_identifier(compose->replyinfo->folder);
+               if (folderid == NULL)
+                       folderid = g_strdup("NULL");
+
                err |= (fprintf(fp, "RMID:%s\t%d\t%s\n", folderid, compose->replyinfo->msgnum, compose->replyinfo->msgid) < 0);
                g_free(folderid);
        }
        /* Message-ID of message forwarding to */
        if ((compose->fwdinfo != NULL) && (compose->fwdinfo->msgid != NULL)) {
-               gchar *folderid;
+               gchar *folderid = NULL;
                
-               folderid = folder_item_get_identifier(compose->fwdinfo->folder);
+               if (compose->fwdinfo->folder)
+                       folderid = folder_item_get_identifier(compose->fwdinfo->folder);
+               if (folderid == NULL)
+                       folderid = g_strdup("NULL");
+
                err |= (fprintf(fp, "FMID:%s\t%d\t%s\n", folderid, compose->fwdinfo->msgnum, compose->fwdinfo->msgid) < 0);
                g_free(folderid);
        }
@@ -7899,6 +7945,11 @@ static Compose *compose_create(PrefsAccount *account,
        compose->exteditor_tag     = -1;
        compose->draft_timeout_tag = COMPOSE_DRAFT_TIMEOUT_FORBIDDEN; /* inhibit auto-drafting while loading */
 
+       compose->folder_update_callback_id =
+               hooks_register_hook(FOLDER_UPDATE_HOOKLIST,
+                               compose_update_folder_hook,
+                               (gpointer) compose);
+
 #if USE_ENCHANT
        cm_menu_set_sensitive_full(compose->ui_manager, "Menu/Spelling", FALSE);
        if (mode != COMPOSE_REDIRECT) {
@@ -8654,6 +8705,9 @@ static void compose_destroy(Compose *compose)
 
        g_hash_table_destroy(compose->email_hashtable);
 
+       hooks_unregister_hook(FOLDER_UPDATE_HOOKLIST,
+                       compose->folder_update_callback_id);
+
        procmsg_msginfo_free(compose->targetinfo);
        procmsg_msginfo_free(compose->replyinfo);
        procmsg_msginfo_free(compose->fwdinfo);
@@ -9765,17 +9819,25 @@ gboolean compose_draft (gpointer data, guint action)
 
        /* Message-ID of message replying to */
        if ((compose->replyinfo != NULL) && (compose->replyinfo->msgid != NULL)) {
-               gchar *folderid;
-               
-               folderid = folder_item_get_identifier(compose->replyinfo->folder);
+               gchar *folderid = NULL;
+
+               if (compose->replyinfo->folder)
+                       folderid = folder_item_get_identifier(compose->replyinfo->folder);
+               if (folderid == NULL)
+                       folderid = g_strdup("NULL");
+
                err |= (fprintf(fp, "RMID:%s\t%d\t%s\n", folderid, compose->replyinfo->msgnum, compose->replyinfo->msgid) < 0);
                g_free(folderid);
        }
        /* Message-ID of message forwarding to */
        if ((compose->fwdinfo != NULL) && (compose->fwdinfo->msgid != NULL)) {
-               gchar *folderid;
-               
-               folderid = folder_item_get_identifier(compose->fwdinfo->folder);
+               gchar *folderid = NULL;
+
+               if (compose->fwdinfo->folder)
+                       folderid = folder_item_get_identifier(compose->fwdinfo->folder);
+               if (folderid == NULL)
+                       folderid = g_strdup("NULL");
+
                err |= (fprintf(fp, "FMID:%s\t%d\t%s\n", folderid, compose->fwdinfo->msgnum, compose->fwdinfo->msgid) < 0);
                g_free(folderid);
        }
@@ -11803,33 +11865,6 @@ static void compose_subject_entry_activated(GtkWidget *widget, gpointer data)
        gtk_widget_grab_focus(compose->text);
 }
 
-void compose_list_update_folders(FolderItem *old_item, FolderItem *new_item)
-{
-       const GList *compose_list = compose_get_compose_list();
-       const GList *elem = NULL;
-       
-       if (compose_list) {
-               for (elem = compose_list; elem != NULL && elem->data != NULL; 
-                    elem = elem->next) {
-                       Compose *c = (Compose*)elem->data;
-
-                       if (c->targetinfo && c->targetinfo->folder == old_item)
-                               c->targetinfo->folder = new_item;
-
-                       if (c->replyinfo && c->replyinfo->folder == old_item)
-                               c->replyinfo->folder = new_item;
-
-                       if (c->fwdinfo && c->fwdinfo->folder == old_item)
-                               c->fwdinfo->folder = new_item;
-
-                       if (c->autosaved_draft && c->autosaved_draft->folder == old_item)
-                               c->autosaved_draft->folder = new_item;
-
-               }
-       }
-}
-
-
 /*
  * End of Source.
  */
index b7eb416..51d0e7e 100644 (file)
@@ -238,14 +238,16 @@ struct _Compose
        gint close_timeout_tag;
        gchar *orig_charset;
        gint set_cursor_pos;
-       
+
        gboolean updating;
        gboolean deferred_destroy;
        ComposeMode rmode;
        GtkWidget *first_combo;
        GtkWidget *first_entry;
-       
+
        GtkUIManager *ui_manager;
+
+       gint folder_update_callback_id;
 #if USE_ENCHANT
         /* GNU/aspell spell checker */
         GtkAspell *gtkaspell;
@@ -342,6 +344,5 @@ void compose_clear_exit_drafts              (void);
 void compose_reopen_exit_drafts                (void);
 void compose_attach_from_list (Compose *compose, GList *file_list, gboolean free_data);
 void compose_check_for_email_account(Compose *compose);
-void compose_list_update_folders(FolderItem *old_item, FolderItem *new_item);
 
 #endif /* __COMPOSE_H__ */
index 52fc192..781b24c 100644 (file)
@@ -258,6 +258,7 @@ void folder_item_change_type(FolderItem *item, SpecialFolderItemType newtype)
        hookdata.folder = folder;
        hookdata.update_flags = FOLDER_TREE_CHANGED;
        hookdata.item = NULL;
+       hookdata.item2 = NULL;
        hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
 }
 
@@ -432,7 +433,6 @@ void folder_item_remove(FolderItem *item)
                }
        }
 
-       compose_list_update_folders(item, NULL);
        /* remove myself */
        if (item->cache != NULL) {
                msgcache_destroy(item->cache);
@@ -451,6 +451,7 @@ void folder_item_remove(FolderItem *item)
        hookdata.folder = item->folder;
        hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_REMOVE_FOLDERITEM;
        hookdata.item = item;
+       hookdata.item2 = NULL;
        hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
 
        node = start_node;
@@ -794,6 +795,7 @@ void folder_add(Folder *folder)
        hookdata.folder = folder;
        hookdata.update_flags = FOLDER_ADD_FOLDER;
        hookdata.item = NULL;
+       hookdata.item2 = NULL;
        hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
 }
 
@@ -808,6 +810,7 @@ void folder_remove(Folder *folder)
        hookdata.folder = folder;
        hookdata.update_flags = FOLDER_REMOVE_FOLDER;
        hookdata.item = NULL;
+       hookdata.item2 = NULL;
        hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
 }
 
@@ -948,6 +951,7 @@ void folder_scan_tree(Folder *folder, gboolean rebuild)
        hookdata.folder = folder;
        hookdata.update_flags = FOLDER_TREE_CHANGED;
        hookdata.item = NULL;
+       hookdata.item2 = NULL;
        hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
 
        if (rebuild)
@@ -980,6 +984,7 @@ FolderItem *folder_create_folder(FolderItem *parent, const gchar *name)
                hookdata.folder = new_item->folder;
                hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDERITEM;
                hookdata.item = new_item;
+               hookdata.item2 = NULL;
                hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
        }
 
@@ -1006,6 +1011,7 @@ gint folder_item_rename(FolderItem *item, gchar *newname)
 
                hookdata2.folder = item->folder;
                hookdata2.item = item;
+               hookdata2.item2 = NULL;
                hookdata2.update_flags = FOLDER_RENAME_FOLDERITEM;
                hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata2);
        }
@@ -3231,6 +3237,7 @@ static FolderItem *folder_item_move_recursive(FolderItem *src, FolderItem *dest,
        FolderItem *next_item;
        GNode *srcnode;
        gchar *old_id, *new_id;
+       FolderUpdateData hookdata;
 
        /* move messages */
        debug_print("%s %s to %s\n", copy?"Copying":"Moving", src->path, dest->path);
@@ -3293,7 +3300,11 @@ static FolderItem *folder_item_move_recursive(FolderItem *src, FolderItem *dest,
        old_id = folder_item_get_identifier(src);
        new_id = folder_item_get_identifier(new_item);
 
-       compose_list_update_folders(src, new_item);
+       hookdata.folder = src->folder;
+       hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_MOVE_FOLDERITEM;
+       hookdata.item = src;
+       hookdata.item2 = new_item;
+       hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
 
        /* if src supports removing, otherwise only copy folder */
        if (src->folder->klass->remove_folder != NULL && !copy) 
index b54c9f4..e0ad21a 100644 (file)
@@ -107,7 +107,8 @@ typedef enum
        FOLDER_TREE_CHANGED             = 1 << 2,
        FOLDER_ADD_FOLDERITEM           = 1 << 3,
        FOLDER_REMOVE_FOLDERITEM        = 1 << 4,
-       FOLDER_RENAME_FOLDERITEM        = 1 << 5
+       FOLDER_RENAME_FOLDERITEM        = 1 << 5,
+       FOLDER_MOVE_FOLDERITEM          = 1 << 6
 } FolderUpdateFlags;
 
 typedef enum
@@ -803,6 +804,7 @@ struct _FolderUpdateData
        Folder                  *folder;
        FolderUpdateFlags        update_flags;
        FolderItem              *item;
+       FolderItem              *item2;
 };
 
 struct _FolderItemUpdateData
index 209c617..5e55e91 100644 (file)
@@ -2709,6 +2709,8 @@ static gboolean folderview_update_folder(gpointer source, gpointer userdata)
                        if (folderview->opened == node)
                                folderview->opened = NULL;
                }
+       } else if (hookdata->update_flags & FOLDER_MOVE_FOLDERITEM) {
+               /* do nothing, it's done by the ADD and REMOVE) */
        } else if (hookdata->update_flags & (FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDER | FOLDER_REMOVE_FOLDER))
                folderview_set(folderview);
 
index c848853..74d82df 100644 (file)
@@ -226,6 +226,7 @@ static void subscribe_newsgroup_cb(GtkAction *action, gpointer data)
                hookdata.folder = newitem->folder;
                hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDERITEM;
                hookdata.item = newitem;
+               hookdata.item2 = NULL;
                hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
        }