summaryrefslogtreecommitdiff
path: root/common/modules/bookmarkcache.jsm
blob: c4586d539f57df3c02dba454432f7ec54b0f37fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright ©2008-2010 Kris Maglione <maglione.k at Gmail>
//
// This work is licensed for reuse under an MIT license. Details are
// given in the LICENSE.txt file included with this file.
"use strict";

Components.utils.import("resource://dactyl/base.jsm");
defineModule("bookmarkcache", {
    exports: ["Bookmark", "BookmarkCache", "Keyword", "bookmarkcache"],
    require: ["services", "storage", "util"]
});


const Bookmark = Struct("url", "title", "icon", "keyword", "tags", "id");
const Keyword = Struct("keyword", "title", "icon", "url");
Bookmark.defaultValue("icon", function () BookmarkCache.getFavicon(this.url));
Bookmark.prototype.__defineGetter__("extra", function () [
                        ["keyword", this.keyword,         "Keyword"],
                        ["tags",    this.tags.join(", "), "Tag"]
                    ].filter(function (item) item[1]));

const bookmarks = services.get("bookmarks");
const history   = services.get("history");
const tagging   = services.get("tagging");
const name      = "bookmark-cache";

const BookmarkCache = Module("BookmarkCache", XPCOM(Ci.nsINavBookmarkObserver), {
    init: function init() {
        bookmarks.addObserver(this, false);
    },

    __iterator__: function () (val for ([, val] in Iterator(bookmarkcache.bookmarks))),

    get bookmarks() Class.replaceProperty(this, "bookmarks", this.load()),

    rootFolders: ["toolbarFolder", "bookmarksMenuFolder", "unfiledBookmarksFolder"]
        .map(function (s) bookmarks[s]),

    _deleteBookmark: function deleteBookmark(id) {
        let length = this.bookmarks.length;
        let result;
        this.bookmarks = this.bookmarks.filter(function (item) {
            if (item.id == id)
                result = item;
            return item.id != id;
        });
        return result;
    },

    _loadBookmark: function loadBookmark(node) {
        if (node.uri == null) // How does this happen?
            return false;
        let uri = util.newURI(node.uri);
        let keyword = bookmarks.getKeywordForBookmark(node.itemId);
        let tags = tagging.getTagsForURI(uri, {}) || [];
        return Bookmark(node.uri, node.title, node.icon && node.icon.spec, keyword, tags, node.itemId);
    },

    readBookmark: function readBookmark(id) {
        return {
            itemId: id,
            uri:    bookmarks.getBookmarkURI(id).spec,
            title:  bookmarks.getItemTitle(id)
        };
    },

    findRoot: function findRoot(id) {
        do {
            var root = id;
            id = bookmarks.getFolderIdForItem(id);
        } while (id != bookmarks.placesRoot && id != root);
        return root;
    },

    isBookmark: function (id) this.rootFolders.indexOf(this.findRoot(id)) >= 0,

    isRegularBookmark: function isRegularBookmark(id) {
        do {
            var root = id;
            if (services.get("livemark") && services.get("livemark").isLivemark(id))
                return false;
            id = bookmarks.getFolderIdForItem(id);
        } while (id != bookmarks.placesRoot && id != root);
        return this.rootFolders.indexOf(root) >= 0;
    },

    get keywords() [Keyword(k.keyword, k.title, k.icon, k.url) for ([, k] in Iterator(this.bookmarks)) if (k.keyword)],

    // Should be made thread safe.
    load: function load() {
        let bookmarks = [];

        let folders = this.rootFolders.slice();
        let query = history.getNewQuery();
        let options = history.getNewQueryOptions();
        while (folders.length > 0) {
            query.setFolders(folders, 1);
            folders.shift();
            let result = history.executeQuery(query, options);
            let folder = result.root;
            folder.containerOpen = true;

            // iterate over the immediate children of this folder
            for (let i = 0; i < folder.childCount; i++) {
                let node = folder.getChild(i);
                if (node.type == node.RESULT_TYPE_FOLDER)   // folder
                    folders.push(node.itemId);
                else if (node.type == node.RESULT_TYPE_URI) // bookmark
                    bookmarks.push(this._loadBookmark(node));
            }

            // close a container after using it!
            folder.containerOpen = false;
        }

        return bookmarks;
    },

    onItemAdded: function onItemAdded(itemId, folder, index) {
        if (bookmarks.getItemType(itemId) == bookmarks.TYPE_BOOKMARK) {
            if (this.isBookmark(itemId)) {
                let bmark = this._loadBookmark(this.readBookmark(itemId));
                this.bookmarks.push(bmark);
                storage.fireEvent(name, "add", bmark);
            }
        }
    },
    onItemRemoved: function onItemRemoved(itemId, folder, index) {
        let result = this._deleteBookmark(itemId);
        if (result)
            storage.fireEvent(name, "remove", result);
    },
    onItemChanged: function onItemChanged(itemId, property, isAnnotation, value) {
        if (isAnnotation)
            return;
        let bookmark = bookmarkcache.bookmarks.filter(function (item) item.id == itemId)[0];
        if (bookmark) {
            if (property == "tags")
                value = tagging.getTagsForURI(util.newURI(bookmark.url), {});
            if (property in bookmark) {
                bookmark[property] = value;
                storage.fireEvent(name, "change", { __proto__: bookmark, changed: property });
            }
        }
    }
}, {
    getFavicon: function getFavicon(uri) {
        try {
            return service.get("favicon").getFaviconImageForPage(util.newURI(uri)).spec;
        }
        catch (e) {
            return "";
        }
    }
});

endModule();

// vim: set fdm=marker sw=4 sts=4 et ft=javascript: