Added work offline availability.
[claws.git] / src / inc.c
index 37b535a3534d2bd7b982e7e74698ae8ce42a6a0c..de6183bc6bd3a82e28e485f59a74110ee08466ab 100644 (file)
--- a/src/inc.c
+++ b/src/inc.c
@@ -145,7 +145,7 @@ static void inc_finished(MainWindow *mainwin, gboolean new_messages)
 
        if (prefs_common.open_inbox_on_inc) {
                item = cur_account && cur_account->inbox
-                       ? folder_find_item_from_path(cur_account->inbox)
+                       ? folder_find_item_from_identifier(cur_account->inbox)
                        : folder_get_default_inbox();
                if (FOLDER_SUMMARY_MISMATCH(item, mainwin->summaryview)) {      
                        folderview_unselect(mainwin->folderview);
@@ -166,6 +166,12 @@ void inc_mail(MainWindow *mainwin, gboolean notify)
 
        if (inc_lock_count) return;
 
+       if (prefs_common.work_offline)
+               if (alertpanel(_("Offline warning"), 
+                              _("You're working offline. Override?"),
+                              _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
+               return;
+
        inc_autocheck_timer_remove();
        summary_write_cache(mainwin->summaryview);
        main_window_lock(mainwin);
@@ -249,6 +255,12 @@ void inc_all_account_mail(MainWindow *mainwin, gboolean notify)
        GList *list, *queue_list = NULL;
        IncProgressDialog *inc_dialog;
        gint new_msgs = 0;
+       
+       if (prefs_common.work_offline)
+               if (alertpanel(_("Offline warning"), 
+                              _("You're working offline. Override?"),
+                              _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
+               return;
 
        if (inc_lock_count) return;
 
@@ -420,6 +432,7 @@ static Pop3State *inc_pop3_state_new(PrefsAccount *account)
 
        state->ac_prefs = account;
        state->folder_table = g_hash_table_new(NULL, NULL);
+       state->uidl_todelete_list = NULL;
        state->uidl_table = inc_get_uidl_table(account);
        state->inc_state = INC_SUCCESS;
 
@@ -440,7 +453,7 @@ static void inc_pop3_state_destroy(Pop3State *state)
                hash_free_strings(state->uidl_table);
                g_hash_table_destroy(state->uidl_table);
        }
-
+       g_slist_free(state->uidl_todelete_list);
        g_free(state->greeting);
        g_free(state->user);
        g_free(state->pass);
@@ -770,10 +783,13 @@ static GHashTable *inc_get_uidl_table(PrefsAccount *ac_prefs)
        gchar *path;
        FILE *fp;
        gchar buf[IDLEN + 3];
+       GDate curdate;
+       gchar **data;
 
        path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
                           "uidl-", ac_prefs->recv_server,
                           "-", ac_prefs->userid, NULL);
+                          
        if ((fp = fopen(path, "rb")) == NULL) {
                if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
                g_free(path);
@@ -783,13 +799,37 @@ static GHashTable *inc_get_uidl_table(PrefsAccount *ac_prefs)
 
        table = g_hash_table_new(g_str_hash, g_str_equal);
 
+       g_date_clear(&curdate, 1);
+
+       /*
+        * NOTE: g_date_set_time() has to be called inside this 
+        * loop, because a day change may happen??? That right?
+        */
+
        while (fgets(buf, sizeof(buf), fp) != NULL) {
                strretchomp(buf);
-               g_hash_table_insert(table, g_strdup(buf), GINT_TO_POINTER(1));
+               
+               /* data[0] will contain uidl
+                * data[1] will contain day of retrieval */
+
+               /* 
+                * FIXME: convoluted implementation. need to find
+                * a better way to split the string.
+                */
+               if (strchr(buf, '\t')) {
+                       data = g_strsplit(buf, "\t", 2);
+                       if (data) {
+                               g_hash_table_insert(table, g_strdup(data[0]), g_strdup(data[1]));
+                               g_strfreev(data);
+                       }       
+               } else {
+                       g_date_set_time(&curdate, time(NULL));  
+                       g_hash_table_insert(table, g_strdup(buf), 
+                                           g_strdup_printf("%d", g_date_day_of_year(&curdate)));
+               }                           
        }
 
        fclose(fp);
-
        return table;
 }
 
@@ -798,9 +838,13 @@ static void inc_write_uidl_list(Pop3State *state)
        gchar *path;
        FILE *fp;
        gint n;
+       GDate curdate;
+       const char *sdate;
+       int tdate;
 
-       if (!state->uidl_is_valid) return;
-
+       if (!state->uidl_is_valid)
+               return;
+       
        path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
                           "uidl-", state->ac_prefs->recv_server,
                           "-", state->user, NULL);
@@ -810,6 +854,8 @@ static void inc_write_uidl_list(Pop3State *state)
                return;
        }
 
+       g_date_clear(&curdate, 1);
+
        for (n = 1; n <= state->count; n++) {
                if (state->msg[n].uidl && state->msg[n].received &&
                    !state->msg[n].deleted) {
@@ -817,14 +863,38 @@ static void inc_write_uidl_list(Pop3State *state)
                                FILE_OP_ERROR(path, "fputs");
                                break;
                        }
-                       if (fputc('\n', fp) == EOF) {
+                       if (fputc('\t', fp) == EOF) {
                                FILE_OP_ERROR(path, "fputc");
                                break;
                        }
+                       
+                       /*
+                        * NOTE: need to set time to watch for day changes??
+                        */
+                       g_date_set_time(&curdate, time(NULL));
+
+                       if (NULL != (sdate = g_hash_table_lookup(state->uidl_table, state->msg[n].uidl))) {
+                               tdate = sdate != NULL ? atoi(sdate) : g_date_day_of_year(&curdate);
+                               if (fprintf(fp, "%3d", tdate) == EOF) {
+                                       FILE_OP_ERROR(path, "fprintf");
+                                       break;
+                               }
+                       } else {
+                               if (fprintf(fp, "%d", g_date_day_of_year(&curdate)) == EOF) {
+                                       FILE_OP_ERROR(path, "fputs");
+                                       break;
+                               }
+                       }
+
+                       if (fputc('\n', fp) == EOF) {
+                               FILE_OP_ERROR(path, "fputc");
+                               break;
+                       }               
                }
        }
 
-       if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
+       if (fclose(fp) == EOF) 
+               FILE_OP_ERROR(path, "fclose");
        g_free(path);
 }
 
@@ -958,7 +1028,8 @@ gint inc_drop_message(const gchar *file, Pop3State *state)
        /* CLAWS: get default inbox (perhaps per account) */
        if (state->ac_prefs->inbox) {
                /* CLAWS: get destination folder / mailbox */
-               inbox = folder_find_item_from_identifier(state->ac_prefs->inbox);
+               inbox = folder_find_item_from_identifier
+                       (state->ac_prefs->inbox);
                if (!inbox)
                        inbox = folder_get_default_inbox();
        } else
@@ -1183,7 +1254,7 @@ static void inc_notify_cmd(gint new_msgs, gboolean notify)
        else
                buf = g_strdup(prefs_common.newmail_notify_cmd);
 
-       system(buf);
+       execute_command_line(buf, TRUE);
 
        g_free(buf);
 }
@@ -1197,8 +1268,10 @@ void inc_autocheck_timer_init(MainWindow *mainwin)
 static void inc_autocheck_timer_set_interval(guint interval)
 {
        inc_autocheck_timer_remove();
-
-       if (prefs_common.autochk_newmail && autocheck_data) {
+       /* last test is to avoid re-enabling auto_check after modifying 
+          the common preferences */
+       if (prefs_common.autochk_newmail && autocheck_data
+           && prefs_common.work_offline == FALSE) {
                autocheck_timer = gtk_timeout_add
                        (interval, inc_autocheck_func, autocheck_data);
                debug_print("added timer = %d\n", autocheck_timer);