home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 June / PCpro_2006_06.ISO / files / firefox / calendar_windows_latest.xpi / components / calAlarmMonitor.js next >
Encoding:
Text File  |  2006-01-21  |  7.1 KB  |  192 lines

  1. /* -*- Mode: javascript; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  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 Oracle Corporation code.
  16.  *
  17.  * The Initial Developer of the Original Code is Oracle Corporation
  18.  * Portions created by the Initial Developer are Copyright (C) 2005
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Stuart Parmenter <stuart.parmenter@oracle.com>
  23.  *   Joey Minta       <jminta@gmail.com>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. const CI = Components.interfaces;
  40.  
  41. function getAlarmService()
  42. {
  43.     return Components.classes["@mozilla.org/calendar/alarm-service;1"].getService(CI.calIAlarmService);
  44. }
  45.  
  46. var alarmServiceObserver = {
  47.     onAlarm: function( event ) {
  48.  
  49.         //If an event has alarm email address, we assume the alarm is of email
  50.         //type.  See similar note in eventDialog.js
  51.         if(event.alarmEmailAddress && event.alarmEmailAddress != "") {
  52.             //XXX Re-implement this!
  53.             return;
  54.         }
  55.  
  56.         //The alarm dialog will play a sound, but if it isn't opened, we need
  57.         //to play a sound on our own here.
  58.         var prefService = Components.classes["@mozilla.org/preferences-service;1"]
  59.                           .getService(Components.interfaces.nsIPrefService);
  60.         var calendarPrefs = prefService.getBranch("calendar.alarms.");
  61.         if(!calendarPrefs.getBoolPref("show") && 
  62.            calendarPrefs.getBoolPref("playsound")) {
  63.             playSound();
  64.             return;
  65.         }
  66.  
  67.         //Check to see if an alarm window already exists.  It not, create it.
  68.  
  69.         var wMediator = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  70.                                   .getService(Components.interfaces.nsIWindowMediator);
  71.         var gAlarmWindow = wMediator.getMostRecentWindow("calendarAlarmWindow");
  72.         if( !gAlarmWindow ) {
  73.             var wWatcher = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  74.                                      .getService(Components.interfaces.nsIWindowWatcher);
  75.             gAlarmWindow = wWatcher.openWindow(null,
  76.                                                "chrome://calendar/content/calendar-alarm-dialog.xul",
  77.                                                "_blank",
  78.                                                "chrome,dialog=yes,all",
  79.                                                 null);
  80.  
  81.         }
  82.         gAlarmWindow.focus();
  83.         if ("addAlarm" in gAlarmWindow) {
  84.             gAlarmWindow.addAlarm(event);
  85.         } 
  86.         else {
  87.             var addAlarm = function() {
  88.                 gAlarmWindow.addAlarm( event );
  89.             }
  90.             gAlarmWindow.addEventListener("load", addAlarm, false);
  91.         }
  92.     }
  93. };
  94.  
  95. function playSound() {
  96.     var sound = Components.classes["@mozilla.org/sound;1"]
  97.                 .createInstance( Components.interfaces.nsISound );
  98.     var prefService = Components.classes["@mozilla.org/preferences-service;1"]
  99.                       .getService(Components.interfaces.nsIPrefService);
  100.     var calendarPrefs = prefService.getBranch("calendar.alarms.");
  101.     try {
  102.         soundURL = calendarPrefs.getCharPref("soundURL");
  103.         var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  104.                         .getService(Components.interfaces.nsIIOService);
  105.         var url = ioService.newURI(soundURL, null, null);
  106.         sound.init();
  107.         sound.play( url );
  108.     }
  109.     catch (ex) {
  110.        sound.beep();
  111.        dump("unable to play sound...\n" + ex + "\n");
  112.     }
  113. }
  114.  
  115. function calAlarmMonitor() { }
  116.  
  117. calAlarmMonitor.prototype = {
  118.     QueryInterface: function (aIID) {
  119.         if (!aIID.equals(CI.nsISupports) &&
  120.             !aIID.equals(CI.nsIObserver))
  121.             throw Components.interfaces.NS_ERROR_NO_INTERFACE;
  122.  
  123.         return this;
  124.     },
  125.  
  126.     /* nsIObserver */
  127.     observe: function (subject, topic, data) {
  128.         switch (topic) {
  129.         case "alarm-service-startup":
  130.             getAlarmService().addObserver(alarmServiceObserver);
  131.             break;
  132.  
  133.         case "alarm-service-shutdown":
  134.             getAlarmService().removeObserver(alarmServiceObserver);
  135.             break;
  136.         }
  137.     },
  138. };
  139.  
  140. var myModule = {
  141.     registerSelf: function (compMgr, fileSpec, location, type) {
  142.         compMgr = compMgr.QueryInterface(CI.nsIComponentRegistrar);
  143.         compMgr.registerFactoryLocation(this.myCID,
  144.                                         "Calendar Alarm Monitor",
  145.                                         this.myContractID,
  146.                                         fileSpec,
  147.                                         location,
  148.                                         type);
  149.  
  150.         var catman = Components.classes["@mozilla.org/categorymanager;1"]
  151.             .getService(CI.nsICategoryManager);
  152.  
  153.         catman.addCategoryEntry("alarm-service-startup", "calendar",
  154.                                 "service," + this.myContractID, true, true);
  155.         catman.addCategoryEntry("alarm-service-shutdown", "calendar",
  156.                                 "service," + this.myContractID, true, true);
  157.     },
  158.  
  159.     getClassObject: function (compMgr, cid, iid) {
  160.         if (!cid.equals(this.myCID))
  161.             throw Components.results.NS_ERROR_NO_INTERFACE;
  162.  
  163.         if (!iid.equals(CI.nsIFactory))
  164.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  165.  
  166.         return this.myFactory;
  167.     },
  168.  
  169.     myCID: Components.ID("{4b7ae030-ed79-11d9-8cd6-0800200c9a66}"),
  170.  
  171.     myContractID: "@mozilla.org/calendar/alarm-monitor;1",
  172.  
  173.     myFactory: {
  174.         createInstance: function (outer, iid) {
  175.             if (outer != null)
  176.                 throw Components.results.NS_ERROR_NO_AGGREGATION;
  177.  
  178.             return (new calAlarmMonitor()).QueryInterface(iid);
  179.         }
  180.     },
  181.  
  182.     canUnload: function(compMgr) {
  183.         return true;
  184.     }
  185. };
  186.  
  187. function NSGetModule(compMgr, fileSpec) {
  188.     return myModule;
  189. }
  190.  
  191.  
  192.