[Xfce4-commits] <postler:master> Same search support for unread and getting messages

Christian Dywan noreply at xfce.org
Sun Jul 3 14:02:06 CEST 2011


Updating branch refs/heads/master
         to 6738287d513dcb83d75f0ba440c6feec984c1d61 (commit)
       from 866af036a722f4c8db9c1db4692e1a3ce75249f1 (commit)

commit 6738287d513dcb83d75f0ba440c6feec984c1d61
Author: Christian Dywan <christian at twotoasts.de>
Date:   Sun Jul 3 03:13:59 2011 +0200

    Same search support for unread and getting messages

 postler/postler-index.vala |   90 ++++++++++++++++++++++++-------------------
 1 files changed, 50 insertions(+), 40 deletions(-)

diff --git a/postler/postler-index.vala b/postler/postler-index.vala
index a8d4d30..f8c1f16 100644
--- a/postler/postler-index.vala
+++ b/postler/postler-index.vala
@@ -227,23 +227,58 @@ namespace Postler {
             statement_remove.reset ();
         }
 
+        string search_statement (string folder, string sql,
+            ref Sqlite.Statement statement) throws GLib.Error {
+
+            /* file:// or search:field/value */
+            if (folder.has_prefix ("search:")) {
+                string[] parts = folder.slice (7, -1).split ("/", 2);
+                if (parts[0] == null || parts[1] == null)
+                    throw new GLib.FileError.FAILED (_("Failed to list messages: %s"),
+                                                     "parts[0] == null || parts[1] == null");
+                string field = parts[0];
+                if (field == "from")
+                    field = "sender";
+                else if (field == "to")
+                    field = "recipients";
+                /* We can use the prepared statement for uri search */
+                if (field != "uri") {
+                    /* Filter out Trash and Junk */
+                    string real_sql = sql.replace ("$CASE",
+                        "AND uri NOT LIKE '%/Trash/%' AND uri NOT LIKE '%/Junk/%'");
+                    if (database.prepare_v2 (real_sql.replace ("$FIELD", field),
+                                             -1, out statement) != Sqlite.OK)
+                        throw new GLib.FileError.FAILED (_("Failed to list messages: %s"), database.errmsg ());
+                }
+                return "%%%s%%".printf (parts[1]);
+            }
+            return folder;
+        }
+
         public int64 unread_messages (string folder) throws GLib.Error {
+            unowned string sql = """
+                SELECT COUNT (id) FROM
+                (SELECT DISTINCT id FROM messages
+                WHERE unread != 0 AND $FIELD LIKE ?2 $CASE)
+                """;
+
             if (statement_unread == null) {
-                if (database.prepare_v2 ("""
-                    SELECT COUNT (id) FROM (SELECT DISTINCT id FROM messages WHERE uri LIKE ?1 AND unread != 0)
-                    """,
-                    -1, out statement_unread) != Sqlite.OK)
+                if (database.prepare_v2 (sql.replace ("$FIELD", "uri").replace ("$CASE", ""),
+                                         -1, out statement_unread) != Sqlite.OK)
                     throw new GLib.FileError.FAILED (_("Failed to count unread messages: %s"), database.errmsg ());
             }
-            bool success =
-                statement_unread.bind_text (1, folder, -1) == Sqlite.OK
-             && statement_unread.step () == Sqlite.ROW;
+            Sqlite.Statement? temporary_statement = null;
+            string search_value = search_statement (folder, sql, ref temporary_statement);
+            unowned Sqlite.Statement statement = temporary_statement != null
+                                               ? temporary_statement : statement_unread;
+            bool success = statement.bind_text (2, search_value, -1) == Sqlite.OK
+             && statement.step () == Sqlite.ROW;
             if (!success) {
-                statement_unread.reset ();
+                statement.reset ();
                 throw new GLib.FileError.FAILED (_("Failed to count unread messages: %s"), database.errmsg ());
             }
-            int64 count = statement_unread.column_int64 (0);
-            statement_unread.reset ();
+            int64 count = statement.column_int64 (0);
+            statement.reset ();
             return count;
         }
 
@@ -285,7 +320,7 @@ namespace Postler {
         public string[] get_thread (string thread) throws GLib.Error {
             if (statement_thread == null) {
                 if (database.prepare_v2 ("""
-                    SELECT id FROM messages WHERE threads = ?1 ORDER BY date ASC
+                    SELECT id FROM messages WHERE threads = ?1 GROUP BY id ORDER BY date ASC
                     """,
                     -1, out statement_thread) != Sqlite.OK)
                     throw new GLib.FileError.FAILED (_("Failed to thread messages: %s"), database.errmsg ());
@@ -321,35 +356,10 @@ namespace Postler {
                     throw new GLib.FileError.FAILED (_("Failed to list messages: %s"), database.errmsg ());
             }
 
-            /* file:// or search:field/value */
-            unowned Sqlite.Statement statement = statement_list;
-            Sqlite.Statement temporary_statement;
-            string search_value;
-            if (folder.has_prefix ("search:")) {
-                string[] parts = folder.slice (7, -1).split ("/", 2);
-                if (parts[0] == null || parts[1] == null)
-                    throw new GLib.FileError.FAILED (_("Failed to list messages: %s"),
-                                                     "parts[0] == null || parts[1] == null");
-                string field = parts[0];
-                if (field == "from")
-                    field = "sender";
-                else if (field == "to")
-                    field = "recipients";
-                /* We can use the prepared statement for uri search */
-                search_value = "%%%s%%".printf (parts[1]);
-                if (field != "uri") {
-                    /* Filter out Trash and Junk */
-                    string real_sql = sql.replace ("$CASE",
-                        "AND uri NOT LIKE '%/Trash/%' AND uri NOT LIKE '%/Junk/%'");
-                    if (database.prepare_v2 (real_sql.replace ("$FIELD", field),
-                                             -1, out temporary_statement) != Sqlite.OK)
-                        throw new GLib.FileError.FAILED (_("Failed to list messages: %s"), database.errmsg ());
-                    statement = temporary_statement;
-                }
-            }
-            else
-                search_value = folder;
-
+            Sqlite.Statement? temporary_statement = null;
+            string search_value = search_statement (folder, sql, ref temporary_statement);
+            unowned Sqlite.Statement statement = temporary_statement != null
+                                               ? temporary_statement : statement_list;
             bool success = statement.bind_text (2, search_value, -1) == Sqlite.OK;
             int result = Sqlite.ERROR;
             success = success && row_or_done (((result = statement.step ())));



More information about the Xfce4-commits mailing list