1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2013, 2014, 2016 Synacor, Inc.
  5  *
  6  * The contents of this file are subject to the Common Public Attribution License Version 1.0 (the "License");
  7  * you may not use this file except in compliance with the License.
  8  * You may obtain a copy of the License at: https://www.zimbra.com/license
  9  * The License is based on the Mozilla Public License Version 1.1 but Sections 14 and 15
 10  * have been added to cover use of software over a computer network and provide for limited attribution
 11  * for the Original Developer. In addition, Exhibit A has been modified to be consistent with Exhibit B.
 12  *
 13  * Software distributed under the License is distributed on an "AS IS" basis,
 14  * WITHOUT WARRANTY OF ANY KIND, either express or implied.
 15  * See the License for the specific language governing rights and limitations under the License.
 16  * The Original Code is Zimbra Open Source Web Client.
 17  * The Initial Developer of the Original Code is Zimbra, Inc.  All rights to the Original Code were
 18  * transferred by Zimbra, Inc. to Synacor, Inc. on September 14, 2015.
 19  *
 20  * All portions of the code are Copyright (C) 2006, 2007, 2008, 2009, 2010, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * @overview
 26  * This file contains the timezone class.
 27  */
 28 
 29 /**
 30  * Creates a timezone
 31  * @class
 32  * This class represents a timezone.
 33  * 
 34  */
 35 ZmTimezone = function() {}
 36 
 37 // Static methods
 38 
 39 /**
 40  * Gets the default timezone.
 41  * 
 42  * @return	{AjxTimezone}	the timezone
 43  */
 44 ZmTimezone.getDefault =
 45 function() {
 46 	var shell = DwtShell.getShell(window);
 47 	var serverId = appCtxt.get(ZmSetting.DEFAULT_TIMEZONE);
 48 	return (serverId) ? AjxTimezone.getClientId(serverId) : AjxTimezone.DEFAULT;
 49 };
 50 
 51 /**
 52  * Gets the default rule.
 53  * 
 54  * @return	{String}	the rule
 55  */
 56 ZmTimezone.getDefaultRule =
 57 function() {
 58 	return AjxTimezone.getRule(ZmTimezone.getDefault());
 59 };
 60 
 61 /**
 62  * This function mirrors the <code>AjxSoapDoc#set</code> method
 63  * to add a timezone element at the specific place within the
 64  * given SOAP document. The added element takes the form of the
 65  * <code><tz></code> element as defined for <code><SearchRequest></code>.
 66  *
 67  * @param {object|AjxSoapDoc}	request			the JSON request object or SOAP document
 68  * @param {String}	timezoneClientId	the client identifier
 69  * @param {Node}	parentNode		(optional) the parent node at which to add
 70  * @param {Boolean}	skipKnownTimezone	(optional) if <code>true</code>, does not add the "tz" element if it's one of the known set
 71  */
 72 ZmTimezone.set =
 73 function(request, timezoneClientId, parentNode, skipKnownTimezone) {
 74 	var timezone = AjxTimezone.getRule(timezoneClientId);
 75 	if (!timezone) { return; }
 76 
 77 	if (timezone.autoDetected || !skipKnownTimezone) {
 78 		if (request instanceof AjxSoapDoc) {
 79 			ZmTimezone._setSoap(request, timezoneClientId, parentNode, timezone);
 80 		} else {
 81 			ZmTimezone._setJson(request, timezoneClientId, timezone);
 82 		}
 83 	}
 84 };
 85 
 86 /**
 87  * @private
 88  */
 89 ZmTimezone._setSoap =
 90 function(soapDoc, timezoneClientId, parentNode, timezone) {
 91 	var tz = soapDoc.set("tz", null, parentNode);
 92 	var id = AjxTimezone.getServerId(timezoneClientId);
 93 	tz.setAttribute("id", id);
 94 	if (timezone.autoDetected) {
 95 		tz.setAttribute("stdoff", timezone.standard.offset);
 96 		if (timezone.daylight) {
 97 			tz.setAttribute("dayoff", timezone.daylight.offset);
 98             var enames = [ "standard", "daylight" ];
 99             var pnames = [ "mon", "mday", "week", "wkday", "hour", "min", "sec" ];
100             for (var i = 0; i < enames.length; i++) {
101                 var ename = enames[i];
102                 var onset = timezone[ename];
103                 
104                 var el = soapDoc.set(ename, null, tz);
105                 for (var j = 0; j < pnames.length; j++) {
106                     var pname = pnames[j];
107                     if (pname in onset) {
108                         el.setAttribute(pname, onset[pname]);
109                     }
110                 }
111             }
112         }
113 	}
114 };
115 
116 /**
117  * @private
118  */
119 ZmTimezone._setJson =
120 function(request, timezoneClientId, timezone) {
121 	var id = AjxTimezone.getServerId(timezoneClientId);
122 	var tz = request.tz = {id:id};
123 	if (timezone.autoDetected) {
124 		tz.stdoff = timezone.standard.offset;
125 		if (timezone.daylight) {
126 			tz.dayoff = timezone.daylight.offset;
127             var enames = [ "standard", "daylight" ];
128             var pnames = [ "mon", "mday", "week", "wkday", "hour", "min", "sec" ];
129             for (var i = 0; i < enames.length; i++) {
130                 var ename = enames[i];
131                 var onset = timezone[ename];
132                 tz[ename] = {};
133                 for (var j = 0; j < pnames.length; j++) {
134                     var pname = pnames[j];
135                     if (pname in onset) {
136                     	tz[ename][pname] = onset[pname];
137                     }
138                 }
139             }
140         }
141 	}
142 };
143