2005-09-15 09:58:31 -04:00
|
|
|
/* Internal bookmarks support - file format backends multiplexing */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
|
|
|
#include "bfu/dialog.h"
|
|
|
|
#include "bookmarks/bookmarks.h"
|
|
|
|
#include "bookmarks/backend/common.h"
|
|
|
|
#include "config/home.h"
|
|
|
|
#include "util/memory.h"
|
|
|
|
#include "util/secsave.h"
|
|
|
|
#include "util/string.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* Backends dynamic area: */
|
|
|
|
|
|
|
|
#include "bookmarks/backend/default.h"
|
|
|
|
#include "bookmarks/backend/xbel.h"
|
|
|
|
|
|
|
|
/* Note that the numbering is static, that means that you have to provide at
|
|
|
|
* least dummy NULL handlers even when no support is compiled in. */
|
|
|
|
/* XXX: keep original order since we use bookmarks.file_format option value
|
|
|
|
* as index. So it means you should add any new backend just before the
|
|
|
|
* ending NULL. */
|
|
|
|
static struct bookmarks_backend *bookmarks_backends[] = {
|
|
|
|
&default_bookmarks_backend,
|
|
|
|
#ifdef CONFIG_XBEL_BOOKMARKS
|
|
|
|
&xbel_bookmarks_backend,
|
|
|
|
#else
|
|
|
|
NULL,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-12-30 16:08:50 -05:00
|
|
|
static int loaded_backend_num = -1;
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
/* Loads the bookmarks from file */
|
|
|
|
void
|
|
|
|
bookmarks_read(void)
|
|
|
|
{
|
2007-08-28 12:41:18 -04:00
|
|
|
int backend_num = get_opt_int("bookmarks.file_format", NULL);
|
2005-09-15 09:58:31 -04:00
|
|
|
struct bookmarks_backend *backend = bookmarks_backends[backend_num];
|
2021-01-02 10:20:27 -05:00
|
|
|
char *file_name;
|
2022-01-31 09:32:24 -05:00
|
|
|
const char *file_name_orig;
|
2005-09-15 09:58:31 -04:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if (!backend
|
|
|
|
|| !backend->read
|
|
|
|
|| !backend->filename) return;
|
|
|
|
|
2022-01-31 09:32:24 -05:00
|
|
|
file_name_orig = backend->filename(0);
|
|
|
|
if (!file_name_orig) return;
|
2005-09-15 09:58:31 -04:00
|
|
|
if (elinks_home) {
|
2022-01-31 09:32:24 -05:00
|
|
|
file_name = straconcat(elinks_home, file_name_orig,
|
2021-01-02 10:20:27 -05:00
|
|
|
(char *) NULL);
|
2005-09-15 09:58:31 -04:00
|
|
|
if (!file_name) return;
|
2022-01-31 09:32:24 -05:00
|
|
|
f = fopen(file_name, "rb");
|
|
|
|
mem_free(file_name);
|
|
|
|
} else {
|
|
|
|
f = fopen(file_name_orig, "rb");
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!f) return;
|
|
|
|
|
|
|
|
backend->read(f);
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
bookmarks_unset_dirty();
|
2005-12-30 16:08:50 -05:00
|
|
|
loaded_backend_num = backend_num;
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-07-26 15:39:08 -04:00
|
|
|
bookmarks_write(LIST_OF(struct bookmark) *bookmarks_list)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2007-08-28 12:41:18 -04:00
|
|
|
int backend_num = get_opt_int("bookmarks.file_format", NULL);
|
2005-09-15 09:58:31 -04:00
|
|
|
struct bookmarks_backend *backend = bookmarks_backends[backend_num];
|
|
|
|
struct secure_save_info *ssi;
|
2021-01-02 10:20:27 -05:00
|
|
|
char *file_name;
|
2022-01-31 09:32:24 -05:00
|
|
|
const char *file_name_orig;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2005-12-30 16:08:50 -05:00
|
|
|
if (!bookmarks_are_dirty() && backend_num == loaded_backend_num) return;
|
2005-09-15 09:58:31 -04:00
|
|
|
if (!backend
|
|
|
|
|| !backend->write
|
|
|
|
|| !elinks_home
|
|
|
|
|| !backend->filename) return;
|
|
|
|
|
|
|
|
/* We do this two-passes because we want backend to possibly decide to
|
|
|
|
* return NULL if it's not suitable to save the bookmarks (otherwise
|
|
|
|
* they would be just truncated to zero by secure_open()). */
|
2022-01-31 09:32:24 -05:00
|
|
|
file_name_orig = backend->filename(1);
|
|
|
|
if (!file_name_orig) return;
|
|
|
|
file_name = straconcat(elinks_home, file_name_orig, (char *) NULL);
|
2005-09-15 09:58:31 -04:00
|
|
|
if (!file_name) return;
|
|
|
|
|
2006-01-10 17:49:35 -05:00
|
|
|
ssi = secure_open(file_name);
|
2005-09-15 09:58:31 -04:00
|
|
|
mem_free(file_name);
|
|
|
|
if (!ssi) return;
|
|
|
|
|
|
|
|
backend->write(ssi, bookmarks_list);
|
|
|
|
|
|
|
|
if (!secure_close(ssi)) bookmarks_unset_dirty();
|
|
|
|
}
|