This repository has been archived by the owner on Nov 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfolderdisplay.js
2762 lines (2489 loc) · 99.1 KB
/
folderdisplay.js
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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 commandglue.js */
/* import-globals-from mailWindow.js */
/* import-globals-from ../../extensions/mailviews/content/msgViewPickerOverlay.js */
var { DBViewWrapper } = ChromeUtils.import(
"resource:///modules/DBViewWrapper.jsm"
);
var { JSTreeSelection } = ChromeUtils.import(
"resource:///modules/jsTreeSelection.js"
);
var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
var { MailUtils } = ChromeUtils.import("resource:///modules/MailUtils.jsm");
var gFolderDisplay = null;
var gMessageDisplay = null;
/**
* Maintains a list of listeners for all FolderDisplayWidget instances in this
* window. The assumption is that because of our multiplexed tab
* implementation all consumers are effectively going to care about all such
* tabs.
*
* We are not just a global list so that we can add brains about efficiently
* building lists, provide try-wrapper convenience, etc.
*/
var FolderDisplayListenerManager = {
_listeners: [],
/**
* Register a listener that implements one or more of the methods defined on
* |IDBViewWrapperListener|. Note that a change from those interface
* signatures is that the first argument is always a reference to the
* FolderDisplayWidget generating the notification.
*
* We additionally support the following notifications:
* - onMakeActive. Invoked when makeActive is called on the
* FolderDisplayWidget. The second argument (after the folder display) is
* aWasInactive.
*
* - onActiveCreatedView. onCreatedView deferred to when the tab is actually
* made active.
*
* - onActiveMessagesLoaded. onMessagesLoaded deferred to when the
* tab is actually made active. Use this if the actions you need to take
* are based on the folder display actually being visible, such as updating
* some UI widget, etc. Not all messages may have been loaded, but some.
*
*/
registerListener(aListener) {
this._listeners.push(aListener);
},
/**
* Unregister a previously registered event listener.
*/
unregisterListener(aListener) {
let idx = this._listeners.indexOf(aListener);
if (idx >= 0) {
this._listeners.splice(idx, 1);
}
},
/**
* For use by FolderDisplayWidget to trigger listener invocation.
*/
_fireListeners(aEventName, aArgs) {
for (let listener of this._listeners) {
if (aEventName in listener) {
try {
listener[aEventName].apply(listener, aArgs);
} catch (e) {
Cu.reportError(
aEventName + " event listener FAILED; " + e + " at: " + e.stack
);
}
}
}
},
};
/**
* Abstraction for a widget that (roughly speaking) displays the contents of
* folders. The widget belongs to a tab and has a lifetime as long as the tab
* that contains it. This class is strictly concerned with the UI aspects of
* this; the DBViewWrapper class handles the view details (and is exposed on
* the 'view' attribute.)
*
* The search window subclasses this into the SearchFolderDisplayWidget rather
* than us attempting to generalize everything excessively. This is because
* we hate the search window and don't want to clutter up this code for it.
* The standalone message display window also subclasses us; we do not hate it,
* but it's not invited to our birthday party either.
* For reasons of simplicity and the original order of implementation, this
* class does alter its behavior slightly for the benefit of the standalone
* message window. If no tab info is provided, we avoid touching tabmail
* (which is good, because it won't exist!) And now we guard against treeBox
* manipulations...
*/
function FolderDisplayWidget(aTabInfo, aMessageDisplayWidget) {
this._tabInfo = aTabInfo;
// If the folder does not get handled by the DBViewWrapper, stash it here.
// ex: when isServer is true.
this._nonViewFolder = null;
this.view = new DBViewWrapper(this);
this.messageDisplay = aMessageDisplayWidget;
this.messageDisplay.folderDisplay = this;
/**
* The XUL tree node, as retrieved by getDocumentElementById. The caller is
* responsible for setting this.
*/
this.tree = null;
/**
* The nsIMsgWindow corresponding to the window that holds us. There is only
* one of these per tab. The caller is responsible for setting this.
*/
this.msgWindow = null;
/**
* The nsIMessenger instance that corresponds to our tab/window. We do not
* use this ourselves, but are responsible for using it to update the
* global |messenger| object so that our tab maintains its own undo and
* navigation history. At some point we might touch it for those reasons.
*/
this.messenger = null;
this.threadPaneCommandUpdater = this;
/**
* Flag to expose whether all messages are loaded or not. Set by
* onMessagesLoaded() when aAll is true.
*/
this._allMessagesLoaded = false;
/**
* Save the top row displayed when we go inactive, restore when we go active,
* nuke it when we destroy the view.
*/
this._savedFirstVisibleRow = null;
/** the next view index to select once the delete completes */
this._nextViewIndexAfterDelete = null;
/**
* Track when a mass move is in effect (we get told by hintMassMoveStarting,
* and hintMassMoveCompleted) so that we can avoid deletion-triggered
* moving to _nextViewIndexAfterDelete until the mass move completes.
*/
this._massMoveActive = false;
/**
* Track when a message is being deleted so we can respond appropriately.
*/
this._deleteInProgress = false;
/**
* Used by pushNavigation to queue a navigation request for when we enter the
* next folder; onMessagesLoaded(true) is the one that processes it.
*/
this._pendingNavigation = null;
this._active = false;
/**
* A list of methods to call on 'this' object when we are next made active.
* This list is populated by calls to |_notifyWhenActive| when we are
* not active at the moment.
*/
this._notificationsPendingActivation = [];
/**
* Create a fake tree object for if/when this folder is in the background.
* Hide the tree using CSS, because if it's not attached to the document or
* is hidden="true", it won't fire select events and stuff will break.
*/
this._fakeTree = document.createXULElement("tree");
this._fakeTree.setAttribute("style", "visibility: collapse");
this._fakeTree.appendChild(document.createXULElement("treechildren"));
document.documentElement.appendChild(this._fakeTree);
/**
* Create a fake tree selection for cases where we have opened a background
* tab. We'll get rid of this as soon as we've switched to the tab for the
* first time, and have a real tree selection.
*/
this._fakeTreeSelection = new JSTreeSelection(this._fakeTree);
this._mostRecentSelectionCounts = [];
this._mostRecentCurrentIndices = [];
}
FolderDisplayWidget.prototype = {
/**
* @return the currently displayed folder. This is just proxied from the
* view wrapper.
* @groupName Displayed
*/
get displayedFolder() {
return this._nonViewFolder || this.view.displayedFolder;
},
/**
* @return true if the selection should be summarized for this folder. This
* is based on the mail.operate_on_msgs_in_collapsed_threads pref and
* if we are in a newsgroup folder. XXX When bug 478167 is fixed, this
* should be limited to being disabled for newsgroups that are not stored
* offline.
*/
get summarizeSelectionInFolder() {
return (
Services.prefs.getBoolPref("mail.operate_on_msgs_in_collapsed_threads") &&
!(this.displayedFolder instanceof Ci.nsIMsgNewsFolder)
);
},
/**
* @return the nsITreeSelection object for our tree view. This exists for
* the benefit of message tabs that haven't been switched to yet.
* We provide a fake tree selection in those cases.
* @protected
*/
get treeSelection() {
// If we haven't switched to this tab yet, dbView will exist but
// dbView.selection won't, so use the fake tree selection instead.
if (this._fakeTreeSelection) {
return this._fakeTreeSelection;
}
if (this.view.dbView) {
return this.view.dbView.selection;
}
return null;
},
/**
* Determine which pane currently has focus (one of the folder pane, thread
* pane, or message pane). The message pane node is the common ancestor of
* the single- and multi-message content windows. When changing focus to the
* message pane, be sure to focus the appropriate content window in addition
* to the messagepanebox (doing both is required in order to blur the
* previously-focused chrome element).
*
* @return the focused pane
*/
get focusedPane() {
let panes = ["threadTree", "folderTree", "messagepanebox"].map(id =>
document.getElementById(id)
);
let currentNode = top.document.activeElement;
while (currentNode) {
if (panes.includes(currentNode)) {
return currentNode;
}
currentNode = currentNode.parentNode;
}
return null;
},
/**
* Number of headers to tell the message database to cache when we enter a
* folder. This value is being propagated from legacy code which provided
* no explanation for its choice.
*
* We definitely want the header cache size to be larger than the number of
* rows that can be displayed on screen simultaneously.
*
* @private
*/
PERF_HEADER_CACHE_SIZE: 100,
/**
* @name Selection Persistence
* @private
*/
// @{
/**
* An optional object, with the following properties:
* - messages: This is a list where each item is an object with the following
* attributes sufficient to re-establish the selected items even in the
* face of folder renaming.
* - messageId: The value of the message's message-id header.
*
* That's right, we only save the message-id header value. This is arguably
* overkill and ambiguous in the face of duplicate messages, but it's the
* most persistent/reliable thing we have without gloda.
* Using the view index was ruled out because it is hardly stable. Using the
* message key alone is insufficient for cross-folder searches. Using a
* folder identifier and message key is insufficient for local folders in the
* face of compaction, let alone complexities where the folder name may
* change due to renaming/moving. Which means we eventually need to fall
* back to message-id anyways. Feel free to add in lots of complexity if
* you actually write unit tests for all the many possible cases.
* Additional justification is that selection saving/restoration should not
* happen all that frequently. A nice freebie is that message-id is
* definitely persistable.
*
* - forceSelect: Whether we are allowed to drop all filters in our quest to
* select messages.
*/
_savedSelection: null,
/**
* Save the current view selection for when we the view is getting destroyed
* or otherwise re-ordered in such a way that the nsITreeSelection will lose
* track of things (because it just has a naive view-index 'view' of the
* world.) We just save each message's message-id header. This is overkill
* and ambiguous in the face of duplicate messages (and expensive to
* restore), but is also the most reliable option for this use case.
*/
_saveSelection() {
this._savedSelection = {
messages: this.selectedMessages.map(msgHdr => ({
messageId: msgHdr.messageId,
})),
forceSelect: false,
};
},
/**
* Clear the saved selection.
*/
_clearSavedSelection() {
this._savedSelection = null;
},
/**
* Restore the view selection if we have a saved selection. We must be
* active!
*
* @return true if we were able to restore the selection and there was
* a selection, false if there was no selection (anymore).
*/
_restoreSelection() {
if (!this._savedSelection || !this._active) {
return false;
}
// translate message IDs back to messages. this is O(s(m+n)) where:
// - s is the number of messages saved in the selection
// - m is the number of messages in the view (from findIndexOfMsgHdr)
// - n is the number of messages in the underlying folders (from
// DBViewWrapper.getMsgHdrForMessageID).
// which ends up being O(sn)
let messages = this._savedSelection.messages
.map(savedInfo => this.view.getMsgHdrForMessageID(savedInfo.messageId))
.filter(msgHdr => !!msgHdr);
this.selectMessages(messages, this._savedSelection.forceSelect, true);
this._savedSelection = null;
return this.selectedCount != 0;
},
/**
* Restore the last expandAll/collapseAll state, for both grouped and threaded
* views. Not all views respect viewFlags, ie single folder non-virtual.
*/
restoreThreadState() {
if (!this._active || !this.tree || !this.view.dbView.viewFolder) {
return;
}
if (
this.view._threadExpandAll &&
!(this.view.dbView.viewFlags & Ci.nsMsgViewFlagsType.kExpandAll)
) {
this.view.dbView.doCommand(Ci.nsMsgViewCommandType.expandAll);
}
if (
!this.view._threadExpandAll &&
this.view.dbView.viewFlags & Ci.nsMsgViewFlagsType.kExpandAll
) {
this.view.dbView.doCommand(Ci.nsMsgViewCommandType.collapseAll);
}
},
// @}
/**
* @name Columns
* @protected Folder Display
*/
// @{
/**
* The map of all stock sortable columns and their sortType. The key must
* match the column's xul <treecol> id.
*/
COLUMNS_MAP: new Map([
["accountCol", "byAccount"],
["attachmentCol", "byAttachments"],
["senderCol", "byAuthor"],
["correspondentCol", "byCorrespondent"],
["dateCol", "byDate"],
["flaggedCol", "byFlagged"],
["idCol", "byId"],
["junkStatusCol", "byJunkStatus"],
["locationCol", "byLocation"],
["priorityCol", "byPriority"],
["receivedCol", "byReceived"],
["recipientCol", "byRecipient"],
["sizeCol", "bySize"],
["statusCol", "byStatus"],
["subjectCol", "bySubject"],
["tagsCol", "byTags"],
["threadCol", "byThread"],
["unreadButtonColHeader", "byUnread"],
]),
/**
* The map of stock non-sortable columns. The key must match the column's
* xul <treecol> id.
*/
COLUMNS_MAP_NOSORT: new Set(["totalCol", "unreadCol"]),
/**
* The set of potential default columns in their default display order. Each
* column in this list is checked against |COLUMN_DEFAULT_TESTERS| to see if
* it is actually an appropriate default for the folder type.
*/
DEFAULT_COLUMNS: [
"threadCol",
"attachmentCol",
"flaggedCol",
"subjectCol",
"unreadButtonColHeader",
"senderCol", // news folders or incoming folders when correspondents not in use
"recipientCol", // outgoing folders when correspondents not in use
"correspondentCol", // mail folders
"junkStatusCol",
"dateCol",
"locationCol", // multiple-folder backed folders
],
/**
* Maps column ids to functions that test whether the column is a good default
* for display for the folder. Each function should expect a DBViewWrapper
* instance as its argument. The intent is that the various helper
* properties like isMailFolder/isIncomingFolder/isOutgoingFolder allow the
* constraint to be expressed concisely. If a helper does not exist, add
* one! (If doing so is out of reach, than access viewWrapper.displayedFolder
* to get at the nsIMsgFolder.)
* If a column does not have a function, it is assumed that it should be
* displayed by default.
*/
COLUMN_DEFAULT_TESTERS: {
correspondentCol(viewWrapper) {
if (Services.prefs.getBoolPref("mail.threadpane.use_correspondents")) {
// Don't show the correspondent for news or RSS where it doesn't make sense.
return viewWrapper.isMailFolder && !viewWrapper.isFeedFolder;
}
return false;
},
senderCol(viewWrapper) {
if (Services.prefs.getBoolPref("mail.threadpane.use_correspondents")) {
// Show the sender even if correspondent is enabled for news and feeds.
return viewWrapper.isNewsFolder || viewWrapper.isFeedFolder;
}
// senderCol = From. You only care in incoming folders.
return viewWrapper.isIncomingFolder;
},
recipientCol(viewWrapper) {
if (Services.prefs.getBoolPref("mail.threadpane.use_correspondents")) {
// No recipient column if we use correspondent.
return false;
}
// recipientCol = To. You only care in outgoing folders.
return viewWrapper.isOutgoingFolder;
},
// Only show the location column for non-single-folder results
locationCol(viewWrapper) {
return !viewWrapper.isSingleFolder;
},
// core UI does not provide an ability to mark newsgroup messages as spam
junkStatusCol(viewWrapper) {
return !viewWrapper.isNewsFolder;
},
},
/**
* The property name we use to store the column states on the
* dbFolderInfo.
*/
PERSISTED_COLUMN_PROPERTY_NAME: "columnStates",
/**
* Given a dbFolderInfo, extract the persisted state from it if there is any.
*
* @return null if there was no persisted state, the persisted state in object
* form otherwise. (Ideally the state conforms to the documentation on
* |_savedColumnStates| but we can't stop people from doing bad things.)
*/
_depersistColumnStatesFromDbFolderInfo(aDbFolderInfo) {
let columnJsonString = aDbFolderInfo.getCharProperty(
this.PERSISTED_COLUMN_PROPERTY_NAME
);
if (!columnJsonString) {
return null;
}
return JSON.parse(columnJsonString);
},
/**
* Persist the column state for the currently displayed folder. We are
* assuming that the message database is already open when we are called and
* therefore that we do not need to worry about cleaning up after the message
* database.
* The caller should only call this when they have reason to suspect that the
* column state has been changed. This could be because there was no
* persisted state so we figured out a default one and want to save it.
* Otherwise this should be because the user explicitly changed up the column
* configurations. You should not call this willy-nilly.
*
* @param aState State to persist.
*/
_persistColumnStates(aState) {
if (this.view.isSynthetic) {
let syntheticView = this.view._syntheticView;
if ("setPersistedSetting" in syntheticView) {
syntheticView.setPersistedSetting("columns", aState);
}
return;
}
if (!this.view.displayedFolder || !this.view.displayedFolder.msgDatabase) {
return;
}
let msgDatabase = this.view.displayedFolder.msgDatabase;
let dbFolderInfo = msgDatabase.dBFolderInfo;
dbFolderInfo.setCharProperty(
this.PERSISTED_COLUMN_PROPERTY_NAME,
JSON.stringify(aState)
);
msgDatabase.Commit(Ci.nsMsgDBCommitType.kLargeCommit);
},
/**
* Let us know that the state of the columns has changed. This is either due
* to a re-ordering or hidden-ness being toggled.
*
* This method should only be called on (the active) gFolderDisplay.
*/
hintColumnsChanged() {
// ignore this if we are the ones doing things
if (this._touchingColumns) {
return;
}
this._persistColumnStates(this.getColumnStates());
},
/**
* Either inherit the column state of another folder or use heuristics to
* figure out the best column state for the current folder.
*/
_getDefaultColumnsForCurrentFolder(aDoNotInherit) {
// If the view is synthetic, try asking it for its default columns. If it
// fails, just return nothing, since most synthetic views don't care about
// columns anyway.
if (this.view.isSynthetic) {
if ("getDefaultSetting" in this.view._syntheticView) {
return this.view._syntheticView.getDefaultSetting("columns");
}
return {};
}
// do not inherit from the inbox if:
// - It's an outgoing folder; these have a different use-case and there
// should be a small number of these, so it's okay to have no defaults.
// - It's a virtual folder (single or multi-folder backed). Who knows what
// the intent of the user is in this case. This should also be bounded
// in number and our default heuristics should be pretty good.
// - It's a multiple folder; this is either a search view (which has no
// displayed folder) or a virtual folder (which we eliminated above).
// - News folders. There is no inbox so there's nothing to inherit from.
// (Although we could try and see if they have opened any other news
// folders in the same account. But it's not all that important to us.)
// - It's an inbox!
let doNotInherit =
aDoNotInherit ||
this.view.isOutgoingFolder ||
this.view.isVirtual ||
this.view.isMultiFolder ||
this.view.isNewsFolder ||
this.displayedFolder.getFlag(Ci.nsMsgFolderFlags.Inbox);
// Try and grab the inbox for this account's settings. we may not be able
// to, in which case we just won't inherit. (It ends up the same since the
// inbox is obviously not customized in this case.)
if (!doNotInherit) {
let inboxFolder = this.displayedFolder.rootFolder.getFolderWithFlags(
Ci.nsMsgFolderFlags.Inbox
);
if (inboxFolder) {
let state = this._depersistColumnStatesFromDbFolderInfo(
inboxFolder.msgDatabase.dBFolderInfo
);
// inbox message databases don't get closed as a matter of policy.
if (state) {
return state;
}
}
}
// if we are still here, use the defaults and helper functions
let state = {};
for (let colId of this.DEFAULT_COLUMNS) {
let shouldShowColumn = true;
if (colId in this.COLUMN_DEFAULT_TESTERS) {
// This is potentially going to be used by extensions; avoid them
// killing us.
try {
shouldShowColumn = this.COLUMN_DEFAULT_TESTERS[colId](this.view);
} catch (ex) {
shouldShowColumn = false;
Cu.reportError(ex);
}
}
state[colId] = { visible: shouldShowColumn };
}
return state;
},
/**
* Is setColumnStates messing with the columns' DOM? This is used by
* hintColumnsChanged to avoid wasteful state persistence.
*/
_touchingColumns: false,
/**
* Set the column states of this FolderDisplay to the provided state.
*
* @param aColumnStates an object of the form described on
* |_savedColumnStates|. If ordinal attributes are omitted then no
* re-ordering will be performed. This is intentional, but potentially a
* bad idea. (Right now only gloda search underspecifies ordinals.)
* @param [aPersistChanges=false] Should we persist the changes to the view?
* This only has an effect if we are active.
*
* @public
*/
setColumnStates(aColumnStates, aPersistChanges) {
// If we are not active, just overwrite our current state with the provided
// state and bail.
if (!this._active) {
this._savedColumnStates = aColumnStates;
return;
}
this._touchingColumns = true;
try {
let cols = document.getElementById("threadCols");
let colChildren = cols.children;
for (let iKid = 0; iKid < colChildren.length; iKid++) {
let colChild = colChildren[iKid];
if (colChild == null) {
continue;
}
// We only care about treecols. The splitters do not need to be marked
// hidden or un-hidden.
if (colChild.tagName == "treecol") {
// if it doesn't have preserved state it should be hidden
let shouldBeHidden = true;
// restore state
if (colChild.id in aColumnStates) {
let colState = aColumnStates[colChild.id];
if ("visible" in colState) {
shouldBeHidden = !colState.visible;
}
if (
"ordinal" in colState &&
colChild.getAttribute("ordinal") != colState.ordinal
) {
colChild.setAttribute("ordinal", colState.ordinal);
}
}
let isHidden = colChild.getAttribute("hidden") == "true";
if (isHidden != shouldBeHidden) {
if (shouldBeHidden) {
colChild.setAttribute("hidden", "true");
} else {
colChild.removeAttribute("hidden");
}
}
}
}
} finally {
this._touchingColumns = false;
}
if (aPersistChanges) {
this.hintColumnsChanged();
}
},
/**
* A dictionary that maps column ids to dictionaries where each dictionary
* has the following fields:
* - visible: Is the column visible.
* - ordinal: The 1-based XUL 'ordinal' value assigned to the column. This
* corresponds to the position but is not something you want to manipulate.
* See the documentation in _saveColumnStates for more information.
*/
_savedColumnStates: null,
/**
* Return a dictionary in the form of |_savedColumnStates| representing the
* current column states.
*
* @public
*/
getColumnStates() {
if (!this._active) {
return this._savedColumnStates;
}
let columnStates = {};
let cols = document.getElementById("threadCols");
let colChildren = cols.children;
for (let iKid = 0; iKid < colChildren.length; iKid++) {
let colChild = colChildren[iKid];
if (colChild.tagName != "treecol") {
continue;
}
columnStates[colChild.id] = {
visible: colChild.getAttribute("hidden") != "true",
ordinal: colChild.getAttribute("ordinal"),
};
}
return columnStates;
},
/**
* For now, just save the visible columns into a dictionary for use in a
* subsequent call to |setColumnStates|.
*/
_saveColumnStates() {
// In the actual nsITreeColumn, the index property indicates the column
// number. This column number is a 0-based index with no gaps; it only
// increments the number each time it sees a column.
// However, this is subservient to the 'ordinal' property which
// defines the _apparent content sequence_ provided by GetNextSibling.
// The underlying content ordering is still the same, which is how
// restoreNaturalOrder can reset things to their XUL definition sequence.
// The 'ordinal' stuff works because nsBoxFrame::RelayoutChildAtOrdinal
// messes with the sibling relationship.
// Ordinals are 1-based. restoreNaturalOrder apparently is dumb and does
// not know this, although the ordering is relative so it doesn't actually
// matter. The annoying splitters do have ordinals, and live between
// tree columns. The splitters adjacent to a tree column do not need to
// have any 'ordinal' relationship, although it would appear user activity
// tends to move them around in a predictable fashion with oddness involved
// at the edges.
// Changes to the ordinal attribute should take immediate effect in terms of
// sibling relationship, but will merely invalidate the columns rather than
// cause a re-computation of column relationships every time.
// restoreNaturalOrder invalidates the tree when it is done re-ordering; I'm
// not sure that's entirely necessary...
this._savedColumnStates = this.getColumnStates();
},
/**
* Restores the visible columns saved by |_saveColumnStates|.
*/
_restoreColumnStates() {
if (this._savedColumnStates) {
this.setColumnStates(this._savedColumnStates);
this._savedColumnStates = null;
}
},
// @}
/**
* @name What To Display
* @protected
*/
// @{
showFolderUri(aFolderURI) {
return this.show(MailUtils.getExistingFolder(aFolderURI));
},
/**
* Invoked by showFolder when it turns out the folder is in fact a server.
* @private
*/
_showServer() {
// currently nothing to do. makeActive handles everything for us (because
// what is displayed needs to be re-asserted each time we are activated
// too.)
},
/**
* Select a folder for display.
*
* @param aFolder The nsIMsgDBFolder to display.
*/
show(aFolder) {
if (aFolder == null) {
this._nonViewFolder = null;
this.view.close();
} else if (aFolder instanceof Ci.nsIMsgFolder) {
if (aFolder.isServer) {
this._nonViewFolder = aFolder;
this._showServer();
this.view.close();
// A server is fully loaded immediately, for now. (When we have the
// account summary, we might want to change this to wait for the page
// load to complete.)
this._allMessagesLoaded = true;
} else {
this._nonViewFolder = null;
this.view.open(aFolder);
}
} else {
// it must be a synthetic view
this.view.openSynthetic(aFolder);
}
if (this._active) {
this.makeActive();
}
if (this._tabInfo) {
document.getElementById("tabmail").setTabTitle(this._tabInfo);
}
},
/**
* Clone an existing view wrapper as the basis for our display.
*/
cloneView(aViewWrapper) {
this.view = aViewWrapper.clone(this);
// generate a view created notification; this will cause us to do the right
// thing in terms of associating the view with the tree and such.
this.onCreatedView();
if (this._active) {
this.makeActive();
}
},
/**
* Close resources associated with the currently displayed folder because you
* no longer care about this FolderDisplayWidget.
*/
close() {
// Mark ourselves as inactive without doing any of the hard work of becoming
// inactive. This saves us from trying to update things as they go away.
this._active = false;
// Tell the message display to close itself too. We do this before we do
// anything else because closing the view could theoretically propagate
// down to the message display and we don't want it doing anything it
// doesn't have to do.
this.messageDisplay._close();
this.view.close();
this.messenger.setWindow(null, null);
this.messenger = null;
this._fakeTree.remove();
this._fakeTree = null;
this._fakeTreeSelection = null;
},
// @}
/* =============================== */
/* ===== IDBViewWrapper Listener ===== */
/* =============================== */
/**
* @name IDBViewWrapperListener Interface
* @private
*/
// @{
/**
* @return true if the mail view picker is visible. This affects whether the
* DBViewWrapper will actually use the persisted mail view or not.
*/
get shouldUseMailViews() {
return ViewPickerBinding.isVisible;
},
/**
* Let the viewWrapper know if we should defer message display because we
* want the user to connect to the server first so password authentication
* can occur.
*
* @return true if the folder should be shown immediately, false if we should
* wait for updateFolder to complete.
*/
get shouldDeferMessageDisplayUntilAfterServerConnect() {
let passwordPromptRequired = false;
if (Services.prefs.getBoolPref("mail.password_protect_local_cache")) {
passwordPromptRequired = this.view.displayedFolder.server
.passwordPromptRequired;
}
return passwordPromptRequired;
},
/**
* Let the viewWrapper know if it should mark the messages read when leaving
* the provided folder.
*
* @return true if the preference is set for the folder's server type.
*/
shouldMarkMessagesReadOnLeavingFolder(aMsgFolder) {
return Services.prefs.getBoolPref(
"mailnews.mark_message_read." + aMsgFolder.server.type
);
},
/**
* The view wrapper tells us when it starts loading a folder, and we set the
* cursor busy. Setting the cursor busy on a per-tab basis is us being
* nice to the future. Loading a folder is a blocking operation that is going
* to make us unresponsive and accordingly make it very hard for the user to
* change tabs.
*/
onFolderLoading(aFolderLoading) {
if (this._tabInfo) {
document
.getElementById("tabmail")
.setTabBusy(this._tabInfo, aFolderLoading);
}
FolderDisplayListenerManager._fireListeners("onFolderLoading", [
this,
aFolderLoading,
]);
},
/**
* The view wrapper tells us when a search is active, and we mark the tab as
* thinking so the user knows something is happening. 'Searching' in this
* case is more than just a user-initiated search. Virtual folders / saved
* searches, mail views, plus the more obvious quick search are all based off
* of searches and we will receive a notification for them.
*/
onSearching(aIsSearching) {
if (this._tabInfo) {
let searchBundle = document.getElementById("bundle_search");
document
.getElementById("tabmail")
.setTabThinking(
this._tabInfo,
aIsSearching && searchBundle.getString("searchingMessage")
);
}
FolderDisplayListenerManager._fireListeners("onSearching", [
this,
aIsSearching,
]);
},
/**
* Things we do on creating a view:
* - notify the observer service so that custom column handler providers can
* add their custom columns to our view.
*/
onCreatedView() {
// All of our messages are not displayed if the view was just created. We
// will get an onMessagesLoaded(true) nearly immediately if this is a local
// folder where view creation is synonymous with having all messages.
this._allMessagesLoaded = false;
this.messageDisplay.onCreatedView();
FolderDisplayListenerManager._fireListeners("onCreatedView", [this]);
this._notifyWhenActive(this._activeCreatedView);
},
_activeCreatedView() {
gDBView = this.view.dbView;
// A change in view may result in changes to sorts, the view menu, etc.
// Do this before we 'reroot' the dbview.
this._updateThreadDisplay();
// this creates a new selection object for the view.
if (this.tree) {
this.tree.view = this.view.dbView;
}
FolderDisplayListenerManager._fireListeners("onActiveCreatedView", [this]);
// The data payload used to be viewType + ":" + viewFlags. We no longer
// do this because we already have the implied contract that gDBView is
// valid at the time we generate the notification. In such a case, you
// can easily get that information from the gDBView. (The documentation
// on creating a custom column assumes gDBView.)
Services.obs.notifyObservers(this.displayedFolder, "MsgCreateDBView");