1 /* -*- Mode: javascript; tab-width: 20; 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 Calendar code.
16 *
17 * The Initial Developer of the Original Code is Eric Belhaire.
18 * Portions created by the Initial Developer are Copyright (C) 2003
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 * Matthew Willis <mattwillis@gmail.com>
23 * Philipp Kewisch <mozilla@kewis.ch>
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * 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 const nsIWindowMediator = Components.interfaces.nsIWindowMediator;
41
toOpenWindowByType
42 function toOpenWindowByType(inType, uri)
43 {
44 var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService();
45 var windowManagerInterface = windowManager.QueryInterface(nsIWindowMediator);
46 var topWindow = windowManagerInterface.getMostRecentWindow(inType);
47
48 if (topWindow)
49 topWindow.focus();
50 else
51 window.open(uri, "_blank", "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar");
52 }
53
toJavaScriptConsole
54 function toJavaScriptConsole()
55 {
56 toOpenWindowByType("global:console", "chrome://global/content/console.xul");
57 }
58
goToggleToolbar
59 function goToggleToolbar(id, elementID)
60 {
61 var toolbar = document.getElementById(id);
62 var element = document.getElementById(elementID);
63 if (toolbar) {
64 var isHidden = toolbar.hidden;
65 toolbar.hidden = !isHidden;
66 document.persist(id, 'hidden');
67 if (element) {
68 element.setAttribute("checked", isHidden ? "true" : "false");
69 document.persist(elementID, 'checked');
70 }
71 }
72 }
73
74
goOpenAddons
75 function goOpenAddons()
76 {
77 const EMTYPE = "Extension:Manager";
78 var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
79 .getService(Components.interfaces.nsIWindowMediator);
80 var theEM = wm.getMostRecentWindow(EMTYPE);
81 if (theEM) {
82 theEM.focus();
83 return;
84 }
85
86 const EMURL = "chrome://mozapps/content/extensions/extensions.xul";
87 const EMFEATURES = "chrome,menubar,extra-chrome,toolbar,dialog=no,resizable";
88 window.openDialog(EMURL, "", EMFEATURES);
89 }
90
91 /**
92 * We recreate the View > Toolbars menu each time it is opened to include any
93 * user-created toolbars.
94 */
95 function sbOnViewToolbarsPopupShowing(aEvent)
96 {
97 var popup = aEvent.target;
98 var i;
99
100 // Empty the menu
101 for (i = popup.childNodes.length-1; i >= 0; i--) {
102 var deadItem = popup.childNodes[i];
103 if (deadItem.hasAttribute("toolbarindex")) {
104 deadItem.removeEventListener("command", sbOnViewToolbarCommand, false);
105 popup.removeChild(deadItem);
106 }
107 }
108
109 var firstMenuItem = popup.firstChild;
110
111 var toolbox = document.getElementById("calendar-toolbox");
112 for (i = 0; i < toolbox.childNodes.length; i++) {
113 var toolbar = toolbox.childNodes[i];
114 var toolbarName = toolbar.getAttribute("toolbarname");
115 var type = toolbar.getAttribute("type");
116 if (toolbarName && type != "menubar") {
117 var menuItem = document.createElement("menuitem");
118 menuItem.setAttribute("toolbarindex", i);
119 menuItem.setAttribute("type", "checkbox");
120 menuItem.setAttribute("label", toolbarName);
121 menuItem.setAttribute("accesskey", toolbar.getAttribute("accesskey"));
122 menuItem.setAttribute("checked", toolbar.getAttribute("collapsed") != "true");
123 popup.insertBefore(menuItem, firstMenuItem);
124
125 menuItem.addEventListener("command", sbOnViewToolbarCommand, false);
126 }
127 }
128 }
129
130 /**
131 * Toggles the visibility of the associated toolbar when fired.
132 */
sbOnViewToolbarCommand
133 function sbOnViewToolbarCommand(aEvent)
134 {
135 var toolbox = document.getElementById("calendar-toolbox");
136 var index = aEvent.originalTarget.getAttribute("toolbarindex");
137 var toolbar = toolbox.childNodes[index];
138
139 toolbar.collapsed = (aEvent.originalTarget.getAttribute("checked") != "true");
140 document.persist(toolbar.id, "collapsed");
141 }
142
143 /**
144 * Checks for available updates using AUS
145 */
sbCheckForUpdates
146 function sbCheckForUpdates()
147 {
148 var um = Components.classes["@mozilla.org/updates/update-manager;1"]
149 .getService(Components.interfaces.nsIUpdateManager);
150 var prompter = Components.classes["@mozilla.org/updates/update-prompt;1"]
151 .createInstance(Components.interfaces.nsIUpdatePrompt);
152
153 // If there's an update ready to be applied, show the "Update Downloaded"
154 // UI instead and let the user know they have to restart the application
155 // for the changes to be applied.
156 if (um.activeUpdate && um.activeUpdate.state == "pending") {
157 prompter.showUpdateDownloaded(um.activeUpdate);
158 } else {
159 prompter.checkForUpdates();
160 }
161 }
162
163 /**
164 * Controls the update check menu item
165 */
sbUpdateItem
166 function sbUpdateItem()
167 {
168 var updateService = Components.classes["@mozilla.org/updates/update-service;1"]
169 .getService(Components.interfaces.nsIApplicationUpdateService);
170 var updateManager = Components.classes["@mozilla.org/updates/update-manager;1"]
171 .getService(Components.interfaces.nsIUpdateManager);
172
173 // Disable the UI if the update enabled pref has been locked by the
174 // administrator or if we cannot update for some other reason
175 var checkForUpdates = document.getElementById("checkForUpdates");
176 var canUpdate = updateService.canUpdate;
177 checkForUpdates.setAttribute("disabled", !canUpdate);
178 if (!canUpdate) {
179 return;
180 }
181
182 var activeUpdate = updateManager.activeUpdate;
183
184 // By default, show "Check for Updates..."
185 var key = "default";
186 if (activeUpdate) {
187 switch (activeUpdate.state) {
188 case "downloading":
189 // If we're downloading an update at present, show the text:
190 // "Downloading Sunbird x.x..." otherwise we're paused, and show
191 // "Resume Downloading Sunbird x.x..."
192 key = updateService.isDownloading ? "downloading" : "resume";
193 break;
194 case "pending":
195 // If we're waiting for the user to restart, show: "Apply Downloaded
196 // Updates Now..."
197 key = "pending";
198 break;
199 }
200 }
201
202 // If there's an active update, substitute its name into the label
203 // we show for this item, otherwise display a generic label.
204 if (activeUpdate && activeUpdate.name) {
205 checkForUpdates.label = calGetString("calendar", "updatesItem_" + key, [activeUpdate.name]);
206 } else {
207 checkForUpdates.label = calGetString("calendar", "updatesItem_" + key + "Fallback");
208 }
209
210 if (updateManager.activeUpdate && updateService.isDownloading) {
211 checkForUpdates.setAttribute("loading", "true");
212 }
213 else {
214 checkForUpdates.removeAttribute("loading");
215 }
216 }