history.js: Fix clear-history for FF >= 45
[conkeror.git] / modules / history.js
blobdd4960a21374123a02eb1cb3a1f31f507ba2322b
1 /**
2  * (C) Copyright 2008 Eli Naeher
3  * (C) Copyright 2008 Jeremy Maitin-Shepard
4  * (C) Copyright 2011-2012 John J. Foerch
5  *
6  * Use, modification, and distribution are subject to the terms specified in the
7  * COPYING file.
8 **/
10 function history_completions (completer, root) {
11     completions.call(this, completer);
12     this.root = root;
13     this.root.containerOpen = true;
14     this.count = this.root.childCount;
16 history_completions.prototype = {
17     constructor: history_completions,
18     __proto__: completions.prototype,
19     toString: function () "#<history_completions>",
20     root: null,
21     destroy: function () { this.root.containerOpen = false; },
22     get_string: function (i) this.root.getChild(i).uri,
23     get_description: function (i) this.root.getChild(i).title,
24     get_value: function (i) this.root.getChild(i),
28 define_keywords("$use_history", "$use_bookmarks", "$sort_order");
29 function history_completer () {
30     keywords(arguments,
31              $use_history = false,
32              $use_bookmarks = false,
33              $sort_order = "visitcount_descending");
34     completer.call(this);
35     this.use_history = arguments.$use_history;
36     this.use_bookmarks = arguments.$use_bookmarks;
37     this.sort_order = arguments.$sort_order;
39 history_completer.prototype = {
40     constructor: history_completer,
41     __proto__: completer.prototype,
42     toString: function () "#<history_completer>",
43     use_history: false,
44     use_bookmarks: false,
45     sort_order: null,
46     complete: function (input, pos) {
47         var query = nav_history_service.getNewQuery();
48         query.searchTerms = input;
49         if (! this.use_history)
50             query.onlyBookmarked = true;
51         var options = nav_history_service.getNewQueryOptions();
52         options.sortingMode = Ci.nsINavHistoryQueryOptions[
53             "SORT_BY_" + this.sort_order.toUpperCase()];
54         if (this.use_bookmarks && ! this.use_history)
55             options.queryType = options.QUERY_TYPE_BOOKMARKS;
56         else if (this.use_history && ! this.use_bookmarks)
57             options.queryType = options.QUERY_TYPE_HISTORY;
58         else
59             options.queryType = options.QUERY_TYPE_UNIFIED; //XXX: not implemented yet
60         var root = nav_history_service.executeQuery(query, options).root;
61         return new history_completions(this, root);
62     }
65 define_keywords("$use_webjumps");
66 function url_completer () {
67     keywords(arguments, $sort_order = "visitcount_descending");
68     var sort_order = arguments.$sort_order;
69     var completers = [];
70     completers.push(new file_path_completer());
71     if (arguments.$use_webjumps)
72         completers.push(new webjump_completer());
73     // Do queries separately (which can lead to duplicates).  The queries
74     // can be combined when QUERY_TYPE_UNIFIED is implemented.
75     if (arguments.$use_bookmarks)
76         completers.push(new history_completer($use_bookmarks = true,
77                                               $sort_order = sort_order));
78     if (arguments.$use_history)
79         completers.push(new history_completer($use_history = true,
80                                               $sort_order = sort_order));
81     merged_completer.call(this, completers);
83 url_completer.prototype = {
84     constructor: url_completer,
85     __proto__: merged_completer.prototype,
86     toString: function () "#<url_completer>"
90 function add_bookmark (url, title) {
91     nav_bookmarks_service.insertBookmark(nav_bookmarks_service.unfiledBookmarksFolder,
92                                          make_uri(url), -1, title);
95 // See
96 // http://mxr.mozilla.org/mozilla-central/source/browser/base/content/sanitize.js
97 // for the Firefox implementation for clearing various history information.
99 function clear_form_history () {
100     var FormHistory = Cu.import("resource://gre/modules/FormHistory.jsm", null).FormHistory;
101     // This is asynchronous, but we don't care about waiting for it to finish.
102     FormHistory.update( { op: "remove" } );
104 interactive("clear-form-history",
105     "Permanently delete all form autocomplete history.",
106     function (I) {
107         clear_form_history();
108         I.minibuffer.message("Form history cleared.");
109     });
111 function clear_history () {
112     var PlacesUtils = Cu.import("resource://gre/modules/PlacesUtils.jsm").PlacesUtils;
113     var xulrunner_version = get_mozilla_version();
114     if (version_compare(xulrunner_version, "45.0") < 0) {
115         PlacesUtils.history.removeAllPages();
116     } else {
117         PlacesUtils.history.clear();
118     }
120 interactive("clear-history",
121     "Permanently delete all location history.",
122     function (I) {
123         clear_history();
124         I.minibuffer.message("Location history cleared.");
125     });
129  * history_clean takes a predicate or an array of predicates, iterates
130  * through the browser history, and removes all items for which any of the
131  * given predicates return true.  Predicates are called with three
132  * arguments: string URI, age in days of the entry, and access count of
133  * the entry.  Age is a decimal number, so smaller divisions that days can
134  * be obtained by dividing appropriately.
136  * It accepts the keywords $dry_run and $verbose.  When $dry_run is given,
137  * entries will not be deleted.  When $verbose is given, overall and
138  * itemized deletion counts will be reported in the terminal.
139  */
140 define_keywords("$dry_run", "$verbose");
141 function history_clean (predicates) {
142     keywords(arguments, $verbose = false, $dry_run = false);
143     predicates = make_array(predicates);
144     var npred = predicates.length;
145     var predhits = [];
146     var verbose = arguments.$verbose;
147     var dry_run = arguments.$dry_run;
148     var query = nav_history_service.getNewQuery();
149     query.searchTerms = "";
150     var options = nav_history_service.getNewQueryOptions();
151     options.queryType = options.QUERY_TYPE_HISTORY;
152     options.includeHidden = true;
153     var root = nav_history_service.executeQuery(query, options).root;
154     root.containerOpen = true;
155     var count = root.childCount;
156     var now = Date.now() / 86400000; // now in days
157     var history = Cc["@mozilla.org/browser/nav-history-service;1"]
158         .getService(Ci.nsIBrowserHistory);
159     var to_remove = [];
160     var remove_count = 0;
161     outer:
162     for (var i = count - 1; i >= 0; --i) {
163         var o = root.getChild(i); // nsINavHistoryResultNode
164         var age = now - o.time / 86400000000; // age in days
165         for (var j = 0; j < npred; ++j) {
166             var p = predicates[j];
167             if (p(o.uri, age, o.accessCount)) {
168                 predhits[j] = (predhits[j] || 0) + 1;
169                 to_remove.push(make_uri(o.uri));
170                 remove_count++;
171                 continue outer;
172             }
173         }
174     }
175     if (! dry_run && remove_count > 0)
176         history.removePages(to_remove, remove_count);
177     if (verbose) {
178         dumpln("[history_clean] "+
179                (dry_run ? "(DRY_RUN) " : "")+
180                "count: "+count+" -> "+
181                (count - predhits.reduce(function (a, b) a + b, 0)));
182         for (j = 0; j < npred; ++j) {
183             var name = predicates[j].name;
184             if (! name)
185                 name = pretty_print_function(predicates[j]);
186             var hits = predhits[j] || 0;
187             dumpln("[history_clean] " + name + ": " + hits);
188         }
189     }
193 provide("history");