[epiphany] ephy-history-service: add API to query for hosts



commit 6ebe03244f6bcc983e4ad3d06ab0dd09634a5937
Author: Claudio Saavedra <csaavedra igalia com>
Date:   Thu Mar 8 00:16:23 2012 +0200

    ephy-history-service: add API to query for hosts
    
    By now, the public API only supports filtering by time range, but
    filtering by string matching is also possible.

 lib/history/ephy-history-service-hosts-table.c |  101 ++++++++++++++++++++++++
 lib/history/ephy-history-service-private.h     |    1 +
 lib/history/ephy-history-service.c             |   54 ++++++++++++-
 lib/history/ephy-history-service.h             |    2 +
 4 files changed, 156 insertions(+), 2 deletions(-)
---
diff --git a/lib/history/ephy-history-service-hosts-table.c b/lib/history/ephy-history-service-hosts-table.c
index aced16e..d323f6d 100644
--- a/lib/history/ephy-history-service-hosts-table.c
+++ b/lib/history/ephy-history-service-hosts-table.c
@@ -243,6 +243,107 @@ ephy_history_service_get_all_hosts (EphyHistoryService *self)
   return hosts;
 }
 
+GList*
+ephy_history_service_find_host_rows (EphyHistoryService *self, EphyHistoryQuery *query)
+{
+  EphyHistoryServicePrivate *priv = EPHY_HISTORY_SERVICE (self)->priv;
+  EphySQLiteStatement *statement = NULL;
+  GList *substring;
+  GString *statement_str;
+  GList *hosts = NULL;
+  GError *error = NULL;
+  const char *base_statement = ""
+    "SELECT "
+      "DISTINCT hosts.id, "
+      "hosts.url, "
+      "hosts.title, "
+      "hosts.visit_count, "
+      "hosts.zoom_level "
+    "FROM "
+      "hosts ";
+
+  int i = 0;
+
+  g_assert (priv->history_thread == g_thread_self ());
+  g_assert (priv->history_database != NULL);
+
+  statement_str = g_string_new (base_statement);
+
+  /* In either of these cases we need to at least join with the urls table. */
+  if (query->substring_list || query->from > 0 || query->to > 0)
+    statement_str = g_string_append (statement_str,  "JOIN urls on hosts.id = urls.host ");
+
+  /* In these cases, we additionally need to join with the visits table. */
+  if (query->from > 0 || query->to > 0) {
+    statement_str = g_string_append (statement_str, "JOIN visits on urls.id = visits.url WHERE ");
+    if (query->from > 0)
+      statement_str = g_string_append (statement_str, "visits.visit_time >= ? AND ");
+    if (query->to > 0)
+      statement_str = g_string_append (statement_str, "visits.visit_time <= ? AND ");
+  } else {
+    statement_str = g_string_append (statement_str, "WHERE ");
+  }
+
+  for (substring = query->substring_list; substring != NULL; substring = substring->next)
+    statement_str = g_string_append (statement_str, "(hosts.url LIKE ? OR hosts.title LIKE ? OR "
+                                     "urls.url LIKE ? OR urls.title LIKE ?) AND ");
+
+  statement_str = g_string_append (statement_str, "1 ");
+
+  statement = ephy_sqlite_connection_create_statement (priv->history_database,
+						       statement_str->str, &error);
+  g_string_free (statement_str, TRUE);
+
+  if (error) {
+    g_error ("Could not build hosts table query statement: %s", error->message);
+    g_error_free (error);
+    g_object_unref (statement);
+    return NULL;
+  }
+  if (query->from > 0) {
+    if (ephy_sqlite_statement_bind_int (statement, i++, (int)query->from, &error) == FALSE) {
+      g_error ("Could not build hosts table query statement: %s", error->message);
+      g_error_free (error);
+      g_object_unref (statement);
+      return NULL;
+    }
+  }
+  if (query->to > 0) {
+    if (ephy_sqlite_statement_bind_int (statement, i++, (int)query->to, &error) == FALSE) {
+      g_error ("Could not build hosts table query statement: %s", error->message);
+      g_error_free (error);
+      g_object_unref (statement);
+      return NULL;
+    }
+  }
+  for (substring = query->substring_list; substring != NULL; substring = substring->next) {
+    int j = 4;
+    char *string = g_strdup_printf ("%%%s%%", (char*)substring->data);
+    while (j--)
+      if (ephy_sqlite_statement_bind_string (statement, i++, string, &error) == FALSE) {
+        g_error ("Could not build hosts table query statement: %s", error->message);
+        g_error_free (error);
+        g_object_unref (statement);
+        g_free (string);
+        return NULL;
+      }
+    g_free (string);
+  }
+
+  while (ephy_sqlite_statement_step (statement, &error))
+    hosts = g_list_prepend (hosts, create_host_from_statement (statement));
+
+  hosts = g_list_reverse (hosts);
+
+  if (error) {
+    g_error ("Could not execute hosts table query statement: %s", error->message);
+    g_error_free (error);
+  }
+  g_object_unref (statement);
+
+  return hosts;
+}
+
 /* Inspired from ephy-history.c */
 static GList *
 get_hostname_and_locations (const gchar *url, gchar **hostname)
diff --git a/lib/history/ephy-history-service-private.h b/lib/history/ephy-history-service-private.h
index 0894770..c616b34 100644
--- a/lib/history/ephy-history-service-private.h
+++ b/lib/history/ephy-history-service-private.h
@@ -50,6 +50,7 @@ void                     ephy_history_service_add_host_row            (EphyHisto
 void                     ephy_history_service_update_host_row         (EphyHistoryService *self, EphyHistoryHost *host);
 EphyHistoryHost *        ephy_history_service_get_host_row            (EphyHistoryService *self, const gchar *url_string, EphyHistoryHost *host);
 GList *                  ephy_history_service_get_all_hosts           (EphyHistoryService *self);
+GList*                   ephy_history_service_find_host_rows          (EphyHistoryService *self, EphyHistoryQuery *query);
 EphyHistoryHost *        ephy_history_service_get_host_row_from_url   (EphyHistoryService *self, const gchar *url);
 void                     ephy_history_service_delete_host_row         (EphyHistoryService *self, EphyHistoryHost *host);
 void                     ephy_history_service_delete_orphan_hosts     (EphyHistoryService *self);
diff --git a/lib/history/ephy-history-service.c b/lib/history/ephy-history-service.c
index ca656c7..436aa1c 100644
--- a/lib/history/ephy-history-service.c
+++ b/lib/history/ephy-history-service.c
@@ -41,7 +41,8 @@ typedef enum {
   GET_HOST_FOR_URL,
   QUERY_URLS,
   QUERY_VISITS,
-  GET_HOSTS
+  GET_HOSTS,
+  QUERY_HOSTS
 } EphyHistoryServiceMessageType;
 
 enum {
@@ -532,6 +533,18 @@ ephy_history_service_execute_get_hosts (EphyHistoryService *self,
   return TRUE;
 }
 
+static gboolean
+ephy_history_service_execute_query_hosts (EphyHistoryService *self,
+                                          EphyHistoryQuery *query, gpointer *results)
+{
+  GList *hosts;
+
+  hosts = ephy_history_service_find_host_rows (self, query);
+  *results = hosts;
+
+  return TRUE;
+}
+
 void
 ephy_history_service_add_visit (EphyHistoryService *self, EphyHistoryPageVisit *visit, EphyHistoryJobCallback callback, gpointer user_data)
 {
@@ -628,6 +641,23 @@ ephy_history_service_get_hosts (EphyHistoryService *self,
   ephy_history_service_send_message (self, message);
 }
 
+void
+ephy_history_service_query_hosts (EphyHistoryService *self,
+                                  EphyHistoryQuery *query,
+                                  EphyHistoryJobCallback callback,
+                                  gpointer user_data)
+{
+  EphyHistoryServiceMessage *message;
+
+  g_return_if_fail (EPHY_IS_HISTORY_SERVICE (self));
+
+  message = ephy_history_service_message_new (self, QUERY_HOSTS,
+                                              ephy_history_query_copy (query),
+                                              (GDestroyNotify) ephy_history_query_free,
+                                              callback, user_data);
+  ephy_history_service_send_message (self, message);
+}
+
 static gboolean
 ephy_history_service_execute_set_url_title (EphyHistoryService *self,
                                             EphyHistoryURL *url,
@@ -888,7 +918,8 @@ static EphyHistoryServiceMethod methods[] = {
   (EphyHistoryServiceMethod)ephy_history_service_execute_get_host_for_url,
   (EphyHistoryServiceMethod)ephy_history_service_execute_query_urls,
   (EphyHistoryServiceMethod)ephy_history_service_execute_find_visits,
-  (EphyHistoryServiceMethod)ephy_history_service_execute_get_hosts
+  (EphyHistoryServiceMethod)ephy_history_service_execute_get_hosts,
+  (EphyHistoryServiceMethod)ephy_history_service_execute_query_hosts
 };
 
 static void
@@ -950,3 +981,22 @@ ephy_history_service_visit_url (EphyHistoryService *self,
 
   g_signal_emit (self, signals[VISIT_URL], 0, url, &result);
 }
+
+void
+ephy_history_service_find_hosts (EphyHistoryService *self,
+                                 gint64 from, gint64 to,
+                                 EphyHistoryJobCallback callback,
+                                 gpointer user_data)
+{
+  EphyHistoryQuery *query;
+
+  g_return_if_fail (EPHY_IS_HISTORY_SERVICE (self));
+
+  query = ephy_history_query_new ();
+
+  query->from = from;
+  query->to = to;
+
+  ephy_history_service_query_hosts (self,
+                                    query, callback, user_data);
+}
diff --git a/lib/history/ephy-history-service.h b/lib/history/ephy-history-service.h
index e6c522d..8e36d63 100644
--- a/lib/history/ephy-history-service.h
+++ b/lib/history/ephy-history-service.h
@@ -67,12 +67,14 @@ void                     ephy_history_service_set_url_title           (EphyHisto
 void                     ephy_history_service_set_url_zoom_level      (EphyHistoryService *self, const char *url, const double zoom_level, EphyHistoryJobCallback callback, gpointer user_data);
 void                     ephy_history_service_get_host_for_url        (EphyHistoryService *self, const char *url, EphyHistoryJobCallback callback, gpointer user_data);
 void                     ephy_history_service_get_hosts               (EphyHistoryService *self, EphyHistoryJobCallback callback, gpointer user_data);
+void                     ephy_history_service_query_hosts             (EphyHistoryService *self, EphyHistoryQuery *query, EphyHistoryJobCallback callback, gpointer user_data);
 void                     ephy_history_service_delete_host             (EphyHistoryService *self, EphyHistoryHost *host, EphyHistoryJobCallback callback, gpointer user_data);
 void                     ephy_history_service_get_url                 (EphyHistoryService *self, const char *url, EphyHistoryJobCallback callback, gpointer user_data);
 void                     ephy_history_service_delete_urls             (EphyHistoryService *self, GList *urls, EphyHistoryJobCallback callback, gpointer user_data);
 void                     ephy_history_service_find_urls               (EphyHistoryService *self, gint64 from, gint64 to, guint limit, gint host, GList *substring_list, EphyHistoryJobCallback callback, gpointer user_data);
 void                     ephy_history_service_visit_url               (EphyHistoryService *self, const char *orig_url);
 void                     ephy_history_service_clear                   (EphyHistoryService *self, EphyHistoryJobCallback callback, gpointer user_data);
+void                     ephy_history_service_find_hosts              (EphyHistoryService *self, gint64 from, gint64 to, EphyHistoryJobCallback callback, gpointer user_data);
 
 G_END_DECLS
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]