1 /* 2 * ***** BEGIN LICENSE BLOCK ***** 3 * Zimbra Collaboration Suite Web Client 4 * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 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, 2011, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved. 21 * ***** END LICENSE BLOCK ***** 22 */ 23 24 /** 25 * Creates a signature. 26 * @class 27 * This class represents a signature. 28 * 29 * 30 */ 31 ZmSignature = function(id) { 32 this.id = id; 33 }; 34 35 ZmSignature.prototype.toString = function() { 36 return "ZmSignature"; 37 }; 38 39 // 40 // Data 41 // 42 /** 43 * The name property. 44 * @type String 45 */ 46 ZmSignature.prototype.name = ""; 47 /** 48 * The content type property. 49 * @type String 50 * @see ZmMimeTable 51 */ 52 ZmSignature.prototype.contentType = ZmMimeTable.TEXT_PLAIN; 53 /** 54 * The value property. 55 * @type String 56 */ 57 ZmSignature.prototype.value = ""; 58 59 // 60 // Static functions 61 // 62 63 ZmSignature.createFromJson = 64 function(object) { 65 var signature = new ZmSignature(object.id); 66 signature.setFromJson(object); 67 return signature; 68 }; 69 70 // 71 // Public methods 72 // 73 /** 74 * Creates the signature. 75 * 76 * @param {AjxCallback} callback the callback 77 * @param {AjxCallback} errorCallback the error callback 78 * @param {ZmBatchCommand} batchCmd the batch command 79 */ 80 ZmSignature.prototype.create = 81 function(callback, errorCallback, batchCmd) { 82 var respCallback = callback ? new AjxCallback(this, this._handleCreateResponse, [callback]) : null; 83 var resp = this._sendRequest("CreateSignatureRequest", false, respCallback, errorCallback, batchCmd); 84 if (!callback && !batchCmd) { 85 this._handleCreateResponse(callback, resp); 86 } 87 }; 88 89 /** 90 * Saves the signature. 91 * 92 * @param {AjxCallback} callback the callback 93 * @param {AjxCallback} errorCallback the error callback 94 * @param {ZmBatchCommand} batchCmd the batch command 95 */ 96 ZmSignature.prototype.save = 97 function(callback, errorCallback, batchCmd) { 98 var respCallback = callback ? new AjxCallback(this, this._handleModifyResponse, [callback]) : null; 99 var resp = this._sendRequest("ModifySignatureRequest", false, respCallback, errorCallback, batchCmd); 100 if (!callback && !batchCmd) { 101 this._handleModifyResponse(callback, resp); 102 } 103 }; 104 105 /** 106 * Deletes the signature. 107 * 108 * @param {AjxCallback} callback the callback 109 * @param {AjxCallback} errorCallback the error callback 110 * @param {ZmBatchCommand} batchCmd the batch command 111 */ 112 ZmSignature.prototype.doDelete = 113 function(callback, errorCallback, batchCmd) { 114 var respCallback = callback ? new AjxCallback(this, this._handleDeleteResponse, [callback]) : null; 115 var resp = this._sendRequest("DeleteSignatureRequest", true, respCallback, errorCallback, batchCmd); 116 if (!callback && !batchCmd) { 117 this._handleDeleteResponse(callback, resp); 118 } 119 }; 120 121 /** 122 * Sets the signature from JSON object. 123 * 124 * @param {Object} object the object 125 */ 126 ZmSignature.prototype.setFromJson = 127 function(object) { 128 129 this.name = object.name || this.name; 130 var c = object.content; 131 if (c) { 132 var sig = c[0]._content ? c[0] : c[1]; 133 this.contentType = sig.type || this.contentType; 134 this.value = sig._content || this.value; 135 } 136 if (object.cid) { 137 this.contactId = object.cid[0]._content; 138 } 139 }; 140 141 /** 142 * Gets the content type. 143 * 144 * @return {String} the content type 145 */ 146 ZmSignature.prototype.getContentType = 147 function() { 148 return this.contentType; 149 }; 150 151 /** 152 * Sets the content type. 153 * 154 * @param {String} ct the content type 155 * @see ZmMimeTable 156 */ 157 ZmSignature.prototype.setContentType = 158 function(ct){ 159 this.contentType = ct || ZmMimeTable.TEXT_PLAIN; 160 }; 161 162 /** 163 * @param outputType [string] (Optional) Formats the resulting 164 * signature text to the specified 165 * content-type. If not specified, 166 * the signature text is returned in 167 * the original format. 168 * 169 * @private 170 */ 171 ZmSignature.prototype.getValue = 172 function(outputType) { 173 174 var isHtml = this.contentType == ZmMimeTable.TEXT_HTML; 175 var value = this.value; 176 177 var type = outputType || this.contentType; 178 if (type != this.contentType) { 179 value = isHtml ? AjxStringUtil.convertHtml2Text(value) : AjxStringUtil.convertToHtml(value); 180 } 181 182 if (appCtxt.isWebClientOffline()) { 183 value = ZmOffline.modifySignature(value); 184 } 185 186 return value; 187 }; 188 189 190 // 191 // Protected methods 192 // 193 194 ZmSignature.prototype._sendRequest = 195 function(method, idOnly, respCallback, errorCallback, batchCmd) { 196 197 /* 198 var jsonObj = {}; 199 var request = jsonObj[method] = {_jsns:"urn:zimbraAccount"}; 200 var sig = request.signature = {}; 201 if (this.id) { 202 sig.id = this.id; 203 } 204 if (!idOnly) { 205 sig.name = this.name; 206 sig.cid = this.contactId || null; 207 sig.content = []; 208 sig.content.push({_content:this.value, type:this.contentType}); 209 210 // Empty the other content type 211 var emptyType = (this.contentType == ZmMimeTable.TEXT_HTML) ? ZmMimeTable.TEXT_PLAIN : ZmMimeTable.TEXT_HTML; 212 sig.content.push({_content:"", type:emptyType}); 213 } 214 */ 215 216 var soapDoc = AjxSoapDoc.create(method, "urn:zimbraAccount"); 217 var signatureEl = soapDoc.set("signature"); 218 if (this.id) { 219 signatureEl.setAttribute("id", this.id); 220 } 221 if (!idOnly) { 222 signatureEl.setAttribute("name", this.name); 223 if (this.contactId || (method == "ModifySignatureRequest")) { 224 soapDoc.set("cid", this.contactId || null, signatureEl); 225 } 226 var contentEl = soapDoc.set("content", this.value, signatureEl); 227 contentEl.setAttribute("type", this.contentType); 228 229 //Empty the other content type 230 var emptyType = (this.contentType == ZmMimeTable.TEXT_HTML) ? ZmMimeTable.TEXT_PLAIN : ZmMimeTable.TEXT_HTML; 231 contentEl = soapDoc.set("content", "", signatureEl); 232 contentEl.setAttribute("type", emptyType); 233 234 } 235 236 if (batchCmd) { 237 batchCmd.addNewRequestParams(soapDoc, respCallback, errorCallback); 238 return; 239 } 240 241 var appController = appCtxt.getAppController(); 242 var params = { 243 soapDoc: soapDoc, 244 asyncMode: Boolean(respCallback), 245 callback: respCallback, 246 errorCallback: errorCallback 247 } 248 return appController.sendRequest(params); 249 }; 250 251 ZmSignature.prototype._handleCreateResponse = 252 function(callback, resp) { 253 // save id 254 this.id = resp._data.CreateSignatureResponse.signature[0].id; 255 256 // add to global hash 257 var signatures = appCtxt.getSignatureCollection(); 258 signatures.add(this); 259 260 if (callback) { 261 callback.run(); 262 } 263 }; 264 265 ZmSignature.prototype._handleModifyResponse = function(callback, resp) { 266 // promote settings to global signature 267 var signatures = appCtxt.getSignatureCollection(); 268 var signature = signatures.getById(this.id); 269 signature.name = this.name; 270 signature.value = this.value; 271 signature.contentType = this.contentType; 272 signatures._notify(ZmEvent.E_MODIFY, { item: signature }); 273 274 if (callback) { 275 callback.run(); 276 } 277 }; 278 279 ZmSignature.prototype._handleDeleteResponse = function(callback, resp) { 280 // remove from global hash 281 var signatures = appCtxt.getSignatureCollection(); 282 signatures.remove(this); 283 284 if (callback) { 285 callback.run(); 286 } 287 }; 288