From 20cc14ae5cb9c7e57c14a26fea9ae00e6c61b22a Mon Sep 17 00:00:00 2001
From: Julia Radzhabova <Julia.Radzhabova@onlyoffice.com>
Date: Wed, 22 Nov 2017 16:34:00 +0300
Subject: [PATCH] [DE mobile] Change applying the license (disable editing when
 connections are over the limitations).

---
 .../mobile/app/controller/Main.js             | 28 +++++++++++++++----
 .../mobile/app/controller/Toolbar.js          | 13 +++++++++
 2 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/apps/documenteditor/mobile/app/controller/Main.js b/apps/documenteditor/mobile/app/controller/Main.js
index 3aa2febc6..021255431 100644
--- a/apps/documenteditor/mobile/app/controller/Main.js
+++ b/apps/documenteditor/mobile/app/controller/Main.js
@@ -237,6 +237,7 @@ define([
                 }
 
                 this.api.asc_registerCallback('asc_onGetEditorPermissions', _.bind(this.onEditorPermissions, this));
+                this.api.asc_registerCallback('asc_onLicenseChanged',       _.bind(this.onLicenseChanged, this));
                 this.api.asc_setDocInfo(docInfo);
                 this.api.asc_getEditorPermissions(this.editorConfig.licenseUrl, this.editorConfig.customerId);
 
@@ -551,10 +552,26 @@ define([
                     me.api.zoomFitToWidth();
                 }
 
-                DE.getController('Toolbar').activateControls();
+                me.applyLicense();
+            },
 
+            onLicenseChanged: function(params) {
+                var licType = params.asc_getLicenseType();
+                if (licType !== undefined && (licType===Asc.c_oLicenseResult.Connections || licType===Asc.c_oLicenseResult.Users) && this.appOptions.canEdit && this.editorConfig.mode !== 'view') {
+                    this._state.licenseWarning = (licType===Asc.c_oLicenseResult.Connections) ? this.warnNoLicense : this.warnNoLicenseUsers;
+                }
+
+                if (this._isDocReady && this._state.licenseWarning)
+                    this.applyLicense();
+            },
+
+            applyLicense: function() {
+                var me = this;
                 if (me._state.licenseWarning) {
-                    value = Common.localStorage.getItem("de-license-warning");
+                    DE.getController('Toolbar').deactivateControls();
+                    Common.NotificationCenter.trigger('api:disconnect');
+
+                    var value = Common.localStorage.getItem("de-license-warning");
                     value = (value!==null) ? parseInt(value) : 0;
                     var now = (new Date).getTime();
 
@@ -577,10 +594,11 @@ define([
                                         window.open('mailto:sales@onlyoffice.com', "_blank");
                                     }
                                 }
-                            ],
+                            ]
                         });
                     }
-                }
+                } else
+                    DE.getController('Toolbar').activateControls();
             },
 
             onOpenDocument: function(progress) {
@@ -638,8 +656,6 @@ define([
                 me.appOptions.canDownload       = me.permissions.download !== false && (!type || typeof type[1] !== 'string');
                 me.appOptions.canReader         = (!type || typeof type[1] !== 'string');
 
-                me._state.licenseWarning = (licType===Asc.c_oLicenseResult.Connections) && me.appOptions.canEdit && me.editorConfig.mode !== 'view';
-
                 me.appOptions.canBranding  = (licType === Asc.c_oLicenseResult.Success) && (typeof me.editorConfig.customization == 'object');
                 me.appOptions.canBrandingExt = params.asc_getCanBranding() && (typeof me.editorConfig.customization == 'object');
 
diff --git a/apps/documenteditor/mobile/app/controller/Toolbar.js b/apps/documenteditor/mobile/app/controller/Toolbar.js
index 2da2aca06..e295d2e64 100644
--- a/apps/documenteditor/mobile/app/controller/Toolbar.js
+++ b/apps/documenteditor/mobile/app/controller/Toolbar.js
@@ -79,6 +79,7 @@ define([
                 this.api.asc_registerCallback('asc_onCanUndo',      _.bind(this.onApiCanRevert, this, 'undo'));
                 this.api.asc_registerCallback('asc_onCanRedo',      _.bind(this.onApiCanRevert, this, 'redo'));
                 this.api.asc_registerCallback('asc_onFocusObject',  _.bind(this.onApiFocusObject, this));
+                Common.NotificationCenter.on('api:disconnect',      _.bind(this.onCoAuthoringDisconnect, this));
             },
 
             setMode: function (mode) {
@@ -138,6 +139,8 @@ define([
             // API handlers
 
             onApiCanRevert: function(which, can) {
+                if (this.isDisconnected) return;
+
                 if (which == 'undo') {
                     $('#toolbar-undo').toggleClass('disabled', !can);
                 } else {
@@ -146,6 +149,8 @@ define([
             },
 
             onApiFocusObject: function (objects) {
+                if (this.isDisconnected) return;
+
                 if (objects.length > 0) {
                     var topObject = _.find(objects.reverse(), function (obj) {
                             return obj.get_ObjectType() != Asc.c_oAscTypeSelectElement.SpellCheck;
@@ -161,6 +166,14 @@ define([
                 $('#toolbar-edit, #toolbar-add, #toolbar-settings, #toolbar-search, #document-back').removeClass('disabled');
             },
 
+            deactivateControls: function() {
+                $('#toolbar-edit, #toolbar-add, #toolbar-settings, #toolbar-search, #document-back').addClass('disabled');
+            },
+
+            onCoAuthoringDisconnect: function() {
+                this.isDisconnected = true;
+            },
+
             dlgLeaveTitleText   : 'You leave the application',
             dlgLeaveMsgText     : 'You have unsaved changes in this document. Click \'Stay on this Page\' to await the autosave of the document. Click \'Leave this Page\' to discard all the unsaved changes.',
             leaveButtonText     : 'Leave this Page',