private void _CommandHelp(CommandModel cd) { MessageModel msg = new MessageModel(); TextMessagePartModel msgPart; msgPart = new TextMessagePartModel(); // TRANSLATOR: this line is used as a label / category for a // list of commands below msgPart.Text = "[" + _("Frontend Commands") + "]"; msgPart.Bold = true; msg.MessageParts.Add(msgPart); cd.FrontendManager.AddMessageToChat(cd.Chat, msg); string[] help = { "window (number|channelname|queryname|close", "clear", "echo data", "exec command", "detach", }; foreach (string line in help) { cd.FrontendManager.AddTextToChat(cd.Chat, "-!- " + line); } }
public void AddMessageToChat(ChatModel page, MessageModel msg) { Trace.Call(page, msg); string finalMsg = String.Empty; foreach (MessagePartModel msgPart in msg.MessageParts) { // TODO: implement other types if (msgPart is TextMessagePartModel) { TextMessagePartModel fmsgti = (TextMessagePartModel)msgPart; finalMsg += fmsgti.Text; } } string timestamp; try { timestamp = msg.TimeStamp.ToLocalTime().ToString((string)Frontend.UserConfig["Interface/Notebook/TimestampFormat"]); } catch (FormatException e) { timestamp = "Timestamp Format ERROR: " + e.Message; } finalMsg = timestamp + " " + page.Name + " " + finalMsg; //Console.WriteLine(finalMsg); _TextView.AddText(finalMsg); }
public MessageModel ToMessage() { var msg = new MessageModel() { MessageType = this.MessageType, TimeStamp = this.TimeStamp }; foreach (var msgPart in MessageParts) { MessagePartModel part = null; switch (msgPart.Type) { case "Text": var textPart = new TextMessagePartModel() { ForegroundColor = msgPart.ForegroundColor, BackgroundColor = msgPart.BackgroundColor, Underline = msgPart.Underline, Bold = msgPart.Bold, Italic = msgPart.Italic, Text = msgPart.Text }; part = textPart; break; case "URL": var urlPart = new UrlMessagePartModel() { Url = msgPart.Url, Text = msgPart.Text }; part = urlPart; break; case "Image": var imagePart = new ImageMessagePartModel() { ImageFileName = msgPart.ImageFileName, AlternativeText = msgPart.AlternativeText }; part = imagePart; break; } if (part == null) { continue; } part.IsHighlight = msgPart.IsHighlight; msg.MessageParts.Add(part); } return(msg); }
public void ToMarkup() { MessageModel testmodel = new MessageModel(); testmodel.IsCompactable = false; TextMessagePartModel textmodel; UrlMessagePartModel urlmodel; textmodel = new TextMessagePartModel("normal"); testmodel.MessageParts.Add(textmodel); textmodel = new TextMessagePartModel("blue"); textmodel.ForegroundColor = TextColor.Parse("0000FF"); testmodel.MessageParts.Add(textmodel); textmodel = new TextMessagePartModel("bold"); textmodel.Bold = true; testmodel.MessageParts.Add(textmodel); textmodel = new TextMessagePartModel("bold2"); textmodel.Bold = true; testmodel.MessageParts.Add(textmodel); textmodel = new TextMessagePartModel("normal"); testmodel.MessageParts.Add(textmodel); textmodel = new TextMessagePartModel("underline"); textmodel.Underline = true; testmodel.MessageParts.Add(textmodel); textmodel = new TextMessagePartModel("combined"); textmodel.Underline = true; textmodel.Bold = true; textmodel.Italic = true; textmodel.ForegroundColor = TextColor.Parse("00FF00"); textmodel.BackgroundColor = TextColor.Parse("0000FF"); testmodel.MessageParts.Add(textmodel); urlmodel = new UrlMessagePartModel("http://www.smuxi.org"); testmodel.MessageParts.Add(urlmodel); textmodel = new TextMessagePartModel("normal"); testmodel.MessageParts.Add(textmodel); string expected = "normal<span color='#0000FF'>blue</span>" + "<b>bold</b><b>bold2</b>normal<u>underline</u>" + "<span color='#00FF00'><u><b><i>combined</i></b></u></span>" + "<span color='#00008B'><u>http://www.smuxi.org</u></span>normal"; string tested = PangoTools.ToMarkup(testmodel); Assert.AreEqual(expected, tested); }
public virtual void AddMessage(MessageModel msg) { Trace.Call(msg); string timestamp; try { string format = (string)Frontend.UserConfig["Interface/Notebook/TimestampFormat"]; timestamp = msg.TimeStamp.ToLocalTime().ToString(format); } catch (FormatException e) { timestamp = "Timestamp Format ERROR: " + e.Message; } timestamp += " "; _OutputTextView.SelectionHangingIndent = TextRenderer.MeasureText(timestamp, _Font).Width; _OutputTextView.AppendText(timestamp); bool hasHighlight = false; foreach (MessagePartModel msgPart in msg.MessageParts) { #if LOG4NET _Logger.Debug("AddMessage(): msgPart.GetType(): " + msgPart.GetType()); #endif if (msgPart.IsHighlight) { hasHighlight = true; } // TODO: implement all types if (msgPart is UrlMessagePartModel) { UrlMessagePartModel fmsgui = (UrlMessagePartModel)msgPart; /*TODO: Create a link in the TextView (possibly requiring WinAPI hacks...)*/ _OutputTextView.SelectionColor = Color.Blue; _OutputTextView.SelectionFont = new Font(_Font, FontStyle.Underline); _OutputTextView.AppendText(fmsgui.Url); } else if (msgPart is TextMessagePartModel) { /*TODO: Add required formatting to the TextView (possibly requiring WinAPI hacks...)*/ TextMessagePartModel fmsgti = (TextMessagePartModel)msgPart; #if LOG4NET _Logger.Debug("AddMessage(): fmsgti.Text: '" + fmsgti.Text + "'"); #endif FontStyle fstyle = FontStyle.Regular; if (fmsgti.ForegroundColor == TextColor.None) { _OutputTextView.SelectionColor = _ForegroundColor ?? Color.White; } else { #if LOG4NET _Logger.Debug("AddMessage(): fmsgti.ForegroundColor: '" + fmsgti.ForegroundColor.ToString() + "'"); #endif _OutputTextView.SelectionColor = ColorTools.GetColor(fmsgti.ForegroundColor); } if (fmsgti.BackgroundColor == TextColor.None) { _OutputTextView.SelectionBackColor = _BackgroundColor ?? Color.Black; } else { #if LOG4NET _Logger.Debug("AddMessage(): fmsgti.BackgroundColor: '" + fmsgti.BackgroundColor.ToString() + "'"); #endif _OutputTextView.SelectionBackColor = ColorTools.GetColor(fmsgti.BackgroundColor); } if (fmsgti.Underline) { #if LOG4NET _Logger.Debug("AddMessage(): fmsgti.Underline is true"); #endif fstyle |= FontStyle.Underline; } if (fmsgti.Bold) { #if LOG4NET _Logger.Debug("AddMessage(): fmsgti.Bold is true"); #endif fstyle |= FontStyle.Bold; } if (fmsgti.Italic) { #if LOG4NET _Logger.Debug("AddMessage(): fmsgti.Italic is true"); #endif fstyle |= FontStyle.Italic; } _OutputTextView.SelectionFont = new Font(Font, fstyle); _OutputTextView.AppendText(fmsgti.Text); } } _OutputTextView.AppendText("\n"); // HACK: out of scope? if (hasHighlight /*&& !Frontend.MainWindow.HasToplevelFocus*/) { /*TODO: Flash the main window*/ if (Frontend.UserConfig["Sound/BeepOnHighlight"] != null && (bool)Frontend.UserConfig["Sound/BeepOnHighlight"]) { System.Media.SystemSounds.Beep.Play(); } } // HACK: out of scope? if (((TabControl)this.Parent).SelectedTab != this) { string color = null; if (hasHighlight) { _HasHighlight = hasHighlight; color = (string)Frontend.UserConfig["Interface/Notebook/Tab/HighlightColor"]; } else if (!_HasHighlight) { color = (string)Frontend.UserConfig["Interface/Notebook/Tab/ActivityColor"]; } if (color != null) { /*TODO: Color the associated Tab*/ } } }
public void AddMessage(MessageModel msg, bool addLinebreak) { Trace.Call(msg, addLinebreak); if (msg == null) { throw new ArgumentNullException("msg"); } var buffer = Buffer; var iter = buffer.EndIter; Gdk.Color bgColor = DefaultAttributes.Appearance.BgColor; if (_ThemeSettings.BackgroundColor != null) { bgColor = _ThemeSettings.BackgroundColor.Value; } TextColor bgTextColor = ColorConverter.GetTextColor(bgColor); if (_ShowTimestamps) { var msgTimeStamp = msg.TimeStamp.ToLocalTime(); if (_LastMessage != null) { var lastMsgTimeStamp = _LastMessage.TimeStamp.ToLocalTime(); var span = msgTimeStamp.Date - lastMsgTimeStamp.Date; string dayLine = null; if (span.Days > 1) { dayLine = String.Format( "-!- " + _("Day changed from {0} to {1}"), lastMsgTimeStamp.ToShortDateString(), msgTimeStamp.ToShortDateString() ); } else if (span.Days > 0) { dayLine = String.Format( "-!- " + _("Day changed to {0}"), msgTimeStamp.ToLongDateString() ); } if (dayLine != null) { buffer.Insert(ref iter, dayLine + "\n"); } } string timestamp = null; try { string format = (string)Frontend.UserConfig["Interface/Notebook/TimestampFormat"]; if (!String.IsNullOrEmpty(format)) { timestamp = msgTimeStamp.ToString(format); } } catch (FormatException e) { timestamp = "Timestamp Format ERROR: " + e.Message; } if (timestamp != null) { timestamp = String.Format("{0} ", timestamp); if (msg.MessageType == MessageType.Event) { // get best contrast for the event font color Gtk.TextTag eventTag = _MessageTextTagTable.Lookup("event"); Gdk.Color eventColor = eventTag.ForegroundGdk; TextColor eventTextColor = TextColorTools.GetBestTextColor( ColorConverter.GetTextColor(eventColor), bgTextColor, TextColorContrast.High ); eventTag.ForegroundGdk = ColorConverter.GetGdkColor( eventTextColor ); buffer.InsertWithTagsByName(ref iter, timestamp, "event"); } else { buffer.Insert(ref iter, timestamp); } } } bool hasHighlight = false; foreach (MessagePartModel msgPart in msg.MessageParts) { // supposed to be used only in a ChatView if (msgPart.IsHighlight) { hasHighlight = true; } // TODO: implement all types if (msgPart is UrlMessagePartModel) { var urlPart = (UrlMessagePartModel)msgPart; // HACK: the engine should set a color for us! var linkStyleTag = _MessageTextTagTable.Lookup("link"); var linkColor = ColorConverter.GetTextColor( linkStyleTag.ForegroundGdk ); linkColor = TextColorTools.GetBestTextColor( linkColor, bgTextColor ); var linkText = urlPart.Text ?? urlPart.Url; var url = urlPart.Url; // HACK: assume http if no protocol/scheme was specified if (urlPart.Protocol == UrlProtocol.None || !Regex.IsMatch(url, @"^[a-zA-Z0-9\-]+:")) { url = String.Format("http://{0}", url); } Uri uri; try { uri = new Uri(url); } catch (UriFormatException ex) { #if LOG4NET _Logger.Error("AddMessage(): Invalid URL: " + url, ex); #endif buffer.Insert(ref iter, linkText); continue; } var linkTag = new LinkTag(uri); linkTag.ForegroundGdk = ColorConverter.GetGdkColor(linkColor); linkTag.TextEvent += OnLinkTagTextEvent; _MessageTextTagTable.Add(linkTag); buffer.InsertWithTags(ref iter, linkText, linkStyleTag, linkTag); } else if (msgPart is TextMessagePartModel) { TextMessagePartModel fmsgti = (TextMessagePartModel)msgPart; List <string> tags = new List <string>(); if (fmsgti.ForegroundColor != TextColor.None) { var bg = bgTextColor; if (fmsgti.BackgroundColor != TextColor.None) { bg = fmsgti.BackgroundColor; } TextColor color = TextColorTools.GetBestTextColor( fmsgti.ForegroundColor, bg ); //Console.WriteLine("GetBestTextColor({0}, {1}): {2}", fmsgti.ForegroundColor, bgTextColor, color); string tagname = GetTextTagName(color, null); //string tagname = _GetTextTagName(fmsgti.ForegroundColor, null); tags.Add(tagname); } if (fmsgti.BackgroundColor != TextColor.None) { // TODO: get this from ChatView string tagname = GetTextTagName(null, fmsgti.BackgroundColor); tags.Add(tagname); } if (fmsgti.Underline) { #if LOG4NET _Logger.Debug("AddMessage(): fmsgti.Underline is true"); #endif tags.Add("underline"); } if (fmsgti.Bold) { #if LOG4NET _Logger.Debug("AddMessage(): fmsgti.Bold is true"); #endif tags.Add("bold"); } if (fmsgti.Italic) { #if LOG4NET _Logger.Debug("AddMessage(): fmsgti.Italic is true"); #endif tags.Add("italic"); } if (msg.MessageType == MessageType.Event && fmsgti.ForegroundColor == TextColor.None) { // only mark parts that don't have a color set tags.Add("event"); } if (tags.Count > 0) { buffer.InsertWithTagsByName(ref iter, fmsgti.Text, tags.ToArray()); } else { buffer.Insert(ref iter, fmsgti.Text); } } } if (addLinebreak) { buffer.Insert(ref iter, "\n"); } CheckBufferSize(); // HACK: force a redraw of the widget, as for some reason // GTK+ 2.17.6 is not redrawing some lines we add here, especially // for local messages. See: // http://projects.qnetp.net/issues/show/185 QueueDraw(); if (MessageAdded != null) { MessageAdded(this, new MessageTextViewMessageAddedEventArgs(msg)); } if (hasHighlight) { if (MessageHighlighted != null) { MessageHighlighted(this, new MessageTextViewMessageHighlightedEventArgs(msg)); } } _LastMessage = msg; }
public void AddMessage(MessageModel msg, bool addLinebreak, bool showTimestamps) { #if MSG_DEBUG Trace.Call(msg, addLinebreak); #endif if (msg == null) { throw new ArgumentNullException("msg"); } var buffer = Buffer; var iter = buffer.EndIter; var startMark = new Gtk.TextMark(null, true); buffer.AddMark(startMark, iter); var senderPrefixWidth = GetSenderPrefixWidth(msg); Gtk.TextTag indentTag = null; if (senderPrefixWidth != 0) { // TODO: re-use text tags that have the same indent width indentTag = new Gtk.TextTag(null) { Indent = -senderPrefixWidth }; _MessageTextTagTable.Add(indentTag); } if (showTimestamps) { var msgTimeStamp = msg.TimeStamp.ToLocalTime(); if (_LastMessage != null) { var lastMsgTimeStamp = _LastMessage.TimeStamp.ToLocalTime(); var span = msgTimeStamp.Date - lastMsgTimeStamp.Date; if (span.Days > 0) { var dayLine = new MessageBuilder(). AppendEventPrefix(); if (span.Days > 1) { dayLine.AppendText(_("Day changed from {0} to {1}"), lastMsgTimeStamp.ToShortDateString(), msgTimeStamp.ToShortDateString()); } else { dayLine.AppendText(_("Day changed to {0}"), msgTimeStamp.ToLongDateString()); } dayLine.AppendText("\n"); var dayLineMsg = dayLine.ToMessage().ToString(); Buffer.InsertWithTags(ref iter, dayLineMsg, EventTag); } } string timestamp = null; try { string format = (string)Frontend.UserConfig["Interface/Notebook/TimestampFormat"]; if (!String.IsNullOrEmpty(format)) { timestamp = msgTimeStamp.ToString(format); } } catch (FormatException e) { timestamp = "Timestamp Format ERROR: " + e.Message; } if (timestamp != null) { timestamp = String.Format("{0} ", timestamp); buffer.Insert(ref iter, timestamp); // apply timestamp width to indent tag if (indentTag != null) { indentTag.Indent -= GetPangoWidth(timestamp); } } } var msgStartMark = new Gtk.TextMark(null, true); buffer.AddMark(msgStartMark, iter); bool hasHighlight = false; foreach (MessagePartModel msgPart in msg.MessageParts) { // supposed to be used only in a ChatView if (msgPart.IsHighlight) { hasHighlight = true; } // 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()); } else if (msgPart is TextMessagePartModel) { var tags = new List <Gtk.TextTag>(); TextMessagePartModel fmsgti = (TextMessagePartModel)msgPart; 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); } } } var startIter = buffer.GetIterAtMark(startMark); if (msg.MessageType == MessageType.Event) { buffer.ApplyTag(EventTag, startIter, iter); } if (indentTag != null) { buffer.ApplyTag(indentTag, startIter, iter); } var nick = msg.GetNick(); if (nick != null) { // TODO: re-use the same person tag for the same nick var personTag = new PersonTag(nick, nick); personTag.TextEvent += OnPersonTagTextEvent; _MessageTextTagTable.Add(personTag); var msgStartIter = buffer.GetIterAtMark(msgStartMark); var nickEndIter = msgStartIter; nickEndIter.ForwardChars(nick.Length + 2); buffer.ApplyTag(PersonTag, msgStartIter, nickEndIter); buffer.ApplyTag(personTag, msgStartIter, nickEndIter); } buffer.DeleteMark(startMark); buffer.DeleteMark(msgStartMark); if (addLinebreak) { buffer.Insert(ref iter, "\n"); } CheckBufferSize(); if (IsGtk2_17) { // HACK: force a redraw of the widget, as for some reason // GTK+ 2.17.6 is not redrawing some lines we add here, especially // for local messages. See: // http://projects.qnetp.net/issues/show/185 QueueDraw(); } if (Frontend.IsWindows && _LastMessage == null) { // HACK: workaround rendering issue on Windows where the // first inserted text is not showing up until the next insert QueueDraw(); } if (MessageAdded != null) { MessageAdded(this, new MessageTextViewMessageAddedEventArgs(msg)); } if (hasHighlight) { if (MessageHighlighted != null) { MessageHighlighted(this, new MessageTextViewMessageHighlightedEventArgs(msg)); } } _LastMessage = msg; }
public static string ToMarkup(MessageModel msg, Gdk.Color?bgColor) { if (msg == null) { return(String.Empty); } /* Pango Markup doesn't support hyperlinks: * (smuxi-frontend-gnome:9824): Gtk-WARNING **: Failed to set * text from markup due to error parsing markup: Unknown tag * 'a' on line 1 char 59 * * For this reason, for UrlMessagePartModels, we'll render them as * plaintext. * * Here we loop over the MessageModel to build up a proper Pango * Markup. * * The colour codes/values have been taken from BuildTagTable(), in * MessageTextView.cs. * * Documentation for Pango Markup is located at: * http://library.gnome.org/devel/pango/unstable/PangoMarkupFormat.html */ StringBuilder markup = new StringBuilder(); foreach (MessagePartModel msgPart in msg.MessageParts) { if (msgPart is UrlMessagePartModel) { UrlMessagePartModel url = (UrlMessagePartModel)msgPart; string str = GLib.Markup.EscapeText(url.Text); Gdk.Color gdkColor = Gdk.Color.Zero; Gdk.Color.Parse("darkblue", ref gdkColor); TextColor urlColor = ColorConverter.GetTextColor(gdkColor); if (bgColor != null) { // we have a bg color so lets try to get a url color // with a good contrast urlColor = TextColorTools.GetBestTextColor( urlColor, ColorConverter.GetTextColor(bgColor.Value) ); } str = String.Format("<span color='#{0}'><u>{1}</u></span>", urlColor.HexCode, str); markup.Append(str); } else if (msgPart is TextMessagePartModel) { TextMessagePartModel text = (TextMessagePartModel)msgPart; List <string> tags = new List <string>(); string str = GLib.Markup.EscapeText(text.Text); if (text.ForegroundColor != TextColor.None) { TextColor fgColor; if (bgColor == null) { fgColor = text.ForegroundColor; } else { var bgTextColor = ColorConverter.GetTextColor( bgColor.Value ); fgColor = TextColorTools.GetBestTextColor( text.ForegroundColor, bgTextColor ); } tags.Add(String.Format("span color='#{0}'", fgColor.HexCode)); } if (text.Underline) { tags.Add("u"); } if (text.Bold) { tags.Add("b"); } if (text.Italic) { tags.Add("i"); } if (tags.Count > 0) { tags.Reverse(); foreach (string tag in tags) { string endTag; if (tag.Contains(" ")) { // tag contains attributes, only get tag name endTag = tag.Split(' ')[0]; } else { endTag = tag; } str = String.Format("<{0}>{1}</{2}>", tag, str, endTag); } } markup.Append(str); } } return(markup.ToString()); }
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); } }
public virtual MessageModel ToMessage() { var msg = new MessageModel() { MessageType = this.MessageType, TimeStamp = this.TimeStamp }; foreach (var msgPart in MessageParts) { MessagePartModel part = null; switch (msgPart.Type) { case "Text": var textPart = new TextMessagePartModel() { ForegroundColor = msgPart.ForegroundColor, BackgroundColor = msgPart.BackgroundColor, Underline = msgPart.Underline, Bold = msgPart.Bold, Italic = msgPart.Italic, Text = msgPart.Text }; part = textPart; break; case "URL": var urlPart = new UrlMessagePartModel() { Url = msgPart.Url, Text = msgPart.Text }; part = urlPart; break; case "Image": var imagePart = new ImageMessagePartModel() { ImageFileName = msgPart.ImageFileName, AlternativeText = msgPart.AlternativeText }; part = imagePart; break; } if (part == null) { continue; } part.IsHighlight = msgPart.IsHighlight; msg.MessageParts.Add(part); } return msg; }