home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Complet / thunderbird / components / offlineStartup.js < prev    next >
Encoding:
Text File  |  2004-02-07  |  9.0 KB  |  261 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  * ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is the Offline Startup Handler
  16.  *
  17.  *
  18.  * Contributor(s):
  19.  *  David Bienvenu <bienvenu@nventure.com> (Original Author) 
  20.  *
  21.  * Alternatively, the contents of this file may be used under the terms of
  22.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  23.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  24.  * in which case the provisions of the GPL or the LGPL are applicable instead
  25.  * of those above. If you wish to allow use of your version of this file only
  26.  * under the terms of either the GPL or the LGPL, and not to allow others to
  27.  * use your version of this file under the terms of the MPL, indicate your
  28.  * decision by deleting the provisions above and replace them with the notice
  29.  * and other provisions required by the GPL or the LGPL. If you do not delete
  30.  * the provisions above, a recipient may use your version of this file under
  31.  * the terms of any one of the MPL, the GPL or the LGPL.
  32.  *
  33.  * ***** END LICENSE BLOCK ***** */
  34. const kDebug               = false;
  35. const kBundleURI         = "chrome://messenger/locale/offlineStartup.properties";
  36. const kOfflineStartupPref = "offline.startup_state";
  37. var gShuttingDown = false;
  38. var gOfflineStartupMode; //0 = remember last state, 1 = ask me, 2 == online, 3 == offline
  39. const kRememberLastState = 0;
  40. const kAskForOnlineState = 1;
  41. ////////////////////////////////////////////////////////////////////////
  42. // 
  43. //   nsOfflineStartup : nsIProfileStartupListener, nsIObserver
  44. //
  45. //   Check if the user has set the pref to be prompted for
  46. //   online/offline startup mode. If so, prompt the user. Also,
  47. //   check if the user wants to remember their offline state
  48. //   the next time they start up.
  49. //
  50. ////////////////////////////////////////////////////////////////////////
  51.  
  52. var nsOfflineStartup = 
  53. {
  54.   onProfileStartup: function(aProfileName)
  55.   {
  56.     debug("onProfileStartup");
  57.  
  58.       var prefs = Components.classes["@mozilla.org/preferences-service;1"].
  59.         getService(Components.interfaces.nsIPrefBranch);
  60.   
  61.       gOfflineStartupMode = prefs.getIntPref(kOfflineStartupPref);
  62.  
  63.     if (gOfflineStartupMode == kRememberLastState)
  64.     {    
  65.       var prefs = Components.classes["@mozilla.org/preferences-service;1"].
  66.         getService(Components.interfaces.nsIPrefBranch);
  67.       var offline = !prefs.getBoolPref("network.online");
  68.         var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
  69.         ioService.offline = offline;
  70.       var observerService = Components.
  71.         classes["@mozilla.org/observer-service;1"].
  72.         getService(Components.interfaces.nsIObserverService);
  73.       observerService.addObserver(this, "network:offline-status-changed", false);
  74.       observerService.addObserver(this, "quit-application", false);
  75.       observerService.addObserver(this, "xpcom-shutdown", false);
  76.     }
  77.     else if (gOfflineStartupMode == kAskForOnlineState)
  78.     {
  79.       var promptService = Components.
  80.         classes["@mozilla.org/embedcomp/prompt-service;1"].
  81.         getService(Components.interfaces.nsIPromptService);
  82.       
  83.       var bundle = getBundle(kBundleURI);
  84.       if (!bundle)
  85.         return;
  86.  
  87.       var title = bundle.GetStringFromName("title");
  88.       var desc = bundle.GetStringFromName("desc");
  89.       var button0Text = bundle.GetStringFromName("workOnline");
  90.       var button1Text = bundle.GetStringFromName("workOffline");
  91.       var checkVal = {value:0};
  92.  
  93.       var result = promptService.confirmEx(null, title, desc, 
  94.         (promptService.BUTTON_POS_0 * promptService.BUTTON_TITLE_IS_STRING) +
  95.         (promptService.BUTTON_POS_1 * promptService.BUTTON_TITLE_IS_STRING),
  96.         button0Text, button1Text, null, null, checkVal);
  97.       debug ("result = " + result + "\n");
  98.       if (result == 1)
  99.       {
  100.         var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
  101.         ioService.offline = true;
  102.       }
  103.  
  104.     }
  105.   },
  106.  
  107.   observe: function(aSubject, aTopic, aData)
  108.   {
  109.     debug("observe: " + aTopic);
  110.  
  111.     if (aTopic == "network:offline-status-changed")
  112.     {
  113.       debug("network:offline-status-changed: " + aData);
  114.       // if we're not shutting down, and startup mode is "remember online state"
  115.       if (!gShuttingDown && gOfflineStartupMode == kRememberLastState)
  116.       {
  117.         debug("remembering offline state: ");
  118.         var prefs = Components.classes["@mozilla.org/preferences-service;1"].
  119.           getService(Components.interfaces.nsIPrefBranch);
  120.         prefs.setBoolPref("network.online", aData == "online");
  121.       }
  122.  
  123.     }
  124.     else if (aTopic == "xpcom-shutdown" || aTopic == "quit-application")
  125.     {
  126.       gShuttingDown = true;
  127.     }
  128.   },
  129.  
  130.  
  131.   QueryInterface: function(aIID)
  132.   {
  133.     if (!aIID.equals(Components.interfaces.nsIObserver) &&
  134.         !aIID.equals(Components.interfaces.nsIProfileStartupListener) &&
  135.         !aIID.equals(Components.interfaces.nsISupports))
  136.       throw Components.results.NS_ERROR_NO_INTERFACE;
  137.  
  138.     return this;
  139.   }
  140. }
  141.  
  142.  
  143. var nsOfflineStartupModule = 
  144. {
  145.   mClassName:     "Offline Startup",
  146.   mContractID:    "@mozilla.org/offline-startup;1",
  147.   mClassID:       Components.ID("3028a3c8-2165-42a4-b878-398da5d32736"),
  148.  
  149.   getClassObject: function(aCompMgr, aCID, aIID)
  150.   {
  151.     if (!aCID.equals(this.mClassID))
  152.       throw Components.results.NS_ERROR_NO_INTERFACE;
  153.     if (!aIID.equals(Components.interfaces.nsIFactory))
  154.       throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  155.  
  156.     return this.mFactory;
  157.   },
  158.  
  159.   registerSelf: function(aCompMgr, aFileSpec, aLocation, aType)
  160.   {
  161.     if (kDebug)
  162.       dump("*** Registering nsOfflineStartupModule (a JavaScript Module)\n");
  163.  
  164.     aCompMgr = aCompMgr.QueryInterface(
  165.                  Components.interfaces.nsIComponentRegistrar);
  166.     aCompMgr.registerFactoryLocation(this.mClassID, this.mClassName, 
  167.       this.mContractID, aFileSpec, aLocation, aType);
  168.  
  169.     // receive startup notification from the profile manager
  170.     // (we get |createInstance()|d at startup-notification time)
  171.     this.getCategoryManager().addCategoryEntry("profile-startup-category", 
  172.       this.mContractID, "", true, true);
  173.   },
  174.  
  175.   unregisterSelf: function(aCompMgr, aFileSpec, aLocation)
  176.   {
  177.     aCompMgr = aCompMgr.QueryInterface(
  178.                  Components.interfaces.nsIComponentRegistrar);
  179.     aCompMgr.unregisterFactoryLocation(this.mClassID, aFileSpec);
  180.  
  181.     this.getCategoryManager().deleteCategoryEntry("profile-startup-category", 
  182.       this.mContractID, true);
  183.   },
  184.  
  185.   canUnload: function(aCompMgr)
  186.   {
  187.     return true;
  188.   },
  189.  
  190.   getCategoryManager: function()
  191.   {
  192.     return Components.classes["@mozilla.org/categorymanager;1"].
  193.       getService(Components.interfaces.nsICategoryManager);
  194.   },
  195.  
  196.   //////////////////////////////////////////////////////////////////////
  197.   //
  198.   //   mFactory : nsIFactory
  199.   //
  200.   //////////////////////////////////////////////////////////////////////
  201.   mFactory:
  202.   {
  203.     createInstance: function(aOuter, aIID)
  204.     {
  205.       if (aOuter != null)
  206.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  207.       if (!aIID.equals(Components.interfaces.nsIObserver) &&
  208.           !aIID.equals(Components.interfaces.nsIProfileStartupListener) &&
  209.           !aIID.equals(Components.interfaces.nsISupports))
  210.         throw Components.results.NS_ERROR_INVALID_ARG;
  211.  
  212.       // return the singleton 
  213.       return nsOfflineStartup.QueryInterface(aIID);
  214.     },
  215.  
  216.     lockFactory: function(aLock)
  217.     {
  218.       // quiten warnings
  219.     }
  220.   }
  221. };
  222.  
  223. function NSGetModule(aCompMgr, aFileSpec)
  224. {
  225.   return nsOfflineStartupModule;
  226. }
  227.  
  228. function getBundle(aURI)
  229. {
  230.   if (!aURI)
  231.     return null;
  232.  
  233.   debug(aURI);
  234.   var bundle = null;
  235.   try
  236.   {
  237.     var strBundleService = Components.
  238.       classes["@mozilla.org/intl/stringbundle;1"].
  239.       getService(Components.interfaces.nsIStringBundleService);
  240.     bundle = strBundleService.createBundle(aURI);
  241.   }
  242.   catch (ex)
  243.   {
  244.     bundle = null;
  245.     debug("Exception getting bundle " + aURI + ": " + ex);
  246.   }
  247.  
  248.   return bundle;
  249. }
  250.  
  251.  
  252. ////////////////////////////////////////////////////////////////////////
  253. //
  254. //   Debug helper
  255. //
  256. ////////////////////////////////////////////////////////////////////////
  257. if (!kDebug)
  258.   debug = function(m) {};
  259. else
  260.   debug = function(m) {dump("\t *** nsOfflineStartup: " + m + "\n");};
  261.