Пример #1
0
        public bool AddNewMessage(Channel ch, MessageClass message, bool insert = false)
        {
            if (Properties.Settings.Default.ChatMode && (
                    message.Style.Type == MessageTypes.Part ||
                    message.Style.Type == MessageTypes.Join ||
                    message.Style.Type == MessageTypes.Quit)
                )
            {
                return(false);
            }

            try
            {
                Paragraph p = new Paragraph();
                MessageSettings.LoadSettingsFor(p, message.Style);
                p.Foreground            = message.Style.MessageColor;
                p.Margin                = new Thickness(0, 2, 0, 2);
                p.Tag                   = message;
                p.MouseRightButtonDown += InstantColorMenu;

                // Time when the message arrived
                if (Properties.Settings.Default.MessageTime)
                {
                    Run word = new Run(message.Time.ToString("T") + " ");
                    MessageSettings.LoadSettingsFor(word, MessageSettings.MessageTimeStyle);
                    word.Foreground = MessageSettings.MessageTimeStyle.NickColor;
                    p.Inlines.Add(word);
                }

                // Sender of the message
                Run nick = (message.Style.Type == MessageTypes.Action) ? new Run(message.Sender.Name + " ") : new Run(message.Sender.Name + ": ");

                SolidColorBrush b;
                // Instant color
                if (InstantColors.TryGetValue(message.Sender.LowerName, out b))
                {
                    nick.Foreground = b;
                }
                // Group color
                else if (message.Sender.Group.ID != UserGroups.SystemGroupID)
                {
                    nick.Foreground = message.Sender.Group.TextColor;
                    nick.FontStyle  = FontStyles.Italic;
                }
                else
                {
                    nick.Foreground = message.Style.NickColor;
                }
                nick.FontWeight = FontWeights.Bold;
                p.Inlines.Add(nick);

                // Message content
                if (message.Style.IsFixedText)
                {
                    p.Inlines.Add(new Run(message.Message));
                }
                else
                {
                    string[] words;
                    if (message.Words != null)
                    {
                        words = message.Words;
                    }
                    else
                    {
                        words = message.Message.Split(' ');
                    }
                    Uri             uri = null;
                    HightLightTypes highlightType;
                    sb.Clear(); // this StringBuilder is for minimizing the number on Runs in a paragraph
                    for (int i = 0; i < words.Length; i++)
                    {
                        if (message.HighlightWords != null && message.HighlightWords.TryGetValue(i, out highlightType))
                        {
                            // Flush the sb content
                            if (sb.Length > 0)
                            {
                                p.Inlines.Add(new Run(sb.ToString()));
                                sb.Clear();
                            }

                            Run word = new Run(words[i]);
                            if (highlightType == HightLightTypes.Highlight)
                            {
                                word.FontStyle = FontStyles.Italic;
                            }
                            else
                            {
                                MessageSettings.LoadSettingsFor(word, MessageSettings.LeagueFoundMessage);
                                word.Foreground = MessageSettings.LeagueFoundMessage.NickColor;
                            }
                            p.Inlines.Add(word);
                        }
                        // Links
                        else if (
                            (
                                words[i].StartsWith("ftp://", StringComparison.OrdinalIgnoreCase) ||
                                words[i].StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
                                words[i].StartsWith("https://", StringComparison.OrdinalIgnoreCase)
                            ) && Uri.TryCreate(words[i], UriKind.RelativeOrAbsolute, out uri)
                            )
                        {
                            // Flush the sb content
                            if (sb.Length > 0)
                            {
                                p.Inlines.Add(new Run(sb.ToString()));
                                sb.Clear();
                            }

                            Hyperlink word = new Hyperlink(new Run(words[i]));
                            MessageSettings.LoadSettingsFor(word, MessageSettings.HyperLinkStyle);
                            word.Foreground       = MessageSettings.HyperLinkStyle.NickColor;
                            word.NavigateUri      = new Uri(words[i]);
                            word.RequestNavigate += OpenURLInBrowser;
                            p.Inlines.Add(word);
                        }
                        else
                        {
                            sb.Append(words[i]);
                        }
                        if (i + 1 < words.Length)
                        {
                            sb.Append(' ');
                        }
                    }

                    // Flush the sb content
                    if (sb.Length > 0)
                    {
                        p.Inlines.Add(new Run(sb.ToString()));
                    }
                }

                // Insert the new paragraph
                if (insert)
                {
                    ch.TheFlowDocument.Blocks.InsertBefore(ch.TheFlowDocument.Blocks.FirstBlock, p);
                }
                else
                {
                    ch.TheFlowDocument.Blocks.Add(p);
                }

                while (ch.TheFlowDocument.Blocks.Count > GlobalManager.MaxMessagesInMemory)
                {
                    ch.TheFlowDocument.Blocks.Remove(ch.TheFlowDocument.Blocks.FirstBlock);
                }

                return(true);
            }
            catch (Exception e)
            {
                ErrorLog.Log(e);
            }
            return(false);
        }
Пример #2
0
 private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
 {
     ErrorLog.Log(e.Exception);
 }