From 82dcbb7488ce1625ba4f41fbdc8e6319d3da9691 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Sun, 20 Sep 2015 11:45:53 -0400
Subject: [PATCH] Fix various issues in Mailvelope integration (#1490533)

---
 program/js/app.js |   65 ++++++++++++++++++++++++--------
 1 files changed, 49 insertions(+), 16 deletions(-)

diff --git a/program/js/app.js b/program/js/app.js
index 628fc19..8a9712f 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -325,7 +325,9 @@
         else if (this.env.action == 'get')
           this.enable_command('download', 'print', true);
         // show printing dialog
-        else if (this.env.action == 'print' && this.env.uid) {
+        else if (this.env.action == 'print' && this.env.uid
+          && !this.env.is_pgp_content && !this.env.pgp_mime_part
+        ) {
           this.print_dialog();
         }
 
@@ -3361,13 +3363,13 @@
     }
   };
 
-  // 
+  // Load Mailvelope functionality (and initialize keyring if needed)
   this.mailvelope_load = function(action)
   {
     if (this.env.browser_capabilities)
       this.env.browser_capabilities['pgpmime'] = 1;
 
-    var keyring = this.get_local_storage_prefix();
+    var keyring = this.env.user_id;
 
     mailvelope.getKeyring(keyring).then(function(kr) {
       ref.mailvelope_keyring = kr;
@@ -3383,17 +3385,20 @@
     });
   };
 
-  // 
+  // Initializes Mailvelope editor or display container
   this.mailvelope_init = function(action, keyring)
   {
-    if (action == 'show' || action == 'preview') {
+    if (!window.mailvelope)
+      return;
+
+    if (action == 'show' || action == 'preview' || action == 'print') {
       // decrypt text body
-      if (this.env.is_pgp_content && window.mailvelope) {
+      if (this.env.is_pgp_content) {
         var data = $(this.env.is_pgp_content).text();
         ref.mailvelope_display_container(this.env.is_pgp_content, data, keyring);
       }
       // load pgp/mime message and pass it to the mailvelope display container
-      else if (this.env.pgp_mime_part && window.mailvelope) {
+      else if (this.env.pgp_mime_part) {
         var msgid = this.display_message(this.get_label('loadingdata'), 'loading'),
           selector = this.env.pgp_mime_container;
 
@@ -3409,21 +3414,32 @@
         });
       }
     }
-    else if (action == 'compose' && window.mailvelope) {
+    else if (action == 'compose') {
       this.env.compose_commands.push('compose-encrypted');
+      // display the toolbar button
+      $('#' + this.buttons['compose-encrypted'][0].id).show();
+
+      var is_html = $('input[name="_is_html"]').val() > 0;
 
       if (this.env.pgp_mime_message) {
         // fetch PGP/Mime part and open load into Mailvelope editor
         var lock = this.set_busy(true, this.get_label('loadingdata'));
+
         $.ajax({
           type: 'GET',
           url: this.url('get', this.env.pgp_mime_message),
           error: function(o, status, err) {
             ref.http_error(o, status, err, lock);
-            ref.enable_command('compose-encrypted', true);
+            ref.enable_command('compose-encrypted', !is_html);
           },
           success: function(data) {
             ref.set_busy(false, null, lock);
+
+            if (is_html) {
+              ref.command('toggle-editor', {html: false, noconvert: true});
+              $('#' + ref.env.composebody).val('');
+            }
+
             ref.compose_encrypted({ quotedMail: data });
             ref.enable_command('compose-encrypted', true);
           }
@@ -3431,7 +3447,7 @@
       }
       else {
         // enable encrypted compose toggle
-        this.enable_command('compose-encrypted', true);
+        this.enable_command('compose-encrypted', !is_html);
       }
     }
   };
@@ -3439,7 +3455,7 @@
   // handler for the 'compose-encrypted' command
   this.compose_encrypted = function(props)
   {
-    var container = $('#' + this.env.composebody).parent();
+    var options, container = $('#' + this.env.composebody).parent();
 
     // remove Mailvelope editor if active
     if (ref.mailvelope_editor) {
@@ -3451,13 +3467,23 @@
         .find('iframe:not([aria-hidden=true])').remove();
       $('#' + ref.env.composebody).show();
       $("[name='_pgpmime']").remove();
+
+      // disable commands that operate on the compose body
+      ref.enable_command('spellcheck', 'insert-sig', 'toggle-editor', 'insert-response', 'save-response', true);
+      ref.triggerEvent('compose-encrypted', { active:false });
     }
     // embed Mailvelope editor container
     else {
-      var options = { predefinedText: $('#' + this.env.composebody).val() };
+      if (this.spellcheck_state())
+        this.editor.spellcheck_stop();
+
       if (props.quotedMail) {
         options = { quotedMail: props.quotedMail, quotedMailIndent: false };
       }
+      else {
+        options = { predefinedText: $('#' + this.env.composebody).val() };
+      }
+
       if (this.env.compose_mode == 'reply') {
         options.quotedMailIndent = true;
         options.quotedMailHeader = this.env.compose_reply_header;
@@ -3471,6 +3497,10 @@
         container.addClass('mailvelope');
         $('#' + ref.env.composebody).hide();
 
+        // disable commands that operate on the compose body
+        ref.enable_command('spellcheck', 'insert-sig', 'toggle-editor', 'insert-response', 'save-response', false);
+        ref.triggerEvent('compose-encrypted', { active:true });
+
         // notify user about loosing attachments
         if (ref.env.attachments && !$.isEmptyObject(ref.env.attachments)) {
           alert(ref.get_label('encryptnoattachments'));
@@ -3481,6 +3511,7 @@
         }
       }).catch(function(err) {
         console.error(err);
+        console.log(options);
       });
     }
   };
@@ -3620,7 +3651,7 @@
   this.mailvelope_display_container = function(selector, data, keyring, msgid)
   {
     mailvelope.createDisplayContainer(selector, data, keyring, { showExternalContent: this.env.safemode }).then(function() {
-      $(selector).addClass('mailvelope').find('.message-part, .part-notice').hide();
+      $(selector).addClass('mailvelope').children().not('iframe').hide();
       ref.hide_message(msgid);
       setTimeout(function() { $(window).resize(); }, 10);
     }).catch(function(err) {
@@ -3763,7 +3794,7 @@
           ref.hide_message(lock);
 
           if (errorCode) {
-            ref.display_message('Failed to get key from keyserver', 'error');
+            ref.display_message(ref.get_label('keyservererror'), 'error');
             return;
           }
 
@@ -3773,9 +3804,9 @@
               // alert(ref.get_label('Key import was rejected'));
             }
             else {
+              var $key = keyid.substr(-8).toUpperCase();
               btn.closest('.key').fadeOut();
-              ref.display_message(ref.get_label('Public key $key successfully imported into your key ring')
-                .replace('$key', keyid.substr(-8).toUpperCase()), 'confirmation');
+              ref.display_message(ref.get_label('keyimportsuccess').replace('$key', $key), 'confirmation');
             }
           }).catch(function(err) {
             console.log(err);
@@ -4235,6 +4266,8 @@
     if (result) {
       // update internal format flag
       $("input[name='_is_html']").val(props.html ? 1 : 0);
+      // enable encrypted compose toggle
+      this.enable_command('compose-encrypted', !props.html);
     }
 
     return result;

--
Gitblit v1.9.1