summaryrefslogtreecommitdiffstats
path: root/comm/suite/mailnews/content/tabmail.js
blob: 694941ce9212f58e7b2c681e33f881538dd9b487 (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
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
/* -*- 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/. */

// Traditionally, mailnews tabs come in two flavours: "folder" and
// "message" tabs. But these modes are just mere default settings on tab
// creation, defining layout, URI to load, etc.
// The user can turn a "message" tab into a "folder" tab just by unhiding
// the folder pane (F9) and hiding the message pane (F8), and vice versa.
// Tab title and icon will change accordingly.
// Both flavours are just instances of the basic "3pane" mode, triggered by
// a bitwise or combination of these possible pane values:
const kTabShowNoPane      = 0;
const kTabShowFolderPane  = 1 << 0;
const kTabShowMessagePane = 1 << 1;
const kTabShowThreadPane  = 1 << 2;
const kTabShowAcctCentral = 1 << 3;
// predefined mode masks
const kTabMaskDisplayDeck = kTabShowThreadPane | kTabShowAcctCentral;
// predefined traditional flavours
const kTabModeFolder      = kTabShowFolderPane | kTabShowThreadPane | kTabShowMessagePane;
const kTabModeMessage     = kTabShowMessagePane;  // message tab


// global mailnews tab definition object
var gMailNewsTabsType =
{
  name: "mailnews",
  panelId: "mailContent",

  modes:
  {
    "3pane":
    {
      isDefault: true,
      type: "3pane",

      // aTabInfo belongs to the newly created tab,
      // aArgs can contain:
      // * modeBits is a combination of kTabShow* layout bits (or null),
      // * folderURI designates the folder to select (or null)
      // * msgHdr designates the message to select (or null)
      openTab: function(aTabInfo, {modeBits: aModeBits, folderURI: aFolderURI,
                                   msgHdr: aMsgHdr}) {
        // clone the current 3pane state before overriding parts of it
        this.saveTabState(aTabInfo);

        // aModeBits must have at least one bit set
        // if not, we just copy the current state
        let cloneMode = !aModeBits;
        if (cloneMode)
          aModeBits = this.getCurrentModeBits() || kTabModeFolder;
        aTabInfo.modeBits = aModeBits;
        // Currently, we only check for kTabModeMessage vs. kTabModeFolder,
        // but in theory we could distinguish in much more detail!
        let messageId = null;
        if (aModeBits == kTabModeMessage || cloneMode)
        {
          if (!aMsgHdr && gDBView)
          {
            try
            {
              // duplicate current message tab if nothing else is specified
              aMsgHdr = gDBView.hdrForFirstSelectedMessage;
              // Use the header's folder - this will open a msg in a virtual folder view
              // in its real folder, which is needed if the msg wouldn't be in a new
              // view with the same terms - e.g., it's read and the view is unread only.
              // If we cloned the view, we wouldn't have to do this.
              if (aTabInfo.switchToNewTab)
              {
                // Fix it so we won't try to load the previously loaded message.
                aMsgHdr.folder.lastMessageLoaded = nsMsgKey_None;
              }
              aFolderURI = aMsgHdr.folder.URI;
            }
            catch (ex) {}
          }
          if (aMsgHdr)
            messageId = aMsgHdr.messageId;
          aTabInfo.clearSplitter = true;
        }

        if (!messageId)
        {
          // only sanitize the URL, if possible
          let clearSplitter = aModeBits == kTabModeFolder;
          if (!aFolderURI)
          {
            // Use GetSelectedMsgFolders() to find out which folder to open
            // instead of GetLoadedMsgFolder().URI. This is required because on a
            // right-click, the currentIndex value will be different from the
            // actual row that is highlighted. GetSelectedMsgFolders() will
            // return the message that is highlighted.
            let msgFolder = GetSelectedMsgFolders()[0];
            aFolderURI = msgFolder.URI;
            // don't kill the splitter settings for account central
            clearSplitter &= !msgFolder.isServer;
          }
          aMsgHdr = null;
          aTabInfo.clearSplitter = clearSplitter;
        }
        aTabInfo.uriToOpen = aFolderURI;
        aTabInfo.hdr = aMsgHdr;
        aTabInfo.selectedMsgId = messageId;

        // call superclass logic
        this.openTab(aTabInfo);
      },

      // We can close all mailnews tabs - but one.
      // Closing the last mailnews tab would destroy our mailnews functionality.
      canCloseTab: function(aTabInfo)
      {
        return aTabInfo.mode.tabs.length > 1;
      }
    }
  },

  // combines the current pane visibility states into a mode bit mask
  getCurrentModeBits: function()
  {
    let modeBits = kTabShowNoPane;
    if (!IsFolderPaneCollapsed())
      modeBits |= kTabShowFolderPane;
    if (!IsDisplayDeckCollapsed())
    {
      // currently, the display deck has only two panes
      if (gAccountCentralLoaded)
        modeBits |= kTabShowAcctCentral;
      else
        modeBits |= kTabShowThreadPane;
    }
    if (!IsMessagePaneCollapsed())
      modeBits |= kTabShowMessagePane;
    return modeBits;
  },

  _updatePaneLayout: function(aTabInfo)
  {
    // first show all needed panes, then hide all unwanted ones
    // (we have to keep this order to avoid hiding all panes!)
    let showFolderPane  = aTabInfo.modeBits & kTabShowFolderPane;
    let showMessagePane = aTabInfo.modeBits & kTabShowMessagePane;
    let showDisplayDeck = aTabInfo.modeBits & (kTabShowThreadPane | kTabShowAcctCentral);
    if (showMessagePane && IsMessagePaneCollapsed())
      MsgToggleMessagePane(true); // show message pane
    if (showDisplayDeck && IsDisplayDeckCollapsed())
      MsgToggleThreadPane();      // show thread pane
    if (showFolderPane && IsFolderPaneCollapsed())
      MsgToggleFolderPane(true);  // show folder pane
    if (!showMessagePane && !IsMessagePaneCollapsed())
      MsgToggleMessagePane(true); // hide message pane
    if (!showDisplayDeck && !IsDisplayDeckCollapsed())
      MsgToggleThreadPane();      // hide thread pane
    if (!showFolderPane && !IsFolderPaneCollapsed())
      MsgToggleFolderPane(true);  // hide folder pane
    UpdateLayoutVisibility();
  },

  /**
   * Create the new tab's state, which engenders some side effects.
   * Part of our contract is that we leave the tab in the selected state.
   */
  openTab: function(aTabInfo)
  {
    // each tab gets its own messenger instance
    // for undo/redo, backwards/forwards, etc.
    messenger = Cc["@mozilla.org/messenger;1"]
                  .createInstance(Ci.nsIMessenger);
    messenger.setWindow(window, msgWindow);
    aTabInfo.messenger = messenger;

    // remember the currently selected folder
    aTabInfo.msgSelectedFolder = gMsgFolderSelected;

    // show tab if permitted
    if (aTabInfo.switchToNewTab)
      this.showTab(aTabInfo);
  },

  showTab: function(aTabInfo)
  {
    // don't allow saveTabState while restoring a tab
    aTabInfo.lock = true;
    // set the messagepane as the primary browser for content
    var messageBrowser = getMessageBrowser();
    messageBrowser.setAttribute("type", "content");
    messageBrowser.setAttribute("primary", "true");

    if (aTabInfo.uriToOpen)
    {
      // HACK: Since we've switched away from the tab, we need to bring
      // back the real selection before selecting the folder, so do that
      RestoreSelectionWithoutContentLoad(document.getElementById("folderTree"));

      // Clear selection, because context clicking on a folder and opening in a
      // new tab needs to have selectFolder think the selection has changed.
      gFolderTreeView.selection.clearSelection();
      gFolderTreeView.selection.currentIndex = -1;
      gMsgFolderSelected = null;
      msgWindow.openFolder = null;

      // clear gDBView so we won't try to close it
      gDBView = null;

      // reroot the message sink (we might have switched layout)
      messenger.setWindow(null, null);
      messenger.setWindow(window, msgWindow);

      // Clear thread pane selection - otherwise, the tree tries to impose the
      // the current selection on the new view.
      let msgHdr = aTabInfo.hdr;
      let msgId  = aTabInfo.selectedMsgId;
      aTabInfo.hdr = null;
      aTabInfo.selectedMsgId = null;
      aTabInfo.dbView = null;
      let folder = MailUtils.getFolderForURI(aTabInfo.uriToOpen);
      gFolderTreeView.selectFolder(folder);
      gCurrentFolderToReroot = null;
      delete aTabInfo.uriToOpen; // destroy after use!
      // Store the folder that is being opened.
      aTabInfo.msgSelectedFolder = folder;

      // restore our message data
      aTabInfo.hdr = msgHdr;
      aTabInfo.selectedMsgId = msgId;

      aTabInfo.dbView = gDBView;
      UpdateMailToolbar("new tab");
    }

    // Do not bother with Thread and Message panes if at server level.
    if (!aTabInfo.msgSelectedFolder.isServer) {
      // Restore the layout if present.
      ShowThreadPane();
      // Some modes (e.g. new message tabs) need to initially hide the
      // splitters, this is marked by aTabInfo.clearSplitter=true.
      let clearSplitter = "clearSplitter" in aTabInfo && aTabInfo.clearSplitter;
      if (clearSplitter) {
        aTabInfo.messageSplitter.collapsible = true;
        aTabInfo.folderSplitter.collapsible = true;
        delete aTabInfo.clearSplitter;
      }
      SetSplitterState(GetThreadAndMessagePaneSplitter(),
                       aTabInfo.messageSplitter);
      SetSplitterState(GetFolderPaneSplitter(),
                       aTabInfo.folderSplitter);
      this._updatePaneLayout(aTabInfo);
      ClearMessagePane();
      // Force the header pane twisty state restoration by toggling from the
      // opposite.
      if (gCollapsedHeaderViewMode != aTabInfo.headerViewMode)
        ToggleHeaderView();
    }

    // restore globals
    messenger = aTabInfo.messenger;
    gDBView = aTabInfo.dbView;
    gSearchSession = aTabInfo.searchSession;
    let folderToSelect = aTabInfo.msgSelectedFolder || gDBView && gDBView.msgFolder;

    // restore view state if we had one
    let row = gFolderTreeView.getIndexOfFolder(folderToSelect);
    let treeBoxObj = document.getElementById("folderTree").treeBoxObject;
    let folderTreeSelection = gFolderTreeView.selection;

    // make sure that row.value is valid so that it doesn't mess up
    // the call to ensureRowIsVisible()
    if ((row >= 0) && !folderTreeSelection.isSelected(row))
    {
      gMsgFolderSelected = folderToSelect;
      msgWindow.openFolder = folderToSelect;
      folderTreeSelection.select(row);
      treeBoxObj.ensureRowIsVisible(row);
    }

    if (gDBView)
    {
      // This sets the thread pane tree's view to the gDBView view.
      UpdateSortIndicators(gDBView.sortType, gDBView.sortOrder);
      RerootThreadPane();

      // We don't want to reapply the mailview (threadpane changes by switching
      // tabs alone would be rather surprising), just update the viewpicker
      // and resave the new view.
      UpdateViewPickerByValue(aTabInfo.mailView);
      SetMailViewForFolder(folderToSelect, aTabInfo.mailView);

      // restore quick search
      GetSearchInput().value = aTabInfo.searchInput;

      // We need to restore the selection to what it was when we switched away
      // from this tab. We need to remember the selected keys, instead of the
      // selected indices, since the view might have changed. But maybe the
      // selectedIndices adjust as items are added/removed from the (hidden)
      // view.
      try
      {
        if (aTabInfo.selectedMsgId && aTabInfo.msgSelectedFolder)
        {
          // We clear the selection in order to generate an event when we
          // re-select our message. This destroys aTabInfo.selectedMsgId.
          let selectedMsgId = aTabInfo.selectedMsgId;
          ClearThreadPaneSelection();
          aTabInfo.selectedMsgId = selectedMsgId;
          let msgDB = aTabInfo.msgSelectedFolder.msgDatabase;
          let msgHdr = msgDB.getMsgHdrForMessageID(aTabInfo.selectedMsgId);
          setTimeout(gDBView.selectFolderMsgByKey,
                     0,
                     aTabInfo.msgSelectedFolder,
                     msgHdr.messageKey);
        }
        // We do not clear the selection if there was more than one message
        // displayed.  this leaves our selection intact. there was originally
        // some claim that the selection might lose synchronization with the
        // view, but this is unsubstantiated.  said comment came from the
        // original code that stored information on the selected rows, but
        // then failed to do anything with it, probably because there is no
        // existing API call that accomplishes it.
      }
      catch (ex)
      {
        dump(ex);
      }
      GetThreadTree().treeBoxObject.scrollToRow(aTabInfo.firstVisibleRow);
    }
    else if (gMsgFolderSelected.isServer)
    {
      // Load AccountCentral page here.
      ShowAccountCentral(gMsgFolderSelected);
    }
    SetUpToolbarButtons(gMsgFolderSelected.URI);
    UpdateMailToolbar("tab changed");
    delete aTabInfo.lock;
  },

  closeTab: function(aTabInfo)
  {
    // If the tab has never been opened, we must not clean up the view,
    // because it still belongs to a different tab.
    if (aTabInfo.uriToOpen)
      return;

    if (aTabInfo.dbView)
      aTabInfo.dbView.close();
    if (aTabInfo.messenger)
      aTabInfo.messenger.setWindow(null, null);
  },

  // called when switching away from aTabInfo
  saveTabState: function(aTabInfo)
  {
    if (aTabInfo.lock)
      return;

    // save message db data and view filters
    aTabInfo.messenger = messenger;
    aTabInfo.dbView = gDBView;
    aTabInfo.searchSession = gSearchSession;
    aTabInfo.msgSelectedFolder = gMsgFolderSelected;
    aTabInfo.selectedMsgId = null;
    if (gDBView)
    {
      // save thread pane scroll position
      aTabInfo.firstVisibleRow = GetThreadTree().treeBoxObject.getFirstVisibleRow();

      let curMsgViewIndex = gDBView.currentlyDisplayedMessage;
      if (curMsgViewIndex != nsMsgViewIndex_None)
      {
        try // there may not be a selected message.
        {
          // the currentlyDisplayedMessage is not always the first selected
          // message, e.g. on a right click for the context menu
          let curMsgHdr = gDBView.getMsgHdrAt(curMsgViewIndex);
          aTabInfo.selectedMsgId = curMsgHdr.messageId;
        }
        catch (ex) {}
      }
      if (!aTabInfo.selectedMsgId)
        aTabInfo.msgSelectedFolder = gDBView.msgFolder;
    }
    aTabInfo.mailView = GetMailViewForFolder(aTabInfo.msgSelectedFolder);

    // remember layout
    aTabInfo.modeBits = this.getCurrentModeBits();
    aTabInfo.messageSplitter = GetSplitterState(GetThreadAndMessagePaneSplitter());
    aTabInfo.folderSplitter  = GetSplitterState(GetFolderPaneSplitter());

    // header pane twisty state
    aTabInfo.headerViewMode = gCollapsedHeaderViewMode;

    // quick search
    aTabInfo.searchInput = GetSearchInput().value;
  },

  onTitleChanged: function(aTabInfo, aTabNode)
  {
    // If we have an account, we also always have a "Local Folders" account,
    let multipleRealAccounts = MailServices.accounts.accounts.length > 2;

    // clear out specific tab data now, because we might need to return early
    aTabNode.removeAttribute("SpecialFolder");
    aTabNode.removeAttribute("ServerType");
    aTabNode.removeAttribute("IsServer");
    aTabNode.removeAttribute("IsSecure");
    aTabNode.removeAttribute("NewMessages");
    aTabNode.removeAttribute("ImapShared");
    aTabNode.removeAttribute("BiffState");
    aTabNode.removeAttribute("MessageType");
    aTabNode.removeAttribute("Offline");
    aTabNode.removeAttribute("Attachment");
    aTabNode.removeAttribute("IMAPDeleted");

    // aTabInfo.msgSelectedFolder may contain the base folder of saved search
    let folder = null;
    if (aTabInfo.uriToOpen)
    {
      // select folder for the backgound tab without changing the current one
      // (stolen from SelectFolder)
      folder = MailUtils.getFolderForURI(aTabInfo.uriToOpen);
    }
    else
    {
      folder = (aTabInfo.dbView && aTabInfo.dbView.viewFolder) ||
               (aTabInfo.dbView && aTabInfo.dbView.msgFolder) ||
               aTabInfo.msgSelectedFolder || gMsgFolderSelected;
    }

    // update the message header only if we're the current tab
    if (aTabNode.selected)
    {
      try
      {
        aTabInfo.hdr = aTabInfo.dbView && aTabInfo.dbView.hdrForFirstSelectedMessage;
      }
      catch (e)
      {
        aTabInfo.hdr = null;
      }
    }

    // update tab title and icon state
    aTabInfo.title = "";
    if (IsMessagePaneCollapsed() || !aTabInfo.hdr)
    {
      // Folder Tab
      aTabNode.setAttribute("type", "folder"); // override "3pane"
      if (!folder)
      {
        // nothing to do
        return;
      }
      else
      {
        aTabInfo.title = folder.prettyName;
        if (!folder.isServer && multipleRealAccounts)
          aTabInfo.title += " - " + folder.server.prettyName;
      }

      // The user may have changed folders, triggering our onTitleChanged callback.
      // Update the appropriate attributes on the tab.
      aTabNode.setAttribute("SpecialFolder", FolderUtils.getSpecialFolderString(folder));
      aTabNode.setAttribute("ServerType", folder.server.type);
      aTabNode.setAttribute("IsServer", folder.isServer);
      aTabNode.setAttribute("IsSecure", folder.server.isSecure);
      aTabNode.setAttribute("NewMessages", folder.hasNewMessages);
      aTabNode.setAttribute("ImapShared", folder.imapShared);

      let biffState = "UnknownMail";
      switch (folder.biffState)
      {
        case Ci.nsIMsgFolder.nsMsgBiffState_NewMail:
          biffState = "NewMail";
          break;
        case Ci.nsIMsgFolder.nsMsgBiffState_NoMail:
          biffState = "NoMail";
          break;
      }
      aTabNode.setAttribute("BiffState", biffState);
    }
    else
    {
      // Message Tab
      aTabNode.setAttribute("type", "message"); // override "3pane"
      if (aTabInfo.hdr.flags & Ci.nsMsgMessageFlags.HasRe)
        aTabInfo.title = "Re: ";
      if (aTabInfo.hdr.mime2DecodedSubject)
        aTabInfo.title += aTabInfo.hdr.mime2DecodedSubject;
      aTabInfo.title += " - " + aTabInfo.hdr.folder.prettyName;
      if (multipleRealAccounts)
        aTabInfo.title += " - " + aTabInfo.hdr.folder.server.prettyName;

      // message specific tab data
      let flags = aTabInfo.hdr.flags;
      aTabNode.setAttribute("MessageType", folder.server.type);
      aTabNode.setAttribute("Offline",
                            Boolean(flags & Ci.nsMsgMessageFlags.Offline));
      aTabNode.setAttribute("Attachment",
                            Boolean(flags & Ci.nsMsgMessageFlags.Attachment));
      aTabNode.setAttribute("IMAPDeleted",
                            Boolean(flags & Ci.nsMsgMessageFlags.IMAPDeleted));
    }
  },

  getBrowser: function(aTabInfo)
  {
    // we currently use the messagepane element for all 3pane tab types
    return getMessageBrowser();
  },

  //
  // nsIController implementation
  //
  // We ignore the aTabInfo parameter sent by tabmail when calling nsIController
  // stuff and just delegate the call to the DefaultController by using it as
  // our proto chain.
  // XXX remove the MessageWindowController stuff once we kill messageWindow.xul
  __proto__: "DefaultController" in window && window.DefaultController ||
             "MessageWindowController" in window && window.MessageWindowController
};



//
//  tabmail support methods
//

function GetTabMail()
{
  return document.getElementById("tabmail");
}

function MsgOpenNewTab(aType, aModeBits, aBackground) {
  // duplicate the current tab
  var tabmail = GetTabMail();
  if (tabmail)
    tabmail.openTab(aType, {modeBits: aModeBits, background: aBackground});
}

function MsgOpenNewTabForFolder(aBackground) {
  // open current folder in full 3pane tab
  MsgOpenNewTab("3pane", kTabModeFolder, aBackground);
}

function MsgOpenNewTabForMessage(aBackground) {
  // open current message in message tab
  MsgOpenNewTab("3pane", kTabModeMessage, aBackground);
}

// A Thunderbird compatibility function called from e.g. newsblog.
// We ignore aHandlerRegExp as it is not needed by SeaMonkey.
function openContentTab(aUrl, aWhere, aHandlerRegExp)
{
  openUILinkIn(aUrl, aWhere);
}

function AllowOpenTabOnMiddleClick()
{
  return Services.prefs.getBoolPref("mail.tabs.opentabfor.middleclick");
}

function AllowOpenTabOnDoubleClick()
{
  return Services.prefs.getBoolPref("mail.tabs.opentabfor.doubleclick");
}

//
// pane management
// (maybe we should cache these items in a global object?)
//

function GetFolderPane()
{
  return document.getElementById("folderPaneBox");
}

function GetThreadPane()
{
  return document.getElementById("threadPaneBox");
}

function GetDisplayDeck()
{
  return document.getElementById("displayDeck");
}

function GetMessagePane()
{
  return document.getElementById("messagepanebox");
}

function GetHeaderPane()
{
  return document.getElementById("msgHeaderView");
}

function GetFolderPaneSplitter()
{
  return document.getElementById("folderpane-splitter");
}

function GetThreadAndMessagePaneSplitter()
{
  return document.getElementById("threadpane-splitter");
}



//
// pane visibility management
//
// - collapsing the folderpane by clicking its splitter doesn't need
//   additional processing
// - collapsing the messagepane by clicking its splitter needs some special
//   treatment of attachments, gDBView, etc.
// - the threadpane has no splitter assigned to it
// - collapsing the messagepane, threadpane or folderpane by <key> needs to
//   pay attention to the other panes' (and splitters') visibility

function IsMessagePaneCollapsed()
{
  return GetMessagePane().collapsed;
}

function IsDisplayDeckCollapsed()
{
  // regard display deck as collapsed in the standalone message window
  var displayDeck = GetDisplayDeck();
  return !displayDeck || displayDeck.collapsed;
}

function IsFolderPaneCollapsed()
{
  // regard folderpane as collapsed in the standalone message window
  var folderPane = GetFolderPane();
  return !folderPane || folderPane.collapsed;
}

// Which state is the splitter in? Is it collapsed?
// How wide/high is the associated pane?
function GetSplitterState(aSplitter)
{
  var next = aSplitter.getAttribute("collapse") == "after";
  var pane = next ? aSplitter.nextSibling : aSplitter.previousSibling;
  var vertical = aSplitter.orient == "vertical";
  var rv =
  {
    state:     aSplitter.getAttribute("state"),
    collapsed: aSplitter.collapsed,
    // <splitter>s are <hbox>es,
    // thus the "orient" attribute is usually either unset or "vertical"
    size:      vertical ? pane.height : pane.width,
    collapsible: "collapsible" in aSplitter && aSplitter.collapsible
  };
  return rv;
}

function SetSplitterState(aSplitter, aState)
{
  // all settings in aState are optional
  if (!aState)
    return;
  if ("state" in aState)
    aSplitter.setAttribute("state", aState.state);
  if ("collapsed" in aState)
    aSplitter.collapsed = aState.collapsed;
  if ("size" in aState)
  {
    let next = aSplitter.getAttribute("collapse") == "after";
    let pane = next ? aSplitter.nextSibling : aSplitter.previousSibling;
    let vertical = aSplitter.orient == "vertical";
    if (vertical)
    {
      // vertical splitter orientation
      pane.height = aState.size;
    }
    else
    {
      // horizontal splitter orientation
      pane.width = aState.size;
    }
  }
  if ("collapsible" in aState)
    aSplitter.collapsible = aState.collapsible;
}

// If we hit one of the pane splitter <key>s or choose the respective menuitem,
// we show/hide both the pane *and* the splitter, just like we do for the
// browser sidebar. Clicking a splitter's grippy, though, will hide the pane
// but not the splitter.
function MsgToggleSplitter(aSplitter)
{
  var state = aSplitter.getAttribute("state");
  if (state == "collapsed")
  {
    // removing the attribute would hurt persistency
    aSplitter.setAttribute("state", "open");
    aSplitter.collapsed = false; // always show splitter when open
  }
  else
  {
    aSplitter.setAttribute("state", "collapsed");
    aSplitter.collapsed = true; // hide splitter
  }
}

function MsgCollapseSplitter(aSplitter, aCollapse)
{
  if (!("collapsible" in aSplitter))
    aSplitter.collapsible = true;
  aSplitter.collapsed = aCollapse && aSplitter.collapsible;
}

// helper function for UpdateLayoutVisibility
function UpdateFolderPaneFlex(aTuneLayout)
{
  var folderBox = GetFolderPane();
  var messagesBox = document.getElementById("messagesBox");
  if (aTuneLayout)
  {
    // tune folderpane layout
    folderBox.setAttribute("flex", "1");
    messagesBox.removeAttribute("flex");
  }
  else
  {
    // restore old layout
    folderBox.removeAttribute("flex");
    messagesBox.setAttribute("flex", "1");
  }
}

// we need to finetune the pane and splitter layout in certain circumstances
function UpdateLayoutVisibility()
{
  var modeBits = gMailNewsTabsType.getCurrentModeBits();
  var folderPaneVisible  = modeBits & kTabShowFolderPane;
  var messagePaneVisible = modeBits & kTabShowMessagePane;
  var threadPaneVisible  = modeBits & kTabShowThreadPane;
  var displayDeckVisible = modeBits & kTabMaskDisplayDeck;
  var onlyFolderPane     = modeBits == kTabShowFolderPane;
  var onlyMessagePane    = modeBits == kTabShowMessagePane;
  var onlyDisplayDeck    = modeBits == kTabShowThreadPane ||
                           modeBits == kTabShowAcctCentral;
  var onlyOnePane = onlyFolderPane || onlyMessagePane || onlyDisplayDeck;
  var showFolderSplitter  = false;
  var showMessageSplitter = false;
  switch (Services.prefs.getIntPref("mail.pane_config.dynamic"))
  {
    case kClassicMailLayout:
      // if only the folderpane is visible it has to flex,
      // while the messagesbox must not
      UpdateFolderPaneFlex(onlyFolderPane);
      if (!onlyOnePane)
      {
        showFolderSplitter  = folderPaneVisible;
        showMessageSplitter = threadPaneVisible && messagePaneVisible;
      }
      break;

    case kWideMailLayout:
      // if only the messagepane is visible, collapse the rest
      let messengerBox = document.getElementById("messengerBox");
      messengerBox.collapsed = onlyMessagePane;
      // a hidden displaydeck must not flex, while the folderpane has to
      if (!onlyMessagePane)
        UpdateFolderPaneFlex(!displayDeckVisible);
      if (!onlyOnePane)
      {
        showFolderSplitter  = folderPaneVisible && displayDeckVisible;
        showMessageSplitter = messagePaneVisible;
      }
      break;

    case kVerticalMailLayout:
      // if the threadpane is hidden, we need to hide its outer box as well
      let messagesBox = document.getElementById("messagesBox");
      messagesBox.collapsed = !displayDeckVisible;
      // if only the folderpane is visible, it needs to flex
      UpdateFolderPaneFlex(onlyFolderPane);
      if (!onlyOnePane)
      {
        showFolderSplitter  = folderPaneVisible;
        showMessageSplitter = messagePaneVisible;
      }
      break;
  }

  // set splitter visibility
  // if the pane was hidden by clicking the splitter grippy,
  // the splitter must not hide
  MsgCollapseSplitter(GetFolderPaneSplitter(),           !showFolderSplitter);
  MsgCollapseSplitter(GetThreadAndMessagePaneSplitter(), !showMessageSplitter);

  // disable location bar if only message pane is visible
  document.getElementById("locationFolders").disabled = onlyMessagePane;
  // disable mailviews and search if threadpane is invisible
  if (!threadPaneVisible)
    gDisableViewsSearch.setAttribute("disabled", true);
  else
    gDisableViewsSearch.removeAttribute("disabled");
}

function ChangeMessagePaneVisibility()
{
  var hidden = IsMessagePaneCollapsed();
  // We also have to disable the Message/Attachments menuitem.
  // It will be enabled when loading a message with attachments
  // (see messageHeaderSink.handleAttachment).
  if (hidden)
  {
    let node = document.getElementById("msgAttachmentMenu");
    if (node)
      node.setAttribute("disabled", "true");
  }

  if (gDBView)
  {
    // clear the subject, collapsing won't automatically do this
    setTitleFromFolder(GetThreadPaneFolder(), null);
    // the collapsed state is the state after we released the mouse
    // so we take it as it is
    gDBView.suppressMsgDisplay = hidden;
    // set the subject, uncollapsing won't automatically do this
    gDBView.loadMessageByUrl("about:blank");
    gDBView.selectionChanged();
  }

  var event = new Event( "messagepane-"+ (hidden ? "hide" : "unhide"),
                          { bubbles: false, cancelable: true });
  document.getElementById("messengerWindow").dispatchEvent(event);
}

function MsgToggleMessagePane(aToggleManually)
{
  // don't hide all three panes at once
  if (IsDisplayDeckCollapsed() && IsFolderPaneCollapsed())
    return;
  // toggle the splitter manually if it wasn't clicked and remember that
  var splitter = GetThreadAndMessagePaneSplitter();
  if (aToggleManually)
    MsgToggleSplitter(splitter);
  splitter.collapsible = aToggleManually;
  ChangeMessagePaneVisibility();
  UpdateLayoutVisibility();
}

function MsgToggleFolderPane(aToggleManually)
{
  // don't hide all three panes at once
  if (IsDisplayDeckCollapsed() && IsMessagePaneCollapsed())
    return;
  // toggle the splitter manually if it wasn't clicked and remember that
  var splitter = GetFolderPaneSplitter();
  if (aToggleManually)
    MsgToggleSplitter(splitter);
  splitter.collapsible = aToggleManually;
  UpdateLayoutVisibility();
}

function MsgToggleThreadPane()
{
  // don't hide all three panes at once
  if (IsFolderPaneCollapsed() && IsMessagePaneCollapsed())
    return;
  var threadPane = GetDisplayDeck();
  threadPane.collapsed = !threadPane.collapsed;
  // we only get here by hitting a key, so always hide border splitters
  UpdateLayoutVisibility();
}

// When the ThreadPane is hidden via the displayDeck, we should collapse the
// elements that are only meaningful to the thread pane. When AccountCentral is
// shown via the displayDeck, we need to switch the displayDeck to show the
// accountCentralBox and load the iframe in the AccountCentral box with the
// corresponding page.
function ShowAccountCentral(displayedFolder)
{
  GetDisplayDeck().selectedPanel = accountCentralBox;
  let acctCentralPage = GetLocalizedStringPref("mailnews.account_central_page.url");
  if (acctCentralPage) {
    let loadURL =
      acctCentralPage +
      (displayedFolder ? "?folderURI=" + displayedFolder : "");
    if (window.frames["accountCentralPane"].location.href != loadURL) {
      window.frames["accountCentralPane"].location.href = loadURL;
    }
  } else {
    dump("Error loading AccountCentral page\n");
  }
}

function ShowThreadPane()
{
  GetDisplayDeck().selectedPanel = GetThreadPane();
}

function ShowingThreadPane()
{
  gDisableViewsSearch.removeAttribute("disabled");
  var threadPaneSplitter = GetThreadAndMessagePaneSplitter();
  threadPaneSplitter.collapsed = false;
  if (!threadPaneSplitter.hidden && threadPaneSplitter.getAttribute("state") != "collapsed")
  {
    GetMessagePane().collapsed = false;
    // XXX We need to force the tree to refresh its new height
    // so that it will correctly scroll to the newest message
    GetThreadTree().boxObject.height;
  }
  document.getElementById("key_toggleThreadPane").removeAttribute("disabled");
  document.getElementById("key_toggleMessagePane").removeAttribute("disabled");
}

function HidingThreadPane()
{
  ClearThreadPane();
  GetUnreadCountElement().hidden = true;
  GetTotalCountElement().hidden = true;
  GetMessagePane().collapsed = true;
  GetThreadAndMessagePaneSplitter().collapsed = true;
  gDisableViewsSearch.setAttribute("disabled", true);
  document.getElementById("key_toggleThreadPane").setAttribute("disabled", "true");
  document.getElementById("key_toggleMessagePane").setAttribute("disabled", "true");
}

var gCurrentDisplayDeckId = "";
function ObserveDisplayDeckChange(aEvent)
{
  var selectedPanel = GetDisplayDeck().selectedPanel;
  var nowSelected = selectedPanel ? selectedPanel.id : "";
  // onselect fires for every mouse click inside the deck, so ObserveDisplayDeckChange
  // is getting called every time we click on a message in the thread pane.
  // Only show/hide elements if the selected deck is actually changing.
  if (nowSelected != gCurrentDisplayDeckId)
  {
    if (nowSelected == "threadPaneBox")
      ShowingThreadPane();
    else
      HidingThreadPane();

    if (nowSelected == "accountCentralBox") {
      if (!document.getElementById("folderPaneBox").collapsed)
        document.getElementById("folderTree").focus();
      gAccountCentralLoaded = true;
    } else {
      gAccountCentralLoaded = false;
    }
    gCurrentDisplayDeckId = nowSelected;
  }
}

function InvalidateTabDBs()
{
  // enforce reloading the tab's dbView
  var tabInfos = GetTabMail().tabInfo;
  for (let i = 0; i < tabInfos.length; ++i)
  {
    let tabInfo = tabInfos[i];
    // only reroot 3pane tabs
    if (tabInfo.mode.type == "3pane")
    {
      // don't change URI if already set -
      // we might try to read from an invalid msgSelectedFolder
      if (!("uriToOpen" in tabInfo))
        tabInfo.uriToOpen = tabInfo.msgSelectedFolder.URI;
    }
  }
}