summaryrefslogtreecommitdiffstats
path: root/mailnews/base/content/virtualFolderProperties.js
blob: b9a984bd1de755b2515767b36d97259b56ea2a14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var gPickedFolder;
var gMailView = null;
var msgWindow; // important, don't change the name of this variable. it's really a global used by commandglue.js
var gSearchTermSession; // really an in memory temporary filter we use to read in and write out the search terms
var gSearchFolderURIs = "";
var gMessengerBundle = null;

var nsMsgSearchScope = Components.interfaces.nsMsgSearchScope;

Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/PluralForm.jsm");
Components.utils.import("resource:///modules/mailServices.js");
Components.utils.import("resource:///modules/virtualFolderWrapper.js");
Components.utils.import("resource:///modules/iteratorUtils.jsm");
Components.utils.import("resource:///modules/MailUtils.js");

function onLoad()
{
  var windowArgs = window.arguments[0];
  var acceptButton = document.documentElement.getButton("accept");

  gMessengerBundle = document.getElementById("bundle_messenger");

  // call this when OK is pressed
  msgWindow = windowArgs.msgWindow;

  initializeSearchWidgets();

  setSearchScope(nsMsgSearchScope.offlineMail);
  if (windowArgs.editExistingFolder)
  {
    acceptButton.label = 
        document.documentElement.getAttribute("editFolderAcceptButtonLabel");
    acceptButton.accesskey = 
        document.documentElement.getAttribute("editFolderAcceptButtonAccessKey");
    InitDialogWithVirtualFolder(windowArgs.folder);
  }
  else // we are creating a new virtual folder
  {
    acceptButton.label = 
        document.documentElement.getAttribute("newFolderAcceptButtonLabel");
    acceptButton.accesskey = 
        document.documentElement.getAttribute("newFolderAcceptButtonAccessKey");
    // it is possible that we were given arguments to pre-fill the dialog with...
    gSearchTermSession = Components.classes["@mozilla.org/messenger/searchSession;1"]
                                   .createInstance(Components.interfaces.nsIMsgSearchSession);

    if (windowArgs.searchTerms) // then add them to our search session
    {
      for (let searchTerm in fixIterator(windowArgs.searchTerms,
                                         Components.interfaces.nsIMsgSearchTerm))
        gSearchTermSession.appendTerm(searchTerm);
    }
    if (windowArgs.folder)
    {
      // pre select the folderPicker, based on what they selected in the folder pane
      gPickedFolder = windowArgs.folder;
      try {
        document.getElementById("msgNewFolderPopup").selectFolder(windowArgs.folder);
      } catch(ex) {
        document.getElementById("msgNewFolderPicker")
                .setAttribute("label", windowArgs.folder.prettyName);
      }

      // if the passed in URI is not a server then pre-select it as the folder to search
      if (!windowArgs.folder.isServer)
        gSearchFolderURIs = windowArgs.folder.URI;
    }

    let folderNameField = document.getElementById("name");
    folderNameField.hidden = false;
    folderNameField.focus();
    if (windowArgs.newFolderName)
      folderNameField.value = windowArgs.newFolderName;
    if (windowArgs.searchFolderURIs)
      gSearchFolderURIs = windowArgs.searchFolderURIs;

    setupSearchRows(gSearchTermSession.searchTerms);
    doEnabling(); // we only need to disable/enable the OK button for new virtual folders
  }

  if (typeof windowArgs.searchOnline != "undefined")
    document.getElementById('searchOnline').checked = windowArgs.searchOnline;
  updateOnlineSearchState();
  updateFoldersCount();
}

function setupSearchRows(aSearchTerms)
{
  if (aSearchTerms && aSearchTerms.Count() > 0)
    initializeSearchRows(nsMsgSearchScope.offlineMail, aSearchTerms); // load the search terms for the folder
  else
    onMore(null);
}

function updateOnlineSearchState()
{
  var enableCheckbox = false;
  var checkbox = document.getElementById('searchOnline');
  // only enable the checkbox for selection, for online servers
  var srchFolderUriArray = gSearchFolderURIs.split('|');
  if (srchFolderUriArray[0])
  {
    var realFolder = MailUtils.getFolderForURI(srchFolderUriArray[0]);
    enableCheckbox =  realFolder.server.offlineSupportLevel; // anything greater than 0 is an online server like IMAP or news
  }

  if (enableCheckbox)
    checkbox.removeAttribute('disabled');
  else
  {
    checkbox.setAttribute('disabled', true);
    checkbox.checked = false;
  }
}

function InitDialogWithVirtualFolder(aVirtualFolder)
{
  let virtualFolderWrapper =
    VirtualFolderHelper.wrapVirtualFolder(window.arguments[0].folder);

  // when editing an existing folder, hide the folder picker that stores the parent location of the folder
  document.getElementById("chooseFolderLocationRow").collapsed = true;
  let folderNameField = document.getElementById("existingName");
  folderNameField.hidden = false;

  gSearchFolderURIs = virtualFolderWrapper.searchFolderURIs;
  updateFoldersCount();
  document.getElementById('searchOnline').checked = virtualFolderWrapper.onlineSearch;
  gSearchTermSession = virtualFolderWrapper.searchTermsSession;

  setupSearchRows(gSearchTermSession.searchTerms);

  // set the name of the folder
  let folderBundle = document.getElementById("bundle_folder");
  let name = folderBundle.getFormattedString("verboseFolderFormat",
               [aVirtualFolder.prettyName, aVirtualFolder.server.prettyName]);
  folderNameField.setAttribute("label", name);
  // update the window title based on the name of the saved search
  document.title = gMessengerBundle.getFormattedString("editVirtualFolderPropertiesTitle",
                                                       [aVirtualFolder.prettyName]);
}

function onFolderPick(aEvent) {
  gPickedFolder = aEvent.target._folder;
  document.getElementById("msgNewFolderPopup")
          .selectFolder(gPickedFolder);
}

function onOK()
{
  var name = document.getElementById("name").value;
  var searchOnline = document.getElementById('searchOnline').checked;

  if (!gSearchFolderURIs)
  {
    Services.prompt.alert(window, null,
                          gMessengerBundle.getString('alertNoSearchFoldersSelected'));
    return false;
  }

  if (window.arguments[0].editExistingFolder)
  {
    // update the search terms
    saveSearchTerms(gSearchTermSession.searchTerms, gSearchTermSession);
    // save the settings
    let virtualFolderWrapper =
      VirtualFolderHelper.wrapVirtualFolder(window.arguments[0].folder);
    virtualFolderWrapper.searchTerms = gSearchTermSession.searchTerms;
    virtualFolderWrapper.searchFolders = gSearchFolderURIs;
    virtualFolderWrapper.onlineSearch = searchOnline;
    virtualFolderWrapper.cleanUpMessageDatabase();

    MailServices.accounts.saveVirtualFolders();

    if (window.arguments[0].onOKCallback)
      window.arguments[0].onOKCallback(virtualFolderWrapper.virtualFolder.URI);
    return true;
  }
  var uri = gPickedFolder.URI;
  if (name && uri) // create a new virtual folder
  {
    // check to see if we already have a folder with the same name and alert the user if so...
    var parentFolder = MailUtils.getFolderForURI(uri);

    // 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 ' '.
    // it can't contain a ';' or '#'.
    if (/^\.|[\.\~ ]$|[\;\#]/.test(name))
    {
      Services.prompt.alert(window, null,
                            gMessengerBundle.getString("folderCreationFailed"));
      return false;
    }
    else if (parentFolder.containsChildNamed(name))
    {
      Services.prompt.alert(window, null,
                            gMessengerBundle.getString("folderExists"));
      return false;
    }

    saveSearchTerms(gSearchTermSession.searchTerms, gSearchTermSession);
    VirtualFolderHelper.createNewVirtualFolder(name, parentFolder, gSearchFolderURIs,
                                               gSearchTermSession.searchTerms,
                                               searchOnline);
  }

  return true;
}

function doEnabling()
{
  var acceptButton = document.documentElement.getButton("accept");
  acceptButton.disabled = !document.getElementById("name").value;
}

function chooseFoldersToSearch()
{
  // if we have some search folders already, then root the folder picker dialog off the account
  // for those folders. Otherwise fall back to the preselectedfolderURI which is the parent folder
  // for this new virtual folder.
  var dialog = window.openDialog("chrome://messenger/content/virtualFolderListDialog.xul", "",
                                 "chrome,titlebar,modal,centerscreen,resizable",
                                 {searchFolderURIs:gSearchFolderURIs,
                                  okCallback:onFolderListDialogCallback});
}

// callback routine from chooseFoldersToSearch
function onFolderListDialogCallback(searchFolderURIs)
{
  gSearchFolderURIs = searchFolderURIs;
  updateFoldersCount();
  updateOnlineSearchState(); // we may have changed the server type we are searching...
}

function updateFoldersCount()
{
  let srchFolderUriArray = gSearchFolderURIs.split('|');
  let folderCount = gSearchFolderURIs ? srchFolderUriArray.length : 0;
  let foldersList = document.getElementById("chosenFoldersCount");
  foldersList.textContent =
    PluralForm.get(folderCount, gMessengerBundle.getString("virtualFolderSourcesChosen"))
              .replace("#1", folderCount);
  if (folderCount > 0) {
    let folderNames = [];
    for (let folderURI of srchFolderUriArray) {
      let folder = MailUtils.getFolderForURI(folderURI);
      let name = this.gMessengerBundle.getFormattedString("verboseFolderFormat",
        [folder.prettyName, folder.server.prettyName]);
      folderNames.push(name);
    }
    foldersList.setAttribute("tooltiptext", folderNames.join("\n"));
  } else {
    foldersList.removeAttribute("tooltiptext");
  }
}

function onEnterInSearchTerm()
{
  // stub function called by the core search widget code...
  // nothing for us to do here
}