[PATCH 3/3] cli: deduplicate addresses for --output=address-*

Jani Nikula jani at nikula.org
Sat Sep 6 05:53:30 PDT 2014


---
 notmuch-search.c | 32 ++++++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 6 deletions(-)

diff --git a/notmuch-search.c b/notmuch-search.c
index c84ecc31262c..c3ca3246bceb 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -218,7 +218,8 @@ do_search_threads (sprinter_t *format,
 }
 
 static void
-print_address_list (sprinter_t *format, InternetAddressList *list)
+print_address_list (sprinter_t *format, GHashTable *addrs,
+		    InternetAddressList *list)
 {
     InternetAddress *address;
     int i;
@@ -234,7 +235,7 @@ print_address_list (sprinter_t *format, InternetAddressList *list)
 	    if (group_list == NULL)
 		continue;
 
-	    print_address_list (format, group_list);
+	    print_address_list (format, addrs, group_list);
 	} else {
 	    InternetAddressMailbox *mailbox;
 	    const char *name;
@@ -246,6 +247,11 @@ print_address_list (sprinter_t *format, InternetAddressList *list)
 	    name = internet_address_get_name (address);
 	    addr = internet_address_mailbox_get_addr (mailbox);
 
+	    if (g_hash_table_lookup_extended (addrs, addr, NULL, NULL))
+		continue;
+
+	    g_hash_table_insert (addrs, talloc_strdup (NULL, addr), NULL);
+
 	    if (name && *name)
 		full_address = talloc_asprintf (NULL, "%s <%s>", name, addr);
 	    else
@@ -263,7 +269,7 @@ print_address_list (sprinter_t *format, InternetAddressList *list)
 }
 
 static void
-print_address_string (sprinter_t *format, const char *recipients)
+print_address_string (sprinter_t *format, GHashTable *addrs, const char *recipients)
 {
     InternetAddressList *list;
 
@@ -274,7 +280,13 @@ print_address_string (sprinter_t *format, const char *recipients)
     if (list == NULL)
 	return;
 
-    print_address_list (format, list);
+    print_address_list (format, addrs, list);
+}
+
+static void
+_my_talloc_free_for_g_hash (void *ptr)
+{
+    talloc_free (ptr);
 }
 
 static int
@@ -288,8 +300,13 @@ do_search_messages (sprinter_t *format,
     notmuch_message_t *message;
     notmuch_messages_t *messages;
     notmuch_filenames_t *filenames;
+    GHashTable *addresses = NULL;
     int i;
 
+    if (output & OUTPUT_ADDRESS_ALL)
+	addresses = g_hash_table_new_full (g_str_hash, g_str_equal,
+				       _my_talloc_free_for_g_hash, NULL);
+
     if (offset < 0) {
 	offset += notmuch_query_count_messages (query);
 	if (offset < 0)
@@ -341,7 +358,7 @@ do_search_messages (sprinter_t *format,
 		if (addrs == NULL || *addrs == '\0')
 		    addrs = notmuch_message_get_header (message, "from");
 
-		print_address_string (format, addrs);
+		print_address_string (format, addresses, addrs);
 	    }
 
 	    if (output & OUTPUT_ADDRESS_TO) {
@@ -351,7 +368,7 @@ do_search_messages (sprinter_t *format,
 
 		for (j = 0; j < ARRAY_SIZE (hdrs); j++) {
 		    addrs = notmuch_message_get_header (message, hdrs[j]);
-		    print_address_string (format, addrs);
+		    print_address_string (format, addresses, addrs);
 		}
 	    }
 	}
@@ -359,6 +376,9 @@ do_search_messages (sprinter_t *format,
 	notmuch_message_destroy (message);
     }
 
+    if (addresses)
+	g_hash_table_unref (addresses);
+
     notmuch_messages_destroy (messages);
 
     format->end (format);
-- 
2.1.0



More information about the notmuch mailing list