home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / 01_02.iso / software / netscape62win / mail.xpi / bin / chrome / messenger.jar / content / messenger / mailWindow.js < prev    next >
Text File  |  2001-08-22  |  18KB  |  549 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributors(s):
  21.  *   Jan Varga <varga@utcru.sk>
  22.  *   Hσkan Waara (hwaara@chello.se)
  23.  */
  24.  
  25.  //This file stores variables common to mail windows
  26. var messengerContractID        = "@mozilla.org/messenger;1";
  27. var statusFeedbackContractID   = "@mozilla.org/messenger/statusfeedback;1";
  28. var mailSessionContractID      = "@mozilla.org/messenger/services/session;1";
  29. var secureUIContractID         = "@mozilla.org/secure_browser_ui;1";
  30.  
  31.  
  32. var prefContractID             = "@mozilla.org/preferences;1";
  33. var msgWindowContractID      = "@mozilla.org/messenger/msgwindow;1";
  34.  
  35. var messenger;
  36. var pref;
  37. var statusFeedback;
  38. var messagePaneController;
  39. var msgWindow;
  40.  
  41. var msgComposeService;
  42. var accountManager;
  43. var RDF;
  44. var msgComposeType;
  45. var msgComposeFormat;
  46.  
  47. var mailSession;
  48.  
  49. var gMessengerBundle;
  50. var gBrandBundle;
  51.  
  52. var datasourceContractIDPrefix = "@mozilla.org/rdf/datasource;1?name=";
  53. var accountManagerDSContractID = datasourceContractIDPrefix + "msgaccountmanager";
  54. var folderDSContractID         = datasourceContractIDPrefix + "mailnewsfolders";
  55.  
  56. var accountManagerDataSource;
  57. var folderDataSource;
  58.  
  59. var messagesBox = null;
  60. var accountCentralBox = null;
  61. var gAccountCentralLoaded = false;
  62. var gPaneConfig = null;
  63. //End progress and Status variables
  64.  
  65. // for checking if the folder loaded is Draft or Unsent which msg is editable
  66. var gIsEditableMsgFolder = false;
  67. var gOfflineManager;
  68.  
  69.  
  70. function OnMailWindowUnload()
  71. {
  72.   RemoveMailOfflineObserver();
  73.  
  74.   var dbview = GetDBView();
  75.   if (dbview) {
  76.     dbview.close(); 
  77.   }
  78.  
  79.   var mailSession = Components.classes[mailSessionContractID].getService();
  80.   if(mailSession)
  81.   {
  82.     mailSession = mailSession.QueryInterface(Components.interfaces.nsIMsgMailSession);
  83.     if(mailSession)
  84.     {
  85.       mailSession.RemoveFolderListener(folderListener);
  86.     }
  87.   }
  88.  
  89.   mailSession.RemoveMsgWindow(msgWindow);
  90.   messenger.SetWindow(null, null);
  91.  
  92.   var msgDS = folderDataSource.QueryInterface(Components.interfaces.nsIMsgRDFDataSource);
  93.   msgDS.window = null;
  94.  
  95.   msgDS = accountManagerDataSource.QueryInterface(Components.interfaces.nsIMsgRDFDataSource);
  96.   msgDS.window = null;
  97.  
  98.  
  99.   msgWindow.closeWindow();
  100.  
  101. }
  102.  
  103. function CreateMailWindowGlobals()
  104. {
  105.   // get the messenger instance
  106.   messenger = Components.classes[messengerContractID].createInstance();
  107.   messenger = messenger.QueryInterface(Components.interfaces.nsIMessenger);
  108.  
  109.   pref = Components.classes[prefContractID].getService(Components.interfaces.nsIPref);
  110.  
  111.   //Create windows status feedback
  112.   // set the JS implementation of status feedback before creating the c++ one..
  113.   window.MsgStatusFeedback = new nsMsgStatusFeedback();
  114.   // double register the status feedback object as the xul browser window implementation
  115.   window.XULBrowserWindow = window.MsgStatusFeedback;
  116.  
  117.   statusFeedback           = Components.classes[statusFeedbackContractID].createInstance();
  118.   statusFeedback = statusFeedback.QueryInterface(Components.interfaces.nsIMsgStatusFeedback);
  119.  
  120.   // try to create and register ourselves with a security icon...
  121.   var securityIcon = document.getElementById("security-button");
  122.   if (securityIcon)
  123.   {
  124.     // if the client isn't built with psm enabled then we won't have a secure UI to monitor the lock icon
  125.     // so be sure to wrap this in a try / catch clause...
  126.     try
  127.     {
  128.       var secureUI = Components.classes[secureUIContractID].createInstance();
  129.       // we may not have a secure UI if psm isn't installed!
  130.       if (secureUI)
  131.       {
  132.         secureUI = secureUI.QueryInterface(Components.interfaces.nsISecureBrowserUI);
  133.         secureUI.init(_content, securityIcon);
  134.       }
  135.     }
  136.     catch (ex) {}
  137.   }
  138.  
  139.   window.MsgWindowCommands = new nsMsgWindowCommands();
  140.  
  141.   //Create message window object
  142.   msgWindow = Components.classes[msgWindowContractID].createInstance();
  143.   msgWindow = msgWindow.QueryInterface(Components.interfaces.nsIMsgWindow);
  144.  
  145.   msgComposeService = Components.classes['@mozilla.org/messengercompose;1'].getService();
  146.   msgComposeService = msgComposeService.QueryInterface(Components.interfaces.nsIMsgComposeService);
  147.  
  148.   mailSession = Components.classes["@mozilla.org/messenger/services/session;1"].getService(Components.interfaces.nsIMsgMailSession);
  149.  
  150.   accountManager = Components.classes["@mozilla.org/messenger/account-manager;1"].getService(Components.interfaces.nsIMsgAccountManager);
  151.  
  152.   RDF = Components.classes['@mozilla.org/rdf/rdf-service;1'].getService();
  153.   RDF = RDF.QueryInterface(Components.interfaces.nsIRDFService);
  154.  
  155.   msgComposeType = Components.interfaces.nsIMsgCompType;
  156.   msgComposeFormat = Components.interfaces.nsIMsgCompFormat;
  157.  
  158.   gMessengerBundle = document.getElementById("bundle_messenger");
  159.   gBrandBundle = document.getElementById("bundle_brand");
  160.  
  161.   //Create datasources
  162.   accountManagerDataSource = Components.classes[accountManagerDSContractID].createInstance();
  163.   folderDataSource         = Components.classes[folderDSContractID].createInstance();
  164.  
  165.   messagesBox       = document.getElementById("messagesBox");
  166.   accountCentralBox = document.getElementById("accountCentralBox");
  167.   gPaneConfig = pref.GetIntPref("mail.pane_config");
  168. }
  169.  
  170. function InitMsgWindow()
  171. {
  172.   msgWindow.messagePaneController = new nsMessagePaneController();
  173.   msgWindow.statusFeedback = statusFeedback;
  174.   msgWindow.msgHeaderSink = messageHeaderSink;
  175.   msgWindow.SetDOMWindow(window);
  176.   mailSession.AddMsgWindow(msgWindow);
  177. }
  178.  
  179. function AddDataSources()
  180. {
  181.  
  182.   accountManagerDataSource = accountManagerDataSource.QueryInterface(Components.interfaces.nsIRDFDataSource);
  183.   folderDataSource = folderDataSource.QueryInterface(Components.interfaces.nsIRDFDataSource);
  184.   //to move menu item
  185.   SetupMoveCopyMenus('moveMenu', accountManagerDataSource, folderDataSource);
  186.  
  187.   //to copy menu item
  188.   SetupMoveCopyMenus('copyMenu', accountManagerDataSource, folderDataSource);
  189.  
  190.  
  191.   //To FileButton menu
  192.   SetupMoveCopyMenus('button-file', accountManagerDataSource, folderDataSource);
  193.  
  194.   //To move and copy menus in message pane context
  195.   SetupMoveCopyMenus("messagePaneContext-copyMenu", accountManagerDataSource, folderDataSource);
  196.   SetupMoveCopyMenus("messagePaneContext-moveMenu", accountManagerDataSource, folderDataSource);
  197.  
  198.   //Add statusFeedback
  199.  
  200.   var msgDS = folderDataSource.QueryInterface(Components.interfaces.nsIMsgRDFDataSource);
  201.   msgDS.window = msgWindow;
  202.  
  203.   msgDS = accountManagerDataSource.QueryInterface(Components.interfaces.nsIMsgRDFDataSource);
  204.   msgDS.window = msgWindow;
  205.  
  206. }
  207.  
  208. function SetupMoveCopyMenus(menuid, accountManagerDataSource, folderDataSource)
  209. {
  210.   var menu = document.getElementById(menuid);
  211.   if(menu)
  212.   {
  213.     menu.database.AddDataSource(accountManagerDataSource);
  214.     menu.database.AddDataSource(folderDataSource);
  215.     menu.setAttribute('ref', 'msgaccounts:/');
  216.   }
  217. }
  218. function dumpProgress() {
  219.     var broadcaster = document.getElementById("Messenger:LoadingProgress");
  220.     dump( "broadcaster mode=" + broadcaster.getAttribute("mode") + "\n" );
  221.     dump( "broadcaster value=" + broadcaster.getAttribute("value") + "\n" );
  222.     dump( "meter mode=" + meter.getAttribute("mode") + "\n" );
  223.     dump( "meter value=" + meter.getAttribute("value") + "\n" );
  224. }
  225.  
  226. // We're going to implement our status feedback for the mail window in JS now.
  227. // the following contains the implementation of our status feedback object
  228.  
  229. function nsMsgStatusFeedback()
  230. {
  231. }
  232.  
  233. nsMsgStatusFeedback.prototype =
  234. {
  235.   // global variables for status / feedback information....
  236.   startTime  : 0,
  237.   statusTextFld : null,
  238.   statusBar     : null,
  239.   throbber      : null,
  240.   stopMenu      : null,
  241.   stopButton    : null,
  242.   startTimeoutID : null,
  243.   stopTimeoutID  : null,
  244.   pendingStartRequests : 0,
  245.   meteorsSpinning : false,
  246.  
  247.   ensureStatusFields : function()
  248.     {
  249.       if (!this.statusTextFld ) this.statusTextFld = document.getElementById("statusText");
  250.       if (!this.statusBar) this.statusBar = document.getElementById("statusbar-icon");
  251.       if(!this.throbber)   this.throbber = document.getElementById("navigator-throbber");
  252.       if(!this.stopButton) this.stopButton = document.getElementById("button-stop");
  253.       if(!this.stopMenu)   this.stopMenu = document.getElementById("stopMenuitem");
  254.     },
  255.  
  256.   // nsIXULBrowserWindow implementation
  257.   setJSStatus : function(status)
  258.     {
  259.     },
  260.   setJSDefaultStatus : function(status)
  261.     {
  262.     },
  263.   setOverLink : function(link)
  264.     {
  265.       this.showStatusString(link);
  266.     },
  267.   QueryInterface : function(iid)
  268.     {
  269.       if (iid.equals(Components.interfaces.nsIMsgStatusFeedback) ||
  270.           iid.equals(Components.interfaces.nsIXULBrowserWindow))
  271.         return this;
  272.       throw Components.results.NS_NOINTERFACE;
  273.     },
  274.  
  275.   // nsIMsgStatusFeedback implementation.
  276.   showStatusString : function(statusText)
  277.     {
  278.       this.ensureStatusFields();
  279.       if ( statusText == "" )
  280.         statusText = defaultStatus;
  281.       this.statusTextFld.label = statusText;
  282.   },
  283.   _startMeteors : function()
  284.     {
  285.       this.ensureStatusFields();
  286.  
  287.       this.meteorsSpinning = true;
  288.       this.startTimeoutID = null;
  289.  
  290.       // Turn progress meter on.
  291.       this.statusBar.setAttribute("mode","undetermined");
  292.  
  293.       // turn throbber on
  294.       this.throbber.setAttribute("busy", true);
  295.  
  296.       //turn on stop button and menu
  297.     this.stopButton.setAttribute("disabled", false);
  298.     this.stopMenu.setAttribute("disabled", false);
  299.  
  300.       // Remember when loading commenced.
  301.       this.startTime = (new Date()).getTime();
  302.     },
  303.   startMeteors : function()
  304.     {
  305.       this.pendingStartRequests++;
  306.       // if we don't already have a start meteor timeout pending
  307.       // and the meteors aren't spinning, then kick off a start
  308.       if (!this.startTimeoutID && !this.meteorsSpinning)
  309.         this.startTimeoutID = setTimeout('window.MsgStatusFeedback._startMeteors();', 500);
  310.  
  311.       // since we are going to start up the throbber no sense in processing
  312.       // a stop timeout...
  313.       if (this.stopTimeoutID)
  314.       {
  315.         clearTimeout(this.stopTimeoutID);
  316.         this.stopTimeoutID = null;
  317.       }
  318.   },
  319.    _stopMeteors : function()
  320.     {
  321.       this.ensureStatusFields();
  322.       // Record page loading time.
  323.       var elapsed = ( (new Date()).getTime() - this.startTime ) / 1000;
  324.       var msg = gMessengerBundle.getFormattedString("documentDoneTime",
  325.                                                     [ elapsed ]);
  326.       this.showStatusString(msg);
  327.       defaultStatus = msg;
  328.  
  329.       this.throbber.setAttribute("busy", false);
  330.  
  331.       // Turn progress meter off.
  332.       this.statusBar.setAttribute("mode","normal");
  333.       this.statusBar.value = 0;  // be sure to clear the progress bar
  334.       this.statusBar.label = "";
  335.       this.stopButton.setAttribute("disabled", true);
  336.       this.stopMenu.setAttribute("disabled", true);
  337.  
  338.       this.meteorsSpinning = false;
  339.       this.stopTimeoutID = null;
  340.     },
  341.    stopMeteors : function()
  342.     {
  343.       if (this.pendingStartRequests > 0)
  344.         this.pendingStartRequests--;
  345.  
  346.       // if we are going to be starting the meteors, cancel the start
  347.       if (this.pendingStartRequests == 0 && this.startTimeoutID)
  348.       {
  349.         clearTimeout(this.startTimeoutID);
  350.         this.startTimeoutID = null;
  351.       }
  352.  
  353.       // if we have no more pending starts and we don't have a stop timeout already in progress
  354.       // AND the meteors are currently running then fire a stop timeout to shut them down.
  355.       if (this.pendingStartRequests == 0 && !this.stopTimeoutID)
  356.       {
  357.         if (this.meteorsSpinning)
  358.           this.stopTimeoutID = setTimeout('window.MsgStatusFeedback._stopMeteors();', 500);
  359.       }
  360.   },
  361.   showProgress : function(percentage)
  362.     {
  363.       this.ensureStatusFields();
  364.       if (percentage >= 0)
  365.       {
  366.         this.statusBar.setAttribute("mode", "normal");
  367.         this.statusBar.value = percentage;
  368.         this.statusBar.label = Math.round(percentage) + "%";
  369.       }
  370.     },
  371.   closeWindow : function(percent)
  372.     {
  373.   }
  374. }
  375.  
  376.  
  377. function nsMsgWindowCommands()
  378. {
  379. }
  380.  
  381. nsMsgWindowCommands.prototype =
  382. {
  383.   QueryInterface : function(iid)
  384.   {
  385.     if(iid.equals(Components.interfaces.nsIMsgWindowCommands))
  386.       return this;
  387.     throw Components.results.NS_NOINTERFACE;
  388.         return null;
  389.   },
  390.   SelectFolder: function(folderUri)
  391.   {
  392.     SelectFolder(folderUri);
  393.  
  394.   },
  395.   SelectMessage: function(messageUri)
  396.   {
  397.     SelectMessage(messageUri);
  398.   }
  399. }
  400.  
  401. function nsMessagePaneController()
  402. {
  403. }
  404.  
  405. nsMessagePaneController.prototype =
  406. {
  407.   QueryInterface : function(iid)
  408.   {
  409.     if(iid.equals(Components.interfaces.nsIMsgMessagePaneController))
  410.       return this;
  411.     throw Components.results.NS_NOINTERFACE;
  412.         return null;
  413.   },
  414.   clearMsgPane: function()
  415.   {
  416.     ClearMessagePane();
  417.   }
  418. }
  419.  
  420. function StopUrls()
  421. {
  422.   msgWindow.StopUrls();
  423. }
  424.  
  425. function loadStartPage() {
  426.     try {
  427.     var startpageenabled= pref.GetBoolPref("mailnews.start_page.enabled");
  428.  
  429.     if (startpageenabled) {
  430.       var startpage = pref.getLocalizedUnicharPref("mailnews.start_page.url");
  431.             if (startpage != "") {
  432.                 // first, clear out the charset setting.
  433.                 messenger.setDisplayCharset("");
  434.  
  435.                 window.frames["messagepane"].location = startpage;
  436.                 //dump("start message pane with: " + startpage + "\n");
  437.                 ClearMessageSelection();
  438.             }
  439.         }
  440.   }
  441.     catch (ex) {
  442.         dump("Error loading start page.\n");
  443.         return;
  444.     }
  445. }
  446.  
  447. // Display AccountCentral page when users clicks on the Account Folder.
  448. // When AccountCentral page need to be shown, we need to hide
  449. // the box containing threadPane, splitter and messagePane.
  450. // Load iframe in the AccountCentral box with corresponding page
  451. function ShowAccountCentral()
  452. {
  453.     try
  454.     {
  455.         var acctCentralPage = pref.getLocalizedUnicharPref("mailnews.account_central_page.url");
  456.         switch (gPaneConfig)
  457.         {
  458.             case 0:
  459.                 messagesBox.setAttribute("collapsed", "true");
  460.                 accountCentralBox.removeAttribute("collapsed");
  461.                 window.frames["accountCentralPane"].location = acctCentralPage;
  462.                 gAccountCentralLoaded = true;
  463.                 break;
  464.  
  465.             case 1:
  466.                 var messagePaneBox = document.getElementById("messagepanebox");
  467.                 messagePaneBox.setAttribute("collapsed", "true");
  468.                 var threadPaneBox = document.getElementById("threadpaneBox");
  469.                 threadPaneBox.setAttribute("collapsed", "true");
  470.                 var threadPaneSplitter = document.getElementById("threadpane-splitter");
  471.                 threadPaneSplitter.setAttribute("collapsed", "true");
  472.                 accountCentralBox.removeAttribute("collapsed");
  473.                 window.frames["accountCentralPane"].location = acctCentralPage;
  474.                 gAccountCentralLoaded = true;
  475.                 break;
  476.         }
  477.     }
  478.     catch (ex)
  479.     {
  480.         dump("Error loading AccountCentral page -> " + ex + "\n");
  481.         return;
  482.     }
  483. }
  484.  
  485. // Display thread and message panes with splitter when user tries
  486. // to read messages by clicking on msgfolders. Hide AccountCentral
  487. // box and display message box.
  488. function HideAccountCentral()
  489. {
  490.     try
  491.     {
  492.         switch (gPaneConfig)
  493.         {
  494.             case 0:
  495.                 accountCentralBox.setAttribute("collapsed", "true");
  496.                 messagesBox.removeAttribute("collapsed");
  497.                 gAccountCentralLoaded = false;
  498.                 break;
  499.  
  500.             case 1:
  501.                 accountCentralBox.setAttribute("collapsed", "true");
  502.                 var messagePaneBox = document.getElementById("messagepanebox");
  503.                 messagePaneBox.removeAttribute("collapsed");
  504.                 var threadPaneBox = document.getElementById("threadpaneBox");
  505.                 threadPaneBox.removeAttribute("collapsed");
  506.                 var threadPaneSplitter = document.getElementById("threadpane-splitter");
  507.                 threadPaneSplitter.removeAttribute("collapsed");
  508.                 gAccountCentralLoaded = false;
  509.                 break;
  510.         }
  511.     }
  512.     catch (ex)
  513.     {
  514.         dump("Error hiding AccountCentral page -> " + ex + "\n");
  515.         return;
  516.     }
  517. }
  518.  
  519. // Given the server, open the twisty and the set the selection
  520. // on inbox of that server. 
  521. // prompt if offline.
  522. function OpenInboxForServer(server)
  523. {
  524.     try {
  525.         HideAccountCentral();
  526.         var inboxFolder = GetInboxFolder(server);
  527.         SelectFolder(inboxFolder.URI);
  528.  
  529.         if(CheckOnline())
  530.             GetMessagesForInboxOnServer(server);
  531.         else {
  532.             var option = PromptGetMessagesOffline();
  533.             if(option == 0) {
  534.                 if (!gOfflineManager) 
  535.                     GetOfflineMgrService();
  536.  
  537.                 gOfflineManager.goOnline(false /* sendUnsentMessages */, 
  538.                                          false /* playbackOfflineImapOperations */, 
  539.                                          msgWindow);
  540.                 GetMessagesForInboxOnServer(server);
  541.             }
  542.         }
  543.     }
  544.     catch (ex) {
  545.         dump("Error opening inbox for server -> " + ex + "\n");
  546.         return;
  547.     }
  548. }
  549.