home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / sunbird / js / calWcapCalendar.js < prev    next >
Encoding:
JavaScript  |  2007-05-23  |  14.5 KB  |  425 lines

  1. /* -*- Mode: javascript; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public
  6.  * License Version 1.1 (the "License"); you may not use this file
  7.  * except in compliance with the License. You may obtain a copy of
  8.  * the License at http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS
  11.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  12.  * implied. See the License for the specific language governing
  13.  * rights and limitations under the License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is Sun Microsystems, Inc.
  18.  * Portions created by Sun Microsystems are Copyright (C) 2006 Sun
  19.  * Microsystems, Inc. All Rights Reserved.
  20.  *
  21.  * Original Author: Daniel Boelzle (daniel.boelzle@sun.com)
  22.  *
  23.  * Contributor(s):
  24.  *
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  28.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the NPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the NPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. function createWcapCalendar(session, /*optional*/calProps)
  41. {
  42.     var cal = new calWcapCalendar(session, calProps);
  43. //     switch (CACHE) {
  44. //     case "memory":
  45. //     case "storage":
  46. //         // wrap it up:
  47. //         var cal_ = new calWcapCachedCalendar();
  48. //         cal_.remoteCal = cal;
  49. //         cal = cal_;
  50. //         break;
  51. //     }
  52.     return cal;
  53. }
  54.  
  55. function calWcapCalendar(session, /*optional*/calProps) {
  56.     this.wrappedJSObject = this;
  57.     this.m_session = session;
  58.     this.m_calProps = calProps;
  59.     this.m_bSuppressAlarms = SUPPRESS_ALARMS;
  60.     
  61.     if (this.m_calProps) {
  62.         var ar = this.getCalProps("X-NSCP-CALPROPS-RELATIVE-CALID");
  63.         if (ar.length > 0)
  64.             this.m_calId = ar[0];
  65.         else
  66.             this.notifyError("no X-NSCP-CALPROPS-RELATIVE-CALID!");
  67.     }
  68. }
  69. calWcapCalendar.prototype = {
  70.     m_ifaces: [ calIWcapCalendar,
  71.                 calICalendar,
  72.                 Components.interfaces.calICalendarProvider,
  73.                 Components.interfaces.nsIInterfaceRequestor,
  74.                 Components.interfaces.nsIClassInfo,
  75.                 Components.interfaces.nsISupports ],
  76.     
  77.     // nsISupports:
  78.     QueryInterface: function calWcapCalendar_QueryInterface(iid) {
  79.         qiface(this.m_ifaces, iid);
  80.         return this;
  81.     },
  82.     
  83.     // nsIClassInfo:
  84.     getInterfaces: function calWcapCalendar_getInterfaces( count ) {
  85.         count.value = this.m_ifaces.length;
  86.         return this.m_ifaces;
  87.     },
  88.     get classDescription() {
  89.         return calWcapCalendarModule.WcapCalendarInfo.classDescription;
  90.     },
  91.     get contractID() {
  92.         return calWcapCalendarModule.WcapCalendarInfo.contractID;
  93.     },
  94.     get classID() {
  95.         return calWcapCalendarModule.WcapCalendarInfo.classID;
  96.     },
  97.     getHelperForLanguage:
  98.     function calWcapCalendar_getHelperForLanguage(language) { return null; },
  99.     implementationLanguage:
  100.     Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
  101.     flags: 0,
  102.     
  103.     // nsIInterfaceRequestor:
  104.     getInterface: function calWcapCalendar_getInterface(iid, instance)
  105.     {
  106.         if (iid.equals(Components.interfaces.nsIAuthPrompt)) {
  107.             // use the window watcher service to get a nsIAuthPrompt impl
  108.             return getWindowWatcher().getNewAuthPrompter(null);
  109.         }
  110.         else if (iid.equals(Components.interfaces.nsIPrompt)) {
  111.             // use the window watcher service to get a nsIPrompt impl
  112.             return getWindowWatcher().getNewPrompter(null);
  113.         }
  114.         Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
  115.         return null;
  116.     },
  117.     
  118.     toString: function calWcapCalendar_toString() {
  119.         var str = this.session.toString();
  120.         if (this.m_calId)
  121.             str += (", calId=" + this.calId);
  122.         else
  123.             str += ", default calendar";
  124.         return str;
  125.     },
  126.     notifyError: function calWcapCalendar_notifyError(err, suppressOnError)
  127.     {
  128.         debugger;
  129.         var msg = logError(err, this);
  130.         if (!suppressOnError) {
  131.             this.notifyObservers(
  132.                 "onError",
  133.                 err instanceof Components.interfaces.nsIException
  134.                 ? [err.result, err.message] : [isNaN(err) ? -1 : err, msg]);
  135.         }
  136.     },
  137.     
  138.     // calICalendarProvider:
  139.     get prefChromeOverlay() {
  140.         return null;
  141.     },
  142.     // displayName attribute already part of calIWcapCalendar
  143.     createCalendar: function calWcapCalendar_createCalendar(name, url, listener) {
  144.         throw NS_ERROR_NOT_IMPLEMENTED;
  145.     },
  146.     deleteCalendar: function calWcapCalendar_deleteCalendar(calendar, listener) {
  147.         throw NS_ERROR_NOT_IMPLEMENTED;
  148.     },
  149.     getCalendar: function calWcapCalendar_getCalendar( url ) {
  150.         throw NS_ERROR_NOT_IMPLEMENTED;
  151.     },
  152.     
  153.     // calICalendar:
  154.     mID: null,
  155.     get id() {
  156.         return this.mID;
  157.     },
  158.     set id(id) {
  159.         if (this.mID)
  160.             throw Components.results.NS_ERROR_ALREADY_INITIALIZED;
  161.         return (this.mID = id);
  162.     },
  163.  
  164.     get name() {
  165.         return getCalendarManager().getCalendarPref(
  166.             this.session.defaultCalendar, "NAME");
  167.     },
  168.     set name( name ) {
  169.         getCalendarManager().setCalendarPref(
  170.             this.session.defaultCalendar, "NAME", name);
  171.         return name;
  172.     },
  173.     
  174.     get type() { return "wcap"; },
  175.     
  176.     m_superCalendar: null,
  177.     get superCalendar() {
  178.         return (this.m_superCalendar || this);
  179.     },
  180.     set superCalendar(cal) {
  181.         return (this.m_superCalendar = cal);
  182.     },
  183.     
  184.     m_bReadOnly: false,
  185.     get readOnly() {
  186.         return (this.m_bReadOnly ||
  187.                 // xxx todo:
  188.                 // read-only if not logged in, this flag is tested quite
  189.                 // early, so don't log in here if not logged in already...
  190.                 !this.session.isLoggedIn ||
  191.                 // limit to write permission on components:
  192.                 !this.checkAccess(calIWcapCalendar.AC_COMP_WRITE));
  193.     },
  194.     set readOnly(bReadOnly) {
  195.         return (this.m_bReadOnly = bReadOnly);
  196.     },
  197.     
  198.     get uri() {
  199.         if (this.m_calId) {
  200.             var ret = this.session.uri.clone();
  201.             ret.path += ("?calid=" + encodeURIComponent(this.m_calId));
  202.             return ret;
  203.         }
  204.         else
  205.             return this.session.uri;
  206.     },
  207.     set uri(thatUri) {
  208.         return (this.session.uri = thatUri);
  209.     },
  210.     
  211.     notifyObservers: function calWcapCalendar_notifyObservers(func, args) {
  212.         this.session.notifyObservers(func, args);
  213.     },
  214.     addObserver: function calWcapCalendar_addObserver(observer) {
  215.         this.session.addObserver(observer);
  216.     },
  217.     removeObserver: function calWcapCalendar_removeObserver(observer) {
  218.         this.session.removeObserver(observer);
  219.     },
  220.     
  221.     // xxx todo: batch currently not used
  222.     startBatch: function calWcapCalendar_startBatch() {
  223.         this.notifyObservers("onStartBatch", []);
  224.     },
  225.     endBatch: function calWcapCalendar_endBatch() {
  226.         this.notifyObservers("onEndBatch", []);
  227.     },
  228.     
  229.     // xxx todo: rework like in
  230.     //           https://bugzilla.mozilla.org/show_bug.cgi?id=257428
  231.     m_bSuppressAlarms: false,
  232.     get suppressAlarms() {
  233.         return (this.m_bSuppressAlarms ||
  234.                 // writing lastAck does currently not work on readOnly cals,
  235.                 // so avoid alarms if not writable at all... discuss!
  236.                 // use m_bReadOnly here instead of attribute, because this
  237.                 // calendar acts read-only if not logged in
  238.                 this.m_bReadOnly ||
  239.                 // xxx todo: check write permissions in advance
  240.                 // alarms only for own calendars:
  241.                 // xxx todo: assume alarms if not logged in already
  242.                 (this.session.isLoggedIn && !this.isOwnedCalendar));
  243.     },
  244.     set suppressAlarms(bSuppressAlarms) {
  245.         return (this.m_bSuppressAlarms = bSuppressAlarms);
  246.     },
  247.     
  248.     get sendItipInvitations() {
  249.         return false;
  250.     },
  251.  
  252.     get canRefresh() { return (this.m_cachedResults != null); },
  253.     refresh: function calWcapCalendar_refresh() {
  254.         log("refresh.", this);
  255.         // invalidate cached results:
  256.         delete this.m_cachedResults;
  257.     },
  258.     
  259.     issueNetworkRequest: function calWcapCalendar_issueNetworkRequest(
  260.         request, respFunc, dataConvFunc, wcapCommand, params, accessRights)
  261.     {
  262.         var this_ = this;
  263.         // - bootstrap problem: no cal_props, no access check, no default calId
  264.         // - assure being logged in, thus the default cal_props are available
  265.         // - every subscribed calendar will come along with cal_props
  266.         return this.session.getSessionId(
  267.             request,
  268.             function getSessionId_resp(err, sessionId) {
  269.                 try {
  270.                     if (err)
  271.                         throw err;
  272.                     this_.assureAccess(accessRights);
  273.                     params += ("&calid=" + encodeURIComponent(this_.calId));
  274.                     this_.session.issueNetworkRequest(
  275.                         request, respFunc, dataConvFunc, wcapCommand, params);
  276.                 }
  277.                 catch (exc) {
  278.                     respFunc(exc);
  279.                 }
  280.             });
  281.     },
  282.     
  283.     // calIWcapCalendar:
  284.     
  285.     m_session: null,
  286.     get session() {
  287.         return this.m_session;
  288.     },
  289.     
  290.     m_calId: null,
  291.     get calId() {
  292.         if (this.m_calId)
  293.             return this.m_calId;
  294.         return this.session.defaultCalId;
  295.     },
  296.     
  297.     get ownerId() {
  298.         var ar = this.getCalProps("X-NSCP-CALPROPS-PRIMARY-OWNER");
  299.         if (ar.length == 0) {
  300.             var calId = this.calId;
  301.             logError("cannot determine primary owner of calendar " + calId, this);
  302.             // fallback to calId prefix:
  303.             var nColon = calId.indexOf(":");
  304.             if (nColon >= 0)
  305.                 calId = calId.substring(0, nColon);
  306.             return calId;
  307.         }
  308.         return ar[0];
  309.     },
  310.     
  311.     get description() {
  312.         var ar = this.getCalProps("X-NSCP-CALPROPS-DESCRIPTION");
  313.         if (ar.length == 0) {
  314.             // fallback to display name:
  315.             return this.displayName;
  316.         }
  317.         return ar[0];
  318.     },
  319.     
  320.     get displayName() {
  321.         var ar = this.getCalProps("X-NSCP-CALPROPS-NAME");
  322.         if (ar.length == 0) {
  323.             // fallback to common name:
  324.             ar = this.getCalProps("X-S1CS-CALPROPS-COMMON-NAME");
  325.             if (ar.length == 0) {
  326.                 return this.calId;
  327.             }
  328.         }
  329.         return ar[0];
  330.     },
  331.     
  332.     get isOwnedCalendar() {
  333.         if (this.isDefaultCalendar)
  334.             return true; // default calendar is owned
  335.         return (this.ownerId == this.session.userId);
  336.     },
  337.     
  338.     get isDefaultCalendar() {
  339.         return !this.m_calId;
  340.     },
  341.     
  342.     getCalendarProperties:
  343.     function(propName, out_count) {
  344.         var ret = this.getCalProps(propName);
  345.         out_count.value = ret.length;
  346.         return ret;
  347.     },
  348.     
  349.     getCalProps: function calWcapCalendar_getCalProps(propName) {
  350.         if (!this.m_calProps) {
  351.             log("soft error: no calprops, most possibly not logged in.", this);
  352. //             throw new Components.Exception("No calprops available!",
  353. //                                            Components.results.NS_ERROR_NOT_AVAILABLE);
  354.         }
  355.         return filterXmlNodes(propName, this.m_calProps);
  356.     },
  357.     
  358.     get defaultTimezone() {
  359.         var tzid = this.getCalProps("X-NSCP-CALPROPS-TZID");
  360.         if (tzid.length == 0) {
  361.             logError("defaultTimezone: cannot get X-NSCP-CALPROPS-TZID!", this);
  362.             return "UTC"; // fallback
  363.         }
  364.         return tzid[0];
  365.     },
  366.     
  367.     getAlignedTimezone: function calWcapCalendar_getAlignedTimezone(tzid) {
  368.         // check whether it is one of cs:
  369.         if (tzid.indexOf("/mozilla.org/") == 0) {
  370.             // cut mozilla prefix: assuming that the latter string portion
  371.             //                     semantically equals the demanded timezone
  372.             tzid = tzid.substring( // next slash after "/mozilla.org/"
  373.                 tzid.indexOf("/", "/mozilla.org/".length) + 1);
  374.         }
  375.         if (!this.session.isSupportedTimezone(tzid)) {
  376.             // xxx todo: we could further on search for a matching region,
  377.             //           e.g. CET (in TZNAME), but for now stick to
  378.             //           user's default if not supported directly
  379.             var ret = this.defaultTimezone;
  380.             // use calendar's default:
  381.             log(tzid + " not supported, falling back to default: " + ret, this);
  382.             return ret;
  383.         }
  384.         else // is ok (supported):
  385.             return tzid;
  386.     },
  387.     
  388.     checkAccess: function calWcapCalendar_checkAccess(accessControlBits)
  389.     {
  390.         // xxx todo: take real acl into account
  391.         // for now, optimistically assuming that everybody has full access, server will check:
  392.         var granted = calIWcapCalendar.AC_FULL;
  393.         // check whether every bit fits:
  394.         return ((accessControlBits & granted) == accessControlBits);
  395.     },
  396.     
  397.     assureAccess: function calWcapCalendar_assureAccess(accessControlBits)
  398.     {
  399.         if (!this.checkAccess(accessControlBits)) {
  400.             throw new Components.Exception("Access denied!",
  401.                                            calIWcapErrors.WCAP_ACCESS_DENIED_TO_CALENDAR);
  402.             // xxx todo: throwing different error here, no
  403.             //           calIErrors.CAL_IS_READONLY anymore
  404.         }
  405.     },
  406.     
  407.     defineAccessControl: function calWcapCalendar_defineAccessControl(
  408.         userId, accessControlBits)
  409.     {
  410.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  411.     },
  412.     
  413.     resetAccessControl: function calWcapCalendar_resetAccessControl(userId)
  414.     {
  415.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  416.     },
  417.     
  418.     getAccessControlDefinitions: function calWcapCalendar_getAccessControlDefinitions(
  419.         out_count, out_users, out_accessControlBits)
  420.     {
  421.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  422.     }
  423. };
  424.  
  425.