25
25
#include <unistd.h>
29
midori_bookmarks_insert_item_db (sqlite3* db,
34
midori_bookmarks_update_item_db (sqlite3* db,
38
midori_bookmarks_insert_item_db (sqlite3* db,
29
* SECTION:midory-bookmarks-db
30
* @short_description: A #KatzeArray connected to a database
31
* @see_also: #KatzeArray
33
* #MidoriBookmarksDb is a #KatzeArray specialized for database
37
struct _MidoriBookmarksDb
39
KatzeArray parent_instance;
44
struct _MidoriBookmarksDbClass
46
KatzeArrayClass parent_class;
50
(*update_item) (MidoriBookmarksDb* bookmarks,
54
G_DEFINE_TYPE (MidoriBookmarksDb, midori_bookmarks_db, KATZE_TYPE_ARRAY);
62
static guint signals[LAST_SIGNAL];
65
_midori_bookmarks_db_add_item (KatzeArray* array,
69
_midori_bookmarks_db_update_item (MidoriBookmarksDb* bookmarks,
73
_midori_bookmarks_db_remove_item (KatzeArray* array,
77
_midori_bookmarks_db_move_item (KatzeArray* array,
82
_midori_bookmarks_db_clear (KatzeArray* array);
85
midori_bookmarks_db_finalize (GObject* object);
88
midori_bookmarks_db_insert_item_db (sqlite3* db,
93
midori_bookmarks_db_update_item_db (sqlite3* db,
97
midori_bookmarks_db_remove_item_db (sqlite3* db,
101
midori_bookmarks_db_class_init (MidoriBookmarksDbClass* class)
103
GObjectClass* gobject_class;
104
KatzeArrayClass* katze_array_class;
106
gobject_class = G_OBJECT_CLASS (class);
107
gobject_class->finalize = midori_bookmarks_db_finalize;
109
signals[UPDATE_ITEM] = g_signal_new (
111
G_TYPE_FROM_CLASS (class),
112
(GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
113
G_STRUCT_OFFSET (MidoriBookmarksDbClass, update_item),
116
g_cclosure_marshal_VOID__POINTER,
120
katze_array_class = KATZE_ARRAY_CLASS (class);
122
katze_array_class->add_item = _midori_bookmarks_db_add_item;
123
katze_array_class->remove_item = _midori_bookmarks_db_remove_item;
124
katze_array_class->move_item = _midori_bookmarks_db_move_item;
125
katze_array_class->clear = _midori_bookmarks_db_clear;
127
class->update_item = _midori_bookmarks_db_update_item;
131
midori_bookmarks_db_init (MidoriBookmarksDb* bookmarks)
133
bookmarks->db = NULL;
135
katze_item_set_meta_integer (KATZE_ITEM (bookmarks), "id", 0);
136
katze_item_set_name (KATZE_ITEM (bookmarks), _("Bookmarks"));
137
/* g_object_ref (bookmarks); */
141
midori_bookmarks_db_finalize (GObject* object)
143
MidoriBookmarksDb* bookmarks = MIDORI_BOOKMARKS_DB (object);
147
sqlite3_close (bookmarks->db);
150
G_OBJECT_CLASS (midori_bookmarks_db_parent_class)->finalize (object);
154
* midori_bookmarks_db_get_item_parent:
155
* @bookmarks: the main bookmarks array
156
* @item: a #KatzeItem
158
* Internal function that find the parent of the @item thanks to its %parentid
161
midori_bookmarks_db_get_item_parent (MidoriBookmarksDb* bookmarks,
167
parentid = katze_item_get_meta_integer (KATZE_ITEM (item), "parentid");
171
parent = KATZE_ARRAY (bookmarks);
182
* _midori_bookmarks_db_add_item:
183
* @array: the main bookmarks array
184
* @item: a #KatzeItem
186
* Internal function that overloads the #KatzeArray %katze_array_add_item().
187
* It relays the add item to the appropriate #KatzeArray.
190
_midori_bookmarks_db_add_item (KatzeArray* array,
193
MidoriBookmarksDb *bookmarks;
195
KatzeArray* db_parent;
197
g_return_if_fail (IS_MIDORI_BOOKMARKS_DB (array));
198
g_return_if_fail (KATZE_IS_ITEM (item));
200
bookmarks = MIDORI_BOOKMARKS_DB (array);
202
parent = katze_item_get_parent (KATZE_ITEM (item));
204
db_parent = midori_bookmarks_db_get_item_parent (bookmarks, item);
206
g_return_if_fail (db_parent);
208
if (parent == db_parent)
210
if (IS_MIDORI_BOOKMARKS_DB (parent))
211
KATZE_ARRAY_CLASS (midori_bookmarks_db_parent_class)->update (parent);
213
katze_array_update (parent);
217
if (IS_MIDORI_BOOKMARKS_DB (parent))
218
KATZE_ARRAY_CLASS (midori_bookmarks_db_parent_class)->add_item (parent, item);
220
katze_array_add_item (parent, item);
224
* _midori_bookmarks_db_update_item:
225
* @array: the main bookmarks array
226
* @item: a #KatzeItem
228
* Internal function that implements the %midori_bookmarks_db_update_item() post-processing.
229
* It relays an update to the appropriate #KatzeArray.
232
_midori_bookmarks_db_update_item (MidoriBookmarksDb* bookmarks,
237
g_return_if_fail (IS_MIDORI_BOOKMARKS_DB (bookmarks));
238
g_return_if_fail (KATZE_IS_ITEM (item));
240
parent = katze_item_get_parent (KATZE_ITEM (item));
242
g_return_if_fail (parent);
244
katze_array_update (parent);
248
* _midori_bookmarks_db_remove_item:
249
* @array: the main bookmarks array
250
* @item: a #KatzeItem
252
* Internal function that overloads the #KatzeArray %katze_array_remove_item().
253
* It relays the remove item to the appropriate #KatzeArray.
256
_midori_bookmarks_db_remove_item (KatzeArray* array,
261
g_return_if_fail (IS_MIDORI_BOOKMARKS_DB (array));
262
g_return_if_fail (KATZE_IS_ITEM (item));
264
parent = katze_item_get_parent (KATZE_ITEM (item));
266
g_return_if_fail (parent);
268
if (IS_MIDORI_BOOKMARKS_DB (parent))
269
KATZE_ARRAY_CLASS (midori_bookmarks_db_parent_class)->remove_item (parent, item);
271
katze_array_remove_item (parent, item);
275
* _midori_bookmarks_db_move_item:
276
* @array: the main bookmarks array
277
* @item: a #KatzeItem
278
* @position: the new @item position
280
* Internal function that overloads the #KatzeArray %katze_array_move_item().
281
* It relays the move @item to the appropriate #KatzeArray.
284
_midori_bookmarks_db_move_item (KatzeArray* array,
288
MidoriBookmarksDb *bookmarks;
291
g_return_if_fail (IS_MIDORI_BOOKMARKS_DB (array));
292
g_return_if_fail (KATZE_IS_ITEM (item));
294
parent = katze_item_get_parent (KATZE_ITEM (item));
296
g_return_if_fail (parent);
298
KATZE_ARRAY_CLASS (midori_bookmarks_db_parent_class)->move_item (parent, item, position);
302
* _midori_bookmarks_db_clear:
303
* @array: the main bookmarks array
305
* Internal function that overloads the #KatzeArray %katze_array_clear().
306
* It deletes the whole bookmarks data.
309
_midori_bookmarks_db_clear (KatzeArray* array)
311
g_return_if_fail (IS_MIDORI_BOOKMARKS_DB (array));
313
g_critical ("_midori_bookmarks_db_clear: not implemented\n");
317
* midori_bookmarks_db_signal_update_item:
318
* @array: a #KatzeArray
321
* Notify an update of the item of the array.
325
midori_bookmarks_db_signal_update_item (MidoriBookmarksDb* array,
328
g_return_if_fail (IS_MIDORI_BOOKMARKS_DB (array));
330
g_signal_emit (array, signals[UPDATE_ITEM], 0, item);
334
* midori_bookmarks_db_insert_item_db:
336
* @item: #KatzeItem the item to insert
338
* Internal function that does the actual SQL INSERT of the @item in @db.
343
midori_bookmarks_db_insert_item_db (sqlite3* db,
43
348
char* errmsg = NULL;
75
380
new_parentid = g_strdup_printf ("NULL");
77
382
sqlcmd = sqlite3_mprintf (
78
"INSERT INTO bookmarks (id, parentid, title, uri, desc, toolbar, app) "
79
"VALUES (%q, %q, '%q', '%q', '%q', %d, %d)",
82
katze_item_get_name (item),
83
katze_str_non_null (uri),
84
katze_str_non_null (desc),
85
katze_item_get_meta_boolean (item, "toolbar"),
86
katze_item_get_meta_boolean (item, "app"));
383
"INSERT INTO bookmarks (id, parentid, title, uri, desc, toolbar, app) "
384
"VALUES (%q, %q, '%q', '%q', '%q', %d, %d)",
387
katze_item_get_name (item),
388
katze_str_non_null (uri),
389
katze_str_non_null (desc),
390
katze_item_get_meta_boolean (item, "toolbar"),
391
katze_item_get_meta_boolean (item, "app"));
88
393
if (sqlite3_exec (db, sqlcmd, NULL, NULL, &errmsg) == SQLITE_OK)
484
* midori_bookmarks_db_remove_item_db:
486
* @item: #KatzeItem the item to delete
488
* Internal function that does the actual SQL DELETE of the @item in @db.
493
midori_bookmarks_db_remove_item_db (sqlite3* db,
498
gboolean removed = TRUE;
501
id = g_strdup_printf ("%" G_GINT64_FORMAT,
502
katze_item_get_meta_integer (item, "id"));
504
sqlcmd = sqlite3_mprintf ("DELETE FROM bookmarks WHERE id = %q", id);
506
if (sqlite3_exec (db, sqlcmd, NULL, NULL, &errmsg) != SQLITE_OK)
508
g_printerr (_("Failed to remove bookmark item: %s\n"), errmsg);
509
sqlite3_free (errmsg);
513
sqlite3_free (sqlcmd);
519
* midori_bookmarks_db_add_item:
520
* @bookmarks: the main bookmark array
521
* @item: #KatzeItem the item to update
523
* Adds the @item in the bookmark data base.
528
midori_bookmarks_db_add_item (MidoriBookmarksDb* bookmarks, KatzeItem* item)
530
g_return_if_fail (IS_MIDORI_BOOKMARKS_DB (bookmarks));
531
g_return_if_fail (KATZE_IS_ITEM (item));
532
g_return_if_fail (NULL == katze_item_get_meta_string (item, "id"));
534
midori_bookmarks_db_insert_item_db (bookmarks->db, item,
535
katze_item_get_meta_integer (item, "parentid"));
537
katze_array_add_item (KATZE_ARRAY (bookmarks), item);
170
541
* midori_bookmarks_db_update_item:
171
542
* @bookmarks: the main bookmark array
172
543
* @item: #KatzeItem the item to update
174
545
* Updates the @item in the bookmark data base.
179
midori_array_update_item (KatzeArray* bookmarks,
182
g_return_if_fail (KATZE_IS_ARRAY (bookmarks));
183
g_return_if_fail (KATZE_IS_ITEM (item));
184
g_return_if_fail (katze_item_get_meta_string (item, "id"));
185
g_return_if_fail (0 != katze_item_get_meta_integer (item, "id"));
187
sqlite3* db = g_object_get_data (G_OBJECT (bookmarks), "db");
189
g_return_if_fail (db);
191
midori_bookmarks_update_item_db (db, item);
195
midori_bookmarks_dbtracer (void* dummy,
198
g_printerr ("%s\n", query);
202
midori_bookmarks_add_item_cb (KatzeArray* array,
206
midori_bookmarks_insert_item_db (db, item,
207
katze_item_get_meta_integer (item, "parentid"));
211
midori_bookmarks_remove_item_cb (KatzeArray* array,
219
id = g_strdup_printf ("%" G_GINT64_FORMAT,
220
katze_item_get_meta_integer (item, "id"));
222
sqlcmd = sqlite3_mprintf ("DELETE FROM bookmarks WHERE id = %q", id);
224
if (sqlite3_exec (db, sqlcmd, NULL, NULL, &errmsg) != SQLITE_OK)
226
g_printerr (_("Failed to remove bookmark item: %s\n"), errmsg);
227
sqlite3_free (errmsg);
230
sqlite3_free (sqlcmd);
550
midori_bookmarks_db_update_item (MidoriBookmarksDb* bookmarks, KatzeItem* item)
552
g_return_if_fail (IS_MIDORI_BOOKMARKS_DB (bookmarks));
553
g_return_if_fail (KATZE_IS_ITEM (item));
554
g_return_if_fail (katze_item_get_meta_string (item, "id"));
555
g_return_if_fail (0 != katze_item_get_meta_integer (item, "id"));
557
midori_bookmarks_db_update_item_db (bookmarks->db, item);
559
midori_bookmarks_db_signal_update_item (bookmarks, item);
563
* midori_bookmarks_db_remove_item:
564
* @bookmarks: the main bookmark array
565
* @item: #KatzeItem the item to remove
567
* Removes the @item from the bookmark data base.
572
midori_bookmarks_db_remove_item (MidoriBookmarksDb* bookmarks, KatzeItem* item)
574
g_return_if_fail (IS_MIDORI_BOOKMARKS_DB (bookmarks));
575
g_return_if_fail (KATZE_IS_ITEM (item));
576
g_return_if_fail (katze_item_get_meta_string (item, "id"));
577
g_return_if_fail (0 != katze_item_get_meta_integer (item, "id"));
579
midori_bookmarks_db_remove_item_db (bookmarks->db, item);
581
katze_array_remove_item (KATZE_ARRAY (bookmarks), item);
234
584
#define _APPEND_TO_SQL_ERRORMSG(custom_errmsg) \
321
688
if (sqlite3_open (newfile, &db) != SQLITE_OK)
323
690
*errmsg = g_strdup_printf (_("Failed to open database: %s\n"),
324
db ? sqlite3_errmsg (db) : "(db = NULL)");
691
db ? sqlite3_errmsg (db) : "(db = NULL)");
325
692
goto init_failed;
328
695
if (midori_debug ("bookmarks"))
329
sqlite3_trace (db, midori_bookmarks_dbtracer, NULL);
696
sqlite3_trace (db, midori_bookmarks_db_dbtracer, NULL);
331
698
create_stmt = /* Table structure */
332
"CREATE TABLE IF NOT EXISTS bookmarks "
333
"(id INTEGER PRIMARY KEY AUTOINCREMENT, "
334
"parentid INTEGER DEFAULT NULL, "
335
"title TEXT, uri TEXT, desc TEXT, app INTEGER, toolbar INTEGER, "
336
"pos_panel INTEGER, pos_bar INTEGER, "
337
"created DATE DEFAULT CURRENT_TIMESTAMP, "
338
"last_visit DATE, visit_count INTEGER DEFAULT 0, "
340
"FOREIGN KEY(parentid) REFERENCES bookmarks(id) "
341
"ON DELETE CASCADE); PRAGMA foreign_keys = ON;"
343
/* trigger: insert panel position */
344
"CREATE TRIGGER IF NOT EXISTS bookmarkInsertPosPanel "
345
"AFTER INSERT ON bookmarks FOR EACH ROW "
346
"BEGIN UPDATE bookmarks SET pos_panel = ("
347
"SELECT ifnull(MAX(pos_panel),0)+1 FROM bookmarks WHERE "
348
"(NEW.parentid IS NOT NULL AND parentid = NEW.parentid) "
349
"OR (NEW.parentid IS NULL AND parentid IS NULL)) "
350
"WHERE id = NEW.id; END;"
352
/* trigger: insert Bookmarkbar position */
353
"CREATE TRIGGER IF NOT EXISTS bookmarkInsertPosBar "
354
"AFTER INSERT ON bookmarks FOR EACH ROW WHEN NEW.toolbar=1 "
355
"BEGIN UPDATE bookmarks SET pos_bar = ("
356
"SELECT ifnull(MAX(pos_bar),0)+1 FROM bookmarks WHERE "
357
"((NEW.parentid IS NOT NULL AND parentid = NEW.parentid) "
358
"OR (NEW.parentid IS NULL AND parentid IS NULL)) AND toolbar=1) "
359
"WHERE id = NEW.id; END;"
361
/* trigger: update panel position */
362
"CREATE TRIGGER IF NOT EXISTS bookmarkUpdatePosPanel "
363
"BEFORE UPDATE OF parentid ON bookmarks FOR EACH ROW "
364
"WHEN ((NEW.parentid IS NULL OR OLD.parentid IS NULL) "
365
"AND NEW.parentid IS NOT OLD.parentid) OR "
366
"((NEW.parentid IS NOT NULL AND OLD.parentid IS NOT NULL) "
367
"AND NEW.parentid!=OLD.parentid) "
368
"BEGIN UPDATE bookmarks SET pos_panel = pos_panel-1 "
369
"WHERE ((OLD.parentid IS NOT NULL AND parentid = OLD.parentid) "
370
"OR (OLD.parentid IS NULL AND parentid IS NULL)) AND pos_panel > OLD.pos_panel; "
371
"UPDATE bookmarks SET pos_panel = ("
372
"SELECT ifnull(MAX(pos_panel),0)+1 FROM bookmarks "
373
"WHERE (NEW.parentid IS NOT NULL AND parentid = NEW.parentid) "
374
"OR (NEW.parentid IS NULL AND parentid IS NULL)) "
375
"WHERE id = OLD.id; END;"
377
/* trigger: update Bookmarkbar position */
378
"CREATE TRIGGER IF NOT EXISTS bookmarkUpdatePosBar0 "
379
"AFTER UPDATE OF parentid, toolbar ON bookmarks FOR EACH ROW "
380
"WHEN ((NEW.parentid IS NULL OR OLD.parentid IS NULL) "
381
"AND NEW.parentid IS NOT OLD.parentid) "
382
"OR ((NEW.parentid IS NOT NULL AND OLD.parentid IS NOT NULL) "
383
"AND NEW.parentid!=OLD.parentid) OR (OLD.toolbar=1 AND NEW.toolbar=0) "
384
"BEGIN UPDATE bookmarks SET pos_bar = NULL WHERE id = NEW.id; "
385
"UPDATE bookmarks SET pos_bar = pos_bar-1 "
386
"WHERE ((OLD.parentid IS NOT NULL AND parentid = OLD.parentid) "
387
"OR (OLD.parentid IS NULL AND parentid IS NULL)) AND pos_bar > OLD.pos_bar; END;"
389
/* trigger: update Bookmarkbar position */
390
"CREATE TRIGGER IF NOT EXISTS bookmarkUpdatePosBar1 "
391
"BEFORE UPDATE OF parentid, toolbar ON bookmarks FOR EACH ROW "
392
"WHEN ((NEW.parentid IS NULL OR OLD.parentid IS NULL) "
393
"AND NEW.parentid IS NOT OLD.parentid) OR "
394
"((NEW.parentid IS NOT NULL AND OLD.parentid IS NOT NULL) "
395
"AND NEW.parentid!=OLD.parentid) OR (OLD.toolbar=0 AND NEW.toolbar=1) "
396
"BEGIN UPDATE bookmarks SET pos_bar = ("
397
"SELECT ifnull(MAX(pos_bar),0)+1 FROM bookmarks WHERE "
398
"(NEW.parentid IS NOT NULL AND parentid = NEW.parentid) "
399
"OR (NEW.parentid IS NULL AND parentid IS NULL)) "
400
"WHERE id = OLD.id; END;"
402
/* trigger: delete panel position */
403
"CREATE TRIGGER IF NOT EXISTS bookmarkDeletePosPanel "
404
"AFTER DELETE ON bookmarks FOR EACH ROW "
405
"BEGIN UPDATE bookmarks SET pos_panel = pos_panel-1 "
406
"WHERE ((OLD.parentid IS NOT NULL AND parentid = OLD.parentid) "
407
"OR (OLD.parentid IS NULL AND parentid IS NULL)) AND pos_panel > OLD.pos_panel; END;"
409
/* trigger: delete Bookmarkbar position */
410
"CREATE TRIGGER IF NOT EXISTS bookmarkDeletePosBar "
411
"AFTER DELETE ON bookmarks FOR EACH ROW WHEN OLD.toolbar=1 "
412
"BEGIN UPDATE bookmarks SET pos_bar = pos_bar-1 "
413
"WHERE ((OLD.parentid IS NOT NULL AND parentid = OLD.parentid) "
414
"OR (OLD.parentid IS NULL AND parentid IS NULL)) AND pos_bar > OLD.pos_bar; END;";
699
"CREATE TABLE IF NOT EXISTS bookmarks "
700
"(id INTEGER PRIMARY KEY AUTOINCREMENT, "
701
"parentid INTEGER DEFAULT NULL, "
702
"title TEXT, uri TEXT, desc TEXT, app INTEGER, toolbar INTEGER, "
703
"pos_panel INTEGER, pos_bar INTEGER, "
704
"created DATE DEFAULT CURRENT_TIMESTAMP, "
705
"last_visit DATE, visit_count INTEGER DEFAULT 0, "
707
"FOREIGN KEY(parentid) REFERENCES bookmarks(id) "
708
"ON DELETE CASCADE); PRAGMA foreign_keys = ON;"
710
/* trigger: insert panel position */
711
"CREATE TRIGGER IF NOT EXISTS bookmarkInsertPosPanel "
712
"AFTER INSERT ON bookmarks FOR EACH ROW "
713
"BEGIN UPDATE bookmarks SET pos_panel = ("
714
"SELECT ifnull(MAX(pos_panel),0)+1 FROM bookmarks WHERE "
715
"(NEW.parentid IS NOT NULL AND parentid = NEW.parentid) "
716
"OR (NEW.parentid IS NULL AND parentid IS NULL)) "
717
"WHERE id = NEW.id; END;"
719
/* trigger: insert Bookmarkbar position */
720
"CREATE TRIGGER IF NOT EXISTS bookmarkInsertPosBar "
721
"AFTER INSERT ON bookmarks FOR EACH ROW WHEN NEW.toolbar=1 "
722
"BEGIN UPDATE bookmarks SET pos_bar = ("
723
"SELECT ifnull(MAX(pos_bar),0)+1 FROM bookmarks WHERE "
724
"((NEW.parentid IS NOT NULL AND parentid = NEW.parentid) "
725
"OR (NEW.parentid IS NULL AND parentid IS NULL)) AND toolbar=1) "
726
"WHERE id = NEW.id; END;"
728
/* trigger: update panel position */
729
"CREATE TRIGGER IF NOT EXISTS bookmarkUpdatePosPanel "
730
"BEFORE UPDATE OF parentid ON bookmarks FOR EACH ROW "
731
"WHEN ((NEW.parentid IS NULL OR OLD.parentid IS NULL) "
732
"AND NEW.parentid IS NOT OLD.parentid) OR "
733
"((NEW.parentid IS NOT NULL AND OLD.parentid IS NOT NULL) "
734
"AND NEW.parentid!=OLD.parentid) "
735
"BEGIN UPDATE bookmarks SET pos_panel = pos_panel-1 "
736
"WHERE ((OLD.parentid IS NOT NULL AND parentid = OLD.parentid) "
737
"OR (OLD.parentid IS NULL AND parentid IS NULL)) AND pos_panel > OLD.pos_panel; "
738
"UPDATE bookmarks SET pos_panel = ("
739
"SELECT ifnull(MAX(pos_panel),0)+1 FROM bookmarks "
740
"WHERE (NEW.parentid IS NOT NULL AND parentid = NEW.parentid) "
741
"OR (NEW.parentid IS NULL AND parentid IS NULL)) "
742
"WHERE id = OLD.id; END;"
744
/* trigger: update Bookmarkbar position */
745
"CREATE TRIGGER IF NOT EXISTS bookmarkUpdatePosBar0 "
746
"AFTER UPDATE OF parentid, toolbar ON bookmarks FOR EACH ROW "
747
"WHEN ((NEW.parentid IS NULL OR OLD.parentid IS NULL) "
748
"AND NEW.parentid IS NOT OLD.parentid) "
749
"OR ((NEW.parentid IS NOT NULL AND OLD.parentid IS NOT NULL) "
750
"AND NEW.parentid!=OLD.parentid) OR (OLD.toolbar=1 AND NEW.toolbar=0) "
751
"BEGIN UPDATE bookmarks SET pos_bar = NULL WHERE id = NEW.id; "
752
"UPDATE bookmarks SET pos_bar = pos_bar-1 "
753
"WHERE ((OLD.parentid IS NOT NULL AND parentid = OLD.parentid) "
754
"OR (OLD.parentid IS NULL AND parentid IS NULL)) AND pos_bar > OLD.pos_bar; END;"
756
/* trigger: update Bookmarkbar position */
757
"CREATE TRIGGER IF NOT EXISTS bookmarkUpdatePosBar1 "
758
"BEFORE UPDATE OF parentid, toolbar ON bookmarks FOR EACH ROW "
759
"WHEN ((NEW.parentid IS NULL OR OLD.parentid IS NULL) "
760
"AND NEW.parentid IS NOT OLD.parentid) OR "
761
"((NEW.parentid IS NOT NULL AND OLD.parentid IS NOT NULL) "
762
"AND NEW.parentid!=OLD.parentid) OR (OLD.toolbar=0 AND NEW.toolbar=1) "
763
"BEGIN UPDATE bookmarks SET pos_bar = ("
764
"SELECT ifnull(MAX(pos_bar),0)+1 FROM bookmarks WHERE "
765
"(NEW.parentid IS NOT NULL AND parentid = NEW.parentid) "
766
"OR (NEW.parentid IS NULL AND parentid IS NULL)) "
767
"WHERE id = OLD.id; END;"
769
/* trigger: delete panel position */
770
"CREATE TRIGGER IF NOT EXISTS bookmarkDeletePosPanel "
771
"AFTER DELETE ON bookmarks FOR EACH ROW "
772
"BEGIN UPDATE bookmarks SET pos_panel = pos_panel-1 "
773
"WHERE ((OLD.parentid IS NOT NULL AND parentid = OLD.parentid) "
774
"OR (OLD.parentid IS NULL AND parentid IS NULL)) AND pos_panel > OLD.pos_panel; END;"
776
/* trigger: delete Bookmarkbar position */
777
"CREATE TRIGGER IF NOT EXISTS bookmarkDeletePosBar "
778
"AFTER DELETE ON bookmarks FOR EACH ROW WHEN OLD.toolbar=1 "
779
"BEGIN UPDATE bookmarks SET pos_bar = pos_bar-1 "
780
"WHERE ((OLD.parentid IS NOT NULL AND parentid = OLD.parentid) "
781
"OR (OLD.parentid IS NULL AND parentid IS NULL)) AND pos_bar > OLD.pos_bar; END;";
417
784
if (newfile_did_exist)
448
815
if (oldfile_exists)
449
816
/* import from old db */
450
if (!midori_bookmarks_import_from_old_db (db, oldfile, &import_errmsg))
817
if (!midori_bookmarks_db_import_from_old_db (db, oldfile, &import_errmsg))
452
819
*errmsg = g_strdup_printf (_("Couldn't import from old database: %s\n"),
453
import_errmsg ? import_errmsg : "(err = NULL)");
820
import_errmsg ? import_errmsg : "(err = NULL)");
454
821
g_free (import_errmsg);
460
array = katze_array_new (KATZE_TYPE_ARRAY);
461
g_signal_connect (array, "add-item",
462
G_CALLBACK (midori_bookmarks_add_item_cb), db);
463
g_signal_connect (array, "remove-item",
464
G_CALLBACK (midori_bookmarks_remove_item_cb), db);
465
g_object_set_data (G_OBJECT (array), "db", db);
827
bookmarks = MIDORI_BOOKMARKS_DB (g_object_new (TYPE_MIDORI_BOOKMARKS_DB, NULL));
830
g_object_set_data (G_OBJECT (bookmarks), "db", db);
844
* midori_bookmarks_db_on_quit:
845
* @bookmarks: the main bookmark array
847
* Delete the main bookmark array.
479
midori_bookmarks_on_quit (KatzeArray* array)
852
midori_bookmarks_db_on_quit (MidoriBookmarksDb* bookmarks)
481
g_return_if_fail (KATZE_IS_ARRAY (array));
854
g_return_if_fail (IS_MIDORI_BOOKMARKS_DB (bookmarks));
483
sqlite3* db = g_object_get_data (G_OBJECT (array), "db");
484
g_return_if_fail (db != NULL);
856
g_object_unref (bookmarks);
860
* midori_bookmarks_db_import_array:
861
* @array: the main bookmark array
862
* @array: #KatzeArray containing the items to import
863
* @parentid: the id of folder
865
* Imports the items of @array as childs of the folder
866
* identfied by @parentid.
489
midori_bookmarks_import_array (KatzeArray* bookmarks,
871
midori_bookmarks_db_import_array (MidoriBookmarksDb* bookmarks,
490
872
KatzeArray* array,
878
g_return_if_fail (IS_MIDORI_BOOKMARKS_DB (bookmarks));
879
g_return_if_fail (KATZE_IS_ARRAY (array));
499
881
KATZE_ARRAY_FOREACH_ITEM_L (item, array, list)
501
883
katze_item_set_meta_integer (item, "parentid", parentid);
502
katze_array_add_item (bookmarks, item);
884
midori_bookmarks_db_add_item (bookmarks, item);
503
885
if (KATZE_IS_ARRAY (item))
504
midori_bookmarks_import_array (bookmarks, KATZE_ARRAY (item),
505
katze_item_get_meta_integer(item, "id"));
886
midori_bookmarks_db_import_array (bookmarks, KATZE_ARRAY (item),
887
katze_item_get_meta_integer(item, "id"));
507
889
g_list_free (list);
511
* midori_array_query_recursive:
893
* midori_bookmarks_db_array_from_statement:
894
* @stmt: the sqlite returned statement
895
* @bookmarks: the database controller
897
* Internal function that populate a #KatzeArray by processing the @stmt
899
* a- if the item is already in memory
900
* in this case the item data is updated with retreived database content
901
* and the already existing item is populated in the returned #KatzeArray
902
* b- if the data is a folder
903
* a new #KatzeArray item is populated in the returned #KatzeArray and
904
* memorized for future use.
905
* c- if the data is a bookmark
906
* a new #KatzeItem item is populated in the returned #KatzeArray and
907
* memorized for furure use.
909
* Return value: the populated #KatzeArray
912
midori_bookmarks_db_array_from_statement (sqlite3_stmt* stmt,
913
MidoriBookmarksDb* bookmarks)
919
array = katze_array_new (KATZE_TYPE_ITEM);
920
cols = sqlite3_column_count (stmt);
922
while ((result = sqlite3_step (stmt)) == SQLITE_ROW)
928
item = katze_item_new ();
929
for (i = 0; i < cols; i++)
930
katze_item_set_value_from_column (stmt, i, item);
932
if (KATZE_ITEM_IS_FOLDER (item))
934
g_object_unref (item);
936
item = KATZE_ITEM (katze_array_new (KATZE_TYPE_ITEM));
938
for (i = 0; i < cols; i++)
939
katze_item_set_value_from_column (stmt, i, item);
942
katze_array_add_item (array, item);
945
sqlite3_clear_bindings (stmt);
946
sqlite3_reset (stmt);
951
* midori_bookmarks_db_array_from_sqlite:
952
* @array: the main bookmark array
953
* @sqlcmd: the sqlcmd to execute
955
* Internal function that process the requested @sqlcmd.
957
* Return value: a #KatzeArray on success, %NULL otherwise
960
midori_bookmarks_db_array_from_sqlite (MidoriBookmarksDb* bookmarks,
966
g_return_val_if_fail (bookmarks->db != NULL, NULL);
968
result = sqlite3_prepare_v2 (bookmarks->db, sqlcmd, -1, &stmt, NULL);
969
if (result != SQLITE_OK)
972
return midori_bookmarks_db_array_from_statement (stmt, bookmarks);
976
* midori_bookmarks_db_query_recursive:
512
977
* @array: the main bookmark array
513
978
* @fields: comma separated list of fields
514
979
* @condition: condition, like "folder = '%q'"
656
sqlcmd_value = sqlite3_mprintf (
657
"SELECT id FROM bookmarks "
658
"WHERE parentid = %" G_GINT64_FORMAT " AND uri = ''", id);
1116
sqlcmd_value = sqlite3_mprintf (
1117
"SELECT id FROM bookmarks "
1118
"WHERE parentid = %" G_GINT64_FORMAT " AND uri = ''", id);
660
sqlcmd_value = sqlite3_mprintf (
661
"SELECT id FROM bookmarks "
662
"WHERE parentid IS NULL AND uri = ''");
1120
sqlcmd_value = sqlite3_mprintf (
1121
"SELECT id FROM bookmarks "
1122
"WHERE parentid IS NULL AND uri = ''");
664
if (sqlite3_prepare_v2 (db, sqlcmd_value, -1, &stmt, NULL) == SQLITE_OK)
1124
if (sqlite3_prepare_v2 (bookmarks->db, sqlcmd_value, -1, &stmt, NULL) == SQLITE_OK)
666
g_assert (sqlite3_column_count (stmt) == 1);
668
if ((result = sqlite3_step (stmt)) == SQLITE_ROW)
670
gint64* pid = g_new (gint64, 1);
672
*pid = sqlite3_column_int64(stmt, 0);
673
ids = g_list_append (ids, pid);
676
sqlite3_clear_bindings (stmt);
677
sqlite3_reset (stmt);
1126
g_assert (sqlite3_column_count (stmt) == 1);
1128
if ((result = sqlite3_step (stmt)) == SQLITE_ROW)
1130
gint64* pid = g_new (gint64, 1);
1132
*pid = sqlite3_column_int64(stmt, 0);
1133
ids = g_list_append (ids, pid);
1136
sqlite3_clear_bindings (stmt);
1137
sqlite3_reset (stmt);
680
1140
sqlite3_free (sqlcmd_value);