When called without arguments do the work by
installing observable action. This speeds up execution by the factor of three.
Showing
1 changed file
with
76 additions
and
40 deletions
... | @@ -24,6 +24,7 @@ | ... | @@ -24,6 +24,7 @@ |
24 | #include <sys/types.h> | 24 | #include <sys/types.h> |
25 | #include <sys/stat.h> | 25 | #include <sys/stat.h> |
26 | #include <time.h> | 26 | #include <time.h> |
27 | #include <mailutils/observer.h> | ||
27 | 28 | ||
28 | const char *argp_program_version = "scan (" PACKAGE_STRING ")"; | 29 | const char *argp_program_version = "scan (" PACKAGE_STRING ")"; |
29 | static char doc[] = "GNU MH scan"; | 30 | static char doc[] = "GNU MH scan"; |
... | @@ -72,8 +73,11 @@ static mh_format_t format; | ... | @@ -72,8 +73,11 @@ static mh_format_t format; |
72 | 73 | ||
73 | static mh_msgset_t msgset; | 74 | static mh_msgset_t msgset; |
74 | 75 | ||
76 | static char *buffer; | ||
77 | |||
75 | void list_message __P((mailbox_t mbox, message_t msg, size_t num, void *data)); | 78 | void list_message __P((mailbox_t mbox, message_t msg, size_t num, void *data)); |
76 | int scan __P((mailbox_t mbox)); | 79 | void print_header __P((mailbox_t mbox)); |
80 | void clear_screen __P((void)); | ||
77 | 81 | ||
78 | static int | 82 | static int |
79 | opt_handler (int key, char *arg, void *unused) | 83 | opt_handler (int key, char *arg, void *unused) |
... | @@ -128,12 +132,32 @@ opt_handler (int key, char *arg, void *unused) | ... | @@ -128,12 +132,32 @@ opt_handler (int key, char *arg, void *unused) |
128 | return 0; | 132 | return 0; |
129 | } | 133 | } |
130 | 134 | ||
135 | /* Observable Action this is being call at every message discover. */ | ||
136 | static int | ||
137 | action (observer_t o, size_t type) | ||
138 | { | ||
139 | static int counter; | ||
140 | mailbox_t mbox; | ||
141 | message_t msg = NULL; | ||
142 | size_t num; | ||
143 | |||
144 | if (type == MU_EVT_MESSAGE_ADD) | ||
145 | { | ||
146 | mbox = observer_get_owner (o); | ||
147 | counter++; | ||
148 | mailbox_get_message (mbox, counter, &msg); | ||
149 | mh_message_number (msg, &num); | ||
150 | list_message (mbox, msg, num, NULL); | ||
151 | } | ||
152 | return 0; | ||
153 | } | ||
131 | 154 | ||
132 | int | 155 | int |
133 | main (int argc, char **argv) | 156 | main (int argc, char **argv) |
134 | { | 157 | { |
135 | int index; | 158 | int index; |
136 | mailbox_t mbox; | 159 | mailbox_t mbox; |
160 | int status; | ||
137 | 161 | ||
138 | /* Native Language Support */ | 162 | /* Native Language Support */ |
139 | mu_init_nls (); | 163 | mu_init_nls (); |
... | @@ -148,12 +172,58 @@ main (int argc, char **argv) | ... | @@ -148,12 +172,58 @@ main (int argc, char **argv) |
148 | } | 172 | } |
149 | 173 | ||
150 | mbox = mh_open_folder (current_folder, 0); | 174 | mbox = mh_open_folder (current_folder, 0); |
151 | mh_msgset_parse (mbox, &msgset, argc - index, argv + index, "all"); | 175 | |
176 | buffer = xmalloc (width); | ||
177 | |||
178 | argc -= index; | ||
179 | argv += index; | ||
180 | if ((argc == 0 || strcmp (argv[0], "all") == 0) && !reverse) | ||
181 | { | ||
182 | /* Fast approach */ | ||
183 | observer_t observer; | ||
184 | observable_t observable; | ||
185 | size_t total; | ||
186 | |||
187 | print_header (mbox); | ||
188 | |||
189 | observer_create (&observer, mbox); | ||
190 | observer_set_action (observer, action, mbox); | ||
191 | mailbox_get_observable (mbox, &observable); | ||
192 | observable_attach (observable, MU_EVT_MESSAGE_ADD, observer); | ||
193 | |||
194 | status = mailbox_scan (mbox, 1, &total); | ||
195 | } | ||
196 | else | ||
197 | { | ||
198 | mh_msgset_parse (mbox, &msgset, argc, argv, "all"); | ||
152 | 199 | ||
153 | if (reverse) | 200 | if (reverse) |
154 | mh_msgset_reverse (&msgset); | 201 | mh_msgset_reverse (&msgset); |
155 | 202 | ||
156 | return scan (mbox); | 203 | print_header (mbox); |
204 | status = mh_iterate (mbox, &msgset, list_message, NULL); | ||
205 | } | ||
206 | |||
207 | clear_screen (); | ||
208 | mh_global_save_state (); | ||
209 | |||
210 | return status; | ||
211 | } | ||
212 | |||
213 | void | ||
214 | print_header (mailbox_t mbox) | ||
215 | { | ||
216 | if (header) | ||
217 | { | ||
218 | url_t url = NULL; | ||
219 | char datestr[64]; | ||
220 | time_t t; | ||
221 | |||
222 | mailbox_get_url (mbox, &url); | ||
223 | time (&t); | ||
224 | strftime (datestr, sizeof datestr, "%c", localtime (&t)); | ||
225 | printf (_("Folder %s %s\n"), url_to_string (url), datestr); | ||
226 | } | ||
157 | } | 227 | } |
158 | 228 | ||
159 | #ifdef HAVE_TERMCAP_H | 229 | #ifdef HAVE_TERMCAP_H |
... | @@ -197,44 +267,10 @@ clear_screen () | ... | @@ -197,44 +267,10 @@ clear_screen () |
197 | } | 267 | } |
198 | } | 268 | } |
199 | 269 | ||
200 | struct list_data { | ||
201 | char *buffer; | ||
202 | size_t width; | ||
203 | }; | ||
204 | |||
205 | int | ||
206 | scan (mailbox_t mbox) | ||
207 | { | ||
208 | struct list_data list_data; | ||
209 | |||
210 | list_data.buffer = xmalloc (width); | ||
211 | list_data.width = width; | ||
212 | |||
213 | if (header) | ||
214 | { | ||
215 | url_t url = NULL; | ||
216 | char datestr[64]; | ||
217 | time_t t; | ||
218 | |||
219 | mailbox_get_url (mbox, &url); | ||
220 | time (&t); | ||
221 | strftime (datestr, sizeof datestr, "%c", localtime (&t)); | ||
222 | printf (_("Folder %s %s\n"), url_to_string (url), datestr); | ||
223 | } | ||
224 | |||
225 | mh_iterate (mbox, &msgset, list_message, &list_data); | ||
226 | |||
227 | clear_screen (); | ||
228 | mh_global_save_state (); | ||
229 | return 0; | ||
230 | } | ||
231 | |||
232 | void | 270 | void |
233 | list_message (mailbox_t mbox, message_t msg, size_t num, void *data) | 271 | list_message (mailbox_t mbox, message_t msg, size_t num, void *data) |
234 | { | 272 | { |
235 | struct list_data *ld = data; | 273 | buffer[0] = 0; |
236 | 274 | mh_format (&format, msg, num, buffer, width); | |
237 | ld->buffer[0] = 0; | 275 | printf ("%s\n", buffer); |
238 | mh_format (&format, msg, num, ld->buffer, ld->width); | ||
239 | printf ("%s\n", ld->buffer); | ||
240 | } | 276 | } | ... | ... |
-
Please register or sign in to post a comment