Implemented browser_input_minibuffer_status_mode
[conkeror.git] / modules / utils.js
blob7a1ee0f2eba6518166f2f79931f74fe4b4d91d9d
1 /***** BEGIN LICENSE BLOCK *****
2 Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 The contents of this file are subject to the Mozilla Public License Version
5 1.1 (the "License"); you may not use this file except in compliance with
6 the License. You may obtain a copy of the License at
7 http://www.mozilla.org/MPL/
9 Software distributed under the License is distributed on an "AS IS" basis,
10 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 for the specific language governing rights and limitations under the
12 License.
14 The Initial Developer of the Original Code is Shawn Betts.
15 Portions created by the Initial Developer are Copyright (C) 2004,2005
16 by the Initial Developer. All Rights Reserved.
18 Alternatively, the contents of this file may be used under the terms of
19 either the GNU General Public License Version 2 or later (the "GPL"), or
20 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
21 in which case the provisions of the GPL or the LGPL are applicable instead
22 of those above. If you wish to allow use of your version of this file only
23 under the terms of either the GPL or the LGPL, and not to allow others to
24 use your version of this file under the terms of the MPL, indicate your
25 decision by deleting the provisions above and replace them with the notice
26 and other provisions required by the GPL or the LGPL. If you do not delete
27 the provisions above, a recipient may use your version of this file under
28 the terms of any one of the MPL, the GPL or the LGPL.
29 ***** END LICENSE BLOCK *****/
32  * Utility functions for application scope.
33  *
34  */
37 function string_hashset() {
40 string_hashset.prototype = {
41     constructor : string_hashset,
43     add : function(s) {
44         this["-" + s] = true;
45     },
46     
47     contains : function(s) {
48         return (("-" + s) in this);
49     },
51     remove : function (s) {
52         delete this["-" + s];
53     },
55     for_each : function (f) {
56         for (var i in this) {
57             if (i[0] == "-")
58                 f(i.slice(1));
59         }
60     }
63 function string_hashmap() {
66 string_hashmap.prototype = {
67     constructor : string_hashmap,
69     put : function(s,value) {
70         this["-" + s] = value;
71     },
72     
73     contains : function(s) {
74         return (("-" + s) in this);
75     },
77     get : function(s, default_value) {
78         if (this.contains(s))
79             return this["-" + s];
80         return default_value;
81     },
83     get_put_default : function(s, default_value) {
84         if (this.contains(s))
85             return this["-" + s];
86         return (this["-" + s] = default_value);
87     },
89     remove : function (s) {
90         delete this["-" + s];
91     },
93     for_each : function (f) {
94         for (var i in this) {
95             if (i[0] == "-")
96                 f(i.slice(1), this[i]);
97         }
98     },
100     for_each_value : function (f) {
101         for (var i in this) {
102             if (i[0] == "-")
103                 f(this[i]);
104         }
105     }
108 /// Window title formatting
111  * Default tile formatter.  The page url is ignored.  If there is a
112  * page_title, returns: "Page title - Conkeror".  Otherwise, it
113  * returns just: "Conkeror".
114  */
115 function default_title_formatter (frame)
117     var page_title = frame.buffers.current.title;
119     if (page_title && page_title.length > 0)
120         return page_title + " - Conkeror";
121     else
122         return "Conkeror";
125 var title_format_fn = null;
127 function set_window_title (frame)
129     frame.document.title = title_format_fn(frame);
132 function init_window_title ()
134     title_format_fn = default_title_formatter;
136     add_hook("frame_initialize_late_hook", set_window_title);
137     add_hook("current_browser_buffer_location_change_hook",
138              function (buffer) {
139                  set_window_title(buffer.frame);
140              });
141     add_hook("select_buffer_hook", function (buffer) { set_window_title(buffer.frame); }, true);
142     add_hook("current_buffer_title_change_hook",
143              function (buffer) {
144                  set_window_title(buffer.frame);
145              });
150 // Put the string on the clipboard
151 function writeToClipboard(str)
153     const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
154         .getService(Components.interfaces.nsIClipboardHelper);
155     gClipboardHelper.copyString(str);
159 function makeURLAbsolute (base, url)
161     // Construct nsIURL.
162     var ioService = Components.classes["@mozilla.org/network/io-service;1"]
163         .getService(Components.interfaces.nsIIOService);
164     var baseURI  = ioService.newURI(base, null, null);
166     return ioService.newURI (baseURI.resolve (url), null, null).spec;
170 function get_link_location (element)
172     if (element && element.getAttribute("href")) {
173         var loc = element.getAttribute("href");
174         return makeURLAbsolute(element.baseURI, loc);
175     }
176     return null;
180 function makeURL(aURL)
182   var ioService = Components.classes["@mozilla.org/network/io-service;1"]
183                 .getService(Components.interfaces.nsIIOService);
184   return ioService.newURI(aURL, null, null);
187 function makeFileURL(aFile)
189   var ioService = Components.classes["@mozilla.org/network/io-service;1"]
190                 .getService(Components.interfaces.nsIIOService);
191   return ioService.newFileURI(aFile);
195 function get_document_content_disposition (document_o)
197     var content_disposition = null;
198     try {
199         content_disposition =
200             document_o.defaultView
201             .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
202             .getInterface(Components.interfaces.nsIDOMWindowUtils)
203             .getDocumentMetadata("content-disposition");
204     } catch (e) { }
205     return content_disposition;
209 function open_url_in_prompt(prefix, str)
211     if (str == null)
212         str = "Find URL";
213     if (prefix == 1) {
214         return str + ":";
215     } else if (prefix <= 4) {
216         return str + " in new buffer:";
217     } else {
218         return str + " in new frame:";
219     }
223 function set_focus_no_scroll(frame, element)
225     frame.document.commandDispatcher.suppressFocusScroll = true;
226     element.focus();
227     frame.document.commandDispatcher.suppressFocusScroll = false;
231 function do_N_times(func, n)
233     var args = Array.prototype.slice.call(arguments, 2);
234     while (n-- > 0)
235         func.apply(null, args);
238 // remove whitespace from the beginning and end
239 function trim_whitespace (str)
241     var tmp = new String (str);
242     return tmp.replace (/^\s+/, "").replace (/\s+$/, "");
245 function abs_point (node)
247     var orig = node;
248     var pt = {};
249     try {
250         pt.x = node.offsetLeft;
251         pt.y = node.offsetTop;
252         // find imagemap's coordinates
253         if (node.tagName == "AREA") {
254             var coords = node.getAttribute("coords").split(",");
255             pt.x += Number(coords[0]);
256             pt.y += Number(coords[1]);
257         }
259         node = node.offsetParent;
260         // Sometimes this fails, so just return what we got.
262         while (node.tagName != "BODY") {
263             pt.x += node.offsetLeft;
264             pt.y += node.offsetTop;
265             node = node.offsetParent;
266         }
267     } catch(e) {
268 //      node = orig;
269 //      while (node.tagName != "BODY") {
270 //          alert("okay: " + node + " " + node.tagName + " " + pt.x + " " + pt.y);
271 //          node = node.offsetParent;
272 //      }
273     }
274     return pt;
277 function get_os ()
279     // possible return values: 'Darwin', 'Linux', 'WINNT', ...
280     var appinfo = Cc['@mozilla.org/xre/app-info;1'].createInstance (Ci.nsIXULRuntime);
281     return appinfo.OS;
284 var default_directory = null;
286 function set_default_directory(directory_s) {
287     function getenv (variable) {
288         var env = Cc['@mozilla.org/process/environment;1'].createInstance(Ci.nsIEnvironment);
289         if (env.exists (variable))
290             return env.get(variable);
291         return null;
292     }
294     if (! directory_s)
295     {
296         if ( this.get_os() == "WINNT")
297         {
298             directory_s = getenv ('USERPROFILE') ||
299                 getenv ('HOMEDRIVE') + getenv ('HOMEPATH');
300         }
301         else {
302             directory_s = getenv ('HOME');
303         }
304     }
306     default_directory = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
307     default_directory.initWithPath (directory_s);
310 set_default_directory();
312 const XHTML_NS = "http://www.w3.org/1999/xhtml";
313 const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
314 const MATHML_NS = "http://www.w3.org/1998/Math/MathML";
316 function create_XUL(frame, tag_name)
318     return frame.document.createElementNS(XUL_NS, tag_name);
322 /* Used in calls to XPath evaluate */
323 function xpath_lookup_namespace(prefix) {
324     if (prefix == "xhtml")
325         return XHTML_NS;
326     if (prefix == "m")
327         return MATHML_NS;
328     return null;
331 function method_caller(obj, func) {
332     return function () {
333         func.apply(obj, arguments);
334     }