[geary/wip/712912-select-quote: 2/7] Use selected text in quotes when available



commit 3bb74bb0149f88a87912b1a107403219bf4c8e04
Author: Robert Schroll <rschroll gmail com>
Date:   Sun Aug 24 19:52:12 2014 -0400

    Use selected text in quotes when available
    
    https://bugzilla.gnome.org/show_bug.cgi?id=712912

 src/client/application/geary-controller.vala       |   29 +++++++++++--------
 src/client/composer/composer-widget.vala           |    6 ++--
 .../conversation-viewer/conversation-viewer.vala   |   18 +++++++++---
 src/engine/rfc822/rfc822-utils.vala                |   26 ++++++++---------
 4 files changed, 46 insertions(+), 33 deletions(-)
---
diff --git a/src/client/application/geary-controller.vala b/src/client/application/geary-controller.vala
index bb4b73d..38795af 100644
--- a/src/client/application/geary-controller.vala
+++ b/src/client/application/geary-controller.vala
@@ -1409,7 +1409,7 @@ public class GearyController : Geary.BaseObject {
     }
     
     private void on_edit_draft(Geary.Email draft) {
-        create_compose_widget(ComposerWidget.ComposeType.NEW_MESSAGE, draft, null, true);
+        create_compose_widget(ComposerWidget.ComposeType.NEW_MESSAGE, draft, null, null, true);
     }
     
     private void on_special_folder_type_changed(Geary.Folder folder, Geary.SpecialFolderType old_type,
@@ -2062,12 +2062,14 @@ public class GearyController : Geary.BaseObject {
     }
     
     private void create_compose_widget(ComposerWidget.ComposeType compose_type,
-        Geary.Email? referred = null, string? mailto = null, bool is_draft = false) {
-        create_compose_widget_async.begin(compose_type, referred, mailto, is_draft);
+        Geary.Email? referred = null, string? quote = null, string? mailto = null,
+        bool is_draft = false) {
+        create_compose_widget_async.begin(compose_type, referred, quote, mailto, is_draft);
     }
     
     private async void create_compose_widget_async(ComposerWidget.ComposeType compose_type,
-        Geary.Email? referred = null, string? mailto = null, bool is_draft = false) {
+        Geary.Email? referred = null, string? quote = null, string? mailto = null,
+        bool is_draft = false) {
         if (current_account == null)
             return;
         
@@ -2090,7 +2092,7 @@ public class GearyController : Geary.BaseObject {
                 }
             }
             
-            widget = new ComposerWidget(current_account, compose_type, full, is_draft);
+            widget = new ComposerWidget(current_account, compose_type, full, quote, is_draft);
         }
         widget.show_all();
         
@@ -2195,9 +2197,10 @@ public class GearyController : Geary.BaseObject {
     }
     
     private void on_reply_to_message_action() {
-        Geary.Email? message = main_window.conversation_viewer.get_selected_message();
+        string? quote;
+        Geary.Email? message = main_window.conversation_viewer.get_selected_message(out quote);
         if (message != null)
-            on_reply_to_message(message);
+            create_compose_widget(ComposerWidget.ComposeType.REPLY, message, quote);
     }
     
     private void on_reply_all_message(Geary.Email message) {
@@ -2205,9 +2208,10 @@ public class GearyController : Geary.BaseObject {
     }
     
     private void on_reply_all_message_action() {
-        Geary.Email? message = main_window.conversation_viewer.get_selected_message();
+        string? quote;
+        Geary.Email? message = main_window.conversation_viewer.get_selected_message(out quote);
         if (message != null)
-            on_reply_all_message(message);
+            create_compose_widget(ComposerWidget.ComposeType.REPLY_ALL, message, quote);
     }
     
     private void on_forward_message(Geary.Email message) {
@@ -2215,9 +2219,10 @@ public class GearyController : Geary.BaseObject {
     }
     
     private void on_forward_message_action() {
-        Geary.Email? message = main_window.conversation_viewer.get_selected_message();
+        string? quote;
+        Geary.Email? message = main_window.conversation_viewer.get_selected_message(out quote);
         if (message != null)
-            on_forward_message(message);
+            create_compose_widget(ComposerWidget.ComposeType.FORWARD, message, quote);
     }
     
     private void on_find_in_conversation_action() {
@@ -2463,7 +2468,7 @@ public class GearyController : Geary.BaseObject {
             return;
         }
         
-        create_compose_widget(ComposerWidget.ComposeType.NEW_MESSAGE, null, mailto);
+        create_compose_widget(ComposerWidget.ComposeType.NEW_MESSAGE, null, null, mailto);
     }
     
     // Returns a list of composer windows for an account, or null if none.
diff --git a/src/client/composer/composer-widget.vala b/src/client/composer/composer-widget.vala
index 52308d0..b3c8f88 100644
--- a/src/client/composer/composer-widget.vala
+++ b/src/client/composer/composer-widget.vala
@@ -233,7 +233,7 @@ public class ComposerWidget : Gtk.EventBox {
     }
     
     public ComposerWidget(Geary.Account account, ComposeType compose_type,
-        Geary.Email? referred = null, bool is_referred_draft = false) {
+        Geary.Email? referred = null, string? quote = null, bool is_referred_draft = false) {
         this.account = account;
         this.compose_type = compose_type;
         if (compose_type == ComposeType.NEW_MESSAGE)
@@ -428,13 +428,13 @@ public class ComposerWidget : Gtk.EventBox {
                     subject = reply_subject;
                     in_reply_to = reply_message_id;
                     references = Geary.RFC822.Utils.reply_references(referred);
-                    body_html = "\n\n" + Geary.RFC822.Utils.quote_email_for_reply(referred, true);
+                    body_html = "\n\n" + Geary.RFC822.Utils.quote_email_for_reply(referred, quote, true);
                     pending_attachments = referred.attachments;
                 break;
                 
                 case ComposeType.FORWARD:
                     subject = forward_subject;
-                    body_html = "\n\n" + Geary.RFC822.Utils.quote_email_for_forward(referred, true);
+                    body_html = "\n\n" + Geary.RFC822.Utils.quote_email_for_forward(referred, quote, true);
                     add_attachments(referred.attachments);
                     pending_attachments = referred.attachments;
                 break;
diff --git a/src/client/conversation-viewer/conversation-viewer.vala 
b/src/client/conversation-viewer/conversation-viewer.vala
index 8146e69..66bf8e8 100644
--- a/src/client/conversation-viewer/conversation-viewer.vala
+++ b/src/client/conversation-viewer/conversation-viewer.vala
@@ -211,8 +211,10 @@ public class ConversationViewer : Gtk.Box {
         return messages.is_empty ? null : messages.last();
     }
     
-    public Geary.Email? get_selected_message() {
-        WebKit.DOM.DOMSelection selection = web_view.get_dom_document().default_view.get_selection();
+    public Geary.Email? get_selected_message(out string? quote) {
+        quote = null;
+        WebKit.DOM.Document document = web_view.get_dom_document();
+        WebKit.DOM.DOMSelection selection = document.default_view.get_selection();
         if (selection.is_collapsed)
             return get_last_message();
         
@@ -230,8 +232,16 @@ public class ConversationViewer : Gtk.Box {
         if (focus_element != null)
             focus_email = get_email_from_element(focus_element);
         
-        if (anchor_email != null && anchor_email == focus_email)
-            return anchor_email;
+        if (anchor_email != null && anchor_email == focus_email) {
+            try {
+                WebKit.DOM.HTMLElement dummy = (WebKit.DOM.HTMLElement) document.create_element("div");
+                dummy.append_child(selection.get_range_at(0).clone_contents());
+                quote = dummy.get_inner_html();
+                return anchor_email;
+            } catch (Error error) {
+                debug("Problem getting selected text: %s", error.message);
+            }
+        }
         
         return get_last_message();
     }
diff --git a/src/engine/rfc822/rfc822-utils.vala b/src/engine/rfc822/rfc822-utils.vala
index 7d94d83..208bb10 100644
--- a/src/engine/rfc822/rfc822-utils.vala
+++ b/src/engine/rfc822/rfc822-utils.vala
@@ -148,14 +148,14 @@ public string email_addresses_for_reply(Geary.RFC822.MailboxAddresses? addresses
 /**
  * Returns a quoted text string needed for a reply.
  *
- * If there's no message body in the supplied email, this function will
- * return the empty string.
+ * If there's no message body in the supplied email or quote text, this
+ * function will return the empty string.
  * 
  * If html_format is true, the message will be quoted in HTML format.
  * Otherwise it will be in plain text.
  */
-public string quote_email_for_reply(Geary.Email email, bool html_format) {
-    if (email.body == null)
+public string quote_email_for_reply(Geary.Email email, string? quote, bool html_format) {
+    if (email.body == null && quote == null)
         return "";
     
     string quoted = "<br /><br />";
@@ -187,8 +187,7 @@ public string quote_email_for_reply(Geary.Email email, bool html_format) {
     
     quoted += "<br />";
     
-    if (email.body != null)
-        quoted += "\n" + quote_body(email, true, html_format);
+    quoted += "\n" + quote_body(email, quote, true, html_format);
     
     return quoted;
 }
@@ -196,14 +195,14 @@ public string quote_email_for_reply(Geary.Email email, bool html_format) {
 /**
  * Returns a quoted text string needed for a forward.
  *
- * If there's no message body in the supplied email, this function will
- * return the empty string.
+ * If there's no message body in the supplied email or quote text, this
+ * function will return the empty string.
  *
  * If html_format is true, the message will be quoted in HTML format.
  * Otherwise it will be in plain text.
  */
-public string quote_email_for_forward(Geary.Email email, bool html_format) {
-    if (email.body == null)
+public string quote_email_for_forward(Geary.Email email, string? quote, bool html_format) {
+    if (email.body == null && quote == null)
         return "";
     
     string quoted = "\n\n";
@@ -225,17 +224,16 @@ public string quote_email_for_forward(Geary.Email email, bool html_format) {
     
     quoted = quoted.replace("\n", "<br />");
     
-    if (email.body != null)
-        quoted += quote_body(email, false, html_format);
+    quoted += quote_body(email, quote, false, html_format);
     
     return quoted;
 }
 
-private string quote_body(Geary.Email email, bool use_quotes, bool html_format) {
+private string quote_body(Geary.Email email, string? quote, bool use_quotes, bool html_format) {
     string? body_text = "";
     
     try {
-        body_text = email.get_message().get_body(html_format);
+        body_text = quote ?? email.get_message().get_body(html_format);
     } catch (Error error) {
         debug("Could not get message text. %s", error.message);
     }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]