1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2007, 2008, 2009, 2010, 2011, 2013, 2014, 2015, 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) 2007, 2008, 2009, 2010, 2011, 2013, 2014, 2015, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 ZmAccountTestDialog = function(parent) {
 25 	DwtDialog.call(this, {parent:parent, title:ZmMsg.accountTest, className:"DwtBaseDialog ZmDataSourceTestDialog" });
 26 	this.registerCallback(DwtDialog.OK_BUTTON, this._handleOkButton, this);
 27 	this.registerCallback(DwtDialog.CANCEL_BUTTON, this._handleCancelButton, this);
 28 };
 29 ZmAccountTestDialog.prototype = new DwtDialog;
 30 ZmAccountTestDialog.prototype.constructor = ZmAccountTestDialog;
 31 
 32 ZmAccountTestDialog.prototype.toString = function() {
 33 	return "ZmAccountTestDialog";
 34 };
 35 
 36 //
 37 // DwtDialog methods
 38 //
 39 
 40 ZmAccountTestDialog.prototype.popup = function(accounts, okCallback, cancelCallback) {
 41 	delete this._reqId;
 42 	// perform tests
 43 	if (accounts && accounts.length > 0) {
 44 		this._okCallback = okCallback;
 45 		this._cancelCallback = cancelCallback;
 46 
 47 		// setup display
 48 		this._initializeAccounts(accounts);
 49 		this.setButtonEnabled(DwtDialog.OK_BUTTON, false);
 50 
 51 		// show dialog
 52 		DwtDialog.prototype.popup.call(this);
 53 
 54 		// begin test
 55 		var testCallback = new AjxCallback(this, this._handleTestResult);
 56 		testCallback.args = [testCallback, 0];
 57 		this._handleTestResult.apply(this, testCallback.args);
 58 	}
 59 
 60 	// nothing to do; report success
 61 	else if (okCallback) {
 62 		var successes = [];
 63 		okCallback.run(successes);
 64 	}
 65 };
 66 
 67 //
 68 // Protected methods
 69 //
 70 
 71 ZmAccountTestDialog.prototype._initializeAccounts = function(accounts) {
 72 	this._accounts = accounts;
 73 	this._successes = new Array(accounts.length);
 74 
 75 	var data = { id: this._htmlElId, accounts: accounts };
 76 	var html = AjxTemplate.expand("prefs.Pages#AccountTestContent", data);
 77 	this.setContent(html);
 78 };
 79 
 80 ZmAccountTestDialog.prototype._handleTestResult =
 81 function(testCallback, index, result) {
 82 	// show results
 83 	if (result) {
 84 		var account = this._accounts[index - 1];
 85 		var statusEl = document.getElementById(account.id+"_test_status");
 86 
 87 		var error = null;
 88 		var resp = result._data && result._data.TestDataSourceResponse;
 89 		if (resp) {
 90 			this._successes[index - 1] = true;
 91 			var dsrc = resp[ZmDataSource.prototype.ELEMENT_NAME] ||
 92 					   resp[ZmPopAccount.prototype.ELEMENT_NAME] ||
 93 					   resp[ZmImapAccount.prototype.ELEMENT_NAME];
 94 			dsrc = dsrc && dsrc[0];
 95 			if (dsrc.success) {
 96 				statusEl.className = [statusEl.className,"ZmTestSucceeded"].join(" ");
 97 				statusEl.innerHTML = ZmMsg.popAccountTestSuccess;
 98 			}
 99 			else {
100 				error = dsrc.error;
101 			}
102 		}
103 		else {
104 			error = "Generic Test Failure"; // TODO: i18n
105 		}
106 
107 		if (error) {
108 			this._successes[index - 1] = false;
109 
110 			statusEl.className = [statusEl.className,"ZmTestFailed"].join(" ");
111 			statusEl.innerHTML = ZmMsg.popAccountTestFailure;
112 
113 			var detailsEl = document.getElementById(account.id+"_test_details");
114 			var errorEl = document.getElementById(account.id+"_test_error");
115 			error = AjxStringUtil.htmlEncode(error);
116 			errorEl.innerHTML = error.replace(/(\bhttps?:[^\s<]*)/igm, '<a href="$1" target="_blank">$1</a>');
117 			Dwt.setVisible(detailsEl, true);
118 		}
119 
120 		this._position();
121 	}
122 
123 	// finish
124 	if (this._accounts.length == index) {
125 		this.setButtonEnabled(DwtDialog.OK_BUTTON, true);
126 		return;
127 	}
128 
129 	// continue testing
130 	var account = this._accounts[ testCallback.args[1]++ ];
131 	var statusEl = document.getElementById(account.id+"_test_status");
132 	statusEl.innerHTML = ZmMsg.popAccountTestInProgress;
133 	this._reqId = account.testConnection(testCallback, testCallback, null, true);
134 };
135 
136 ZmAccountTestDialog.prototype._handleOkButton = function(evt) {
137 	this.popdown();
138 	if (this._reqId) {
139 		appCtxt.getAppController().cancelRequest(this._reqId);
140 	}
141 	if (this._okCallback) {
142 		this._okCallback.run(this._successes);
143 	}
144 };
145 ZmAccountTestDialog.prototype._handleCancelButton = function(evt) {
146 	this.popdown();
147 	if (this._cancelCallback) {
148 		this._cancelCallback.run();
149 	}
150 };