Fix buffer clash (one already present, one added in 3.16.0-254-gb9c4e27),
[claws.git] / src / summaryview.c
index 60938efc35f43a81e695304b7e1176605c7773f6..d6f179ee7fbdc5369ef35777792f525bf9c75451 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,
@@ -1766,7 +1768,11 @@ void summary_set_menu_sensitive(SummaryView *summaryview)
        gboolean sensitive;
        gint i;
 
+#ifndef GENERIC_UMPC
 #define N_ENTRIES 39
+#else
+#define N_ENTRIES 28
+#endif
        static struct {
                const gchar *entry;
                SensitiveCondMask cond;
@@ -3358,7 +3364,8 @@ static inline void summary_set_header(SummaryView *summaryview, gchar *text[],
 {
        static gchar date_modified[80];
        static gchar col_score[11];
-       static gchar buf[BUFFSIZE], tmp1[BUFFSIZE], tmp2[BUFFSIZE], tmp3[BUFFSIZE];
+       static gchar from_buf[BUFFSIZE], to_buf[BUFFSIZE];
+       static gchar tmp1[BUFFSIZE], tmp2[BUFFSIZE], tmp3[BUFFSIZE];
        gint *col_pos = summaryview->col_pos;
        gchar *from_text = NULL, *to_text = NULL, *tags_text = NULL;
        gboolean should_swap = FALSE;
@@ -3448,9 +3455,9 @@ static inline void summary_set_header(SummaryView *summaryview, gchar *text[],
        } else {
                gchar *tmp = summary_complete_address(msginfo->from);
                if (tmp) {
-                       strncpy2(buf, tmp, sizeof(buf));
+                       strncpy2(from_buf, tmp, sizeof(from_buf));
                        g_free(tmp);
-                       from_text = buf;
+                       from_text = from_buf;
                } else {
                        if (prefs_common.summary_from_show == SHOW_NAME)
                                from_text = msginfo->fromname;
@@ -3468,20 +3475,38 @@ static inline void summary_set_header(SummaryView *summaryview, gchar *text[],
        
        to_text = msginfo->to ? msginfo->to : 
                   (msginfo->cc ? msginfo->cc :
-                    (msginfo->newsgroups ? msginfo->newsgroups : _("(No Recipient)")
+                    (msginfo->newsgroups ? msginfo->newsgroups : NULL
                     )
                   );
 
+       if (!to_text)
+               to_text = _("(No Recipient)");
+       else {
+               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 to_buf */
+                       if (tmp != NULL) {
+                               strncpy2(to_buf, tmp, sizeof(to_buf));
+                               g_free(tmp);
+                               to_text = to_buf;
+                       }
+               } else if (prefs_common.summary_from_show == SHOW_ADDR)
+                       extract_address(to_text);
+       }
+
        text[col_pos[S_COL_TO]] = to_text;
        if (!should_swap) {
                text[col_pos[S_COL_FROM]] = from_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 to_buf */
                        if (tmp) {
-                               strncpy2(buf, tmp, sizeof(buf));
+                               strncpy2(to_buf, tmp, sizeof(to_buf));
                                g_free(tmp);
-                               to_text = buf;
+                               to_text = to_buf;
                        } else {
                                to_text = to_text ? to_text : _("(No From)");
                        }
@@ -4129,6 +4154,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)
@@ -4148,6 +4178,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)
@@ -4193,29 +4228,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;
@@ -4232,29 +4274,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;
@@ -4377,7 +4426,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;
@@ -4465,7 +4514,7 @@ void summary_delete(SummaryView *summaryview)
 
        if (!prefs_common.live_dangerously) {
                gchar *buf = NULL;
-               int num = g_list_length(GTK_CMCLIST(summaryview->ctree)->selection);
+               guint num = g_list_length(GTK_CMCLIST(summaryview->ctree)->selection);
                buf = g_strdup_printf(ngettext(
                        "Do you really want to delete the selected message?",
                        "Do you really want to delete the %d selected messages?", num),
@@ -4819,11 +4868,26 @@ void summary_add_address(SummaryView *summaryview)
 
 void summary_select_all(SummaryView *summaryview)
 {
+       GtkCMCTreeNode *node;
+
        if (!summaryview->folder_item) return;
 
+       if (GTK_CMCLIST(summaryview->ctree)->focus_row < 0) {
+               /* If no row is selected, select (but do not open) the first
+                * row, to get summaryview into correct state for selecting all. */
+               debug_print("summary_select_all: no row selected, selecting first one\n");
+               if (GTK_CMCLIST(summaryview->ctree)->row_list != NULL) {
+                       node = gtk_cmctree_node_nth(GTK_CMCTREE(summaryview->ctree), 0);
+                       summary_select_node(summaryview, node, FALSE);
+               }
+       }
+
+       /* Now select all rows while locking the summaryview for
+        * faster performance. */
        summary_lock(summaryview);
        gtk_cmclist_select_all(GTK_CMCLIST(summaryview->ctree));
        summary_unlock(summaryview);
+
        summary_status_show(summaryview);
 }
 
@@ -5858,7 +5922,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)
 {
@@ -5919,6 +5983,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,
@@ -7189,7 +7301,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);