1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 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) 2010, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 ZmCheckinDialog = function(parent, controller, className) {
 24 	if (arguments.length == 0) return;
 25 	DwtDialog.call(this, {parent:parent, className:className, title:ZmMsg.checkInFileToBriefcase});
 26 
 27     this._controller = controller;
 28 
 29     this._createUploadHtml();
 30 
 31     this.getButton(DwtDialog.OK_BUTTON).setText(ZmMsg.checkIn);
 32     this.setButtonListener(DwtDialog.OK_BUTTON, new AjxListener(this, this._upload));
 33 };
 34 
 35 ZmCheckinDialog.prototype = new DwtDialog;
 36 ZmCheckinDialog.prototype.constructor = ZmCheckinDialog;
 37 
 38 ZmCheckinDialog.prototype.popup =
 39 function(item, callback){
 40 
 41     this._item = item;
 42     this._uploadCallback = callback;
 43     //this._uploadFolder = appCtxt.get(item.folderId);    
 44 
 45     this._verDiv.innerHTML = Number(item.version) + 1;
 46     this._fileTD.innerHTML = "";
 47     this._fileTD.innerHTML = [
 48         '<input type="file" name="file" id="',this._templateId,'_file" size="35"/>'
 49     ].join('');
 50     this._notes.value = "";
 51 
 52     DwtDialog.prototype.popup.call(this);
 53 };
 54 
 55 ZmCheckinDialog.prototype._createUploadHtml =
 56 function(){
 57     this._templateId = Dwt.getNextId();
 58     var uri = appCtxt.get(ZmSetting.CSFE_UPLOAD_URI);
 59     this.setContent(AjxTemplate.expand("briefcase.Briefcase#CheckinDialog", {id: this._templateId, uri:uri}));
 60     this._verDiv = document.getElementById(this._templateId+"_version");
 61     this._fileTD = document.getElementById(this._templateId+"_fileTD");
 62     this._notes = document.getElementById(this._templateId+"_notes");
 63 };
 64 
 65 ZmCheckinDialog.prototype._upload = function(){
 66     var fileInput = document.getElementById(this._templateId+"_file");
 67     if(!fileInput.value) return;
 68     var item = this._item;
 69     var file = {
 70         fullname: fileInput.value,
 71         name: fileInput.value.replace(/^.*[\\\/:]/, ""),
 72         id: item.id,
 73         version: item.version,
 74         folder: item.folderId,
 75         notes: this._notes.value
 76     };
 77 
 78     var callback = new AjxCallback(this, this._uploadSaveDocs, file);
 79 
 80     this._initiateUpload(this._templateId+"_form", callback)
 81 
 82 };
 83 
 84 ZmCheckinDialog.prototype._initiateUpload =
 85 function(formId, callback){
 86 
 87     this.setButtonEnabled(DwtDialog.OK_BUTTON, false);
 88 	this.setButtonEnabled(DwtDialog.CANCEL_BUTTON, false);
 89 
 90 	var uploadForm = document.getElementById(formId);
 91 
 92 	var uploadMgr = appCtxt.getUploadManager();
 93 	window._uploadManager = uploadMgr;
 94 	try {
 95 		uploadMgr.execute(callback, uploadForm);
 96 	} catch (ex) {
 97 		if (ex.msg) {
 98 			this._popupErrorDialog(ex.msg);
 99 		} else {
100 			this._popupErrorDialog(ZmMsg.unknownError);
101 		}
102 	}
103 };
104 
105 ZmCheckinDialog.prototype._uploadSaveDocs = function(file, status, guid) {
106 	if (status != AjxPost.SC_OK) {
107 		appCtxt.getAppController().popupUploadErrorDialog(ZmItem.BRIEFCASE,
108 		                                                  status);
109 		this.setButtonEnabled(DwtDialog.OK_BUTTON, true );
110 		this.setButtonEnabled(DwtDialog.CANCEL_BUTTON, true);
111 	} else {
112 
113         file.guid = guid;
114 		this._uploadSaveDocs2(file, status, guid);
115 
116 	}
117 };
118 
119 ZmCheckinDialog.prototype._uploadSaveDocs2 =
120 function(file, status, guid) {
121 
122     var json = {
123 		SaveDocumentRequest: {
124 			_jsns: "urn:zimbraMail",
125 			doc: {
126 				id:	    file.id,
127                 ver:    file.version,
128                 l:      file.folderId,
129                 name:   file.name,
130                 desc:   file.notes,
131                 upload: {
132                     id: file.guid
133                 }
134 			}
135 		}
136 	};
137 
138     var callback = new AjxCallback(this, this._uploadSaveDocsResponse, [ file, status, guid ]);
139     var params = {
140         jsonObj:    json,
141         asyncMode:  true,
142         callback:   callback,
143         errorCallback: new AjxCallback(this, this._handleSaveDocError, [file, status, guid])
144     };
145     appCtxt.getAppController().sendRequest(params);    
146 };
147 
148 ZmCheckinDialog.prototype._handleSaveDocError =
149 function(file, status, guid, ex){
150 
151     this.setButtonEnabled(DwtDialog.OK_BUTTON, true );
152     this.setButtonEnabled(DwtDialog.CANCEL_BUTTON, true);
153 
154     if(ex.code == ZmCsfeException.MAIL_ALREADY_EXISTS){
155         //Warning Message
156         var warning = appCtxt.getMsgDialog();
157         warning.reset();
158         warning.setMessage(AjxMessageFormat.format(ZmMsg.itemWithFileNameExits, file.name), DwtMessageDialog.CRITICAL_STYLE, ZmMsg.briefcase);
159         warning.popup();
160         //Error Handled
161         return true;
162     }
163 
164     return false;
165 };        
166 
167 ZmCheckinDialog.prototype._uploadSaveDocsResponse =
168 function(file, status, guid, response) {
169 
170     var resp = response && response._data;
171     var saveDocResp = resp && resp.SaveDocumentResponse;
172 
173     if(saveDocResp){
174         saveDocResp = saveDocResp.doc[0];
175         file.done     = true;
176         file.name     = saveDocResp.name;
177         file.version  = saveDocResp.ver;
178 
179         this._finishUpload(file);
180     }
181 
182     this.popdown();
183 
184     if (resp && resp.Fault) {
185         var fault = resp.Fault;
186         var error = fault.Detail.Error;
187         var code = error.Code;
188         //Handle Mailbox Exceeded Exception
189         if(code == ZmCsfeException.MAIL_QUOTA_EXCEEDED){
190             this._popupErrorDialog(ZmMsg.errorQuotaExceeded);
191         }
192     }
193 
194 };
195 
196 ZmCheckinDialog.prototype._finishUpload = function(file) {
197 	if(this._uploadCallback)
198 	    this._uploadCallback.run([file]);
199 };
200 
201 ZmCheckinDialog.prototype._popupErrorDialog = function(message) {
202 	this.setButtonEnabled(DwtDialog.OK_BUTTON, true);
203 	this.setButtonEnabled(DwtDialog.CANCEL_BUTTON, true);
204 
205 	var dialog = appCtxt.getMsgDialog();
206 	dialog.setMessage(message, DwtMessageDialog.CRITICAL_STYLE, this._title);
207 	dialog.popup();
208 };
209