]> git.notmuchmail.org Git - notmuch/commitdiff
Add public notmuch_thread_get_subject
authorCarl Worth <cworth@cworth.org>
Tue, 27 Oct 2009 00:35:31 +0000 (17:35 -0700)
committerCarl Worth <cworth@cworth.org>
Tue, 27 Oct 2009 00:35:31 +0000 (17:35 -0700)
And use this in "notmuch search" to display subject line as well as
thread ID.

message-file.c
message.cc
notmuch-private.h
notmuch.c
notmuch.h
query.cc
thread.cc

index a4c0860698d535eac3f9f5d1c2883e9e452314be..18275fbf853fc89ecb4c5b703c34a5a9b084e995 100644 (file)
@@ -70,12 +70,36 @@ strcase_hash (const void *ptr)
     return hash;
 }
 
+static int
+_notmuch_message_file_destructor (notmuch_message_file_t *message)
+{
+    if (message->line)
+       free (message->line);
+
+    if (message->value.size)
+       free (message->value.str);
+
+    if (message->headers)
+       g_hash_table_destroy (message->headers);
+
+    if (message->file)
+       fclose (message->file);
+
+    return 0;
+}
+
+/* Create a new notmuch_message_file_t for 'filename' with 'ctx' as
+ * the talloc owner. */
 notmuch_message_file_t *
-notmuch_message_file_open (const char *filename)
+_notmuch_message_file_open_ctx (void *ctx, const char *filename)
 {
     notmuch_message_file_t *message;
 
-    message = talloc_zero (NULL, notmuch_message_file_t);
+    message = talloc_zero (ctx, notmuch_message_file_t);
+    if (unlikely (message == NULL))
+       return NULL;
+
+    talloc_set_destructor (message, _notmuch_message_file_destructor);
 
     message->file = fopen (filename, "r");
     if (message->file == NULL)
@@ -98,24 +122,15 @@ notmuch_message_file_open (const char *filename)
     return NULL;
 }
 
+notmuch_message_file_t *
+notmuch_message_file_open (const char *filename)
+{
+    return _notmuch_message_file_open_ctx (NULL, filename);
+}
+
 void
 notmuch_message_file_close (notmuch_message_file_t *message)
 {
-    if (message == NULL)
-       return;
-
-    if (message->line)
-       free (message->line);
-
-    if (message->value.size)
-       free (message->value.str);
-
-    if (message->headers)
-       g_hash_table_destroy (message->headers);
-
-    if (message->file)
-       fclose (message->file);
-
     talloc_free (message);
 }
 
index 66dfc806d9f4e887b3fd2b3611667878184f05ea..6b6141f33fa4dbbd423866c77b1e9eb7c4dfaf6a 100644 (file)
@@ -29,6 +29,7 @@ struct _notmuch_message {
     char *message_id;
     char *thread_id;
     char *filename;
+    notmuch_message_file_t *message_file;
     Xapian::Document doc;
 };
 
@@ -98,9 +99,12 @@ _notmuch_message_create (const void *talloc_owner,
 
     message->notmuch = notmuch;
     message->doc_id = doc_id;
-    message->message_id = NULL; /* lazily created */
-    message->thread_id = NULL; /* lazily created */
-    message->filename = NULL; /* lazily created */
+
+    /* Each of these will be lazily created as needed. */
+    message->message_id = NULL;
+    message->thread_id = NULL;
+    message->filename = NULL;
+    message->message_file = NULL;
 
     /* This is C++'s creepy "placement new", which is really just an
      * ugly way to call a constructor for a pre-allocated object. So
@@ -224,6 +228,28 @@ notmuch_message_get_message_id (notmuch_message_t *message)
     return message->message_id;
 }
 
+const char *
+_notmuch_message_get_subject (notmuch_message_t *message)
+{
+    if (! message->message_file) {
+       notmuch_message_file_t *message_file;
+       const char *filename;
+
+       filename = notmuch_message_get_filename (message);
+       if (unlikely (filename == NULL))
+           return NULL;
+
+       message_file = _notmuch_message_file_open_ctx (message, filename);
+       if (unlikely (message_file == NULL))
+           return NULL;
+
+       message->message_file = message_file;
+    }
+
+    return notmuch_message_file_get_header (message->message_file,
+                                           "subject");
+}
+
 const char *
 notmuch_message_get_thread_id (notmuch_message_t *message)
 {
index ddc59b4ce5c056fbaf1408be862f882f9af47156..42f8d2756f7258b9145e12ff4b2d46e959ab1b22 100644 (file)
@@ -137,6 +137,16 @@ typedef enum _notmuch_private_status {
      :                                                                 \
      (notmuch_status_t) private_status)
 
+/* database.cc */
+
+/* Lookup a prefix value by name.
+ *
+ * XXX: This should really be static inside of message.cc, and we can
+ * do that once we convert database.cc to use the
+ * _notmuch_message_add/remove_term functions. */
+const char *
+_find_prefix (const char *name);
+
 /* thread.cc */
 
 notmuch_thread_t *
@@ -147,6 +157,12 @@ _notmuch_thread_create (const void *talloc_owner,
 void
 _notmuch_thread_add_tag (notmuch_thread_t *thread, const char *tag);
 
+void
+_notmuch_thread_set_subject (notmuch_thread_t *thread, const char *subject);
+
+const char *
+_notmuch_thread_get_subject (notmuch_thread_t *thread);
+
 /* message.cc */
 
 notmuch_message_t *
@@ -161,13 +177,8 @@ _notmuch_message_create_for_message_id (const void *talloc_owner,
                                        const char *message_id,
                                        notmuch_status_t *status);
 
-/* Lookup a prefix value by name.
- *
- * XXX: This should really be static inside of message.cc, and we can
- * do that once we convert database.cc to use the
- * _notmuch_message_add/remove_term functions. */
 const char *
-_find_prefix (const char *name);
+_notmuch_message_get_subject (notmuch_message_t *message);
 
 notmuch_private_status_t
 _notmuch_message_add_term (notmuch_message_t *message,
@@ -214,6 +225,10 @@ typedef struct _notmuch_message_file notmuch_message_file_t;
 notmuch_message_file_t *
 notmuch_message_file_open (const char *filename);
 
+/* Like notmuch_message_file_open but with 'ctx' as the talloc owner. */
+notmuch_message_file_t *
+_notmuch_message_file_open_ctx (void *ctx, const char *filename);
+
 /* Close a notmuch message preivously opened with notmuch_message_open. */
 void
 notmuch_message_file_close (notmuch_message_file_t *message);
index fd0e0b253c56388a8c5fd2bec01053016c4cb7b6..ae1d5976b71f426329e09964b9c412ba854ab606 100644 (file)
--- a/notmuch.c
+++ b/notmuch.c
@@ -651,8 +651,11 @@ search_command (int argc, char *argv[])
 
        thread = notmuch_thread_results_get (results);
 
-       printf ("%s (", notmuch_thread_get_thread_id (thread));
+       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))
@@ -662,7 +665,6 @@ search_command (int argc, char *argv[])
            printf ("%s", notmuch_tags_get (tags));
            first = 0;
        }
-
        printf (")\n");
 
        notmuch_thread_destroy (thread);
index b4ff53e34b223e03b3ad94570c85b0e949b0c036..bf390beaff52e6f6959d57d76b126b47f64e1bd4 100644 (file)
--- a/notmuch.h
+++ b/notmuch.h
@@ -458,6 +458,20 @@ notmuch_thread_results_destroy (notmuch_thread_results_t *results);
 const char *
 notmuch_thread_get_thread_id (notmuch_thread_t *thread);
 
+/* Get the subject of 'thread'
+ *
+ * The subject is taken from the first message (according to the query
+ * order---see notmuch_query_set_sort) in the query results that
+ * belongs to this thread.
+ *
+ * The returned string belongs to 'thread' and as such, should not be
+ * modified by the caller and will only be valid for as long as the
+ * thread is valid, (which is until notmuch_thread_destroy or until
+ * the query from which it derived is destroyed).
+ */
+const char *
+notmuch_thread_get_subject (notmuch_thread_t *thread);
+
 /* Get the tags for 'thread', returning a notmuch_tags_t object which
  * can be used to iterate over all tags.
  *
index c8a9149118b7793bfff7e2525a37e8bf4f29bdb0..5fac024e7a23be7f17d2d5eed4852b48445e27b4 100644 (file)
--- a/query.cc
+++ b/query.cc
@@ -207,9 +207,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_subject (message);
+
+           _notmuch_thread_set_subject (thread, subject);
+
            g_hash_table_insert (seen, xstrdup (thread_id), thread);
 
            g_ptr_array_add (thread_results->threads, thread);
@@ -222,6 +228,8 @@ notmuch_query_search_threads (notmuch_query_t *query)
            tag = notmuch_tags_get (tags);
            _notmuch_thread_add_tag (thread, tag);
        }
+
+       notmuch_message_destroy (message);
     }
 
     g_hash_table_unref (seen);
index 73d17722f574b3b052bd211e2e462f8a53637720..9b49c5502fe544479f82479aed4cfe002805777d 100644 (file)
--- a/thread.cc
+++ b/thread.cc
@@ -28,6 +28,7 @@
 struct _notmuch_thread {
     notmuch_database_t *notmuch;
     char *thread_id;
+    char *subject;
     GHashTable *tags;
 };
 
@@ -69,6 +70,7 @@ _notmuch_thread_create (const void *talloc_owner,
 
     thread->notmuch = notmuch;
     thread->thread_id = talloc_strdup (thread, thread_id);
+    thread->subject = NULL;
     thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
                                          free, NULL);
 
@@ -87,6 +89,18 @@ _notmuch_thread_add_tag (notmuch_thread_t *thread, const char *tag)
     g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
 }
 
+void
+_notmuch_thread_set_subject (notmuch_thread_t *thread, const char *subject)
+{
+    thread->subject = talloc_strdup (thread, subject);
+}
+
+const char *
+_notmuch_thread_get_subject (notmuch_thread_t *thread)
+{
+    return thread->subject;
+}
+
 notmuch_tags_t *
 notmuch_thread_get_tags (notmuch_thread_t *thread)
 {