[notmuch] [PATCH] Added regress option to threads iterator

2009-12-09 Thread Ruben Pollan
Added the functions notmuch_threads_regress and notmuch_threads_is_first to
notmuch library. With them is possible to iterate backwards on threads.

* notmuch_threads_regress do the opposite than notmuch_threads_advance,
  getting the threads iterator one position backwards.

* notmuch_threads_is_first return TRUE if the iterator is in the first
  thread.
---
 lib/notmuch.h |8 
 lib/query.cc  |   28 
 2 files changed, 36 insertions(+), 0 deletions(-)

diff --git a/lib/notmuch.h b/lib/notmuch.h
index 69bd98a..e28ce46 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -429,6 +429,10 @@ notmuch_query_destroy (notmuch_query_t *query);
 notmuch_bool_t
 notmuch_threads_has_more (notmuch_threads_t *threads);

+/* Is the given notmuch_threads_t object on the first threads */
+notmuch_bool_t
+notmuch_threads_is_first (notmuch_threads_t *threads);
+
 /* Get the current thread from 'threads' as a notmuch_thread_t.
  *
  * Note: The returned thread belongs to 'threads' and has a lifetime
@@ -451,6 +455,10 @@ notmuch_threads_get (notmuch_threads_t *threads);
 void
 notmuch_threads_advance (notmuch_threads_t *threads);

+/* Regress the 'threads' iterator to the previous result. */
+void
+notmuch_threads_regress (notmuch_threads_t *threads);
+
 /* Destroy a notmuch_threads_t object.
  *
  * It's not strictly necessary to call this function. All memory from
diff --git a/lib/query.cc b/lib/query.cc
index 94a6860..cade17b 100644
--- a/lib/query.cc
+++ b/lib/query.cc
@@ -310,6 +310,12 @@ notmuch_threads_has_more (notmuch_threads_t *threads)
 return FALSE;
 }

+notmuch_bool_t
+notmuch_threads_is_first (notmuch_threads_t *threads)
+{
+return (g_hash_table_size (threads->threads) <= 1);
+}
+
 notmuch_thread_t *
 notmuch_threads_get (notmuch_threads_t *threads)
 {
@@ -329,6 +335,28 @@ notmuch_threads_advance (notmuch_threads_t *threads)
 }

 void
+notmuch_threads_regress (notmuch_threads_t *threads)
+{
+notmuch_message_t *message;
+   const char *thread_id;
+
+   thread_id = threads->thread_id;
+
+while (!notmuch_messages_is_first (threads->messages))
+{
+   notmuch_messages_regress (threads->messages);
+   message = notmuch_messages_get (threads->messages);
+   threads->thread_id = notmuch_message_get_thread_id (message);
+
+   if (strcmp (threads->thread_id, thread_id))
+   {
+   g_hash_table_remove (threads->threads, thread_id);
+   return;
+   }
+}
+}
+
+void
 notmuch_threads_destroy (notmuch_threads_t *threads)
 {
 talloc_free (threads);
-- 
1.6.5.4



[notmuch] [PATCH] Added regress option to threads iterator

2009-12-09 Thread Ruben Pollan
Added the functions notmuch_threads_regress and notmuch_threads_is_first to
notmuch library. With them is possible to iterate backwards on threads.

* notmuch_threads_regress do the opposite than notmuch_threads_advance,
  getting the threads iterator one position backwards.

* notmuch_threads_is_first return TRUE if the iterator is in the first
  thread.
---
 lib/notmuch.h |8 
 lib/query.cc  |   28 
 2 files changed, 36 insertions(+), 0 deletions(-)

diff --git a/lib/notmuch.h b/lib/notmuch.h
index 69bd98a..e28ce46 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -429,6 +429,10 @@ notmuch_query_destroy (notmuch_query_t *query);
 notmuch_bool_t
 notmuch_threads_has_more (notmuch_threads_t *threads);
 
+/* Is the given notmuch_threads_t object on the first threads */
+notmuch_bool_t
+notmuch_threads_is_first (notmuch_threads_t *threads);
+
 /* Get the current thread from 'threads' as a notmuch_thread_t.
  *
  * Note: The returned thread belongs to 'threads' and has a lifetime
@@ -451,6 +455,10 @@ notmuch_threads_get (notmuch_threads_t *threads);
 void
 notmuch_threads_advance (notmuch_threads_t *threads);
 
+/* Regress the 'threads' iterator to the previous result. */
+void
+notmuch_threads_regress (notmuch_threads_t *threads);
+
 /* Destroy a notmuch_threads_t object.
  *
  * It's not strictly necessary to call this function. All memory from
diff --git a/lib/query.cc b/lib/query.cc
index 94a6860..cade17b 100644
--- a/lib/query.cc
+++ b/lib/query.cc
@@ -310,6 +310,12 @@ notmuch_threads_has_more (notmuch_threads_t *threads)
 return FALSE;
 }
 
+notmuch_bool_t
+notmuch_threads_is_first (notmuch_threads_t *threads)
+{
+return (g_hash_table_size (threads-threads) = 1);
+}
+
 notmuch_thread_t *
 notmuch_threads_get (notmuch_threads_t *threads)
 {
@@ -329,6 +335,28 @@ notmuch_threads_advance (notmuch_threads_t *threads)
 }
 
 void
+notmuch_threads_regress (notmuch_threads_t *threads)
+{
+notmuch_message_t *message;
+   const char *thread_id;
+
+   thread_id = threads-thread_id;
+
+while (!notmuch_messages_is_first (threads-messages))
+{
+   notmuch_messages_regress (threads-messages);
+   message = notmuch_messages_get (threads-messages);
+   threads-thread_id = notmuch_message_get_thread_id (message);
+
+   if (strcmp (threads-thread_id, thread_id))
+   {
+   g_hash_table_remove (threads-threads, thread_id);
+   return;
+   }
+}
+}
+
+void
 notmuch_threads_destroy (notmuch_threads_t *threads)
 {
 talloc_free (threads);
-- 
1.6.5.4

___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch