!import
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is mozilla.org code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Alec Flett <alecf@netscape.com>
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39 /**
40 * Communicator Shared Utility Library
41 * for shared application glue for the Communicator suite of applications
42 **/
43
44 /*
45 Note: All Editor/Composer-related methods have been moved to editorApplicationOverlay.js,
46 so app windows that require those must include editorNavigatorOverlay.xul
47 */
48
49 var gShowBiDi = false;
50
getBrowserURL
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
51 function getBrowserURL() {
52
53 try {
54 var prefs = Components.classes["@mozilla.org/preferences-service;1"]
55 .getService(Components.interfaces.nsIPrefBranch);
56 var url = prefs.getCharPref("browser.chromeURL");
57 if (url)
58 return url;
59 } catch(e) {
60 }
61 return "chrome://navigator/content/navigator.xul";
62 }
63
64 // update menu items that rely on focus
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
65 function goUpdateGlobalEditMenuItems()
66 {
67 goUpdateCommand('cmd_undo');
68 goUpdateCommand('cmd_redo');
69 goUpdateCommand('cmd_cut');
70 goUpdateCommand('cmd_copy');
71 goUpdateCommand('cmd_paste');
72 goUpdateCommand('cmd_selectAll');
73 goUpdateCommand('cmd_delete');
74 if (gShowBiDi)
75 goUpdateCommand('cmd_switchTextDirection');
76 }
77
78 // update menu items that rely on the current selection
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
79 function goUpdateSelectEditMenuItems()
80 {
81 goUpdateCommand('cmd_cut');
82 goUpdateCommand('cmd_copy');
83 goUpdateCommand('cmd_delete');
84 goUpdateCommand('cmd_selectAll');
85 }
86
87 // update menu items that relate to undo/redo
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
88 function goUpdateUndoEditMenuItems()
89 {
90 goUpdateCommand('cmd_undo');
91 goUpdateCommand('cmd_redo');
92 }
93
94 // update menu items that depend on clipboard contents
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
95 function goUpdatePasteMenuItems()
96 {
97 goUpdateCommand('cmd_paste');
98 }
99
100 // update Find As You Type menu items, they rely on focus
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
101 function goUpdateFindTypeMenuItems()
102 {
103 goUpdateCommand('cmd_findTypeText');
104 goUpdateCommand('cmd_findTypeLinks');
105 }
106
107 // Gather all descendent text under given document node.
gatherTextUnder
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
108 function gatherTextUnder ( root )
109 {
110 var text = "";
111 var node = root.firstChild;
112 var depth = 1;
113 while ( node && depth > 0 ) {
114 // See if this node is text.
115 if ( node.nodeType == Node.TEXT_NODE ) {
116 // Add this text to our collection.
117 text += " " + node.data;
118 } else if ( node instanceof HTMLImageElement ) {
119 // If it has an alt= attribute, add that.
120 var altText = node.getAttribute( "alt" );
121 if ( altText && altText != "" ) {
122 text += " " + altText;
123 }
124 }
125 // Find next node to test.
126 // First, see if this node has children.
127 if ( node.hasChildNodes() ) {
128 // Go to first child.
129 node = node.firstChild;
130 depth++;
131 } else {
132 // No children, try next sibling.
133 if ( node.nextSibling ) {
134 node = node.nextSibling;
135 } else {
136 // Last resort is a sibling of an ancestor.
137 while ( node && depth > 0 ) {
138 node = node.parentNode;
139 depth--;
140 if ( node.nextSibling ) {
141 node = node.nextSibling;
142 break;
143 }
144 }
145 }
146 }
147 }
148 // Strip leading whitespace.
149 text = text.replace( /^\s+/, "" );
150 // Strip trailing whitespace.
151 text = text.replace( /\s+$/, "" );
152 // Compress remaining whitespace.
153 text = text.replace( /\s+/g, " " );
154 return text;
155 }
156
GenerateValidFilename
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
157 function GenerateValidFilename(filename, extension)
158 {
159 if (filename) // we have a title; let's see if it's usable
160 {
161 // clean up the filename to make it usable and
162 // then trim whitespace from beginning and end
163 filename = validateFileName(filename).replace(/^\s+|\s+$/g, "");
164 if (filename.length > 0)
165 return filename + extension;
166 }
167 return null;
168 }
169
validateFileName
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
170 function validateFileName(aFileName)
171 {
172 var re = /[\/]+/g;
173 if (navigator.appVersion.indexOf("Windows") != -1) {
174 re = /[\\\/\|]+/g;
175 aFileName = aFileName.replace(/[\"]+/g, "'");
176 aFileName = aFileName.replace(/[\*\:\?]+/g, " ");
177 aFileName = aFileName.replace(/[\<]+/g, "(");
178 aFileName = aFileName.replace(/[\>]+/g, ")");
179 }
180 else if (navigator.appVersion.indexOf("Macintosh") != -1)
181 re = /[\:\/]+/g;
182
183 return aFileName.replace(re, "_");
184 }
185
goToggleToolbar
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
186 function goToggleToolbar( id, elementID )
187 {
188 var toolbar = document.getElementById( id );
189 var element = document.getElementById( elementID );
190 if ( toolbar )
191 {
192 var isHidden = toolbar.getAttribute("hidden") == "true";
193 toolbar.setAttribute("hidden", !isHidden);
194 if ( element )
195 element.setAttribute("checked", isHidden)
196 document.persist(id, 'hidden');
197 document.persist(elementID, 'checked');
198 }
199 }
200
201 /**
202 * Opens the update manager and checks for updates to the application.
203 */
checkForUpdates
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
204 function checkForUpdates()
205 {
206 var um =
207 Components.classes["@mozilla.org/updates/update-manager;1"].
208 getService(Components.interfaces.nsIUpdateManager);
209 var prompter =
210 Components.classes["@mozilla.org/updates/update-prompt;1"].
211 createInstance(Components.interfaces.nsIUpdatePrompt);
212
213 // If there's an update ready to be applied, show the "Update Downloaded"
214 // UI instead and let the user know they have to restart the application for
215 // the changes to be applied.
216 if (um.activeUpdate && um.activeUpdate.state == "pending")
217 prompter.showUpdateDownloaded(um.activeUpdate);
218 else
219 prompter.checkForUpdates();
220 }
221
222 /**
223 * Set up the help menu software update items to show proper status,
224 * also disabling the items if update is disabled.
225 */
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
226 function buildHelpMenu()
227 {
228 var updates =
229 Components.classes["@mozilla.org/updates/update-service;1"].
230 getService(Components.interfaces.nsIApplicationUpdateService);
231 var um =
232 Components.classes["@mozilla.org/updates/update-manager;1"].
233 getService(Components.interfaces.nsIUpdateManager);
234
235 // Disable the UI if the update enabled pref has been locked by the
236 // administrator or if we cannot update for some other reason.
237 var checkForUpdates = document.getElementById("checkForUpdates");
238 var canUpdate = updates.canUpdate;
239 checkForUpdates.setAttribute("disabled", !canUpdate);
240 if (!canUpdate)
241 return;
242
243 var strings = document.getElementById("bundle_messenger");
244 var activeUpdate = um.activeUpdate;
245
246 // If there's an active update, substitute its name into the label
247 // we show for this item, otherwise display a generic label.
getStringWithUpdateName
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
248 function getStringWithUpdateName(key) {
249 if (activeUpdate && activeUpdate.name)
250 return strings.getFormattedString(key, [activeUpdate.name]);
251 return strings.getString(key + "Fallback");
252 }
253
254 // By default, show "Check for Updates..."
255 var key = "default";
256 if (activeUpdate) {
257 switch (activeUpdate.state) {
258 case "downloading":
259 // If we're downloading an update at present, show the text:
260 // "Downloading Thunderbird x.x..." otherwise we're paused, and show
261 // "Resume Downloading Thunderbird x.x..."
262 key = updates.isDownloading ? "downloading" : "resume";
263 break;
264 case "pending":
265 // If we're waiting for the user to restart, show: "Apply Downloaded
266 // Updates Now..."
267 key = "pending";
268 break;
269 }
270 }
271
272 checkForUpdates.label = getStringWithUpdateName("updatesItem_" + key);
273 if (um.activeUpdate && updates.isDownloading)
274 checkForUpdates.setAttribute("loading", "true");
275 else
276 checkForUpdates.removeAttribute("loading");
277 }