!import
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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 the virtual folder properties dialog
16 *
17 * The Initial Developer of the Original Code is
18 * David Bienvenu.
19 * Portions created by the Initial Developer are Copyright (C) 2004
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * David Bienvenu <bienvenu@nventure.com>
24 * Scott MacGregor <mscott@mozilla.org>
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
39
40 var nsMsgSearchScope = Components.interfaces.nsMsgSearchScope;
41
42 var gDialog;
43 var gMailView = null;
44 var searchSessionContractID = "@mozilla.org/messenger/searchSession;1";
45 var msgWindow; // important, don't change the name of this variable. it's really a global used by commandglue.js
46 var gSearchTermSession; // really an in memory temporary filter we use to read in and write out the search terms
47 var gSearchFolderURIs = "";
48
onLoad
49 function onLoad()
50 {
51 var arguments = window.arguments[0];
52
53 gDialog = {};
54
55 gDialog.OKButton = document.documentElement.getButton("accept");
56
57 gDialog.nameField = document.getElementById("name");
58 gDialog.nameField.focus();
59
60 // call this when OK is pressed
61 msgWindow = arguments.msgWindow;
62
63 // pre select the folderPicker, based on what they selected in the folder pane
64 gDialog.picker = document.getElementById("msgNewFolderPicker");
65 MsgFolderPickerOnLoad("msgNewFolderPicker");
66
67 initializeSearchWidgets();
68 setSearchScope(nsMsgSearchScope.offlineMail);
69
70 if (arguments.editExistingFolder)
71 InitDialogWithVirtualFolder(arguments.preselectedURI);
72 else // we are creating a new virtual folder
73 {
74 // it is possible that we were given arguments to pre-fill the dialog with...
75 gSearchTermSession = Components.classes[searchSessionContractID].createInstance(Components.interfaces.nsIMsgSearchSession);
76
77 if (arguments.searchTerms) // then add them to our search session
78 {
79 var count = arguments.searchTerms.Count();
80 for (var searchIndex = 0; searchIndex < count; )
81 gSearchTermSession.appendTerm(arguments.searchTerms.QueryElementAt(searchIndex++, Components.interfaces.nsIMsgSearchTerm));
82 }
83 if (arguments.preselectedURI)
84 {
85 var folderToSearch = GetMsgFolderFromUri(arguments.preselectedURI, false);
86 SetFolderPicker(folderToSearch.parent ? folderToSearch.parent.URI : arguments.preselectedURI, "msgNewFolderPicker");
87
88 // if the passed in URI is not a server then pre-select it as the folder to search
89 if (!folderToSearch.isServer)
90 gSearchFolderURIs = arguments.preselectedURI;
91 }
92 if (arguments.newFolderName)
93 document.getElementById("name").value = arguments.newFolderName;
94 if (arguments.searchFolderURIs)
95 gSearchFolderURIs = arguments.searchFolderURIs;
96
97 setupSearchRows(gSearchTermSession.searchTerms);
98 doEnabling(); // we only need to disable/enable the OK button for new virtual folders
99 }
100
101 updateOnlineSearchState();
102 doSetOKCancel(onOK, onCancel);
103 }
104
setupSearchRows
105 function setupSearchRows(aSearchTerms)
106 {
107 if (aSearchTerms && aSearchTerms.Count() > 0)
108 initializeSearchRows(nsMsgSearchScope.offlineMail, aSearchTerms); // load the search terms for the folder
109 else
110 onMore(null);
111 }
112
updateOnlineSearchState
113 function updateOnlineSearchState()
114 {
115 var enableCheckbox = false;
116 var checkbox = document.getElementById('searchOnline');
117 // only enable the checkbox for selection, for online servers
118 var srchFolderUriArray = gSearchFolderURIs.split('|');
119 if (srchFolderUriArray[0])
120 {
121 var realFolderRes = GetResourceFromUri(srchFolderUriArray[0]);
122 var realFolder = realFolderRes.QueryInterface(Components.interfaces.nsIMsgFolder);
123 enableCheckbox = realFolder.server.offlineSupportLevel; // anything greater than 0 is an online server like IMAP or news
124 }
125
126 if (enableCheckbox)
127 checkbox.removeAttribute('disabled');
128 else
129 {
130 checkbox.setAttribute('disabled', true);
131 checkbox.checked = false;
132 }
133 }
134
InitDialogWithVirtualFolder
135 function InitDialogWithVirtualFolder(aVirtualFolderURI)
136 {
137 // when editing an existing folder, hide the folder picker that stores the parent location of the folder
138 document.getElementById("chooseFolderLocationRow").collapsed = true;
139 var folderNameField = document.getElementById("name");
140 folderNameField.disabled = true;
141
142 var msgFolder = GetMsgFolderFromUri(aVirtualFolderURI);
143 var msgDatabase = msgFolder.getMsgDatabase(msgWindow);
144 var dbFolderInfo = msgDatabase.dBFolderInfo;
145
146 gSearchFolderURIs = dbFolderInfo.getCharProperty("searchFolderUri");
147 var searchTermString = dbFolderInfo.getCharProperty("searchStr");
148 document.getElementById('searchOnline').checked = dbFolderInfo.getBooleanProperty("searchOnline", false);
149
150 // work around to get our search term string converted into a real array of search terms
151 var filterService = Components.classes["@mozilla.org/messenger/services/filters;1"].getService(Components.interfaces.nsIMsgFilterService);
152 var filterList = filterService.getTempFilterList(msgFolder);
153 gSearchTermSession = filterList.createFilter("temp");
154 filterList.parseCondition(gSearchTermSession, searchTermString);
155
156 setupSearchRows(gSearchTermSession.searchTerms);
157
158 // set the name of the folder
159 folderNameField.value = msgFolder.prettyName;
160
161 // update the window title based on the name of the saved search
162 var messengerBundle = document.getElementById("bundle_messenger");
163 document.title = messengerBundle.getFormattedString('editVirtualFolderPropertiesTitle', [msgFolder.prettyName]);
164 }
165
onOK
166 function onOK()
167 {
168 var name = gDialog.nameField.value;
169 var uri = gDialog.picker.getAttribute("uri");
170 var messengerBundle = document.getElementById("bundle_messenger");
171 var searchOnline = document.getElementById('searchOnline').checked;
172
173 if (!gSearchFolderURIs)
174 {
175 window.alert(messengerBundle.getString('alertNoSearchFoldersSelected'));
176 return false;
177 }
178
179 if (window.arguments[0].editExistingFolder)
180 {
181 // update the search terms
182 saveSearchTerms(gSearchTermSession.searchTerms, gSearchTermSession);
183 var searchTermString = getSearchTermString(gSearchTermSession.searchTerms);
184
185 var msgFolder = GetMsgFolderFromUri(window.arguments[0].preselectedURI);
186 var msgDatabase = msgFolder.getMsgDatabase(msgWindow);
187 var dbFolderInfo = msgDatabase.dBFolderInfo;
188
189 // set the view string as a property of the db folder info
190 // set the original folder name as well.
191 dbFolderInfo.setCharProperty("searchStr", searchTermString);
192 dbFolderInfo.setCharProperty("searchFolderUri", gSearchFolderURIs);
193 dbFolderInfo.setBooleanProperty("searchOnline", searchOnline);
194 msgDatabase.Close(true);
195
196 var accountManager = Components.classes["@mozilla.org/messenger/account-manager;1"].getService(Components.interfaces.nsIMsgAccountManager);
197 accountManager.saveVirtualFolders();
198
199 if (window.arguments[0].onOKCallback)
200 window.arguments[0].onOKCallback(msgFolder.URI);
201
202 }
203 else if (name && uri) // create a new virtual folder
204 {
205 // check to see if we already have a folder with the same name and alert the user if so...
206 var parentFolder = GetMsgFolderFromUri(uri);
207
208 // sanity check the name based on the logic used by nsMsgBaseUtils.cpp. It can't start with a '.', it can't end with a '.', '~' or ' '.
209 // it can't contain a ';' or '#'.
210 if (/^\.|[\.\~ ]$|[\;\#]/.test(name))
211 {
212 window.alert(messengerBundle.getString('folderCreationFailed'));
213 return false;
214 }
215 else if (parentFolder.containsChildNamed(name))
216 {
217 window.alert(messengerBundle.getString('folderExists'));
218 return false;
219 }
220
221 saveSearchTerms(gSearchTermSession.searchTerms, gSearchTermSession);
222 CreateVirtualFolder(name, parentFolder, gSearchFolderURIs, gSearchTermSession.searchTerms, searchOnline);
223 }
224
225 return true;
226 }
227
onCancel
228 function onCancel()
229 {
230 // close the window
231 return true;
232 }
233
doEnabling
234 function doEnabling()
235 {
236 if (gDialog.nameField.value && gDialog.picker.getAttribute("uri"))
237 {
238 if (gDialog.OKButton.disabled)
239 gDialog.OKButton.disabled = false;
240 }
241 else
242 gDialog.OKButton.disabled = true;
243 }
244
chooseFoldersToSearch
245 function chooseFoldersToSearch()
246 {
247 // if we have some search folders already, then root the folder picker dialog off the account
248 // for those folders. Otherwise fall back to the preselectedfolderURI which is the parent folder
249 // for this new virtual folder.
250 var srchFolderUriArray = gSearchFolderURIs.split('|');
251 var dialog = window.openDialog("chrome://messenger/content/virtualFolderListDialog.xul", "",
252 "chrome,titlebar,modal,centerscreen,resizable",
253 {searchFolderURIs:gSearchFolderURIs,
254 okCallback:onFolderListDialogCallback});
255 }
256
257 // callback routine from chooseFoldersToSearch
onFolderListDialogCallback
258 function onFolderListDialogCallback(searchFolderURIs)
259 {
260 gSearchFolderURIs = searchFolderURIs;
261 updateOnlineSearchState(); // we may have changed the server type we are searching...
262 }
263
onEnterInSearchTerm
264 function onEnterInSearchTerm()
265 {
266 // stub function called by the core search widget code...
267 // nothing for us to do here
268 }