From 517286fdab0313dfc4f7d0876bb16480b80036aa Mon Sep 17 00:00:00 2001
From: Ralf Becker <ralfbecker@outdoor-training.de>
Date: Tue, 19 May 2015 20:23:38 +0000
Subject: [PATCH] using now a domain-specific "egroupware" keyring, instead of
 default "mailvelope", which only works on localhost, plus improved
 instructions

---
 mail/js/app.js                |  5 +++--
 phpgwapi/js/jsapi/app_base.js | 28 ++++++++++++++++++++++++++--
 2 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/mail/js/app.js b/mail/js/app.js
index a7dfab2ee0..37ba69cb22 100644
--- a/mail/js/app.js
+++ b/mail/js/app.js
@@ -4407,8 +4407,9 @@ app.classes.mail = AppJS.extend(
 		{
 			if (typeof mailvelope == 'undefined')
 			{
-				this.egw.message(this.egw.lang('You need to install Mailvelope plugin available for Chrome and Firefox and enable it for your domain.')+
-					"\n"+this.egw.lang('Download from %1','<a href="https://www.mailvelope.com/">mailvelope.com</a>'), 'info');
+				this.egw.message(this.egw.lang('You need to install Mailvelope plugin available for Chrome and Firefox from %1.','<a href="https://www.mailvelope.com/">mailvelope.com</a>')+"\n"+
+					this.egw.lang('Add your domain as "%1" in options to list of email providers and enable API.',
+					'*.'+this._mailvelopeDomain()), 'info');
 				return;
 			}
 			var mimeType = this.et2.getWidgetById('mimeType');
diff --git a/phpgwapi/js/jsapi/app_base.js b/phpgwapi/js/jsapi/app_base.js
index 9f2d662f80..dd5c814011 100644
--- a/phpgwapi/js/jsapi/app_base.js
+++ b/phpgwapi/js/jsapi/app_base.js
@@ -858,13 +858,37 @@ var AppJS = Class.extend(
 		var callback = _callback;
 		var self = this;
 
-		mailvelope.getKeyring('mailvelope').then(function(_keyring)
+		mailvelope.getKeyring('egroupware').then(function(_keyring)
 		{
 			callback.call(self, _keyring);
 		},
 		function(_err)
 		{
-			self.egw.message(_err.message, 'error');
+			mailvelope.createKeyring('egroupware').then(function(_keyring)
+			{
+				self.egw.message(self.egw.lang('Keyring "%1" created.', self._mailvelopeDomain()+' (egroupware)')+"\n\n"+
+					self.egw.lang('Please click on lock icon in lower right corner to create or import a key:')+"\n"+
+					self.egw.lang("Go to Key Management and create a new key-pair or import your existing one.")+"\n\n"+
+					self.egw.lang("You will NOT be able to send or receive encrypted mails before completing that step!"), 'info');
+
+				callback.call(self, _keyring);
+			},
+			function(_err)
+			{
+				self.egw.message(_err.message, 'error');
+			});
 		});
+	},
+
+	/**
+	 * Mailvelope uses Domain without first part: eg. "stylite.de" for "egw.stylite.de"
+	 *
+	 * @returns {string}
+	 */
+	_mailvelopeDomain: function()
+	{
+		var parts = document.location.hostname.split('.');
+		if (parts.length > 1) parts.shift();
+		return parts.join('.');
 	}
 });