local_mails: put code to where it belongs
authorPhil Sutter <phil@nwl.cc>
Sat, 24 Oct 2009 23:13:50 +0000 (01:13 +0200)
committerPhil Sutter <phil@nwl.cc>
Tue, 3 Nov 2009 00:50:28 +0000 (01:50 +0100)
Note that the code does not only use a pre-processor generator for
defining the print functions, but also for their prototypes. Sadly, this
generated a conflict in mboxscan.c which this patch resolves, too.

src/conky.c
src/core.c
src/mail.c
src/mail.h
src/mboxscan.c

index 13a6ea7..531b2ba 100644 (file)
@@ -1672,63 +1672,40 @@ void generate_text_internal(char *p, int p_max_size,
 
                        /* mail stuff */
                        OBJ(mails) {
-                               update_mail_count(&obj->data.local_mail);
-                               snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
+                               print_mails(obj, p, p_max_size);
                        }
                        OBJ(new_mails) {
-                               update_mail_count(&obj->data.local_mail);
-                               snprintf(p, p_max_size, "%d",
-                                       obj->data.local_mail.new_mail_count);
+                               print_new_mails(obj, p, p_max_size);
                        }
                        OBJ(seen_mails) {
-                               update_mail_count(&obj->data.local_mail);
-                               snprintf(p, p_max_size, "%d",
-                                       obj->data.local_mail.seen_mail_count);
+                               print_seen_mails(obj, p, p_max_size);
                        }
                        OBJ(unseen_mails) {
-                               update_mail_count(&obj->data.local_mail);
-                               snprintf(p, p_max_size, "%d",
-                                       obj->data.local_mail.unseen_mail_count);
+                               print_unseen_mails(obj, p, p_max_size);
                        }
                        OBJ(flagged_mails) {
-                               update_mail_count(&obj->data.local_mail);
-                               snprintf(p, p_max_size, "%d",
-                                       obj->data.local_mail.flagged_mail_count);
+                               print_flagged_mails(obj, p, p_max_size);
                        }
                        OBJ(unflagged_mails) {
-                               update_mail_count(&obj->data.local_mail);
-                               snprintf(p, p_max_size, "%d",
-                                       obj->data.local_mail.unflagged_mail_count);
+                               print_unflagged_mails(obj, p, p_max_size);
                        }
                        OBJ(forwarded_mails) {
-                               update_mail_count(&obj->data.local_mail);
-                               snprintf(p, p_max_size, "%d",
-                                       obj->data.local_mail.forwarded_mail_count);
+                               print_forwarded_mails(obj, p, p_max_size);
                        }
                        OBJ(unforwarded_mails) {
-                               update_mail_count(&obj->data.local_mail);
-                               snprintf(p, p_max_size, "%d",
-                                       obj->data.local_mail.unforwarded_mail_count);
+                               print_unforwarded_mails(obj, p, p_max_size);
                        }
                        OBJ(replied_mails) {
-                               update_mail_count(&obj->data.local_mail);
-                               snprintf(p, p_max_size, "%d",
-                                       obj->data.local_mail.replied_mail_count);
+                               print_replied_mails(obj, p, p_max_size);
                        }
                        OBJ(unreplied_mails) {
-                               update_mail_count(&obj->data.local_mail);
-                               snprintf(p, p_max_size, "%d",
-                                       obj->data.local_mail.unreplied_mail_count);
+                               print_unreplied_mails(obj, p, p_max_size);
                        }
                        OBJ(draft_mails) {
-                               update_mail_count(&obj->data.local_mail);
-                               snprintf(p, p_max_size, "%d",
-                                       obj->data.local_mail.draft_mail_count);
+                               print_draft_mails(obj, p, p_max_size);
                        }
                        OBJ(trashed_mails) {
-                               update_mail_count(&obj->data.local_mail);
-                               snprintf(p, p_max_size, "%d",
-                                       obj->data.local_mail.trashed_mail_count);
+                               print_trashed_mails(obj, p, p_max_size);
                        }
                        OBJ(mboxscan) {
                                mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
index 6d1a4e8..7856002 100644 (file)
@@ -689,214 +689,29 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
        END OBJ(kernel, 0)
        END OBJ(machine, 0)
        END OBJ(mails, 0)
-               float n1;
-               char mbox[256], dst[256];
-
-               if (!arg) {
-                       n1 = 9.5;
-                       /* Kapil: Changed from MAIL_FILE to
-                          current_mail_spool since the latter
-                          is a copy of the former if undefined
-                          but the latter should take precedence
-                          if defined */
-                       strncpy(mbox, current_mail_spool, sizeof(mbox));
-               } else {
-                       if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
-                               n1 = 9.5;
-                               strncpy(mbox, arg, sizeof(mbox));
-                       }
-               }
-
-               variable_substitute(mbox, dst, sizeof(dst));
-               obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
-               obj->data.local_mail.interval = n1;
+               parse_local_mail_args(obj, arg);
        END OBJ(new_mails, 0)
-               float n1;
-               char mbox[256], dst[256];
-
-               if (!arg) {
-                       n1 = 9.5;
-                       strncpy(mbox, current_mail_spool, sizeof(mbox));
-               } else {
-                       if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
-                               n1 = 9.5;
-                               strncpy(mbox, arg, sizeof(mbox));
-                       }
-               }
-
-               variable_substitute(mbox, dst, sizeof(dst));
-               obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
-               obj->data.local_mail.interval = n1;
+               parse_local_mail_args(obj, arg);
        END OBJ(seen_mails, 0)
-               float n1;
-               char mbox[256], dst[256];
-
-               if (!arg) {
-                       n1 = 9.5;
-                       strncpy(mbox, current_mail_spool, sizeof(mbox));
-               } else {
-                       if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
-                               n1 = 9.5;
-                               strncpy(mbox, arg, sizeof(mbox));
-                       }
-               }
-
-               variable_substitute(mbox, dst, sizeof(dst));
-               obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
-               obj->data.local_mail.interval = n1;
+               parse_local_mail_args(obj, arg);
        END OBJ(unseen_mails, 0)
-               float n1;
-               char mbox[256], dst[256];
-
-               if (!arg) {
-                       n1 = 9.5;
-                       strncpy(mbox, current_mail_spool, sizeof(mbox));
-               } else {
-                       if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
-                               n1 = 9.5;
-                               strncpy(mbox, arg, sizeof(mbox));
-                       }
-               }
-
-               variable_substitute(mbox, dst, sizeof(dst));
-               obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
-               obj->data.local_mail.interval = n1;
+               parse_local_mail_args(obj, arg);
        END OBJ(flagged_mails, 0)
-               float n1;
-               char mbox[256], dst[256];
-
-               if (!arg) {
-                       n1 = 9.5;
-                       strncpy(mbox, current_mail_spool, sizeof(mbox));
-               } else {
-                       if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
-                               n1 = 9.5;
-                               strncpy(mbox, arg, sizeof(mbox));
-                       }
-               }
-
-               variable_substitute(mbox, dst, sizeof(dst));
-               obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
-               obj->data.local_mail.interval = n1;
+               parse_local_mail_args(obj, arg);
        END OBJ(unflagged_mails, 0)
-               float n1;
-               char mbox[256], dst[256];
-
-               if (!arg) {
-                       n1 = 9.5;
-                       strncpy(mbox, current_mail_spool, sizeof(mbox));
-               } else {
-                       if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
-                               n1 = 9.5;
-                               strncpy(mbox, arg, sizeof(mbox));
-                       }
-               }
-
-               variable_substitute(mbox, dst, sizeof(dst));
-               obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
-               obj->data.local_mail.interval = n1;
+               parse_local_mail_args(obj, arg);
        END OBJ(forwarded_mails, 0)
-               float n1;
-               char mbox[256], dst[256];
-
-               if (!arg) {
-                       n1 = 9.5;
-                       strncpy(mbox, current_mail_spool, sizeof(mbox));
-               } else {
-                       if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
-                               n1 = 9.5;
-                               strncpy(mbox, arg, sizeof(mbox));
-                       }
-               }
-
-               variable_substitute(mbox, dst, sizeof(dst));
-               obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
-               obj->data.local_mail.interval = n1;
+               parse_local_mail_args(obj, arg);
        END OBJ(unforwarded_mails, 0)
-               float n1;
-               char mbox[256], dst[256];
-
-               if (!arg) {
-                       n1 = 9.5;
-                       strncpy(mbox, current_mail_spool, sizeof(mbox));
-               } else {
-                       if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
-                               n1 = 9.5;
-                               strncpy(mbox, arg, sizeof(mbox));
-                       }
-               }
-
-               variable_substitute(mbox, dst, sizeof(dst));
-               obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
-               obj->data.local_mail.interval = n1;
+               parse_local_mail_args(obj, arg);
        END OBJ(replied_mails, 0)
-               float n1;
-               char mbox[256], dst[256];
-
-               if (!arg) {
-                       n1 = 9.5;
-                       strncpy(mbox, current_mail_spool, sizeof(mbox));
-               } else {
-                       if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
-                               n1 = 9.5;
-                               strncpy(mbox, arg, sizeof(mbox));
-                       }
-               }
-
-               variable_substitute(mbox, dst, sizeof(dst));
-               obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
-               obj->data.local_mail.interval = n1;
+               parse_local_mail_args(obj, arg);
        END OBJ(unreplied_mails, 0)
-               float n1;
-               char mbox[256], dst[256];
-
-               if (!arg) {
-                       n1 = 9.5;
-                       strncpy(mbox, current_mail_spool, sizeof(mbox));
-               } else {
-                       if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
-                               n1 = 9.5;
-                               strncpy(mbox, arg, sizeof(mbox));
-                       }
-               }
-
-               variable_substitute(mbox, dst, sizeof(dst));
-               obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
-               obj->data.local_mail.interval = n1;
+               parse_local_mail_args(obj, arg);
        END OBJ(draft_mails, 0)
-               float n1;
-               char mbox[256], dst[256];
-
-               if (!arg) {
-                       n1 = 9.5;
-                       strncpy(mbox, current_mail_spool, sizeof(mbox));
-               } else {
-                       if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
-                               n1 = 9.5;
-                               strncpy(mbox, arg, sizeof(mbox));
-                       }
-               }
-
-               variable_substitute(mbox, dst, sizeof(dst));
-               obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
-               obj->data.local_mail.interval = n1;
+               parse_local_mail_args(obj, arg);
        END OBJ(trashed_mails, 0)
-               float n1;
-               char mbox[256], dst[256];
-
-               if (!arg) {
-                       n1 = 9.5;
-                       strncpy(mbox, current_mail_spool, sizeof(mbox));
-               } else {
-                       if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
-                               n1 = 9.5;
-                               strncpy(mbox, arg, sizeof(mbox));
-                       }
-               }
-
-               variable_substitute(mbox, dst, sizeof(dst));
-               obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
-               obj->data.local_mail.interval = n1;
+               parse_local_mail_args(obj, arg);
        END OBJ(mboxscan, 0)
                obj->data.mboxscan.args = (char *) malloc(text_buffer_size);
                obj->data.mboxscan.output = (char *) malloc(text_buffer_size);
index dbcdb1c..71ee319 100644 (file)
@@ -294,6 +294,51 @@ void update_mail_count(struct local_mail_s *mail)
        }
 }
 
+void parse_local_mail_args(struct text_object *obj, const char *arg)
+{
+       float n1;
+       char mbox[256], dst[256];
+
+       if (!arg) {
+               n1 = 9.5;
+               /* Kapil: Changed from MAIL_FILE to
+                  current_mail_spool since the latter
+                  is a copy of the former if undefined
+                  but the latter should take precedence
+                  if defined */
+               strncpy(mbox, current_mail_spool, sizeof(mbox));
+       } else {
+               if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
+                       n1 = 9.5;
+                       strncpy(mbox, arg, sizeof(mbox));
+               }
+       }
+
+       variable_substitute(mbox, dst, sizeof(dst));
+       obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
+       obj->data.local_mail.interval = n1;
+}
+
+#define PRINT_MAILS_GENERATOR(x) \
+void print_##x##mails(struct text_object *obj, char *p, int p_max_size) \
+{ \
+       update_mail_count(&obj->data.local_mail); \
+       snprintf(p, p_max_size, "%d", obj->data.local_mail.x##mail_count); \
+}
+
+PRINT_MAILS_GENERATOR()
+PRINT_MAILS_GENERATOR(new_)
+PRINT_MAILS_GENERATOR(seen_)
+PRINT_MAILS_GENERATOR(unseen_)
+PRINT_MAILS_GENERATOR(flagged_)
+PRINT_MAILS_GENERATOR(unflagged_)
+PRINT_MAILS_GENERATOR(forwarded_)
+PRINT_MAILS_GENERATOR(unforwarded_)
+PRINT_MAILS_GENERATOR(replied_)
+PRINT_MAILS_GENERATOR(unreplied_)
+PRINT_MAILS_GENERATOR(draft_)
+PRINT_MAILS_GENERATOR(trashed_)
+
 #define MAXDATASIZE 1000
 
 struct mail_s *parse_mail_args(char type, const char *arg)
index e6cb90e..a2f5a41 100644 (file)
@@ -53,6 +53,24 @@ void update_mail_count(struct local_mail_s *);
 #define POP3_TYPE 1
 #define IMAP_TYPE 2
 
+void parse_local_mail_args(struct text_object *, const char *);
+
+#define PRINT_MAILS_PROTO_GENERATOR(x) \
+void print_##x##mails(struct text_object *, char *, int);
+
+PRINT_MAILS_PROTO_GENERATOR()
+PRINT_MAILS_PROTO_GENERATOR(new_)
+PRINT_MAILS_PROTO_GENERATOR(seen_)
+PRINT_MAILS_PROTO_GENERATOR(unseen_)
+PRINT_MAILS_PROTO_GENERATOR(flagged_)
+PRINT_MAILS_PROTO_GENERATOR(unflagged_)
+PRINT_MAILS_PROTO_GENERATOR(forwarded_)
+PRINT_MAILS_PROTO_GENERATOR(unforwarded_)
+PRINT_MAILS_PROTO_GENERATOR(replied_)
+PRINT_MAILS_PROTO_GENERATOR(unreplied_)
+PRINT_MAILS_PROTO_GENERATOR(draft_)
+PRINT_MAILS_PROTO_GENERATOR(trashed_)
+
 /* FIXME: this is here for the config leftovers only */
 struct mail_s *parse_mail_args(char, const char *);
 
index 24b2f24..13f0582 100644 (file)
@@ -55,7 +55,7 @@ static double last_update;
 static int args_ok = 0;
 static int from_width;
 static int subject_width;
-static int print_mails;
+static int print_num_mails;
 static int time_delay;
 
 static char mbox_mail_spool[DEFAULT_TEXT_BUFFER_SIZE];
@@ -81,14 +81,14 @@ void mbox_scan(char *args, char *output, size_t max_len)
                char *substr = strstr(args, "-n");
 
                if (substr) {
-                       if (sscanf(substr, "-n %i", &print_mails) != 1) {
-                               print_mails = PRINT_MAILS;
+                       if (sscanf(substr, "-n %i", &print_num_mails) != 1) {
+                               print_num_mails = PRINT_MAILS;
                        }
                } else {
-                       print_mails = PRINT_MAILS;
+                       print_num_mails = PRINT_MAILS;
                }
-               if (print_mails < 1) {
-                       print_mails = 1;
+               if (print_num_mails < 1) {
+                       print_num_mails = 1;
                }
 
                substr = strstr(args, "-t");
@@ -181,7 +181,7 @@ void mbox_scan(char *args, char *output, size_t max_len)
 
        /* build up double-linked ring-list to hold data, while scanning down the
         * mbox */
-       for (i = 0; i < print_mails; i++) {
+       for (i = 0; i < print_num_mails; i++) {
                curr = (struct ring_list *) malloc(sizeof(struct ring_list));
                curr->from = (char *) malloc(sizeof(char[from_width + 1]));
                curr->subject = (char *) malloc(sizeof(char[subject_width + 1]));
@@ -340,11 +340,11 @@ void mbox_scan(char *args, char *output, size_t max_len)
 
        output[0] = '\0';
 
-       i = print_mails;
+       i = print_num_mails;
        while (i) {
                struct ring_list *tmp;
                if (curr->from[0] != '\0') {
-                       if (i != print_mails) {
+                       if (i != print_num_mails) {
                                snprintf(buf, text_buffer_size, "\nF: %-*s S: %-*s", from_width,
                                        curr->from, subject_width, curr->subject);
                        } else {        /* first time - no \n in front */