Start using new sqlite helper functions

This commit is contained in:
Alexander Butenko 2010-07-01 15:21:01 -04:00 committed by Christian Dywan
parent bd97f069d4
commit 3c4896f94f
2 changed files with 20 additions and 82 deletions

View file

@ -2728,39 +2728,27 @@ _action_history_populate_popup (GtkAction* action,
GtkMenu* menu, GtkMenu* menu,
MidoriBrowser* browser) MidoriBrowser* browser)
{ {
#if HAVE_SQLITE
sqlite3* db; sqlite3* db;
sqlite3_stmt* statement;
gint result;
const gchar* sqlcmd; const gchar* sqlcmd;
KatzeArray* array;
KatzeItem* item;
gint i = 0;
db = g_object_get_data (G_OBJECT (browser->history), "db"); db = g_object_get_data (G_OBJECT (browser->history), "db");
sqlcmd = "SELECT uri, title, date FROM history " sqlcmd = "SELECT uri, title, date FROM history "
"GROUP BY uri ORDER BY date ASC LIMIT 10"; "GROUP BY uri ORDER BY date ASC LIMIT 10";
result = sqlite3_prepare_v2 (db, sqlcmd, -1, &statement, NULL); array = katze_array_from_sqlite (db, sqlcmd);
if (result != SQLITE_OK)
{
g_print (_("Failed to execute database statement: %s\n"),
sqlite3_errmsg (db));
return;
}
while ((result = sqlite3_step (statement)) == SQLITE_ROW) while ((item = katze_array_get_nth_item (KATZE_ARRAY (array), i)))
{ {
const unsigned char* uri;
const unsigned char* title;
KatzeItem* item;
GtkWidget* menuitem; GtkWidget* menuitem;
GdkPixbuf* icon; GdkPixbuf* icon;
GtkWidget* image; GtkWidget* image;
const gchar* uri;
const gchar* title;
uri = sqlite3_column_text (statement, 0); uri = katze_item_get_uri (item);
title = sqlite3_column_text (statement, 1); title = katze_item_get_name (item);
item = katze_item_new ();
katze_item_set_uri (item, (gchar*)uri);
katze_item_set_name (item, (gchar*)title);
menuitem = katze_image_menu_item_new_ellipsized ((gchar*)title); menuitem = katze_image_menu_item_new_ellipsized ((gchar*)title);
icon = katze_load_cached_icon ((gchar*)uri, GTK_WIDGET (browser)); icon = katze_load_cached_icon ((gchar*)uri, GTK_WIDGET (browser));
image = gtk_image_new_from_pixbuf (icon); image = gtk_image_new_from_pixbuf (icon);
@ -2783,9 +2771,8 @@ _action_history_populate_popup (GtkAction* action,
G_CALLBACK (midori_browser_menu_item_select_cb), browser); G_CALLBACK (midori_browser_menu_item_select_cb), browser);
g_signal_connect (menuitem, "deselect", g_signal_connect (menuitem, "deselect",
G_CALLBACK (midori_browser_menu_item_deselect_cb), browser); G_CALLBACK (midori_browser_menu_item_deselect_cb), browser);
i++;
} }
sqlite3_finalize (statement);
#endif
} }
static void static void
@ -6515,9 +6502,10 @@ midori_bookmarkbar_populate (MidoriBrowser* browser)
{ {
GtkWidget* homepage; GtkWidget* homepage;
sqlite3* db; sqlite3* db;
gint result;
const gchar* sqlcmd; const gchar* sqlcmd;
sqlite3_stmt* statement; KatzeArray* array;
KatzeItem* item;
gint i;
homepage = gtk_action_create_tool_item (_action_by_name (browser, "Homepage")); homepage = gtk_action_create_tool_item (_action_by_name (browser, "Homepage"));
gtk_tool_item_set_is_important (GTK_TOOL_ITEM (homepage), TRUE); gtk_tool_item_set_is_important (GTK_TOOL_ITEM (homepage), TRUE);
@ -6532,34 +6520,19 @@ midori_bookmarkbar_populate (MidoriBrowser* browser)
sqlcmd = "SELECT uri, title, app FROM bookmarks WHERE " sqlcmd = "SELECT uri, title, app FROM bookmarks WHERE "
" toolbar = 1 ORDER BY type ASC"; " toolbar = 1 ORDER BY type ASC";
result = sqlite3_prepare_v2 (db, sqlcmd, -1, &statement, NULL);
if (result != SQLITE_OK) array = katze_array_from_sqlite (db, sqlcmd);
if (!array)
{ {
_action_set_sensitive (browser, "BookmarkAdd", FALSE); _action_set_sensitive (browser, "BookmarkAdd", FALSE);
_action_set_sensitive (browser, "BookmarkFolderAdd", FALSE); _action_set_sensitive (browser, "BookmarkFolderAdd", FALSE);
return; return;
} }
while ((result = sqlite3_step (statement)) == SQLITE_ROW) while ((item = katze_array_get_nth_item (KATZE_ARRAY (array), i)))
{ {
gint app;
KatzeItem* item;
const unsigned char* uri;
const unsigned char* title;
uri = sqlite3_column_text (statement, 0);
title = sqlite3_column_text (statement, 1);
app = sqlite3_column_int64 (statement, 2);
item = katze_item_new ();
katze_item_set_name (item, (gchar*)title);
katze_item_set_meta_integer (item, "toolbar", 1);
if (uri)
{
katze_item_set_uri (item, (gchar*)uri);
katze_item_set_meta_integer (item, "app", app);
}
midori_bookmarkbar_insert_item (browser->bookmarkbar, item); midori_bookmarkbar_insert_item (browser->bookmarkbar, item);
i++;
} }
_action_set_sensitive (browser, "BookmarkAdd", TRUE); _action_set_sensitive (browser, "BookmarkAdd", TRUE);
_action_set_sensitive (browser, "BookmarkFolderAdd", TRUE); _action_set_sensitive (browser, "BookmarkFolderAdd", TRUE);

View file

@ -11,6 +11,7 @@
#include "midori-bookmarks.h" #include "midori-bookmarks.h"
#include "midori-array.h"
#include "midori-app.h" #include "midori-app.h"
#include "midori-browser.h" #include "midori-browser.h"
#include "midori-stock.h" #include "midori-stock.h"
@ -143,7 +144,6 @@ midori_bookmarks_read_from_db (MidoriBookmarks* bookmarks,
sqlite3_stmt* statement; sqlite3_stmt* statement;
gint result; gint result;
const gchar* sqlcmd; const gchar* sqlcmd;
KatzeArray* array;
db = g_object_get_data (G_OBJECT (bookmarks->array), "db"); db = g_object_get_data (G_OBJECT (bookmarks->array), "db");
@ -155,42 +155,7 @@ midori_bookmarks_read_from_db (MidoriBookmarks* bookmarks,
if (result != SQLITE_OK) if (result != SQLITE_OK)
return NULL; return NULL;
array = katze_array_new (KATZE_TYPE_ITEM); return katze_array_from_statement (statement);
while ((result = sqlite3_step (statement)) == SQLITE_ROW)
{
gint type;
gint app;
gint toolbar;
KatzeItem* item;
const unsigned char* uri;
const unsigned char* title;
uri = sqlite3_column_text (statement, 0);
title = sqlite3_column_text (statement, 1);
type = sqlite3_column_int64 (statement, 2);
app = sqlite3_column_int64 (statement, 3);
toolbar = sqlite3_column_int64 (statement, 4);
item = katze_item_new ();
katze_item_set_name (item, (gchar*)title);
katze_item_set_meta_integer (item, "toolbar", toolbar);
/* type 0 -- folder, 1 -- entry */
if (type == 1)
{
katze_item_set_uri (item, (gchar*)uri);
katze_item_set_meta_integer (item, "app", app);
}
katze_array_add_item (array, item);
}
if (result != SQLITE_DONE)
g_print (_("Failed to execute database statement: %s\n"),
sqlite3_errmsg (db));
sqlite3_finalize (statement);
return array;
} }
static void static void