updated selective download
authorPaul Mangan <paul@claws-mail.org>
Mon, 10 Jun 2002 21:55:17 +0000 (21:55 +0000)
committerPaul Mangan <paul@claws-mail.org>
Mon, 10 Jun 2002 21:55:17 +0000 (21:55 +0000)
ChangeLog.claws
configure.in
src/inc.c
src/inc.h
src/pop.c
src/prefs_account.c
src/prefs_account.h
src/selective_download.c
src/selective_download.h

index ddcac41882dd93a32628c206bfc35f5eb9fcb5b4..05e67c67096abab2a55d92847e9086f148140c0c 100644 (file)
@@ -1,3 +1,32 @@
+2002-06-10 [paul]      0.7.6claws37
+
+       * src/inc.[ch]
+         src/pop.c
+         src/prefs_account.[ch]
+         src/selective_download.[ch]
+               apply PATCH #561712
+                NEW:
+               - check if POP Account
+               - Receive Button renamed to Preview
+               - Preview Button Popup: preview new/all messages
+               - Download Button
+               - Account Preferences-->Receive tab checkbutton if you
+                 want to remove Mail after Download
+               -: default YES
+               - Account Preferences-->Receive tab checkbutton use
+                 filtering rules on preview
+                 (think of delete_on_server)
+               -: default YES
+               - Account selectable from SD dialog
+               - checkbutton to show already downloaded messages if
+                 all messages are previewed
+               - new clist column showing Date
+               - ESC KEY close
+               FIXES:
+               - play ball with UIDL
+               - fault tolerant (?) 
+               Patch submitted by Oliver Haertel <oha@users.sourceforge.net>
+
 2002-06-10 [melvin]    0.7.6claws36
 
        * README.claws
@@ -10,8 +39,8 @@
 2002-06-10 [melvin]    0.7.6claws35
        
        * src/prefs_actions.c
-               Made it possible to use a more elaborate command line
-               (e.g. |foo|bar|cat>there;echo Done|).
+               Make it possible to use a more elaborate command line
+               (e.g. |foo|bar|cat>there;echo Done|)
                Corrected error message when calling %f, %F and %p commands
                from the compose window.
 
index 72f1b06a904e26fb7fb9e8bc5479e6b71cca7fab..9ad2ac8b47ea344ed8fa1c0879c24ef0b9fba98d 100644 (file)
@@ -8,7 +8,7 @@ MINOR_VERSION=7
 MICRO_VERSION=6
 INTERFACE_AGE=0
 BINARY_AGE=0
-EXTRA_VERSION=claws36
+EXTRA_VERSION=claws37
 VERSION=$MAJOR_VERSION.$MINOR_VERSION.$MICRO_VERSION$EXTRA_VERSION
 
 dnl set $target
index 75d56c74b752c492c6bcfda5616c45309d32c5e3..b3170495769b2a51631d7a19a3fe42c65c0f1486 100644 (file)
--- a/src/inc.c
+++ b/src/inc.c
@@ -199,16 +199,28 @@ void inc_mail(MainWindow *mainwin, gboolean notify)
        inc_autocheck_timer_set();
 }
 
-gint inc_selective_download(MainWindow *mainwin, gint session_type)
+void inc_selective_download(MainWindow *mainwin, PrefsAccount *acc, gint session)
 {
-       PrefsAccount *account = cur_account;
-       gint new_msgs = 0;      
+       GSList *cur;
+       gint new_msgs = 0;
 
-       account->session_type = session_type;
-       new_msgs = inc_account_mail(account, mainwin);
-       account->session_type = RETR_NORMAL;
+       acc->session = session;
+       inc_account_mail(acc, mainwin);
+       acc->session = STYPE_NORMAL;
        
-       return new_msgs;
+       for (cur = acc->msg_list; cur != NULL; cur = cur->next) {
+               HeaderItems *items =(HeaderItems*)cur->data;
+
+               if (items->state == SD_DOWNLOADED && 
+                   items->del_by_old_session == FALSE) {
+                       new_msgs++;                     
+               }
+       }
+
+       if (new_msgs) {
+               inc_finished(mainwin, TRUE);
+               inc_notify_cmd(new_msgs, prefs_common.newmail_notify_manu);
+       }
 }
 
 static gint inc_account_mail(PrefsAccount *account, MainWindow *mainwin)
@@ -295,7 +307,7 @@ void inc_all_account_mail(MainWindow *mainwin, gboolean notify)
        for (list = account_get_list(); list != NULL; list = list->next) {
                IncSession *session;
                PrefsAccount *account = list->data;
-               account->session_type = RETR_NORMAL;
+               account->session = STYPE_NORMAL;
                if (account->recv_at_getall) {
                        session = inc_session_new(account);
                        if (session)
@@ -745,6 +757,13 @@ static IncState inc_pop3_session_do(IncSession *session)
        atm->help_sock = sockinfo;
 
        log_verbosity_set(TRUE);
+       /* oha: this messes up inc_progress update:
+          disabling this would avoid the label "Retrieve Header"
+          being overwritten by "Retrieve Message"
+          Setting inc_pop3_recv_func is not necessary
+          since atm already handles the progress dialog ui
+          just fine.
+       */
        recv_set_ui_func(inc_pop3_recv_func, session);
 
        atm->tag = sock_gdk_input_add(sockinfo,
@@ -755,6 +774,7 @@ static IncState inc_pop3_session_do(IncSession *session)
                gtk_main_iteration();
 
        log_verbosity_set(FALSE);
+       /* oha: see above */
        recv_set_ui_func(NULL, NULL);
 
        automaton_destroy(atm);
index 76f643c7444c791636d741c57faad71390b4f59a..2eaf3cdae4dab769b0f22fa10bdeffbb400610cb 100644 (file)
--- a/src/inc.h
+++ b/src/inc.h
@@ -105,8 +105,9 @@ void inc_mail                       (MainWindow     *mainwin,
                                 gboolean notify);
 void inc_all_account_mail      (MainWindow     *mainwin,
                                 gboolean notify);
-gint inc_selective_download     (MainWindow    *mainwin, 
-                                gint           session_type);
+void inc_selective_download     (MainWindow    *mainwin, 
+                                PrefsAccount *acc,
+                                gint         session);
 void inc_progress_update       (Pop3State      *state,
                                 Pop3Phase       phase);
 gint inc_drop_message          (const gchar    *file,
index 0951490b5b6d9b79a13c554cef432397273e202f..e9326525f7a7a6717b9fc70a4826df0cecae0187 100644 (file)
--- a/src/pop.c
+++ b/src/pop.c
@@ -68,7 +68,9 @@
 static gint pop3_ok(SockInfo *sock, gchar *argbuf);
 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...);
 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size);
-static gboolean pop3_delete_header (Pop3State *state);
+static gboolean pop3_sd_get_next (Pop3State *state);
+static void pop3_sd_new_header(Pop3State *state);
+gboolean pop3_sd_state(Pop3State *state, gint cur_state, guint *next_state);
 static gboolean should_delete (const char *uidl, gpointer data); 
 
 gint pop3_greeting_recv(SockInfo *sock, gpointer data)
@@ -162,13 +164,9 @@ gint pop3_getauth_pass_recv(SockInfo *sock, gpointer data)
 {
        Pop3State *state = (Pop3State *)data;
 
-       if (pop3_ok(sock, NULL) == PS_SUCCESS) {
-               
-               if (pop3_delete_header(state) == TRUE)
-                       return POP3_DELETE_SEND;
-               else
-                       return POP3_GETRANGE_STAT_SEND;
-       }
+       if (pop3_ok(sock, NULL) == PS_SUCCESS) 
+               return POP3_GETRANGE_STAT_SEND;
+       
        else {
                log_warning(_("error occurred on authentication\n"));
                state->error_val = PS_AUTHFAIL;
@@ -213,13 +211,8 @@ gint pop3_getauth_apop_recv(SockInfo *sock, gpointer data)
 {
        Pop3State *state = (Pop3State *)data;
 
-       if (pop3_ok(sock, NULL) == PS_SUCCESS) {
-               
-               if (pop3_delete_header(state) == TRUE)
-                       return POP3_DELETE_SEND;
-               else
+       if (pop3_ok(sock, NULL) == PS_SUCCESS) 
                return POP3_GETRANGE_STAT_SEND;
-       }
 
        else {
                log_warning(_("error occurred on authentication\n"));
@@ -308,6 +301,7 @@ gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
        gboolean get_all = FALSE;
        gchar buf[POPBUFSIZE];
        gchar id[IDLEN + 1];
+       gint next_state;
 
        if (!state->uidl_table) new = TRUE;
        if (state->ac_prefs->getall)
@@ -315,6 +309,9 @@ gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
 
        if (pop3_ok(sock, NULL) != PS_SUCCESS) {
                /* UIDL is not supported */
+               if (pop3_sd_state(state, POP3_GETRANGE_UIDL_RECV, &next_state))
+                       return next_state;
+
                if (!get_all)
                        return POP3_GETRANGE_LAST_SEND;
                else
@@ -351,6 +348,9 @@ gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
 
        state->uidl_is_valid = TRUE;
 
+       if (pop3_sd_state(state, POP3_GETRANGE_UIDL_RECV, &next_state))
+               return next_state;
+
        if (new == TRUE)
                return POP3_GETSIZE_LIST_SEND;
        else
@@ -408,6 +408,7 @@ gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
 {
        Pop3State *state = (Pop3State *)data;
        gchar buf[POPBUFSIZE];
+       gint next_state;
 
        if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
 
@@ -426,12 +427,12 @@ gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
                        state->cur_total_bytes += size;
        }
 
-       LOOKUP_NEXT_MSG();
-       if (state->ac_prefs->session_type == RETR_HEADER) 
-               return POP3_TOP_SEND;
-       else
-               return POP3_RETR_SEND;
- }
+       if (pop3_sd_state(state, POP3_GETSIZE_LIST_RECV, &next_state))
+               return next_state;
+
+       LOOKUP_NEXT_MSG();      
+       return POP3_RETR_SEND;
+}
  
 gint pop3_top_send(SockInfo *sock, gpointer data)
 {
@@ -447,13 +448,9 @@ gint pop3_top_send(SockInfo *sock, gpointer data)
 gint pop3_top_recv(SockInfo *sock, gpointer data)
 {
        Pop3State *state = (Pop3State *)data;
-       FILE  *fp;
-       gchar buf[POPBUFSIZE];
-       gchar *header;
        gchar *filename, *path;
+       gint next_state;
        
-       inc_progress_update(state, POP3_TOP_RECV); 
-
        if (pop3_ok(sock, NULL) != PS_SUCCESS) 
                return POP3_LOGOUT_SEND;
 
@@ -468,21 +465,9 @@ gint pop3_top_recv(SockInfo *sock, gpointer data)
                state->inc_state = INC_NOSPACE;
                return -1;
        }
-       /* we add a Complete-Size Header Item ...
-          note: overwrites first line  --> this is dirty */
-       if ( (fp = fopen(filename, "rb+")) != NULL ) {
-               gchar *buf = g_strdup_printf("%s%i", SIZE_HEADER, 
-                                            state->msg[state->cur_msg].size);
-       
-               if (change_file_mode_rw(fp, filename) == 0) 
-                       fprintf(fp, "%s\n", buf);
-               g_free(buf);    
-               fclose(fp);
-       }
-       
-       g_free(path);
-       g_free(filename);
 
+       pop3_sd_state(state, POP3_TOP_RECV, &next_state);
+       
        if (state->cur_msg < state->count) {
                state->cur_msg++;
                return POP3_TOP_SEND;
@@ -504,6 +489,7 @@ gint pop3_retr_recv(SockInfo *sock, gpointer data)
        Pop3State *state = (Pop3State *)data;
        const gchar *file;
        gint ok, drop_ok;
+       gint next_state;
        int keep_for;
        
        if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
@@ -517,6 +503,9 @@ gint pop3_retr_recv(SockInfo *sock, gpointer data)
                        state->inc_state = INC_ERROR;
                        return -1;
                }
+
+               if (pop3_sd_state(state, POP3_RETR_RECV, &next_state))
+                       return next_state;
        
                state->cur_total_bytes += state->msg[state->cur_msg].size;
                state->cur_total_num++;
@@ -553,14 +542,15 @@ gint pop3_delete_send(SockInfo *sock, gpointer data)
 gint pop3_delete_recv(SockInfo *sock, gpointer data)
 {
        Pop3State *state = (Pop3State *)data;
+       gint next_state;
        gint ok;
 
        if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
-               if (state->ac_prefs->session_type == RETR_NORMAL)
-                       state->msg[state->cur_msg].deleted = TRUE;
 
-               if (pop3_delete_header(state) == TRUE) 
-                       return POP3_DELETE_SEND;
+               state->msg[state->cur_msg].deleted = TRUE;
+               
+               if (pop3_sd_state(state, POP3_DELETE_RECV, &next_state))
+                       return next_state;      
 
                if (state->cur_msg < state->count) {
                        state->cur_msg++;
@@ -677,17 +667,147 @@ static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
        }
 }
 
-gboolean pop3_delete_header (Pop3State *state)
+static void pop3_sd_new_header(Pop3State *state)
 {
-       
-       if ( (state->ac_prefs->session_type == DELE_HEADER) &&
-            (g_slist_length(state->ac_prefs->to_delete) > 0) ) {
+       HeaderItems *new_msg;
+       if (state->cur_msg <= state->count) {
+               new_msg = g_new0(HeaderItems, 1); 
+               
+               new_msg->index              = state->cur_msg;
+               new_msg->state              = SD_UNCHECKED;
+               new_msg->size               = state->msg[state->cur_msg].size; 
+               new_msg->received           = state->msg[state->cur_msg].received;
+               new_msg->del_by_old_session = FALSE;
+               
+               state->ac_prefs->msg_list = g_slist_append(state->ac_prefs->msg_list, 
+                                                          new_msg);
+       }
+}
+
+gboolean pop3_sd_state(Pop3State *state, gint cur_state, guint *next_state) 
+{
+       gint session = state->ac_prefs->session;
+       guint goto_state = -1;
+
+       switch (cur_state) { 
+       case POP3_GETRANGE_UIDL_RECV:
+               switch (session) {
+               case STYPE_DOWNLOAD:
+               case STYPE_DELETE:
+               case STYPE_PREVIEW_ALL:
+                       goto_state = POP3_GETSIZE_LIST_SEND;
+               default:
+                       break;
+               }
+               break;
+       case POP3_GETSIZE_LIST_RECV:
+               switch (session) {
+               case STYPE_PREVIEW_ALL:
+                       state->cur_msg = 1;
+               case STYPE_PREVIEW_NEW:
+                       goto_state = POP3_TOP_SEND;
+                       break;
+               case STYPE_DELETE:
+                       if (pop3_sd_get_next(state))
+                               goto_state = POP3_DELETE_SEND;          
+                       else
+                               goto_state = POP3_LOGOUT_SEND;
+                       break;
+               case STYPE_DOWNLOAD:
+                       if (pop3_sd_get_next(state))
+                               goto_state = POP3_RETR_SEND;
+                       else
+                               goto_state = POP3_LOGOUT_SEND;
+               default:
+                       break;
+               }
+               break;
+       case POP3_TOP_RECV: 
+               switch (session) { 
+               case STYPE_PREVIEW_ALL:
+               case STYPE_PREVIEW_NEW:
+                       pop3_sd_new_header(state);
+               default:
+                       break;
+               }
+               break;
+       case POP3_RETR_RECV:
+               switch (session) {
+               case STYPE_DOWNLOAD:
+                       if (state->ac_prefs->sd_rmmail_on_download) 
+                               goto_state = POP3_DELETE_SEND;
+                       else {
+                               if (pop3_sd_get_next(state)) 
+                                       goto_state = POP3_RETR_SEND;
+                               else
+                                       goto_state = POP3_LOGOUT_SEND;
+                       }
+               default:        
+                       break;
+               }
+               break;
+       case POP3_DELETE_RECV:
+               switch (session) {
+               case STYPE_DELETE:
+                       if (pop3_sd_get_next(state)) 
+                               goto_state = POP3_DELETE_SEND;
+                       else
+                               goto_state =  POP3_LOGOUT_SEND;
+                       break;
+               case STYPE_DOWNLOAD:
+                       if (pop3_sd_get_next(state)) 
+                               goto_state = POP3_RETR_SEND;
+                       else
+                               goto_state = POP3_LOGOUT_SEND;
+               default:
+                       break;
+               }
+       default:
+               break;
+               
+       }                 
 
-               state->cur_msg = (gint) state->ac_prefs->to_delete->data;
-               debug_print(_("next to delete %i\n"), state->cur_msg);
-               state->ac_prefs->to_delete = g_slist_remove 
-                       (state->ac_prefs->to_delete, state->ac_prefs->to_delete->data);
+       *next_state = goto_state;
+       if (goto_state != -1)
                return TRUE;
+       else 
+               return FALSE;
+}
+
+gboolean pop3_sd_get_next(Pop3State *state)
+{
+       GSList *cur;
+       gint deleted_msgs = 0;
+       
+       switch (state->ac_prefs->session) {
+       case STYPE_DOWNLOAD:
+       case STYPE_DELETE:      
+               for (cur = state->ac_prefs->msg_list; cur != NULL; cur = cur->next) {
+                       HeaderItems *items = (HeaderItems*)cur->data;
+
+                       if (items->del_by_old_session)
+                               deleted_msgs++;
+
+                       switch (items->state) {
+                       case SD_REMOVE:
+                               items->state = SD_REMOVED;
+                               break;
+                       case SD_DOWNLOAD:
+                               items->state = SD_DOWNLOADED;
+                               break;
+                       case SD_CHECKED:
+                               state->cur_msg = items->index - deleted_msgs;
+                               if (state->ac_prefs->session == STYPE_DELETE)
+                                       items->state = SD_REMOVE;
+                               else
+                                       items->state = SD_DOWNLOAD;
+                               return TRUE;
+                       default:
+                               break;
+                       }
+               }
+               return FALSE;
+       default:
+               return FALSE;
        }
-       return FALSE;
 }
index f3eb9812d77aed23c925b2386d51a58026e6f775..458f4ae827197c53592cbab1f958f52f3d15799a 100644 (file)
@@ -88,6 +88,8 @@ static struct Receive {
        GtkWidget *rmmail_chkbtn;
        GtkWidget *leave_time_entry;
        GtkWidget *getall_chkbtn;
+       GtkWidget *sd_filter_on_recv_chkbtn;
+       GtkWidget *sd_rmmail_chkbtn;
        GtkWidget *size_limit_chkbtn;
        GtkWidget *size_limit_entry;
        GtkWidget *filter_on_recv_chkbtn;
@@ -285,6 +287,14 @@ static PrefParam param[] = {
         &receive.filter_on_recv_chkbtn,
         prefs_set_data_from_toggle, prefs_set_toggle},
 
+       /* selective download */        
+       {"sd_filter_on_receive", "TRUE", &tmp_ac_prefs.sd_filter_on_recv, P_BOOL,
+        &receive.sd_filter_on_recv_chkbtn,
+        prefs_set_data_from_toggle, prefs_set_toggle},
+       {"sd_remove_mail_on_download", "TRUE", &tmp_ac_prefs.sd_rmmail_on_download, P_BOOL,
+        &receive.sd_rmmail_chkbtn,
+        prefs_set_data_from_toggle, prefs_set_toggle},
+
        {"receive_at_get_all", "TRUE", &tmp_ac_prefs.recv_at_getall, P_BOOL,
         &receive.recvatgetall_chkbtn,
         prefs_set_data_from_toggle, prefs_set_toggle},
@@ -1036,6 +1046,8 @@ static void prefs_account_receive_create(void)
        GtkWidget *size_limit_entry;
        GtkWidget *label;
        GtkWidget *filter_on_recv_chkbtn;
+       GtkWidget *sd_filter_on_recv_chkbtn;
+       GtkWidget *sd_rmmail_chkbtn;
        GtkWidget *vbox3;
        GtkWidget *hbox2;
        GtkWidget *inbox_label;
@@ -1080,6 +1092,10 @@ static void prefs_account_receive_create(void)
                
        PACK_CHECK_BUTTON (vbox2, getall_chkbtn,
                           _("Download all messages on server"));
+       PACK_CHECK_BUTTON (vbox2, sd_filter_on_recv_chkbtn,
+                          _("use filtering rules with Selective Donwload"));
+       PACK_CHECK_BUTTON (vbox2, sd_rmmail_chkbtn,
+                          _("Remove Mail after downloading with Selective Download"));
 
        hbox1 = gtk_hbox_new (FALSE, 8);
        gtk_widget_show (hbox1);
@@ -1139,18 +1155,20 @@ static void prefs_account_receive_create(void)
                (vbox1, recvatgetall_chkbtn,
                 _("`Get all' checks for new messages on this account"));
 
-       receive.pop3_frame            = frame1;
-       receive.rmmail_chkbtn         = rmmail_chkbtn;
-       receive.leave_time_entry      = leave_time_entry;
-       receive.getall_chkbtn         = getall_chkbtn;
-       receive.size_limit_chkbtn     = size_limit_chkbtn;
-       receive.size_limit_entry      = size_limit_entry;
-       receive.filter_on_recv_chkbtn = filter_on_recv_chkbtn;
-       receive.inbox_label           = inbox_label;
-       receive.inbox_entry           = inbox_entry;
-       receive.inbox_btn             = inbox_btn;
-
-       receive.recvatgetall_chkbtn   = recvatgetall_chkbtn;
+       receive.pop3_frame               = frame1;
+       receive.rmmail_chkbtn            = rmmail_chkbtn;
+       receive.leave_time_entry         = leave_time_entry;
+       receive.getall_chkbtn            = getall_chkbtn;
+       receive.size_limit_chkbtn        = size_limit_chkbtn;
+       receive.size_limit_entry         = size_limit_entry;
+       receive.filter_on_recv_chkbtn    = filter_on_recv_chkbtn;
+       receive.sd_filter_on_recv_chkbtn = sd_filter_on_recv_chkbtn;
+       receive.sd_rmmail_chkbtn         = sd_rmmail_chkbtn;
+       receive.inbox_label              = inbox_label;
+       receive.inbox_entry              = inbox_entry;
+       receive.inbox_btn                = inbox_btn;
+
+       receive.recvatgetall_chkbtn      = recvatgetall_chkbtn;
 }
 
 static void prefs_account_send_create(void)
index e5d6825a5f3841c5b61f5fb92e5be559ff989926..126826374cd98e64403b7e0a8d9c2bb3f211f9a6 100644 (file)
@@ -46,11 +46,15 @@ typedef enum {
 
 typedef enum {
        /* login and retrieve messages, as before */
-       RETR_NORMAL,
-       /* send TOP to server and retrieve Header */
-       RETR_HEADER, 
+       STYPE_NORMAL,
+       /* send TOP to server and retrieve all available Headers */
+       STYPE_PREVIEW_ALL,
+       /* send TOP to server and retrieve new Headers */
+       STYPE_PREVIEW_NEW,
        /* delete selected Headers on Server */
-       DELE_HEADER 
+       STYPE_DELETE, 
+       /* download + remove Mail from Server */
+       STYPE_DOWNLOAD,
 } Pop3SessionType;
 
 #if USE_GPGME
@@ -98,14 +102,16 @@ struct _PrefsAccount
        gchar *leave_time;      
        gboolean getall;
        gboolean recv_at_getall;
+       gboolean sd_rmmail_on_download;
+       gboolean sd_filter_on_recv;
        gboolean enable_size_limit;
        gint size_limit;
        gboolean filter_on_recv;
        gchar *inbox;
 
        /* selective Download */
-       gint   session_type;
-       GSList *to_delete;
+       gint   session;
+       GSList *msg_list;
 
        /* Send */
        gboolean add_date;
index de89efc3dc74c3ee1a2d420f41467113bd83f4f1..dd2b75e899b0897df21161445197092ba386d1bb 100644 (file)
 #include <gdk/gdkkeysyms.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <regex.h>
 
 #include "intl.h"
 #include "main.h"
 #include "prefs.h"
 #include "prefs_matcher.h"
 #include "prefs_filtering.h"
-#include "prefs_common.h"
 #include "prefs_account.h"
 #include "account.h"
 #include "mainwindow.h"
-#include "foldersel.h"
 #include "manage_window.h"
+#include "menu.h"
 #include "stock_pixmap.h"
 #include "inc.h"
 #include "utils.h"
 #include "selective_download.h"
 #include "procheader.h"
 
-struct _SDView {
+static struct _SDView {
        MainWindow *mainwin;
 
        GtkWidget *window;
-       GtkWidget *btn_getmail;
-       GtkWidget *btn_remove;
-       GtkWidget *btn_done;
        GtkWidget *clist;
-       GtkWidget *label_account_name;
-       GtkWidget *label_mails;
-}selective;
+       GtkWidget *preview_btn;
+       GtkWidget *remove_btn;
+       GtkWidget *download_btn;
+       GtkWidget *ac_button;
+       GtkWidget *ac_label;
+       GtkWidget *ac_menu;
+       GtkWidget *preview_popup;
+       GtkWidget *msgs_label;
+       GtkWidget *show_old_chkbtn;
 
-GSList *header_item_list = NULL;
+}selective;
 
 static GdkPixmap *checkboxonxpm;
-static GdkPixmap *checkboxonxpmmask;
+static GdkBitmap *checkboxonxpmmask;
 static GdkPixmap *checkboxoffxpm;
-static GdkPixmap *checkboxoffxpmmask;
+static GdkBitmap *checkboxoffxpmmask;
+static GdkPixmap *markxpm;
+static GdkBitmap *markxpmmask;
+static GdkPixmap *deletedxpm;
+static GdkBitmap *deletedxpmmask;
+static GdkPixmap *continuexpm;
+static GdkBitmap *continuexpmmask;
+
 
-static void sd_window_create(MainWindow *mainwin);
-static void sd_clist_set();
+/* local functions */
+static void sd_clear_msglist();
 static void sd_remove_header_files();
+static void sd_toggle_btn();
+static SD_State sd_header_filter (MsgInfo *msginfo);
+static MsgInfo *sd_get_msginfo_from_file (const gchar *filename);
+
+static void sd_clist_set_pixmap(HeaderItems *items, gint row);
+static void sd_clist_get_items();
+static void sd_clist_set_items();
+static void sd_update_msg_num(PrefsAccount *acc);
+
+enum {
+       PREVIEW_NEW,
+       PREVIEW_ALL,
+       REMOVE,
+       DOWNLOAD,
+       DONE,
+       CHECKBTN,
+};
+
+/* callbacks */
+static void sd_action_cb(GtkWidget *widget, guint action);
+
+static void sd_select_row_cb (GtkCList *clist, gint row, gint column,
+                             GdkEvent *event, gpointer user_data);
+static void sd_key_pressed (GtkWidget *widget,
+                           GdkEventKey *event,
+                           gpointer data);
+/* account menu */
+static void sd_ac_label_pressed(GtkWidget *widget, 
+                               GdkEventButton *event,
+                               gpointer data);
+
+static void sd_ac_menu_popup_closed(GtkMenuShell *menu_shell);
+static void sd_ac_menu_cb(GtkMenuItem *menuitem, gpointer data);
+static void sd_ac_menu_set();
+
+/* preview popup */
+static void sd_preview_popup_closed(GtkMenuShell *menu_shell);
+static void sd_preview_popup_cb(GtkWidget *widget, GdkEventButton *event);
+static GtkItemFactoryEntry preview_popup_entries[] =
+{
+       {N_("/Preview _new Messages"), NULL, sd_action_cb, PREVIEW_NEW, NULL},
+       {N_("/Preview _all Messages"), NULL, sd_action_cb, PREVIEW_ALL, NULL}
+};
 
+/* create dialog */
+static void sd_window_create (MainWindow *mainwin);
 
 void selective_download(MainWindow *mainwin)
 {
-       PrefsAccount *account = cur_account;
-
        summary_write_cache(mainwin->summaryview);
-       main_window_lock(mainwin);
 
        sd_remove_header_files();
-
-       header_item_list = NULL;
        
-       sd_window_create(mainwin);
-
-       stock_pixmap_gdk(selective.clist, STOCK_PIXMAP_CHECKBOX_OFF,
+       stock_pixmap_gdk(mainwin->window, STOCK_PIXMAP_CHECKBOX_OFF,
                         &checkboxoffxpm, &checkboxoffxpmmask);
-       stock_pixmap_gdk(selective.clist, STOCK_PIXMAP_CHECKBOX_ON,
+       stock_pixmap_gdk(mainwin->window, STOCK_PIXMAP_CHECKBOX_ON,
                         &checkboxonxpm, &checkboxonxpmmask);
-       
-       gtk_label_set_text(GTK_LABEL(selective.label_account_name), account->account_name);
+       stock_pixmap_gdk(mainwin->window, STOCK_PIXMAP_DELETED,
+                        &deletedxpm, &deletedxpmmask);
+       stock_pixmap_gdk(mainwin->window, STOCK_PIXMAP_CONTINUE,
+                        &continuexpm, &continuexpmmask);
+       stock_pixmap_gdk(mainwin->window, STOCK_PIXMAP_MARK,
+                        &markxpm, &markxpmmask);
+       inc_lock();
+
+       if (!selective.window)
+           sd_window_create(mainwin);
+
+       manage_window_set_transient(GTK_WINDOW(selective.window));
+       gtk_widget_show(selective.window);
+       sd_clear_msglist();
+       sd_ac_menu_set();
+       gtk_clist_clear(GTK_CLIST(selective.clist));
+
+
 }
 
+static void sd_clear_msglist()
+{
+       PrefsAccount *acc = cur_account;
+       while (acc->msg_list != NULL) {
+               HeaderItems *item = (HeaderItems*)acc->msg_list->data;
+               acc->msg_list = g_slist_remove(acc->msg_list, item);
+       }       
+       g_slist_free(acc->msg_list);
+       sd_update_msg_num(acc);
+}
 
 /* sd_remove_header_files()
  * 
@@ -115,41 +185,36 @@ static void sd_remove_header_files()
        g_free(path);
 }
 
-
-/* sd_toggle_btn_remove()
+/* sd_toggle_btn()
  *
- * - checks whether at least on email is selected for deletion
- *   if so, untoggle remove button
+ * - checks whether at least on email is selected 
+ *   if so, untoggle remove / download button
  */
-static gboolean sd_toggle_btn_remove()
+static void sd_toggle_btn()
 {
+       PrefsAccount *acc = cur_account;
        GSList *cur;
-       gint num = 0;
-       gpointer row;
-
-
-       for (cur = header_item_list; cur != NULL; cur = cur->next) {
-               HeaderItems *item = (HeaderItems*)cur->data;
+       
+       for (cur = acc->msg_list; cur != NULL; cur = cur->next) {
+               HeaderItems *items = (HeaderItems*)cur->data;
                
-               if (item->state == CHECKED) {
-                       gtk_widget_set_sensitive (selective.btn_remove, TRUE);
-                       return CHECKED;
+               if (items->state == SD_CHECKED) {
+                       gtk_widget_set_sensitive (selective.remove_btn, TRUE);
+                       gtk_widget_set_sensitive (selective.download_btn, TRUE);
+                       return;
                }
-               num++;
        }
 
-       gtk_widget_set_sensitive (selective.btn_remove, FALSE);
-       return UNCHECKED;
+       gtk_widget_set_sensitive (selective.remove_btn, FALSE);
 }
 
-
 /* sd_header_filter(MsgInfo *msginfo)
  *
  * - parse header line and look for any applying filtering rules
  * - if message matches other MATCHACTION --> return
  *
  */
-gboolean sd_header_filter(MsgInfo *msginfo)
+SD_State sd_header_filter(MsgInfo *msginfo)
 {
        GSList *rules;
 
@@ -157,77 +222,169 @@ gboolean sd_header_filter(MsgInfo *msginfo)
        for (rules = global_processing; rules != NULL; rules = rules->next) { 
 
                FilteringProp *prop = (FilteringProp*) rules->data; 
-               gchar line[POPBUFSIZE];
 
                if ( matcherlist_match(prop->matchers, msginfo) ) {
                        if (prop->action->type == MATCHACTION_DELETE_ON_SERVER) {
-                               return CHECKED;
+                               debug_print(_("action matched\n"));
+                               return SD_CHECKED;
+                       }
+                       else {
+                               debug_print(_("action not matched\n"));
+                               return SD_UNCHECKED;
                        }
-                       else 
-                               return UNCHECKED;
                }
-               
        }
-       return UNCHECKED;
+       return SD_UNCHECKED;
 }
 
-
 /* sd_get_msginfo_from_file(const gchar *filename)
  *
  * - retrieve msginfo from saved header files
  */
 MsgInfo *sd_get_msginfo_from_file(const gchar *filename)
 {
-       FILE *fp;
-       gchar buf[BUFFSIZE];
        MsgInfo *msginfo  = g_new0(MsgInfo, 1);
+       MsgInfo *msg;
        MsgFlags msgflags = { 0, 0 };
 
-       msginfo  = (MsgInfo*) procheader_parse(filename, msgflags, TRUE, FALSE);
-       if ( (fp = fopen(filename, "rb")) != NULL ) {
-               static HeaderEntry hentry[] = { { SIZE_HEADER, NULL, FALSE},
-                                               { NULL, NULL, FALSE} };
-               
-               procheader_get_one_field (buf, sizeof(buf), fp, hentry);
-               fclose(fp);
+       msg  = procheader_parse(filename, msgflags, TRUE, FALSE);
+
+       if (!msg) {
+               msginfo->subject = _("(No Subject)");
+               msginfo->from    = _("(No Sender)");
+               msginfo->date    = _("(No Date)");
+       } else {
+               g_memmove(&msginfo, &msg, sizeof(msginfo)); 
+
+               if (msginfo->date_t) {
+                       gchar date_modified[80];
+                       procheader_date_get_localtime(date_modified,
+                                                     sizeof(date_modified),
+                                                     msginfo->date_t);
+                       msginfo->date = date_modified;
+               }
+       } 
+       return msginfo;
+}
+
+static void sd_clist_set_pixmap(HeaderItems *items, gint row)
+{
+       
+       switch (items->state) {
+       case SD_REMOVED:
+               gtk_clist_set_pixmap (GTK_CLIST (selective.clist), 
+                                     row, 0,
+                                    deletedxpm, deletedxpmmask);
+               break;
+       case SD_CHECKED:
+               gtk_clist_set_pixmap (GTK_CLIST (selective.clist), 
+                                     row, 0,
+                                     checkboxonxpm, checkboxonxpmmask);
+               break;
+       case SD_DOWNLOADED:
+               gtk_clist_set_pixmap (GTK_CLIST (selective.clist), 
+                                     row, 0,
+                                     markxpm, markxpmmask);
+               break;          
+       default:
+               gtk_clist_set_pixmap (GTK_CLIST (selective.clist), 
+                                     row, 0,
+                                     checkboxoffxpm, checkboxoffxpmmask);
+               break;
        }
+}
 
-       if (buf)
-               msginfo->size = (off_t)atoi(buf + SIZE_HEADER_LEN);
-       else
-               msginfo->size = 0;
+static void sd_clist_set_items()
+{
+       PrefsAccount *acc = cur_account;
+       GSList *cur;
+       gboolean show_old = 
+               gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(selective.show_old_chkbtn));
        
-       return msginfo;
+       gtk_clist_clear(GTK_CLIST(selective.clist));
+       gtk_clist_freeze(GTK_CLIST(selective.clist));
+
+       for (cur = acc->msg_list; cur != NULL; cur = cur->next) {
+               HeaderItems *items = (HeaderItems*)cur->data;
+               gchar *row[5];
+               gint row_num;
+               
+               row[0] = _("");
+               row[1] = items->from;
+               row[2] = items->subject;
+               row[3] = items->date;
+               row[4] = g_strdup_printf("%i KB", items->size/1024);
+               
+               switch (items->state) {
+               case SD_REMOVED:
+               case SD_DOWNLOADED:
+                       items->del_by_old_session = TRUE;
+               default:
+                       break;
+               }
+               if (show_old) {
+                       if (items->received) {
+                               row_num = gtk_clist_append(GTK_CLIST(selective.clist), row);
+                               sd_clist_set_pixmap(items, row_num);
+                       }
+               }
+               else {
+                       row_num = gtk_clist_append(GTK_CLIST(selective.clist), row);
+                       sd_clist_set_pixmap(items, row_num);
+               }
+               g_free(row[4]);
+       }
+
+       gtk_clist_thaw(GTK_CLIST(selective.clist));
+       sd_toggle_btn();
 }
 
+/* sd_update_msg_num(PrefsAccount *acc)
+ * - keep track of msgs still on server
+ * - count UNCHECKED items as well as downloaded but not removed
+ */
+static void sd_update_msg_num(PrefsAccount *acc)
+{
+       GSList *cur;
+       gint msg_num = g_slist_length(acc->msg_list);
+       gchar *text;
 
-/* sd_clist_set()
+       for (cur = acc->msg_list; cur != NULL; cur = cur->next) {
+               HeaderItems *items = (HeaderItems*) cur->data;
+
+               if (items->state != SD_UNCHECKED)
+                       msg_num--;
+               if (items->state == SD_DOWNLOADED)
+                       if (!acc->sd_rmmail_on_download)
+                               msg_num++;
+       }
+
+       text = g_strdup_printf("%i Messages", msg_num);
+       gtk_label_set_text(GTK_LABEL(selective.msgs_label), text);
+
+       g_free(text);
+}
+
+/* sd_clist_get_items()
  *
- * - fill clist and set toggle button accordingly
+ * - get items for clist from Files
  */
-void sd_clist_set()
+static void sd_clist_get_items()
 {
-       gint index = 1;
-
-       gchar *path     = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
-       gchar *filename = g_strdup_printf("%s%i", path, index);
+       GSList *cur;
+       PrefsAccount *acc = cur_account;
+       gchar *path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
        
-       while ( is_file_exist(filename) ) {
+       for (cur = acc->msg_list; cur != NULL; cur = cur->next) {
 
+               HeaderItems *items = (HeaderItems*) cur->data;
+               gchar *filename = g_strdup_printf("%s%i", path, items->index);
                MsgInfo *msginfo  = sd_get_msginfo_from_file(filename);
-               HeaderItems *line = g_new0 (HeaderItems, 1);
-               gchar *row[4];
-               gint msgid;     
                
-               line->index = index;
-
-               row[0] = "";
-               row[1] = line->from    = msginfo->from;
-               row[2] = line->subject = msginfo->subject;
-               row[3] = line->size    = to_human_readable(msginfo->size);
+               items->from    = msginfo->from;
+               items->subject = msginfo->subject;
+               strncpy2(items->date, msginfo->date, sizeof(items->date));
                
-               gtk_clist_append (GTK_CLIST(selective.clist), row);
-
                msginfo->folder = folder_get_default_processing();
 
                /* move msg file to drop folder */
@@ -237,123 +394,194 @@ void sd_clist_set()
                        return;
                }
                
-               if (sd_header_filter(msginfo)) {
-                       line->state = CHECKED;
-                       gtk_clist_set_pixmap (GTK_CLIST (selective.clist), index - 1, 0,
-                                             checkboxonxpm, checkboxonxpmmask);
-               }
-               else {
-                       line->state = UNCHECKED;
-                       gtk_clist_set_pixmap (GTK_CLIST (selective.clist), index - 1, 0,
-                                             checkboxoffxpm, checkboxoffxpmmask);
-               }
-               
-               folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
+               if (acc->sd_filter_on_recv)
+                       items->state = sd_header_filter(msginfo);
 
-               header_item_list = g_slist_append(header_item_list, line);
-               
-               index++;
-               filename = g_strdup_printf("%s%i", path, index);
+               folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
+               g_free(filename);
+               g_free(msginfo);
        }
-       
-       sd_toggle_btn_remove();
 
        g_free(path);
-       g_free(filename);
 }
 
+static gint sd_deleted_cb(GtkWidget *widget, GdkEventAny *event, gpointer data)
+{
+       sd_remove_header_files();
+       sd_clear_msglist();
+       gtk_widget_destroy(selective.window);   
+       selective.window = NULL;
+       inc_unlock();
+       return TRUE;
+}
 
 /* --- Callbacks -- */
-static void sd_btn_remove_cb()
+static void sd_action_cb(GtkWidget *widget, guint action)
 {
-       GSList *cur;
-       PrefsAccount *account = cur_account;
-       gboolean enable_dele  = FALSE;
-
-       account->to_delete = NULL;
-
-       /* loop through list collecting mails marked for delete */
-       for (cur = header_item_list; cur != NULL; cur = cur->next) {
-               
-               HeaderItems *items = (HeaderItems*)cur->data;
-               
-               if (items->state == CHECKED) {
-                       gchar *row[4];
-
-                       /* replace deleted Mail */
-                       row[0] = "D";
-                       row[1] = items->from;
-                       row[2] = items->subject;
-                       row[3] = items->size;
-
-                       account->to_delete = g_slist_append(account->to_delete, 
-                                                           GINT_TO_POINTER(items->index));
-
-                       gtk_clist_remove(GTK_CLIST(selective.clist), items->index-1);
-                       gtk_clist_insert(GTK_CLIST(selective.clist), items->index-1, row);
-                       gtk_clist_set_selectable(GTK_CLIST(selective.clist), 
-                                                items->index - 1, FALSE);
-                       enable_dele = TRUE;
-                       debug_print(_("marked to delete %i\n"), 
-                                   items->index);
-                       
-                       items->state = UNCHECKED;
+       PrefsAccount *acc = cur_account;
+       
+       switch(action) {
+       case PREVIEW_NEW:
+       case PREVIEW_ALL:
+               if ( (acc->protocol != A_APOP) &&
+                    (acc->protocol != A_POP3) ) {
+                       alertpanel_error(
+                               _("Selected Account \"%s\" is not a POP Mail Server.\nPlease select a different Account"), acc->account_name);
+                       return;
+               }
+               sd_clear_msglist();
+               if (action == PREVIEW_NEW) {
+                       gtk_widget_set_sensitive(selective.show_old_chkbtn, FALSE);
+                       inc_selective_download(selective.mainwin, acc, STYPE_PREVIEW_NEW);
                }
+               else {
+                       gtk_widget_set_sensitive(selective.show_old_chkbtn, TRUE);
+                       inc_selective_download(selective.mainwin, acc, STYPE_PREVIEW_ALL);
+               }
+               
+               gtk_clist_clear(GTK_CLIST(selective.clist));
+               sd_clist_get_items();   
+               sd_clist_set_items();
+               break;
+       case REMOVE:
+               inc_selective_download(selective.mainwin, acc, STYPE_DELETE);
+               sd_clist_set_items();
+               break;
+       case DOWNLOAD:
+               inc_selective_download(selective.mainwin, acc, STYPE_DOWNLOAD);
+               sd_clist_set_items();
+               break;
+       case DONE:
+               sd_remove_header_files();
+               sd_clear_msglist();
+               gtk_widget_hide(selective.window);
+               inc_unlock();
+               break;
+       case CHECKBTN:
+               sd_clist_set_items();
+               break;
+       default:
+               break;
        }
+       
+       sd_update_msg_num(acc);
+}
 
+/* Events */
+static void sd_select_row_cb(GtkCList *clist, gint row, gint column,
+                            GdkEvent *event, gpointer user_data)
+{
+       if ((row >= 0) && (column >= 0)) {
+               PrefsAccount *acc  = cur_account;
+               HeaderItems *items = (HeaderItems*) g_slist_nth_data (acc->msg_list, row);
 
-       if (enable_dele == TRUE) {
-               inc_selective_download(selective.mainwin, DELE_HEADER);
-       }
+               if (!items) return;
+               if (!gtk_clist_get_selectable(GTK_CLIST(selective.clist), row)) return;
 
-       sd_toggle_btn_remove(); 
+               if (items->state == SD_UNCHECKED) {
+                       items->state = SD_CHECKED;
+                       gtk_clist_set_pixmap (GTK_CLIST (selective.clist), row, 0,
+                                             checkboxonxpm, checkboxonxpmmask);
+               } else if (items->state == SD_CHECKED) {
+                       items->state = SD_UNCHECKED;
+                       gtk_clist_set_pixmap (GTK_CLIST (selective.clist), row, 0,
+                                             checkboxoffxpm, checkboxoffxpmmask);
+               }
+               sd_toggle_btn();
+       }
+}
 
-       g_slist_free(account->to_delete);
-       account->to_delete = NULL;      
+static void sd_key_pressed(GtkWidget *widget,
+                          GdkEventKey *event,
+                          gpointer data)
+{
+       if (event && event->keyval == GDK_Escape)
+               sd_action_cb(widget, DONE);
 }
 
-static void sd_btn_receive_cb()
+/* account menu */
+static void sd_ac_label_pressed(GtkWidget *widget, GdkEventButton *event,
+                                   gpointer data)
 {
+       if (!event) return;
 
-       manage_window_focus_in(selective.window, NULL, NULL);
+       gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
+       gtk_object_set_data(GTK_OBJECT(selective.ac_menu), "menu_button",
+                           widget);
 
-       inc_selective_download(selective.mainwin, RETR_HEADER);
+       gtk_menu_popup(GTK_MENU(selective.ac_menu), NULL, NULL,
+                      menu_button_position, widget,
+                      event->button, event->time);
+}
+
+static void sd_ac_menu_popup_closed(GtkMenuShell *menu_shell)
+{
+       GtkWidget *button;
 
+       button = gtk_object_get_data(GTK_OBJECT(menu_shell), "menu_button");
+       if (!button) return;
+       gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
        gtk_clist_clear(GTK_CLIST(selective.clist));
-       sd_clist_set();
+       sd_clear_msglist();
+       sd_toggle_btn();
+       gtk_object_remove_data(GTK_OBJECT(selective.ac_menu), "menu_button");
 }
 
-static void sd_btn_done_cb()
+static void sd_ac_menu_cb(GtkMenuItem *menuitem, gpointer data)
 {
-       sd_remove_header_files();
-       
-       if (header_item_list)
-               slist_free_strings (header_item_list);
-
-       gtk_widget_hide(selective.window);
-       main_window_unlock(selective.mainwin);
+       cur_account = (PrefsAccount *)data;
+       gtk_label_set_text(GTK_LABEL(selective.ac_label), cur_account->account_name);
+       gtk_widget_queue_resize(selective.ac_button);
+       main_window_reflect_prefs_all();
 }
 
-static void sd_select_row_cb(GtkCList *clist, gint row, gint column,
-                            GdkEvent *event, gpointer user_data)
+static void sd_ac_menu_set()
 {
+       GList *cur_ac, *cur_item;
+       GtkWidget *menuitem;
+       PrefsAccount *ac_prefs;
+       GList *account_list = account_get_list();
+
+       /* destroy all previous menu item */
+       cur_item = GTK_MENU_SHELL(selective.ac_menu)->children;
+       while (cur_item != NULL) {
+               GList *next = cur_item->next;
+               gtk_widget_destroy(GTK_WIDGET(cur_item->data));
+               cur_item = next;
+       }
 
-       if ((row >= 0) && (column >= 0)) {
-               HeaderItems *item = (HeaderItems*) g_slist_nth_data (header_item_list, row);
+       gtk_label_set_text(GTK_LABEL(selective.ac_label), cur_account->account_name);
 
-               if (!item) return;
-               if (!gtk_clist_get_selectable(GTK_CLIST(selective.clist), row)) return;
+       for (cur_ac = account_list; cur_ac != NULL; cur_ac = cur_ac->next) {
+               ac_prefs = (PrefsAccount *)cur_ac->data;
+               
+               menuitem = gtk_menu_item_new_with_label
+                       (ac_prefs->account_name
+                        ? ac_prefs->account_name : _("Untitled"));
+               gtk_widget_show(menuitem);
+               gtk_menu_append(GTK_MENU(selective.ac_menu), menuitem);
+               gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
+                                          GTK_SIGNAL_FUNC(sd_ac_menu_cb),
+                                          ac_prefs);
+       }
+}
 
-               if (item->state == UNCHECKED) {
-                       item->state = CHECKED;
-                       gtk_clist_set_pixmap (GTK_CLIST (selective.clist), row, 0,
-                                             checkboxonxpm, checkboxonxpmmask);
-               } else {
-                       item->state = UNCHECKED;
-                       gtk_clist_set_pixmap (GTK_CLIST (selective.clist), row, 0,
-                                             checkboxoffxpm, checkboxoffxpmmask);
-               }
-               sd_toggle_btn_remove();
+/* receive button popup */
+static void sd_preview_popup_closed(GtkMenuShell *menu_shell)
+{
+       gtk_button_set_relief(GTK_BUTTON(selective.preview_btn), GTK_RELIEF_NORMAL);
+       manage_window_focus_in(selective.window, NULL, NULL);
+}
+
+static void sd_preview_popup_cb(GtkWidget *widget, GdkEventButton *event)
+{
+       if (!event) return;
+       
+       if (event->button == 1) {
+               gtk_button_set_relief(GTK_BUTTON(widget), GTK_RELIEF_NORMAL);
+               gtk_menu_popup(GTK_MENU(selective.preview_popup), NULL, NULL,
+                      menu_button_position, widget,
+                      event->button, event->time);
        }
 }
 
@@ -361,204 +589,212 @@ static void sd_window_create(MainWindow *mainwin)
 {
        GtkWidget *window;
        GtkWidget *table;
-       GtkWidget *label_mails;
-       GtkWidget *hbox_bottom;
-       GtkWidget *label_fixed;
-       GtkWidget *label_account_name;
+       GtkWidget *msgs_label;
+       GtkWidget *bottom_hbox;
+       GtkWidget *fixed_label;
+       GtkWidget *expand_label;
+       GtkWidget *ac_button;
+       GtkWidget *ac_label;
+       GtkWidget *ac_menu;
+       GtkWidget *show_old_chkbtn;
        GtkWidget *scrolledwindow;
        GtkWidget *clist;
-       GtkWidget *label_state;
-       GtkWidget *label_from;
-       GtkWidget *label_subject;
-       GtkWidget *label_size;
-       GtkWidget *hbox_toolbar;
+       GtkWidget *state_label;
+       GtkWidget *from_label;
+       GtkWidget *subject_label;
+       GtkWidget *size_label;
+       GtkWidget *date_label;
+       GtkWidget *toolbar_hbox;
        GtkWidget *toolbar;
-       GtkWidget *btn_receive;
        GtkWidget *tmp_toolbar_icon;
-       GtkWidget *btn_remove;
-       GtkWidget *btn_done;
+       GtkWidget *preview_btn;
+       GtkWidget *preview_popup;
+       GtkWidget *remove_btn;
+       GtkWidget *download_btn;
+       GtkWidget *done_btn;
+       gint n_menu_entries;
 
        window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        gtk_object_set_data (GTK_OBJECT (window), "window", window);
        gtk_window_set_title (GTK_WINDOW (window), _("Selective download"));
        gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
-       gtk_window_set_default_size (GTK_WINDOW (window), 450, 250);
-       
+       gtk_window_set_modal (GTK_WINDOW (window), TRUE);
+       gtk_window_set_policy (GTK_WINDOW (window), TRUE, TRUE, TRUE);
+
        table = gtk_table_new (2, 2, FALSE);
-       gtk_widget_ref (table);
-       gtk_object_set_data_full (GTK_OBJECT (window), "table", table,
-                                 (GtkDestroyNotify) gtk_widget_unref);
-       gtk_widget_show (table);
        gtk_container_add (GTK_CONTAINER (window), table);
-       
-       label_mails = gtk_label_new (_("0 Mail(s)"));
-       gtk_widget_ref (label_mails);
-       gtk_object_set_data_full (GTK_OBJECT (window), "label_mails", label_mails,
-                                 (GtkDestroyNotify) gtk_widget_unref);
-       gtk_widget_show (label_mails);
-       gtk_table_attach (GTK_TABLE (table), label_mails, 1, 2, 1, 2,
+
+       msgs_label = gtk_label_new (_("0 Messages"));
+       gtk_table_attach (GTK_TABLE (table), msgs_label, 1, 2, 1, 2,
                          (GtkAttachOptions) (GTK_FILL),
                          (GtkAttachOptions) (0), 0, 0);
-       gtk_misc_set_alignment (GTK_MISC (label_mails), 0, 0.5);
-       
-       hbox_bottom = gtk_hbox_new (FALSE, 0);
-       gtk_widget_ref (hbox_bottom);
-       gtk_object_set_data_full (GTK_OBJECT (window), "hbox_bottom", hbox_bottom,
-                                 (GtkDestroyNotify) gtk_widget_unref);
-       gtk_widget_show (hbox_bottom);
-       gtk_table_attach (GTK_TABLE (table), hbox_bottom, 0, 1, 1, 2,
+       gtk_misc_set_alignment (GTK_MISC (msgs_label), 0, 0.5);
+
+       bottom_hbox = gtk_hbox_new (FALSE, 0);
+       gtk_table_attach (GTK_TABLE (table), bottom_hbox, 0, 1, 1, 2,
                          (GtkAttachOptions) (GTK_FILL),
                          (GtkAttachOptions) (GTK_FILL), 0, 0);
        
-       label_fixed = gtk_label_new (_("current Account:"));
-       gtk_widget_ref (label_fixed);
-       gtk_object_set_data_full (GTK_OBJECT (window), "label_fixed", label_fixed,
-                                 (GtkDestroyNotify) gtk_widget_unref);
-       gtk_widget_show (label_fixed);
-       gtk_box_pack_start (GTK_BOX (hbox_bottom), label_fixed, FALSE, FALSE, 0);
+       show_old_chkbtn = gtk_check_button_new_with_label("Show only old Messages"); 
+       gtk_box_pack_start(GTK_BOX(bottom_hbox), show_old_chkbtn, FALSE, FALSE, 0); 
        
-       label_account_name = gtk_label_new (_("none"));
-       gtk_widget_ref (label_account_name);
-       gtk_object_set_data_full (GTK_OBJECT (window), "label_account_name", label_account_name,
-                                 (GtkDestroyNotify) gtk_widget_unref);
-       gtk_widget_show (label_account_name);
-       gtk_box_pack_start (GTK_BOX (hbox_bottom), label_account_name, TRUE, FALSE, 0);
+       gtk_signal_connect(GTK_OBJECT(show_old_chkbtn), "toggled", 
+                          GTK_SIGNAL_FUNC(sd_action_cb), GUINT_TO_POINTER(CHECKBTN)); 
+       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(show_old_chkbtn), FALSE);
+       GTK_WIDGET_UNSET_FLAGS(show_old_chkbtn, GTK_CAN_FOCUS);                                                 
+                          
+       expand_label = gtk_label_new (_(" "));
+       gtk_box_pack_start (GTK_BOX (bottom_hbox), expand_label, TRUE, TRUE, 0);
        
+       fixed_label = gtk_label_new (_(" contains "));
+       gtk_box_pack_end (GTK_BOX (bottom_hbox), fixed_label, FALSE, FALSE, 0);
+
+       ac_menu = gtk_menu_new();
+       gtk_signal_connect(GTK_OBJECT(ac_menu), "selection_done",
+                          GTK_SIGNAL_FUNC(sd_ac_menu_popup_closed), NULL);
+       ac_button = gtk_button_new();
+       gtk_button_set_relief(GTK_BUTTON(ac_button), GTK_RELIEF_NONE);
+       GTK_WIDGET_UNSET_FLAGS(ac_button, GTK_CAN_FOCUS);
+       gtk_widget_set_usize(ac_button, -1, 1);
+       gtk_box_pack_start(GTK_BOX(bottom_hbox), ac_button, FALSE, FALSE, 0);
+       gtk_signal_connect(GTK_OBJECT(ac_button), "button_press_event",
+                          GTK_SIGNAL_FUNC(sd_ac_label_pressed), GTK_OBJECT(ac_menu));
+
+       ac_label = gtk_label_new("");
+       gtk_container_add(GTK_CONTAINER(ac_button), ac_label);
+
        scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
-       gtk_widget_ref (scrolledwindow);
-       gtk_object_set_data_full (GTK_OBJECT (window), "scrolledwindow", scrolledwindow,
-                                 (GtkDestroyNotify) gtk_widget_unref);
-       gtk_widget_show (scrolledwindow);
        gtk_table_attach (GTK_TABLE (table), scrolledwindow, 0, 1, 0, 1,
                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                          (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), 0, 0);
-       gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
+       gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow), 
+                                       GTK_POLICY_NEVER, 
+                                       GTK_POLICY_AUTOMATIC);
        
-       clist = gtk_clist_new (4);
-       gtk_widget_ref (clist);
-       gtk_object_set_data_full (GTK_OBJECT (window), "clist", clist,
-                                 (GtkDestroyNotify) gtk_widget_unref);
-       gtk_widget_show (clist);
+       clist = gtk_clist_new (5);
        gtk_container_add (GTK_CONTAINER (scrolledwindow), clist);
        gtk_container_set_border_width (GTK_CONTAINER (clist), 5);
-       gtk_clist_set_column_width (GTK_CLIST (clist), 0, 21);
-       gtk_clist_set_column_width (GTK_CLIST (clist), 1, 143);
-       gtk_clist_set_column_width (GTK_CLIST (clist), 2, 158);
-       gtk_clist_set_column_width (GTK_CLIST (clist), 3, 15);
+       gtk_clist_set_column_width (GTK_CLIST (clist), 0, 20);
+       gtk_clist_set_column_width (GTK_CLIST (clist), 1, 150);
+       gtk_clist_set_column_width (GTK_CLIST (clist), 2, 150);
+       gtk_clist_set_column_width (GTK_CLIST (clist), 3, 100);
+       gtk_clist_set_column_width (GTK_CLIST (clist), 4, 30);
        gtk_clist_set_selection_mode (GTK_CLIST (clist), GTK_SELECTION_BROWSE);
        gtk_clist_column_titles_show (GTK_CLIST (clist));
        
-       label_state = gtk_label_new (_("#"));
-       gtk_widget_ref (label_state);
-       gtk_object_set_data_full (GTK_OBJECT (window), "label_state", label_state,
-                                 (GtkDestroyNotify) gtk_widget_unref);
-       gtk_widget_show (label_state);
-       gtk_clist_set_column_widget (GTK_CLIST (clist), 0, label_state);
+       state_label = gtk_label_new (_("#"));
+       gtk_clist_set_column_widget (GTK_CLIST (clist), 0, state_label);
        
-       label_from = gtk_label_new (_("From"));
-       gtk_widget_ref (label_from);
-       gtk_object_set_data_full (GTK_OBJECT (window), "label_from", label_from,
-                                 (GtkDestroyNotify) gtk_widget_unref);
-       gtk_widget_show (label_from);
-       gtk_clist_set_column_widget (GTK_CLIST (clist), 1, label_from);
+       from_label = gtk_label_new (_("From"));
+       gtk_clist_set_column_widget (GTK_CLIST (clist), 1, from_label);
        
-       label_subject = gtk_label_new (_("Subject"));
-       gtk_widget_ref (label_subject);
-       gtk_object_set_data_full (GTK_OBJECT (window), "label_subject", label_subject,
-                                 (GtkDestroyNotify) gtk_widget_unref);
-       gtk_widget_show (label_subject);
-       gtk_clist_set_column_widget (GTK_CLIST (clist), 2, label_subject);
+       subject_label = gtk_label_new (_("Subject"));
+       gtk_clist_set_column_widget (GTK_CLIST (clist), 2, subject_label);
        
-       label_size = gtk_label_new (_("Size"));
-       gtk_widget_ref (label_size);
-       gtk_object_set_data_full (GTK_OBJECT (window), "label_size", label_size,
-                                 (GtkDestroyNotify) gtk_widget_unref);
-       gtk_widget_show (label_size);
-       gtk_clist_set_column_widget (GTK_CLIST (clist), 3, label_size);
+       date_label = gtk_label_new (_("Date"));
+       gtk_clist_set_column_widget (GTK_CLIST (clist), 3, date_label);
+
+       size_label = gtk_label_new (_("Size"));
+       gtk_widget_ref (size_label);
+       gtk_clist_set_column_widget (GTK_CLIST (clist), 4, size_label);
        
-       hbox_toolbar = gtk_hbox_new (FALSE, 0);
-       gtk_widget_ref (hbox_toolbar);
-       gtk_object_set_data_full (GTK_OBJECT (window), "hbox_toolbar", hbox_toolbar,
-                                 (GtkDestroyNotify) gtk_widget_unref);
-       gtk_widget_show (hbox_toolbar);
-       gtk_table_attach (GTK_TABLE (table), hbox_toolbar, 1, 2, 0, 1,
+       toolbar_hbox = gtk_hbox_new (FALSE, 0);
+
+       gtk_table_attach (GTK_TABLE (table), toolbar_hbox, 1, 2, 0, 1,
                          (GtkAttachOptions) (GTK_FILL),
                          (GtkAttachOptions) (GTK_FILL), 0, 0);
        
        toolbar = gtk_toolbar_new (GTK_ORIENTATION_VERTICAL, GTK_TOOLBAR_BOTH);
-       gtk_widget_ref (toolbar);
-       gtk_object_set_data_full (GTK_OBJECT (window), "toolbar", toolbar,
-                                 (GtkDestroyNotify) gtk_widget_unref);
-       gtk_widget_show (toolbar);
-       gtk_box_pack_start (GTK_BOX (hbox_toolbar), toolbar, FALSE, FALSE, 0);
-       gtk_toolbar_set_space_size (GTK_TOOLBAR (toolbar), 20);
+
+       gtk_box_pack_end (GTK_BOX (toolbar_hbox), toolbar, FALSE, FALSE, 0);
+       gtk_toolbar_set_space_size (GTK_TOOLBAR (toolbar), 30);
        gtk_toolbar_set_space_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_SPACE_LINE);
+       gtk_container_set_border_width (GTK_CONTAINER (toolbar), 5);
        
-       tmp_toolbar_icon = stock_pixmap_widget(hbox_toolbar, STOCK_PIXMAP_MAIL_RECEIVE);
-       btn_receive = gtk_toolbar_append_element (GTK_TOOLBAR (toolbar),
+       tmp_toolbar_icon = stock_pixmap_widget(toolbar_hbox, STOCK_PIXMAP_MAIL_RECEIVE);
+       preview_btn = gtk_toolbar_append_element (GTK_TOOLBAR (toolbar),
                                                  GTK_TOOLBAR_CHILD_BUTTON,
                                                  NULL,
-                                                 _("Receive"),
-                                                 _("preview E-Mail"), NULL,
+                                                 _("Preview Mail"),
+                                                 _("preview old/new E-Mail on account"), NULL,
                                                  tmp_toolbar_icon, NULL, NULL);
-       gtk_widget_ref (btn_receive);
-       gtk_object_set_data_full (GTK_OBJECT (window), "btn_receive", btn_receive,
-                                 (GtkDestroyNotify) gtk_widget_unref);
-       gtk_widget_show (btn_receive);
-       
+
+       n_menu_entries = sizeof(preview_popup_entries)/sizeof(preview_popup_entries[0]);
+       preview_popup = popupmenu_create(preview_btn, preview_popup_entries, n_menu_entries,
+                                     "<SelectiveDownload>", window);
+
+       gtk_signal_connect(GTK_OBJECT(preview_popup), "selection_done",
+                          GTK_SIGNAL_FUNC(sd_preview_popup_closed), NULL);
+
        gtk_toolbar_append_space (GTK_TOOLBAR (toolbar));
        
-       tmp_toolbar_icon = stock_pixmap_widget(hbox_toolbar, STOCK_PIXMAP_CONTINUE);
-       btn_remove = gtk_toolbar_append_element (GTK_TOOLBAR (toolbar),
+       tmp_toolbar_icon = stock_pixmap_widget(toolbar_hbox, STOCK_PIXMAP_CLOSE);
+       remove_btn = gtk_toolbar_append_element (GTK_TOOLBAR (toolbar),
                                                 GTK_TOOLBAR_CHILD_BUTTON,
                                                 NULL,
                                                 _("Remove"),
                                                 _("remove selected E-Mails"), NULL,
                                                 tmp_toolbar_icon, NULL, NULL);
-       gtk_widget_ref (btn_remove);
-       gtk_object_set_data_full (GTK_OBJECT (window), "btn_remove", btn_remove,
-                                 (GtkDestroyNotify) gtk_widget_unref);
-       gtk_widget_set_sensitive (btn_remove, FALSE);
-       
+
+       gtk_widget_set_sensitive (remove_btn, FALSE);
+
+       tmp_toolbar_icon = stock_pixmap_widget(toolbar_hbox, STOCK_PIXMAP_DOWN_ARROW);
+       download_btn = gtk_toolbar_append_element (GTK_TOOLBAR (toolbar),
+                                                GTK_TOOLBAR_CHILD_BUTTON,
+                                                NULL,
+                                                _("Download"),
+                                                _("Download selected E-Mails"), NULL,
+                                                tmp_toolbar_icon, NULL, NULL);
+
+       gtk_widget_set_sensitive (download_btn, FALSE);
+
        gtk_toolbar_append_space (GTK_TOOLBAR (toolbar));
-       
-       tmp_toolbar_icon = stock_pixmap_widget (hbox_toolbar, STOCK_PIXMAP_CLOSE);
-       btn_done = gtk_toolbar_append_element (GTK_TOOLBAR (toolbar),
+
+       tmp_toolbar_icon = stock_pixmap_widget (toolbar_hbox, STOCK_PIXMAP_COMPLETE);
+       done_btn = gtk_toolbar_append_element (GTK_TOOLBAR (toolbar),
                                               GTK_TOOLBAR_CHILD_BUTTON,
                                               NULL,
                                               _("Done"),
                                               _("Exit Dialog"), NULL,
                                               tmp_toolbar_icon, NULL, NULL);
-       gtk_widget_ref (btn_done);
-       gtk_object_set_data_full (GTK_OBJECT (window), "btn_done", btn_done,
-                                 (GtkDestroyNotify) gtk_widget_unref);
-       gtk_widget_show (btn_done);
        
-       gtk_signal_connect (GTK_OBJECT (window), "destroy",
-                           GTK_SIGNAL_FUNC (sd_btn_done_cb),
-                           NULL);
-       gtk_signal_connect (GTK_OBJECT (btn_receive), "clicked",
-                           GTK_SIGNAL_FUNC (sd_btn_receive_cb),
+       gtk_signal_connect (GTK_OBJECT (window), "delete_event",
+                           GTK_SIGNAL_FUNC (gtk_widget_hide_on_delete),
                            NULL);
-       gtk_signal_connect (GTK_OBJECT (btn_remove), "clicked",
-                           GTK_SIGNAL_FUNC (sd_btn_remove_cb),
+       gtk_signal_connect (GTK_OBJECT(window), "key_press_event",
+                           GTK_SIGNAL_FUNC(sd_key_pressed),
                            NULL);
-       gtk_signal_connect (GTK_OBJECT (btn_done), "clicked",
-                           GTK_SIGNAL_FUNC (sd_btn_done_cb),
+       MANAGE_WINDOW_SIGNALS_CONNECT(window);
+       gtk_signal_connect (GTK_OBJECT (preview_btn), "button_press_event",
+                           GTK_SIGNAL_FUNC (sd_preview_popup_cb),
                            NULL);
+
+       gtk_signal_connect (GTK_OBJECT (remove_btn), "clicked",
+                           GTK_SIGNAL_FUNC (sd_action_cb),
+                           GUINT_TO_POINTER(REMOVE));
+       gtk_signal_connect (GTK_OBJECT (download_btn), "clicked",
+                           GTK_SIGNAL_FUNC (sd_action_cb),
+                           GUINT_TO_POINTER(DOWNLOAD));
+       gtk_signal_connect (GTK_OBJECT (done_btn), "clicked",
+                           GTK_SIGNAL_FUNC (sd_action_cb),
+                           GUINT_TO_POINTER(DONE));
        gtk_signal_connect (GTK_OBJECT (clist), "select_row",
                            GTK_SIGNAL_FUNC (sd_select_row_cb),
                            NULL);
 
 
-
-       selective.mainwin            = mainwin;
-       selective.window             = window;
-       selective.btn_remove         = btn_remove;
-       selective.btn_done           = btn_done;
-       selective.clist              = clist;
-       selective.label_account_name = label_account_name;
-       selective.label_mails        = label_mails;
+       selective.mainwin         = mainwin;
+       selective.window          = window;
+       selective.clist           = clist;
+       selective.preview_btn     = preview_btn;
+       selective.remove_btn      = remove_btn;
+       selective.download_btn    = download_btn;
+       selective.ac_label        = ac_label;
+       selective.ac_button       = ac_button;
+       selective.ac_menu         = ac_menu;
+       selective.preview_popup   = preview_popup;
+       selective.msgs_label      = msgs_label;
+       selective.show_old_chkbtn = show_old_chkbtn;
 
        gtk_widget_show_all(window);
 }
index 3e892acab8d37b046c8e4b14455b3a283da8ef7e..eac309d8e17fb0492bcdb9cb2cf62e6b38ccd25d 100644 (file)
 
 #include "mainwindow.h"
 
-#define SIZE_HEADER "Complete-Size: "
-#define SIZE_HEADER_LEN strlen(SIZE_HEADER)
-
-#define CHECKED    TRUE
-#define UNCHECKED  FALSE
-
 typedef struct _HeaderItems HeaderItems;
 
+typedef enum {
+       SD_CHECKED,      /* checkbox selected for action */ 
+       SD_UNCHECKED,    /* checkbox untouched */
+       SD_REMOVE,       /* ask server to delete msg */
+       SD_REMOVED,      /* msg successfully removed from server */
+       SD_DOWNLOAD,     /* ask to download msg */
+       SD_DOWNLOADED,   /* msg successfully received + removed from server */
+} SD_State;
+
 struct _HeaderItems {
-       gint     index;
-       gboolean state;
-       gchar    *from;
-       gchar    *subject;
-       gchar    *size;
+       gint        index;     /* msg reference number on server */
+       SD_State    state;
+       gchar       *from;
+       gchar       *subject;
+       gchar       date[80];
+       gint        size;
+       guint       received : 1;
+       guint       del_by_old_session : 1;
 };
 
 void selective_download(MainWindow *mainwin);