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

  1. /* -*- Mode: C++; 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-2001 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  */
  20.  
  21. var gPrefs;
  22. var gOfflinePromptsBundle;
  23. var gPromptService;
  24. var gOfflineManager;
  25.  
  26.  
  27. function MailOfflineStateChanged(goingOffline)
  28. {
  29.   // tweak any mail UI here that needs to change when we go offline or come back online
  30.   gFolderJustSwitched = true;
  31. }
  32.  
  33. function MsgSettingsOffline()
  34. {
  35.     window.parent.MsgAccountManager('am-offline.xul');
  36. }
  37.  
  38. // Init PrefsService
  39. function GetPrefsService()
  40. {
  41.   // Store the prefs object
  42.   try {
  43.     var prefsService = Components.classes["@mozilla.org/preferences;1"];
  44.     if (prefsService)
  45.     prefsService = prefsService.getService();
  46.     if (prefsService)
  47.     gPrefs = prefsService.QueryInterface(Components.interfaces.nsIPref);
  48.  
  49.     if (!gPrefs)
  50.     dump("failed to get prefs service!\n");
  51.   }
  52.   catch(ex) {
  53.     dump("failed to get prefs service!\n");
  54.   }
  55. }
  56.  
  57. // Check for unsent messages
  58. function CheckForUnsentMessages()
  59. {
  60.   var am = Components.classes["@mozilla.org/messenger/account-manager;1"]
  61.                .getService(Components.interfaces.nsIMsgAccountManager);
  62.   var msgSendlater = Components.classes["@mozilla.org/messengercompose/sendlater;1"]
  63.                .getService(Components.interfaces.nsIMsgSendLater);
  64.   var identitiesCount, allIdentities, currentIdentity, numMessages, msgFolder;
  65.  
  66.   if(am) { 
  67.     allIdentities = am.allIdentities;
  68.     identitiesCount = allIdentities.Count();
  69.     for (var i = 0; i < identitiesCount; i++) {
  70.       currentIdentity = allIdentities.QueryElementAt(i, Components.interfaces.nsIMsgIdentity);
  71.       msgFolder = msgSendlater.getUnsentMessagesFolder(currentIdentity);
  72.       if(msgFolder) {
  73.         // if true, descends into all subfolders 
  74.         numMessages = msgFolder.getTotalMessages(false);
  75.         if(numMessages > 0) return true;
  76.       }
  77.     } 
  78.   }
  79.   return false;
  80. }
  81.  
  82. // Init nsIPromptService & strings.
  83. function InitPrompts()
  84. {
  85.   if(!gPromptService) {
  86.     gPromptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService();
  87.     gPromptService = gPromptService.QueryInterface(Components.interfaces.nsIPromptService);
  88.   }
  89.   if (!gOfflinePromptsBundle) 
  90.     gOfflinePromptsBundle = document.getElementById("bundle_offlinePrompts");
  91. }
  92.  
  93. // prompt for sending messages while going online, and go online.
  94. function PromptSendMessages()
  95. {
  96.   InitPrompts();
  97.   InitServices();
  98.  
  99.   if (gPromptService) {
  100.     var buttonPressed = {value:0};
  101.     var checkValue = {value:true};
  102.     gPromptService.confirmEx(window, 
  103.                             gOfflinePromptsBundle.getString('sendMessagesWindowTitle'), 
  104.                             gOfflinePromptsBundle.getString('sendMessagesLabel'),
  105.                             (gPromptService.BUTTON_TITLE_IS_STRING * gPromptService.BUTTON_POS_0) +
  106.                             (gPromptService.BUTTON_TITLE_IS_STRING * gPromptService.BUTTON_POS_2) +
  107.                             (gPromptService.BUTTON_TITLE_IS_STRING * gPromptService.BUTTON_POS_1),
  108.                             gOfflinePromptsBundle.getString('sendMessagesSendButtonLabel'),
  109.                             gOfflinePromptsBundle.getString('sendMessagesCancelButtonLabel'),
  110.                             gOfflinePromptsBundle.getString('sendMessagesNoSendButtonLabel'),
  111.                             gOfflinePromptsBundle.getString('sendMessagesCheckboxLabel'), 
  112.                             checkValue, buttonPressed);
  113.     if(buttonPressed) {
  114.       if(buttonPressed.value == 0) {
  115.         if(checkValue.value) 
  116.           gPrefs.SetIntPref("offline.send.unsent_messages", 0);
  117.         else 
  118.           gPrefs.SetIntPref("offline.send.unsent_messages", 1);
  119.  
  120.         gOfflineManager.goOnline(true /* sendUnsentMessages */, 
  121.                                  true /* playbackOfflineImapOperations */, 
  122.                                  msgWindow);
  123.         return true;
  124.       }
  125.       else if(buttonPressed.value == 1) {
  126.         return false;
  127.       }
  128.       else if(buttonPressed.value == 2) {
  129.         if(checkValue.value) 
  130.           gPrefs.SetIntPref("offline.send.unsent_messages", 0);
  131.         else 
  132.           gPrefs.SetIntPref("offline.send.unsent_messages", 2);
  133.         gOfflineManager.goOnline(false /* sendUnsentMessages */, 
  134.                                  true /* playbackOfflineImapOperations */, 
  135.                                  msgWindow);
  136.         return true;
  137.       }
  138.     }
  139.   }
  140. }
  141.  
  142. // prompt for downlading messages while going offline, and synchronise
  143. function PromptDownloadMessages()
  144. {
  145.   InitPrompts();
  146.   InitServices();
  147.  
  148.   if(gPromptService) {
  149.     var buttonPressed = {value:0};
  150.     var checkValue = {value:true};
  151.     gPromptService.confirmEx(window, 
  152.                             gOfflinePromptsBundle.getString('downloadMessagesWindowTitle'), 
  153.                             gOfflinePromptsBundle.getString('downloadMessagesLabel'),
  154.                             (gPromptService.BUTTON_TITLE_IS_STRING * gPromptService.BUTTON_POS_0) +
  155.                             (gPromptService.BUTTON_TITLE_IS_STRING * gPromptService.BUTTON_POS_2) + 
  156.                             (gPromptService.BUTTON_TITLE_IS_STRING * gPromptService.BUTTON_POS_1),
  157.                             gOfflinePromptsBundle.getString('downloadMessagesDownloadButtonLabel'),
  158.                             gOfflinePromptsBundle.getString('downloadMessagesCancelButtonLabel'),
  159.                             gOfflinePromptsBundle.getString('downloadMessagesNoDownloadButtonLabel'), 
  160.                             gOfflinePromptsBundle.getString('downloadMessagesCheckboxLabel'), 
  161.                             checkValue, buttonPressed);
  162.     if(buttonPressed) {
  163.       if(buttonPressed.value == 0) {
  164.         if(checkValue.value) 
  165.           gPrefs.SetIntPref("offline.download.download_messages", 0);
  166.         else 
  167.           gPrefs.SetIntPref("offline.download.download_messages", 1);
  168.         // download news, download mail, send unsent messages, go offline when done, msg window
  169.         gOfflineManager.synchronizeForOffline(false, true, false, true, msgWindow);
  170.         return true;
  171.       }
  172.       else if(buttonPressed.value == 1) {
  173.         return false;
  174.       }
  175.       else if(buttonPressed.value == 2) {
  176.         if(checkValue.value) 
  177.           gPrefs.SetIntPref("offline.download.download_messages", 0);
  178.         else 
  179.           gPrefs.SetIntPref("offline.download.download_messages", 2);
  180.         // download news, download mail, send unsent messages, go offline when done, msg window
  181.         gOfflineManager.synchronizeForOffline(false, false, false, true, msgWindow);
  182.         return true;
  183.       }
  184.     }
  185.   }
  186. }
  187.  
  188. // online?
  189. function CheckOnline()
  190. {
  191.   var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  192.                          .getService(Components.interfaces.nsIIOService);
  193.   return (!ioService.offline);
  194. }
  195.  
  196. // Init Pref Service & Offline Manager
  197. function InitServices()
  198. {
  199.   if (!gPrefs) 
  200.     GetPrefsService();
  201.  
  202.   if (!gOfflineManager) 
  203.     GetOfflineMgrService();
  204. }
  205.  
  206. // Init Offline Manager
  207. function GetOfflineMgrService()
  208. {
  209.   if (!gOfflineManager) {
  210.     gOfflineManager = Components.classes["@mozilla.org/messenger/offline-manager;1"]                 
  211.         .getService(Components.interfaces.nsIMsgOfflineManager);
  212.   }
  213. }
  214.  
  215. // return false if you want to prevent the offline state change
  216. function MailCheckBeforeOfflineChange()
  217. {
  218.   var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  219.                             .getService(Components.interfaces.nsIIOService);
  220.  
  221.   var goingOnline = ioService.offline;
  222.   var bundle = srGetStrBundle("chrome://communicator/locale/utilityOverlay.properties");
  223.  
  224. //  messenger.SetWindow(window, msgWindow);
  225.  
  226.   InitServices();
  227.  
  228.   var prefSendUnsentMessages = gPrefs.GetIntPref("offline.send.unsent_messages");
  229.   var prefDownloadMessages   = gPrefs.GetIntPref("offline.download.download_messages");
  230.  
  231.   if(goingOnline) {
  232.     switch(prefSendUnsentMessages) { 
  233.     case 0:
  234.       if(CheckForUnsentMessages()) { 
  235.         if(! PromptSendMessages()) 
  236.           return false;
  237.       }
  238.       else 
  239.         gOfflineManager.goOnline(false /* sendUnsentMessages */, 
  240.                                  true /* playbackOfflineImapOperations */, 
  241.                                  msgWindow);
  242.       break;
  243.     case 1:
  244.       gOfflineManager.goOnline(CheckForUnsentMessages() /* sendUnsentMessages */, 
  245.                                true  /* playbackOfflineImapOperations */, 
  246.                                msgWindow);
  247.       break;
  248.     case 2:
  249.       gOfflineManager.goOnline(false /* sendUnsentMessages */, 
  250.                                true /* playbackOfflineImapOperations */, 
  251.                                msgWindow);
  252.       break;
  253.     }
  254.   }
  255.   else {
  256.     // going offline
  257.     switch(prefDownloadMessages) {    
  258.       case 0:
  259.         if(! PromptDownloadMessages()) return false;
  260.       break;
  261.       case 1:
  262.         // download news, download mail, send unsent messages, go offline when done, msg window
  263.         gOfflineManager.synchronizeForOffline(false, true, false, true, msgWindow);
  264.         break;
  265.       case 2:
  266.         // download news, download mail, send unsent messages, go offline when done, msg window
  267.         gOfflineManager.synchronizeForOffline(false, false, false, true, msgWindow);
  268.         break;
  269.     }
  270.   }
  271. }
  272.  
  273.