1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 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) 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * Creates a dialog for offline settings which can be enabled or disabled.
 26  * @constructor
 27  * @class
 28  * @author  Hem Aravind
 29  *
 30  * @extends	DwtDialog
 31  */
 32 ZmOfflineSettingsDialog = function() {
 33 
 34     var params = {
 35         parent : appCtxt.getShell(),
 36         className : "ZmOfflineSettingDialog",
 37         id : "ZmOfflineSettingDialog",
 38         title : ZmMsg.offlineSettings
 39     };
 40     DwtDialog.call(this, params);
 41 
 42     // set content
 43     this.setContent(this._contentHtml());
 44 
 45     this.setButtonListener(DwtDialog.OK_BUTTON, new AjxListener(this, this._okButtonListener));
 46 };
 47 
 48 ZmOfflineSettingsDialog.prototype = new DwtDialog;
 49 ZmOfflineSettingsDialog.prototype.constructor = ZmOfflineSettingsDialog;
 50 
 51 ZmOfflineSettingsDialog.prototype.toString =
 52 function() {
 53     return "ZmOfflineSettingDialog";
 54 };
 55 
 56 /**
 57  * Gets the HTML that forms the basic framework of the dialog.
 58  *
 59  * @private
 60  */
 61 ZmOfflineSettingsDialog.prototype._contentHtml =
 62 function() {
 63     // identifiers
 64     var id = this._htmlElId;
 65     this._enableRadioBtnId = id + "_ENABLE_OFFLINE_RADIO";
 66     this._disableRadioBtnId = id + "_DISABLE_OFFLINE_RADIO";
 67     // content html
 68     return AjxTemplate.expand("prefs.Pages#OfflineSettings", {id : id, isWebClientOfflineSupported : appCtxt.isWebClientOfflineSupported});
 69 };
 70 
 71 ZmOfflineSettingsDialog.prototype._okButtonListener =
 72 function(ev) {
 73     var enableRadioBtn = document.getElementById(this._enableRadioBtnId),
 74         disableRadioBtn = document.getElementById(this._disableRadioBtnId);
 75 
 76     if (enableRadioBtn && enableRadioBtn.checked) {
 77 		ZmOfflineSettingsDialog.modifySetting(true);
 78     }
 79     else if (disableRadioBtn && disableRadioBtn.checked) {
 80 		ZmOfflineSettingsDialog.modifySetting(false);
 81     }
 82     DwtDialog.prototype._buttonListener.call(this, ev);
 83 };
 84 
 85 /**
 86  * Pops-down the ZmOfflineSettingsDialog dialog.
 87  *
 88  * Radio button state restored based on the current setting.
 89  */
 90 ZmOfflineSettingsDialog.prototype.popdown =
 91 function() {
 92 	var offlineBrowserKey = appCtxt.get(ZmSetting.WEBCLIENT_OFFLINE_BROWSER_KEY);
 93 	var localOfflineBrowserKey = localStorage.getItem(ZmSetting.WEBCLIENT_OFFLINE_BROWSER_KEY);
 94 	if (offlineBrowserKey && offlineBrowserKey.indexOf(localOfflineBrowserKey) !== -1) {
 95         var enableRadioBtn = document.getElementById(this._enableRadioBtnId);
 96         enableRadioBtn && (enableRadioBtn.checked = true);
 97     }
 98     else {
 99         var disableRadioBtn = document.getElementById(this._disableRadioBtnId);
100         disableRadioBtn && (disableRadioBtn.checked = true);
101     }
102     DwtDialog.prototype.popdown.call(this);
103 };
104 
105 /**
106  * Gets the sign out confirmation dialog if webclient offline is enabled
107  *
108  * @return	{dialog}
109  */
110 ZmOfflineSettingsDialog.showConfirmSignOutDialog =
111 function() {
112     var dialog = appCtxt.getYesNoMsgDialog();
113     dialog.reset();
114 	dialog.registerCallback(DwtDialog.YES_BUTTON, ZmOfflineSettingsDialog.modifySetting.bind(null, false, true, dialog));
115     dialog.setMessage(ZmMsg.offlineSignOutWarning, DwtMessageDialog.WARNING_STYLE);
116     dialog.popup();
117 };
118 
119 ZmOfflineSettingsDialog.modifySetting =
120 function(offlineEnable, logOff, dialog) {
121     if (logOff) {
122         dialog.popdown();
123         var setting = appCtxt.getSettings().getSetting(ZmSetting.WEBCLIENT_OFFLINE_BROWSER_KEY);
124         if (setting) {
125 			setting.addChangeListener(ZmOfflineSettingsDialog._handleLogOff.bind(window));
126         }
127         else {
128 			ZmOfflineSettingsDialog._handleLogOff();
129         }
130     }
131     var existingBrowserKey = appCtxt.get(ZmSetting.WEBCLIENT_OFFLINE_BROWSER_KEY);
132     if (existingBrowserKey) {
133         existingBrowserKey = existingBrowserKey.split(",");
134     }
135     if (offlineEnable) {
136         var browserKey = new Date().getTime().toString();
137         localStorage.setItem(ZmSetting.WEBCLIENT_OFFLINE_BROWSER_KEY, browserKey);
138         if (existingBrowserKey) {
139             AjxUtil.arrayAdd(existingBrowserKey, browserKey);
140         }
141         else {
142             existingBrowserKey = [].concat(browserKey);
143         }
144     }
145     else {
146         if (existingBrowserKey) {
147             AjxUtil.arrayRemove(existingBrowserKey, localStorage.getItem(ZmSetting.WEBCLIENT_OFFLINE_BROWSER_KEY));
148         }
149         localStorage.removeItem(ZmSetting.WEBCLIENT_OFFLINE_BROWSER_KEY);
150 		AjxCookie.deleteCookie(document, "ZM_OFFLINE_KEY", "/");
151     }
152     if (existingBrowserKey) {
153         appCtxt.set(ZmSetting.WEBCLIENT_OFFLINE_BROWSER_KEY, existingBrowserKey.join());
154     }
155 };
156 
157 ZmOfflineSettingsDialog._handleLogOff =
158 function() {
159 	ZmOffline.deleteOfflineData();
160 	setTimeout(ZmZimbraMail.logOff, 2500);//Give some time for deleting indexeddb data and application cache data
161 };