Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

GoalBook - A Hybrid Smart Client

, 25 Sep 2009
A WPF hybrid smart client that synchronises your goals with the Toodledo online To-do service.
Prize winner in Competition "Smart Client Article Contest"
GoalBook.zip
GoalBook
Dependencies
CAG WPF 2
Microsoft.Practices.Composite.dll
Microsoft.Practices.Composite.Presentation.dll
Microsoft.Practices.Composite.UnityExtensions.dll
Microsoft.Practices.ObjectBuilder2.dll
Microsoft.Practices.ServiceLocation.dll
Microsoft.Practices.Unity.dll
vssver2.scc
CSLA
Csla.dll
Csla.XmlSerializers.dll
vssver2.scc
Infragistics
Infragistics3.Wpf.DataPresenter.v9.1.Express.dll
Infragistics3.Wpf.Editors.v9.1.Express.dll
Infragistics3.Wpf.v9.1.Express.dll
vssver2.scc
WPFToolkit
vssver2.scc
WPFToolkit.dll
GoalBook.Controls
CheckedList
vssver2.scc
Properties
vssver2.scc
vssver2.scc
GoalBook.Goals
GoalBook.Goals.csproj.user
Properties
vssver2.scc
Views
vssver2.scc
vssver2.scc
GoalBook.Infrastructure
Comparers
vssver2.scc
Constants
vssver2.scc
Controls
Converters
vssver2.scc
Enums
vssver2.scc
Events
vssver2.scc
GoalBook.Infrastructure.csproj.user
Helpers
vssver2.scc
Interfaces
vssver2.scc
ObjectModel
vssver2.scc
Printing
vssver2.scc
Properties
vssver2.scc
vssver2.scc
GoalBook.Notes
GoalBook.Notes.csproj.user
Properties
vssver2.scc
Resources
arrow_redo.png
arrow_undo.png
cut.png
page_copy.png
paste_plain.png
text_bold.png
text_indent.png
text_indent_remove.png
text_italic.png
text_list_bullets.png
text_list_numbers.png
vssver2.scc
world_link.png
Views
vssver2.scc
vssver2.scc
GoalBook.Public
Encryption
vssver2.scc
HtmlConverter
vssver2.scc
HtmlParser
vssver2.scc
Misc
vssver2.scc
Properties
vssver2.scc
vssver2.scc
GoalBook.Shell
App.ico
Commands
vssver2.scc
GoalBook.Shell.csproj.user
Journal.ico
Journal48.ico
Misc
vssver2.scc
Modules
vssver2.scc
Properties
licenses.licx
vssver2.scc
Resources
arrow_refresh.png
arrow_undo.png
checked.gif
cross.png
disk.png
email.png
erase.png
error.png
exclamation.png
flag_red.png
group.png
help.png
information.png
magnifier.png
page_red.png
pencil.png
printer.png
user.png
vssver2.scc
Wave.jpg
world.png
world_link.png
Services
vssver2.scc
Splash.png
Views
vssver2.scc
vssver2.scc
Windows
vssver2.scc
GoalBook.snk
GoalBook.Synchronisation
Events
vssver2.scc
Properties
vssver2.scc
ToodleDo
vssver2.scc
vssver2.scc
GoalBook.Tasks
Controls
vssver2.scc
Properties
vssver2.scc
Views
vssver2.scc
vssver2.scc
vssver2.scc
// <copyright file="ToodledoToXamlConverter.cs" company="GoalBook"> 
//    Copyright © 2009 Mark Brownsword. All rights reserved.
//    This source code and supporting files are licensed under The Code Project  
//    Open License (CPOL) as detailed at http://www.codeproject.com/info/cpol10.aspx. 
// </copyright>
namespace GoalBook.Infrastructure.Converters
{
    #region Using Statements
    using System;
    using System.Globalization;
    using System.IO;
    using System.Text;
    using System.Windows;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Xml;
    using GoalBook.Public.HtmlConverter;
    using Parser = GoalBook.Public.HtmlParser;
    #endregion

    /// <summary>
    /// Value Converter for object model item foreign keys.
    /// </summary>
    public class ToodledoToXamlConverter : IValueConverter
    {
        #region Constants and Enums 
        #endregion

        #region IValueConverter Members
        /// <summary>
        /// Convert Toodledo text to Xaml.
        /// </summary>
        /// <param name="value">value parameter</param>
        /// <param name="targetType">targetType parameter</param>
        /// <param name="parameter">object parameter</param>
        /// <param name="culture">culture parameter</param>
        /// <returns>Xaml string from Toodledo text</returns>      
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            XamlToFlowDocumentConverter converter = new XamlToFlowDocumentConverter();

            // Convert Html to Xaml string.             
            string xaml = HtmlToXamlConverter.ConvertHtmlToXaml(value.ToString(), true);

            // Initialise FlowDocument from xaml.            
            FlowDocument flowDocument = converter.Convert(xaml, null, null, null) as FlowDocument;

            // Add Margin Styling to FlowDocument. This ensures that when the document is
            // edited in RichTextBox, new paragraphs have no margin.
            Style style = new Style(typeof(Paragraph));
            style.Setters.Add(new Setter(Block.MarginProperty, new Thickness(0)));
            flowDocument.Resources.Add(typeof(Paragraph), style);
            
            // Convert back to xaml string for result.
            return converter.ConvertBack(flowDocument, null, null, null).ToString();
        }

        /// <summary>
        /// Convert Xaml to Toodledo text.
        /// </summary>
        /// <param name="value">value parameter</param>
        /// <param name="targetType">targetType parameter</param>
        /// <param name="parameter">object parameter</param>
        /// <param name="culture">culture parameter</param>
        /// <returns>Toodledo text from Html string</returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Parser.ParseHTML parser = new Parser.ParseHTML();
            Parser.AttributeList htmlTag = null;
            Parser.Attribute attribute = null;
            StringBuilder result = new StringBuilder();
            XmlTextWriter writer = new XmlTextWriter(new StringWriter(result));
            ushort listItemCounter = 0;
            ushort formatTagCounter = 0;

            parser.Source = HtmlFromXamlConverter.ConvertXamlToHtml(value.ToString());
            
            while (!parser.Eof())
            {
                char ch = parser.Parse();
                if (ch == 0)
                {
                    // Html Tag
                    htmlTag = parser.GetTag();
                    
                    if (htmlTag.Name == "SPAN")
                    {
                        attribute = htmlTag[0];
                        if (attribute != null)
                        {
                            if (attribute.Name == "STYLE")
                            {
                                if (attribute.Value.Contains("font-weight:bold;"))
                                {
                                    writer.WriteStartElement("B");
                                    formatTagCounter++;
                                }

                                if (attribute.Value.Contains("font-style:italic;"))
                                {
                                    writer.WriteStartElement("I");
                                    formatTagCounter++;
                                }
                            }
                        }
                    }
                    else if (htmlTag.Name == "UL" || htmlTag.Name == "OL" || htmlTag.Name == "LI")
                    {
                        writer.WriteStartElement(htmlTag.Name);
                        listItemCounter++;
                    }
                    else if (htmlTag.Name == "A")
                    {
                        writer.WriteStartElement(htmlTag.Name);
                        for (int i = 0; i < htmlTag.Count; i++)
                        {
                            attribute = htmlTag[i];
                            writer.WriteAttributeString(attribute.Name, attribute.Value);
                        }                                                                                                                                                        
                    }                    
                    else if (htmlTag.Name == "/UL" || htmlTag.Name == "/OL" || htmlTag.Name == "/LI")
                    {
                        writer.WriteEndElement();
                        listItemCounter--;
                    }
                    else if (htmlTag.Name == "/A")
                    {
                        writer.WriteEndElement();
                    }
                    else if (htmlTag.Name == "/SPAN")
                    {
                        while (formatTagCounter > 0)
                        {
                            writer.WriteEndElement();
                            formatTagCounter--;
                        }
                    }
                    else if (IsEmptyParagraph(htmlTag) || htmlTag.Name == "/P" || htmlTag.Name == "BR")
                    {
                        if (listItemCounter == 0 && this.NewLineRequired(parser))
                        {
                            writer.WriteString(Environment.NewLine);
                        }
                    }
                }
                else
                {
                    // Text
                    writer.WriteString(ch.ToString());
                }
            }            
                                    
            return result.ToString();
        }
       
        /// <summary>
        /// Determine if tag Is Empty Paragraph.
        /// </summary>
        /// <returns></returns>
        private bool IsEmptyParagraph(Parser.AttributeList htmlTag)
        {
            if (htmlTag.Name == "P")
            {
                for (int i = 0; i < htmlTag.List.Count; i++)
                {
                    if ((htmlTag.List[i] as Parser.Attribute).Name == "/")
                    {
                        return true;
                    }
                }
            }

            return false;            
        }

        /// <summary>
        /// Determine if NewLine Required.
        /// </summary>
        /// <param name="parser">ParseHTML parameter</param>
        /// <returns>True if NewLine Required</returns>
        private bool NewLineRequired(Parser.ParseHTML parser)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < 4; i++)
            {
                if (!parser.Eof())
                {
                    builder.Append(parser.GetCurrentChar(i));
                }
            }

            if (builder.ToString() == "<UL>" || builder.ToString() == "<OL>" || builder.ToString() == "<LI>" || builder.ToString() == "</BO")
            {
                return false;
            }

            return true;
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mark Brownsword
Software Developer (Senior)
Australia Australia
I've been working as a software developer since 2000 and hold a Bachelor of Business degree from The Open Polytechnic of New Zealand. Computers are for people and I aim to build applications for people that they would want to use.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 25 Sep 2009
Article Copyright 2009 by Mark Brownsword
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid