Improved subject simplification by regexp.
[claws.git] / src / summaryview.c
index f6a6f6becd545134dcd83a7a864aeb770c1f3a37..bfdb8976645c7012f415a8dec239a3e19a0d88ae 100644 (file)
@@ -16,7 +16,6 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
-
 #include "defs.h"
 
 #include <glib.h>
@@ -46,6 +45,7 @@
 #include <ctype.h>
 #include <unistd.h>
 #include <sys/stat.h>
+#include <regex.h>
 
 #include "intl.h"
 #include "main.h"
@@ -65,6 +65,7 @@
 #include "compose.h"
 #include "utils.h"
 #include "gtkutils.h"
+#include "stock_pixmap.h"
 #include "filesel.h"
 #include "manage_window.h"
 #include "alertpanel.h"
 #include "scoring.h"
 #include "prefs_folder_item.h"
 #include "filtering.h"
-
-#include "pixmaps/dir-open.xpm"
-#include "pixmaps/mark.xpm"
-#include "pixmaps/deleted.xpm"
-#include "pixmaps/new.xpm"
-#include "pixmaps/unread.xpm"
-#include "pixmaps/replied.xpm"
-#include "pixmaps/forwarded.xpm"
-#include "pixmaps/clip.xpm"
-#include "pixmaps/ignorethread.xpm"
-#include "pixmaps/locked.xpm"
+#include "string_match.h"
 
 #define STATUSBAR_PUSH(mainwin, str) \
 { \
 #define SUMMARY_COL_MARK_WIDTH         10
 #define SUMMARY_COL_UNREAD_WIDTH       13
 #define SUMMARY_COL_LOCKED_WIDTH       13
-#define SUMMARY_COL_MIME_WIDTH         10
+#define SUMMARY_COL_MIME_WIDTH         11
 
 static GdkFont *boldfont;
 static GdkFont *smallfont;
@@ -120,9 +111,6 @@ static GtkStyle *small_style;
 static GtkStyle *small_marked_style;
 static GtkStyle *small_deleted_style;
 
-static GdkPixmap *folderxpm;
-static GdkBitmap *folderxpmmask;
-
 static GdkPixmap *markxpm;
 static GdkBitmap *markxpmmask;
 static GdkPixmap *deletedxpm;
@@ -143,6 +131,10 @@ static GdkBitmap *lockedxpmmask;
 
 static GdkPixmap *clipxpm;
 static GdkBitmap *clipxpmmask;
+static GdkPixmap *keyxpm;
+static GdkBitmap *keyxpmmask;
+static GdkPixmap *clipkeyxpm;
+static GdkBitmap *clipkeyxpmmask;
 
 static void summary_free_msginfo_func  (GtkCTree               *ctree,
                                         GtkCTreeNode           *node,
@@ -206,6 +198,7 @@ static void summary_display_msg             (SummaryView            *summaryview,
 static void summary_toggle_view                (SummaryView            *summaryview);
 static void summary_set_row_marks      (SummaryView            *summaryview,
                                         GtkCTreeNode           *row);
+static void summaryview_subject_filter_init (PrefsFolderItem    *prefs);
 
 /* message handling */
 static void summary_mark_row           (SummaryView            *summaryview,
@@ -254,6 +247,9 @@ static void summary_unthread_for_exec_func  (GtkCTree       *ctree,
                                                 GtkCTreeNode   *node,
                                                 gpointer        data);
 
+void summary_simplify_subject(SummaryView *summaryview, gchar * rexp,
+                             GSList * mlist);
+
 void summary_processing(SummaryView *summaryview, GSList * mlist);
 static void summary_filter_func                (GtkCTree               *ctree,
                                         GtkCTreeNode           *node,
@@ -534,21 +530,28 @@ void summary_init(SummaryView *summaryview)
        GtkStyle *style;
        GtkWidget *pixmap;
 
-       PIXMAP_CREATE(summaryview->ctree, markxpm, markxpmmask, mark_xpm);
-       PIXMAP_CREATE(summaryview->ctree, deletedxpm, deletedxpmmask,
-                     deleted_xpm);
-       PIXMAP_CREATE(summaryview->ctree, newxpm, newxpmmask, new_xpm);
-       PIXMAP_CREATE(summaryview->ctree, unreadxpm, unreadxpmmask, unread_xpm);
-       PIXMAP_CREATE(summaryview->ctree, repliedxpm, repliedxpmmask,
-                     replied_xpm);
-       PIXMAP_CREATE(summaryview->ctree, forwardedxpm, forwardedxpmmask,
-                     forwarded_xpm);
-       PIXMAP_CREATE(summaryview->ctree, ignorethreadxpm, ignorethreadxpmmask,
-                     ignorethread_xpm);
-       PIXMAP_CREATE(summaryview->ctree, lockedxpm, lockedxpmmask, locked_xpm);                      
-       PIXMAP_CREATE(summaryview->ctree, clipxpm, clipxpmmask, clip_xpm);
-       PIXMAP_CREATE(summaryview->hbox, folderxpm, folderxpmmask,
-                     dir_open_xpm);
+       stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_MARK,
+                        &markxpm, &markxpmmask);
+       stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_DELETED,
+                        &deletedxpm, &deletedxpmmask);
+       stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_NEW,
+                        &newxpm, &newxpmmask);
+       stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_UNREAD,
+                        &unreadxpm, &unreadxpmmask);
+       stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_REPLIED,
+                        &repliedxpm, &repliedxpmmask);
+       stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_FORWARDED,
+                        &forwardedxpm, &forwardedxpmmask);
+       stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_CLIP,
+                        &clipxpm, &clipxpmmask);
+       stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_LOCKED,
+                        &lockedxpm, &lockedxpmmask);
+       stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_IGNORETHREAD,
+                        &ignorethreadxpm, &ignorethreadxpmmask);
+       stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_CLIP_KEY,
+                        &clipkeyxpm, &clipkeyxpmmask);
+       stock_pixmap_gdk(summaryview->ctree, STOCK_PIXMAP_KEY,
+                        &keyxpm, &keyxpmmask);
 
        if (!small_style) {
                small_style = gtk_style_copy
@@ -584,7 +587,7 @@ void summary_init(SummaryView *summaryview)
        gtk_widget_set_style(summaryview->statlabel_select, style);
        gtk_widget_set_style(summaryview->statlabel_msgs, style);
 
-       pixmap = gtk_pixmap_new(folderxpm, folderxpmmask);
+       pixmap = stock_pixmap_widget(summaryview->hbox, STOCK_PIXMAP_DIR_OPEN);
        gtk_box_pack_start(GTK_BOX(summaryview->hbox), pixmap, FALSE, FALSE, 4);
        gtk_box_reorder_child(GTK_BOX(summaryview->hbox), pixmap, 0);
        gtk_widget_show(pixmap);
@@ -709,9 +712,8 @@ gboolean summary_show(SummaryView *summaryview, FolderItem *item,
                        return FALSE;
                }
                folder_update_op_count();
-       }
-        
-       summary_write_cache(summaryview);
+       } else
+               summary_write_cache(summaryview);
         
        summaryview->folderview->opened = selected_node;
 
@@ -1766,14 +1768,16 @@ static void summary_set_column_titles(SummaryView *summaryview)
                SORT_BY_FROM,
                SORT_BY_DATE,
                SORT_BY_SIZE,
-               SORT_BY_NUMBER
+               SORT_BY_NUMBER,
+               SORT_BY_SCORE,
+               SORT_BY_LOCKED
        };
 
        for (pos = 0; pos < N_SUMMARY_COLS; pos++) {
                type = summaryview->col_state[pos].type;
 
-               single_char = (type == S_COL_MIME ||
-                              type == S_COL_MARK || type == S_COL_UNREAD);
+               /* CLAWS: mime and unread are single char headers */
+               single_char = (type == S_COL_MIME || type == S_COL_UNREAD);
                justify = (type == S_COL_NUMBER || type == S_COL_SIZE)
                        ? GTK_JUSTIFY_RIGHT : GTK_JUSTIFY_LEFT;
 
@@ -1787,6 +1791,11 @@ static void summary_set_column_titles(SummaryView *summaryview)
                        else
                                title = col_label[type];
                        break;
+               /* CLAWS: dummies for mark and locked headers */        
+               case S_COL_MARK:        
+               case S_COL_LOCKED:
+                       title = "";
+                       break;
                default:
                        title = gettext(col_label[type]);
                }
@@ -1802,8 +1811,17 @@ static void summary_set_column_titles(SummaryView *summaryview)
                        continue;
                }
 
-               hbox = gtk_hbox_new(FALSE, 4);
-               label = gtk_label_new(title);
+               /* CLAWS: changed so that locked and mark headers
+                * show a pixmap instead of single character */
+               hbox  = gtk_hbox_new(FALSE, 4);
+               
+               if (type == S_COL_LOCKED)
+                       label = gtk_pixmap_new(lockedxpm, lockedxpmmask);
+               else if (type == S_COL_MARK) 
+                       label = gtk_pixmap_new(markxpm, markxpmmask);
+               else 
+                       label = gtk_label_new(title);
+               
                if (justify == GTK_JUSTIFY_RIGHT)
                        gtk_box_pack_end(GTK_BOX(hbox), label,
                                         FALSE, FALSE, 0);
@@ -1986,6 +2004,8 @@ static void summary_set_ctree_from_list(SummaryView *summaryview,
                        summaryview->important_score =
                                summaryview->folder_item->prefs->important_score;
        }
+
+       summaryview_subject_filter_init(summaryview->folder_item->prefs);
        
        if (summaryview->folder_item->threaded) {
                GNode *root, *gnode;
@@ -2168,6 +2188,8 @@ static void summary_set_header(SummaryView *summaryview, gchar *text[],
        static gchar *from_name = NULL;
        static gchar col_number[11];
        static gchar col_score[11];
+       static gchar buf[BUFFSIZE];
+       PrefsFolderItem *prefs = summaryview->folder_item->prefs;
        gint *col_pos = summaryview->col_pos;
 
        text[col_pos[S_COL_MARK]]   = NULL;
@@ -2227,8 +2249,16 @@ static void summary_set_header(SummaryView *summaryview, gchar *text[],
                }
        }
 
-       text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
-               _("(No Subject)");
+       if (prefs->enable_simplify_subject 
+           && prefs->simplify_subject_preg != NULL )
+               text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? 
+                       string_remove_match(buf, BUFFSIZE, msginfo->subject, 
+                                       prefs->simplify_subject_preg) : 
+                       
+                       _("(No Subject)");
+       else 
+               text[col_pos[S_COL_SUBJECT]] = msginfo->subject ? msginfo->subject :
+                       _("(No Subject)");
 }
 
 #define CHANGE_FLAGS(msginfo) \
@@ -2244,6 +2274,7 @@ static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row,
 {
        GtkCTree *ctree = GTK_CTREE(summaryview->ctree);
        MsgInfo *msginfo;
+       MsgFlags flags;
        gchar *filename;
 
        if (!new_window && summaryview->displayed == row) return;
@@ -2278,6 +2309,8 @@ static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row,
                summary_status_show(summaryview);
        }
 
+       flags = msginfo->flags;
+
        if (new_window) {
                MessageView *msgview;
 
@@ -2300,7 +2333,8 @@ static void summary_display_msg(SummaryView *summaryview, GtkCTreeNode *row,
                gtkut_ctree_node_move_if_on_the_edge(ctree, row);
        }
 
-       if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags)) {
+       if (MSG_IS_NEW(msginfo->flags) || MSG_IS_UNREAD(msginfo->flags) ||
+           (MSG_IS_MIME(msginfo->flags) - MSG_IS_MIME(flags) != 0)) {
                MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
                CHANGE_FLAGS(msginfo);
                summary_set_row_marks(summaryview, row);
@@ -2557,7 +2591,13 @@ static void summary_set_row_marks(SummaryView *summaryview, GtkCTreeNode *row)
                gtk_ctree_node_set_text(ctree, row, col_pos[S_COL_LOCKED], NULL);
        }
 
-       if (MSG_IS_MIME(flags)) {
+       if (MSG_IS_MIME(flags) && MSG_IS_ENCRYPTED(flags)) {
+               gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
+                                         clipkeyxpm, clipkeyxpmmask);
+       } else if (MSG_IS_ENCRYPTED(flags)) {
+               gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
+                                         keyxpm, keyxpmmask);
+       } else if (MSG_IS_MIME(flags)) {
                gtk_ctree_node_set_pixmap(ctree, row, col_pos[S_COL_MIME],
                                          clipxpm, clipxpmmask);
        } else {
@@ -4909,6 +4949,38 @@ static void summary_set_hide_read_msgs_menu (SummaryView *summaryview,
        gtk_object_set_data(GTK_OBJECT(widget), "dont_toggle",
                            GINT_TO_POINTER(0));
 }
+static void summaryview_subject_filter_init(PrefsFolderItem *prefs)
+{
+       int err;
+       gchar buf[BUFFSIZE];
+       if (prefs->enable_simplify_subject) {
+               if (prefs->simplify_subject_regexp && 
+                               *prefs->simplify_subject_regexp != 0x00) {
+
+                       if (!prefs->simplify_subject_preg) 
+                               prefs->simplify_subject_preg = g_new(regex_t, 1);
+                       else
+                               regfree(prefs->simplify_subject_preg);
+
+                       err = string_match_precompile(prefs->simplify_subject_regexp, 
+                                       prefs->simplify_subject_preg, REG_EXTENDED);
+                       if (err) {
+                               regerror(err, prefs->simplify_subject_preg, buf, BUFFSIZE);
+                               alertpanel_error(_("Regular expression (regexp) error:\n%s"), buf);
+                               g_free(prefs->simplify_subject_preg);
+                               prefs->simplify_subject_preg = NULL;
+                       }
+               } else {
+                       if (prefs->simplify_subject_preg) {
+                               regfree(prefs->simplify_subject_preg);
+                               g_free(prefs->simplify_subject_preg);
+                               prefs->simplify_subject_preg = NULL;
+                       }
+               }
+       }
+}
+
+
 /*
  * End of Source.
  */