1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010, 2011, 2012, 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, 2009, 2010, 2011, 2012, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * @overview
 26  * 
 27  * This file defines a list of attachment types.
 28  *
 29  */
 30 
 31 /**
 32  * Creates an attachment type list
 33  * @class
 34  * This class represents attachment types.
 35  * 
 36  * @extends	ZmModel
 37  */
 38 ZmAttachmentTypeList = function() {
 39 	ZmModel.call(this, ZmEvent.S_ATT);
 40 };
 41 
 42 ZmAttachmentTypeList.prototype = new ZmModel;
 43 ZmAttachmentTypeList.prototype.constructor = ZmAttachmentTypeList;
 44 
 45 ZmAttachmentTypeList.prototype.isZmAttachmentTypeList = true;
 46 ZmAttachmentTypeList.prototype.toString = function() { return "ZmAttachmentTypeList"; };
 47 
 48 /**
 49  * Gets the attachments.
 50  * 
 51  * @return	{Array}	an array of attachments
 52  */
 53 ZmAttachmentTypeList.prototype.getAttachments =
 54 function() {
 55 	return this._attachments;
 56 };
 57 
 58 /**
 59  * Compares attachment type lists by description.
 60  * 
 61  * @param	{ZmAttachmentTypeList}	a			the first entry
 62  * @param	{ZmAttachmentTypeList}	b			the first entry
 63  * @return	{int}	0 if the entries match; 1 if "a" is before "b"; -1 if "b" is before "a"
 64  */
 65 ZmAttachmentTypeList.compareEntry = 
 66 function(a,b) {
 67 	if (a.desc.toLowerCase() < b.desc.toLowerCase())	{ return -1; }
 68 	if (a.desc.toLowerCase() > b.desc.toLowerCase())	{ return 1; }
 69 	return 0;
 70 };
 71 
 72 /**
 73  * Loads the attachments.
 74  * 
 75  * @param	{AjxCallback}	callback		the callback to call after load
 76  */
 77 ZmAttachmentTypeList.prototype.load =
 78 function(callback) {
 79 
 80 	this._attachments = [];
 81 
 82 	var jsonObj = {BrowseRequest:{_jsns:"urn:zimbraMail"}};
 83 	var request = jsonObj.BrowseRequest;
 84 	request.browseBy = "attachments";
 85 
 86 	var respCallback = this._handleResponseLoad.bind(this, callback);
 87 	appCtxt.getAppController().sendRequest({jsonObj: jsonObj, asyncMode: true, callback: respCallback});
 88 };
 89 
 90 /**
 91  * @private
 92  */
 93 ZmAttachmentTypeList.prototype._handleResponseLoad =
 94 function(callback, result) {
 95 	var att = this._organizeTypes(result.getResponse().BrowseResponse.bd);
 96     var isZipFileIncluded = false;
 97 	if (att) {
 98 		for (var i = 0; i < att.length; i++) {
 99 			var type = att[i]._content;
100 			if (!ZmMimeTable.isIgnored(type)) {
101 				this._attachments.push(ZmMimeTable.getInfo(type, true));
102 			}
103 		}
104 		this._attachments.sort(ZmAttachmentTypeList.compareEntry);
105 	}
106 
107 	if (callback) {
108 		callback.run(this._attachments);
109 	}
110 };
111 
112 /**
113  * Check whether type is from the following list
114  * Adobe PDF
115  * Microsoft Word (doc, docx)
116  * Microsoft Powerpoint
117  * Microsoft Excel
118  * Email Message
119  * HTML
120  * Calendar (ical)
121  *
122  *
123  * @param	{String}			attachment type
124  * @return	{Boolean}           true if the type in the above list, otherwise false
125  *
126  * @private
127  */
128 ZmAttachmentTypeList.prototype._isSupportedType  =
129 function(type){
130 	var supportedTypes =  [ZmMimeTable.APP_ADOBE_PDF, ZmMimeTable.APP_MS_WORD,ZmMimeTable.APP_MS_EXCEL,
131     	                   ZmMimeTable.APP_MS_PPT, ZmMimeTable.APP_ZIP,ZmMimeTable.APP_ZIP2, ZmMimeTable.MSG_RFC822,
132         	               ZmMimeTable.TEXT_HTML, ZmMimeTable.TEXT_CAL];
133 
134     return AjxUtil.arrayContains(supportedTypes, type);
135 };
136 
137 /**
138  * Returns group type if type belongs to following group:
139  *  Text (vcard, csv)
140  *  Video (mpeg, mov)
141  *  Audio (wav, mp3, etc)
142  *  Archive (zip, etc)
143  *  Application (any)
144  *  Image (bmp, png, gif, tiff, jpg, psd, ai, jpeg)
145  *
146  * @param	{String}	    attachment type
147  * @return	{String}	    attachment group if it exits in the above list, otherwise null
148  *
149  * @private
150  */
151 ZmAttachmentTypeList.prototype._isSupportedGroup =
152 function(type){
153     var supportedGroups = [ZmMimeTable.APP,ZmMimeTable.AUDIO,ZmMimeTable.IMG,ZmMimeTable.TEXT,ZmMimeTable.VIDEO ];
154     var regExp =new RegExp("^" + supportedGroups.join("|^") ,"ig");
155     var groupType = type.match(regExp)
156     return groupType && groupType[0];
157 
158 };
159 
160 /**
161  * Returns set of supported type/group of attachments
162  *
163  * @param	{Array} list of attachment types
164  * @return	{Array} Set of types which is an intersection of att and supported types/groups
165  * @private
166  */
167 ZmAttachmentTypeList.prototype._organizeTypes =
168 function(att) {
169 
170 	var res = [];
171 	if (!(att && att.length)) {
172 		return res;
173 	}
174 
175 	for (var i = 0; i < att.length; i++) {
176 		var type = att[i]._content;
177 		var freq = att[i].freq;
178 		var skip = true;
179 		var groupType = null;
180 		if (this._isSupportedType(type)) {
181 			skip = false;
182 		} else if (type = this._isSupportedGroup(type)) {
183 			skip = false;
184 			// Check if group is already in result
185 			for (var j=0; j < res.length; j++) {
186 				if (res[j]. _content === type ) {
187 					res[j].freq += freq;
188 					skip = true;
189 					break;
190 				}
191 			}
192 		}
193 	
194 		if (!skip) {
195 			res.push({_content: type, freq: freq});
196 		}
197 
198 	}
199 	return res;
200 };
201