1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* ev-bookmarks.c
 *  this file is part of evince, a gnome document viewer
 *
 * Copyright (C) 2010 Carlos Garcia Campos  <carlosgc@gnome.org>
 *
 * Evince is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Evince is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "config.h"

#include <string.h>

#include "ev-bookmarks.h"

enum {
        PROP_0,
        PROP_METADATA
};

enum {
        CHANGED,
        N_SIGNALS
};

struct _EvBookmarks {
        GObject base;

        EvMetadata *metadata;
        GList *items;
};

struct _EvBookmarksClass {
        GObjectClass base_class;

        void (*changed) (EvBookmarks *bookmarks);
};

G_DEFINE_TYPE (EvBookmarks, ev_bookmarks, G_TYPE_OBJECT)<--- There is an unknown macro here somewhere. Configuration is required. If G_DEFINE_TYPE is a macro then please configure it.

static guint signals[N_SIGNALS];

static gint
ev_bookmark_compare (EvBookmark *a,
                     EvBookmark *b)
{
        if (a->page < b->page)
                return -1;
        if (a->page > b->page)
                return 1;
        return 0;
}

static void
ev_bookmark_free (EvBookmark *bm)
{
        if (G_UNLIKELY(!bm))
                return;

        g_free (bm->title);
        g_slice_free (EvBookmark, bm);
}

static void
ev_bookmarks_finalize (GObject *object)
{
        EvBookmarks *bookmarks = EV_BOOKMARKS (object);

        if (bookmarks->items) {
                g_list_free_full (bookmarks->items, (GDestroyNotify)ev_bookmark_free);
                bookmarks->items = NULL;
        }

        if (bookmarks->metadata) {
                g_object_unref (bookmarks->metadata);
                bookmarks->metadata = NULL;
        }

        G_OBJECT_CLASS (ev_bookmarks_parent_class)->finalize (object);
}

static void
ev_bookmarks_init (EvBookmarks *bookmarks)
{
}

static void
ev_bookmarks_set_property (GObject      *object,
                           guint         prop_id,
                           const GValue *value,
                           GParamSpec   *pspec)
{
        EvBookmarks *bookmarks = EV_BOOKMARKS (object);

        switch (prop_id) {
        case PROP_METADATA:
                bookmarks->metadata = (EvMetadata *)g_value_dup_object (value);
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        }
}

static void
ev_bookmarks_constructed (GObject *object)
{
        EvBookmarks *bookmarks = EV_BOOKMARKS (object);
        gchar       *bm_list_str;
        GVariant    *bm_list;
        GVariantIter iter;
        GVariant    *child;
        GError      *error = NULL;

        if (!ev_metadata_get_string (bookmarks->metadata, "bookmarks", &bm_list_str))
                return;

        if (!bm_list_str || bm_list_str[0] == '\0')
                return;

        bm_list = g_variant_parse ((const GVariantType *)"a(us)",
                                   bm_list_str, NULL, NULL,
                                   &error);
        if (!bm_list) {
                g_warning ("Error getting bookmarks: %s\n", error->message);
                g_error_free (error);

                return;
        }

        g_variant_iter_init (&iter, bm_list);
        while ((child = g_variant_iter_next_value (&iter))) {
                EvBookmark *bm = g_slice_new (EvBookmark);

                g_variant_get (child, "(us)", &bm->page, &bm->title);
                if (bm->title && strlen (bm->title) > 0)
                        bookmarks->items = g_list_prepend (bookmarks->items, bm);
                g_variant_unref (child);
        }
        g_variant_unref (bm_list);

        bookmarks->items = g_list_reverse (bookmarks->items);
}

static void
ev_bookmarks_class_init (EvBookmarksClass *klass)
{
        GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

        gobject_class->set_property = ev_bookmarks_set_property;
        gobject_class->finalize = ev_bookmarks_finalize;
        gobject_class->constructed = ev_bookmarks_constructed;

        g_object_class_install_property (gobject_class,
                                         PROP_METADATA,
                                         g_param_spec_object ("metadata",
                                                              "Metadata",
                                                              "The document metadata",
                                                              EV_TYPE_METADATA,
                                                              G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
        /* Signals */
        signals[CHANGED] =
                g_signal_new ("changed",
                              EV_TYPE_BOOKMARKS,
                              G_SIGNAL_RUN_LAST,
                              G_STRUCT_OFFSET (EvBookmarksClass, changed),
                              NULL, NULL,
                              g_cclosure_marshal_VOID__VOID,
                              G_TYPE_NONE, 0);
}

EvBookmarks *
ev_bookmarks_new (EvMetadata *metadata)
{
        g_return_val_if_fail (EV_IS_METADATA (metadata), NULL);

        return EV_BOOKMARKS (g_object_new (EV_TYPE_BOOKMARKS,
                                           "metadata", metadata, NULL));
}

static void
ev_bookmarks_save (EvBookmarks *bookmarks)
{
        GList          *l;
        GVariantBuilder builder;
        GVariant       *bm_list;
        gchar          *bm_list_str;

        if (!bookmarks->items) {
                ev_metadata_set_string (bookmarks->metadata, "bookmarks", "");
                return;
        }

        g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
        for (l = bookmarks->items; l; l = g_list_next (l)) {
                EvBookmark *bm = (EvBookmark *)l->data;

                g_variant_builder_add (&builder, "(u&s)", bm->page, bm->title);
        }
        bm_list = g_variant_builder_end (&builder);

        bm_list_str = g_variant_print (bm_list, FALSE);
        g_variant_unref (bm_list);
        ev_metadata_set_string (bookmarks->metadata, "bookmarks", bm_list_str);
        g_free (bm_list_str);
}

GList *
ev_bookmarks_get_bookmarks (EvBookmarks *bookmarks)
{
        g_return_val_if_fail (EV_IS_BOOKMARKS (bookmarks), NULL);

        return g_list_copy (bookmarks->items);
}

void
ev_bookmarks_add (EvBookmarks *bookmarks,
                  EvBookmark  *bookmark)
{
        EvBookmark *bm;

        g_return_if_fail (EV_IS_BOOKMARKS (bookmarks));
        g_return_if_fail (bookmark->title != NULL);

        if (g_list_find_custom (bookmarks->items, bookmark, (GCompareFunc)ev_bookmark_compare))
                return;

        bm = g_slice_new (EvBookmark);
        *bm = *bookmark;
        bookmarks->items = g_list_append (bookmarks->items, bm);
        g_signal_emit (bookmarks, signals[CHANGED], 0);
        ev_bookmarks_save (bookmarks);
}

void
ev_bookmarks_delete (EvBookmarks *bookmarks,
                     EvBookmark  *bookmark)
{
        GList *bm_link;

        g_return_if_fail (EV_IS_BOOKMARKS (bookmarks));

        bm_link = g_list_find_custom (bookmarks->items, bookmark, (GCompareFunc)ev_bookmark_compare);
        if (!bm_link)
                return;

        bookmarks->items = g_list_delete_link (bookmarks->items, bm_link);
        g_signal_emit (bookmarks, signals[CHANGED], 0);
        ev_bookmarks_save (bookmarks);
}

void
ev_bookmarks_update (EvBookmarks *bookmarks,
                     EvBookmark  *bookmark)
{
        GList      *bm_link;
        EvBookmark *bm;

        g_return_if_fail (EV_IS_BOOKMARKS (bookmarks));
        g_return_if_fail (bookmark->title != NULL);

        bm_link = g_list_find_custom (bookmarks->items, bookmark, (GCompareFunc)ev_bookmark_compare);
        if (!bm_link)
                return;

        bm = (EvBookmark *)bm_link->data;

        if (strcmp (bookmark->title, bm->title) == 0)
                return;

        g_free (bm->title);
        *bm = *bookmark;
        g_signal_emit (bookmarks, signals[CHANGED], 0);
        ev_bookmarks_save (bookmarks);
}