From 868deb5dabdc4d63210e4f53a2a80a904247af6a Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Wed, 06 Oct 2010 13:15:38 -0400
Subject: [PATCH] - Make htmleditor option behaviour consistent, add option to use HTML on reply to HTML message (#1485840)

---
 CHANGELOG                             |    1 
 installer/config.php                  |   10 ++-
 program/steps/mail/compose.inc        |  101 ++++++++++++++++++++-------------
 program/steps/settings/func.inc       |    7 +
 config/main.inc.php.dist              |    3 
 program/include/rcube_message.php     |   17 ++---
 program/steps/settings/save_prefs.inc |    3 
 7 files changed, 83 insertions(+), 59 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 0b7c850..650b4ba 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -19,6 +19,7 @@
 - Fix keyboard doesn't work with autocomplete list with Chrome (#1487029)
 - Improve tabs to fixed width and add tabs in identities info (#1486974)
 - Add unique index on users.username+users.mail_host
+- Make htmleditor option more consistent and add option to use HTML on reply to HTML message (#1485840)
 
 RELEASE 0.4.2
 -------------
diff --git a/config/main.inc.php.dist b/config/main.inc.php.dist
index 00ba825..8ec7f08 100644
--- a/config/main.inc.php.dist
+++ b/config/main.inc.php.dist
@@ -479,7 +479,8 @@
 $rcmail_config['show_images'] = 0;
 
 // compose html formatted messages by default
-$rcmail_config['htmleditor'] = false;
+// 0 - never, 1 - always, 2 - on reply to HTML message only 
+$rcmail_config['htmleditor'] = 0;
 
 // show pretty dates as standard
 $rcmail_config['prettydate'] = true;
diff --git a/installer/config.php b/installer/config.php
index f35c6c2..5250782 100644
--- a/installer/config.php
+++ b/installer/config.php
@@ -14,7 +14,6 @@
   'smtp_log' => 1,
   'prefer_html' => 1,
   'preview_pane' => 1,
-  'htmleditor' => 1,
   'debug_level' => 1,
 );
 
@@ -543,13 +542,16 @@
 
 <dt class="propname">htmleditor <span class="userconf">*</span></dt>
 <dd>
+<label for="cfghtmlcompose">Compose HTML formatted messages</label>
 <?php
 
-$check_htmlcomp = new html_checkbox(array('name' => '_htmleditor', 'id' => "cfghtmlcompose", 'value' => 1));
-echo $check_htmlcomp->show(intval($RCI->getprop('htmleditor')));
+$select_htmlcomp = new html_select(array('name' => '_htmleditor', 'id' => "cfghtmlcompose"));
+$select_htmlcomp->add('never', 0);
+$select_htmlcomp->add('always', 1);
+$select_htmlcomp->add('on reply to HTML message only', 2);
+echo $select_htmlcomp->show(intval($RCI->getprop('htmleditor')));
 
 ?>
-<label for="cfghtmlcompose">Compose HTML formatted messages</label><br />
 </dd>
 
 <dt class="propname">draft_autosave <span class="userconf">*</span></dt>
diff --git a/program/include/rcube_message.php b/program/include/rcube_message.php
index 19f36b3..dfccb36 100644
--- a/program/include/rcube_message.php
+++ b/program/include/rcube_message.php
@@ -214,27 +214,21 @@
     /**
      * Return the first text part of this message
      *
+     * @param rcube_message_part $part Reference to the part if found
      * @return string Plain text message/part content
      */
-    function first_text_part()
+    function first_text_part(&$part=null)
     {
         // no message structure, return complete body
         if (empty($this->parts))
             return $this->body;
-
-        $out = null;
 
         // check all message parts
         foreach ($this->mime_parts as $mime_id => $part) {
             $mimetype = $part->ctype_primary . '/' . $part->ctype_secondary;
 
             if ($mimetype == 'text/plain') {
-                $out = $this->imap->get_message_part($this->uid, $mime_id, $part);
-
-                // re-format format=flowed content
-                if ($part->ctype_secondary == 'plain' && $part->ctype_parameters['format'] == 'flowed')
-                    $out = self::unfold_flowed($out);
-                break;
+                return $this->imap->get_message_part($this->uid, $mime_id, $part);
             }
             else if ($mimetype == 'text/html') {
                 $out = $this->imap->get_message_part($this->uid, $mime_id, $part);
@@ -245,11 +239,12 @@
 
                 // create instance of html2text class
                 $txt = new html2text($out);
-                $out = $txt->get_text();
+                return $txt->get_text();
             }
         }
 
-        return $out;
+        $part = null;
+        return null;
     }
 
 
diff --git a/program/steps/mail/compose.inc b/program/steps/mail/compose.inc
index c69a0a1..c1f4914 100644
--- a/program/steps/mail/compose.inc
+++ b/program/steps/mail/compose.inc
@@ -358,7 +358,6 @@
     }
   }
 
-
   if ($fname && $field_type)
   {
     // pass the following attributes to the form class
@@ -491,48 +490,74 @@
 }
 
 
+function rcmail_compose_editor_mode()
+{
+  global $RCMAIL, $MESSAGE, $compose_mode;
+  static $useHtml;
+
+  if ($useHtml !== null)
+    return $useHtml;
+
+  $html_editor = intval($RCMAIL->config->get('htmleditor'));
+
+  if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT) {
+    $useHtml = $MESSAGE->has_html_part();
+  }
+  else if ($compose_mode == RCUBE_COMPOSE_REPLY) {
+    $useHtml = ($html_editor == 1 || ($html_editor == 2 && $MESSAGE->has_html_part()));
+  }
+  else { // RCUBE_COMPOSE_FORWARD or NEW
+    $useHtml = ($html_editor == 1);
+  }
+
+  return $useHtml;
+}
+
+
 function rcmail_prepare_message_body()
 {
-  global $RCMAIL, $CONFIG, $MESSAGE, $compose_mode, $LINE_LENGTH, $HTML_MODE;
-
-  if ($CONFIG['htmleditor'] || (($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT) && $MESSAGE->has_html_part()))
-    $isHtml = true;
-  else
-    $isHtml = false;
-
-  $body = '';
+  global $RCMAIL, $MESSAGE, $compose_mode, $LINE_LENGTH, $HTML_MODE;
 
   // use posted message body
-  if (!empty($_POST['_message']))
-  {
+  if (!empty($_POST['_message'])) {
     $body = get_input_value('_message', RCUBE_INPUT_POST, true);
+    $isHtml = (bool) get_input_value('_is_html', RCUBE_INPUT_POST);
   }
-  else if ($_SESSION['compose']['param']['body'])
-  {
+  else if ($_SESSION['compose']['param']['body']) {
     $body = $_SESSION['compose']['param']['body'];
     $isHtml = false;
   }
-  else if ($compose_mode)
-  {
+  // reply/edit/draft/forward
+  else if ($compose_mode) {
     $has_html_part = $MESSAGE->has_html_part();
-    if (($isHtml || $compose_mode == RCUBE_COMPOSE_DRAFT) && $has_html_part)
-    {
-      $body = $MESSAGE->first_html_part();
-      $isHtml = true;
+    $isHtml = rcmail_compose_editor_mode();
+
+    if ($isHtml) {
+      if ($has_html_part) {
+        $body = $MESSAGE->first_html_part();
+      }
+      else {
+        $body = rcmail_plain_body($MESSAGE->first_text_part());
+        if ($body)
+          $body = '<pre>' . $body . '</pre>';
+      }
     }
-    else if ($has_html_part)
-    {
-      // use html part if it has been used for message (pre)viewing
-      // decrease line length for quoting
-      $len = $compose_mode == RCUBE_COMPOSE_REPLY ? $LINE_LENGTH-2 : $LINE_LENGTH;
-      $txt = new html2text($MESSAGE->first_html_part(), false, true, $len);
-      $body = $txt->get_text();
-      $isHtml = false;
-    }
-    else
-    {
-      $body = $MESSAGE->first_text_part();
-      $isHtml = false;
+    else {
+      if ($has_html_part) {
+        // use html part if it has been used for message (pre)viewing
+        // decrease line length for quoting
+        $len = $compose_mode == RCUBE_COMPOSE_REPLY ? $LINE_LENGTH-2 : $LINE_LENGTH;
+        $txt = new html2text($MESSAGE->first_html_part(), false, true, $len);
+        $body = $txt->get_text();
+      }
+      else {
+        $body = $MESSAGE->first_text_part($part);
+        if ($body && $part && $part->ctype_secondary == 'plain'
+            && $part->ctype_parameters['format'] == 'flowed'
+        ) {
+          $body = rcube_message::unfold_flowed($body);
+        }
+      }
     }
 
     // compose reply-body
@@ -544,6 +569,9 @@
     // load draft message body
     else if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT)
       $body = rcmail_create_draft_body($body, $isHtml);
+  }
+  else { // new message
+    $isHtml = rcmail_compose_editor_mode();
   }
 
   $plugin = $RCMAIL->plugins->exec_hook('message_compose_body',
@@ -1151,11 +1179,7 @@
   global $CONFIG, $MESSAGE, $compose_mode;
 
   // determine whether HTML or plain text should be checked
-  if ($compose_mode)
-    $useHtml = (($CONFIG['htmleditor'] || $compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT)
-	&& $MESSAGE->has_html_part());
-  else
-    $useHtml = $CONFIG['htmleditor'] ? true : false;
+  $useHtml = rcmail_compose_editor_mode();
 
   if (empty($attrib['editorid']))
     $attrib['editorid'] = 'rcmComposeBody';
@@ -1172,8 +1196,7 @@
 
   return $select->show($useHtml ? 'html' : 'plain');
 
-  foreach ($choices as $value => $text)
-  {
+  foreach ($choices as $value => $text) {
     $attrib['id'] = '_' . $value;
     $attrib['value'] = $value;
     $selector .= $radio->show($chosenvalue, $attrib) . html::label($attrib['id'], Q(rcube_label($text)));
diff --git a/program/steps/settings/func.inc b/program/steps/settings/func.inc
index 6717b2c..a7d4c11 100644
--- a/program/steps/settings/func.inc
+++ b/program/steps/settings/func.inc
@@ -466,11 +466,14 @@
     // Show checkbox for HTML Editor
     if (!isset($no_override['htmleditor'])) {
       $field_id = 'rcmfd_htmleditor';
-      $input_htmleditor = new html_checkbox(array('name' => '_htmleditor', 'id' => $field_id, 'value' => 1));
+      $select_htmleditor = new html_select(array('name' => '_htmleditor', 'id' => $field_id));
+      $select_htmleditor->add(rcube_label('never'), 0);
+      $select_htmleditor->add(rcube_label('always'), 1);
+      $select_htmleditor->add(rcube_label('htmlonreply'), 2);
 
       $blocks['main']['options']['htmleditor'] = array(
         'title' => html::label($field_id, Q(rcube_label('htmleditor'))),
-        'content' => $input_htmleditor->show($config['htmleditor']?1:0),
+        'content' => $select_htmleditor->show(intval($config['htmleditor'])),
       );
     }
 
diff --git a/program/steps/settings/save_prefs.inc b/program/steps/settings/save_prefs.inc
index 4621427..ae3d6d7 100644
--- a/program/steps/settings/save_prefs.inc
+++ b/program/steps/settings/save_prefs.inc
@@ -58,11 +58,10 @@
       'default_charset' => get_input_value('_default_charset', RCUBE_INPUT_POST),
     );
 
-
   break;
   case 'compose':
     $a_user_prefs = array(
-      'htmleditor'         => isset($_POST['_htmleditor']) ? TRUE : FALSE,
+      'htmleditor'         => intval($_POST['_htmleditor']),
       'draft_autosave'     => isset($_POST['_draft_autosave']) ? intval($_POST['_draft_autosave']) : 0,
       'mime_param_folding' => isset($_POST['_mime_param_folding']) ? intval($_POST['_mime_param_folding']) : 0,
       'force_7bit'         => isset($_POST['_force_7bit']) ? TRUE : FALSE,

--
Gitblit v1.9.1