bookmarks.c 3.71 KB
Newer Older
1 2 3 4
/* See LICENSE file for license and copyright information */

#include <string.h>
#include "bookmarks.h"
5
#include "database.h"
6
#include "document.h"
7
#include "adjustment.h"
8

9 10
#include <girara/datastructures.h>
#include <girara/utils.h>
11
#include <girara/session.h>
12

Sebastian Ramacher's avatar
Sebastian Ramacher committed
13 14 15 16 17 18 19 20 21
static int
bookmark_compare_find(const void* item, const void* data)
{
  const zathura_bookmark_t* bookmark = item;
  const char* id = data;

  return g_strcmp0(bookmark->id, id);
}

22 23 24
zathura_bookmark_t*
zathura_bookmark_add(zathura_t* zathura, const gchar* id, unsigned int page)
{
25
  g_return_val_if_fail(zathura && zathura->document && zathura->bookmarks.bookmarks, NULL);
26 27
  g_return_val_if_fail(id, NULL);

28 29
  double position_x = zathura_document_get_position_x(zathura->document);
  double position_y = zathura_document_get_position_y(zathura->document);
30 31
  zathura_bookmark_t* old = zathura_bookmark_get(zathura, id);

Sebastian Ramacher's avatar
Sebastian Ramacher committed
32
  if (old != NULL) {
33
    old->page = page;
34 35
    old->x = position_x;
    old->y = position_y;
36 37 38 39 40 41 42 43 44 45 46 47 48

    if (zathura->database != NULL) {
      const char* path = zathura_document_get_path(zathura->document);
      if (zathura_db_remove_bookmark(zathura->database, path, old->id) == false) {
        girara_warning("Failed to remove old bookmark from database.");
      }

      if (zathura_db_add_bookmark(zathura->database, path, old) == false) {
        girara_warning("Failed to add new bookmark to database.");
      }
    }

    return old;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
49
  }
50

51 52 53 54
  zathura_bookmark_t* bookmark = g_try_malloc0(sizeof(zathura_bookmark_t));
  if (bookmark == NULL) {
    return NULL;
  }
55

56 57
  bookmark->id = g_strdup(id);
  bookmark->page = page;
58 59
  bookmark->x = position_x;
  bookmark->y = position_y;
60
  girara_list_append(zathura->bookmarks.bookmarks, bookmark);
Moritz Lipp's avatar
Moritz Lipp committed
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 66 67
      girara_warning("Failed to add bookmark to database.");
    }
  }
68

69 70 71
  return bookmark;
}

72 73
bool
zathura_bookmark_remove(zathura_t* zathura, const gchar* id)
74
{
75 76
  g_return_val_if_fail(zathura && zathura->document && zathura->bookmarks.bookmarks, false);
  g_return_val_if_fail(id, false);
77 78

  zathura_bookmark_t* bookmark = zathura_bookmark_get(zathura, id);
Moritz Lipp's avatar
Moritz Lipp committed
79
  if (bookmark == NULL) {
80
    return false;
81
  }
82

Moritz Lipp's avatar
Moritz Lipp committed
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 87 88
      girara_warning("Failed to remove bookmark from database.");
    }
  }
Moritz Lipp's avatar
Moritz Lipp committed
89

90 91 92
  girara_list_remove(zathura->bookmarks.bookmarks, bookmark);

  return true;
93 94
}

95 96
zathura_bookmark_t*
zathura_bookmark_get(zathura_t* zathura, const gchar* id)
97 98 99 100
{
  g_return_val_if_fail(zathura && zathura->bookmarks.bookmarks, NULL);
  g_return_val_if_fail(id, NULL);

Sebastian Ramacher's avatar
Sebastian Ramacher committed
101
  return girara_list_find(zathura->bookmarks.bookmarks, bookmark_compare_find, id);
102 103
}

104 105
void
zathura_bookmark_free(zathura_bookmark_t* bookmark)
106
{
Moritz Lipp's avatar
Moritz Lipp committed
107
  if (bookmark == NULL) {
108 109 110 111 112 113 114
    return;
  }

  g_free(bookmark->id);
  g_free(bookmark);
}

115
bool
Moritz Lipp's avatar
Moritz Lipp committed
116 117
zathura_bookmarks_load(zathura_t* zathura, const gchar* file)
{
Sebastian Ramacher's avatar
Sebastian Ramacher committed
118
  g_return_val_if_fail(zathura, false);
119
  g_return_val_if_fail(file, false);
Moritz Lipp's avatar
Moritz Lipp committed
120

Sebastian Ramacher's avatar
Sebastian Ramacher committed
121 122 123
  if (zathura->database == NULL) {
    return false;
  }
124 125

  girara_list_t* bookmarks = zathura_db_load_bookmarks(zathura->database, file);
Moritz Lipp's avatar
Moritz Lipp committed
126
  if (bookmarks == NULL) {
127 128 129 130 131
    return false;
  }

  girara_list_free(zathura->bookmarks.bookmarks);
  zathura->bookmarks.bookmarks = bookmarks;
Moritz Lipp's avatar
Moritz Lipp committed
132

133 134 135
  return true;
}

136 137 138 139 140 141
int
zathura_bookmarks_compare(zathura_bookmark_t* lhs, zathura_bookmark_t* rhs)
{
  if (lhs == NULL && rhs == NULL) {
    return 0;
  }
Moritz Lipp's avatar
Moritz Lipp committed
142

143 144 145
  if (lhs == NULL) {
    return -1;
  }
Moritz Lipp's avatar
Moritz Lipp committed
146

147 148 149 150 151 152
  if (rhs == NULL) {
    return 1;
  }

  return g_strcmp0(lhs->id, rhs->id);
}