~midori/midori/trunk

« back to all changes in this revision

Viewing changes to panels/midori-bookmarks.c

  • Committer: André Auzi
  • Date: 2013-08-05 12:50:34 UTC
  • mto: This revision was merged to the branch mainline in revision 6388.
  • Revision ID: aauzi@free.fr-20130805125034-tyywj27asg4egh3b
prepare to KatzeItem derivation for update-item signals
as a result, all db operations are centralized in midori-bookmarks-db 

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
#include "midori-platform.h"
18
18
#include "midori-view.h"
19
19
#include "midori-core.h"
 
20
#include "midori-bookmarks-db.h"
20
21
 
21
22
#include <glib/gi18n.h>
22
23
#include <string.h>
137
138
    gchar* parent_id;
138
139
 
139
140
    parent_id = g_strdup_printf ("%" G_GINT64_FORMAT, parentid);
140
 
    if (!(root_array = midori_array_query (array, "*", "parentid = %q", parent_id)))
 
141
    if (!(root_array = midori_array_query_recursive (array, "*", "parentid = %q", parent_id, FALSE)))
141
142
    {
142
143
        g_free (parent_id);
143
144
        return;
160
161
    g_list_free (list);
161
162
}
162
163
 
163
 
void
164
 
midori_bookmarks_import_array (KatzeArray* bookmarks,
165
 
                               KatzeArray* array,
166
 
                               gint64      parentid)
167
 
{
168
 
    GList* list;
169
 
    KatzeItem* item;
170
 
 
171
 
    if (!bookmarks)
172
 
        return;
173
 
 
174
 
    KATZE_ARRAY_FOREACH_ITEM_L (item, array, list)
175
 
    {
176
 
        Katze_item_set_meta_integer (item, "parentid", parentid);
177
 
        katze_array_add_item (bookmarks, item);
178
 
        if (KATZE_IS_ARRAY (item))
179
 
          midori_bookmarks_import_array (bookmarks, KATZE_ARRAY (item),
180
 
                                         katze_item_get_meta_integer(item, "id"));
181
 
    }
182
 
    g_list_free (list);
183
 
}
184
 
 
185
164
static KatzeArray*
186
165
midori_bookmarks_read_from_db (MidoriBookmarks* bookmarks,
187
166
                               gint64           parentid,
190
169
    KatzeArray* array;
191
170
 
192
171
    if (keyword && *keyword)
193
 
        array = midori_array_query (bookmarks->array,
194
 
           "id, parentid, title, uri, desc, app, toolbar, pos_panel, pos_bar", "title LIKE '%%%q%%'", keyword);
 
172
        array = midori_array_query_recursive  (bookmarks->array,
 
173
                                    "id, parentid, title, uri, desc, app, toolbar, pos_panel, pos_bar", "title LIKE '%%%q%%'", keyword, FALSE);
195
174
    else
196
175
    {
197
176
        if (parentid > 0)
198
177
        {
199
178
            gchar* parent_id = g_strdup_printf ("%" G_GINT64_FORMAT, parentid);
200
 
            array = midori_array_query (bookmarks->array,
201
 
               "id, parentid, title, uri, desc, app, toolbar, pos_panel, pos_bar", "parentid = %q", parent_id);
 
179
            array = midori_array_query_recursive  (bookmarks->array,
 
180
                                        "id, parentid, title, uri, desc, app, toolbar, pos_panel, pos_bar", "parentid = %q", parent_id, FALSE);
202
181
 
203
182
            g_free (parent_id);
204
183
        }
205
184
        else
206
 
            array = midori_array_query (bookmarks->array,
207
 
               "id, parentid, title, uri, desc, app, toolbar, pos_panel, pos_bar", "parentid IS NULL", NULL);
 
185
            array = midori_array_query_recursive (bookmarks->array,
 
186
                                                  "id, parentid, title, uri, desc, app, toolbar, pos_panel, pos_bar", "parentid IS NULL", NULL, FALSE);
208
187
    }
209
188
    return array ? array : katze_array_new (KATZE_TYPE_ITEM);
210
189
}
235
214
        g_object_unref (item);
236
215
}
237
216
 
238
 
gint64
239
 
midori_bookmarks_insert_item_db (sqlite3*   db,
240
 
                                 KatzeItem* item,
241
 
                                 gint64     parentid)
242
 
{
243
 
    gchar* sqlcmd;
244
 
    char* errmsg = NULL;
245
 
    KatzeItem* old_parent;
246
 
    gchar* new_parentid;
247
 
    gchar* id = NULL;
248
 
    const gchar* uri = NULL;
249
 
    const gchar* desc = NULL;
250
 
    gint64 seq = 0;
251
 
 
252
 
    /* Bookmarks must have a name, import may produce invalid items */
253
 
    g_return_val_if_fail (katze_item_get_name (item), seq);
254
 
 
255
 
    if (!db)
256
 
        return seq;
257
 
 
258
 
    if (katze_item_get_meta_integer (item, "id") > 0)
259
 
        id = g_strdup_printf ("%" G_GINT64_FORMAT, katze_item_get_meta_integer(item, "id"));
260
 
    else
261
 
        id = g_strdup_printf ("NULL");
262
 
 
263
 
    if (KATZE_ITEM_IS_BOOKMARK (item))
264
 
        uri = katze_item_get_uri (item);
265
 
 
266
 
    if (katze_item_get_text (item))
267
 
        desc = katze_item_get_text (item);
268
 
 
269
 
    /* Use folder, otherwise fallback to parent folder */
270
 
    old_parent = katze_item_get_parent (item);
271
 
    if (parentid > 0)
272
 
        new_parentid = g_strdup_printf ("%" G_GINT64_FORMAT, parentid);
273
 
    else if (old_parent && katze_item_get_meta_integer (old_parent, "id") > 0)
274
 
        new_parentid = g_strdup_printf ("%" G_GINT64_FORMAT, katze_item_get_meta_integer (old_parent, "id"));
275
 
    else
276
 
        new_parentid = g_strdup_printf ("NULL");
277
 
 
278
 
    sqlcmd = sqlite3_mprintf (
279
 
            "INSERT INTO bookmarks (id, parentid, title, uri, desc, toolbar, app) "
280
 
            "VALUES (%q, %q, '%q', '%q', '%q', %d, %d)",
281
 
            id,
282
 
            new_parentid,
283
 
            katze_item_get_name (item),
284
 
            katze_str_non_null (uri),
285
 
            katze_str_non_null (desc),
286
 
            katze_item_get_meta_boolean (item, "toolbar"),
287
 
            katze_item_get_meta_boolean (item, "app"));
288
 
 
289
 
    if (sqlite3_exec (db, sqlcmd, NULL, NULL, &errmsg) == SQLITE_OK)
290
 
    {
291
 
        /* Get insert id */
292
 
        if (g_str_equal (id, "NULL"))
293
 
        {
294
 
            KatzeArray* seq_array;
295
 
 
296
 
            sqlite3_free (sqlcmd);
297
 
            sqlcmd = sqlite3_mprintf (
298
 
                    "SELECT seq FROM sqlite_sequence WHERE name = 'bookmarks'");
299
 
 
300
 
            seq_array = katze_array_from_sqlite (db, sqlcmd);
301
 
            if (katze_array_get_nth_item (seq_array, 0))
302
 
            {
303
 
                KatzeItem* seq_item = katze_array_get_nth_item (seq_array, 0);
304
 
 
305
 
                seq = katze_item_get_meta_integer (seq_item, "seq");
306
 
                katze_item_set_meta_integer (item, "id", seq);
307
 
            }
308
 
            g_object_unref (seq_array);
309
 
        }
310
 
    }
311
 
    else
312
 
    {
313
 
        g_printerr (_("Failed to add bookmark item: %s\n"), errmsg);
314
 
        sqlite3_free (errmsg);
315
 
    }
316
 
 
317
 
    sqlite3_free (sqlcmd);
318
 
    g_free (new_parentid);
319
 
    g_free (id);
320
 
 
321
 
    return seq;
322
 
}
323
 
 
324
217
static void
325
218
midori_bookmarks_add_item_cb (KatzeArray*      array,
326
219
                              KatzeItem*       item,
590
483
    }
591
484
}
592
485
 
593
 
gboolean
594
 
midori_bookmarks_update_item_db (sqlite3*   db,
595
 
                                 KatzeItem* item)
596
 
{
597
 
    gchar* sqlcmd;
598
 
    char* errmsg = NULL;
599
 
    gchar* parentid;
600
 
    gboolean updated;
601
 
    gchar* id;
602
 
 
603
 
    id = g_strdup_printf ("%" G_GINT64_FORMAT,
604
 
            katze_item_get_meta_integer (item, "id"));
605
 
 
606
 
    if (katze_item_get_meta_integer (item, "parentid") > 0)
607
 
        parentid = g_strdup_printf ("%" G_GINT64_FORMAT,
608
 
                                    katze_item_get_meta_integer (item, "parentid"));
609
 
    else
610
 
        parentid = g_strdup_printf ("NULL");
611
 
 
612
 
    sqlcmd = sqlite3_mprintf (
613
 
            "UPDATE bookmarks SET "
614
 
            "parentid=%q, title='%q', uri='%q', desc='%q', toolbar=%d, app=%d "
615
 
            "WHERE id = %q ;",
616
 
            parentid,
617
 
            katze_item_get_name (item),
618
 
            katze_str_non_null (katze_item_get_uri (item)),
619
 
            katze_str_non_null (katze_item_get_meta_string (item, "desc")),
620
 
            katze_item_get_meta_boolean (item, "toolbar"),
621
 
            katze_item_get_meta_boolean (item, "app"),
622
 
            id);
623
 
 
624
 
    updated = TRUE;
625
 
    if (sqlite3_exec (db, sqlcmd, NULL, NULL, &errmsg) != SQLITE_OK)
626
 
    {
627
 
        updated = FALSE;
628
 
        g_printerr (_("Failed to update bookmark: %s\n"), errmsg);
629
 
        sqlite3_free (errmsg);
630
 
    }
631
 
 
632
 
    sqlite3_free (sqlcmd);
633
 
    g_free (parentid);
634
 
    g_free (id);
635
 
 
636
 
    return updated;
637
 
}
638
 
 
639
486
static void
640
487
midori_bookmarks_delete_clicked_cb (GtkWidget*       toolitem,
641
488
                                    MidoriBookmarks* bookmarks)