Merge lp:~aauzi/midori/fix-1179200-5 into lp:midori

Proposed by André Auzi
Status: Superseded
Proposed branch: lp:~aauzi/midori/fix-1179200-5
Merge into: lp:midori
Diff against target: 2702 lines (+1306/-774)
13 files modified
katze/katze-array.c (+0/-30)
katze/katze-array.h (+30/-0)
katze/katze-item.c (+6/-0)
midori/midori-array.c (+28/-249)
midori/midori-array.h (+5/-20)
midori/midori-bookmarks-db.c (+1031/-178)
midori/midori-bookmarks-db.h (+56/-21)
midori/midori-browser.c (+83/-59)
midori/midori-frontend.c (+5/-5)
midori/midori.h (+1/-1)
panels/midori-bookmarks.c (+33/-183)
panels/midori-bookmarks.h (+0/-5)
tests/bookmarks.c (+28/-23)
To merge this branch: bzr merge lp:~aauzi/midori/fix-1179200-5
Reviewer Review Type Date Requested Status
Midori Devs Pending
Review via email: mp+178627@code.launchpad.net

Description of the change

Fifth step for merge of fix-1179200

Here the midori_bookmarks_db class is derived from KatzeArray.

It provides the API for bookmarks db operations.

It introduces the update-item signal, which is used by the bookmark bar and the bookmarks panel to handle bookmarks metadata changes.

So far, the bookmark item may still be duplicated (created by the HMI or retrieved from the database)

To post a comment you must log in.
lp:~aauzi/midori/fix-1179200-5 updated
6325. By André Auzi

fix indentation errors

6326. By André Auzi

make KatzeArray struct content private

6327. By André Auzi

merge lp:midori to fix conficts

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'katze/katze-array.c'
2--- katze/katze-array.c 2012-08-08 23:02:56 +0000
3+++ katze/katze-array.c 2013-08-05 20:10:53 +0000
4@@ -25,36 +25,6 @@
5 * #KatzeArray is a type aware container for items.
6 */
7
8-struct _KatzeArray
9-{
10- KatzeItem parent_instance;
11-
12- GType type;
13- GList* items;
14-};
15-
16-struct _KatzeArrayClass
17-{
18- KatzeItemClass parent_class;
19-
20- /* Signals */
21- void
22- (*add_item) (KatzeArray* array,
23- gpointer item);
24- void
25- (*remove_item) (KatzeArray* array,
26- gpointer item);
27- void
28- (*move_item) (KatzeArray* array,
29- gpointer item,
30- gint index);
31- void
32- (*clear) (KatzeArray* array);
33-
34- void
35- (*update) (KatzeArray* array);
36-};
37-
38 G_DEFINE_TYPE (KatzeArray, katze_array, KATZE_TYPE_ITEM);
39
40 enum {
41
42=== modified file 'katze/katze-array.h'
43--- katze/katze-array.h 2011-01-19 20:58:26 +0000
44+++ katze/katze-array.h 2013-08-05 20:10:53 +0000
45@@ -32,6 +32,36 @@
46 typedef struct _KatzeArray KatzeArray;
47 typedef struct _KatzeArrayClass KatzeArrayClass;
48
49+struct _KatzeArray
50+{
51+ KatzeItem parent_instance;
52+
53+ GType type;
54+ GList* items;
55+};
56+
57+struct _KatzeArrayClass
58+{
59+ KatzeItemClass parent_class;
60+
61+ /* Signals */
62+ void
63+ (*add_item) (KatzeArray* array,
64+ gpointer item);
65+ void
66+ (*remove_item) (KatzeArray* array,
67+ gpointer item);
68+ void
69+ (*move_item) (KatzeArray* array,
70+ gpointer item,
71+ gint index);
72+ void
73+ (*clear) (KatzeArray* array);
74+
75+ void
76+ (*update) (KatzeArray* array);
77+};
78+
79 GType
80 katze_array_get_type (void) G_GNUC_CONST;
81
82
83=== modified file 'katze/katze-item.c'
84--- katze/katze-item.c 2013-07-27 12:35:55 +0000
85+++ katze/katze-item.c 2013-08-05 20:10:53 +0000
86@@ -314,6 +314,9 @@
87 {
88 g_return_if_fail (KATZE_IS_ITEM (item));
89
90+ if (!g_strcmp0 (item->name, name))
91+ return;
92+
93 katze_assign (item->name, g_strdup (name));
94 if (item->parent)
95 katze_array_update ((KatzeArray*)item->parent);
96@@ -418,6 +421,9 @@
97 {
98 g_return_if_fail (KATZE_IS_ITEM (item));
99
100+ if (!g_strcmp0 (katze_item_get_meta_string (item, "icon"), icon))
101+ return;
102+
103 katze_item_set_meta_string (item, "icon", icon);
104 if (item->parent)
105 katze_array_update ((KatzeArray*)item->parent);
106
107=== modified file 'midori/midori-array.c'
108--- midori/midori-array.c 2013-05-30 21:57:04 +0000
109+++ midori/midori-array.c 2013-08-05 20:10:53 +0000
110@@ -97,15 +97,15 @@
111 if (katze_str_equal ((gchar*)cur->name, "title"))
112 {
113 gchar* value = g_strstrip ((gchar*)xmlNodeGetContent (cur));
114+ katze_item_set_name (KATZE_ITEM (array), value);
115+ xmlFree (value);
116+ }
117+ else if (katze_str_equal ((gchar*)cur->name, "desc"))
118+ {
119+ gchar* value = g_strstrip ((gchar*)xmlNodeGetContent (cur));
120 katze_item_set_text (KATZE_ITEM (array), value);
121 xmlFree (value);
122 }
123- else if (katze_str_equal ((gchar*)cur->name, "desc"))
124- {
125- gchar* value = g_strstrip ((gchar*)xmlNodeGetContent (cur));
126- katze_item_set_name (KATZE_ITEM (array), value);
127- xmlFree (value);
128- }
129 else if (katze_str_equal ((gchar*)cur->name, "info"))
130 katze_xbel_parse_info ((KatzeItem*)array, cur);
131 else if (katze_str_equal ((gchar*)cur->name, "folder"))
132@@ -986,7 +986,17 @@
133 return FALSE;
134 }
135
136-static void
137+/**
138+ * katze_item_set_value_from_columne:
139+ * @stmt: prepared statement
140+ * @column: column to read
141+ * @item: #KatzeItem to populate
142+ *
143+ * Stores the column in the given #KatzeItem.
144+ *
145+ * Since: 0.2.7
146+ **/
147+void
148 katze_item_set_value_from_column (sqlite3_stmt* stmt,
149 gint column,
150 KatzeItem* item)
151@@ -1069,6 +1079,17 @@
152 item = katze_item_new ();
153 for (i = 0; i < cols; i++)
154 katze_item_set_value_from_column (stmt, i, item);
155+
156+ if (KATZE_ITEM_IS_FOLDER (item))
157+ {
158+ g_object_unref (item);
159+
160+ item = KATZE_ITEM (katze_array_new (KATZE_TYPE_ITEM));
161+
162+ for (i = 0; i < cols; i++)
163+ katze_item_set_value_from_column (stmt, i, item);
164+ }
165+
166 katze_array_add_item (array, item);
167 }
168
169@@ -1102,245 +1123,3 @@
170
171 return katze_array_from_statement (stmt);
172 }
173-
174-/**
175- * midori_array_query_recursive:
176- * @array: the main bookmark array
177- * @fields: comma separated list of fields
178- * @condition: condition, like "folder = '%q'"
179- * @value: a value to be inserted if @condition contains %q
180- * @recursive: if %TRUE include children
181- *
182- * Stores the result in a #KatzeArray.
183- *
184- * Return value: a #KatzeArray on success, %NULL otherwise
185- *
186- * Since: 0.4.4
187- **/
188-KatzeArray*
189-midori_array_query_recursive (KatzeArray* bookmarks,
190- const gchar* fields,
191- const gchar* condition,
192- const gchar* value,
193- gboolean recursive)
194-{
195- sqlite3* db;
196- gchar* sqlcmd;
197- char* sqlcmd_value;
198- KatzeArray* array;
199- KatzeItem* item;
200- GList* list;
201-
202- g_return_val_if_fail (KATZE_IS_ARRAY (bookmarks), NULL);
203- g_return_val_if_fail (fields, NULL);
204- g_return_val_if_fail (condition, NULL);
205- db = g_object_get_data (G_OBJECT (bookmarks), "db");
206- g_return_val_if_fail (db != NULL, NULL);
207-
208- sqlcmd = g_strdup_printf ("SELECT %s FROM bookmarks WHERE %s "
209- "ORDER BY (uri='') ASC, title DESC", fields, condition);
210- if (strstr (condition, "%q"))
211- {
212- sqlcmd_value = sqlite3_mprintf (sqlcmd, value ? value : "");
213- array = katze_array_from_sqlite (db, sqlcmd_value);
214- sqlite3_free (sqlcmd_value);
215- }
216- else
217- array = katze_array_from_sqlite (db, sqlcmd);
218- g_free (sqlcmd);
219-
220- if (!recursive)
221- return array;
222-
223- KATZE_ARRAY_FOREACH_ITEM_L (item, array, list)
224- {
225- if (KATZE_ITEM_IS_FOLDER (item))
226- {
227- gchar* parentid = g_strdup_printf ("%" G_GINT64_FORMAT,
228- katze_item_get_meta_integer (item, "id"));
229- KatzeArray* subarray = midori_array_query_recursive (bookmarks,
230- fields, "parentid=%q", parentid, TRUE);
231- katze_item_set_name (KATZE_ITEM (subarray), katze_item_get_name (item));
232- katze_array_add_item (array, subarray);
233-
234- g_free (parentid);
235- }
236- }
237- g_list_free (list);
238- return array;
239-}
240-
241-/**
242- * midori_array_query:
243- * @array: the main bookmark array
244- * @fields: comma separated list of fields
245- * @condition: condition, like "folder = '%q'"
246- * @value: a value to be inserted if @condition contains %q
247- *
248- * Stores the result in a #KatzeArray.
249- *
250- * Return value: a #KatzeArray on success, %NULL otherwise
251- *
252- * Since: 0.4.3
253- *
254- * Deprecated: 0.4.4: Use midori_array_query_recursive() instead.
255- **/
256-KatzeArray*
257-midori_array_query (KatzeArray* bookmarks,
258- const gchar* fields,
259- const gchar* condition,
260- const gchar* value)
261-{
262- return midori_array_query_recursive (bookmarks, fields, condition, value, FALSE);
263-}
264-
265-static gint64
266-count_from_sqlite (sqlite3* db,
267- const gchar* sqlcmd)
268-{
269- gint64 count = -1;
270- sqlite3_stmt* stmt;
271- gint result;
272-
273- result = sqlite3_prepare_v2 (db, sqlcmd, -1, &stmt, NULL);
274- if (result != SQLITE_OK)
275- return -1;
276-
277- g_assert (sqlite3_column_count (stmt) == 1);
278-
279- if ((result = sqlite3_step (stmt)) == SQLITE_ROW)
280- count = sqlite3_column_int64(stmt, 0);
281-
282- sqlite3_clear_bindings (stmt);
283- sqlite3_reset (stmt);
284-
285- return count;
286-}
287-
288-static gint64
289-midori_array_count_recursive_by_id (KatzeArray* bookmarks,
290- const gchar* condition,
291- const gchar* value,
292- gint64 id,
293- gboolean recursive)
294-{
295- gint64 count = -1;
296- sqlite3* db;
297- gchar* sqlcmd;
298- char* sqlcmd_value;
299- sqlite3_stmt* stmt;
300- gint result;
301- GList* ids;
302- GList* iter_ids;
303-
304- g_return_val_if_fail (condition, -1);
305- g_return_val_if_fail (KATZE_IS_ARRAY (bookmarks), -1);
306- db = g_object_get_data (G_OBJECT (bookmarks), "db");
307- g_return_val_if_fail (db != NULL, -1);
308-
309- g_assert(!strstr("parentid", condition));
310-
311- if (id > 0)
312- sqlcmd = g_strdup_printf ("SELECT COUNT(*) FROM bookmarks "
313- "WHERE parentid = %" G_GINT64_FORMAT " AND %s",
314- id,
315- condition);
316- else
317- sqlcmd = g_strdup_printf ("SELECT COUNT(*) FROM bookmarks "
318- "WHERE parentid IS NULL AND %s ",
319- condition);
320-
321- if (strstr (condition, "%q"))
322- {
323- sqlcmd_value = sqlite3_mprintf (sqlcmd, value ? value : "");
324- count = count_from_sqlite (db, sqlcmd_value);
325- sqlite3_free (sqlcmd_value);
326- }
327- else
328- count = count_from_sqlite (db, sqlcmd);
329-
330- g_free (sqlcmd);
331-
332- if (!recursive || (count < 0))
333- return count;
334-
335- ids = NULL;
336-
337- if (id > 0)
338- sqlcmd_value = sqlite3_mprintf (
339- "SELECT id FROM bookmarks "
340- "WHERE parentid = %" G_GINT64_FORMAT " AND uri = ''", id);
341- else
342- sqlcmd_value = sqlite3_mprintf (
343- "SELECT id FROM bookmarks "
344- "WHERE parentid IS NULL AND uri = ''");
345-
346- if (sqlite3_prepare_v2 (db, sqlcmd_value, -1, &stmt, NULL) == SQLITE_OK)
347- {
348- g_assert (sqlite3_column_count (stmt) == 1);
349-
350- if ((result = sqlite3_step (stmt)) == SQLITE_ROW)
351- {
352- gint64* pid = g_new (gint64, 1);
353-
354- *pid = sqlite3_column_int64(stmt, 0);
355- ids = g_list_append (ids, pid);
356- }
357-
358- sqlite3_clear_bindings (stmt);
359- sqlite3_reset (stmt);
360- }
361-
362- sqlite3_free (sqlcmd_value);
363-
364- iter_ids = ids;
365- while (iter_ids)
366- {
367- gint64 sub_count = midori_array_count_recursive_by_id (bookmarks,
368- condition,
369- value,
370- *(gint64*)(iter_ids->data),
371- recursive);
372-
373- if (sub_count < 0)
374- {
375- g_list_free_full (ids, g_free);
376- return -1;
377- }
378-
379- count += sub_count;
380- iter_ids = g_list_next (iter_ids);
381- }
382-
383- g_list_free_full (ids, g_free);
384- return count;
385-}
386-
387-/**
388- * midori_array_count_recursive:
389- * @array: the main bookmark array
390- * @condition: condition, like "folder = '%q'"
391- * @value: a value to be inserted if @condition contains %q
392- * @recursive: if %TRUE include children
393- *
394- * Return value: the number of elements on success, -1 otherwise
395- *
396- * Since: 0.5.2
397- **/
398-gint64
399-midori_array_count_recursive (KatzeArray* bookmarks,
400- const gchar* condition,
401- const gchar* value,
402- KatzeItem* folder,
403- gboolean recursive)
404-{
405- gint64 id = -1;
406-
407- g_return_val_if_fail (!folder || KATZE_ITEM_IS_FOLDER (folder), -1);
408-
409- id = folder ? katze_item_get_meta_integer (folder, "id") : 0;
410-
411- return midori_array_count_recursive_by_id (bookmarks, condition,
412- value, id,
413- recursive);
414-}
415
416=== modified file 'midori/midori-array.h'
417--- midori/midori-array.h 2013-05-21 21:46:26 +0000
418+++ midori/midori-array.h 2013-08-05 20:10:53 +0000
419@@ -27,31 +27,16 @@
420 const gchar* format,
421 GError** error);
422
423+void
424+katze_item_set_value_from_column (sqlite3_stmt* stmt,
425+ gint column,
426+ KatzeItem* item);
427+
428 KatzeArray*
429 katze_array_from_statement (sqlite3_stmt* stmt);
430
431 KatzeArray*
432-midori_array_query (KatzeArray* array,
433- const gchar* fields,
434- const gchar* condition,
435- const gchar* value);
436-
437-KatzeArray*
438-midori_array_query_recursive (KatzeArray* array,
439- const gchar* fields,
440- const gchar* condition,
441- const gchar* value,
442- gboolean recursive);
443-
444-KatzeArray*
445 katze_array_from_sqlite (sqlite3* db,
446 const gchar* sqlcmd);
447
448-gint64
449-midori_array_count_recursive (KatzeArray* bookmarks,
450- const gchar* condition,
451- const gchar* value,
452- KatzeItem* folder,
453- gboolean recursive);
454-
455 #endif /* !__MIDORI_ARRAY_H__ */
456
457=== renamed file 'midori/midori-bookmarks.c' => 'midori/midori-bookmarks-db.c'
458--- midori/midori-bookmarks.c 2013-07-03 19:42:45 +0000
459+++ midori/midori-bookmarks-db.c 2013-08-05 20:10:53 +0000
460@@ -10,8 +10,8 @@
461 See the file COPYING for the full license text.
462 */
463
464-#include "midori-bookmarks.h"
465-#include "panels/midori-bookmarks.h"
466+#include "midori-bookmarks-db.h"
467+
468 #include "midori-app.h"
469 #include "midori-array.h"
470 #include "sokoke.h"
471@@ -25,33 +25,481 @@
472 #include <unistd.h>
473 #endif
474
475-void
476-midori_bookmarks_dbtracer (void* dummy,
477- const char* query)
478-{
479- g_printerr ("%s\n", query);
480-}
481-
482-void
483-midori_bookmarks_add_item_cb (KatzeArray* array,
484- KatzeItem* item,
485- sqlite3* db)
486-{
487- midori_bookmarks_insert_item_db (db, item,
488- katze_item_get_meta_integer (item, "parentid"));
489-}
490-
491-void
492-midori_bookmarks_remove_item_cb (KatzeArray* array,
493- KatzeItem* item,
494- sqlite3* db)
495-{
496- gchar* sqlcmd;
497- char* errmsg = NULL;
498- gchar* id;
499-
500- id = g_strdup_printf ("%" G_GINT64_FORMAT,
501- katze_item_get_meta_integer (item, "id"));
502+/**
503+ * SECTION:midory-bookmarks-db
504+ * @short_description: A #KatzeArray connected to a database
505+ * @see_also: #KatzeArray
506+ *
507+ * #MidoriBookmarksDb is a #KatzeArray specialized for database
508+ * interraction.
509+ */
510+
511+struct _MidoriBookmarksDb
512+{
513+ KatzeArray parent_instance;
514+
515+ sqlite3* db;
516+};
517+
518+struct _MidoriBookmarksDbClass
519+{
520+ KatzeArrayClass parent_class;
521+
522+ /* Signals */
523+ void
524+ (*update_item) (MidoriBookmarksDb* bookmarks,
525+ gpointer item);
526+};
527+
528+G_DEFINE_TYPE (MidoriBookmarksDb, midori_bookmarks_db, KATZE_TYPE_ARRAY);
529+
530+enum {
531+ UPDATE_ITEM,
532+
533+ LAST_SIGNAL
534+};
535+
536+static guint signals[LAST_SIGNAL];
537+
538+static void
539+_midori_bookmarks_db_add_item (KatzeArray* array,
540+ gpointer item);
541+
542+static void
543+_midori_bookmarks_db_update_item (MidoriBookmarksDb* bookmarks,
544+ gpointer item);
545+
546+static void
547+_midori_bookmarks_db_remove_item (KatzeArray* array,
548+ gpointer item);
549+
550+static void
551+_midori_bookmarks_db_move_item (KatzeArray* array,
552+ gpointer item,
553+ gint position);
554+
555+static void
556+_midori_bookmarks_db_clear (KatzeArray* array);
557+
558+static void
559+midori_bookmarks_db_finalize (GObject* object);
560+
561+static gint64
562+midori_bookmarks_db_insert_item_db (sqlite3* db,
563+ KatzeItem* item,
564+ gint64 parentid);
565+
566+static gboolean
567+midori_bookmarks_db_update_item_db (sqlite3* db,
568+ KatzeItem* item);
569+
570+static gboolean
571+midori_bookmarks_db_remove_item_db (sqlite3* db,
572+ KatzeItem* item);
573+
574+static void
575+midori_bookmarks_db_class_init (MidoriBookmarksDbClass* class)
576+{
577+ GObjectClass* gobject_class;
578+ KatzeArrayClass* katze_array_class;
579+
580+ gobject_class = G_OBJECT_CLASS (class);
581+ gobject_class->finalize = midori_bookmarks_db_finalize;
582+
583+ signals[UPDATE_ITEM] = g_signal_new (
584+ "update-item",
585+ G_TYPE_FROM_CLASS (class),
586+ (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
587+ G_STRUCT_OFFSET (MidoriBookmarksDbClass, update_item),
588+ 0,
589+ NULL,
590+ g_cclosure_marshal_VOID__POINTER,
591+ G_TYPE_NONE, 1,
592+ G_TYPE_POINTER);
593+
594+ katze_array_class = KATZE_ARRAY_CLASS (class);
595+
596+ katze_array_class->add_item = _midori_bookmarks_db_add_item;
597+ katze_array_class->remove_item = _midori_bookmarks_db_remove_item;
598+ katze_array_class->move_item = _midori_bookmarks_db_move_item;
599+ katze_array_class->clear = _midori_bookmarks_db_clear;
600+
601+ class->update_item = _midori_bookmarks_db_update_item;
602+}
603+
604+static void
605+midori_bookmarks_db_init (MidoriBookmarksDb* bookmarks)
606+{
607+ bookmarks->db = NULL;
608+
609+ katze_item_set_meta_integer (KATZE_ITEM (bookmarks), "id", 0);
610+ katze_item_set_name (KATZE_ITEM (bookmarks), _("Bookmarks"));
611+ /* g_object_ref (bookmarks); */
612+}
613+
614+static void
615+midori_bookmarks_db_finalize (GObject* object)
616+{
617+ MidoriBookmarksDb* bookmarks = MIDORI_BOOKMARKS_DB (object);
618+
619+ if (bookmarks->db)
620+ {
621+ sqlite3_close (bookmarks->db);
622+ }
623+
624+ G_OBJECT_CLASS (midori_bookmarks_db_parent_class)->finalize (object);
625+}
626+
627+/**
628+ * midori_bookmarks_db_get_item_parent:
629+ * @bookmarks: the main bookmarks array
630+ * @item: a #KatzeItem
631+ *
632+ * Internal function that find the parent of the @item thanks to its %parentid
633+ **/
634+static KatzeArray*
635+midori_bookmarks_db_get_item_parent (MidoriBookmarksDb* bookmarks,
636+ gpointer item)
637+{
638+ KatzeArray* parent;
639+ gint64 parentid;
640+
641+ parentid = katze_item_get_meta_integer (KATZE_ITEM (item), "parentid");
642+
643+ if (parentid == 0)
644+ {
645+ parent = KATZE_ARRAY (bookmarks);
646+ }
647+ else
648+ {
649+ parent = NULL;
650+ }
651+
652+ return parent;
653+}
654+
655+/**
656+ * _midori_bookmarks_db_add_item:
657+ * @array: the main bookmarks array
658+ * @item: a #KatzeItem
659+ *
660+ * Internal function that overloads the #KatzeArray %katze_array_add_item().
661+ * It relays the add item to the appropriate #KatzeArray.
662+ **/
663+static void
664+_midori_bookmarks_db_add_item (KatzeArray* array,
665+ gpointer item)
666+{
667+ MidoriBookmarksDb *bookmarks;
668+ KatzeArray* parent;
669+ KatzeArray* db_parent;
670+
671+ g_return_if_fail (IS_MIDORI_BOOKMARKS_DB (array));
672+ g_return_if_fail (KATZE_IS_ITEM (item));
673+
674+ bookmarks = MIDORI_BOOKMARKS_DB (array);
675+
676+ parent = katze_item_get_parent (KATZE_ITEM (item));
677+
678+ db_parent = midori_bookmarks_db_get_item_parent (bookmarks, item);
679+
680+ g_return_if_fail (db_parent);
681+
682+ if (parent == db_parent)
683+ {
684+ if (IS_MIDORI_BOOKMARKS_DB (parent))
685+ KATZE_ARRAY_CLASS (midori_bookmarks_db_parent_class)->update (parent);
686+ else
687+ katze_array_update (parent);
688+ return;
689+ }
690+
691+ if (IS_MIDORI_BOOKMARKS_DB (parent))
692+ KATZE_ARRAY_CLASS (midori_bookmarks_db_parent_class)->add_item (parent, item);
693+ else
694+ katze_array_add_item (parent, item);
695+}
696+
697+/**
698+ * _midori_bookmarks_db_update_item:
699+ * @array: the main bookmarks array
700+ * @item: a #KatzeItem
701+ *
702+ * Internal function that implements the %midori_bookmarks_db_update_item() post-processing.
703+ * It relays an update to the appropriate #KatzeArray.
704+ **/
705+static void
706+_midori_bookmarks_db_update_item (MidoriBookmarksDb* bookmarks,
707+ gpointer item)
708+{
709+ KatzeArray* parent;
710+
711+ g_return_if_fail (IS_MIDORI_BOOKMARKS_DB (bookmarks));
712+ g_return_if_fail (KATZE_IS_ITEM (item));
713+
714+ parent = katze_item_get_parent (KATZE_ITEM (item));
715+
716+ g_return_if_fail (parent);
717+
718+ katze_array_update (parent);
719+}
720+
721+/**
722+ * _midori_bookmarks_db_remove_item:
723+ * @array: the main bookmarks array
724+ * @item: a #KatzeItem
725+ *
726+ * Internal function that overloads the #KatzeArray %katze_array_remove_item().
727+ * It relays the remove item to the appropriate #KatzeArray.
728+ **/
729+static void
730+_midori_bookmarks_db_remove_item (KatzeArray* array,
731+ gpointer item)
732+{
733+ KatzeArray* parent;
734+
735+ g_return_if_fail (IS_MIDORI_BOOKMARKS_DB (array));
736+ g_return_if_fail (KATZE_IS_ITEM (item));
737+
738+ parent = katze_item_get_parent (KATZE_ITEM (item));
739+
740+ g_return_if_fail (parent);
741+
742+ if (IS_MIDORI_BOOKMARKS_DB (parent))
743+ KATZE_ARRAY_CLASS (midori_bookmarks_db_parent_class)->remove_item (parent, item);
744+ else
745+ katze_array_remove_item (parent, item);
746+}
747+
748+/**
749+ * _midori_bookmarks_db_move_item:
750+ * @array: the main bookmarks array
751+ * @item: a #KatzeItem
752+ * @position: the new @item position
753+ *
754+ * Internal function that overloads the #KatzeArray %katze_array_move_item().
755+ * It relays the move @item to the appropriate #KatzeArray.
756+ **/
757+static void
758+_midori_bookmarks_db_move_item (KatzeArray* array,
759+ gpointer item,
760+ gint position)
761+{
762+ MidoriBookmarksDb *bookmarks;
763+ KatzeArray* parent;
764+
765+ g_return_if_fail (IS_MIDORI_BOOKMARKS_DB (array));
766+ g_return_if_fail (KATZE_IS_ITEM (item));
767+
768+ parent = katze_item_get_parent (KATZE_ITEM (item));
769+
770+ g_return_if_fail (parent);
771+
772+ KATZE_ARRAY_CLASS (midori_bookmarks_db_parent_class)->move_item (parent, item, position);
773+}
774+
775+/**
776+ * _midori_bookmarks_db_clear:
777+ * @array: the main bookmarks array
778+ *
779+ * Internal function that overloads the #KatzeArray %katze_array_clear().
780+ * It deletes the whole bookmarks data.
781+ **/
782+static void
783+_midori_bookmarks_db_clear (KatzeArray* array)
784+{
785+ g_return_if_fail (IS_MIDORI_BOOKMARKS_DB (array));
786+
787+ g_critical ("_midori_bookmarks_db_clear: not implemented\n");
788+}
789+
790+/**
791+ * midori_bookmarks_db_signal_update_item:
792+ * @array: a #KatzeArray
793+ * @item: an item
794+ *
795+ * Notify an update of the item of the array.
796+ *
797+ **/
798+static void
799+midori_bookmarks_db_signal_update_item (MidoriBookmarksDb* array,
800+ gpointer item)
801+{
802+ g_return_if_fail (IS_MIDORI_BOOKMARKS_DB (array));
803+
804+ g_signal_emit (array, signals[UPDATE_ITEM], 0, item);
805+}
806+
807+/**
808+ * midori_bookmarks_db_insert_item_db:
809+ * @db: the #sqlite3
810+ * @item: #KatzeItem the item to insert
811+ *
812+ * Internal function that does the actual SQL INSERT of the @item in @db.
813+ *
814+ * Since: 0.5.2
815+ **/
816+static gint64
817+midori_bookmarks_db_insert_item_db (sqlite3* db,
818+ KatzeItem* item,
819+ gint64 parentid)
820+{
821+ gchar* sqlcmd;
822+ char* errmsg = NULL;
823+ KatzeItem* old_parent;
824+ gchar* new_parentid;
825+ gchar* id = NULL;
826+ const gchar* uri = NULL;
827+ const gchar* desc = NULL;
828+ gint64 seq = 0;
829+
830+ /* Bookmarks must have a name, import may produce invalid items */
831+ g_return_val_if_fail (katze_item_get_name (item), seq);
832+
833+ if (!db)
834+ return seq;
835+
836+ if (katze_item_get_meta_integer (item, "id") > 0)
837+ id = g_strdup_printf ("%" G_GINT64_FORMAT, katze_item_get_meta_integer(item, "id"));
838+ else
839+ id = g_strdup_printf ("NULL");
840+
841+ if (KATZE_ITEM_IS_BOOKMARK (item))
842+ uri = katze_item_get_uri (item);
843+
844+ if (katze_item_get_text (item))
845+ desc = katze_item_get_text (item);
846+
847+ /* Use folder, otherwise fallback to parent folder */
848+ old_parent = katze_item_get_parent (item);
849+ if (parentid > 0)
850+ new_parentid = g_strdup_printf ("%" G_GINT64_FORMAT, parentid);
851+ else if (old_parent && katze_item_get_meta_integer (old_parent, "id") > 0)
852+ new_parentid = g_strdup_printf ("%" G_GINT64_FORMAT, katze_item_get_meta_integer (old_parent, "id"));
853+ else
854+ new_parentid = g_strdup_printf ("NULL");
855+
856+ sqlcmd = sqlite3_mprintf (
857+ "INSERT INTO bookmarks (id, parentid, title, uri, desc, toolbar, app) "
858+ "VALUES (%q, %q, '%q', '%q', '%q', %d, %d)",
859+ id,
860+ new_parentid,
861+ katze_item_get_name (item),
862+ katze_str_non_null (uri),
863+ katze_str_non_null (desc),
864+ katze_item_get_meta_boolean (item, "toolbar"),
865+ katze_item_get_meta_boolean (item, "app"));
866+
867+ if (sqlite3_exec (db, sqlcmd, NULL, NULL, &errmsg) == SQLITE_OK)
868+ {
869+ /* Get insert id */
870+ if (g_str_equal (id, "NULL"))
871+ {
872+ KatzeArray* seq_array;
873+
874+ sqlite3_free (sqlcmd);
875+ sqlcmd = sqlite3_mprintf (
876+ "SELECT seq FROM sqlite_sequence WHERE name = 'bookmarks'");
877+
878+ seq_array = katze_array_from_sqlite (db, sqlcmd);
879+ if (katze_array_get_nth_item (seq_array, 0))
880+ {
881+ KatzeItem* seq_item = katze_array_get_nth_item (seq_array, 0);
882+
883+ seq = katze_item_get_meta_integer (seq_item, "seq");
884+ katze_item_set_meta_integer (item, "id", seq);
885+ }
886+ g_object_unref (seq_array);
887+ }
888+ }
889+ else
890+ {
891+ g_printerr (_("Failed to add bookmark item: %s\n"), errmsg);
892+ sqlite3_free (errmsg);
893+ }
894+
895+ sqlite3_free (sqlcmd);
896+ g_free (new_parentid);
897+ g_free (id);
898+
899+ return seq;
900+}
901+
902+/**
903+ * midori_bookmarks_db_update_item_db:
904+ * @db: the #sqlite3
905+ * @item: #KatzeItem the item to update
906+ *
907+ * Internal function that does the actual SQL UPDATE of the @item in @db.
908+ *
909+ * Since: 0.5.2
910+ **/
911+static gboolean
912+midori_bookmarks_db_update_item_db (sqlite3* db,
913+ KatzeItem* item)
914+{
915+ gchar* sqlcmd;
916+ char* errmsg = NULL;
917+ gchar* parentid;
918+ gboolean updated;
919+ gchar* id;
920+
921+ id = g_strdup_printf ("%" G_GINT64_FORMAT,
922+ katze_item_get_meta_integer (item, "id"));
923+
924+ if (katze_item_get_meta_integer (item, "parentid") > 0)
925+ parentid = g_strdup_printf ("%" G_GINT64_FORMAT,
926+ katze_item_get_meta_integer (item, "parentid"));
927+ else
928+ parentid = g_strdup_printf ("NULL");
929+
930+ sqlcmd = sqlite3_mprintf (
931+ "UPDATE bookmarks SET "
932+ "parentid=%q, title='%q', uri='%q', desc='%q', toolbar=%d, app=%d "
933+ "WHERE id = %q ;",
934+ parentid,
935+ katze_item_get_name (item),
936+ katze_str_non_null (katze_item_get_uri (item)),
937+ katze_str_non_null (katze_item_get_meta_string (item, "desc")),
938+ katze_item_get_meta_boolean (item, "toolbar"),
939+ katze_item_get_meta_boolean (item, "app"),
940+ id);
941+
942+ updated = TRUE;
943+ if (sqlite3_exec (db, sqlcmd, NULL, NULL, &errmsg) != SQLITE_OK)
944+ {
945+ updated = FALSE;
946+ g_printerr (_("Failed to update bookmark: %s\n"), errmsg);
947+ sqlite3_free (errmsg);
948+ }
949+
950+ sqlite3_free (sqlcmd);
951+ g_free (parentid);
952+ g_free (id);
953+
954+ return updated;
955+}
956+
957+/**
958+ * midori_bookmarks_db_remove_item_db:
959+ * @db: the #sqlite3
960+ * @item: #KatzeItem the item to delete
961+ *
962+ * Internal function that does the actual SQL DELETE of the @item in @db.
963+ *
964+ * Since: 0.5.2
965+ **/
966+static gboolean
967+midori_bookmarks_db_remove_item_db (sqlite3* db,
968+ KatzeItem* item)
969+{
970+ char* errmsg = NULL;
971+ gchar* sqlcmd;
972+ gboolean removed = TRUE;
973+ gchar* id;
974+
975+ id = g_strdup_printf ("%" G_GINT64_FORMAT,
976+ katze_item_get_meta_integer (item, "id"));
977
978 sqlcmd = sqlite3_mprintf ("DELETE FROM bookmarks WHERE id = %q", id);
979
980@@ -59,10 +507,78 @@
981 {
982 g_printerr (_("Failed to remove bookmark item: %s\n"), errmsg);
983 sqlite3_free (errmsg);
984+ removed = FALSE;
985 }
986
987 sqlite3_free (sqlcmd);
988 g_free (id);
989+ return removed;
990+}
991+
992+/**
993+ * midori_bookmarks_db_add_item:
994+ * @bookmarks: the main bookmark array
995+ * @item: #KatzeItem the item to update
996+ *
997+ * Adds the @item in the bookmark data base.
998+ *
999+ * Since: 0.5.2
1000+ **/
1001+void
1002+midori_bookmarks_db_add_item (MidoriBookmarksDb* bookmarks, KatzeItem* item)
1003+{
1004+ g_return_if_fail (IS_MIDORI_BOOKMARKS_DB (bookmarks));
1005+ g_return_if_fail (KATZE_IS_ITEM (item));
1006+ g_return_if_fail (NULL == katze_item_get_meta_string (item, "id"));
1007+
1008+ midori_bookmarks_db_insert_item_db (bookmarks->db, item,
1009+ katze_item_get_meta_integer (item, "parentid"));
1010+
1011+ katze_array_add_item (KATZE_ARRAY (bookmarks), item);
1012+}
1013+
1014+/**
1015+ * midori_bookmarks_db_update_item:
1016+ * @bookmarks: the main bookmark array
1017+ * @item: #KatzeItem the item to update
1018+ *
1019+ * Updates the @item in the bookmark data base.
1020+ *
1021+ * Since: 0.5.2
1022+ **/
1023+void
1024+midori_bookmarks_db_update_item (MidoriBookmarksDb* bookmarks, KatzeItem* item)
1025+{
1026+ g_return_if_fail (IS_MIDORI_BOOKMARKS_DB (bookmarks));
1027+ g_return_if_fail (KATZE_IS_ITEM (item));
1028+ g_return_if_fail (katze_item_get_meta_string (item, "id"));
1029+ g_return_if_fail (0 != katze_item_get_meta_integer (item, "id"));
1030+
1031+ midori_bookmarks_db_update_item_db (bookmarks->db, item);
1032+
1033+ midori_bookmarks_db_signal_update_item (bookmarks, item);
1034+}
1035+
1036+/**
1037+ * midori_bookmarks_db_remove_item:
1038+ * @bookmarks: the main bookmark array
1039+ * @item: #KatzeItem the item to remove
1040+ *
1041+ * Removes the @item from the bookmark data base.
1042+ *
1043+ * Since: 0.5.2
1044+ **/
1045+void
1046+midori_bookmarks_db_remove_item (MidoriBookmarksDb* bookmarks, KatzeItem* item)
1047+{
1048+ g_return_if_fail (IS_MIDORI_BOOKMARKS_DB (bookmarks));
1049+ g_return_if_fail (KATZE_IS_ITEM (item));
1050+ g_return_if_fail (katze_item_get_meta_string (item, "id"));
1051+ g_return_if_fail (0 != katze_item_get_meta_integer (item, "id"));
1052+
1053+ midori_bookmarks_db_remove_item_db (bookmarks->db, item);
1054+
1055+ katze_array_remove_item (KATZE_ARRAY (bookmarks), item);
1056 }
1057
1058 #define _APPEND_TO_SQL_ERRORMSG(custom_errmsg) \
1059@@ -76,8 +592,8 @@
1060 g_string_append (errmsg_str, custom_errmsg); \
1061 } while (0)
1062
1063-gboolean
1064-midori_bookmarks_import_from_old_db (sqlite3* db,
1065+static gboolean
1066+midori_bookmarks_db_import_from_old_db (sqlite3* db,
1067 const gchar* oldfile,
1068 gchar** errmsg)
1069 {
1070@@ -87,14 +603,14 @@
1071 GString* errmsg_str = g_string_new (NULL);
1072 gchar* attach_stmt = sqlite3_mprintf ("ATTACH DATABASE %Q AS old_db;", oldfile);
1073 const gchar* convert_stmts =
1074- "BEGIN TRANSACTION;"
1075- "INSERT INTO main.bookmarks (parentid, title, uri, desc, app, toolbar) "
1076- "SELECT NULL AS parentid, title, uri, desc, app, toolbar "
1077- "FROM old_db.bookmarks;"
1078- "UPDATE main.bookmarks SET parentid = ("
1079- "SELECT id FROM main.bookmarks AS b1 WHERE b1.title = ("
1080- "SELECT folder FROM old_db.bookmarks WHERE title = main.bookmarks.title));"
1081- "COMMIT;";
1082+ "BEGIN TRANSACTION;"
1083+ "INSERT INTO main.bookmarks (parentid, title, uri, desc, app, toolbar) "
1084+ "SELECT NULL AS parentid, title, uri, desc, app, toolbar "
1085+ "FROM old_db.bookmarks;"
1086+ "UPDATE main.bookmarks SET parentid = ("
1087+ "SELECT id FROM main.bookmarks AS b1 WHERE b1.title = ("
1088+ "SELECT folder FROM old_db.bookmarks WHERE title = main.bookmarks.title));"
1089+ "COMMIT;";
1090 const gchar* detach_stmt = "DETACH DATABASE old_db;";
1091
1092 *errmsg = NULL;
1093@@ -122,7 +638,7 @@
1094
1095 if (failure)
1096 {
1097- convert_failed:
1098+ convert_failed:
1099 *errmsg = g_string_free (errmsg_str, FALSE);
1100 g_print ("ERRORR: %s\n", errmsg_str->str);
1101 return FALSE;
1102@@ -132,8 +648,24 @@
1103 }
1104 #undef _APPEND_TO_SQL_ERRORMSG
1105
1106-KatzeArray*
1107-midori_bookmarks_new (char** errmsg)
1108+static void
1109+midori_bookmarks_db_dbtracer (void* dummy,
1110+ const char* query)
1111+{
1112+ g_printerr ("%s\n", query);
1113+}
1114+
1115+/**
1116+ * midori_bookmarks_db_new:
1117+ *
1118+ * Initializes the bookmark data base.
1119+ *
1120+ * Returns: the main bookmarks array
1121+ *
1122+ * Since: 0.5.2
1123+ **/
1124+MidoriBookmarksDb*
1125+midori_bookmarks_db_new (char** errmsg)
1126 {
1127 sqlite3* db;
1128 gchar* oldfile;
1129@@ -143,6 +675,7 @@
1130 gchar* sql_errmsg = NULL;
1131 gchar* import_errmsg = NULL;
1132 KatzeArray* array;
1133+ MidoriBookmarksDb* bookmarks;
1134
1135 g_return_val_if_fail (errmsg != NULL, NULL);
1136
1137@@ -155,97 +688,97 @@
1138 if (sqlite3_open (newfile, &db) != SQLITE_OK)
1139 {
1140 *errmsg = g_strdup_printf (_("Failed to open database: %s\n"),
1141- db ? sqlite3_errmsg (db) : "(db = NULL)");
1142+ db ? sqlite3_errmsg (db) : "(db = NULL)");
1143 goto init_failed;
1144 }
1145
1146 if (midori_debug ("bookmarks"))
1147- sqlite3_trace (db, midori_bookmarks_dbtracer, NULL);
1148+ sqlite3_trace (db, midori_bookmarks_db_dbtracer, NULL);
1149
1150 create_stmt = /* Table structure */
1151- "CREATE TABLE IF NOT EXISTS bookmarks "
1152- "(id INTEGER PRIMARY KEY AUTOINCREMENT, "
1153- "parentid INTEGER DEFAULT NULL, "
1154- "title TEXT, uri TEXT, desc TEXT, app INTEGER, toolbar INTEGER, "
1155- "pos_panel INTEGER, pos_bar INTEGER, "
1156- "created DATE DEFAULT CURRENT_TIMESTAMP, "
1157- "last_visit DATE, visit_count INTEGER DEFAULT 0, "
1158- "nick TEXT, "
1159- "FOREIGN KEY(parentid) REFERENCES bookmarks(id) "
1160- "ON DELETE CASCADE); PRAGMA foreign_keys = ON;"
1161-
1162- /* trigger: insert panel position */
1163- "CREATE TRIGGER IF NOT EXISTS bookmarkInsertPosPanel "
1164- "AFTER INSERT ON bookmarks FOR EACH ROW "
1165- "BEGIN UPDATE bookmarks SET pos_panel = ("
1166- "SELECT ifnull(MAX(pos_panel),0)+1 FROM bookmarks WHERE "
1167- "(NEW.parentid IS NOT NULL AND parentid = NEW.parentid) "
1168- "OR (NEW.parentid IS NULL AND parentid IS NULL)) "
1169- "WHERE id = NEW.id; END;"
1170-
1171- /* trigger: insert Bookmarkbar position */
1172- "CREATE TRIGGER IF NOT EXISTS bookmarkInsertPosBar "
1173- "AFTER INSERT ON bookmarks FOR EACH ROW WHEN NEW.toolbar=1 "
1174- "BEGIN UPDATE bookmarks SET pos_bar = ("
1175- "SELECT ifnull(MAX(pos_bar),0)+1 FROM bookmarks WHERE "
1176- "((NEW.parentid IS NOT NULL AND parentid = NEW.parentid) "
1177- "OR (NEW.parentid IS NULL AND parentid IS NULL)) AND toolbar=1) "
1178- "WHERE id = NEW.id; END;"
1179-
1180- /* trigger: update panel position */
1181- "CREATE TRIGGER IF NOT EXISTS bookmarkUpdatePosPanel "
1182- "BEFORE UPDATE OF parentid ON bookmarks FOR EACH ROW "
1183- "WHEN ((NEW.parentid IS NULL OR OLD.parentid IS NULL) "
1184- "AND NEW.parentid IS NOT OLD.parentid) OR "
1185- "((NEW.parentid IS NOT NULL AND OLD.parentid IS NOT NULL) "
1186- "AND NEW.parentid!=OLD.parentid) "
1187- "BEGIN UPDATE bookmarks SET pos_panel = pos_panel-1 "
1188- "WHERE ((OLD.parentid IS NOT NULL AND parentid = OLD.parentid) "
1189- "OR (OLD.parentid IS NULL AND parentid IS NULL)) AND pos_panel > OLD.pos_panel; "
1190- "UPDATE bookmarks SET pos_panel = ("
1191- "SELECT ifnull(MAX(pos_panel),0)+1 FROM bookmarks "
1192- "WHERE (NEW.parentid IS NOT NULL AND parentid = NEW.parentid) "
1193- "OR (NEW.parentid IS NULL AND parentid IS NULL)) "
1194- "WHERE id = OLD.id; END;"
1195-
1196- /* trigger: update Bookmarkbar position */
1197- "CREATE TRIGGER IF NOT EXISTS bookmarkUpdatePosBar0 "
1198- "AFTER UPDATE OF parentid, toolbar ON bookmarks FOR EACH ROW "
1199- "WHEN ((NEW.parentid IS NULL OR OLD.parentid IS NULL) "
1200- "AND NEW.parentid IS NOT OLD.parentid) "
1201- "OR ((NEW.parentid IS NOT NULL AND OLD.parentid IS NOT NULL) "
1202- "AND NEW.parentid!=OLD.parentid) OR (OLD.toolbar=1 AND NEW.toolbar=0) "
1203- "BEGIN UPDATE bookmarks SET pos_bar = NULL WHERE id = NEW.id; "
1204- "UPDATE bookmarks SET pos_bar = pos_bar-1 "
1205- "WHERE ((OLD.parentid IS NOT NULL AND parentid = OLD.parentid) "
1206- "OR (OLD.parentid IS NULL AND parentid IS NULL)) AND pos_bar > OLD.pos_bar; END;"
1207-
1208- /* trigger: update Bookmarkbar position */
1209- "CREATE TRIGGER IF NOT EXISTS bookmarkUpdatePosBar1 "
1210- "BEFORE UPDATE OF parentid, toolbar ON bookmarks FOR EACH ROW "
1211- "WHEN ((NEW.parentid IS NULL OR OLD.parentid IS NULL) "
1212- "AND NEW.parentid IS NOT OLD.parentid) OR "
1213- "((NEW.parentid IS NOT NULL AND OLD.parentid IS NOT NULL) "
1214- "AND NEW.parentid!=OLD.parentid) OR (OLD.toolbar=0 AND NEW.toolbar=1) "
1215- "BEGIN UPDATE bookmarks SET pos_bar = ("
1216- "SELECT ifnull(MAX(pos_bar),0)+1 FROM bookmarks WHERE "
1217- "(NEW.parentid IS NOT NULL AND parentid = NEW.parentid) "
1218- "OR (NEW.parentid IS NULL AND parentid IS NULL)) "
1219- "WHERE id = OLD.id; END;"
1220-
1221- /* trigger: delete panel position */
1222- "CREATE TRIGGER IF NOT EXISTS bookmarkDeletePosPanel "
1223- "AFTER DELETE ON bookmarks FOR EACH ROW "
1224- "BEGIN UPDATE bookmarks SET pos_panel = pos_panel-1 "
1225- "WHERE ((OLD.parentid IS NOT NULL AND parentid = OLD.parentid) "
1226- "OR (OLD.parentid IS NULL AND parentid IS NULL)) AND pos_panel > OLD.pos_panel; END;"
1227-
1228- /* trigger: delete Bookmarkbar position */
1229- "CREATE TRIGGER IF NOT EXISTS bookmarkDeletePosBar "
1230- "AFTER DELETE ON bookmarks FOR EACH ROW WHEN OLD.toolbar=1 "
1231- "BEGIN UPDATE bookmarks SET pos_bar = pos_bar-1 "
1232- "WHERE ((OLD.parentid IS NOT NULL AND parentid = OLD.parentid) "
1233- "OR (OLD.parentid IS NULL AND parentid IS NULL)) AND pos_bar > OLD.pos_bar; END;";
1234+ "CREATE TABLE IF NOT EXISTS bookmarks "
1235+ "(id INTEGER PRIMARY KEY AUTOINCREMENT, "
1236+ "parentid INTEGER DEFAULT NULL, "
1237+ "title TEXT, uri TEXT, desc TEXT, app INTEGER, toolbar INTEGER, "
1238+ "pos_panel INTEGER, pos_bar INTEGER, "
1239+ "created DATE DEFAULT CURRENT_TIMESTAMP, "
1240+ "last_visit DATE, visit_count INTEGER DEFAULT 0, "
1241+ "nick TEXT, "
1242+ "FOREIGN KEY(parentid) REFERENCES bookmarks(id) "
1243+ "ON DELETE CASCADE); PRAGMA foreign_keys = ON;"
1244+
1245+ /* trigger: insert panel position */
1246+ "CREATE TRIGGER IF NOT EXISTS bookmarkInsertPosPanel "
1247+ "AFTER INSERT ON bookmarks FOR EACH ROW "
1248+ "BEGIN UPDATE bookmarks SET pos_panel = ("
1249+ "SELECT ifnull(MAX(pos_panel),0)+1 FROM bookmarks WHERE "
1250+ "(NEW.parentid IS NOT NULL AND parentid = NEW.parentid) "
1251+ "OR (NEW.parentid IS NULL AND parentid IS NULL)) "
1252+ "WHERE id = NEW.id; END;"
1253+
1254+ /* trigger: insert Bookmarkbar position */
1255+ "CREATE TRIGGER IF NOT EXISTS bookmarkInsertPosBar "
1256+ "AFTER INSERT ON bookmarks FOR EACH ROW WHEN NEW.toolbar=1 "
1257+ "BEGIN UPDATE bookmarks SET pos_bar = ("
1258+ "SELECT ifnull(MAX(pos_bar),0)+1 FROM bookmarks WHERE "
1259+ "((NEW.parentid IS NOT NULL AND parentid = NEW.parentid) "
1260+ "OR (NEW.parentid IS NULL AND parentid IS NULL)) AND toolbar=1) "
1261+ "WHERE id = NEW.id; END;"
1262+
1263+ /* trigger: update panel position */
1264+ "CREATE TRIGGER IF NOT EXISTS bookmarkUpdatePosPanel "
1265+ "BEFORE UPDATE OF parentid ON bookmarks FOR EACH ROW "
1266+ "WHEN ((NEW.parentid IS NULL OR OLD.parentid IS NULL) "
1267+ "AND NEW.parentid IS NOT OLD.parentid) OR "
1268+ "((NEW.parentid IS NOT NULL AND OLD.parentid IS NOT NULL) "
1269+ "AND NEW.parentid!=OLD.parentid) "
1270+ "BEGIN UPDATE bookmarks SET pos_panel = pos_panel-1 "
1271+ "WHERE ((OLD.parentid IS NOT NULL AND parentid = OLD.parentid) "
1272+ "OR (OLD.parentid IS NULL AND parentid IS NULL)) AND pos_panel > OLD.pos_panel; "
1273+ "UPDATE bookmarks SET pos_panel = ("
1274+ "SELECT ifnull(MAX(pos_panel),0)+1 FROM bookmarks "
1275+ "WHERE (NEW.parentid IS NOT NULL AND parentid = NEW.parentid) "
1276+ "OR (NEW.parentid IS NULL AND parentid IS NULL)) "
1277+ "WHERE id = OLD.id; END;"
1278+
1279+ /* trigger: update Bookmarkbar position */
1280+ "CREATE TRIGGER IF NOT EXISTS bookmarkUpdatePosBar0 "
1281+ "AFTER UPDATE OF parentid, toolbar ON bookmarks FOR EACH ROW "
1282+ "WHEN ((NEW.parentid IS NULL OR OLD.parentid IS NULL) "
1283+ "AND NEW.parentid IS NOT OLD.parentid) "
1284+ "OR ((NEW.parentid IS NOT NULL AND OLD.parentid IS NOT NULL) "
1285+ "AND NEW.parentid!=OLD.parentid) OR (OLD.toolbar=1 AND NEW.toolbar=0) "
1286+ "BEGIN UPDATE bookmarks SET pos_bar = NULL WHERE id = NEW.id; "
1287+ "UPDATE bookmarks SET pos_bar = pos_bar-1 "
1288+ "WHERE ((OLD.parentid IS NOT NULL AND parentid = OLD.parentid) "
1289+ "OR (OLD.parentid IS NULL AND parentid IS NULL)) AND pos_bar > OLD.pos_bar; END;"
1290+
1291+ /* trigger: update Bookmarkbar position */
1292+ "CREATE TRIGGER IF NOT EXISTS bookmarkUpdatePosBar1 "
1293+ "BEFORE UPDATE OF parentid, toolbar ON bookmarks FOR EACH ROW "
1294+ "WHEN ((NEW.parentid IS NULL OR OLD.parentid IS NULL) "
1295+ "AND NEW.parentid IS NOT OLD.parentid) OR "
1296+ "((NEW.parentid IS NOT NULL AND OLD.parentid IS NOT NULL) "
1297+ "AND NEW.parentid!=OLD.parentid) OR (OLD.toolbar=0 AND NEW.toolbar=1) "
1298+ "BEGIN UPDATE bookmarks SET pos_bar = ("
1299+ "SELECT ifnull(MAX(pos_bar),0)+1 FROM bookmarks WHERE "
1300+ "(NEW.parentid IS NOT NULL AND parentid = NEW.parentid) "
1301+ "OR (NEW.parentid IS NULL AND parentid IS NULL)) "
1302+ "WHERE id = OLD.id; END;"
1303+
1304+ /* trigger: delete panel position */
1305+ "CREATE TRIGGER IF NOT EXISTS bookmarkDeletePosPanel "
1306+ "AFTER DELETE ON bookmarks FOR EACH ROW "
1307+ "BEGIN UPDATE bookmarks SET pos_panel = pos_panel-1 "
1308+ "WHERE ((OLD.parentid IS NOT NULL AND parentid = OLD.parentid) "
1309+ "OR (OLD.parentid IS NULL AND parentid IS NULL)) AND pos_panel > OLD.pos_panel; END;"
1310+
1311+ /* trigger: delete Bookmarkbar position */
1312+ "CREATE TRIGGER IF NOT EXISTS bookmarkDeletePosBar "
1313+ "AFTER DELETE ON bookmarks FOR EACH ROW WHEN OLD.toolbar=1 "
1314+ "BEGIN UPDATE bookmarks SET pos_bar = pos_bar-1 "
1315+ "WHERE ((OLD.parentid IS NOT NULL AND parentid = OLD.parentid) "
1316+ "OR (OLD.parentid IS NULL AND parentid IS NULL)) AND pos_bar > OLD.pos_bar; END;";
1317
1318
1319 if (newfile_did_exist)
1320@@ -255,7 +788,7 @@
1321 if (sqlite3_exec (db, setup_stmt, NULL, NULL, &sql_errmsg) != SQLITE_OK)
1322 {
1323 *errmsg = g_strdup_printf (_("Couldn't setup bookmarks: %s\n"),
1324- sql_errmsg ? sql_errmsg : "(err = NULL)");
1325+ sql_errmsg ? sql_errmsg : "(err = NULL)");
1326 sqlite3_free (sql_errmsg);
1327 goto init_failed;
1328 }
1329@@ -269,7 +802,7 @@
1330 if (sqlite3_exec (db, create_stmt, NULL, NULL, &sql_errmsg) != SQLITE_OK)
1331 {
1332 *errmsg = g_strdup_printf (_("Couldn't create bookmarks table: %s\n"),
1333- sql_errmsg ? sql_errmsg : "(err = NULL)");
1334+ sql_errmsg ? sql_errmsg : "(err = NULL)");
1335 sqlite3_free (sql_errmsg);
1336
1337 /* we can as well remove the new file */
1338@@ -281,59 +814,379 @@
1339
1340 if (oldfile_exists)
1341 /* import from old db */
1342- if (!midori_bookmarks_import_from_old_db (db, oldfile, &import_errmsg))
1343+ if (!midori_bookmarks_db_import_from_old_db (db, oldfile, &import_errmsg))
1344 {
1345 *errmsg = g_strdup_printf (_("Couldn't import from old database: %s\n"),
1346- import_errmsg ? import_errmsg : "(err = NULL)");
1347+ import_errmsg ? import_errmsg : "(err = NULL)");
1348 g_free (import_errmsg);
1349 }
1350
1351- init_success:
1352- g_free (newfile);
1353- g_free (oldfile);
1354- array = katze_array_new (KATZE_TYPE_ARRAY);
1355- g_signal_connect (array, "add-item",
1356- G_CALLBACK (midori_bookmarks_add_item_cb), db);
1357- g_signal_connect (array, "remove-item",
1358- G_CALLBACK (midori_bookmarks_remove_item_cb), db);
1359- g_object_set_data (G_OBJECT (array), "db", db);
1360+ init_success:
1361+ g_free (newfile);
1362+ g_free (oldfile);
1363+ bookmarks = MIDORI_BOOKMARKS_DB (g_object_new (TYPE_MIDORI_BOOKMARKS_DB, NULL));
1364+ bookmarks->db = db;
1365+
1366+ g_object_set_data (G_OBJECT (bookmarks), "db", db);
1367+ return bookmarks;
1368+
1369+ init_failed:
1370+ g_free (newfile);
1371+ g_free (oldfile);
1372+
1373+ if (db)
1374+ sqlite3_close (db);
1375+
1376+ return NULL;
1377+}
1378+
1379+/**
1380+ * midori_bookmarks_db_on_quit:
1381+ * @bookmarks: the main bookmark array
1382+ *
1383+ * Delete the main bookmark array.
1384+ *
1385+ * Since: 0.5.2
1386+ **/
1387+void
1388+midori_bookmarks_db_on_quit (MidoriBookmarksDb* bookmarks)
1389+{
1390+ g_return_if_fail (IS_MIDORI_BOOKMARKS_DB (bookmarks));
1391+
1392+ g_object_unref (bookmarks);
1393+}
1394+
1395+/**
1396+ * midori_bookmarks_db_import_array:
1397+ * @array: the main bookmark array
1398+ * @array: #KatzeArray containing the items to import
1399+ * @parentid: the id of folder
1400+ *
1401+ * Imports the items of @array as childs of the folder
1402+ * identfied by @parentid.
1403+ *
1404+ * Since: 0.5.2
1405+ **/
1406+void
1407+midori_bookmarks_db_import_array (MidoriBookmarksDb* bookmarks,
1408+ KatzeArray* array,
1409+ gint64 parentid)
1410+{
1411+ GList* list;
1412+ KatzeItem* item;
1413+
1414+ g_return_if_fail (IS_MIDORI_BOOKMARKS_DB (bookmarks));
1415+ g_return_if_fail (KATZE_IS_ARRAY (array));
1416+
1417+ KATZE_ARRAY_FOREACH_ITEM_L (item, array, list)
1418+ {
1419+ katze_item_set_meta_integer (item, "parentid", parentid);
1420+ midori_bookmarks_db_add_item (bookmarks, item);
1421+ if (KATZE_IS_ARRAY (item))
1422+ midori_bookmarks_db_import_array (bookmarks, KATZE_ARRAY (item),
1423+ katze_item_get_meta_integer(item, "id"));
1424+ }
1425+ g_list_free (list);
1426+}
1427+
1428+/**
1429+ * midori_bookmarks_db_array_from_statement:
1430+ * @stmt: the sqlite returned statement
1431+ * @bookmarks: the database controller
1432+ *
1433+ * Internal function that populate a #KatzeArray by processing the @stmt
1434+ * rows identifying:
1435+ * a- if the item is already in memory
1436+ * in this case the item data is updated with retreived database content
1437+ * and the already existing item is populated in the returned #KatzeArray
1438+ * b- if the data is a folder
1439+ * a new #KatzeArray item is populated in the returned #KatzeArray and
1440+ * memorized for future use.
1441+ * c- if the data is a bookmark
1442+ * a new #KatzeItem item is populated in the returned #KatzeArray and
1443+ * memorized for furure use.
1444+ *
1445+ * Return value: the populated #KatzeArray
1446+ **/
1447+static KatzeArray*
1448+midori_bookmarks_db_array_from_statement (sqlite3_stmt* stmt,
1449+ MidoriBookmarksDb* bookmarks)
1450+{
1451+ KatzeArray *array;
1452+ gint result;
1453+ gint cols;
1454+
1455+ array = katze_array_new (KATZE_TYPE_ITEM);
1456+ cols = sqlite3_column_count (stmt);
1457+
1458+ while ((result = sqlite3_step (stmt)) == SQLITE_ROW)
1459+ {
1460+ gint i;
1461+ KatzeItem* item;
1462+ KatzeItem* found;
1463+
1464+ item = katze_item_new ();
1465+ for (i = 0; i < cols; i++)
1466+ katze_item_set_value_from_column (stmt, i, item);
1467+
1468+ if (KATZE_ITEM_IS_FOLDER (item))
1469+ {
1470+ g_object_unref (item);
1471+
1472+ item = KATZE_ITEM (katze_array_new (KATZE_TYPE_ITEM));
1473+
1474+ for (i = 0; i < cols; i++)
1475+ katze_item_set_value_from_column (stmt, i, item);
1476+ }
1477+
1478+ katze_array_add_item (array, item);
1479+ }
1480+
1481+ sqlite3_clear_bindings (stmt);
1482+ sqlite3_reset (stmt);
1483+ return array;
1484+}
1485+
1486+/**
1487+ * midori_bookmarks_db_array_from_sqlite:
1488+ * @array: the main bookmark array
1489+ * @sqlcmd: the sqlcmd to execute
1490+ *
1491+ * Internal function that process the requested @sqlcmd.
1492+ *
1493+ * Return value: a #KatzeArray on success, %NULL otherwise
1494+ **/
1495+static KatzeArray*
1496+midori_bookmarks_db_array_from_sqlite (MidoriBookmarksDb* bookmarks,
1497+ const gchar* sqlcmd)
1498+{
1499+ sqlite3_stmt* stmt;
1500+ gint result;
1501+
1502+ g_return_val_if_fail (bookmarks->db != NULL, NULL);
1503+
1504+ result = sqlite3_prepare_v2 (bookmarks->db, sqlcmd, -1, &stmt, NULL);
1505+ if (result != SQLITE_OK)
1506+ return NULL;
1507+
1508+ return midori_bookmarks_db_array_from_statement (stmt, bookmarks);
1509+}
1510+
1511+/**
1512+ * midori_bookmarks_db_query_recursive:
1513+ * @array: the main bookmark array
1514+ * @fields: comma separated list of fields
1515+ * @condition: condition, like "folder = '%q'"
1516+ * @value: a value to be inserted if @condition contains %q
1517+ * @recursive: if %TRUE include children
1518+ *
1519+ * Stores the result in a #KatzeArray.
1520+ *
1521+ * Return value: a #KatzeArray on success, %NULL otherwise
1522+ *
1523+ * Since: 0.5.2
1524+ **/
1525+KatzeArray*
1526+midori_bookmarks_db_query_recursive (MidoriBookmarksDb* bookmarks,
1527+ const gchar* fields,
1528+ const gchar* condition,
1529+ const gchar* value,
1530+ gboolean recursive)
1531+{
1532+ gchar* sqlcmd;
1533+ char* sqlcmd_value;
1534+ KatzeArray* array;
1535+ KatzeItem* item;
1536+ GList* list;
1537+
1538+ g_return_val_if_fail (IS_MIDORI_BOOKMARKS_DB (bookmarks), NULL);
1539+ g_return_val_if_fail (fields, NULL);
1540+ g_return_val_if_fail (condition, NULL);
1541+
1542+ sqlcmd = g_strdup_printf ("SELECT %s FROM bookmarks WHERE %s "
1543+ "ORDER BY (uri='') ASC, title DESC", fields, condition);
1544+ if (strstr (condition, "%q"))
1545+ {
1546+ sqlcmd_value = sqlite3_mprintf (sqlcmd, value ? value : "");
1547+ array = midori_bookmarks_db_array_from_sqlite (bookmarks, sqlcmd_value);
1548+ sqlite3_free (sqlcmd_value);
1549+ }
1550+ else
1551+ array = midori_bookmarks_db_array_from_sqlite (bookmarks, sqlcmd);
1552+ g_free (sqlcmd);
1553+
1554+ if (!recursive)
1555 return array;
1556
1557- init_failed:
1558- g_free (newfile);
1559- g_free (oldfile);
1560-
1561- if (db)
1562- sqlite3_close (db);
1563-
1564- return NULL;
1565-}
1566-
1567-void
1568-midori_bookmarks_import (const gchar* filename,
1569- sqlite3* db)
1570-{
1571- KatzeArray* bookmarks;
1572- GError* error = NULL;
1573-
1574- bookmarks = katze_array_new (KATZE_TYPE_ARRAY);
1575-
1576- if (!midori_array_from_file (bookmarks, filename, "xbel", &error))
1577- {
1578- g_warning (_("The bookmarks couldn't be saved. %s"), error->message);
1579- g_error_free (error);
1580- return;
1581- }
1582- midori_bookmarks_import_array_db (db, bookmarks, 0);
1583-}
1584-
1585-void
1586-midori_bookmarks_on_quit (KatzeArray* array)
1587-{
1588- g_return_if_fail (KATZE_IS_ARRAY (array));
1589-
1590- sqlite3* db = g_object_get_data (G_OBJECT (array), "db");
1591- g_return_if_fail (db != NULL);
1592- sqlite3_close (db);
1593-}
1594-
1595+ KATZE_ARRAY_FOREACH_ITEM_L (item, array, list)
1596+ {
1597+ if (KATZE_ITEM_IS_FOLDER (item))
1598+ {
1599+ gchar* parentid = g_strdup_printf ("%" G_GINT64_FORMAT,
1600+ katze_item_get_meta_integer (item, "id"));
1601+ KatzeArray* subarray = midori_bookmarks_db_query_recursive (bookmarks,
1602+ fields, "parentid=%q", parentid, TRUE);
1603+ KatzeItem* subitem;
1604+ GList* sublist;
1605+
1606+ KATZE_ARRAY_FOREACH_ITEM_L (subitem, subarray, sublist)
1607+ {
1608+ katze_array_add_item (KATZE_ARRAY (item), subitem);
1609+ }
1610+
1611+ g_object_unref (subarray);
1612+ g_free (parentid);
1613+ }
1614+ }
1615+ g_list_free (list);
1616+ return array;
1617+}
1618+
1619+static gint64
1620+midori_bookmarks_db_count_from_sqlite (sqlite3* db,
1621+ const gchar* sqlcmd)
1622+{
1623+ gint64 count = -1;
1624+ sqlite3_stmt* stmt;
1625+ gint result;
1626+
1627+ result = sqlite3_prepare_v2 (db, sqlcmd, -1, &stmt, NULL);
1628+ if (result != SQLITE_OK)
1629+ return -1;
1630+
1631+ g_assert (sqlite3_column_count (stmt) == 1);
1632+
1633+ if ((result = sqlite3_step (stmt)) == SQLITE_ROW)
1634+ count = sqlite3_column_int64(stmt, 0);
1635+
1636+ sqlite3_clear_bindings (stmt);
1637+ sqlite3_reset (stmt);
1638+
1639+ return count;
1640+}
1641+
1642+static gint64
1643+midori_bookmarks_db_count_recursive_by_id (MidoriBookmarksDb* bookmarks,
1644+ const gchar* condition,
1645+ const gchar* value,
1646+ gint64 id,
1647+ gboolean recursive)
1648+{
1649+ gint64 count = -1;
1650+ gchar* sqlcmd;
1651+ char* sqlcmd_value;
1652+ sqlite3_stmt* stmt;
1653+ gint result;
1654+ GList* ids;
1655+ GList* iter_ids;
1656+
1657+ g_return_val_if_fail (condition, -1);
1658+ g_return_val_if_fail (MIDORI_BOOKMARKS_DB (bookmarks), -1);
1659+ g_return_val_if_fail (bookmarks->db != NULL, -1);
1660+
1661+ g_assert(!strstr("parentid", condition));
1662+
1663+ if (id > 0)
1664+ sqlcmd = g_strdup_printf ("SELECT COUNT(*) FROM bookmarks "
1665+ "WHERE parentid = %" G_GINT64_FORMAT " AND %s",
1666+ id,
1667+ condition);
1668+ else
1669+ sqlcmd = g_strdup_printf ("SELECT COUNT(*) FROM bookmarks "
1670+ "WHERE parentid IS NULL AND %s ",
1671+ condition);
1672+
1673+ if (strstr (condition, "%q"))
1674+ {
1675+ sqlcmd_value = sqlite3_mprintf (sqlcmd, value ? value : "");
1676+ count = midori_bookmarks_db_count_from_sqlite (bookmarks->db, sqlcmd_value);
1677+ sqlite3_free (sqlcmd_value);
1678+ }
1679+ else
1680+ count = midori_bookmarks_db_count_from_sqlite (bookmarks->db, sqlcmd);
1681+
1682+ g_free (sqlcmd);
1683+
1684+ if (!recursive || (count < 0))
1685+ return count;
1686+
1687+ ids = NULL;
1688+
1689+ if (id > 0)
1690+ sqlcmd_value = sqlite3_mprintf (
1691+ "SELECT id FROM bookmarks "
1692+ "WHERE parentid = %" G_GINT64_FORMAT " AND uri = ''", id);
1693+ else
1694+ sqlcmd_value = sqlite3_mprintf (
1695+ "SELECT id FROM bookmarks "
1696+ "WHERE parentid IS NULL AND uri = ''");
1697+
1698+ if (sqlite3_prepare_v2 (bookmarks->db, sqlcmd_value, -1, &stmt, NULL) == SQLITE_OK)
1699+ {
1700+ g_assert (sqlite3_column_count (stmt) == 1);
1701+
1702+ if ((result = sqlite3_step (stmt)) == SQLITE_ROW)
1703+ {
1704+ gint64* pid = g_new (gint64, 1);
1705+
1706+ *pid = sqlite3_column_int64(stmt, 0);
1707+ ids = g_list_append (ids, pid);
1708+ }
1709+
1710+ sqlite3_clear_bindings (stmt);
1711+ sqlite3_reset (stmt);
1712+ }
1713+
1714+ sqlite3_free (sqlcmd_value);
1715+
1716+ iter_ids = ids;
1717+ while (iter_ids)
1718+ {
1719+ gint64 sub_count = midori_bookmarks_db_count_recursive_by_id (bookmarks,
1720+ condition,
1721+ value,
1722+ *(gint64*)(iter_ids->data),
1723+ recursive);
1724+
1725+ if (sub_count < 0)
1726+ {
1727+ g_list_free_full (ids, g_free);
1728+ return -1;
1729+ }
1730+
1731+ count += sub_count;
1732+ iter_ids = g_list_next (iter_ids);
1733+ }
1734+
1735+ g_list_free_full (ids, g_free);
1736+ return count;
1737+}
1738+
1739+/**
1740+ * midori_bookmarks_db_count_recursive:
1741+ * @bookmarks: the main bookmark array
1742+ * @condition: condition, like "folder = '%q'"
1743+ * @value: a value to be inserted if @condition contains %q
1744+ * @recursive: if %TRUE include children
1745+ *
1746+ * Return value: the number of elements on success, -1 otherwise
1747+ *
1748+ * Since: 0.5.2
1749+ **/
1750+gint64
1751+midori_bookmarks_db_count_recursive (MidoriBookmarksDb* bookmarks,
1752+ const gchar* condition,
1753+ const gchar* value,
1754+ KatzeItem* folder,
1755+ gboolean recursive)
1756+{
1757+ gint64 id = -1;
1758+
1759+ g_return_val_if_fail (!folder || KATZE_ITEM_IS_FOLDER (folder), -1);
1760+
1761+ id = folder ? katze_item_get_meta_integer (folder, "id") : 0;
1762+
1763+ return midori_bookmarks_db_count_recursive_by_id (bookmarks, condition,
1764+ value, id,
1765+ recursive);
1766+}
1767
1768=== renamed file 'midori/midori-bookmarks.h' => 'midori/midori-bookmarks-db.h'
1769--- midori/midori-bookmarks.h 2012-11-25 15:37:41 +0000
1770+++ midori/midori-bookmarks-db.h 2013-08-05 20:10:53 +0000
1771@@ -10,31 +10,66 @@
1772 See the file COPYING for the full license text.
1773 */
1774
1775-#ifndef __MIDORI_BOOKMARKS_H__
1776-#define __MIDORI_BOOKMARKS_H__ 1
1777+#ifndef __MIDORI_BOOKMARKS_DB_H__
1778+#define __MIDORI_BOOKMARKS_DB_H__ 1
1779
1780 #include <sqlite3.h>
1781 #include <katze/katze.h>
1782
1783-void
1784-midori_bookmarks_add_item_cb (KatzeArray* array,
1785- KatzeItem* item,
1786- sqlite3* db);
1787-
1788-void
1789-midori_bookmarks_remove_item_cb (KatzeArray* array,
1790- KatzeItem* item,
1791- sqlite3* db);
1792+G_BEGIN_DECLS
1793+
1794+#define TYPE_MIDORI_BOOKMARKS_DB \
1795+ (midori_bookmarks_db_get_type ())
1796+#define MIDORI_BOOKMARKS_DB(obj) \
1797+ (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_MIDORI_BOOKMARKS_DB, MidoriBookmarksDb))
1798+#define MIDORI_BOOKMARKS_DB_CLASS(klass) \
1799+ (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_MIDORI_BOOKMARKS_DB, MidoriBookmarksDbClass))
1800+#define IS_MIDORI_BOOKMARKS_DB(obj) \
1801+ (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_MIDORI_BOOKMARKS_DB))
1802+#define IS_MIDORI_BOOKMARKS_DB_CLASS(klass) \
1803+ (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_MIDORI_BOOKMARKS_DB))
1804+#define MIDORI_BOOKMARKS_DB_GET_CLASS(obj) \
1805+ (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_MIDORI_BOOKMARKS_DB, MidoriBookmarksDbClass))
1806+
1807+typedef struct _MidoriBookmarksDb MidoriBookmarksDb;
1808+typedef struct _MidoriBookmarksDbClass MidoriBookmarksDbClass;
1809+
1810+GType
1811+midori_bookmarks_db_get_type (void) G_GNUC_CONST;
1812+
1813+MidoriBookmarksDb*
1814+midori_bookmarks_db_new (char** errmsg);
1815+
1816+void
1817+midori_bookmarks_db_on_quit (MidoriBookmarksDb* array);
1818+
1819+void
1820+midori_bookmarks_db_add_item (MidoriBookmarksDb* bookmarks, KatzeItem* item);
1821+
1822+void
1823+midori_bookmarks_db_update_item (MidoriBookmarksDb* bookmarks, KatzeItem* item);
1824+
1825+void
1826+midori_bookmarks_db_remove_item (MidoriBookmarksDb* bookmarks, KatzeItem* item);
1827+
1828+void
1829+midori_bookmarks_db_import_array (MidoriBookmarksDb* bookmarks,
1830+ KatzeArray* array,
1831+ gint64 parentid);
1832
1833 KatzeArray*
1834-midori_bookmarks_new (char** errmsg);
1835-
1836-void
1837-midori_bookmarks_on_quit (KatzeArray* array);
1838-
1839-void
1840-midori_bookmarks_import (const gchar* filename,
1841- sqlite3* db);
1842-
1843-#endif /* !__MIDORI_BOOKMARKS_H__ */
1844+midori_bookmarks_db_query_recursive (MidoriBookmarksDb* bookmarks,
1845+ const gchar* fields,
1846+ const gchar* condition,
1847+ const gchar* value,
1848+ gboolean recursive);
1849+
1850+gint64
1851+midori_bookmarks_db_count_recursive (MidoriBookmarksDb* bookmarks,
1852+ const gchar* condition,
1853+ const gchar* value,
1854+ KatzeItem* folder,
1855+ gboolean recursive);
1856+
1857+#endif /* !__MIDORI_BOOKMARKS_DB_H__ */
1858
1859
1860=== modified file 'midori/midori-browser.c'
1861--- midori/midori-browser.c 2013-08-02 12:00:42 +0000
1862+++ midori/midori-browser.c 2013-08-05 20:10:53 +0000
1863@@ -26,6 +26,7 @@
1864 #include "midori-privatedata.h"
1865 #include "midori-core.h"
1866 #include "midori-privatedata.h"
1867+#include "midori-bookmarks-db.h"
1868 #include "katze-cellrenderercomboboxtext.h"
1869
1870 #include "marshal.h"
1871@@ -86,7 +87,7 @@
1872
1873 MidoriWebSettings* settings;
1874 KatzeArray* proxy_array;
1875- KatzeArray* bookmarks;
1876+ MidoriBookmarksDb* bookmarks;
1877 KatzeArray* trash;
1878 KatzeArray* search_engines;
1879 KatzeArray* history;
1880@@ -97,6 +98,8 @@
1881 gboolean show_statusbar;
1882 guint maximum_history_age;
1883 guint last_web_search;
1884+
1885+ gboolean bookmarkbar_populate;
1886 };
1887
1888 G_DEFINE_TYPE (MidoriBrowser, midori_browser, GTK_TYPE_WINDOW)
1889@@ -163,20 +166,13 @@
1890 GParamSpec* pspec);
1891
1892 void
1893-midori_bookmarks_import_array_db (sqlite3* db,
1894- KatzeArray* array,
1895- gint64 parentid);
1896-
1897-gboolean
1898-midori_bookmarks_update_item_db (sqlite3* db,
1899- KatzeItem* item);
1900-
1901-void
1902 midori_browser_open_bookmark (MidoriBrowser* browser,
1903 KatzeItem* item);
1904
1905 static void
1906 midori_bookmarkbar_populate (MidoriBrowser* browser);
1907+static void
1908+midori_bookmarkbar_populate_idle (MidoriBrowser* browser);
1909
1910 static void
1911 midori_bookmarkbar_clear (GtkWidget* toolbar);
1912@@ -199,8 +195,8 @@
1913 GtkToolbarStyle style);
1914
1915 static void
1916-midori_browser_set_bookmarks (MidoriBrowser* browser,
1917- KatzeArray* bookmarks);
1918+midori_browser_set_bookmarks (MidoriBrowser* browser,
1919+ MidoriBookmarksDb* bookmarks);
1920
1921 static void
1922 midori_browser_add_speed_dial (MidoriBrowser* browser);
1923@@ -867,7 +863,7 @@
1924 }
1925
1926 static GtkWidget*
1927-midori_bookmark_folder_button_new (KatzeArray* array,
1928+midori_bookmark_folder_button_new (MidoriBookmarksDb* array,
1929 gint64 selected_parentid)
1930 {
1931 GtkTreeStore* model;
1932@@ -1097,9 +1093,6 @@
1933 GtkWidget* check_toolbar;
1934 GtkWidget* check_app;
1935 gboolean return_status = FALSE;
1936- sqlite3* db = g_object_get_data (G_OBJECT (browser->bookmarks), "db");
1937- if (!db)
1938- return FALSE;
1939
1940 if (is_folder)
1941 title = new_bookmark ? _("New Folder") : _("Edit Folder");
1942@@ -1227,18 +1220,13 @@
1943 katze_item_set_meta_integer (bookmark, "parentid", selected);
1944
1945 if (new_bookmark)
1946- katze_array_add_item (browser->bookmarks, bookmark);
1947+ midori_bookmarks_db_add_item (browser->bookmarks, bookmark);
1948 else
1949- midori_bookmarks_update_item_db (db, bookmark);
1950- midori_browser_update_history (bookmark, "bookmark", new_bookmark ? "create" : "modify");
1951+ midori_bookmarks_db_update_item (browser->bookmarks, bookmark);
1952
1953- if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (check_toolbar)))
1954- if (!gtk_widget_get_visible (browser->bookmarkbar))
1955- _action_set_active (browser, "Bookmarkbar", TRUE);
1956 return_status = TRUE;
1957 }
1958- if (gtk_widget_get_visible (browser->bookmarkbar))
1959- midori_bookmarkbar_populate (browser);
1960+
1961 gtk_widget_destroy (dialog);
1962 return return_status;
1963 }
1964@@ -2511,7 +2499,7 @@
1965 "bookmarks",
1966 "Bookmarks",
1967 "The bookmarks folder, containing all bookmarks",
1968- KATZE_TYPE_ARRAY,
1969+ TYPE_MIDORI_BOOKMARKS_DB,
1970 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1971
1972 /**
1973@@ -3233,8 +3221,8 @@
1974 else
1975 condition = "parentid = %q";
1976
1977- bookmarks = midori_array_query (browser->bookmarks,
1978- "id, title, parentid, uri, app, pos_panel, pos_bar", condition, id);
1979+ bookmarks = midori_bookmarks_db_query_recursive (browser->bookmarks,
1980+ "id, title, parentid, uri, app, pos_panel, pos_bar", condition, id, FALSE);
1981 if (!bookmarks)
1982 return FALSE;
1983
1984@@ -4261,7 +4249,7 @@
1985 KatzeItem* item;
1986
1987 item = (KatzeItem*)g_object_get_data (G_OBJECT (menuitem), "KatzeItem");
1988- katze_array_remove_item (browser->bookmarks, item);
1989+ midori_bookmarks_db_remove_item (browser->bookmarks, item);
1990 }
1991
1992 static void
1993@@ -4273,7 +4261,7 @@
1994 MidoriContextAction* menu = midori_context_action_new ("BookmarkContextMenu", NULL, NULL, NULL);
1995 if (KATZE_ITEM_IS_FOLDER (item))
1996 {
1997- gint child_bookmarks_count = midori_array_count_recursive (browser->bookmarks,
1998+ gint child_bookmarks_count = midori_bookmarks_db_count_recursive (browser->bookmarks,
1999 "uri <> ''", NULL, item, FALSE);
2000
2001 GtkAction* action = gtk_action_new ("BookmarkOpenAllTabs", _("Open all in _Tabs"), NULL, STOCK_TAB_NEW);
2002@@ -4516,7 +4504,6 @@
2003 gchar* path = NULL;
2004 gint64 selected;
2005 GError* error;
2006- sqlite3* db = g_object_get_data (G_OBJECT (browser->bookmarks), "db");
2007
2008 if (gtk_combo_box_get_active_iter (combobox, &iter))
2009 gtk_tree_model_get (GTK_TREE_MODEL (model), &iter, 2, &path, -1);
2010@@ -4544,8 +4531,7 @@
2011 if (error)
2012 g_error_free (error);
2013 }
2014- midori_bookmarks_import_array_db (db, bookmarks, selected);
2015- katze_array_update (browser->bookmarks);
2016+ midori_bookmarks_db_import_array (browser->bookmarks, bookmarks, selected);
2017 g_object_unref (bookmarks);
2018 g_free (path);
2019 }
2020@@ -4599,7 +4585,7 @@
2021 return;
2022
2023 error = NULL;
2024- bookmarks = midori_array_query_recursive (browser->bookmarks,
2025+ bookmarks = midori_bookmarks_db_query_recursive (browser->bookmarks,
2026 "*", "parentid IS NULL", NULL, TRUE);
2027 if (!midori_array_to_file (bookmarks, path, format, &error))
2028 {
2029@@ -5988,6 +5974,21 @@
2030 }
2031 }
2032
2033+static gboolean
2034+midori_browser_idle (gpointer data)
2035+{
2036+ MidoriBrowser* browser = MIDORI_BROWSER (data);
2037+
2038+ if (browser->bookmarkbar_populate)
2039+ {
2040+ midori_bookmarkbar_populate_idle (browser);
2041+
2042+ browser->bookmarkbar_populate = FALSE;
2043+ }
2044+
2045+ return FALSE;
2046+}
2047+
2048 static void
2049 midori_browser_init (MidoriBrowser* browser)
2050 {
2051@@ -6447,6 +6448,8 @@
2052 katze_object_assign (browser->history, NULL);
2053 katze_object_assign (browser->dial, NULL);
2054
2055+ g_idle_remove_by_data (browser);
2056+
2057 G_OBJECT_CLASS (midori_browser_parent_class)->finalize (object);
2058 }
2059
2060@@ -6982,6 +6985,28 @@
2061 }
2062
2063 static void
2064+midori_bookmarkbar_add_item_cb (KatzeArray* bookmarks,
2065+ KatzeItem* item,
2066+ MidoriBrowser* browser)
2067+{
2068+ if (gtk_widget_get_visible (browser->bookmarkbar))
2069+ midori_bookmarkbar_populate (browser);
2070+ else if (katze_item_get_meta_boolean (item, "toolbar"))
2071+ _action_set_active (browser, "Bookmarkbar", TRUE);
2072+ midori_browser_update_history (item, "bookmark", "created");
2073+}
2074+
2075+static void
2076+midori_bookmarkbar_update_item_cb (KatzeArray* bookmarks,
2077+ KatzeItem* item,
2078+ MidoriBrowser* browser)
2079+{
2080+ if (gtk_widget_get_visible (browser->bookmarkbar))
2081+ midori_bookmarkbar_populate (browser);
2082+ midori_browser_update_history (item, "bookmark", "modify");
2083+}
2084+
2085+static void
2086 midori_bookmarkbar_remove_item_cb (KatzeArray* bookmarks,
2087 KatzeItem* item,
2088 MidoriBrowser* browser)
2089@@ -6994,6 +7019,16 @@
2090 static void
2091 midori_bookmarkbar_populate (MidoriBrowser* browser)
2092 {
2093+ if (browser->bookmarkbar_populate)
2094+ return;
2095+
2096+ g_idle_add (midori_browser_idle, browser);
2097+ browser->bookmarkbar_populate = TRUE;
2098+}
2099+
2100+static void
2101+midori_bookmarkbar_populate_idle (MidoriBrowser* browser)
2102+{
2103 KatzeArray* array;
2104 KatzeItem* item;
2105
2106@@ -7003,8 +7038,8 @@
2107 gtk_toolbar_insert (GTK_TOOLBAR (browser->bookmarkbar),
2108 gtk_separator_tool_item_new (), -1);
2109
2110- array = midori_array_query (browser->bookmarks,
2111- "id, parentid, title, uri, desc, app, toolbar, pos_panel, pos_bar", "toolbar = 1", NULL);
2112+ array = midori_bookmarks_db_query_recursive (browser->bookmarks,
2113+ "id, parentid, title, uri, desc, app, toolbar, pos_panel, pos_bar", "toolbar = 1", NULL, FALSE);
2114 if (!array)
2115 {
2116 _action_set_sensitive (browser, "BookmarkAdd", FALSE);
2117@@ -7014,29 +7049,7 @@
2118
2119 KATZE_ARRAY_FOREACH_ITEM (item, array)
2120 {
2121- if (KATZE_ITEM_IS_BOOKMARK (item))
2122- midori_bookmarkbar_insert_item (browser->bookmarkbar, item);
2123- else
2124- {
2125- gint64 id = katze_item_get_meta_integer (item, "id");
2126- gchar* parentid = g_strdup_printf ("%" G_GINT64_FORMAT, id);
2127- KatzeArray* subfolder = midori_array_query (browser->bookmarks,
2128- "id, parentid, title, uri, desc, app, toolbar, pos_panel, pos_bar", "parentid = %q",
2129- parentid);
2130-
2131- katze_item_set_name (KATZE_ITEM (subfolder), katze_item_get_name (item));
2132- katze_item_set_meta_integer (KATZE_ITEM (subfolder), "id", id);
2133- katze_item_set_meta_integer (KATZE_ITEM (subfolder), "toolbar", 1);
2134- katze_item_set_meta_integer (KATZE_ITEM (subfolder), "parentid",
2135- katze_item_get_meta_integer (item, "parentid"));
2136- katze_item_set_uri (KATZE_ITEM (subfolder), NULL);
2137- katze_item_set_meta_string (KATZE_ITEM (subfolder), "desc",
2138- katze_item_get_meta_string (KATZE_ITEM (item), "desc"));
2139- katze_item_set_meta_integer (KATZE_ITEM (subfolder), "app",
2140- katze_item_get_meta_boolean (KATZE_ITEM (item), "app"));
2141- midori_bookmarkbar_insert_item (browser->bookmarkbar, KATZE_ITEM (subfolder));
2142- g_free (parentid);
2143- }
2144+ midori_bookmarkbar_insert_item (browser->bookmarkbar, item);
2145 }
2146 _action_set_sensitive (browser, "BookmarkAdd", TRUE);
2147 _action_set_sensitive (browser, "BookmarkFolderAdd", TRUE);
2148@@ -7066,13 +7079,20 @@
2149
2150 static void
2151 midori_browser_set_bookmarks (MidoriBrowser* browser,
2152- KatzeArray* bookmarks)
2153+ MidoriBookmarksDb* bookmarks)
2154 {
2155 MidoriWebSettings* settings;
2156
2157 if (browser->bookmarks != NULL)
2158+ {
2159+ g_signal_handlers_disconnect_by_func (browser->bookmarks,
2160+ midori_bookmarkbar_add_item_cb, browser);
2161+ g_signal_handlers_disconnect_by_func (browser->bookmarks,
2162+ midori_bookmarkbar_update_item_cb, browser);
2163 g_signal_handlers_disconnect_by_func (browser->bookmarks,
2164 midori_bookmarkbar_remove_item_cb, browser);
2165+ }
2166+
2167 settings = midori_browser_get_settings (browser);
2168 g_signal_handlers_disconnect_by_func (settings,
2169 midori_browser_show_bookmarkbar_notify_value_cb, browser);
2170@@ -7101,6 +7121,10 @@
2171 g_signal_connect (settings, "notify::show-bookmarkbar",
2172 G_CALLBACK (midori_browser_show_bookmarkbar_notify_value_cb), browser);
2173 g_object_notify (G_OBJECT (settings), "show-bookmarkbar");
2174+ g_signal_connect_after (bookmarks, "add-item",
2175+ G_CALLBACK (midori_bookmarkbar_add_item_cb), browser);
2176+ g_signal_connect_after (bookmarks, "update-item",
2177+ G_CALLBACK (midori_bookmarkbar_update_item_cb), browser);
2178 g_signal_connect_after (bookmarks, "remove-item",
2179 G_CALLBACK (midori_bookmarkbar_remove_item_cb), browser);
2180 }
2181
2182=== modified file 'midori/midori-frontend.c'
2183--- midori/midori-frontend.c 2013-07-30 21:00:46 +0000
2184+++ midori/midori-frontend.c 2013-08-05 20:10:53 +0000
2185@@ -10,7 +10,7 @@
2186 */
2187
2188 #include "midori-array.h"
2189-#include "midori-bookmarks.h"
2190+#include "midori-bookmarks-db.h"
2191 #include "midori-history.h"
2192 #include "midori-preferences.h"
2193 #include "midori-privatedata.h"
2194@@ -487,9 +487,9 @@
2195 }
2196 g_free (uri);
2197
2198- KatzeArray* bookmarks;
2199+ MidoriBookmarksDb* bookmarks;
2200 gchar* errmsg = NULL;
2201- if (!(bookmarks = midori_bookmarks_new (&errmsg)))
2202+ if (!(bookmarks = midori_bookmarks_db_new (&errmsg)))
2203 {
2204 g_string_append_printf (error_messages,
2205 _("Bookmarks couldn't be loaded: %s\n"), errmsg);
2206@@ -595,11 +595,11 @@
2207 midori_normal_app_on_quit (MidoriApp* app)
2208 {
2209 MidoriWebSettings* settings = katze_object_get_object (app, "settings");
2210- KatzeArray* bookmarks = katze_object_get_object (app, "bookmarks");
2211+ MidoriBookmarksDb* bookmarks = katze_object_get_object (app, "bookmarks");
2212 KatzeArray* history = katze_object_get_object (app, "history");
2213
2214 g_object_notify (G_OBJECT (settings), "load-on-startup");
2215- midori_bookmarks_on_quit (bookmarks);
2216+ midori_bookmarks_db_on_quit (bookmarks);
2217 midori_history_on_quit (history, settings);
2218 midori_private_data_on_quit (settings);
2219
2220
2221=== modified file 'midori/midori.h'
2222--- midori/midori.h 2012-12-02 15:32:20 +0000
2223+++ midori/midori.h 2013-08-05 20:10:53 +0000
2224@@ -14,7 +14,7 @@
2225
2226 #include "midori-app.h"
2227 #include "midori-array.h"
2228-#include "midori-bookmarks.h"
2229+#include "midori-bookmarks-db.h"
2230 #include "midori-browser.h"
2231 #include "midori-extension.h"
2232 #include "midori-frontend.h"
2233
2234=== modified file 'panels/midori-bookmarks.c'
2235--- panels/midori-bookmarks.c 2013-07-01 20:20:53 +0000
2236+++ panels/midori-bookmarks.c 2013-08-05 20:10:53 +0000
2237@@ -17,6 +17,7 @@
2238 #include "midori-platform.h"
2239 #include "midori-view.h"
2240 #include "midori-core.h"
2241+#include "midori-bookmarks-db.h"
2242
2243 #include <glib/gi18n.h>
2244 #include <string.h>
2245@@ -44,7 +45,7 @@
2246 GtkWidget* delete;
2247 GtkWidget* treeview;
2248 MidoriApp* app;
2249- KatzeArray* array;
2250+ MidoriBookmarksDb* bookmarks_db;
2251
2252 gint filter_timeout;
2253 gchar* filter;
2254@@ -124,6 +125,7 @@
2255 return STOCK_BOOKMARKS;
2256 }
2257
2258+#if 0
2259 /* TODO: Function never used */
2260 void
2261 midori_bookmarks_export_array_db (sqlite3* db,
2262@@ -137,7 +139,7 @@
2263 gchar* parent_id;
2264
2265 parent_id = g_strdup_printf ("%" G_GINT64_FORMAT, parentid);
2266- if (!(root_array = midori_array_query (array, "*", "parentid = %q", parent_id)))
2267+ if (!(root_array = midori_bookmarks_db_query_recursive (array, "*", "parentid = %q", parent_id, FALSE)))
2268 {
2269 g_free (parent_id);
2270 return;
2271@@ -159,27 +161,7 @@
2272 g_free (parent_id);
2273 g_list_free (list);
2274 }
2275-
2276-void
2277-midori_bookmarks_import_array_db (sqlite3* db,
2278- KatzeArray* array,
2279- gint64 parentid)
2280-{
2281- GList* list;
2282- KatzeItem* item;
2283- gint64 id;
2284-
2285- if (!db)
2286- return;
2287-
2288- KATZE_ARRAY_FOREACH_ITEM_L (item, array, list)
2289- {
2290- id = midori_bookmarks_insert_item_db (db, item, parentid);
2291- if (KATZE_IS_ARRAY (item))
2292- midori_bookmarks_import_array_db (db, KATZE_ARRAY (item), id);
2293- }
2294- g_list_free (list);
2295-}
2296+#endif
2297
2298 static KatzeArray*
2299 midori_bookmarks_read_from_db (MidoriBookmarks* bookmarks,
2300@@ -189,21 +171,21 @@
2301 KatzeArray* array;
2302
2303 if (keyword && *keyword)
2304- array = midori_array_query (bookmarks->array,
2305- "id, parentid, title, uri, desc, app, toolbar, pos_panel, pos_bar", "title LIKE '%%%q%%'", keyword);
2306+ array = midori_bookmarks_db_query_recursive (bookmarks->bookmarks_db,
2307+ "id, parentid, title, uri, desc, app, toolbar, pos_panel, pos_bar", "title LIKE '%%%q%%'", keyword, FALSE);
2308 else
2309 {
2310 if (parentid > 0)
2311 {
2312 gchar* parent_id = g_strdup_printf ("%" G_GINT64_FORMAT, parentid);
2313- array = midori_array_query (bookmarks->array,
2314- "id, parentid, title, uri, desc, app, toolbar, pos_panel, pos_bar", "parentid = %q", parent_id);
2315+ array = midori_bookmarks_db_query_recursive (bookmarks->bookmarks_db,
2316+ "id, parentid, title, uri, desc, app, toolbar, pos_panel, pos_bar", "parentid = %q", parent_id, FALSE);
2317
2318 g_free (parent_id);
2319 }
2320 else
2321- array = midori_array_query (bookmarks->array,
2322- "id, parentid, title, uri, desc, app, toolbar, pos_panel, pos_bar", "parentid IS NULL", NULL);
2323+ array = midori_bookmarks_db_query_recursive (bookmarks->bookmarks_db,
2324+ "id, parentid, title, uri, desc, app, toolbar, pos_panel, pos_bar", "parentid IS NULL", NULL, FALSE);
2325 }
2326 return array ? array : katze_array_new (KATZE_TYPE_ITEM);
2327 }
2328@@ -234,92 +216,6 @@
2329 g_object_unref (item);
2330 }
2331
2332-gint64
2333-midori_bookmarks_insert_item_db (sqlite3* db,
2334- KatzeItem* item,
2335- gint64 parentid)
2336-{
2337- gchar* sqlcmd;
2338- char* errmsg = NULL;
2339- KatzeItem* old_parent;
2340- gchar* new_parentid;
2341- gchar* id = NULL;
2342- const gchar* uri = NULL;
2343- const gchar* desc = NULL;
2344- gint64 seq = 0;
2345-
2346- /* Bookmarks must have a name, import may produce invalid items */
2347- g_return_val_if_fail (katze_item_get_name (item), seq);
2348-
2349- if (!db)
2350- return seq;
2351-
2352- if (katze_item_get_meta_integer (item, "id") > 0)
2353- id = g_strdup_printf ("%" G_GINT64_FORMAT, katze_item_get_meta_integer(item, "id"));
2354- else
2355- id = g_strdup_printf ("NULL");
2356-
2357- if (KATZE_ITEM_IS_BOOKMARK (item))
2358- uri = katze_item_get_uri (item);
2359-
2360- if (katze_item_get_text (item))
2361- desc = katze_item_get_text (item);
2362-
2363- /* Use folder, otherwise fallback to parent folder */
2364- old_parent = katze_item_get_parent (item);
2365- if (parentid > 0)
2366- new_parentid = g_strdup_printf ("%" G_GINT64_FORMAT, parentid);
2367- else if (old_parent && katze_item_get_meta_integer (old_parent, "id") > 0)
2368- new_parentid = g_strdup_printf ("%" G_GINT64_FORMAT, katze_item_get_meta_integer (old_parent, "id"));
2369- else
2370- new_parentid = g_strdup_printf ("NULL");
2371-
2372- sqlcmd = sqlite3_mprintf (
2373- "INSERT INTO bookmarks (id, parentid, title, uri, desc, toolbar, app) "
2374- "VALUES (%q, %q, '%q', '%q', '%q', %d, %d)",
2375- id,
2376- new_parentid,
2377- katze_item_get_name (item),
2378- katze_str_non_null (uri),
2379- katze_str_non_null (desc),
2380- katze_item_get_meta_boolean (item, "toolbar"),
2381- katze_item_get_meta_boolean (item, "app"));
2382-
2383- if (sqlite3_exec (db, sqlcmd, NULL, NULL, &errmsg) == SQLITE_OK)
2384- {
2385- /* Get insert id */
2386- if (g_str_equal (id, "NULL"))
2387- {
2388- KatzeArray* seq_array;
2389-
2390- sqlite3_free (sqlcmd);
2391- sqlcmd = sqlite3_mprintf (
2392- "SELECT seq FROM sqlite_sequence WHERE name = 'bookmarks'");
2393-
2394- seq_array = katze_array_from_sqlite (db, sqlcmd);
2395- if (katze_array_get_nth_item (seq_array, 0))
2396- {
2397- KatzeItem* seq_item = katze_array_get_nth_item (seq_array, 0);
2398-
2399- seq = katze_item_get_meta_integer (seq_item, "seq");
2400- katze_item_set_meta_integer (item, "id", seq);
2401- }
2402- g_object_unref (seq_array);
2403- }
2404- }
2405- else
2406- {
2407- g_printerr (_("Failed to add bookmark item: %s\n"), errmsg);
2408- sqlite3_free (errmsg);
2409- }
2410-
2411- sqlite3_free (sqlcmd);
2412- g_free (new_parentid);
2413- g_free (id);
2414-
2415- return seq;
2416-}
2417-
2418 static void
2419 midori_bookmarks_add_item_cb (KatzeArray* array,
2420 KatzeItem* item,
2421@@ -380,9 +276,9 @@
2422 else
2423 parentid = 0;
2424
2425- katze_array_remove_item (bookmarks->array, item);
2426 katze_item_set_meta_integer (item, "parentid", parentid);
2427- katze_array_add_item (bookmarks->array, item);
2428+
2429+ midori_bookmarks_db_update_item (bookmarks->bookmarks_db, item);
2430
2431 g_object_unref (item);
2432 if (new_parent)
2433@@ -391,7 +287,7 @@
2434
2435 static void
2436 midori_bookmarks_add_clicked_cb (GtkWidget* toolitem,
2437- MidoriBookmarks* bookmarks)
2438+ MidoriBookmarks* bookmarks)
2439 {
2440 MidoriBrowser* browser = midori_browser_get_for_widget (toolitem);
2441 GtkTreeView* treeview = GTK_TREE_VIEW (bookmarks->treeview);
2442@@ -522,9 +418,9 @@
2443
2444 if (KATZE_ITEM_IS_FOLDER (item))
2445 {
2446- gint child_folders_count = midori_array_count_recursive (bookmarks->array,
2447+ gint child_folders_count = midori_bookmarks_db_count_recursive (bookmarks->bookmarks_db,
2448 "uri = ''", NULL, item, FALSE);
2449- gint child_bookmarks_count = midori_array_count_recursive (bookmarks->array,
2450+ gint child_bookmarks_count = midori_bookmarks_db_count_recursive (bookmarks->bookmarks_db,
2451 "uri <> ''", NULL, item, FALSE);
2452 gchar* child_folders_str = midori_bookmarks_statusbar_subfolders_str (child_folders_count);
2453 gchar* child_bookmarks_str = midori_bookmarks_statusbar_bookmarks_str (child_bookmarks_count);
2454@@ -557,9 +453,9 @@
2455 }
2456 else
2457 {
2458- gint child_folders_count = midori_array_count_recursive (bookmarks->array,
2459+ gint child_folders_count = midori_bookmarks_db_count_recursive (bookmarks->bookmarks_db,
2460 "uri = ''", NULL, NULL, FALSE);
2461- gint child_bookmarks_count = midori_array_count_recursive (bookmarks->array,
2462+ gint child_bookmarks_count = midori_bookmarks_db_count_recursive (bookmarks->bookmarks_db,
2463 "uri <> ''", NULL, NULL, FALSE);
2464 gchar* child_folders_str = midori_bookmarks_statusbar_subfolders_str (child_folders_count);
2465 gchar* child_bookmarks_str = midori_bookmarks_statusbar_bookmarks_str (child_bookmarks_count);
2466@@ -589,52 +485,6 @@
2467 }
2468 }
2469
2470-gboolean
2471-midori_bookmarks_update_item_db (sqlite3* db,
2472- KatzeItem* item)
2473-{
2474- gchar* sqlcmd;
2475- char* errmsg = NULL;
2476- gchar* parentid;
2477- gboolean updated;
2478- gchar* id;
2479-
2480- id = g_strdup_printf ("%" G_GINT64_FORMAT,
2481- katze_item_get_meta_integer (item, "id"));
2482-
2483- if (katze_item_get_meta_integer (item, "parentid") > 0)
2484- parentid = g_strdup_printf ("%" G_GINT64_FORMAT,
2485- katze_item_get_meta_integer (item, "parentid"));
2486- else
2487- parentid = g_strdup_printf ("NULL");
2488-
2489- sqlcmd = sqlite3_mprintf (
2490- "UPDATE bookmarks SET "
2491- "parentid=%q, title='%q', uri='%q', desc='%q', toolbar=%d, app=%d "
2492- "WHERE id = %q ;",
2493- parentid,
2494- katze_item_get_name (item),
2495- katze_str_non_null (katze_item_get_uri (item)),
2496- katze_str_non_null (katze_item_get_meta_string (item, "desc")),
2497- katze_item_get_meta_boolean (item, "toolbar"),
2498- katze_item_get_meta_boolean (item, "app"),
2499- id);
2500-
2501- updated = TRUE;
2502- if (sqlite3_exec (db, sqlcmd, NULL, NULL, &errmsg) != SQLITE_OK)
2503- {
2504- updated = FALSE;
2505- g_printerr (_("Failed to update bookmark: %s\n"), errmsg);
2506- sqlite3_free (errmsg);
2507- }
2508-
2509- sqlite3_free (sqlcmd);
2510- g_free (parentid);
2511- g_free (id);
2512-
2513- return updated;
2514-}
2515-
2516 static void
2517 midori_bookmarks_delete_clicked_cb (GtkWidget* toolitem,
2518 MidoriBookmarks* bookmarks)
2519@@ -651,10 +501,10 @@
2520
2521 /* Manually remove the iter and block clearing the treeview */
2522 gtk_tree_store_remove (GTK_TREE_STORE (model), &iter);
2523- g_signal_handlers_block_by_func (bookmarks->array,
2524+ g_signal_handlers_block_by_func (bookmarks->bookmarks_db,
2525 midori_bookmarks_remove_item_cb, bookmarks);
2526- katze_array_remove_item (bookmarks->array, item);
2527- g_signal_handlers_unblock_by_func (bookmarks->array,
2528+ midori_bookmarks_db_remove_item (bookmarks->bookmarks_db, item);
2529+ g_signal_handlers_unblock_by_func (bookmarks->bookmarks_db,
2530 midori_bookmarks_remove_item_cb, bookmarks);
2531 g_object_unref (item);
2532 }
2533@@ -737,9 +587,9 @@
2534 GtkTreeModel* model;
2535
2536 model = gtk_tree_view_get_model (GTK_TREE_VIEW (bookmarks->treeview));
2537- if (bookmarks->array)
2538+ if (bookmarks->bookmarks_db)
2539 {
2540- g_object_unref (bookmarks->array);
2541+ g_object_unref (bookmarks->bookmarks_db);
2542 gtk_tree_store_clear (GTK_TREE_STORE (model));
2543 }
2544 katze_assign (bookmarks->app, app);
2545@@ -747,17 +597,17 @@
2546 return;
2547
2548 g_object_ref (app);
2549- bookmarks->array = katze_object_get_object (app, "bookmarks");
2550+ bookmarks->bookmarks_db = katze_object_get_object (app, "bookmarks");
2551 midori_bookmarks_read_from_db_to_model (bookmarks, GTK_TREE_STORE (model), NULL, 0, NULL);
2552- g_signal_connect_after (bookmarks->array, "add-item",
2553- G_CALLBACK (midori_bookmarks_add_item_cb), bookmarks);
2554- g_signal_connect (bookmarks->array, "remove-item",
2555- G_CALLBACK (midori_bookmarks_remove_item_cb), bookmarks);
2556- g_signal_connect (bookmarks->array, "update",
2557- G_CALLBACK (midori_bookmarks_update_cb), bookmarks);
2558+ g_signal_connect_after (bookmarks->bookmarks_db, "add-item",
2559+ G_CALLBACK (midori_bookmarks_add_item_cb), bookmarks);
2560+ g_signal_connect (bookmarks->bookmarks_db, "remove-item",
2561+ G_CALLBACK (midori_bookmarks_remove_item_cb), bookmarks);
2562+ g_signal_connect (bookmarks->bookmarks_db, "update",
2563+ G_CALLBACK (midori_bookmarks_update_cb), bookmarks);
2564 g_signal_connect_after (model, "row-changed",
2565- G_CALLBACK (midori_bookmarks_row_changed_cb),
2566- bookmarks);
2567+ G_CALLBACK (midori_bookmarks_row_changed_cb),
2568+ bookmarks);
2569 }
2570
2571 static void
2572@@ -987,7 +837,7 @@
2573 menu = gtk_menu_new ();
2574 if (KATZE_ITEM_IS_FOLDER (item))
2575 {
2576- gint child_bookmarks_count = midori_array_count_recursive (bookmarks->array,
2577+ gint child_bookmarks_count = midori_bookmarks_db_count_recursive (bookmarks->bookmarks_db,
2578 "uri <> ''", NULL, item, FALSE);
2579
2580 midori_bookmarks_popup_item (menu,
2581
2582=== modified file 'panels/midori-bookmarks.h'
2583--- panels/midori-bookmarks.h 2012-11-25 11:26:03 +0000
2584+++ panels/midori-bookmarks.h 2013-08-05 20:10:53 +0000
2585@@ -42,11 +42,6 @@
2586 KatzeItem* item,
2587 gint64 parentid);
2588
2589-void
2590-midori_bookmarks_import_array_db (sqlite3* db,
2591- KatzeArray* array,
2592- gint64 parentid);
2593-
2594 gboolean
2595 midori_bookmarks_update_item_db (sqlite3* db,
2596 KatzeItem* item);
2597
2598=== modified file 'tests/bookmarks.c'
2599--- tests/bookmarks.c 2012-11-25 15:37:41 +0000
2600+++ tests/bookmarks.c 2013-08-05 20:10:53 +0000
2601@@ -10,11 +10,11 @@
2602 */
2603
2604 #include "midori.h"
2605-#include "panels/midori-bookmarks.h"
2606+#include "midori-bookmarks-db.h"
2607
2608 typedef struct
2609 {
2610- KatzeArray* db_bookmarks;
2611+ MidoriBookmarksDb* db_bookmarks;
2612 KatzeArray* test_bookmarks;
2613 } BookmarksFixture;
2614
2615@@ -37,7 +37,7 @@
2616 KatzeArray* folder;
2617 gchar *errmsg = NULL;
2618
2619- if (!(fixture->db_bookmarks = midori_bookmarks_new (&errmsg)))
2620+ if (!(fixture->db_bookmarks = midori_bookmarks_db_new (&errmsg)))
2621 g_error ("Bookmarks couldn't be loaded: %s\n", errmsg);
2622 g_assert (errmsg == NULL);
2623 g_assert (g_object_get_data (G_OBJECT (fixture->db_bookmarks), "db"));
2624@@ -84,8 +84,8 @@
2625 fixture_teardown (BookmarksFixture* fixture,
2626 const TestParameters *params)
2627 {
2628- midori_bookmarks_on_quit (fixture->db_bookmarks);
2629- g_object_unref (fixture->db_bookmarks);
2630+ midori_bookmarks_db_on_quit (fixture->db_bookmarks);
2631+ /* g_object_unref (fixture->db_bookmarks); */
2632 g_object_unref (fixture->test_bookmarks);
2633 }
2634
2635@@ -112,7 +112,7 @@
2636 /* NB: assumes "title" is unique in a set */
2637 static void
2638 compare_test_and_db (KatzeArray* test_bookmarks,
2639- KatzeArray* db_bookmarks,
2640+ MidoriBookmarksDb* db_bookmarks,
2641 gboolean verbose)
2642 {
2643 KatzeArray* db_items;
2644@@ -127,7 +127,7 @@
2645 g_print ("----------\n");
2646 }
2647
2648- db_items = midori_array_query_recursive (db_bookmarks,
2649+ db_items = midori_bookmarks_db_query_recursive (db_bookmarks,
2650 "*", "title='%q'", katze_item_get_name (test_item), FALSE);
2651
2652 /* FIXME g_assert_cmpint (katze_array_get_length (db_items), ==, 1); */
2653@@ -142,28 +142,33 @@
2654 }
2655
2656 static void
2657+print_bookmarks (KatzeArray* test_bookmarks)
2658+{
2659+ KatzeItem* item;
2660+ GList* list;
2661+ KATZE_ARRAY_FOREACH_ITEM_L (item, test_bookmarks, list)
2662+ {
2663+ print_bookmark (item);
2664+ g_print ("----------\n");
2665+
2666+ if (KATZE_ITEM_IS_FOLDER(item))
2667+ print_bookmarks (KATZE_ARRAY (item));
2668+ }
2669+ g_list_free (list);
2670+}
2671+
2672+static void
2673 insert_bookmarks (KatzeArray* test_bookmarks,
2674- KatzeArray* db_bookmarks,
2675+ MidoriBookmarksDb* db_bookmarks,
2676 gboolean verbose)
2677 {
2678- KatzeItem* item;
2679- GList* list;
2680- sqlite3 *db = g_object_get_data (G_OBJECT (db_bookmarks), "db");
2681
2682- KATZE_ARRAY_FOREACH_ITEM_L (item, test_bookmarks, list)
2683+ if (verbose)
2684 {
2685- if (verbose)
2686- {
2687- print_bookmark (item);
2688- g_print ("----------\n");
2689- }
2690-
2691- midori_bookmarks_insert_item_db (db, item, 0);
2692-
2693- if (KATZE_ITEM_IS_FOLDER(item))
2694- insert_bookmarks (KATZE_ARRAY (item), db_bookmarks, verbose);
2695+ print_bookmarks (test_bookmarks);
2696 }
2697- g_list_free (list);
2698+
2699+ midori_bookmarks_db_import_array (db_bookmarks, test_bookmarks, 0);
2700 }
2701
2702 static void

Subscribers

People subscribed via source and target branches

to all changes: