~ubuntu-branches/ubuntu/feisty/elinks/feisty-updates

« back to all changes in this revision

Viewing changes to src/bookmarks/backend/common.c

  • Committer: Bazaar Package Importer
  • Author(s): Peter Gervai
  • Date: 2004-01-21 22:13:45 UTC
  • Revision ID: james.westby@ubuntu.com-20040121221345-ju33hai1yhhqt6kn
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Internal bookmarks support - file format backends multiplexing */
 
2
/* $Id: common.c,v 1.16 2004/01/01 15:12:12 pasky Exp $ */
 
3
 
 
4
#ifdef HAVE_CONFIG_H
 
5
#include "config.h"
 
6
#endif
 
7
 
 
8
#ifdef CONFIG_BOOKMARKS
 
9
 
 
10
#include <stdio.h>
 
11
#include <stdlib.h>
 
12
#include <string.h>
 
13
 
 
14
#include "elinks.h"
 
15
 
 
16
#include "bfu/listbox.h"
 
17
#include "bookmarks/bookmarks.h"
 
18
#include "bookmarks/backend/common.h"
 
19
#include "lowlevel/home.h"
 
20
#include "util/memory.h"
 
21
#include "util/secsave.h"
 
22
#include "util/string.h"
 
23
 
 
24
 
 
25
/* Backends dynamic area: */
 
26
 
 
27
#include "bookmarks/backend/default.h"
 
28
#include "bookmarks/backend/xbel.h"
 
29
 
 
30
/* Note that the numbering is static, that means that you have to provide at
 
31
 * least dummy NULL handlers even when no support is compiled in. */
 
32
 
 
33
static struct bookmarks_backend *bookmarks_backends[] = {
 
34
        &default_bookmarks_backend,
 
35
        &xbel_bookmarks_backend,
 
36
};
 
37
 
 
38
 
 
39
/* Loads the bookmarks from file */
 
40
void
 
41
bookmarks_read(void)
 
42
{
 
43
        int backend = get_opt_int("bookmarks.file_format");
 
44
        unsigned char *file_name;
 
45
        FILE *f;
 
46
 
 
47
        if (!bookmarks_backends[backend]->read
 
48
            || !bookmarks_backends[backend]->filename) return;
 
49
 
 
50
        file_name = bookmarks_backends[backend]->filename(0);
 
51
        if (!file_name) return;
 
52
        if (elinks_home) {
 
53
                file_name = straconcat(elinks_home, file_name, NULL);
 
54
                if (!file_name) return;
 
55
        }
 
56
 
 
57
        f = fopen(file_name, "r");
 
58
        if (elinks_home) mem_free(file_name);
 
59
        if (!f) return;
 
60
 
 
61
        bookmarks_backends[backend]->read(f);
 
62
 
 
63
        fclose(f);
 
64
        bookmarks_dirty = 0;
 
65
}
 
66
 
 
67
void
 
68
bookmarks_write(struct list_head *bookmarks_list)
 
69
{
 
70
        int backend = get_opt_int("bookmarks.file_format");
 
71
        struct secure_save_info *ssi;
 
72
        unsigned char *file_name;
 
73
 
 
74
        if (!bookmarks_dirty) return;
 
75
        if (!bookmarks_backends[backend]->write
 
76
            || !elinks_home
 
77
            || !bookmarks_backends[backend]->filename) return;
 
78
 
 
79
        /* We do this two-passes because we want backend to possibly decide to
 
80
         * return NULL if it's not suitable to save the bookmarks (otherwise
 
81
         * they would be just truncated to zero by secure_open()). */
 
82
        file_name = bookmarks_backends[backend]->filename(1);
 
83
        if (!file_name) return;
 
84
        file_name = straconcat(elinks_home, file_name, NULL);
 
85
        if (!file_name) return;
 
86
 
 
87
        ssi = secure_open(file_name, 0177);
 
88
        mem_free(file_name);
 
89
        if (!ssi) return;
 
90
 
 
91
        bookmarks_backends[backend]->write(ssi, bookmarks_list);
 
92
 
 
93
        if (!secure_close(ssi)) bookmarks_dirty = 0;
 
94
}
 
95
 
 
96
#endif /* CONFIG_BOOKMARKS */