diff --git a/notmuch-private.h b/notmuch-private.h index b67aa905..99fbb4b2 100644 --- a/notmuch-private.h +++ b/notmuch-private.h @@ -155,10 +155,8 @@ _notmuch_thread_create (const void *talloc_owner, const char *thread_id); void -_notmuch_thread_add_tag (notmuch_thread_t *thread, const char *tag); - -void -_notmuch_thread_set_subject (notmuch_thread_t *thread, const char *subject); +_notmuch_thread_add_message (notmuch_thread_t *thread, + notmuch_message_t *message); /* message.cc */ diff --git a/notmuch.c b/notmuch.c index c6807e83..ba5ba831 100644 --- a/notmuch.c +++ b/notmuch.c @@ -733,70 +733,6 @@ query_string_from_args (void *ctx, int argc, char *argv[]) return query_string; } -static int -search_command (int argc, char *argv[]) -{ - void *local = talloc_new (NULL); - notmuch_database_t *notmuch = NULL; - notmuch_query_t *query; - notmuch_thread_results_t *results; - notmuch_thread_t *thread; - notmuch_tags_t *tags; - char *query_str; - notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS; - - notmuch = notmuch_database_open (NULL); - if (notmuch == NULL) { - ret = 1; - goto DONE; - } - - query_str = query_string_from_args (local, argc, argv); - - query = notmuch_query_create (notmuch, query_str); - if (query == NULL) { - fprintf (stderr, "Out of memory\n"); - ret = 1; - goto DONE; - } - - for (results = notmuch_query_search_threads (query); - notmuch_thread_results_has_more (results); - notmuch_thread_results_advance (results)) - { - int first = 1; - - thread = notmuch_thread_results_get (results); - - printf ("%s %s", - notmuch_thread_get_thread_id (thread), - notmuch_thread_get_subject (thread)); - - printf (" ("); - for (tags = notmuch_thread_get_tags (thread); - notmuch_tags_has_more (tags); - notmuch_tags_advance (tags)) - { - if (! first) - printf (" "); - printf ("%s", notmuch_tags_get (tags)); - first = 0; - } - printf (")\n"); - - notmuch_thread_destroy (thread); - } - - notmuch_query_destroy (query); - - DONE: - if (notmuch) - notmuch_database_close (notmuch); - talloc_free (local); - - return ret; -} - /* Format a nice representation of 'time' relative to the current time. * * Examples include: @@ -868,6 +804,76 @@ _format_relative_date (void *ctx, time_t then) #undef HOUR #undef DAY +static int +search_command (int argc, char *argv[]) +{ + void *local = talloc_new (NULL); + notmuch_database_t *notmuch = NULL; + notmuch_query_t *query; + notmuch_thread_results_t *results; + notmuch_thread_t *thread; + notmuch_tags_t *tags; + char *query_str; + const char *relative_date; + time_t date; + notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS; + + notmuch = notmuch_database_open (NULL); + if (notmuch == NULL) { + ret = 1; + goto DONE; + } + + query_str = query_string_from_args (local, argc, argv); + + query = notmuch_query_create (notmuch, query_str); + if (query == NULL) { + fprintf (stderr, "Out of memory\n"); + ret = 1; + goto DONE; + } + + for (results = notmuch_query_search_threads (query); + notmuch_thread_results_has_more (results); + notmuch_thread_results_advance (results)) + { + int first = 1; + + thread = notmuch_thread_results_get (results); + + date = notmuch_thread_get_oldest_date (thread); + relative_date = _format_relative_date (local, date); + + printf ("%s (%s) %s", + notmuch_thread_get_thread_id (thread), + relative_date, + notmuch_thread_get_subject (thread)); + + printf (" ("); + for (tags = notmuch_thread_get_tags (thread); + notmuch_tags_has_more (tags); + notmuch_tags_advance (tags)) + { + if (! first) + printf (" "); + printf ("%s", notmuch_tags_get (tags)); + first = 0; + } + printf (")\n"); + + notmuch_thread_destroy (thread); + } + + notmuch_query_destroy (query); + + DONE: + if (notmuch) + notmuch_database_close (notmuch); + talloc_free (local); + + return ret; +} + /* Get a nice, single-line summary of message. */ static const char * _get_one_line_summary (void *ctx, notmuch_message_t *message) diff --git a/notmuch.h b/notmuch.h index 3f59709a..a3608ba0 100644 --- a/notmuch.h +++ b/notmuch.h @@ -488,6 +488,16 @@ notmuch_thread_get_thread_id (notmuch_thread_t *thread); const char * notmuch_thread_get_subject (notmuch_thread_t *thread); +/* Get the date of the oldest message in 'thread' as a time_t value. + */ +time_t +notmuch_thread_get_oldest_date (notmuch_thread_t *thread); + +/* Get the date of the oldest message in 'thread' as a time_t value. + */ +time_t +notmuch_thread_get_newest_date (notmuch_thread_t *thread); + /* Get the tags for 'thread', returning a notmuch_tags_t object which * can be used to iterate over all tags. * diff --git a/query.cc b/query.cc index 170ab752..2fe3965f 100644 --- a/query.cc +++ b/query.cc @@ -178,8 +178,6 @@ notmuch_query_search_threads (notmuch_query_t *query) const char *thread_id; notmuch_message_results_t *message_results; notmuch_message_t *message; - notmuch_tags_t *tags; - const char *tag; GHashTable *seen; thread_results = talloc (query, notmuch_thread_results_t); @@ -207,27 +205,15 @@ notmuch_query_search_threads (notmuch_query_t *query) thread_id, NULL, (void **) &thread)) { - const char *subject; - thread = _notmuch_thread_create (query, query->notmuch, thread_id); - subject = notmuch_message_get_header (message, "subject"); - - _notmuch_thread_set_subject (thread, subject); - g_hash_table_insert (seen, xstrdup (thread_id), thread); g_ptr_array_add (thread_results->threads, thread); } - for (tags = notmuch_message_get_tags (message); - notmuch_tags_has_more (tags); - notmuch_tags_advance (tags)) - { - tag = notmuch_tags_get (tags); - _notmuch_thread_add_tag (thread, tag); - } + _notmuch_thread_add_message (thread, message); notmuch_message_destroy (message); } diff --git a/thread.cc b/thread.cc index e4c9e111..b67dfade 100644 --- a/thread.cc +++ b/thread.cc @@ -30,6 +30,10 @@ struct _notmuch_thread { char *thread_id; char *subject; GHashTable *tags; + + notmuch_bool_t has_message; + time_t oldest; + time_t newest; }; static int @@ -74,6 +78,10 @@ _notmuch_thread_create (const void *talloc_owner, thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal, free, NULL); + thread->has_message = 0; + thread->oldest = 0; + thread->newest = 0; + return thread; } @@ -84,15 +92,36 @@ notmuch_thread_get_thread_id (notmuch_thread_t *thread) } void -_notmuch_thread_add_tag (notmuch_thread_t *thread, const char *tag) +_notmuch_thread_add_message (notmuch_thread_t *thread, + notmuch_message_t *message) { - g_hash_table_insert (thread->tags, xstrdup (tag), NULL); -} + notmuch_tags_t *tags; + const char *tag; + time_t date; -void -_notmuch_thread_set_subject (notmuch_thread_t *thread, const char *subject) -{ - thread->subject = talloc_strdup (thread, subject); + if (! thread->subject) { + const char *subject; + subject = notmuch_message_get_header (message, "subject"); + thread->subject = talloc_strdup (thread, subject); + } + + for (tags = notmuch_message_get_tags (message); + notmuch_tags_has_more (tags); + notmuch_tags_advance (tags)) + { + tag = notmuch_tags_get (tags); + g_hash_table_insert (thread->tags, xstrdup (tag), NULL); + } + + date = notmuch_message_get_date (message); + + if (date < thread->oldest || ! thread->has_message) + thread->oldest = date; + + if (date > thread->newest || ! thread->has_message) + thread->newest = date; + + thread->has_message = 1; } const char * @@ -101,6 +130,18 @@ notmuch_thread_get_subject (notmuch_thread_t *thread) return thread->subject; } +time_t +notmuch_thread_get_oldest_date (notmuch_thread_t *thread) +{ + return thread->oldest; +} + +time_t +notmuch_thread_get_newest_date (notmuch_thread_t *thread) +{ + return thread->newest; +} + notmuch_tags_t * notmuch_thread_get_tags (notmuch_thread_t *thread) {