[Xfce4-commits] <midori:master> Move History out of main.c into its own file

Christian Dywan noreply at xfce.org
Sun Nov 25 13:18:01 CET 2012


Updating branch refs/heads/master
         to 88cb2de37192c6c65e289f3966e04e6c4b5d408a (commit)
       from d9112ae446bebeed8564922254be91d2af437474 (commit)

commit 88cb2de37192c6c65e289f3966e04e6c4b5d408a
Author: Christian Dywan <christian at twotoasts.de>
Date:   Sun Nov 25 12:26:03 2012 +0100

    Move History out of main.c into its own file
    
    Align API calls with Bookmarks.

 midori/main.c             |  125 ++-----------------------------------------
 midori/midori-bookmarks.c |   15 +++---
 midori/midori-bookmarks.h |   11 +++-
 midori/midori-history.c   |  129 +++++++++++++++++++++++++++++++++++++++++++++
 midori/midori-history.h   |   27 +++++++++
 panels/midori-bookmarks.h |    9 +--
 panels/midori-history.h   |    9 +--
 tests/bookmarks.c         |    9 +---
 8 files changed, 185 insertions(+), 149 deletions(-)

diff --git a/midori/main.c b/midori/main.c
index e2f24f9..70917ed 100644
--- a/midori/main.c
+++ b/midori/main.c
@@ -16,6 +16,7 @@
 #include "panels/midori-bookmarks.h"
 #include "midori-extension.h"
 #include "midori-history.h"
+#include "panels/midori-history.h"
 #include "midori-transfers.h"
 #include "midori-panel.h"
 #include "midori-platform.h"
@@ -47,115 +48,6 @@
 #endif
 
 static void
-midori_history_clear_cb (KatzeArray* array,
-                         sqlite3*    db)
-{
-    char* errmsg = NULL;
-    if (sqlite3_exec (db, "DELETE FROM history; DELETE FROM search",
-                      NULL, NULL, &errmsg) != SQLITE_OK)
-    {
-        g_printerr (_("Failed to clear history: %s\n"), errmsg);
-        sqlite3_free (errmsg);
-    }
-}
-
-static gboolean
-midori_history_initialize (KatzeArray*  array,
-                           const gchar* filename,
-                           char**       errmsg)
-{
-    sqlite3* db;
-    gboolean has_day = FALSE;
-    sqlite3_stmt* stmt;
-    gint result;
-    gchar* sql;
-    gchar* bookmarks_filename;
-
-    g_return_val_if_fail (errmsg != NULL, FALSE);
-
-    if (sqlite3_open (filename, &db) != SQLITE_OK)
-    {
-        if (errmsg)
-            *errmsg = g_strdup_printf (_("Failed to open database: %s\n"),
-                                       sqlite3_errmsg (db));
-        sqlite3_close (db);
-        return FALSE;
-    }
-
-    if (sqlite3_exec (db,
-        "PRAGMA journal_mode = WAL; PRAGMA cache_size = 32100;",
-        NULL, NULL, errmsg) != SQLITE_OK)
-        sqlite3_exec (db, "PRAGMA journal_mode = TRUNCATE;", NULL, NULL, errmsg);
-    sqlite3_exec (db,
-        "PRAGMA synchronous = NORMAL; PRAGMA temp_store = MEMORY;",
-        NULL, NULL, errmsg);
-    if (*errmsg)
-    {
-        g_warning ("Failed to set journal mode: %s", *errmsg);
-        sqlite3_free (*errmsg);
-    }
-    if (sqlite3_exec (db,
-                      "CREATE TABLE IF NOT EXISTS "
-                      "history (uri text, title text, date integer, day integer);"
-                      "CREATE TABLE IF NOT EXISTS "
-                      "search (keywords text, uri text, day integer);",
-                      NULL, NULL, errmsg) != SQLITE_OK)
-        return FALSE;
-
-    sqlite3_prepare_v2 (db, "SELECT day FROM history LIMIT 1", -1, &stmt, NULL);
-    result = sqlite3_step (stmt);
-    if (result == SQLITE_ROW)
-        has_day = TRUE;
-    sqlite3_finalize (stmt);
-
-    if (!has_day)
-        sqlite3_exec (db,
-                      "BEGIN TRANSACTION;"
-                      "CREATE TEMPORARY TABLE backup (uri text, title text, date integer);"
-                      "INSERT INTO backup SELECT uri,title,date FROM history;"
-                      "DROP TABLE history;"
-                      "CREATE TABLE history (uri text, title text, date integer, day integer);"
-                      "INSERT INTO history SELECT uri,title,date,"
-                      "julianday(date(date,'unixepoch','start of day','+1 day'))"
-                      " - julianday('0001-01-01','start of day')"
-                      "FROM backup;"
-                      "DROP TABLE backup;"
-                      "COMMIT;",
-                      NULL, NULL, errmsg);
-
-    bookmarks_filename = midori_paths_get_config_filename_for_writing ("bookmarks_v2.db");
-    sql = g_strdup_printf ("ATTACH DATABASE '%s' AS bookmarks", bookmarks_filename);
-    g_free (bookmarks_filename);
-    sqlite3_exec (db, sql, NULL, NULL, errmsg);
-    g_free (sql);
-    g_object_set_data (G_OBJECT (array), "db", db);
-    g_signal_connect (array, "clear",
-                      G_CALLBACK (midori_history_clear_cb), db);
-
-    return TRUE;
-}
-
-static void
-midori_history_terminate (KatzeArray* array,
-                          gint        max_history_age)
-{
-    sqlite3* db = g_object_get_data (G_OBJECT (array), "db");
-    char* errmsg = NULL;
-    gchar* sqlcmd = g_strdup_printf (
-        "DELETE FROM history WHERE "
-        "(julianday(date('now')) - julianday(date(date,'unixepoch')))"
-        " >= %d", max_history_age);
-    if (sqlite3_exec (db, sqlcmd, NULL, NULL, &errmsg) != SQLITE_OK)
-    {
-        /* i18n: Couldn't remove items that are older than n days */
-        g_printerr (_("Failed to remove old history items: %s\n"), errmsg);
-        sqlite3_free (errmsg);
-    }
-    g_free (sqlcmd);
-    sqlite3_close (db);
-}
-
-static void
 settings_notify_cb (MidoriWebSettings* settings,
                     GParamSpec*        pspec,
                     MidoriApp*         app)
@@ -1019,8 +911,6 @@ main (int    argc,
     gchar* uri_ready;
     gchar* errmsg;
     sqlite3* db;
-    gint max_history_age;
-    gint clear_prefs = MIDORI_CLEAR_NONE;
     #ifdef G_ENABLE_DEBUG
         gboolean startup_timer = midori_debug ("startup");
         #define midori_startup_timer(tmrmsg) if (startup_timer) \
@@ -1430,15 +1320,13 @@ main (int    argc,
     g_free (uri);
     midori_startup_timer ("Search read: \t%f");
 
-    bookmarks = katze_array_new (KATZE_TYPE_ARRAY);
     errmsg = NULL;
-    if ((db = midori_bookmarks_initialize (bookmarks, &errmsg)) == NULL)
+    if (!(bookmarks = midori_bookmarks_new (&errmsg)))
     {
         g_string_append_printf (error_messages,
             _("Bookmarks couldn't be loaded: %s\n"), errmsg);
         errmsg = NULL;
     }
-    g_object_set_data (G_OBJECT (bookmarks), "db", db);
     midori_startup_timer ("Bookmarks read: \t%f");
 
     config_file = NULL;
@@ -1474,15 +1362,13 @@ main (int    argc,
     #endif
 
     midori_startup_timer ("Trash read: \t%f");
-    history = katze_array_new (KATZE_TYPE_ARRAY);
-    katze_assign (config_file, g_build_filename (config, "history.db", NULL));
 
     errmsg = NULL;
-    if (!midori_history_initialize (history, config_file, &errmsg))
+    if (!(history = midori_history_new (&errmsg)))
     {
         g_string_append_printf (error_messages,
             _("The history couldn't be loaded: %s\n"), errmsg);
-        errmsg = NULL;
+        katze_assign (errmsg, NULL);
     }
     midori_startup_timer ("History read: \t%f");
 
@@ -1590,8 +1476,7 @@ main (int    argc,
     gtk_main ();
 
     settings_notify_cb (settings, NULL, app);
-    g_object_get (settings, "maximum-history-age", &max_history_age, NULL);
-    midori_history_terminate (history, max_history_age);
+    midori_history_on_quit (history, settings);
     midori_private_data_on_quit (settings);
     /* Removing KatzeHttpCookies makes it save outstanding changes */
     soup_session_remove_feature_by_type (webkit_get_default_session (),
diff --git a/midori/midori-bookmarks.c b/midori/midori-bookmarks.c
index 5e6d4f3..69251ad 100644
--- a/midori/midori-bookmarks.c
+++ b/midori/midori-bookmarks.c
@@ -130,9 +130,8 @@ midori_bookmarks_import_from_old_db (sqlite3*     db,
 }
 #undef _APPEND_TO_SQL_ERRORMSG
 
-sqlite3*
-midori_bookmarks_initialize (KatzeArray*  array,
-                             char**       errmsg)
+KatzeArray*
+midori_bookmarks_new (char** errmsg)
 {
     sqlite3* db;
     gchar* oldfile;
@@ -141,12 +140,13 @@ midori_bookmarks_initialize (KatzeArray*  array,
     const gchar* create_stmt;
     gchar* sql_errmsg = NULL;
     gchar* import_errmsg = NULL;
+    KatzeArray* array;
 
     g_return_val_if_fail (errmsg != NULL, NULL);
 
-    oldfile = g_build_filename (midori_paths_get_config_dir_for_writing (), "bookmarks.db", NULL);
+    oldfile = midori_paths_get_config_filename_for_writing ("bookmarks.db");
     oldfile_exists = g_access (oldfile, F_OK) == 0;
-    newfile = g_build_filename (midori_paths_get_config_dir_for_writing (), "bookmarks_v2.db", NULL);
+    newfile = midori_paths_get_config_filename_for_writing ("bookmarks_v2.db");
     newfile_did_exist = g_access (newfile, F_OK) == 0;
 
     /* sqlite3_open will create the file if it did not exists already */
@@ -298,12 +298,13 @@ midori_bookmarks_initialize (KatzeArray*  array,
     init_success:
         g_free (newfile);
         g_free (oldfile);
+        array = katze_array_new (KATZE_TYPE_ARRAY);
         g_signal_connect (array, "add-item",
                           G_CALLBACK (midori_bookmarks_add_item_cb), db);
         g_signal_connect (array, "remove-item",
                           G_CALLBACK (midori_bookmarks_remove_item_cb), db);
-
-        return db;
+        g_object_set_data (G_OBJECT (array), "db", db);
+        return array;
 
     init_failed:
         g_free (newfile);
diff --git a/midori/midori-bookmarks.h b/midori/midori-bookmarks.h
index e4a3f8d..bb1e416 100644
--- a/midori/midori-bookmarks.h
+++ b/midori/midori-bookmarks.h
@@ -10,6 +10,9 @@
  See the file COPYING for the full license text.
 */
 
+#ifndef __MIDORI_BOOKMARKS_H__
+#define __MIDORI_BOOKMARKS_H__ 1
+
 #include <sqlite3.h>
 #include <katze/katze.h>
 
@@ -23,10 +26,12 @@ midori_bookmarks_remove_item_cb (KatzeArray* array,
                                  KatzeItem*  item,
                                  sqlite3*    db);
 
-sqlite3*
-midori_bookmarks_initialize (KatzeArray*  array,
-                             char**       errmsg);
+KatzeArray*
+midori_bookmarks_new (char** errmsg);
 
 void
 midori_bookmarks_import (const gchar* filename,
                          sqlite3*     db);
+
+#endif /* !__MIDORI_BOOKMARKS_H__ */
+
diff --git a/midori/midori-history.c b/midori/midori-history.c
new file mode 100644
index 0000000..8c283f2
--- /dev/null
+++ b/midori/midori-history.c
@@ -0,0 +1,129 @@
+/*
+ Copyright (C) 2010-2012 Christian Dywan <christian at twotoasts.de>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ See the file COPYING for the full license text.
+*/
+
+#include "midori/midori-history.h"
+
+#include <glib/gi18n-lib.h>
+#include <midori/midori-core.h>
+
+static void
+midori_history_clear_cb (KatzeArray* array,
+                         sqlite3*    db)
+{
+    char* errmsg = NULL;
+    if (sqlite3_exec (db, "DELETE FROM history; DELETE FROM search",
+                      NULL, NULL, &errmsg) != SQLITE_OK)
+    {
+        g_printerr (_("Failed to clear history: %s\n"), errmsg);
+        sqlite3_free (errmsg);
+    }
+}
+
+KatzeArray*
+midori_history_new (char** errmsg)
+{
+    gchar* filename;
+    sqlite3* db;
+    gboolean has_day = FALSE;
+    sqlite3_stmt* stmt;
+    gint result;
+    gchar* sql;
+    gchar* bookmarks_filename;
+    KatzeArray* array;
+
+    g_return_val_if_fail (errmsg != NULL, NULL);
+
+    filename = midori_paths_get_config_filename_for_writing ("history.db");
+    if (sqlite3_open (filename, &db) != SQLITE_OK)
+    {
+        if (errmsg)
+            *errmsg = g_strdup_printf (_("Failed to open database: %s\n"),
+                                       sqlite3_errmsg (db));
+        g_free (filename);
+        sqlite3_close (db);
+        return FALSE;
+    }
+    g_free (filename);
+
+    if (sqlite3_exec (db,
+        "PRAGMA journal_mode = WAL; PRAGMA cache_size = 32100;",
+        NULL, NULL, errmsg) != SQLITE_OK)
+        sqlite3_exec (db, "PRAGMA journal_mode = TRUNCATE;", NULL, NULL, errmsg);
+    sqlite3_exec (db,
+        "PRAGMA synchronous = NORMAL; PRAGMA temp_store = MEMORY;",
+        NULL, NULL, errmsg);
+    if (*errmsg)
+    {
+        g_warning ("Failed to set journal mode: %s", *errmsg);
+        sqlite3_free (*errmsg);
+    }
+    if (sqlite3_exec (db,
+                      "CREATE TABLE IF NOT EXISTS "
+                      "history (uri text, title text, date integer, day integer);"
+                      "CREATE TABLE IF NOT EXISTS "
+                      "search (keywords text, uri text, day integer);",
+                      NULL, NULL, errmsg) != SQLITE_OK)
+        return NULL;
+
+    sqlite3_prepare_v2 (db, "SELECT day FROM history LIMIT 1", -1, &stmt, NULL);
+    result = sqlite3_step (stmt);
+    if (result == SQLITE_ROW)
+        has_day = TRUE;
+    sqlite3_finalize (stmt);
+
+    if (!has_day)
+        sqlite3_exec (db,
+                      "BEGIN TRANSACTION;"
+                      "CREATE TEMPORARY TABLE backup (uri text, title text, date integer);"
+                      "INSERT INTO backup SELECT uri,title,date FROM history;"
+                      "DROP TABLE history;"
+                      "CREATE TABLE history (uri text, title text, date integer, day integer);"
+                      "INSERT INTO history SELECT uri,title,date,"
+                      "julianday(date(date,'unixepoch','start of day','+1 day'))"
+                      " - julianday('0001-01-01','start of day')"
+                      "FROM backup;"
+                      "DROP TABLE backup;"
+                      "COMMIT;",
+                      NULL, NULL, errmsg);
+
+    bookmarks_filename = midori_paths_get_config_filename_for_writing ("bookmarks_v2.db");
+    sql = g_strdup_printf ("ATTACH DATABASE '%s' AS bookmarks", bookmarks_filename);
+    g_free (bookmarks_filename);
+    sqlite3_exec (db, sql, NULL, NULL, errmsg);
+    g_free (sql);
+    array = katze_array_new (KATZE_TYPE_ARRAY);
+    g_object_set_data (G_OBJECT (array), "db", db);
+    g_signal_connect (array, "clear",
+                      G_CALLBACK (midori_history_clear_cb), db);
+    return array;
+}
+
+void
+midori_history_on_quit (KatzeArray*        array,
+                        MidoriWebSettings* settings)
+{
+    gint max_history_age = katze_object_get_int (settings, "maximum-history-age");
+    sqlite3* db = g_object_get_data (G_OBJECT (array), "db");
+    char* errmsg = NULL;
+    gchar* sqlcmd = g_strdup_printf (
+        "DELETE FROM history WHERE "
+        "(julianday(date('now')) - julianday(date(date,'unixepoch')))"
+        " >= %d", max_history_age);
+    if (sqlite3_exec (db, sqlcmd, NULL, NULL, &errmsg) != SQLITE_OK)
+    {
+        /* i18n: Couldn't remove items that are older than n days */
+        g_printerr (_("Failed to remove old history items: %s\n"), errmsg);
+        sqlite3_free (errmsg);
+    }
+    g_free (sqlcmd);
+    sqlite3_close (db);
+}
+
diff --git a/midori/midori-history.h b/midori/midori-history.h
new file mode 100644
index 0000000..9098acc
--- /dev/null
+++ b/midori/midori-history.h
@@ -0,0 +1,27 @@
+/*
+ Copyright (C) 2010-2012 Christian Dywan <christian at twotoasts.de>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ See the file COPYING for the full license text.
+*/
+
+#ifndef __MIDORI_HISTORY_H__
+#define __MIDORI_HISTORY_H__ 1
+
+#include <sqlite3.h>
+#include <katze/katze.h>
+#include "midori/midori-websettings.h"
+
+KatzeArray*
+midori_history_new (char** errmsg);
+
+void
+midori_history_on_quit (KatzeArray*        array,
+                        MidoriWebSettings* settings);
+
+#endif /* !__MIDORI_HISTORY_H__ */
+
diff --git a/panels/midori-bookmarks.h b/panels/midori-bookmarks.h
index cf56dea..d6f1d90 100644
--- a/panels/midori-bookmarks.h
+++ b/panels/midori-bookmarks.h
@@ -9,8 +9,8 @@
  See the file COPYING for the full license text.
 */
 
-#ifndef __MIDORI_BOOKMARKS_H__
-#define __MIDORI_BOOKMARKS_H__
+#ifndef __MIDORI_BOOKMARKS_PANEL_H__
+#define __MIDORI_BOOKMARKS_PANEL_H__
 
 #include <sqlite3.h>
 #include <gtk/gtk.h>
@@ -37,9 +37,6 @@ typedef struct _MidoriBookmarksClass           MidoriBookmarksClass;
 GType
 midori_bookmarks_get_type               (void);
 
-GtkWidget*
-midori_bookmarks_new                    (void);
-
 gint64
 midori_bookmarks_insert_item_db (sqlite3*   db,
                                  KatzeItem* item,
@@ -56,4 +53,4 @@ midori_bookmarks_update_item_db (sqlite3*   db,
 
 G_END_DECLS
 
-#endif /* __MIDORI_BOOKMARKS_H__ */
+#endif /* __MIDORI_BOOKMARKS_PANEL_H__ */
diff --git a/panels/midori-history.h b/panels/midori-history.h
index fc496ee..a1c212c 100644
--- a/panels/midori-history.h
+++ b/panels/midori-history.h
@@ -9,8 +9,8 @@
  See the file COPYING for the full license text.
 */
 
-#ifndef __MIDORI_HISTORY_H__
-#define __MIDORI_HISTORY_H__
+#ifndef __MIDORI_HISTORY_PANEL_H__
+#define __MIDORI_HISTORY_PANEL_H__
 
 #include <gtk/gtk.h>
 
@@ -35,9 +35,6 @@ typedef struct _MidoriHistoryClass           MidoriHistoryClass;
 GType
 midori_history_get_type               (void);
 
-GtkWidget*
-midori_history_new                    (void);
-
 G_END_DECLS
 
-#endif /* __MIDORI_HISTORY_H__ */
+#endif /* __MIDORI_HISTORY_PANEL_H__ */
diff --git a/tests/bookmarks.c b/tests/bookmarks.c
index 7b751f0..0b3564c 100644
--- a/tests/bookmarks.c
+++ b/tests/bookmarks.c
@@ -35,15 +35,12 @@ fixture_setup (BookmarksFixture* fixture,
 {
     KatzeItem* item;
     KatzeArray* folder;
-    sqlite3* db;
     gchar *errmsg = NULL;
 
-    fixture->db_bookmarks = katze_array_new (KATZE_TYPE_ARRAY);
-    if ((db = midori_bookmarks_initialize (fixture->db_bookmarks, &errmsg)) == NULL)
+    if (!(fixture->db_bookmarks = midori_bookmarks_new (&errmsg)))
         g_error ("Bookmarks couldn't be loaded: %s\n", errmsg);
     g_assert (errmsg == NULL);
-    g_object_set_data ( G_OBJECT (fixture->db_bookmarks), "db", db);
-
+    g_assert (g_object_get_data (G_OBJECT (fixture->db_bookmarks), "db"));
 
     fixture->test_bookmarks = katze_array_new (KATZE_TYPE_ARRAY);
 
@@ -87,8 +84,6 @@ static void
 fixture_teardown (BookmarksFixture* fixture,
                   const TestParameters *params)
 {
-    sqlite3* db = g_object_get_data (G_OBJECT (fixture->db_bookmarks), "db");
-    sqlite3_close (db);
     g_object_unref (fixture->db_bookmarks);
     g_object_unref (fixture->test_bookmarks);
 }


More information about the Xfce4-commits mailing list