1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 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) 2008, 2009, 2010, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * @overview
 26  */
 27 
 28 /**
 29  * Creates a folder notification dialog.
 30  * @class
 31  * This class represents a folder notification dialog.
 32  * 
 33  * @param	{DwtControl}	parent		the parent
 34  * @param	{String}	className		the class name
 35  * 
 36  * @extends		DwtDialog
 37  */
 38 ZmFolderNotifyDialog = function(parent, className) {
 39 
 40     className = className || "ZmFolderNotifyDialog";
 41 
 42 	var extraButtons = [ new DwtDialog_ButtonDescriptor(ZmFolderPropsDialog.ADD_SHARE_BUTTON, ZmMsg.addShare, DwtDialog.ALIGN_LEFT)];
 43 
 44 	DwtDialog.call(this, {parent:parent, className:className, title:ZmMsg.folderNotify, extraButtons:extraButtons});
 45     this.getButton(DwtDialog.OK_BUTTON).setText(ZmMsg.notify);
 46 
 47     this.registerCallback(ZmFolderPropsDialog.ADD_SHARE_BUTTON, this._handleAddShareButton, this);
 48 
 49 	this.setButtonListener(DwtDialog.OK_BUTTON, new AjxListener(this, this._handleOkButton));
 50 	this.setButtonListener(DwtDialog.CANCEL_BUTTON, new AjxListener(this, this._handleCancelButton));
 51 
 52 	this._folderChangeListener = new AjxListener(this, this._handleFolderChange);
 53 	
 54     this.setView(this._createView());
 55 };
 56 
 57 ZmFolderNotifyDialog.prototype = new DwtDialog;
 58 ZmFolderNotifyDialog.prototype.constructor = ZmFolderNotifyDialog;
 59 
 60 // Constants
 61 
 62 ZmFolderNotifyDialog.ADD_SHARE_BUTTON = ++DwtDialog.LAST_BUTTON;
 63 
 64 ZmFolderNotifyDialog.SHARES_HEIGHT = "9em";
 65 
 66 // Public methods
 67 
 68 ZmFolderNotifyDialog.prototype.toString =
 69 function() {
 70 	return "ZmFolderNotifyDialog";
 71 };
 72 
 73 /**
 74  * Pops-up the notification dialog.
 75  * 
 76  * @param	{ZmOrganizer}	organizer		the organizer
 77  */
 78 ZmFolderNotifyDialog.prototype.popup =
 79 function(organizer) {
 80 
 81     this._organizer = organizer;
 82 	organizer.addChangeListener(this._folderChangeListener);
 83 
 84     this._populateShares(organizer);
 85 
 86     this._reply.setReplyType(ZmShareReply.STANDARD);
 87     this._reply.setReplyNote("");
 88 
 89     DwtDialog.prototype.popup.call(this);
 90 };
 91 
 92 ZmFolderNotifyDialog.prototype.popdown =
 93 function() {
 94 	this._organizer.removeChangeListener(this._folderChangeListener);
 95 	this._organizer = null;
 96 	DwtDialog.prototype.popdown.call(this);
 97 };
 98 
 99 // Protected methods
100 
101 ZmFolderNotifyDialog.prototype._handleAddShareButton =
102 function(event) {
103 	var sharePropsDialog = appCtxt.getSharePropsDialog();
104 	sharePropsDialog.popup(ZmSharePropsDialog.NEW, this._organizer, null);
105 };
106 
107 ZmFolderNotifyDialog.prototype._handleOkButton =
108 function(event) {
109 
110     var replyType = this._reply.getReplyType();
111     var notes = replyType == ZmShareReply.QUICK ? this._reply.getReplyNote() : "";
112     var shares = this._organizer.shares;
113 
114     for (var i = 0; i < shares.length; i++) {
115         var share = shares[i];
116         var email = share.grantee.email;
117         if (!email) {
118             // last resort: check if grantee name is a valid email address
119             if (AjxEmailAddress.isValid(share.grantee.name))
120                 email = share.grantee.name;
121         }
122 
123         if (!email) { continue; }
124 
125         var addrs = new AjxVector();
126         var addr = new AjxEmailAddress(email, AjxEmailAddress.TO);
127         addrs.add(addr);
128 
129         var tmpShare = new ZmShare({object:share.object});
130 
131         tmpShare.grantee.id = share.grantee.id;
132         tmpShare.grantee.email = email;
133         tmpShare.grantee.name = share.grantee.name;
134 
135 			// REVISIT: What if you have delegated access???
136         if(tmpShare.object.isRemote()) {
137             tmpShare.grantor.id = tmpShare.object.zid;
138             tmpShare.grantor.email = tmpShare.object.owner;
139             tmpShare.grantor.name = tmpShare.grantor.email;
140             tmpShare.link.id = tmpShare.object.rid;
141         }else {
142             tmpShare.grantor.id = appCtxt.get(ZmSetting.USERID);
143             tmpShare.grantor.email = appCtxt.get(ZmSetting.USERNAME);
144             tmpShare.grantor.name = appCtxt.get(ZmSetting.DISPLAY_NAME) || tmpShare.grantor.email;
145             tmpShare.link.id = tmpShare.object.id;
146         }
147 
148         tmpShare.link.perm = share.link.perm;
149         tmpShare.link.name = tmpShare.object.name;
150         tmpShare.link.view = ZmOrganizer.getViewName(tmpShare.object.type);
151         tmpShare.link.inh = this._inheritEl ? this._inheritEl.checked : true;
152 
153         tmpShare.notes = notes;
154 
155         tmpShare.sendMessage(ZmShare.NOTIFY, addrs);
156     }
157 
158     this.popdown();
159 };
160 
161 ZmFolderNotifyDialog.prototype._handleCancelButton =
162 function(event) {
163 	this.popdown();
164 };
165 
166 ZmFolderNotifyDialog.prototype._handleFolderChange =
167 function(event) {
168     this._populateShares(this._organizer);
169 };
170 
171 ZmFolderNotifyDialog.prototype._populateShares =
172 function(organizer) {
173     
174     this._sharesGroup.setContent("");
175 
176 	var link = organizer.link;
177 	var shares = organizer.shares;
178 	var visible = ((!link || organizer.isAdmin()) && shares && shares.length > 0);
179 	if (visible) {
180 		AjxDispatcher.require("Share");
181 		var table = document.createElement("TABLE");
182 		table.border = 0;
183 		table.cellSpacing = 0;
184 		table.cellPadding = 3;
185 		for (var i = 0; i < shares.length; i++) {
186 			var share = shares[i];
187 			var row = table.insertRow(-1);
188 
189 			var nameEl = row.insertCell(-1);
190 			nameEl.style.paddingRight = "15px";
191 			var nameText = share.grantee.name || ZmMsg.userUnknown;
192 			if (share.isAll()) nameText = ZmMsg.shareWithAll;
193 			else if (share.isPublic()) nameText = ZmMsg.shareWithPublic;
194 			nameEl.innerHTML = AjxStringUtil.htmlEncode(nameText);
195 
196 			var roleEl = row.insertCell(-1);
197 			roleEl.style.paddingRight = "15px";
198 			roleEl.innerHTML = ZmShare.getRoleName(share.link.perm);
199 		}
200 		this._sharesGroup.setElement(table);
201 
202 		var width = Dwt.DEFAULT;
203 		var height = shares.length > 5 ? ZmFolderNotifyDialog.SHARES_HEIGHT : Dwt.CLEAR;
204 
205 		var insetElement = this._sharesGroup.getInsetHtmlElement();
206 		Dwt.setScrollStyle(insetElement, Dwt.SCROLL);
207 		Dwt.setSize(insetElement, width, height);
208         this.getButton(DwtDialog.OK_BUTTON).setEnabled(true);
209     }else{
210         this._sharesGroup.setContent("<center>"+ZmMsg.noShareDetailsFound+"</center>");
211         this.getButton(DwtDialog.OK_BUTTON).setEnabled(false);
212     }
213 };
214 
215 ZmFolderNotifyDialog.prototype._createView =
216 function() {
217 
218     var view = new DwtComposite(this);
219 
220     // add message group
221 	this._reply = new ZmShareReply(view, null, [ZmShareReply.STANDARD, ZmShareReply.QUICK]);
222 
223     this._messageGroup = new DwtGrouper(view);
224 	this._messageGroup.setLabel(ZmMsg.message);
225 	this._messageGroup.setView(this._reply);
226     view.getHtmlElement().appendChild(this._messageGroup.getHtmlElement());
227 
228     // setup shares group
229     this._sharesGroup = new DwtGrouper(view);
230     this._sharesGroup.setLabel(ZmMsg.folderSharing);
231     this._sharesGroup.setVisible(true);
232     this._sharesGroup.setScrollStyle(Dwt.SCROLL);    
233     view.getHtmlElement().appendChild(this._sharesGroup.getHtmlElement());
234 
235     return view;
236 };