2 * this file is part of evince, a gnome document viewer
4 * Copyright (C) 2010 Carlos Garcia Campos <carlosgc@gnome.org>
6 * Evince is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Evince is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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 "ev-bookmarks.h"
44 struct _EvBookmarksClass {
45 GObjectClass base_class;
47 void (*changed) (EvBookmarks *bookmarks);
50 G_DEFINE_TYPE (EvBookmarks, ev_bookmarks, G_TYPE_OBJECT)
52 static guint signals[N_SIGNALS];
55 ev_bookmark_compare (EvBookmark *a,
58 if (a->page < b->page)
60 if (a->page > b->page)
66 ev_bookmark_free (EvBookmark *bm)
72 g_slice_free (EvBookmark, bm);
76 ev_bookmarks_finalize (GObject *object)
78 EvBookmarks *bookmarks = EV_BOOKMARKS (object);
80 if (bookmarks->items) {
81 g_list_free_full (bookmarks->items, (GDestroyNotify)ev_bookmark_free);
82 bookmarks->items = NULL;
85 if (bookmarks->metadata) {
86 g_object_unref (bookmarks->metadata);
87 bookmarks->metadata = NULL;
90 G_OBJECT_CLASS (ev_bookmarks_parent_class)->finalize (object);
94 ev_bookmarks_init (EvBookmarks *bookmarks)
99 ev_bookmarks_set_property (GObject *object,
104 EvBookmarks *bookmarks = EV_BOOKMARKS (object);
108 bookmarks->metadata = (EvMetadata *)g_value_dup_object (value);
111 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
116 ev_bookmarks_constructed (GObject *object)
118 EvBookmarks *bookmarks = EV_BOOKMARKS (object);
123 GError *error = NULL;
125 if (!ev_metadata_get_string (bookmarks->metadata, "bookmarks", &bm_list_str))
128 if (!bm_list_str || bm_list_str[0] == '\0')
131 bm_list = g_variant_parse ((const GVariantType *)"a(us)",
132 bm_list_str, NULL, NULL,
135 g_warning ("Error getting bookmarks: %s\n", error->message);
136 g_error_free (error);
141 g_variant_iter_init (&iter, bm_list);
142 while ((child = g_variant_iter_next_value (&iter))) {
143 EvBookmark *bm = g_slice_new (EvBookmark);
145 g_variant_get (child, "(us)", &bm->page, &bm->title);
146 if (bm->title && strlen (bm->title) > 0)
147 bookmarks->items = g_list_prepend (bookmarks->items, bm);
148 g_variant_unref (child);
150 g_variant_unref (bm_list);
152 bookmarks->items = g_list_reverse (bookmarks->items);
156 ev_bookmarks_class_init (EvBookmarksClass *klass)
158 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
160 gobject_class->set_property = ev_bookmarks_set_property;
161 gobject_class->finalize = ev_bookmarks_finalize;
162 gobject_class->constructed = ev_bookmarks_constructed;
164 g_object_class_install_property (gobject_class,
166 g_param_spec_object ("metadata",
168 "The document metadata",
170 G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
173 g_signal_new ("changed",
176 G_STRUCT_OFFSET (EvBookmarksClass, changed),
178 g_cclosure_marshal_VOID__VOID,
183 ev_bookmarks_new (EvMetadata *metadata)
185 g_return_val_if_fail (EV_IS_METADATA (metadata), NULL);
187 return EV_BOOKMARKS (g_object_new (EV_TYPE_BOOKMARKS,
188 "metadata", metadata, NULL));
192 ev_bookmarks_save (EvBookmarks *bookmarks)
195 GVariantBuilder builder;
199 if (!bookmarks->items) {
200 ev_metadata_set_string (bookmarks->metadata, "bookmarks", "");
204 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
205 for (l = bookmarks->items; l; l = g_list_next (l)) {
206 EvBookmark *bm = (EvBookmark *)l->data;
208 g_variant_builder_add (&builder, "(u&s)", bm->page, bm->title);
210 bm_list = g_variant_builder_end (&builder);
212 bm_list_str = g_variant_print (bm_list, FALSE);
213 g_variant_unref (bm_list);
214 ev_metadata_set_string (bookmarks->metadata, "bookmarks", bm_list_str);
215 g_free (bm_list_str);
219 ev_bookmarks_get_bookmarks (EvBookmarks *bookmarks)
221 g_return_val_if_fail (EV_IS_BOOKMARKS (bookmarks), NULL);
223 return g_list_copy (bookmarks->items);
227 ev_bookmarks_add (EvBookmarks *bookmarks,
228 EvBookmark *bookmark)
232 g_return_if_fail (EV_IS_BOOKMARKS (bookmarks));
233 g_return_if_fail (bookmark->title != NULL);
235 if (g_list_find_custom (bookmarks->items, bookmark, (GCompareFunc)ev_bookmark_compare))
238 bm = g_slice_new (EvBookmark);
240 bookmarks->items = g_list_append (bookmarks->items, bm);
241 g_signal_emit (bookmarks, signals[CHANGED], 0);
242 ev_bookmarks_save (bookmarks);
246 ev_bookmarks_delete (EvBookmarks *bookmarks,
247 EvBookmark *bookmark)
251 g_return_if_fail (EV_IS_BOOKMARKS (bookmarks));
253 bm_link = g_list_find_custom (bookmarks->items, bookmark, (GCompareFunc)ev_bookmark_compare);
257 bookmarks->items = g_list_delete_link (bookmarks->items, bm_link);
258 g_signal_emit (bookmarks, signals[CHANGED], 0);
259 ev_bookmarks_save (bookmarks);
263 ev_bookmarks_update (EvBookmarks *bookmarks,
264 EvBookmark *bookmark)
269 g_return_if_fail (EV_IS_BOOKMARKS (bookmarks));
270 g_return_if_fail (bookmark->title != NULL);
272 bm_link = g_list_find_custom (bookmarks->items, bookmark, (GCompareFunc)ev_bookmark_compare);
276 bm = (EvBookmark *)bm_link->data;
278 if (strcmp (bookmark->title, bm->title) == 0)
283 g_signal_emit (bookmarks, signals[CHANGED], 0);
284 ev_bookmarks_save (bookmarks);