[smuxi/experiments/gtk2_builder_pref_dialog: 3/25] Frontend-GNOME: refactor MessageTextView.AddMessage()



commit fe5471a3dbdf975b08ff00c5b61085b3949b33a3
Author: Andrés G. Aragoneses <knocte gmail com>
Date:   Thu May 7 15:55:24 2015 +0200

    Frontend-GNOME: refactor MessageTextView.AddMessage()
    
    The AddMessage() method is very long and contains a very smelly
    foreach which checks for types of each iterated element. It's a bit
    cleaner to then have three methods, with different overloads, which
    are called the same way, but do different things depending on the
    type of the parameter.

 src/Frontend-GNOME/Views/MessageTextView.cs |  194 ++++++++++++++-------------
 1 files changed, 103 insertions(+), 91 deletions(-)
---
diff --git a/src/Frontend-GNOME/Views/MessageTextView.cs b/src/Frontend-GNOME/Views/MessageTextView.cs
index 96db4d8..6a00445 100644
--- a/src/Frontend-GNOME/Views/MessageTextView.cs
+++ b/src/Frontend-GNOME/Views/MessageTextView.cs
@@ -455,99 +455,11 @@ namespace Smuxi.Frontend.Gnome
 
                 // TODO: implement all types
                 if (msgPart is UrlMessagePartModel) {
-                    var urlPart = (UrlMessagePartModel) msgPart;
-                    var linkText = urlPart.Text ?? urlPart.Url;
-
-                    Uri uri;
-                    try {
-                        uri = new Uri(urlPart.Url);
-                    } catch (UriFormatException ex) {
-#if LOG4NET
-                        _Logger.Error("AddMessage(): Invalid URL: " + urlPart.Url, ex);
-#endif
-                        buffer.Insert(ref iter, linkText);
-                        continue;
-                    }
-
-                    var tags = new List<Gtk.TextTag>();
-                    // link URI tag
-                    var linkTag = new LinkTag(uri);
-                    linkTag.TextEvent += OnLinkTagTextEvent;
-                    _MessageTextTagTable.Add(linkTag);
-                    tags.Add(linkTag);
-
-                    // link style tag
-                    tags.Add(LinkTag);
-
-                    buffer.InsertWithTags(ref iter, linkText, tags.ToArray());
+                    InsertToBuffer(buffer, ref iter, (UrlMessagePartModel) msgPart);
                 } else if (msgPart is TextMessagePartModel) {
-                    var tags = new List<Gtk.TextTag>();
-                    TextMessagePartModel fmsgti = (TextMessagePartModel) msgPart;
-                    if (fmsgti.Text == null) {
-                        // Gtk.TextBuffer.Insert*() asserts on text == NULL
-                        continue;
-                    }
-                    if (fmsgti.ForegroundColor != TextColor.None) {
-                        var bg = ColorConverter.GetTextColor(BackgroundColor);
-                        if (fmsgti.BackgroundColor != TextColor.None) {
-                            bg = fmsgti.BackgroundColor;
-                        }
-                        TextColor color = TextColorTools.GetBestTextColor(
-                            fmsgti.ForegroundColor, bg
-                        );
-                        string tagname = GetTextTagName(color, null);
-                        var tag = _MessageTextTagTable.Lookup(tagname);
-                        tags.Add(tag);
-                    }
-                    if (fmsgti.BackgroundColor != TextColor.None) {
-                        // TODO: get this from ChatView
-                        string tagname = GetTextTagName(null, fmsgti.BackgroundColor);
-                        var tag = _MessageTextTagTable.Lookup(tagname);
-                        tags.Add(tag);
-                    }
-                    if (fmsgti.Underline) {
-#if LOG4NET && MSG_DEBUG
-                        _Logger.Debug("AddMessage(): fmsgti.Underline is true");
-#endif
-                        tags.Add(UnderlineTag);
-                    }
-                    if (fmsgti.Bold) {
-#if LOG4NET && MSG_DEBUG
-                        _Logger.Debug("AddMessage(): fmsgti.Bold is true");
-#endif
-                        tags.Add(BoldTag);
-                    }
-                    if (fmsgti.Italic) {
-#if LOG4NET && MSG_DEBUG
-                        _Logger.Debug("AddMessage(): fmsgti.Italic is true");
-#endif
-                        tags.Add(ItalicTag);
-                    }
-
-                    if (tags.Count > 0) {
-                        buffer.InsertWithTags(ref iter, fmsgti.Text, tags.ToArray());
-                    } else {
-                        buffer.Insert(ref iter, fmsgti.Text);
-                    }
+                    InsertToBuffer(buffer, ref iter, (TextMessagePartModel) msgPart);
                 } else if (msgPart is ImageMessagePartModel) {
-                    var imgpart = (ImageMessagePartModel) msgPart;
-                    Uri uri = null;
-                    string scheme = null;
-                    try {
-                        uri = new Uri(imgpart.ImageFileName);
-                        scheme = uri.Scheme;
-                    } catch (UriFormatException) {
-                        AddAlternativeText(buffer, ref iter, imgpart);
-                    }
-                    switch (scheme) {
-                        case "smuxi-emoji":
-                            var shortName = uri.Host;
-                            AddEmoji(buffer, ref iter, imgpart, shortName);
-                            break;
-                        default:
-                            AddAlternativeText(buffer, ref iter, imgpart);
-                            break;
-                    }
+                    InsertToBuffer(buffer, ref iter, (ImageMessagePartModel) msgPart);
                 }
             }
             var startIter = buffer.GetIterAtMark(startMark);
@@ -604,6 +516,106 @@ namespace Smuxi.Frontend.Gnome
             _LastMessage = msg;
         }
 
+        private void InsertToBuffer(Gtk.TextBuffer buffer, ref Gtk.TextIter iter, UrlMessagePartModel 
urlPart)
+        {
+            var linkText = urlPart.Text ?? urlPart.Url;
+
+            Uri uri;
+            try {
+                uri = new Uri(urlPart.Url);
+            } catch (UriFormatException ex) {
+#if LOG4NET
+                _Logger.Error("AddMessage(): Invalid URL: " + urlPart.Url, ex);
+#endif
+                buffer.Insert(ref iter, linkText);
+                return;
+            }
+
+            var tags = new List<Gtk.TextTag>();
+            // link URI tag
+            var linkTag = new LinkTag(uri);
+            linkTag.TextEvent += OnLinkTagTextEvent;
+            _MessageTextTagTable.Add(linkTag);
+            tags.Add(linkTag);
+
+            // link style tag
+            tags.Add(LinkTag);
+
+            buffer.InsertWithTags(ref iter, linkText, tags.ToArray());
+        }
+
+        private void InsertToBuffer(Gtk.TextBuffer buffer, ref Gtk.TextIter iter, TextMessagePartModel 
fmsgti)
+        {
+            var tags = new List<Gtk.TextTag>();
+            if (fmsgti.Text == null) {
+                // Gtk.TextBuffer.Insert*() asserts on text == NULL
+                return;
+            }
+            if (fmsgti.ForegroundColor != TextColor.None) {
+                var bg = ColorConverter.GetTextColor(BackgroundColor);
+                if (fmsgti.BackgroundColor != TextColor.None) {
+                    bg = fmsgti.BackgroundColor;
+                }
+                TextColor color = TextColorTools.GetBestTextColor(
+                    fmsgti.ForegroundColor, bg
+                );
+                string tagname = GetTextTagName(color, null);
+                var tag = _MessageTextTagTable.Lookup(tagname);
+                tags.Add(tag);
+            }
+            if (fmsgti.BackgroundColor != TextColor.None) {
+                // TODO: get this from ChatView
+                string tagname = GetTextTagName(null, fmsgti.BackgroundColor);
+                var tag = _MessageTextTagTable.Lookup(tagname);
+                tags.Add(tag);
+            }
+            if (fmsgti.Underline) {
+#if LOG4NET && MSG_DEBUG
+                _Logger.Debug("AddMessage(): fmsgti.Underline is true");
+#endif
+                tags.Add(UnderlineTag);
+            }
+            if (fmsgti.Bold) {
+#if LOG4NET && MSG_DEBUG
+                _Logger.Debug("AddMessage(): fmsgti.Bold is true");
+#endif
+                tags.Add(BoldTag);
+            }
+            if (fmsgti.Italic) {
+#if LOG4NET && MSG_DEBUG
+                _Logger.Debug("AddMessage(): fmsgti.Italic is true");
+#endif
+                tags.Add(ItalicTag);
+            }
+
+            if (tags.Count > 0) {
+                buffer.InsertWithTags(ref iter, fmsgti.Text, tags.ToArray());
+            } else {
+                buffer.Insert(ref iter, fmsgti.Text);
+            }
+        }
+
+        private void InsertToBuffer(Gtk.TextBuffer buffer, ref Gtk.TextIter iter, ImageMessagePartModel 
imgpart)
+        {
+            Uri uri = null;
+            string scheme = null;
+            try {
+                uri = new Uri(imgpart.ImageFileName);
+                scheme = uri.Scheme;
+            } catch (UriFormatException) {
+                AddAlternativeText(buffer, ref iter, imgpart);
+            }
+            switch (scheme) {
+                case "smuxi-emoji":
+                    var shortName = uri.Host;
+                    AddEmoji(buffer, ref iter, imgpart, shortName);
+                    break;
+                default:
+                    AddAlternativeText(buffer, ref iter, imgpart);
+                    break;
+            }
+        }
+
         public void UpdateMarkerline()
         {
             Trace.Call();


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