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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
|
/* -*- indent-tabs-mode: nil; js-indent-level: 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/. */
const { MailUtils } = ChromeUtils.import("resource:///modules/MailUtils.js");
const {PluralForm} = ChromeUtils.import("resource://gre/modules/PluralForm.jsm");
var searchSessionContractID = "@mozilla.org/messenger/searchSession;1";
var gDBView;
var gSearchSession;
var gMsgFolderSelected;
var nsIMsgFolder = Ci.nsIMsgFolder;
var nsIMsgWindow = Ci.nsIMsgWindow;
var nsMsgSearchScope = Ci.nsMsgSearchScope;
var gFolderPicker;
var gStatusBar = null;
var gStatusFeedback = new nsMsgStatusFeedback();
var gMessengerBundle = null;
var RDF;
var gSearchBundle;
var gNextMessageViewIndexAfterDelete = -2;
// Datasource search listener -- made global as it has to be registered
// and unregistered in different functions.
var gDataSourceSearchListener;
var gViewSearchListener;
var gSearchStopButton;
// Controller object for search results thread pane
var nsSearchResultsController =
{
supportsCommand: function(command)
{
switch(command) {
case "cmd_openMessage":
case "cmd_delete":
case "cmd_shiftDelete":
case "button_delete":
case "file_message_button":
case "goto_folder_button":
case "saveas_vf_button":
case "cmd_selectAll":
case "cmd_markAsRead":
case "cmd_markAsUnread":
case "cmd_markAsFlagged":
return true;
default:
return false;
}
},
// this controller only handles commands
// that rely on items being selected in
// the search results pane.
isCommandEnabled: function(command)
{
var enabled = true;
switch (command) {
case "goto_folder_button":
if (GetNumSelectedMessages() != 1)
enabled = false;
break;
case "cmd_delete":
case "cmd_shiftDelete":
case "button_delete":
// this assumes that advanced searches don't cross accounts
if (GetNumSelectedMessages() <= 0)
enabled = false;
break;
case "saveas_vf_button":
// need someway to see if there are any search criteria...
return true;
case "cmd_selectAll":
return GetDBView() != null;
default:
if (GetNumSelectedMessages() <= 0)
enabled = false;
break;
}
return enabled;
},
doCommand: function(command)
{
switch(command) {
case "cmd_openMessage":
MsgOpenSelectedMessages();
return true;
case "cmd_delete":
case "button_delete":
MsgDeleteSelectedMessages(nsMsgViewCommandType.deleteMsg);
return true;
case "cmd_shiftDelete":
MsgDeleteSelectedMessages(nsMsgViewCommandType.deleteNoTrash);
return true;
case "goto_folder_button":
GoToFolder();
return true;
case "saveas_vf_button":
saveAsVirtualFolder();
return true;
case "cmd_selectAll":
// move the focus to the search results pane
GetThreadTree().focus();
GetDBView().doCommand(nsMsgViewCommandType.selectAll)
return true;
case "cmd_markAsRead":
MsgMarkMsgAsRead(true);
return true;
case "cmd_markAsUnread":
MsgMarkMsgAsRead(false);
return true;
case "cmd_markAsFlagged":
MsgMarkAsFlagged();
return true;
default:
return false;
}
},
onEvent: function(event)
{
}
}
function UpdateMailSearch(caller)
{
//dump("XXX update mail-search " + caller + "\n");
document.commandDispatcher.updateCommands('mail-search');
}
function SetAdvancedSearchStatusText(aNumHits)
{
var statusMsg;
// if there are no hits, it means no matches were found in the search.
if (aNumHits == 0)
{
statusMsg = gSearchBundle.getString("noMatchesFound");
}
else
{
statusMsg = PluralForm.get(aNumHits,
gSearchBundle.getString("matchesFound"));
statusMsg = statusMsg.replace("#1", aNumHits);
}
gStatusFeedback.showStatusString(statusMsg);
}
// nsIMsgSearchNotify object
var gSearchNotificationListener =
{
onSearchHit: function(header, folder)
{
// XXX TODO
// update status text?
},
onSearchDone: function(status)
{
gSearchStopButton.setAttribute("label", gSearchBundle.getString("labelForSearchButton"));
gSearchStopButton.setAttribute("accesskey", gSearchBundle.getString("labelForSearchButton.accesskey"));
gStatusFeedback._stopMeteors();
SetAdvancedSearchStatusText(gDBView.QueryInterface(Ci.nsITreeView).rowCount);
},
onNewSearch: function()
{
gSearchStopButton.setAttribute("label", gSearchBundle.getString("labelForStopButton"));
gSearchStopButton.setAttribute("accesskey", gSearchBundle.getString("labelForStopButton.accesskey"));
UpdateMailSearch("new-search");
gStatusFeedback._startMeteors();
gStatusFeedback.showStatusString(gSearchBundle.getString("searchingMessage"));
}
}
// the folderListener object
var gFolderListener = {
onFolderAdded: function(parentFolder, child) {},
onMessageAdded: function(parentFolder, msg) {},
onFolderRemoved: function(parentFolder, child) {},
onMessageRemoved: function(parentFolder, msg) {},
onFolderPropertyChanged: function(item, property, oldValue, newValue) {},
onFolderIntPropertyChanged: function(item, property, oldValue, newValue) {},
onFolderBoolPropertyChanged: function(item, property, oldValue, newValue) {},
onFolderUnicharPropertyChanged: function(item, property, oldValue, newValue){},
onFolderPropertyFlagChanged: function(item, property, oldFlag, newFlag) {},
onFolderEvent: function(folder, event) {
if (event == "DeleteOrMoveMsgCompleted") {
HandleDeleteOrMoveMessageCompleted(folder);
}
else if (event == "DeleteOrMoveMsgFailed") {
HandleDeleteOrMoveMessageFailed(folder);
}
}
}
function HideSearchColumn(id)
{
var col = document.getElementById(id);
if (col) {
col.setAttribute("hidden","true");
col.setAttribute("ignoreincolumnpicker","true");
}
}
function ShowSearchColumn(id)
{
var col = document.getElementById(id);
if (col) {
col.removeAttribute("hidden");
col.removeAttribute("ignoreincolumnpicker");
}
}
function searchOnLoad()
{
setHelpFileURI("chrome://communicator/locale/help/suitehelp.rdf");
initializeSearchWidgets();
initializeSearchWindowWidgets();
messenger = Cc["@mozilla.org/messenger;1"]
.createInstance(Ci.nsIMessenger);
gSearchBundle = document.getElementById("bundle_search");
gSearchStopButton.setAttribute("label", gSearchBundle.getString("labelForSearchButton"));
gSearchStopButton.setAttribute("accesskey", gSearchBundle.getString("labelForSearchButton.accesskey"));
gMessengerBundle = document.getElementById("bundle_messenger");
setupDatasource();
setupSearchListener();
if (window.arguments && window.arguments[0])
selectFolder(window.arguments[0].folder);
onMore(null);
UpdateMailSearch("onload");
// hide and remove these columns from the column picker. you can't thread search results
HideSearchColumn("threadCol"); // since you can't thread search results
HideSearchColumn("totalCol"); // since you can't thread search results
HideSearchColumn("unreadCol"); // since you can't thread search results
HideSearchColumn("unreadButtonColHeader");
HideSearchColumn("idCol");
HideSearchColumn("junkStatusCol");
HideSearchColumn("accountCol");
// we want to show the location column for search
ShowSearchColumn("locationCol");
}
function searchOnUnload()
{
// unregister listeners
gSearchSession.unregisterListener(gViewSearchListener);
gSearchSession.unregisterListener(gSearchNotificationListener);
MailServices.mailSession.RemoveFolderListener(gFolderListener);
if (gDBView)
{
gDBView.close();
gDBView = null;
}
top.controllers.removeController(nsSearchResultsController);
// release this early because msgWindow holds a weak reference
msgWindow.rootDocShell = null;
}
function initializeSearchWindowWidgets()
{
gFolderPicker = document.getElementById("searchableFolders");
gSearchStopButton = document.getElementById("search-button");
gStatusBar = document.getElementById('statusbar-icon');
hideMatchAllItem();
msgWindow = Cc["@mozilla.org/messenger/msgwindow;1"]
.createInstance(nsIMsgWindow);
msgWindow.domWindow = window;
msgWindow.rootDocShell.allowAuth = true;
msgWindow.rootDocShell.appType = Ci.nsIDocShell.APP_TYPE_MAIL;
msgWindow.statusFeedback = gStatusFeedback;
// functionality to enable/disable buttons using nsSearchResultsController
// depending of whether items are selected in the search results thread pane.
top.controllers.insertControllerAt(0, nsSearchResultsController);
}
function onSearchStop() {
gSearchSession.interruptSearch();
}
function onResetSearch(event) {
onReset(event);
var tree = GetThreadTree();
tree.treeBoxObject.view = null;
gStatusFeedback.showStatusString("");
}
function selectFolder(folder)
{
var folderURI;
// if we can't search messages on this folder, just select the first one
if (!folder || !folder.server.canSearchMessages ||
(folder.flags & Ci.nsMsgFolderFlags.Virtual)) {
// find first item in our folder picker menu list
folderURI = gFolderPicker.firstChild.tree.builderView.getResourceAtIndex(0).Value;
} else {
folderURI = folder.URI;
}
updateSearchFolderPicker(folderURI);
}
function updateSearchFolderPicker(folderURI)
{
SetFolderPicker(folderURI, gFolderPicker.id);
// use the URI to get the real folder
gMsgFolderSelected = MailUtils.getFolderForURI(folderURI);
var searchSubFolders = document.getElementById("checkSearchSubFolders");
if (searchSubFolders)
searchSubFolders.disabled = !gMsgFolderSelected.hasSubFolders;
var searchLocalSystem = document.getElementById("menuSearchLocalSystem");
if (searchLocalSystem)
searchLocalSystem.disabled = gMsgFolderSelected.server.searchScope == nsMsgSearchScope.offlineMail;
setSearchScope(GetScopeForFolder(gMsgFolderSelected));
}
function updateSearchLocalSystem()
{
setSearchScope(GetScopeForFolder(gMsgFolderSelected));
}
function UpdateAfterCustomHeaderChange()
{
updateSearchAttributes();
}
function onChooseFolder(event) {
var folderURI = event.id;
if (folderURI) {
updateSearchFolderPicker(folderURI);
}
}
function onEnterInSearchTerm()
{
// on enter
// if not searching, start the search
// if searching, stop and then start again
if (gSearchStopButton.getAttribute("label") == gSearchBundle.getString("labelForSearchButton")) {
onSearch();
}
else {
onSearchStop();
onSearch();
}
}
function onSearch()
{
// set the view. do this on every search, to
// allow the tree to reset itself
var treeView = gDBView.QueryInterface(Ci.nsITreeView);
if (treeView)
{
var tree = GetThreadTree();
tree.treeBoxObject.view = treeView;
}
gSearchSession.clearScopes();
// tell the search session what the new scope is
if (!gMsgFolderSelected.isServer && !gMsgFolderSelected.noSelect)
gSearchSession.addScopeTerm(GetScopeForFolder(gMsgFolderSelected),
gMsgFolderSelected);
var searchSubfolders = document.getElementById("checkSearchSubFolders").checked;
if (gMsgFolderSelected && (searchSubfolders || gMsgFolderSelected.isServer || gMsgFolderSelected.noSelect))
{
AddSubFolders(gMsgFolderSelected);
}
// reflect the search widgets back into the search session
gSearchSession.searchTerms = saveSearchTerms(gSearchSession.searchTerms, gSearchSession);
try
{
gSearchSession.search(msgWindow);
}
catch(ex)
{
dump("Search Exception\n");
}
// refresh the tree after the search starts, because initiating the
// search will cause the datasource to clear itself
}
function AddSubFolders(folder) {
for (let nextFolder of folder.subFolders) {
if (!(nextFolder.flags & Ci.nsMsgFolderFlags.Virtual))
{
if (!nextFolder.noSelect)
gSearchSession.addScopeTerm(GetScopeForFolder(nextFolder), nextFolder);
AddSubFolders(nextFolder);
}
}
}
function AddSubFoldersToURI(folder)
{
var returnString = "";
for (let nextFolder of folder.subFolders) {
{
if (!(nextFolder.flags & Ci.nsMsgFolderFlags.Virtual))
{
if (!nextFolder.noSelect && !nextFolder.isServer)
{
if (returnString.length > 0)
returnString += '|';
returnString += nextFolder.URI;
}
var subFoldersString = AddSubFoldersToURI(nextFolder);
if (subFoldersString.length > 0)
{
if (returnString.length > 0)
returnString += '|';
returnString += subFoldersString;
}
}
}
return returnString;
}
function GetScopeForFolder(folder)
{
var searchLocalSystem = document.getElementById("menuSearchLocalSystem");
return searchLocalSystem && searchLocalSystem.value == "local" ?
nsMsgSearchScope.offlineMail :
folder.server.searchScope;
}
var nsMsgViewSortType = Ci.nsMsgViewSortType;
var nsMsgViewSortOrder = Ci.nsMsgViewSortOrder;
var nsMsgViewFlagsType = Ci.nsMsgViewFlagsType;
var nsMsgViewCommandType = Ci.nsMsgViewCommandType;
function goUpdateSearchItems(commandset)
{
for (var i = 0; i < commandset.childNodes.length; i++)
{
var commandID = commandset.childNodes[i].getAttribute("id");
if (commandID)
{
goUpdateCommand(commandID);
}
}
}
function nsMsgSearchCommandUpdater()
{}
nsMsgSearchCommandUpdater.prototype =
{
updateCommandStatus : function()
{
// the back end is smart and is only telling us to update command status
// when the # of items in the selection has actually changed.
document.commandDispatcher.updateCommands('mail-search');
},
displayMessageChanged : function(aFolder, aSubject, aKeywords)
{
},
updateNextMessageAfterDelete : function()
{
SetNextMessageAfterDelete();
},
summarizeSelection: function() {return false},
QueryInterface : function(iid)
{
if (iid.equals(Ci.nsIMsgDBViewCommandUpdater) ||
iid.equals(Ci.nsISupports))
return this;
throw Cr.NS_NOINTERFACE;
}
}
function setupDatasource() {
gDBView = Cc["@mozilla.org/messenger/msgdbview;1?type=search"]
.createInstance(Ci.nsIMsgDBView);
var count = new Object;
var cmdupdator = new nsMsgSearchCommandUpdater();
gDBView.init(messenger, msgWindow, cmdupdator);
gDBView.open(null, nsMsgViewSortType.byId, nsMsgViewSortOrder.ascending, nsMsgViewFlagsType.kNone, count);
// the thread pane needs to use the search datasource (to get the
// actual list of messages) and the message datasource (to get any
// attributes about each message)
gSearchSession = Cc[searchSessionContractID].createInstance(Ci.nsIMsgSearchSession);
var nsIFolderListener = Ci.nsIFolderListener;
var notifyFlags = nsIFolderListener.event;
MailServices.mailSession.AddFolderListener(gFolderListener, notifyFlags);
// the datasource is a listener on the search results
gViewSearchListener = gDBView.QueryInterface(Ci.nsIMsgSearchNotify);
gSearchSession.registerListener(gViewSearchListener);
}
function setupSearchListener()
{
// Setup the javascript object as a listener on the search results
gSearchSession.registerListener(gSearchNotificationListener);
}
// used to toggle functionality for Search/Stop button.
function onSearchButton(event)
{
if (event.target.label == gSearchBundle.getString("labelForSearchButton"))
onSearch();
else
onSearchStop();
}
// Stuff after this is implemented to make the thread pane work.
function GetNumSelectedMessages()
{
try {
return gDBView.numSelected;
}
catch (ex) {
return 0;
}
}
function GetDBView()
{
return gDBView;
}
function MsgDeleteSelectedMessages(aCommandType)
{
SetNextMessageAfterDelete();
gDBView.doCommand(aCommandType);
}
function SetNextMessageAfterDelete()
{
gNextMessageViewIndexAfterDelete = gDBView.msgToSelectAfterDelete;
}
function HandleDeleteOrMoveMessageFailed(folder)
{
gDBView.onDeleteCompleted(false);
gNextMessageViewIndexAfterDelete = -2;
}
function HandleDeleteOrMoveMessageCompleted(folder)
{
gDBView.onDeleteCompleted(true);
var treeView = gDBView.QueryInterface(Ci.nsITreeView);
var treeSelection = treeView.selection;
var viewSize = treeView.rowCount;
if (gNextMessageViewIndexAfterDelete == -2) {
// a move or delete can cause our selection can change underneath us.
// this can happen when the user
// deletes message from the stand alone msg window
// or the three pane
if (!treeSelection) {
// this can happen if you open the search window
// and before you do any searches
// and you do delete from another mail window
return;
}
else if (treeSelection.count == 0) {
// this can happen if you double clicked a message
// in the thread pane, and deleted it from the stand alone msg window
// see bug #185147
treeSelection.clearSelection();
UpdateMailSearch("delete from another view, 0 rows now selected");
}
else if (treeSelection.count == 1) {
// this can happen if you had two messages selected
// in the search results pane, and you deleted one of them from another view
// (like the view in the stand alone msg window or the three pane)
// since one item is selected, we should load it.
var startIndex = {};
var endIndex = {};
treeSelection.getRangeAt(0, startIndex, endIndex);
// select the selected item, so we'll load it
treeSelection.select(startIndex.value);
treeView.selectionChanged();
EnsureRowInThreadTreeIsVisible(startIndex.value);
UpdateMailSearch("delete from another view, 1 row now selected");
}
else {
// this can happen if you have more than 2 messages selected
// in the search results pane, and you deleted one of them from another view
// (like the view in the stand alone msg window or the three pane)
// since multiple messages are still selected, do nothing.
}
}
else {
if (gNextMessageViewIndexAfterDelete != nsMsgViewIndex_None && gNextMessageViewIndexAfterDelete >= viewSize)
{
if (viewSize > 0)
gNextMessageViewIndexAfterDelete = viewSize - 1;
else
{
gNextMessageViewIndexAfterDelete = nsMsgViewIndex_None;
// there is nothing to select since viewSize is 0
treeSelection.clearSelection();
UpdateMailSearch("delete from current view, 0 rows left");
}
}
// if we are about to set the selection with a new element then DON'T clear
// the selection then add the next message to select. This just generates
// an extra round of command updating notifications that we are trying to
// optimize away.
if (gNextMessageViewIndexAfterDelete != nsMsgViewIndex_None)
{
treeSelection.select(gNextMessageViewIndexAfterDelete);
// since gNextMessageViewIndexAfterDelete probably has the same value
// as the last index we had selected, the tree isn't generating a new
// selectionChanged notification for the tree view. So we aren't loading the
// next message. to fix this, force the selection changed update.
if (treeView)
treeView.selectionChanged();
EnsureRowInThreadTreeIsVisible(gNextMessageViewIndexAfterDelete);
// XXX TODO
// I think there is a bug in the suppression code above.
// what if I have two rows selected, and I hit delete,
// and so we load the next row.
// what if I have commands that only enable where
// exactly one row is selected?
UpdateMailSearch("delete from current view, at least one row selected");
}
}
// default value after delete/move/copy is over
gNextMessageViewIndexAfterDelete = -2;
// something might have been deleted, so update the status text
SetAdvancedSearchStatusText(viewSize);
}
function MoveMessageInSearch(destFolder)
{
if (destFolder._folder)
{
try {
SetNextMessageAfterDelete();
gDBView.doCommandWithFolder(nsMsgViewCommandType.moveMessages,
destFolder._folder);
}
catch (ex) {
dump("MoveMessageInSearch failed: " + ex + "\n");
}
}
}
function GoToFolder()
{
var hdr = gDBView.hdrForFirstSelectedMessage;
MsgOpenNewWindowForFolder(hdr.folder.URI, hdr.messageKey);
}
function saveAsVirtualFolder()
{
let searchFolderURIs = window.arguments[0].folder.URI;
var searchSubfolders = document.getElementById("checkSearchSubFolders").checked;
if (gMsgFolderSelected && (searchSubfolders || gMsgFolderSelected.isServer || gMsgFolderSelected.noSelect))
{
var subFolderURIs = AddSubFoldersToURI(gMsgFolderSelected);
if (subFolderURIs.length > 0)
searchFolderURIs += '|' + subFolderURIs;
}
var dialog = window.openDialog("chrome://messenger/content/virtualFolderProperties.xul", "",
"chrome,titlebar,modal,centerscreen",
{folder:window.arguments[0].folder,
searchTerms:gSearchSession.searchTerms,
searchFolderURIs: searchFolderURIs});
}
function OnTagsChange()
{
// Dummy, called by RemoveAllMessageTags and ToggleMessageTag
}
|