modules/history.js: whitespace, style
[conkeror.git] / modules / history.js
blob73bdff9736f6624954ed919dc989ea1fd924fe42
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     PlacesUtils.history.removeAllPages();
115 interactive("clear-history",
116     "Permanently delete all location history.",
117     function (I) {
118         clear_history();
119         I.minibuffer.message("Location history cleared.");
120     });
124  * history_clean takes a predicate or an array of predicates, iterates
125  * through the browser history, and removes all items for which any of the
126  * given predicates return true.  Predicates are called with three
127  * arguments: string URI, age in days of the entry, and access count of
128  * the entry.  Age is a decimal number, so smaller divisions that days can
129  * be obtained by dividing appropriately.
131  * It accepts the keywords $dry_run and $verbose.  When $dry_run is given,
132  * entries will not be deleted.  When $verbose is given, overall and
133  * itemized deletion counts will be reported in the terminal.
134  */
135 define_keywords("$dry_run", "$verbose");
136 function history_clean (predicates) {
137     keywords(arguments, $verbose = false, $dry_run = false);
138     predicates = make_array(predicates);
139     var npred = predicates.length;
140     var predhits = [];
141     var verbose = arguments.$verbose;
142     var dry_run = arguments.$dry_run;
143     var query = nav_history_service.getNewQuery();
144     query.searchTerms = "";
145     var options = nav_history_service.getNewQueryOptions();
146     options.queryType = options.QUERY_TYPE_HISTORY;
147     options.includeHidden = true;
148     var root = nav_history_service.executeQuery(query, options).root;
149     root.containerOpen = true;
150     var count = root.childCount;
151     if (verbose)
152         dumpln("[history_clean] before-count: "+count);
153     var now = Date.now() / 86400000; // now in days
154     var history = Cc["@mozilla.org/browser/nav-history-service;1"]
155         .getService(Ci.nsIBrowserHistory);
156     var to_remove = [];
157     var remove_count = 0;
158     outer:
159     for (var i = count - 1; i >= 0; --i) {
160         var o = root.getChild(i); // nsINavHistoryResultNode
161         var age = now - o.time / 86400000000; // age in days
162         for (var j = 0; j < npred; ++j) {
163             var p = predicates[j];
164             if (p(o.uri, age, o.accessCount)) {
165                 predhits[j] = (predhits[j] || 0) + 1;
166                 to_remove.push(make_uri(o.uri));
167                 remove_count++;
168                 continue outer;
169             }
170         }
171     }
172     if (! dry_run && remove_count > 0)
173         history.removePages(to_remove, remove_count);
174     if (verbose) {
175         dumpln("[history_clean] after-count " +
176                (dry_run ? " (DRY_RUN):" : ":") +
177                (count - predhits.reduce(function (a, b) a + b, 0)));
178         for (j = 0; j < npred; ++j) {
179             var name = predicates[j].name;
180             if (! name)
181                 name = pretty_print_function(predicates[j]);
182             var hits = predhits[j] || 0;
183             dumpln("[history_clean] " + name + ": " + hits);
184         }
185     }
189 provide("history");