summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/addrbook/content/abSearchDialog.js
blob: 694d17c12b186f6af215134450320abd407efa64 (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* 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/. */

/* import-globals-from ../../../../mailnews/addrbook/content/abResultsPane.js */
/* import-globals-from ../../../../mailnews/base/content/dateFormat.js */
/* import-globals-from ../../../../mailnews/search/content/searchTerm.js */
/* import-globals-from ../../../base/content/globalOverlay.js */
/* import-globals-from abCommon.js */

var { encodeABTermValue } = ChromeUtils.import(
  "resource:///modules/ABQueryUtils.jsm"
);
var { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);
var { PluralForm } = ChromeUtils.importESModule(
  "resource://gre/modules/PluralForm.sys.mjs"
);

var searchSessionContractID = "@mozilla.org/messenger/searchSession;1";
var gSearchSession;

var nsMsgSearchScope = Ci.nsMsgSearchScope;
var nsMsgSearchOp = Ci.nsMsgSearchOp;
var nsMsgSearchAttrib = Ci.nsMsgSearchAttrib;

var gStatusText;
var gSearchBundle;
var gAddressBookBundle;

var gSearchStopButton;
var gPropertiesCmd;
var gComposeCmd;
var gDeleteCmd;
var gSearchPhoneticName = "false";

var gSearchAbViewListener = {
  onSelectionChanged() {
    UpdateCardView();
  },
  onCountChanged(aTotal) {
    let statusText;
    if (aTotal == 0) {
      statusText = gAddressBookBundle.GetStringFromName("noMatchFound");
    } else {
      statusText = PluralForm.get(
        aTotal,
        gAddressBookBundle.GetStringFromName("matchesFound1")
      ).replace("#1", aTotal);
    }

    gStatusText.setAttribute("value", statusText);
  },
};

function searchOnLoad() {
  initializeSearchWidgets();
  initializeSearchWindowWidgets();

  gSearchBundle = Services.strings.createBundle(
    "chrome://messenger/locale/search.properties"
  );
  gSearchStopButton.setAttribute(
    "label",
    gSearchBundle.GetStringFromName("labelForSearchButton")
  );
  gSearchStopButton.setAttribute(
    "accesskey",
    gSearchBundle.GetStringFromName("labelForSearchButton.accesskey")
  );
  gAddressBookBundle = Services.strings.createBundle(
    "chrome://messenger/locale/addressbook/addressBook.properties"
  );
  gSearchSession = Cc[searchSessionContractID].createInstance(
    Ci.nsIMsgSearchSession
  );

  // initialize a flag for phonetic name search
  gSearchPhoneticName = Services.prefs.getComplexValue(
    "mail.addr_book.show_phonetic_fields",
    Ci.nsIPrefLocalizedString
  ).data;

  if (window.arguments && window.arguments[0]) {
    SelectDirectory(window.arguments[0].directory);
  } else {
    SelectDirectory(
      document.getElementById("abPopup-menupopup").firstElementChild.value
    );
  }

  onMore(null);
}

function searchOnUnload() {
  CloseAbView();
}

function disableCommands() {
  gPropertiesCmd.setAttribute("disabled", "true");
  gComposeCmd.setAttribute("disabled", "true");
  gDeleteCmd.setAttribute("disabled", "true");
}

function initializeSearchWindowWidgets() {
  gSearchStopButton = document.getElementById("search-button");
  gPropertiesCmd = document.getElementById("cmd_properties");
  gComposeCmd = document.getElementById("cmd_compose");
  gDeleteCmd = document.getElementById("cmd_deleteCard");
  gStatusText = document.getElementById("statusText");
  disableCommands();
  // matchAll doesn't make sense for address book search
  hideMatchAllItem();
}

function onSearchStop() {}

function onAbSearchReset(event) {
  disableCommands();
  CloseAbView();

  onReset(event);
  gStatusText.setAttribute("value", "");
}

function SelectDirectory(aURI) {
  // set popup with address book names
  let abPopup = document.getElementById("abPopup");
  if (abPopup) {
    if (aURI) {
      abPopup.value = aURI;
    } else {
      abPopup.selectedIndex = 0;
    }
  }

  setSearchScope(GetScopeForDirectoryURI(aURI));
}

function GetScopeForDirectoryURI(aURI) {
  let directory;
  if (aURI && aURI != "moz-abdirectory://?") {
    directory = MailServices.ab.getDirectory(aURI);
  }
  let booleanAnd = gSearchBooleanRadiogroup.selectedItem.value == "and";

  if (directory?.isRemote) {
    if (booleanAnd) {
      return nsMsgSearchScope.LDAPAnd;
    }
    return nsMsgSearchScope.LDAP;
  }

  if (booleanAnd) {
    return nsMsgSearchScope.LocalABAnd;
  }
  return nsMsgSearchScope.LocalAB;
}

function onEnterInSearchTerm() {
  // on enter
  // if not searching, start the search
  // if searching, stop and then start again
  if (
    gSearchStopButton.getAttribute("label") ==
    gSearchBundle.GetStringFromName("labelForSearchButton")
  ) {
    onSearch();
  } else {
    onSearchStop();
    onSearch();
  }
}

function onSearch() {
  gStatusText.setAttribute("value", "");
  disableCommands();

  gSearchSession.clearScopes();

  var currentAbURI = document.getElementById("abPopup").getAttribute("value");

  gSearchSession.addDirectoryScopeTerm(GetScopeForDirectoryURI(currentAbURI));
  gSearchSession.searchTerms = saveSearchTerms(
    gSearchSession.searchTerms,
    gSearchSession
  );

  let searchUri = "?(";
  for (let i = 0; i < gSearchSession.searchTerms.length; i++) {
    let searchTerm = gSearchSession.searchTerms[i];
    if (!searchTerm.value.str) {
      continue;
    }
    // get the "and" / "or" value from the first term
    if (i == 0) {
      if (searchTerm.booleanAnd) {
        searchUri += "and";
      } else {
        searchUri += "or";
      }
    }

    var attrs;

    switch (searchTerm.attrib) {
      case nsMsgSearchAttrib.Name:
        if (gSearchPhoneticName != "true") {
          attrs = [
            "DisplayName",
            "FirstName",
            "LastName",
            "NickName",
            "_AimScreenName",
          ];
        } else {
          attrs = [
            "DisplayName",
            "FirstName",
            "LastName",
            "NickName",
            "_AimScreenName",
            "PhoneticFirstName",
            "PhoneticLastName",
          ];
        }
        break;
      case nsMsgSearchAttrib.DisplayName:
        attrs = ["DisplayName"];
        break;
      case nsMsgSearchAttrib.Email:
        attrs = ["PrimaryEmail"];
        break;
      case nsMsgSearchAttrib.PhoneNumber:
        attrs = [
          "HomePhone",
          "WorkPhone",
          "FaxNumber",
          "PagerNumber",
          "CellularNumber",
        ];
        break;
      case nsMsgSearchAttrib.Organization:
        attrs = ["Company"];
        break;
      case nsMsgSearchAttrib.Department:
        attrs = ["Department"];
        break;
      case nsMsgSearchAttrib.City:
        attrs = ["WorkCity"];
        break;
      case nsMsgSearchAttrib.Street:
        attrs = ["WorkAddress"];
        break;
      case nsMsgSearchAttrib.Nickname:
        attrs = ["NickName"];
        break;
      case nsMsgSearchAttrib.WorkPhone:
        attrs = ["WorkPhone"];
        break;
      case nsMsgSearchAttrib.HomePhone:
        attrs = ["HomePhone"];
        break;
      case nsMsgSearchAttrib.Fax:
        attrs = ["FaxNumber"];
        break;
      case nsMsgSearchAttrib.Pager:
        attrs = ["PagerNumber"];
        break;
      case nsMsgSearchAttrib.Mobile:
        attrs = ["CellularNumber"];
        break;
      case nsMsgSearchAttrib.Title:
        attrs = ["JobTitle"];
        break;
      case nsMsgSearchAttrib.AdditionalEmail:
        attrs = ["SecondEmail"];
        break;
      case nsMsgSearchAttrib.ScreenName:
        attrs = ["_AimScreenName"];
        break;
      default:
        dump("XXX " + searchTerm.attrib + " not a supported search attr!\n");
        attrs = ["DisplayName"];
        break;
    }

    var opStr;

    switch (searchTerm.op) {
      case nsMsgSearchOp.Contains:
        opStr = "c";
        break;
      case nsMsgSearchOp.DoesntContain:
        opStr = "!c";
        break;
      case nsMsgSearchOp.Is:
        opStr = "=";
        break;
      case nsMsgSearchOp.Isnt:
        opStr = "!=";
        break;
      case nsMsgSearchOp.BeginsWith:
        opStr = "bw";
        break;
      case nsMsgSearchOp.EndsWith:
        opStr = "ew";
        break;
      case nsMsgSearchOp.SoundsLike:
        opStr = "~=";
        break;
      default:
        opStr = "c";
        break;
    }

    // currently, we can't do "and" and "or" searches at the same time
    // (it's either all "and"s or all "or"s)
    var max_attrs = attrs.length;

    for (var j = 0; j < max_attrs; j++) {
      // append the term(s) to the searchUri
      searchUri +=
        "(" +
        attrs[j] +
        "," +
        opStr +
        "," +
        encodeABTermValue(searchTerm.value.str) +
        ")";
    }
  }

  searchUri += ")";
  if (searchUri == "?()") {
    // Empty search.
    searchUri = "";
  }
  SetAbView(currentAbURI, searchUri, "");
}

// used to toggle functionality for Search/Stop button.
function onSearchButton(event) {
  if (
    event.target.label ==
    gSearchBundle.GetStringFromName("labelForSearchButton")
  ) {
    onSearch();
  } else {
    onSearchStop();
  }
}

function GetAbViewListener() {
  return gSearchAbViewListener;
}

function onProperties() {
  if (!gPropertiesCmd.hasAttribute("disabled")) {
    window.opener.toAddressBook({ action: "display", card: GetSelectedCard() });
  }
}

function onCompose() {
  if (!gComposeCmd.hasAttribute("disabled")) {
    AbNewMessage();
  }
}

function onDelete() {
  if (!gDeleteCmd.hasAttribute("disabled")) {
    AbDelete();
  }
}

function AbResultsPaneKeyPress(event) {
  switch (event.keyCode) {
    case KeyEvent.DOM_VK_RETURN:
      onProperties();
      break;
    case KeyEvent.DOM_VK_DELETE:
    case KeyEvent.DOM_VK_BACK_SPACE:
      onDelete();
  }
}

function AbResultsPaneDoubleClick(card) {
  // Kept for abResultsPane.js.
}

function UpdateCardView() {
  disableCommands();
  let numSelected = GetNumSelectedCards();

  if (!numSelected) {
    return;
  }

  if (MailServices.accounts.allIdentities.length > 0) {
    gComposeCmd.removeAttribute("disabled");
  }

  gDeleteCmd.removeAttribute("disabled");
  if (numSelected == 1) {
    gPropertiesCmd.removeAttribute("disabled");
  }
}