toolkit/profile/content/profileSelection.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:59077696e2e7
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 Components.utils.import("resource://gre/modules/Services.jsm");
8
9 const C = Components.classes;
10 const I = Components.interfaces;
11
12 const ToolkitProfileService = "@mozilla.org/toolkit/profile-service;1";
13
14 var gDialogParams;
15 var gProfileManagerBundle;
16 var gBrandBundle;
17 var gProfileService;
18
19 function startup()
20 {
21 try {
22 gDialogParams = window.arguments[0].
23 QueryInterface(I.nsIDialogParamBlock);
24
25 gProfileService = C[ToolkitProfileService].getService(I.nsIToolkitProfileService);
26
27 gProfileManagerBundle = document.getElementById("bundle_profileManager");
28 gBrandBundle = document.getElementById("bundle_brand");
29
30 document.documentElement.centerWindowOnScreen();
31
32 var profilesElement = document.getElementById("profiles");
33
34 var profileList = gProfileService.profiles;
35 while (profileList.hasMoreElements()) {
36 var profile = profileList.getNext().QueryInterface(I.nsIToolkitProfile);
37
38 var listitem = profilesElement.appendItem(profile.name, "");
39
40 var tooltiptext =
41 gProfileManagerBundle.getFormattedString("profileTooltip", [profile.name, profile.rootDir.path]);
42 listitem.setAttribute("tooltiptext", tooltiptext);
43 listitem.setAttribute("class", "listitem-iconic");
44 listitem.profile = profile;
45 try {
46 if (profile === gProfileService.selectedProfile) {
47 setTimeout(function(a) {
48 profilesElement.ensureElementIsVisible(a);
49 profilesElement.selectItem(a);
50 }, 0, listitem);
51 }
52 }
53 catch(e) { }
54 }
55
56 var autoSelectLastProfile = document.getElementById("autoSelectLastProfile");
57 autoSelectLastProfile.checked = gProfileService.startWithLastProfile;
58 profilesElement.focus();
59 }
60 catch(e) {
61 window.close();
62 throw (e);
63 }
64 }
65
66 function acceptDialog()
67 {
68 var appName = gBrandBundle.getString("brandShortName");
69
70 var profilesElement = document.getElementById("profiles");
71 var selectedProfile = profilesElement.selectedItem;
72 if (!selectedProfile) {
73 var pleaseSelectTitle = gProfileManagerBundle.getString("pleaseSelectTitle");
74 var pleaseSelect =
75 gProfileManagerBundle.getFormattedString("pleaseSelect", [appName]);
76 Services.prompt.alert(window, pleaseSelectTitle, pleaseSelect);
77
78 return false;
79 }
80
81 var profileLock;
82
83 try {
84 profileLock = selectedProfile.profile.lock({ value: null });
85 }
86 catch (e) {
87 if (!selectedProfile.profile.rootDir.exists()) {
88 var missingTitle = gProfileManagerBundle.getString("profileMissingTitle");
89 var missing =
90 gProfileManagerBundle.getFormattedString("profileMissing", [appName]);
91 Services.prompt.alert(window, missingTitle, missing);
92 return false;
93 }
94
95 var lockedTitle = gProfileManagerBundle.getString("profileLockedTitle");
96 var locked =
97 gProfileManagerBundle.getFormattedString("profileLocked2", [appName, selectedProfile.profile.name, appName]);
98 Services.prompt.alert(window, lockedTitle, locked);
99
100 return false;
101 }
102 gDialogParams.objects.insertElementAt(profileLock.nsIProfileLock, 0, false);
103
104 gProfileService.selectedProfile = selectedProfile.profile;
105 updateStartupPrefs();
106
107 gDialogParams.SetInt(0, 1);
108
109 gDialogParams.SetString(0, selectedProfile.profile.name);
110
111 return true;
112 }
113
114 function exitDialog()
115 {
116 updateStartupPrefs();
117
118 return true;
119 }
120
121 function updateStartupPrefs()
122 {
123 var autoSelectLastProfile = document.getElementById("autoSelectLastProfile");
124 gProfileService.startWithLastProfile = autoSelectLastProfile.checked;
125
126 /* Bug 257777 */
127 gProfileService.startOffline = document.getElementById("offlineState").checked;
128 }
129
130 // handle key event on listboxes
131 function onProfilesKey(aEvent)
132 {
133 switch( aEvent.keyCode )
134 {
135 case KeyEvent.DOM_VK_DELETE:
136 ConfirmDelete();
137 break;
138 case KeyEvent.DOM_VK_F2:
139 RenameProfile();
140 break;
141 }
142 }
143
144 function onProfilesDblClick(aEvent)
145 {
146 if(aEvent.target.localName == "listitem")
147 document.documentElement.acceptDialog();
148 }
149
150 // invoke the createProfile Wizard
151 function CreateProfileWizard()
152 {
153 window.openDialog('chrome://mozapps/content/profile/createProfileWizard.xul',
154 '', 'centerscreen,chrome,modal,titlebar', gProfileService);
155 }
156
157 /**
158 * Called from createProfileWizard to update the display.
159 */
160 function CreateProfile(aProfile)
161 {
162 var profilesElement = document.getElementById("profiles");
163
164 var listitem = profilesElement.appendItem(aProfile.name, "");
165
166 var tooltiptext =
167 gProfileManagerBundle.getFormattedString("profileTooltip", [aProfile.name, aProfile.rootDir.path]);
168 listitem.setAttribute("tooltiptext", tooltiptext);
169 listitem.setAttribute("class", "listitem-iconic");
170 listitem.profile = aProfile;
171
172 profilesElement.ensureElementIsVisible(listitem);
173 profilesElement.selectItem(listitem);
174 }
175
176 // rename the selected profile
177 function RenameProfile()
178 {
179 var profilesElement = document.getElementById("profiles");
180 var selectedItem = profilesElement.selectedItem;
181 if (!selectedItem) {
182 return false;
183 }
184
185 var selectedProfile = selectedItem.profile;
186
187 var oldName = selectedProfile.name;
188 var newName = {value: oldName};
189
190 var dialogTitle = gProfileManagerBundle.getString("renameProfileTitle");
191 var msg =
192 gProfileManagerBundle.getFormattedString("renameProfilePrompt", [oldName]);
193
194 if (Services.prompt.prompt(window, dialogTitle, msg, newName, null, {value:0})) {
195 newName = newName.value;
196
197 // User hasn't changed the profile name. Treat as if cancel was pressed.
198 if (newName == oldName)
199 return false;
200
201 try {
202 selectedProfile.name = newName;
203 }
204 catch (e) {
205 var alTitle = gProfileManagerBundle.getString("profileNameInvalidTitle");
206 var alMsg = gProfileManagerBundle.getFormattedString("profileNameInvalid", [newName]);
207 Services.prompt.alert(window, alTitle, alMsg);
208 return false;
209 }
210
211 selectedItem.label = newName;
212 var tiptext = gProfileManagerBundle.
213 getFormattedString("profileTooltip",
214 [newName, selectedProfile.rootDir.path]);
215 selectedItem.setAttribute("tooltiptext", tiptext);
216
217 return true;
218 }
219
220 return false;
221 }
222
223 function ConfirmDelete()
224 {
225 var deleteButton = document.getElementById("delbutton");
226 var profileList = document.getElementById( "profiles" );
227
228 var selectedItem = profileList.selectedItem;
229 if (!selectedItem) {
230 return false;
231 }
232
233 var selectedProfile = selectedItem.profile;
234 var deleteFiles = false;
235
236 if (selectedProfile.rootDir.exists()) {
237 var dialogTitle = gProfileManagerBundle.getString("deleteTitle");
238 var dialogText =
239 gProfileManagerBundle.getFormattedString("deleteProfileConfirm",
240 [selectedProfile.rootDir.path]);
241
242 var buttonPressed = Services.prompt.confirmEx(window, dialogTitle, dialogText,
243 (Services.prompt.BUTTON_TITLE_IS_STRING * Services.prompt.BUTTON_POS_0) +
244 (Services.prompt.BUTTON_TITLE_CANCEL * Services.prompt.BUTTON_POS_1) +
245 (Services.prompt.BUTTON_TITLE_IS_STRING * Services.prompt.BUTTON_POS_2),
246 gProfileManagerBundle.getString("dontDeleteFiles"),
247 null,
248 gProfileManagerBundle.getString("deleteFiles"),
249 null, {value:0});
250 if (buttonPressed == 1)
251 return false;
252
253 if (buttonPressed == 2)
254 deleteFiles = true;
255 }
256
257 selectedProfile.remove(deleteFiles);
258 profileList.removeChild(selectedItem);
259 if (profileList.firstChild != undefined) {
260 profileList.selectItem(profileList.firstChild);
261 }
262
263 return true;
264 }

mercurial