[geary/wip/135-contact-popovers: 40/56] Use client contact classes in the conversation viewer



commit aebd0fa0a2bbf040a703d5dda07d6091b1fe6b17
Author: Michael Gratton <mike vee net>
Date:   Thu Mar 14 21:45:09 2019 +1100

    Use client contact classes in the conversation viewer
    
    Update ConversationEmail and ConversationMessage to look up a contact
    for each message's primary originator, use that when determining and
    updating remote resource loading prefs.

 src/client/application/geary-controller.vala       |  1 +
 .../conversation-contact-popover.vala              | 20 +++++---
 .../conversation-viewer/conversation-email.vala    | 50 ++++++-------------
 .../conversation-viewer/conversation-list-box.vala |  6 +++
 .../conversation-viewer/conversation-message.vala  | 57 ++++++++++++++++------
 .../conversation-viewer/conversation-viewer.vala   |  6 ++-
 6 files changed, 81 insertions(+), 59 deletions(-)
---
diff --git a/src/client/application/geary-controller.vala b/src/client/application/geary-controller.vala
index c24c940d..c0b337e1 100644
--- a/src/client/application/geary-controller.vala
+++ b/src/client/application/geary-controller.vala
@@ -1336,6 +1336,7 @@ public class GearyController : Geary.BaseObject {
                     viewer.load_conversation.begin(
                         convo,
                         context.emails,
+                        context.contacts,
                         (obj, ret) => {
                             try {
                                 viewer.load_conversation.end(ret);
diff --git a/src/client/conversation-viewer/conversation-contact-popover.vala 
b/src/client/conversation-viewer/conversation-contact-popover.vala
index c781a9dc..24f87002 100644
--- a/src/client/conversation-viewer/conversation-contact-popover.vala
+++ b/src/client/conversation-viewer/conversation-contact-popover.vala
@@ -12,6 +12,8 @@
 public class Conversation.ContactPopover : Gtk.Popover {
 
 
+    public Application.Contact contact { get; private set; }
+
     public Geary.RFC822.MailboxAddress mailbox { get; private set; }
 
     private GLib.Cancellable load_cancellable = new GLib.Cancellable();
@@ -30,17 +32,21 @@ public class Conversation.ContactPopover : Gtk.Popover {
 
 
     public ContactPopover(Gtk.Widget relative_to,
+                          Application.Contact contact,
                           Geary.RFC822.MailboxAddress mailbox) {
         this.relative_to = relative_to;
+        this.contact = contact;
         this.mailbox = mailbox;
-        if (Geary.String.is_empty_or_whitespace(mailbox.name)) {
-            this.contact_name.set_text(mailbox.address);
+
+        string? display_name = contact.display_name;
+        this.contact_name.set_text(display_name);
+
+        if (!contact.display_name_is_email) {
+            this.contact_address.set_text(mailbox.address);
+        } else {
             this.contact_name.vexpand = true;
             this.contact_name.valign = FILL;
             this.contact_address.hide();
-        } else {
-            this.contact_name.set_text(mailbox.name);
-            this.contact_address.set_text(mailbox.address);
         }
     }
 
@@ -82,7 +88,9 @@ public class Conversation.ContactPopover : Gtk.Popover {
             int pixel_size = Application.AvatarStore.PIXEL_SIZE * window_scale;
             try {
                 Gdk.Pixbuf? avatar_buf = yield loader.load(
-                    this.mailbox, pixel_size, this.load_cancellable
+                    this.mailbox,
+                    pixel_size,
+                    this.load_cancellable
                 );
                 if (avatar_buf != null) {
                     this.avatar.set_from_surface(
diff --git a/src/client/conversation-viewer/conversation-email.vala 
b/src/client/conversation-viewer/conversation-email.vala
index 96f818de..e6174750 100644
--- a/src/client/conversation-viewer/conversation-email.vala
+++ b/src/client/conversation-viewer/conversation-email.vala
@@ -289,9 +289,6 @@ public class ConversationEmail : Gtk.Box, Geary.BaseInterface {
     /** Determines if the email is a draft message. */
     public bool is_draft { get; private set; }
 
-    /** The email's primary originator, if any. */
-    public Geary.RFC822.MailboxAddress? primary_originator { get; private set; }
-
     /** The view displaying the email's primary message headers and body. */
     public ConversationMessage primary_message { get; private set; }
 
@@ -309,7 +306,7 @@ public class ConversationEmail : Gtk.Box, Geary.BaseInterface {
     private Geary.App.EmailStore email_store;
 
     // Store from which to lookup contacts
-    private Geary.ContactStore contact_store;
+    private Application.ContactStore contacts;
 
     // Cancellable to use when loading message content
     private GLib.Cancellable load_cancellable;
@@ -435,6 +432,7 @@ public class ConversationEmail : Gtk.Box, Geary.BaseInterface {
      */
     public ConversationEmail(Geary.Email email,
                              Geary.App.EmailStore email_store,
+                             Application.ContactStore contacts,
                              Configuration config,
                              bool is_sent,
                              bool is_draft,
@@ -442,9 +440,8 @@ public class ConversationEmail : Gtk.Box, Geary.BaseInterface {
         base_ref();
         this.email = email;
         this.is_draft = is_draft;
-        this.primary_originator = Util.Email.get_primary_originator(email);
         this.email_store = email_store;
-        this.contact_store = email_store.account.get_contact_store();
+        this.contacts = contacts;
         this.config = config;
         this.load_cancellable = load_cancellable;
         this.message_bodies_loaded_lock =
@@ -506,18 +503,11 @@ public class ConversationEmail : Gtk.Box, Geary.BaseInterface {
 
         // Construct the view for the primary message, hook into it
 
-        bool load_images = email.load_remote_images().is_certain();
-
-        Geary.Contact? contact = null;
-        if (this.primary_originator != null) {
-            contact = this.contact_store.get_by_rfc822(this.primary_originator);
-        }
-        if (contact != null)  {
-            load_images |= contact.always_load_remote_images();
-        }
-
         this.primary_message = new ConversationMessage.from_email(
-            email, load_images, config
+            email,
+            email.load_remote_images().is_certain(),
+            this.contacts,
+            config
         );
         connect_message_view_signals(this.primary_message);
 
@@ -761,7 +751,6 @@ public class ConversationEmail : Gtk.Box, Geary.BaseInterface {
 
     private void connect_message_view_signals(ConversationMessage view) {
         view.flag_remote_images.connect(on_flag_remote_images);
-        view.remember_remote_images.connect(on_remember_remote_images);
         view.internal_link_activated.connect((y) => {
                 internal_link_activated(y);
             });
@@ -853,7 +842,10 @@ public class ConversationEmail : Gtk.Box, Geary.BaseInterface {
         foreach (Geary.RFC822.Message sub_message in sub_messages) {
             ConversationMessage attached_message =
                 new ConversationMessage.from_message(
-                    sub_message, false, this.config
+                    sub_message,
+                    this.email.load_remote_images().is_certain(),
+                    this.contacts,
+                    this.config
                 );
             connect_message_view_signals(attached_message);
             attached_message.web_view.add_internal_resources(cid_resources);
@@ -1038,22 +1030,10 @@ public class ConversationEmail : Gtk.Box, Geary.BaseInterface {
     }
 
     private void on_flag_remote_images(ConversationMessage view) {
-        // XXX check we aren't already auto loading the image
-        mark_email(Geary.EmailFlags.LOAD_REMOTE_IMAGES, null);
-    }
-
-
-    private void on_remember_remote_images(ConversationMessage view) {
-        Geary.RFC822.MailboxAddress? sender = this.primary_originator;
-        if (sender != null) {
-            Geary.Contact? contact = this.contact_store.get_by_rfc822(sender);
-            if (contact != null) {
-                Geary.ContactFlags flags = new Geary.ContactFlags();
-                flags.add(Geary.ContactFlags.ALWAYS_LOAD_REMOTE_IMAGES);
-                this.contact_store.mark_contacts_async.begin(
-                    Geary.Collection.single(contact), flags, null
-                );
-            }
+        if (!email.email_flags.contains(Geary.EmailFlags.LOAD_REMOTE_IMAGES)) {
+            // Don't pass a cancellable in to make sure the flag is
+            // always saved
+            mark_email(Geary.EmailFlags.LOAD_REMOTE_IMAGES, null);
         }
     }
 
diff --git a/src/client/conversation-viewer/conversation-list-box.vala 
b/src/client/conversation-viewer/conversation-list-box.vala
index 51ed2a3f..b86bc6ba 100644
--- a/src/client/conversation-viewer/conversation-list-box.vala
+++ b/src/client/conversation-viewer/conversation-list-box.vala
@@ -471,6 +471,9 @@ public class ConversationListBox : Gtk.ListBox, Geary.BaseInterface {
     // Used to load messages in conversation.
     private Geary.App.EmailStore email_store;
 
+    // Store from which to lookup contacts
+    private Application.ContactStore contacts;
+
     // App config
     private Configuration config;
 
@@ -549,11 +552,13 @@ public class ConversationListBox : Gtk.ListBox, Geary.BaseInterface {
      */
     public ConversationListBox(Geary.App.Conversation conversation,
                                Geary.App.EmailStore email_store,
+                               Application.ContactStore contacts,
                                Configuration config,
                                Gtk.Adjustment adjustment) {
         base_ref();
         this.conversation = conversation;
         this.email_store = email_store;
+        this.contacts = contacts;
         this.config = config;
 
         this.search = new SearchManager(this, conversation);
@@ -912,6 +917,7 @@ public class ConversationListBox : Gtk.ListBox, Geary.BaseInterface {
         ConversationEmail view = new ConversationEmail(
             email,
             this.email_store,
+            this.contacts,
             this.config,
             is_sent,
             is_draft(email),
diff --git a/src/client/conversation-viewer/conversation-message.vala 
b/src/client/conversation-viewer/conversation-message.vala
index 5a0c530c..ccd7ce48 100644
--- a/src/client/conversation-viewer/conversation-message.vala
+++ b/src/client/conversation-viewer/conversation-message.vala
@@ -153,7 +153,13 @@ public class ConversationMessage : Gtk.Grid, Geary.BaseInterface {
 
     private static GLib.VariantType MAILBOX_TYPE = new GLib.VariantType("(ss)");
 
-    /** Originator used for contact lookup, avatar, and so on. */
+
+    /** Contact for the primary originator, if any. */
+    internal Application.Contact? primary_contact {
+        get; private set;
+    }
+
+    /** Mailbox assumed to be the primary sender. */
     internal Geary.RFC822.MailboxAddress? primary_originator {
         get; private set;
     }
@@ -171,6 +177,8 @@ public class ConversationMessage : Gtk.Grid, Geary.BaseInterface {
 
     private Configuration config;
 
+    private Application.ContactStore contacts;
+
     private GLib.DateTime? local_date = null;
 
     [GtkChild]
@@ -257,7 +265,7 @@ public class ConversationMessage : Gtk.Grid, Geary.BaseInterface {
     private int next_replaced_buffer_number = 0;
 
     // Is the view set to allow remote image loads?
-    private bool is_loading_images;
+    private bool load_remote_resources;
 
     private int remote_resources_requested = 0;
 
@@ -282,9 +290,6 @@ public class ConversationMessage : Gtk.Grid, Geary.BaseInterface {
     /** Fired when the user requests remote images be loaded. */
     public signal void flag_remote_images();
 
-    /** Fired when the user requests remote images be always loaded. */
-    public signal void remember_remote_images();
-
     /** Fired when the user saves an inline displayed image. */
     public signal void save_image(string? uri, string? alt_text, Geary.Memory.Buffer buffer);
 
@@ -297,7 +302,8 @@ public class ConversationMessage : Gtk.Grid, Geary.BaseInterface {
      * loading processes.
      */
     public ConversationMessage.from_email(Geary.Email email,
-                                          bool load_remote_images,
+                                          bool load_remote_resources,
+                                          Application.ContactStore contacts,
                                           Configuration config) {
         this(
             Util.Email.get_primary_originator(email),
@@ -310,7 +316,8 @@ public class ConversationMessage : Gtk.Grid, Geary.BaseInterface {
             email.date,
             email.subject,
             email.preview != null ? email.preview.buffer.get_valid_utf8() : null,
-            load_remote_images,
+            load_remote_resources,
+            contacts,
             config
         );
     }
@@ -323,7 +330,8 @@ public class ConversationMessage : Gtk.Grid, Geary.BaseInterface {
      * loading processes.
      */
     public ConversationMessage.from_message(Geary.RFC822.Message message,
-                                            bool load_remote_images,
+                                            bool load_remote_resources,
+                                            Application.ContactStore contacts,
                                             Configuration config) {
         this(
             Util.Email.get_primary_originator(message),
@@ -336,7 +344,8 @@ public class ConversationMessage : Gtk.Grid, Geary.BaseInterface {
             message.date,
             message.subject,
             message.get_preview(),
-            load_remote_images,
+            load_remote_resources,
+            contacts,
             config
         );
     }
@@ -351,10 +360,16 @@ public class ConversationMessage : Gtk.Grid, Geary.BaseInterface {
                                 Geary.RFC822.Date? date,
                                 Geary.RFC822.Subject? subject,
                                 string? preview,
-                                bool load_remote_images,
+                                bool load_remote_resources,
+                                Application.ContactStore contacts,
                                 Configuration config) {
         base_ref();
-        this.is_loading_images = load_remote_images;
+        this.load_remote_resources = load_remote_resources;
+        this.contacts = contacts;
+
+        if (primary_originator != null) {
+            this.primary_contact = contacts.get(primary_originator);
+        }
         this.primary_originator = primary_originator;
         this.config = config;
 
@@ -454,9 +469,6 @@ public class ConversationMessage : Gtk.Grid, Geary.BaseInterface {
         // Web view
 
         this.web_view = new ConversationWebView(config);
-        if (load_remote_images) {
-            this.web_view.allow_remote_image_loading();
-        }
         this.web_view.context_menu.connect(on_context_menu);
         this.web_view.deceptive_link_clicked.connect(on_deceptive_link_clicked);
         this.web_view.link_activated.connect((link) => {
@@ -651,6 +663,14 @@ public class ConversationMessage : Gtk.Grid, Geary.BaseInterface {
             throw new GLib.IOError.CANCELLED("Conversation load cancelled");
         }
 
+        bool contact_load_images = (
+            this.primary_contact != null &&
+            this.primary_contact.load_remote_resources
+        );
+        if (this.load_remote_resources || contact_load_images) {
+            this.web_view.allow_remote_image_loading();
+        }
+
         show_placeholder_pane(null);
 
         string? body_text = null;
@@ -890,7 +910,7 @@ public class ConversationMessage : Gtk.Grid, Geary.BaseInterface {
 
     private void show_images(bool remember) {
         start_progress_loading();
-        this.is_loading_images = true;
+        this.load_remote_resources = true;
         this.remote_resources_requested = 0;
         this.remote_resources_loaded = 0;
         this.web_view.load_remote_images();
@@ -997,6 +1017,7 @@ public class ConversationMessage : Gtk.Grid, Geary.BaseInterface {
 
             Conversation.ContactPopover popover = new Conversation.ContactPopover(
                 address_child,
+                this.contacts.get(address),
                 address
             );
             popover.load_avatar.begin();
@@ -1127,7 +1148,11 @@ public class ConversationMessage : Gtk.Grid, Geary.BaseInterface {
         case 2:
             // Show images for sender
             show_images(false);
-            remember_remote_images();
+            if (this.primary_contact != null) {
+                this.primary_contact.set_remote_resource_loading.begin(
+                    true, null
+                );
+            }
             break;
         default:
             // Pass
diff --git a/src/client/conversation-viewer/conversation-viewer.vala 
b/src/client/conversation-viewer/conversation-viewer.vala
index fa989206..1e4cf178 100644
--- a/src/client/conversation-viewer/conversation-viewer.vala
+++ b/src/client/conversation-viewer/conversation-viewer.vala
@@ -230,13 +230,15 @@ public class ConversationViewer : Gtk.Stack, Geary.BaseInterface {
      * Shows a conversation in the viewer.
      */
     public async void load_conversation(Geary.App.Conversation conversation,
-                                        Geary.App.EmailStore email_store)
+                                        Geary.App.EmailStore emails,
+                                        Application.ContactStore contacts)
         throws GLib.Error {
         remove_current_list();
 
         ConversationListBox new_list = new ConversationListBox(
             conversation,
-            email_store,
+            emails,
+            contacts,
             this.config,
             this.conversation_scroller.get_vadjustment()
         );


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