aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/thread.cc
diff options
context:
space:
mode:
authorGravatar Carl Worth <cworth@cworth.org>2009-11-12 21:09:54 -0800
committerGravatar Carl Worth <cworth@cworth.org>2009-11-12 21:09:54 -0800
commitec6d3506db0b614ff754293cfb83fe9e93dc66c8 (patch)
tree8c46336e273bf5c063701e342ea22ca4f612616f /lib/thread.cc
parentfc2053b022e9b87928a29fa4af111f0cfce719fe (diff)
notmuch search: Print all authors contributing to a thread.
We've now expanded the notmuch_thread_create function to fire off a secondary database query to find all the messages that belong to this particular thread. This allows us to now have the complete authors' list for the thread, and will also make it trivial to print accurate message counts for threads in the future.
Diffstat (limited to 'lib/thread.cc')
-rw-r--r--lib/thread.cc127
1 files changed, 76 insertions, 51 deletions
diff --git a/lib/thread.cc b/lib/thread.cc
index e85e2afd..197b5ebd 100644
--- a/lib/thread.cc
+++ b/lib/thread.cc
@@ -46,57 +46,9 @@ _notmuch_thread_destructor (notmuch_thread_t *thread)
return 0;
}
-/* Create a new notmuch_thread_t object for an existing document in
- * the database.
- *
- * Here, 'talloc owner' is an optional talloc context to which the new
- * thread will belong. This allows for the caller to not bother
- * calling notmuch_thread_destroy on the thread, and know that all
- * memory will be reclaimed with 'talloc_owner' is freed. The caller
- * still can call notmuch_thread_destroy when finished with the
- * thread if desired.
- *
- * The 'talloc_owner' argument can also be NULL, in which case the
- * caller *is* responsible for calling notmuch_thread_destroy.
- *
- * This function returns NULL in the case of any error.
- */
-notmuch_thread_t *
-_notmuch_thread_create (const void *talloc_owner,
- notmuch_database_t *notmuch,
- const char *thread_id)
-{
- notmuch_thread_t *thread;
-
- thread = talloc (talloc_owner, notmuch_thread_t);
- if (unlikely (thread == NULL))
- return NULL;
-
- talloc_set_destructor (thread, _notmuch_thread_destructor);
-
- thread->notmuch = notmuch;
- thread->thread_id = talloc_strdup (thread, thread_id);
- thread->subject = NULL;
- thread->authors = NULL;
- 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;
-}
-
-const char *
-notmuch_thread_get_thread_id (notmuch_thread_t *thread)
-{
- return thread->thread_id;
-}
-
-void
-_notmuch_thread_add_message (notmuch_thread_t *thread,
- notmuch_message_t *message)
+static void
+_thread_add_message (notmuch_thread_t *thread,
+ notmuch_message_t *message)
{
notmuch_tags_t *tags;
const char *tag;
@@ -153,6 +105,79 @@ _notmuch_thread_add_message (notmuch_thread_t *thread,
thread->has_message = 1;
}
+/* Create a new notmuch_thread_t object for the given thread ID.
+ *
+ * Creating the thread will trigger a database search for the messages
+ * belonging to the thread so that the thread object can return some
+ * details about them, (authors, subject, etc.).
+ *
+ * Here, 'talloc owner' is an optional talloc context to which the new
+ * thread will belong. This allows for the caller to not bother
+ * calling notmuch_thread_destroy on the thread, and know that all
+ * memory will be reclaimed with 'talloc_owner' is freed. The caller
+ * still can call notmuch_thread_destroy when finished with the
+ * thread if desired.
+ *
+ * The 'talloc_owner' argument can also be NULL, in which case the
+ * caller *is* responsible for calling notmuch_thread_destroy.
+ *
+ * This function returns NULL in the case of any error.
+ */
+notmuch_thread_t *
+_notmuch_thread_create (const void *ctx,
+ notmuch_database_t *notmuch,
+ const char *thread_id)
+{
+ notmuch_thread_t *thread;
+ const char *query_string;
+ notmuch_query_t *query;
+ notmuch_messages_t *messages;
+
+ query_string = talloc_asprintf (ctx, "thread:%s", thread_id);
+ if (unlikely (query_string == NULL))
+ return NULL;
+
+ query = notmuch_query_create (notmuch, query_string);
+ if (unlikely (query == NULL))
+ return NULL;
+
+ thread = talloc (ctx, notmuch_thread_t);
+ if (unlikely (thread == NULL))
+ return NULL;
+
+ talloc_set_destructor (thread, _notmuch_thread_destructor);
+
+ thread->notmuch = notmuch;
+ thread->thread_id = talloc_strdup (thread, thread_id);
+ thread->subject = NULL;
+ thread->authors = NULL;
+ 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;
+
+ notmuch_query_set_sort (query, NOTMUCH_SORT_DATE_OLDEST_FIRST);
+
+ for (messages = notmuch_query_search_messages (query, 0, -1);
+ notmuch_messages_has_more (messages);
+ notmuch_messages_advance (messages))
+ {
+ _thread_add_message (thread, notmuch_messages_get (messages));
+ }
+
+ notmuch_query_destroy (query);
+
+ return thread;
+}
+
+const char *
+notmuch_thread_get_thread_id (notmuch_thread_t *thread)
+{
+ return thread->thread_id;
+}
+
const char *
notmuch_thread_get_authors (notmuch_thread_t *thread)
{