Example #1
0
        public void Compact()
        {
            // the idea is to glue each text part that has the same attributes
            // to a combined new one to reduce the number of parts as they are
            // expensive when serialized

            // nothing to glue
            if (MessageParts.Count <= 1)
            {
                return;
            }

            var           parts        = new List <MessagePartModel>(MessageParts.Count);
            StringBuilder gluedText    = null;
            bool          dontMoveNext = false;
            var           iter         = MessageParts.GetEnumerator();

            while (dontMoveNext || iter.MoveNext())
            {
                dontMoveNext = false;
                var current = iter.Current;
                parts.Add(current);

                // we can only glue pure text (not URLs etc)
                if (current.GetType() != typeof(TextMessagePartModel))
                {
                    continue;
                }

                var currentText = (TextMessagePartModel)current;
                while (iter.MoveNext())
                {
                    var next = iter.Current;
                    if (next.GetType() != typeof(TextMessagePartModel))
                    {
                        parts.Add(next);
                        break;
                    }

                    var nextText = (TextMessagePartModel)next;
                    if (!currentText.AttributesEquals(nextText))
                    {
                        // they aren't the same! no candidate for glueing :/
                        // but maybe the next part is
                        dontMoveNext = true;
                        break;
                    }

                    // glue time!
                    if (gluedText == null)
                    {
                        // this is the first element of the gluing
                        gluedText = new StringBuilder(256);
                        gluedText.Append(currentText.Text);
                    }
                    gluedText.Append(nextText.Text);
                }

                if (gluedText != null)
                {
                    currentText.Text = gluedText.ToString();
                    gluedText        = null;
                }
            }

            f_MessageParts = parts;
        }