Add public notmuch_thread_get_subject

And use this in "notmuch search" to display subject line as well as
thread ID.
This commit is contained in:
Carl Worth 2009-10-26 17:35:31 -07:00
parent 8e96a87fff
commit c12823648e
7 changed files with 122 additions and 28 deletions

View file

@ -70,12 +70,36 @@ strcase_hash (const void *ptr)
return hash; 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_t *
notmuch_message_file_open (const char *filename) _notmuch_message_file_open_ctx (void *ctx, const char *filename)
{ {
notmuch_message_file_t *message; 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"); message->file = fopen (filename, "r");
if (message->file == NULL) if (message->file == NULL)
@ -98,24 +122,15 @@ notmuch_message_file_open (const char *filename)
return NULL; return NULL;
} }
notmuch_message_file_t *
notmuch_message_file_open (const char *filename)
{
return _notmuch_message_file_open_ctx (NULL, filename);
}
void void
notmuch_message_file_close (notmuch_message_file_t *message) 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); talloc_free (message);
} }

View file

@ -29,6 +29,7 @@ struct _notmuch_message {
char *message_id; char *message_id;
char *thread_id; char *thread_id;
char *filename; char *filename;
notmuch_message_file_t *message_file;
Xapian::Document doc; Xapian::Document doc;
}; };
@ -98,9 +99,12 @@ _notmuch_message_create (const void *talloc_owner,
message->notmuch = notmuch; message->notmuch = notmuch;
message->doc_id = doc_id; message->doc_id = doc_id;
message->message_id = NULL; /* lazily created */
message->thread_id = NULL; /* lazily created */ /* Each of these will be lazily created as needed. */
message->filename = NULL; /* lazily created */ 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 /* This is C++'s creepy "placement new", which is really just an
* ugly way to call a constructor for a pre-allocated object. So * 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; 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 * const char *
notmuch_message_get_thread_id (notmuch_message_t *message) notmuch_message_get_thread_id (notmuch_message_t *message)
{ {

View file

@ -137,6 +137,16 @@ typedef enum _notmuch_private_status {
: \ : \
(notmuch_status_t) 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 */ /* thread.cc */
notmuch_thread_t * notmuch_thread_t *
@ -147,6 +157,12 @@ _notmuch_thread_create (const void *talloc_owner,
void void
_notmuch_thread_add_tag (notmuch_thread_t *thread, const char *tag); _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 */ /* message.cc */
notmuch_message_t * notmuch_message_t *
@ -161,13 +177,8 @@ _notmuch_message_create_for_message_id (const void *talloc_owner,
const char *message_id, const char *message_id,
notmuch_status_t *status); 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 * const char *
_find_prefix (const char *name); _notmuch_message_get_subject (notmuch_message_t *message);
notmuch_private_status_t notmuch_private_status_t
_notmuch_message_add_term (notmuch_message_t *message, _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_t *
notmuch_message_file_open (const char *filename); 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. */ /* Close a notmuch message preivously opened with notmuch_message_open. */
void void
notmuch_message_file_close (notmuch_message_file_t *message); notmuch_message_file_close (notmuch_message_file_t *message);

View file

@ -651,8 +651,11 @@ search_command (int argc, char *argv[])
thread = notmuch_thread_results_get (results); 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); for (tags = notmuch_thread_get_tags (thread);
notmuch_tags_has_more (tags); notmuch_tags_has_more (tags);
notmuch_tags_advance (tags)) notmuch_tags_advance (tags))
@ -662,7 +665,6 @@ search_command (int argc, char *argv[])
printf ("%s", notmuch_tags_get (tags)); printf ("%s", notmuch_tags_get (tags));
first = 0; first = 0;
} }
printf (")\n"); printf (")\n");
notmuch_thread_destroy (thread); notmuch_thread_destroy (thread);

View file

@ -458,6 +458,20 @@ notmuch_thread_results_destroy (notmuch_thread_results_t *results);
const char * const char *
notmuch_thread_get_thread_id (notmuch_thread_t *thread); 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 /* Get the tags for 'thread', returning a notmuch_tags_t object which
* can be used to iterate over all tags. * can be used to iterate over all tags.
* *

View file

@ -207,9 +207,15 @@ notmuch_query_search_threads (notmuch_query_t *query)
thread_id, NULL, thread_id, NULL,
(void **) &thread)) (void **) &thread))
{ {
const char *subject;
thread = _notmuch_thread_create (query, query->notmuch, thread = _notmuch_thread_create (query, query->notmuch,
thread_id); thread_id);
subject = _notmuch_message_get_subject (message);
_notmuch_thread_set_subject (thread, subject);
g_hash_table_insert (seen, xstrdup (thread_id), thread); g_hash_table_insert (seen, xstrdup (thread_id), thread);
g_ptr_array_add (thread_results->threads, 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); tag = notmuch_tags_get (tags);
_notmuch_thread_add_tag (thread, tag); _notmuch_thread_add_tag (thread, tag);
} }
notmuch_message_destroy (message);
} }
g_hash_table_unref (seen); g_hash_table_unref (seen);

View file

@ -28,6 +28,7 @@
struct _notmuch_thread { struct _notmuch_thread {
notmuch_database_t *notmuch; notmuch_database_t *notmuch;
char *thread_id; char *thread_id;
char *subject;
GHashTable *tags; GHashTable *tags;
}; };
@ -69,6 +70,7 @@ _notmuch_thread_create (const void *talloc_owner,
thread->notmuch = notmuch; thread->notmuch = notmuch;
thread->thread_id = talloc_strdup (thread, thread_id); thread->thread_id = talloc_strdup (thread, thread_id);
thread->subject = NULL;
thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal, thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
free, NULL); 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); 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_tags_t *
notmuch_thread_get_tags (notmuch_thread_t *thread) notmuch_thread_get_tags (notmuch_thread_t *thread)
{ {