1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2004, 2005, 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) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * @overview
 26  * 
 27  * This file defines a domain.
 28  *
 29  */
 30 
 31 /**
 32  * Creates a domain
 33  * @class
 34  * This class represents a domain.
 35  * 
 36  * @param	{String}	name	the name
 37  * @param	{Object}	parent	the parent
 38  * @param	{String}	headerFlags		header flags
 39  * 
 40  * @extends	ZmModel
 41  */
 42 ZmDomain = function(name, parent, headerFlags) {
 43 	
 44 	ZmModel.call(this);
 45 
 46 	this.name = name.toLowerCase();
 47 	this.parent = parent;
 48 	this._parseHeaderFlags(headerFlags);
 49 	this._subdomains = {};
 50 }
 51 
 52 ZmDomain.prototype = new ZmModel;
 53 ZmDomain.prototype.constructor = ZmDomain;
 54 
 55 /**
 56  * Compares two domains by name.
 57  * 
 58  * @param	{ZmDomain}	a		the first domain
 59  * @param	{ZmDomain}	b		the second domain
 60  * @return	{int}	0 if the domains match; 1 if "a" is before "b"; -1 if "b" is before "a"
 61  */
 62 ZmDomain.sortCompare = 
 63 function(a, b) {
 64 	var check = ZmOrganizer.checkSortArgs(a, b);
 65 	if (check != null) return check;
 66 
 67 	if (a.name < b.name) return -1;
 68 	if (a.name > b.name) return 1;
 69 	return 0;
 70 };
 71 
 72 /**
 73  * Returns a string representation of the object.
 74  * 
 75  * @return		{String}		a string representation of the object
 76  */
 77 ZmDomain.prototype.toString = 
 78 function() {
 79 	return "ZmDomain";
 80 };
 81 
 82 /**
 83  * Gets the sub-domain.
 84  * 
 85  * @param	{String}	name		the name
 86  * @return	{ZmDomain}	the sub-domain
 87  */
 88 ZmDomain.prototype.getSubDomain =
 89 function(name) {
 90 	return this._subdomains[name];
 91 };
 92 
 93 /**
 94  * Gets the sub-domains.
 95  * 
 96  * 
 97  */
 98 ZmDomain.prototype.getSubDomains =
 99 function() {
100 	return this._subdomains;
101 };
102 
103 /**
104  * Gets the sub-domains.
105  * 
106  * @return	{Array}	an array of {@link ZmDomain} objects (sorted)
107  */
108 ZmDomain.prototype.getSortedSubDomains = 
109 function() {
110 	if (this._sorted) {
111 		return this._sorted;
112 	}
113 	this._sorted = [];
114 	for (var d in this._subdomains) {
115 		this._sorted.push(this._subdomains[d]);
116 	}
117 	this._sorted.sort(ZmDomain.sortCompare);
118 	return this._sorted;
119 };
120 
121 /**
122  * Adds the sub-domain.
123  * 
124  * @param	{String}	name		the name
125  * @param	{String}	headerFlags		the header flags
126  * @return	{ZmDomain}	the newly created sub-domain
127  */
128 ZmDomain.prototype.addSubDomain =
129 function(name, headerFlags) {
130 	name = name.toLowerCase();
131 	var sd = this._subdomains[name];
132 	if (sd) {
133 		return sd;
134 	}
135 		
136 	sd = new ZmDomain(name, this, headerFlags);
137 	this._subdomains[name] = sd;
138 
139 	if (this._sorted) {
140 		delete this._sorted;
141 	}
142 
143 	return sd;
144 };
145 
146 /**
147  * @private
148  */
149 ZmDomain.prototype._parseHeaderFlags =
150 function(flags) {
151 	this.hasFrom = (flags.indexOf("f") != -1);
152 	this.hasTo = (flags.indexOf("t") != -1);
153 	this.hasCc = (flags.indexOf("c") != -1);
154 };
155