1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8; -*-
3
* Copyright (C) 2010 Carlos Garcia Campos <carlosgc@gnome.org>
4
* Copyright (C) 2013 Red Hat, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2, or (at your option)
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25
#include "gd-bookmarks.h"
45
struct _GdBookmarksClass {
46
GObjectClass base_class;
48
void (*changed) (GdBookmarks *bookmarks);
51
G_DEFINE_TYPE (GdBookmarks, gd_bookmarks, G_TYPE_OBJECT)
53
static guint signals[N_SIGNALS];
56
gd_bookmarks_finalize (GObject *object)
58
GdBookmarks *self = GD_BOOKMARKS (object);
60
g_list_free_full (self->items, g_object_unref);
62
g_clear_object (&self->metadata);
64
G_OBJECT_CLASS (gd_bookmarks_parent_class)->finalize (object);
68
gd_bookmarks_init (GdBookmarks *bookmarks)
73
gd_bookmarks_get_n_items (GdBookmarks *bookmarks)
75
g_return_val_if_fail (GD_IS_BOOKMARKS (bookmarks), 0);
77
return g_list_length (bookmarks->items);
81
gd_bookmarks_get_property (GObject *object,
86
GdBookmarks *self = GD_BOOKMARKS (object);
90
g_value_set_uint (value, gd_bookmarks_get_n_items (self));
93
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
98
gd_bookmarks_set_property (GObject *object,
103
GdBookmarks *self = GD_BOOKMARKS (object);
107
self->metadata = (GdMetadata *)g_value_dup_object (value);
110
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
115
gd_bookmarks_constructed (GObject *object)
117
GdBookmarks *self = GD_BOOKMARKS (object);
118
const char *bm_list_str;
122
GError *error = NULL;
124
if (!gd_metadata_get_string (self->metadata, "bookmarks", &bm_list_str)) {
128
if (bm_list_str == NULL || bm_list_str[0] == '\0') {
132
bm_list = g_variant_parse ((const GVariantType *)"a(us)",
133
bm_list_str, NULL, NULL,
135
if (bm_list == NULL) {
136
g_warning ("Error getting bookmarks: %s\n", error->message);
137
g_error_free (error);
142
g_variant_iter_init (&iter, bm_list);
143
while ((child = g_variant_iter_next_value (&iter))) {
145
const char *title = NULL;
147
g_variant_get (child, "(u&s)", &page_num, &title);
149
GdBookmark *bm = gd_bookmark_new ();
150
gd_bookmark_set_title (bm, title);
151
gd_bookmark_set_page_number (bm, page_num);
152
self->items = g_list_prepend (self->items, bm);
153
g_object_notify (G_OBJECT (self), "n-items");
155
g_variant_unref (child);
157
g_variant_unref (bm_list);
159
self->items = g_list_reverse (self->items);
163
gd_bookmarks_class_init (GdBookmarksClass *klass)
165
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
167
gobject_class->get_property = gd_bookmarks_get_property;
168
gobject_class->set_property = gd_bookmarks_set_property;
169
gobject_class->finalize = gd_bookmarks_finalize;
170
gobject_class->constructed = gd_bookmarks_constructed;
172
g_object_class_install_property (gobject_class,
174
g_param_spec_object ("metadata",
176
"The document metadata",
178
G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE |
179
G_PARAM_STATIC_STRINGS));
180
g_object_class_install_property (gobject_class,
182
g_param_spec_uint ("n-items",
184
"Number of bookmark items",
189
G_PARAM_STATIC_STRINGS));
192
signals[CHANGED] = g_signal_new ("changed",
195
G_STRUCT_OFFSET (GdBookmarksClass, changed),
197
g_cclosure_marshal_VOID__VOID,
202
gd_bookmarks_new (GdMetadata *metadata)
204
g_return_val_if_fail (GD_IS_METADATA (metadata), NULL);
206
return GD_BOOKMARKS (g_object_new (GD_TYPE_BOOKMARKS,
207
"metadata", metadata,
212
gd_bookmarks_save (GdBookmarks *self)
215
GVariantBuilder builder;
219
if (self->items == NULL) {
220
gd_metadata_set_string (self->metadata, "bookmarks", "");
224
g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
225
for (l = self->items; l; l = g_list_next (l)) {
226
GdBookmark *bm = (GdBookmark *)l->data;
227
const char *title = gd_bookmark_get_title (bm);
228
guint page_num = gd_bookmark_get_page_number (bm);
230
g_variant_builder_add (&builder, "(u&s)",
232
title != NULL ? title : "");
234
bm_list = g_variant_builder_end (&builder);
236
bm_list_str = g_variant_print (bm_list, FALSE);
237
g_variant_unref (bm_list);
238
gd_metadata_set_string (self->metadata, "bookmarks", bm_list_str);
239
g_free (bm_list_str);
243
* gd_bookmarks_find_bookmark:
247
* Returns: (transfer none)
250
gd_bookmarks_find_bookmark (GdBookmarks *bookmarks,
251
GdBookmark *bookmark)
255
l = g_list_find_custom (bookmarks->items, bookmark, (GCompareFunc)gd_bookmark_compare);
263
* gd_bookmarks_get_bookmarks:
266
* Returns: (transfer container) (element-type GdBookmark): A list of #GdBookmark objects
269
gd_bookmarks_get_bookmarks (GdBookmarks *bookmarks)
271
g_return_val_if_fail (GD_IS_BOOKMARKS (bookmarks), NULL);
273
return g_list_copy (bookmarks->items);
277
gd_bookmarks_add (GdBookmarks *bookmarks,
278
GdBookmark *bookmark)
282
g_return_if_fail (GD_IS_BOOKMARKS (bookmarks));
284
bm = gd_bookmarks_find_bookmark (bookmarks, bookmark);
289
bookmarks->items = g_list_append (bookmarks->items, g_object_ref (bookmark));
290
g_object_notify (G_OBJECT (bookmarks), "n-items");
291
g_signal_emit (bookmarks, signals[CHANGED], 0);
292
gd_bookmarks_save (bookmarks);
296
gd_bookmarks_remove (GdBookmarks *bookmarks,
297
GdBookmark *bookmark)
301
g_return_if_fail (GD_IS_BOOKMARKS (bookmarks));
303
bm = gd_bookmarks_find_bookmark (bookmarks, bookmark);
308
bookmarks->items = g_list_remove (bookmarks->items, bm);
310
g_object_notify (G_OBJECT (bookmarks), "n-items");
311
g_signal_emit (bookmarks, signals[CHANGED], 0);
312
gd_bookmarks_save (bookmarks);
316
gd_bookmarks_update (GdBookmarks *bookmarks,
317
GdBookmark *bookmark)
324
g_return_if_fail (GD_IS_BOOKMARKS (bookmarks));
326
bm_link = g_list_find_custom (bookmarks->items, bookmark, (GCompareFunc)gd_bookmark_compare);
327
if (bm_link == NULL) {
331
bm = (GdBookmark *)bm_link->data;
333
title_a = gd_bookmark_get_title (bm);
334
title_b = gd_bookmark_get_title (bookmark);
336
if (g_strcmp0 (title_a, title_b) == 0) {
340
g_signal_emit (bookmarks, signals[CHANGED], 0);
341
gd_bookmarks_save (bookmarks);