home *** CD-ROM | disk | FTP | other *** search
/ PC Format (PL) 2008 February / PC_Format_022008.iso / Internet / Mozilla Thunderbird wtyczki / lightning-0.7-tb-win.xpi / js / calFreeBusyService.js < prev    next >
Encoding:
JavaScript  |  2007-09-19  |  5.7 KB  |  139 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Sun Microsystems code.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Sun Microsystems, Inc.
  18.  * Portions created by the Initial Developer are Copyright (C) 2007
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Daniel Boelzle <daniel.boelzle@sun.com>
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. function calFreeBusyListener(numOperations, finalListener) {
  39.     this.mFinalListener = finalListener;
  40.     this.mNumOperations = numOperations;
  41.     this.mResults = [];
  42.  
  43.     var this_ = this;
  44.     function cancelFunc() { // operation group has been cancelled
  45.         this_.notifyResult(null);
  46.     }
  47.     this.opGroup = new calOperationGroup(cancelFunc);
  48. }
  49. calFreeBusyListener.prototype = {
  50.     mFinalListener: null,
  51.     mNumOperations: 0,
  52.     mResults: null,
  53.     opGroup: null,
  54.  
  55.     notifyResult: function calFreeBusyListener_notifyResult(result) {
  56.         var listener = this.mFinalListener
  57.         if (listener) {
  58.             ASSERT(!this.opGroup.isPending, "[calFreeBusyListener_notifyResult] !this.opGroup.isPending");
  59.             this.mFinalListener = null;
  60.             listener.onResult(this.opGroup, result);
  61.         }
  62.     },
  63.  
  64.     // calIGenericOperationListener:
  65.     onResult: function calFreeBusyListener_onResult(aOperation, aResult) {
  66.         if (this.mFinalListener) {
  67.             if (aOperation.success) {
  68.                 this.mResults = this.mResults.concat(aResult);
  69.             }
  70.             --this.mNumOperations;
  71.             if (this.mNumOperations == 0) {
  72.                 this.opGroup.notifyCompleted();
  73.                 this.notifyResult(this.mResults);
  74.             }
  75.         }
  76.     }
  77. };
  78.  
  79. const calFreeBusyService_ifaces = [ Components.interfaces.nsISupports,
  80.                                     Components.interfaces.calIFreeBusyProvider,
  81.                                     Components.interfaces.calIFreeBusyService,
  82.                                     Components.interfaces.nsIClassInfo ];
  83.  
  84. function calFreeBusyService() {
  85.     this.wrappedJSObject = this;
  86.     this.mProviders = new calInterfaceBag(Components.interfaces.calIFreeBusyProvider);
  87. }
  88. calFreeBusyService.prototype = {
  89.     mProviders: null,
  90.  
  91.     QueryInterface: function calFreeBusyService_QueryInterface(aIID) {
  92.         ensureIID(calFreeBusyService_ifaces, aIID);
  93.         return this;
  94.     },
  95.  
  96.     // nsIClassInfo:
  97.     getInterfaces: function calFreeBusyService_getInterfaces(count) {
  98.         count.value = calFreeBusyService_ifaces.length;
  99.         return calFreeBusyService_ifaces;
  100.     },
  101.     getHelperForLanguage: function calFreeBusyService_getHelperForLanguage(language) { return null; },
  102.     contractID: "@mozilla.org/calendar/freebusy-service;1",
  103.     classDescription: "Calendar FreeBusy Service",
  104.     classID: Components.ID("{29C56CD5-D36E-453a-ACDE-0083BD4FE6D3}"),
  105.     implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
  106.     flags: 0,
  107.  
  108.     // calIFreeBusyProvider:
  109.     getFreeBusyIntervals: function calFreeBusyService_getFreeBusyIntervals(aCalId,
  110.                                                                            aRangeStart,
  111.                                                                            aRangeEnd,
  112.                                                                            aBusyTypes,
  113.                                                                            aListener) {
  114.         var groupListener = new calFreeBusyListener(this.mProviders.size, aListener);
  115.         function getFreeBusyIntervals_(provider) {
  116.             try {
  117.                 groupListener.opGroup.add(provider.getFreeBusyIntervals(aCalId,
  118.                                                                         aRangeStart,
  119.                                                                         aRangeEnd,
  120.                                                                         aBusyTypes,
  121.                                                                         groupListener));
  122.             } catch (exc) {
  123.                 Components.utils.reportError(exc);
  124.                 groupListener.onResult(null, []); // dummy to adopt mNumOperations
  125.             }
  126.         }
  127.         this.mProviders.forEach(getFreeBusyIntervals_);
  128.         return groupListener.opGroup;
  129.     },
  130.  
  131.     // calIFreeBusyService:
  132.     addProvider: function calFreeBusyListener_addProvider(aProvider) {
  133.         this.mProviders.add(aProvider);
  134.     },
  135.     removeProvider: function calFreeBusyListener_removeProvider(aProvider) {
  136.         this.mProviders.remove(aProvider);
  137.     }
  138. };
  139.