]> rtime.felk.cvut.cz Git - notmuch.git/blobdiff - notmuch-search.c
cli: search: Do not output duplicate addresses
[notmuch.git] / notmuch-search.c
index 671fe4139981055e77e7f6c7c269a4bdb77ccb81..43d42c609756fb798a0ee678b259bce15818b1de 100644 (file)
@@ -229,6 +229,27 @@ do_search_threads (search_options_t *opt)
     return 0;
 }
 
+/* Returns TRUE iff name and addr is duplicate. */
+static notmuch_bool_t
+is_duplicate (const search_options_t *opt, GHashTable *addrs, const char *name, const char *addr)
+{
+    notmuch_bool_t duplicate;
+    char *key;
+
+    key = talloc_asprintf (opt->format, "%s <%s>", name, addr);
+    if (! key)
+       return FALSE;
+
+    duplicate = g_hash_table_lookup_extended (addrs, key, NULL, NULL);
+
+    if (! duplicate)
+       g_hash_table_insert (addrs, key, NULL);
+    else
+       talloc_free (key);
+
+    return duplicate;
+}
+
 static void
 print_mailbox (const search_options_t *opt, const mailbox_t *mailbox)
 {
@@ -263,7 +284,8 @@ print_mailbox (const search_options_t *opt, const mailbox_t *mailbox)
 
 /* Print addresses from InternetAddressList.  */
 static void
-process_address_list (const search_options_t *opt, InternetAddressList *list)
+process_address_list (const search_options_t *opt, GHashTable *addrs,
+                     InternetAddressList *list)
 {
     InternetAddress *address;
     int i;
@@ -279,7 +301,7 @@ process_address_list (const search_options_t *opt, InternetAddressList *list)
            if (group_list == NULL)
                continue;
 
-           process_address_list (opt, group_list);
+           process_address_list (opt, addrs, group_list);
        } else {
            InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
            mailbox_t mbx = {
@@ -287,6 +309,9 @@ process_address_list (const search_options_t *opt, InternetAddressList *list)
                .addr = internet_address_mailbox_get_addr (mailbox),
            };
 
+           if (is_duplicate (opt, addrs, mbx.name, mbx.addr))
+               continue;
+
            print_mailbox (opt, &mbx);
        }
     }
@@ -294,7 +319,7 @@ process_address_list (const search_options_t *opt, InternetAddressList *list)
 
 /* Print addresses from a message header.  */
 static void
-process_address_header (const search_options_t *opt, const char *value)
+process_address_header (const search_options_t *opt, GHashTable *addrs, const char *value)
 {
     InternetAddressList *list;
 
@@ -305,11 +330,17 @@ process_address_header (const search_options_t *opt, const char *value)
     if (list == NULL)
        return;
 
-    process_address_list (opt, list);
+    process_address_list (opt, addrs, list);
 
     g_object_unref (list);
 }
 
+static void
+_my_talloc_free_for_g_hash (void *ptr)
+{
+    talloc_free (ptr);
+}
+
 static int
 do_search_messages (search_options_t *opt)
 {
@@ -317,8 +348,13 @@ do_search_messages (search_options_t *opt)
     notmuch_messages_t *messages;
     notmuch_filenames_t *filenames;
     sprinter_t *format = opt->format;
+    GHashTable *addresses = NULL;
     int i;
 
+    if (opt->output & OUTPUT_ADDRESS_FLAGS)
+       addresses = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                          _my_talloc_free_for_g_hash, NULL);
+
     if (opt->offset < 0) {
        opt->offset += notmuch_query_count_messages (opt->query);
        if (opt->offset < 0)
@@ -366,7 +402,7 @@ do_search_messages (search_options_t *opt)
                const char *addrs;
 
                addrs = notmuch_message_get_header (message, "from");
-               process_address_header (opt, addrs);
+               process_address_header (opt, addresses, addrs);
            }
 
            if (opt->output & OUTPUT_RECIPIENTS) {
@@ -376,7 +412,7 @@ do_search_messages (search_options_t *opt)
 
                for (j = 0; j < ARRAY_SIZE (hdrs); j++) {
                    addrs = notmuch_message_get_header (message, hdrs[j]);
-                   process_address_header (opt, addrs);
+                   process_address_header (opt, addresses, addrs);
                }
            }
        }
@@ -384,6 +420,9 @@ do_search_messages (search_options_t *opt)
        notmuch_message_destroy (message);
     }
 
+    if (addresses)
+       g_hash_table_unref (addresses);
+
     notmuch_messages_destroy (messages);
 
     format->end (format);