Fix a memory leak in summary_set_header().
[claws.git] / src / summaryview.c
index cb3d1259e513f539146473eb8672150a07c216ed..306972c4f2195f113e5d183207ebd0f7d2ae4556 100644 (file)
@@ -193,6 +193,8 @@ static void summary_mark_row_as_read        (SummaryView            *summaryview,
                                         GtkCMCTreeNode         *row);
 static void summary_mark_row_as_unread (SummaryView            *summaryview,
                                         GtkCMCTreeNode         *row);
+static gboolean summary_mark_all_read_confirm(gboolean ask_if_needed);
+static gboolean summary_mark_all_unread_confirm(gboolean ask_if_needed);
 static void summary_delete_row         (SummaryView            *summaryview,
                                         GtkCMCTreeNode         *row);
 static void summary_unmark_row         (SummaryView            *summaryview,
@@ -3479,9 +3481,16 @@ static inline void summary_set_header(SummaryView *summaryview, gchar *text[],
        if (!to_text)
                to_text = _("(No Recipient)");
        else {
-               if (prefs_common.summary_from_show == SHOW_NAME)
-                       to_text = procheader_get_fromname(to_text);
-               else if (prefs_common.summary_from_show == SHOW_ADDR)
+               if (prefs_common.summary_from_show == SHOW_NAME) {
+                       gchar *tmp = procheader_get_fromname(to_text);
+                       /* need to keep to_text pointing to stack, so heap-allocated
+                        * string from procheader_get_fromname() will be copied to buf */
+                       if (tmp != NULL) {
+                               strncpy2(buf, tmp, sizeof(buf));
+                               g_free(tmp);
+                               to_text = buf;
+                       }
+               } else if (prefs_common.summary_from_show == SHOW_ADDR)
                        extract_address(to_text);
        }
 
@@ -3491,6 +3500,8 @@ static inline void summary_set_header(SummaryView *summaryview, gchar *text[],
        } else {
                if (prefs_common.use_addr_book) {
                        gchar *tmp = summary_complete_address(to_text);
+                       /* need to keep to_text pointing to stack, so heap-allocated
+                        * string from summary_complete_address() will be copied to buf */
                        if (tmp) {
                                strncpy2(buf, tmp, sizeof(buf));
                                g_free(tmp);
@@ -4142,6 +4153,11 @@ void summary_mark_as_read(SummaryView *summaryview)
 
        if (summary_is_locked(summaryview))
                return;
+
+       if ((summaryview->folder_item->total_msgs == (gint)g_list_length(GTK_CMCLIST(ctree)->selection))
+                && !summary_mark_all_read_confirm(TRUE))
+               return;
+
        START_LONG_OPERATION(summaryview, FALSE);
        folder_item_set_batch(summaryview->folder_item, TRUE);
        for (cur = GTK_CMCLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
@@ -4161,6 +4177,11 @@ void summary_mark_as_unread(SummaryView *summaryview)
 
        if (summary_is_locked(summaryview))
                return;
+
+       if ((summaryview->folder_item->total_msgs == (gint)g_list_length(GTK_CMCLIST(ctree)->selection))
+                && !summary_mark_all_unread_confirm(TRUE))
+               return;
+
        START_LONG_OPERATION(summaryview, FALSE);
        folder_item_set_batch(summaryview->folder_item, TRUE);
        for (cur = GTK_CMCLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
@@ -4206,29 +4227,36 @@ void summary_msgs_unlock(SummaryView *summaryview)
        summary_status_show(summaryview);
 }
 
-void summary_mark_all_read(SummaryView *summaryview, gboolean ask_if_needed)
+static gboolean summary_mark_all_read_confirm(gboolean ask_if_needed)
 {
-       GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
-       GtkCMCTreeNode *node;
-       AlertValue val;
-       gboolean froze = FALSE;
-
        /* ask_if_needed is FALSE when user-asking is performed by caller,
           commonly when the caller is a mark-as-read-recursive func */
        if (ask_if_needed && prefs_common.ask_mark_all_read) {
-               val = alertpanel_full(_("Mark all as read"),
+               AlertValue val = alertpanel_full(_("Mark all as read"),
                          _("Do you really want to mark all mails in this folder as read?"),
                          GTK_STOCK_NO, GTK_STOCK_YES, NULL, ALERTFOCUS_FIRST,
                          TRUE, NULL, ALERT_QUESTION);
 
                if ((val & ~G_ALERTDISABLE) != G_ALERTALTERNATE)
-                       return;
+                       return FALSE;
                else if (val & G_ALERTDISABLE)
                        prefs_common.ask_mark_all_read = FALSE;
        }
+       return TRUE;
+}
+
+void summary_mark_all_read(SummaryView *summaryview, gboolean ask_if_needed)
+{
+       GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
+       GtkCMCTreeNode *node;
+       gboolean froze = FALSE;
        
        if (summary_is_locked(summaryview))
                return;
+
+       if (!summary_mark_all_read_confirm(ask_if_needed))
+               return;
+
        START_LONG_OPERATION(summaryview, TRUE);
        folder_item_set_batch(summaryview->folder_item, TRUE);
        for (node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list); node != NULL;
@@ -4245,29 +4273,36 @@ void summary_mark_all_read(SummaryView *summaryview, gboolean ask_if_needed)
        summary_status_show(summaryview);
 }
 
-void summary_mark_all_unread(SummaryView *summaryview, gboolean ask_if_needed)
+static gboolean summary_mark_all_unread_confirm(gboolean ask_if_needed)
 {
-       GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
-       GtkCMCTreeNode *node;
-       AlertValue val;
-       gboolean froze = FALSE;
-
        /* ask_if_needed is FALSE when user-asking is performed by caller,
           commonly when the caller is a mark-as-unread-recursive func */
        if (ask_if_needed && prefs_common.ask_mark_all_read) {
-               val = alertpanel_full(_("Mark all as unread"),
+               AlertValue val = alertpanel_full(_("Mark all as unread"),
                          _("Do you really want to mark all mails in this folder as unread?"),
                          GTK_STOCK_NO, GTK_STOCK_YES, NULL, ALERTFOCUS_FIRST,
                          TRUE, NULL, ALERT_QUESTION);
 
                if ((val & ~G_ALERTDISABLE) != G_ALERTALTERNATE)
-                       return;
+                       return FALSE;
                else if (val & G_ALERTDISABLE)
                        prefs_common.ask_mark_all_read = FALSE;
        }
+       return TRUE;
+}
+
+void summary_mark_all_unread(SummaryView *summaryview, gboolean ask_if_needed)
+{
+       GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
+       GtkCMCTreeNode *node;
+       gboolean froze = FALSE;
        
        if (summary_is_locked(summaryview))
                return;
+
+       if (!summary_mark_all_unread_confirm(ask_if_needed))
+               return;
+
        START_LONG_OPERATION(summaryview, TRUE);
        folder_item_set_batch(summaryview->folder_item, TRUE);
        for (node = GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list); node != NULL;
@@ -4390,7 +4425,7 @@ static gboolean check_permission(SummaryView *summaryview, MsgInfo * msginfo)
                }
 
                if (!found) {
-                       alertpanel_error(_("You're not the author of the article.\n"));
+                       alertpanel_error(_("You're not the author of the article."));
                }
                
                return found;
@@ -5886,7 +5921,7 @@ static void summary_colorlabel_menu_item_activate_cb(GtkWidget *widget,
 }
 
 /* summary_set_colorlabel_color() - labelcolor parameter is the color *flag*
- * for the messsage; not the color index */
+ * for the message; not the color index */
 void summary_set_colorlabel_color(GtkCMCTree *ctree, GtkCMCTreeNode *node,
                                  guint labelcolor)
 {
@@ -5947,6 +5982,54 @@ void summary_set_colorlabel(SummaryView *summaryview, guint labelcolor,
        GList *cur;
        gboolean froze = FALSE;
 
+       if (prefs_common.ask_override_colorlabel) {
+               GtkCMCTree *ctree = GTK_CMCTREE(summaryview->ctree);
+               gboolean ask = FALSE;
+               AlertValue val;
+               guint color;
+               gboolean already_this_color_everywhere = TRUE;
+
+               /* if clearing color labels (applying 'none', 0):
+                   - ask if at least one message has a non-0 color label set
+                  if applying a non-0 color label:
+                   - ask if at least one of the selected messages has a non-0 color label different
+                         from the one we want to apply.
+                   - don't ask if all messages have the same color label as the one we're applying
+               */
+               for (cur = GTK_CMCLIST(ctree)->selection;
+                        !ask && cur != NULL && cur->data != NULL;
+                        cur = cur->next) {
+                       MsgInfo *msginfo = gtk_cmctree_node_get_row_data(ctree, GTK_CMCTREE_NODE(cur->data));
+                       if (msginfo) {
+                               color = MSG_GET_COLORLABEL_VALUE(msginfo->flags);
+                               if (labelcolor == 0) {
+                                       /* clearing color labels */
+                                       ask = (color != 0);
+                               } else {
+                                       already_this_color_everywhere &= (color == labelcolor);
+                                       ask = ((color != 0) && (color != labelcolor)) && !already_this_color_everywhere;
+                               }
+                       }
+               }
+
+               if (ask) {
+                       gchar *msg;
+
+                       if (labelcolor == 0)
+                               msg = _("Do you really want to reset the color label of all selected messages?");
+                       else
+                               msg = _("Do you really want to apply this color label to all selected messages?");
+                       val = alertpanel_full(_("Set color label"), msg,
+                                 GTK_STOCK_NO, GTK_STOCK_YES, NULL, ALERTFOCUS_FIRST,
+                                 TRUE, NULL, ALERT_QUESTION);
+
+                       if ((val & ~G_ALERTDISABLE) != G_ALERTALTERNATE)
+                               return;
+                       else if (val & G_ALERTDISABLE)
+                               prefs_common.ask_override_colorlabel = FALSE;
+               }
+       }
+
        START_LONG_OPERATION(summaryview, FALSE);
        for (cur = GTK_CMCLIST(ctree)->selection; cur != NULL && cur->data != NULL; cur = cur->next)
                summary_set_row_colorlabel(summaryview,
@@ -7217,7 +7300,7 @@ static void summary_sort_by_column_click(SummaryView *summaryview,
                             summaryview->sort_type == SORT_ASCENDING
                             ? SORT_DESCENDING : SORT_ASCENDING);
        else
-               summary_sort(summaryview, sort_key, SORT_ASCENDING);
+               summary_sort(summaryview, sort_key, summaryview->sort_type);
 
        node = GTK_CMCTREE_NODE(GTK_CMCLIST(summaryview->ctree)->row_list);