diff --git a/e_mod_config_box.c b/e_mod_config_box.c index 14a42c2..15dcfb5 100644 --- a/e_mod_config_box.c +++ b/e_mod_config_box.c @@ -7,6 +7,7 @@ struct _E_Config_Dialog_Data int type; int use_exec; char *port; + int monitor; int ssl; int local; char *host; @@ -24,6 +25,7 @@ struct _E_Config_Dialog_Data Evas_Object *new_path_entry; Evas_Object *cur_path_label; Evas_Object *cur_path_entry; + Evas_Object *monitor_check; }; static void *_create_data (E_Config_Dialog * cfd); @@ -34,6 +36,7 @@ static int _basic_apply_data (E_Config_Dialog * cfd, E_Config_Dialog_Data * cfdata); static void _type_cb_change (void *data, Evas_Object * obj); static void _use_exec_cb_change (void *data, Evas_Object * obj); +static void _monitor_cb_change (void *data, Evas_Object * obj); static E_Config_Dialog *prev_dlg; static Config_Item *mail_ci; @@ -69,6 +72,7 @@ _fill_data (Config_Box * cb, E_Config_Dialog_Data * cfdata) if (!cb) { cfdata->type = 0; + cfdata->monitor = 1; cfdata->ssl = 0; cfdata->use_exec = 0; cfdata->local = 0; @@ -81,6 +85,7 @@ _fill_data (Config_Box * cb, E_Config_Dialog_Data * cfdata) cfdata->name = strdup (cb->name); cfdata->type = cb->type; + cfdata->monitor = cb->monitor; cfdata->ssl = cb->ssl; cfdata->use_exec = cb->use_exec; cfdata->local = cb->local; @@ -170,8 +175,23 @@ _basic_create_widgets (E_Config_Dialog * cfd, Evas * evas, e_widget_framelist_object_append (of, ob); ob = e_widget_radio_add (evas, D_("Mbox"), 3, rg); e_widget_on_change_hook_set (ob, _type_cb_change, cfdata); - e_widget_framelist_object_append (of, ob); - e_widget_list_object_append (o, of, 1, 1, 0.5); + e_widget_framelist_object_append (of, ob); + cfdata->monitor_check = e_widget_check_add (evas, D_("Monitor Mbox file permanently"), + &(cfdata->monitor)); + e_widget_on_change_hook_set (cfdata->monitor_check, _monitor_cb_change, cfdata); + if (cfdata->type == 3) + { + e_widget_check_checked_set (cfdata->monitor_check, cfdata->monitor); + e_widget_disabled_set (cfdata->monitor_check, 0); + } + else + { + e_widget_check_checked_set (cfdata->monitor_check, 0); + e_widget_disabled_set (cfdata->monitor_check, 1); + } + e_widget_framelist_object_append (of, cfdata->monitor_check); + + e_widget_list_object_append (o, of, 1, 1, 0.5); of = e_widget_frametable_add (evas, D_("Port Settings"), 1); @@ -257,6 +277,7 @@ _basic_apply_data (E_Config_Dialog * cfd, E_Config_Dialog_Data * cfdata) cb = E_NEW (Config_Box, 1); cb->type = 0; cb->port = 110; + cb->monitor = 1; cb->ssl = 0; cb->local = 0; is_new = 1; @@ -271,10 +292,12 @@ _basic_apply_data (E_Config_Dialog * cfd, E_Config_Dialog_Data * cfdata) cb->type = cfdata->type; cb->port = atoi (cfdata->port); + cb->monitor = cfdata->monitor; cb->ssl = cfdata->ssl; cb->local = cfdata->local; cb->use_exec = cfdata->use_exec; + if (cb->exec) evas_stringshare_del (cb->exec); if (cfdata->exec != NULL) @@ -318,11 +341,14 @@ _basic_apply_data (E_Config_Dialog * cfd, E_Config_Dialog_Data * cfdata) cb->cur_path = evas_stringshare_add (""); if (!is_new) - e_config_save_queue (); + { + e_config_save_queue (); + _mail_mbox_check_monitors (); + } if (is_new) { -// cfd->data = cb; +/* cfd->data = cb; */ mail_ci->boxes = evas_list_append (mail_ci->boxes, cb); e_config_save_queue (); _mail_box_added (mail_ci->id, cb->name); @@ -332,6 +358,17 @@ _basic_apply_data (E_Config_Dialog * cfd, E_Config_Dialog_Data * cfdata) return 1; } +static void +_monitor_cb_change (void *data, Evas_Object * obj) +{ + E_Config_Dialog_Data *cfdata; + + cfdata = data; + if (cfdata->type == MAIL_TYPE_MBOX) + { + } +} + static void _type_cb_change (void *data, Evas_Object * obj) { @@ -384,6 +421,17 @@ _type_cb_change (void *data, Evas_Object * obj) e_widget_disabled_set (cfdata->cur_path_entry, 0); e_widget_entry_text_set (cfdata->port_entry, ""); } + + if (cfdata->type == 3) + { + e_widget_check_checked_set (cfdata->monitor_check, 1); + e_widget_disabled_set (cfdata->monitor_check, 0); + } + else + { + e_widget_check_checked_set (cfdata->monitor_check, 0); + e_widget_disabled_set (cfdata->monitor_check, 1); + } } static void diff --git a/e_mod_main.c b/e_mod_main.c index fb222ca..1ff35c1 100644 --- a/e_mod_main.c +++ b/e_mod_main.c @@ -71,7 +71,7 @@ _gc_init (E_Gadcon * gc, const char *name, const char *id, const char *style) Mail *mail; Config_Item *ci; Evas_List *l, *j; - int have_pop = 0, have_imap = 0; + int have_pop = 0, have_imap = 0, have_mbox = 0; inst = E_NEW (Instance, 1); ci = _mail_config_item_get (id); @@ -132,7 +132,12 @@ _gc_init (E_Gadcon * gc, const char *name, const char *id, const char *style) _mail_mdir_add_mailbox (inst, cb); break; case MAIL_TYPE_MBOX: + have_mbox = 1; _mail_mbox_add_mailbox (inst, cb); + if (!inst->check_timer) + inst->check_timer = + ecore_timer_add ((ci->check_time * 60.0), _mail_cb_check, + inst); break; } } @@ -140,6 +145,8 @@ _gc_init (E_Gadcon * gc, const char *name, const char *id, const char *style) _mail_pop_check_mail (inst); if (have_imap) _mail_imap_check_mail (inst); + if (have_mbox) + _mail_mbox_check_mail (inst); } return gcc; } @@ -356,6 +363,7 @@ e_modapi_init (E_Module * m) E_CONFIG_VAL (D, T, name, STR); E_CONFIG_VAL (D, T, type, INT); E_CONFIG_VAL (D, T, port, INT); + E_CONFIG_VAL (D, T, monitor, UCHAR); E_CONFIG_VAL (D, T, ssl, UCHAR); E_CONFIG_VAL (D, T, local, UCHAR); E_CONFIG_VAL (D, T, host, STR); @@ -543,7 +551,7 @@ _mail_cb_check (void *data) Instance *inst = data; Config_Item *ci; Evas_List *l; - int have_imap = 0, have_pop = 0; + int have_imap = 0, have_pop = 0, have_mbox = 0; if (!inst) return 1; @@ -564,6 +572,7 @@ _mail_cb_check (void *data) case MAIL_TYPE_MDIR: break; case MAIL_TYPE_MBOX: + have_mbox = 1; break; case MAIL_TYPE_POP: have_pop = 1; @@ -574,13 +583,15 @@ _mail_cb_check (void *data) } } - if ((have_imap) || (have_pop)) + if ((have_imap) || (have_pop) || (have_mbox)) edje_object_signal_emit (inst->mail->mail_obj, "check_mail", ""); if (have_imap) _mail_imap_check_mail (inst); if (have_pop) - _mail_pop_check_mail (inst); + _mail_pop_check_mail (inst); + if (have_mbox) + _mail_mbox_check_mail (inst); return 1; } diff --git a/e_mod_main.h b/e_mod_main.h index 1bfbab3..91c1053 100644 --- a/e_mod_main.h +++ b/e_mod_main.h @@ -60,6 +60,7 @@ struct _Config_Box int port; unsigned char local; unsigned char ssl; + unsigned char monitor; const char *host; const char *user; const char *pass; diff --git a/mbox.c b/mbox.c index a4f96a7..c0aa483 100644 --- a/mbox.c +++ b/mbox.c @@ -4,8 +4,9 @@ static Evas_List *mboxes; -static void _mail_mbox_check_mail (void *data, Ecore_File_Monitor * monitor, - Ecore_File_Event event, const char *path); +static void _mail_mbox_check_mail_parser (Config_Box *cb); +static void _mail_mbox_check_mail_monitor (void *data, Ecore_File_Monitor * monitor, + Ecore_File_Event event, const char *path); void _mail_mbox_add_mailbox (void *data, void *data2) @@ -22,12 +23,45 @@ _mail_mbox_add_mailbox (void *data, void *data2) mb->data = data; mb->config->num_new = 0; mb->config->num_total = 0; - mb->monitor = - ecore_file_monitor_add (cb->new_path, _mail_mbox_check_mail, mb); + + if (mb->config->monitor) + mb->monitor = + ecore_file_monitor_add (mb->config->new_path, _mail_mbox_check_mail_monitor, mb); mboxes = evas_list_append (mboxes, mb); } +void +_mail_mbox_check_monitors () +{ + Evas_List *l; + + for (l = mboxes; l; l = l->next) + { + MboxClient *mb; + + mb = l->data; + if (!mb) + continue; + + if (mb->config->monitor) + { + if (!mb->monitor) + mb->monitor = + ecore_file_monitor_add (mb->config->new_path, _mail_mbox_check_mail_monitor, mb); + } + else + { + if (mb->monitor) + { + ecore_file_monitor_del (mb->monitor); + } + mb->monitor = NULL; + } + break; + } +} + void _mail_mbox_del_mailbox (void *data) { @@ -73,16 +107,51 @@ _mail_mbox_shutdown () } } +void _mail_mbox_check_mail (void *data) +{ + Evas_List *l; + Instance *inst; + int num_new_prev; + + inst = data; + if (!inst) + return; + + for (l = mboxes; l; l = l->next) + { + MboxClient *mb; + Config_Box *cb; + + mb = l->data; + if (!mb) + continue; + mb->data = inst; + + cb = mb->config; + if (!cb) + continue; + + num_new_prev = cb->num_new; + _mail_mbox_check_mail_parser(cb); + + /* Only launch the program if the number of new mails increased. + This is hacky but better than launching it every time there's + unread/new mail imho */ + + _mail_set_text (mb->data); + if ((mb->config->num_new > 0) && (mb->config->num_new > num_new_prev) + && (mb->config->use_exec) && (mb->config->exec)) + _mail_start_exe (mb->config); + } +} + /* PRIVATES */ static void -_mail_mbox_check_mail (void *data, Ecore_File_Monitor * monitor, - Ecore_File_Event event, const char *path) +_mail_mbox_check_mail_monitor (void *data, Ecore_File_Monitor * monitor, + Ecore_File_Event event, const char *path) { MboxClient *mb; Config_Box *cb; - FILE *f; - char buf[1024]; - int header; mb = data; if (!mb) @@ -91,33 +160,56 @@ _mail_mbox_check_mail (void *data, Ecore_File_Monitor * monitor, cb = mb->config; if (!cb) return; - if (!cb->new_path) - return; - if (!(f = fopen (cb->new_path, "r"))) - return; - - mb->config->num_new = 0; - mb->config->num_total = 0; - - while (fgets (buf, sizeof (buf), f)) - { - if (buf[0] == '\n') - header = 0; - else if (!strncmp (buf, "From ", 5)) - { - header = 1; - mb->config->num_total++; - mb->config->num_new++; - } - else if ((header) && (!strncmp (buf, "Status: ", 7)) - && (strchr (buf, 'R'))) - mb->config->num_new--; - } - fclose (f); + _mail_mbox_check_mail_parser(cb); _mail_set_text (mb->data); if ((mb->config->num_new > 0) && (mb->config->use_exec) && (mb->config->exec)) _mail_start_exe (mb->config); } + +static void +_mail_mbox_check_mail_parser (Config_Box *cb) +{ + FILE *f; + int header; + char buf[1024]; + + if (!cb->new_path) + return; + + if (!(f = fopen (cb->new_path, "r"))) + return; + + cb->num_new = 0; + cb->num_total = 0; + + while (fgets (buf, sizeof (buf), f)) + { + if (buf[0] == '\n') + header = 0; + else if (!strncmp (buf, "From ", 5)) + { + header = 1; + cb->num_total++; + cb->num_new++; + } + else if (header) + { + if ((!strncmp (buf, "Status: ", 8)) && (strchr (buf, 'R'))) + cb->num_new--; + /* Support for Mozilla/Thunderbird mbox format */ + else if (!strncmp (buf, "X-Mozilla-Status: ", 18)) + { + if (!strstr (buf, "0000")) + { + cb->num_new--; + if (strstr (buf, "0009")) + cb->num_total--; + } + } + } + } + fclose (f); +} diff --git a/mbox.h b/mbox.h index e411269..a1fc5ba 100644 --- a/mbox.h +++ b/mbox.h @@ -16,5 +16,6 @@ struct _MboxClient void _mail_mbox_add_mailbox(void *data, void *data2); void _mail_mbox_del_mailbox(void *data); void _mail_mbox_shutdown(); - +void _mail_mbox_check_mail(void *data); +void _mail_mbox_check_monitors (); #endif