zathura
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
bookmarks.c
Go to the documentation of this file.
1 /* See LICENSE file for license and copyright information */
2 
3 #include <string.h>
4 #include "bookmarks.h"
5 #include "database.h"
6 #include "document.h"
7 #include "adjustment.h"
8 
9 #include <girara/datastructures.h>
10 #include <girara/utils.h>
11 #include <girara/session.h>
12 
13 static int
14 bookmark_compare_find(const void* item, const void* data)
15 {
16  const zathura_bookmark_t* bookmark = item;
17  const char* id = data;
18 
19  return g_strcmp0(bookmark->id, id);
20 }
21 
22 zathura_bookmark_t*
23 zathura_bookmark_add(zathura_t* zathura, const gchar* id, unsigned int page)
24 {
25  g_return_val_if_fail(zathura && zathura->document && zathura->bookmarks.bookmarks, NULL);
26  g_return_val_if_fail(id, NULL);
27 
28  double position_x = zathura_document_get_position_x(zathura->document);
29  double position_y = zathura_document_get_position_y(zathura->document);
30  zathura_bookmark_t* old = zathura_bookmark_get(zathura, id);
31 
32  if (old != NULL) {
33  old->page = page;
34  old->x = position_x;
35  old->y = position_y;
36 
37  if (zathura->database != NULL) {
38  const char* path = zathura_document_get_path(zathura->document);
39  if (zathura_db_remove_bookmark(zathura->database, path, old->id) == false) {
40  girara_warning("Failed to remove old bookmark from database.");
41  }
42 
43  if (zathura_db_add_bookmark(zathura->database, path, old) == false) {
44  girara_warning("Failed to add new bookmark to database.");
45  }
46  }
47 
48  return old;
49  }
50 
51  zathura_bookmark_t* bookmark = g_try_malloc0(sizeof(zathura_bookmark_t));
52  if (bookmark == NULL) {
53  return NULL;
54  }
55 
56  bookmark->id = g_strdup(id);
57  bookmark->page = page;
58  bookmark->x = position_x;
59  bookmark->y = position_y;
60  girara_list_append(zathura->bookmarks.bookmarks, bookmark);
61 
62  if (zathura->database != NULL) {
63  const char* path = zathura_document_get_path(zathura->document);
64  if (zathura_db_add_bookmark(zathura->database, path, bookmark) == false) {
65  girara_warning("Failed to add bookmark to database.");
66  }
67  }
68 
69  return bookmark;
70 }
71 
72 bool
73 zathura_bookmark_remove(zathura_t* zathura, const gchar* id)
74 {
75  g_return_val_if_fail(zathura && zathura->document && zathura->bookmarks.bookmarks, false);
76  g_return_val_if_fail(id, false);
77 
78  zathura_bookmark_t* bookmark = zathura_bookmark_get(zathura, id);
79  if (bookmark == NULL) {
80  return false;
81  }
82 
83  if (zathura->database != NULL) {
84  const char* path = zathura_document_get_path(zathura->document);
85  if (zathura_db_remove_bookmark(zathura->database, path, bookmark->id) == false) {
86  girara_warning("Failed to remove bookmark from database.");
87  }
88  }
89 
90  girara_list_remove(zathura->bookmarks.bookmarks, bookmark);
91 
92  return true;
93 }
94 
95 zathura_bookmark_t*
96 zathura_bookmark_get(zathura_t* zathura, const gchar* id)
97 {
98  g_return_val_if_fail(zathura && zathura->bookmarks.bookmarks, NULL);
99  g_return_val_if_fail(id, NULL);
100 
101  return girara_list_find(zathura->bookmarks.bookmarks, bookmark_compare_find, id);
102 }
103 
104 void
105 zathura_bookmark_free(zathura_bookmark_t* bookmark)
106 {
107  if (bookmark == NULL) {
108  return;
109  }
110 
111  g_free(bookmark->id);
112  g_free(bookmark);
113 }
114 
115 bool
116 zathura_bookmarks_load(zathura_t* zathura, const gchar* file)
117 {
118  g_return_val_if_fail(zathura, false);
119  g_return_val_if_fail(file, false);
120 
121  if (zathura->database == NULL) {
122  return false;
123  }
124 
125  girara_list_t* bookmarks = zathura_db_load_bookmarks(zathura->database, file);
126  if (bookmarks == NULL) {
127  return false;
128  }
129 
130  girara_list_free(zathura->bookmarks.bookmarks);
131  zathura->bookmarks.bookmarks = bookmarks;
132 
133  return true;
134 }
135 
136 int
137 zathura_bookmarks_compare(zathura_bookmark_t* lhs, zathura_bookmark_t* rhs)
138 {
139  if (lhs == NULL && rhs == NULL) {
140  return 0;
141  }
142 
143  if (lhs == NULL) {
144  return -1;
145  }
146 
147  if (rhs == NULL) {
148  return 1;
149  }
150 
151  return g_strcmp0(lhs->id, rhs->id);
152 }