implement cmd_bookmark_remove

This commit is contained in:
Sebastian Ramacher 2011-09-03 13:40:28 +02:00
parent 6f567a6264
commit 7b3d31a3e5
3 changed files with 64 additions and 21 deletions

View file

@ -3,11 +3,12 @@
#include <string.h>
#include "bookmarks.h"
#include "database.h"
#include "document.h"
zathura_bookmark_t*
zathura_bookmark_add(zathura_t* zathura, const gchar* id, unsigned int page)
{
g_return_val_if_fail(zathura && zathura->bookmarks.bookmarks, NULL);
g_return_val_if_fail(zathura && zathura->document && zathura->bookmarks.bookmarks, NULL);
g_return_val_if_fail(id, NULL);
GIRARA_LIST_FOREACH(zathura->bookmarks.bookmarks, zathura_bookmark_t*, iter, bookmark)
@ -21,22 +22,38 @@ zathura_bookmark_add(zathura_t* zathura, const gchar* id, unsigned int page)
bookmark->id = g_strdup(id);
bookmark->page = page;
girara_list_append(zathura->bookmarks.bookmarks, bookmark);
if (zathura->database) {
if (!zathura_db_add_bookmark(zathura->database, zathura->document->file_path, bookmark)) {
girara_warning("Failed to add bookmark to database.");
}
}
return bookmark;
}
void zathura_bookmark_remove(zathura_t* zathura, const gchar* id)
bool
zathura_bookmark_remove(zathura_t* zathura, const gchar* id)
{
g_return_if_fail(zathura && zathura->bookmarks.bookmarks);
g_return_if_fail(id);
g_return_val_if_fail(zathura && zathura->document && zathura->bookmarks.bookmarks, false);
g_return_val_if_fail(id, false);
zathura_bookmark_t* bookmark = zathura_bookmark_get(zathura, id);
if (bookmark) {
girara_list_remove(zathura->bookmarks.bookmarks, bookmark);
if (!bookmark) {
return false;
}
if (zathura->database) {
if (!zathura_db_remove_bookmark(zathura->database, zathura->document->file_path, bookmark->id)) {
girara_warning("Failed to remove bookmark from database.");
}
}
girara_list_remove(zathura->bookmarks.bookmarks, bookmark);
return true;
}
zathura_bookmark_t* zathura_bookmark_get(zathura_t* zathura, const gchar* id)
zathura_bookmark_t*
zathura_bookmark_get(zathura_t* zathura, const gchar* id)
{
g_return_val_if_fail(zathura && zathura->bookmarks.bookmarks, NULL);
g_return_val_if_fail(id, NULL);
@ -51,7 +68,8 @@ zathura_bookmark_t* zathura_bookmark_get(zathura_t* zathura, const gchar* id)
return NULL;
}
void zathura_bookmark_free(zathura_bookmark_t* bookmark)
void
zathura_bookmark_free(zathura_bookmark_t* bookmark)
{
if (!bookmark) {
return;

View file

@ -27,8 +27,9 @@ zathura_bookmark_t* zathura_bookmark_add(zathura_t* zathura, const gchar* id, un
* Remove a bookmark from the list of bookmarks.
* @param zathura The zathura instance.
* @param id The bookmark's id.
* @return true on success, false otherwise
*/
void zathura_bookmark_remove(zathura_t* zathura, const gchar* id);
bool zathura_bookmark_remove(zathura_t* zathura, const gchar* id);
/**
* Get bookmark from the list of bookmarks.

View file

@ -1,8 +1,11 @@
/* See LICENSE file for license and copyright information */
#include "commands.h"
#include "bookmarks.h"
#include "database.h"
#include "zathura.h"
#include "print.h"
#include "document.h"
bool
cmd_bookmark_create(girara_session_t* session, girara_list_t* argument_list)
@ -13,6 +16,27 @@ cmd_bookmark_create(girara_session_t* session, girara_list_t* argument_list)
bool
cmd_bookmark_delete(girara_session_t* session, girara_list_t* argument_list)
{
g_return_val_if_fail(session != NULL, false);
g_return_val_if_fail(session->global.data != NULL, false);
zathura_t* zathura = session->global.data;
if (zathura->document == NULL) {
girara_notify(session, GIRARA_ERROR, "No document opened.");
return false;
}
const unsigned int argc = girara_list_size(argument_list);
if (argc != 1) {
girara_notify(session, GIRARA_ERROR, "Invalid number of arguments given.");
return false;
}
const char* bookmark = girara_list_nth(argument_list, 0);
if (zathura_bookmark_remove(zathura, bookmark)) {
girara_notify(session, GIRARA_INFO, "Removed bookmark: %s", bookmark);
} else {
girara_notify(session, GIRARA_ERROR, "Failed to remove bookmark: %s", bookmark);
}
return true;
}
@ -57,20 +81,20 @@ cmd_open(girara_session_t* session, girara_list_t* argument_list)
g_return_val_if_fail(session->global.data != NULL, false);
zathura_t* zathura = session->global.data;
const int argc = girara_list_size(argument_list);
if (argc > 2) {
girara_notify(session, GIRARA_ERROR, "Too many arguments.");
return false;
}
else if (argc >= 1) {
if (zathura->document) {
document_close(zathura);
}
const int argc = girara_list_size(argument_list);
if (argc > 2) {
girara_error("too many arguments");
return false;
}
else if (argc >= 1) {
document_open(zathura, girara_list_nth(argument_list, 0), (argc == 2) ? girara_list_nth(argument_list, 1) : NULL);
}
else {
girara_error("no arguments");
girara_notify(session, GIRARA_ERROR, "No arguments given.");
return false;
}
@ -85,7 +109,7 @@ cmd_print(girara_session_t* session, girara_list_t* argument_list)
zathura_t* zathura = session->global.data;
if (zathura->document == NULL) {
girara_error("no document as been opened");
girara_notify(session, GIRARA_ERROR, "No open document.");
return false;
}
@ -102,7 +126,7 @@ cmd_save(girara_session_t* session, girara_list_t* argument_list)
zathura_t* zathura = session->global.data;
if (zathura->document == NULL) {
girara_error("no document as been opened");
girara_notify(session, GIRARA_ERROR, "No open document.");
return false;
}
@ -110,7 +134,7 @@ cmd_save(girara_session_t* session, girara_list_t* argument_list)
document_save(zathura, girara_list_nth(argument_list, 0), false);
}
else {
girara_error("invalid arguments");
girara_notify(session, GIRARA_ERROR, "Invalid number of arguments.");
return false;
}
@ -125,7 +149,7 @@ cmd_savef(girara_session_t* session, girara_list_t* argument_list)
zathura_t* zathura = session->global.data;
if (zathura->document == NULL) {
girara_error("no document as been opened");
girara_notify(session, GIRARA_ERROR, "No open document.");
return false;
}
@ -133,7 +157,7 @@ cmd_savef(girara_session_t* session, girara_list_t* argument_list)
document_save(zathura, girara_list_nth(argument_list, 0), true);
}
else {
girara_error("invalid arguments");
girara_notify(session, GIRARA_ERROR, "Invalid number of arguments.");
return false;
}