Click here to Skip to main content
15,886,362 members
Articles / Desktop Programming / WPF

GoalBook - A Hybrid Smart Client

Rate me:
Please Sign up or sign in to vote.
4.86/5 (24 votes)
25 Sep 2009CPOL10 min read 79.1K   834   69  
A WPF hybrid smart client that synchronises your goals with the Toodledo online To-do service.
//===============================================================================
// Goal Book.
// Copyright © 2009 Mark Brownsword. 
//===============================================================================

#region Using Statements
using System;
using System.ComponentModel;
using GoalBook.Infrastructure;
using GoalBook.Infrastructure.ObjectModel;
using GoalBook.Synchronisation.Events;
using GoalBook.Synchronisation.Properties;
using GoalBook.Synchronisation.ToodleDo;
#endregion

namespace GoalBook.Synchronisation
{
    public class SyncService
    {
        #region Constants and Enums
        #endregion

        #region Inner Classes and Structures
        #endregion

        #region Delegates and Events

        #region SyncException event
        private SyncExceptionEventHandler _syncException;
        /// <summary>
        /// SyncException event.
        /// </summary>
        public event SyncExceptionEventHandler SyncException
        {
            add { _syncException = (SyncExceptionEventHandler)Delegate.Combine(_syncException, value); }
            remove { _syncException = (SyncExceptionEventHandler)Delegate.Remove(_syncException, value); }
        }
        /// <summary>
        /// Raise the SyncException event.
        /// </summary>        
        private void OnSyncException(Exception exception)
        {
            if (_syncException != null)
            {
                _syncException(new SyncExceptionEventArgs(exception));                
            }
        }
        #endregion

        #region SyncProgressChanged event
        private SyncProgressChangedEventHandler _syncProgressChanged;
        /// <summary>
        /// SyncProgressChanged event.
        /// </summary>
        public event SyncProgressChangedEventHandler SyncProgressChanged
        {
            add { _syncProgressChanged = (SyncProgressChangedEventHandler)Delegate.Combine(_syncProgressChanged, value); }
            remove { _syncProgressChanged = (SyncProgressChangedEventHandler)Delegate.Remove(_syncProgressChanged, value); }
        }
        /// <summary>
        /// Raise the SyncProgressChanged event.
        /// </summary>        
        private void OnSyncProgressChanged(string message, int percentComplete)
        {
            if (_syncProgressChanged != null)
            {
                _syncProgressChanged(new SyncProgressChangedEventArgs(message, percentComplete));
            }
        }
        #endregion

        #region SyncCompleted event
        private SyncCompletedEventHandler _syncCompleted;
        /// <summary>
        /// SyncCompleted event.
        /// </summary>
        public event SyncCompletedEventHandler SyncCompleted
        {
            add { _syncCompleted = (SyncCompletedEventHandler)Delegate.Combine(_syncCompleted, value); }
            remove { _syncCompleted = (SyncCompletedEventHandler)Delegate.Remove(_syncCompleted, value); }
        }
        /// <summary>
        /// Raise the SyncCompleted event.
        /// </summary>        
        private void OnSyncCompleted(SyncData data)
        {
            if (_syncCompleted != null)
            {                            
                _syncCompleted(new SyncCompletedEventArgs(
                    data, 
                    string.Format(Resources.SyncCompletedMessage, GetUpdateCompletedDateTime())));
            }
        }
        #endregion

        #endregion

        #region Instance and Shared Fields
        private BackgroundWorker _syncBackgroundWorker;        
        #endregion

        #region Constructors
        /// <summary>
        /// Constructor.
        /// </summary>        
        public SyncService()
        {            
            _syncBackgroundWorker = new BackgroundWorker();
            _syncBackgroundWorker.WorkerReportsProgress = true;
            _syncBackgroundWorker.WorkerSupportsCancellation = true;
            _syncBackgroundWorker.DoWork += new DoWorkEventHandler(SyncBackgroundWorker_DoWork);
            _syncBackgroundWorker.ProgressChanged += new ProgressChangedEventHandler(SyncBackgroundWorker_ProgressChanged);
            _syncBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(SyncBackgroundWorker_RunWorkerCompleted);
        }
        #endregion

        #region Properties

        /// <summary>
        /// Reference to ConnectInfo.
        /// </summary>
        public Credentials ConnectInfo { get; set; }

        /// <summary>
        /// Reference to HostInfo.
        /// </summary>
        public Proxy HostInfo { get; set; }
            
        /// <summary>
        /// Reference to SyncTokenInfo.
        /// </summary>
        public SyncTokenInfo TokenInfo { get; set; }

        #endregion

        #region Private and Protected Methods

        /// <summary>
        /// Get Update Completed DateTime.
        /// </summary>        
        private string GetUpdateCompletedDateTime()
        {
            return string.Format(
                Properties.Resources.SyncUpdateCompletedFormat, 
                DateTime.Now.ToShortDateString(), 
                DateTime.Now.ToShortTimeString());
        }

        #endregion

        #region Public and internal Methods

        /// <summary>
        /// Synchronise Data. Runs in worker thread.
        /// </summary>        
        public void BeginSync(SyncData syncData)
        {
            if (_syncBackgroundWorker.IsBusy) { return; }

            _syncBackgroundWorker.RunWorkerAsync(syncData);
        }

        #endregion

        #region Event Handlers

        #region Goal Synchronisation

        /// <summary>
        /// Handle GoalSyncBackgroundWorker_DoWork event.
        /// </summary>        
        private void SyncBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            SyncData data = e.Argument as SyncData;

            // Initialise the ToodledoConnector.
            _syncBackgroundWorker.ReportProgress(10, Resources.SyncInitialisationMessage);
            ToodledoConnector connector = new ToodledoConnector(ConnectInfo, HostInfo, TokenInfo);                     
            connector.Initialise();
                        
            if (connector.LastGoalEdit > data.Goals.LastServerGoalEdit || data.Goals.SyncRequired)
            {
                // Sync Goals.
                _syncBackgroundWorker.ReportProgress(20, Resources.SyncUpdateGoalsMessage);
                Synchronisor<GoalList> goalSynchronisor = new GoalSynchronisor(connector);
                
                goalSynchronisor.Sync(data.Goals);
            }

            if (connector.LastFolderEdit > data.Folders.LastServerFolderEdit || data.Folders.SyncRequired)
            {
                //Sync Folders.
                _syncBackgroundWorker.ReportProgress(40, Resources.SyncUpdateFoldersMessage);
                Synchronisor<FolderList> folderSynchronisor = new FolderSynchronisor(connector);

                folderSynchronisor.Sync(data.Folders);
            }

            if (connector.LastNoteEdit > data.Notes.LastServerNoteEdit || data.Notes.SyncRequired)
            {
                // Sync Notes.
                _syncBackgroundWorker.ReportProgress(60, Resources.SyncUpdateNotesMessage);
                Synchronisor<NoteList> noteSynchronisor = new NoteSynchronisor(connector, data.Folders);                
                noteSynchronisor.Sync(data.Notes);
            }

            if (connector.LastTaskEdit > data.Tasks.LastServerTaskEdit || data.Tasks.SyncRequired)
            {
                // Sync Notes.
                _syncBackgroundWorker.ReportProgress(80, Resources.SyncUpdateTasksMessage);
                Synchronisor<TaskList> taskSynchronisor = new TaskSynchronisor(connector, data.Folders);
                taskSynchronisor.Sync(data.Tasks);
            }

            // Finalise the Synchronisation.
            _syncBackgroundWorker.ReportProgress(90, Resources.SyncFinalisationMessage);
            TokenInfo = connector.Finalise();
            data.Goals.LastServerGoalEdit = connector.LastGoalEdit;
            data.Folders.LastServerFolderEdit = connector.LastFolderEdit;
            data.Notes.LastServerNoteEdit = connector.LastNoteEdit;
            data.Tasks.LastServerTaskEdit = connector.LastTaskEdit;
            
            // Synchronisation complete.            
            e.Result = data;
        }
        
        /// <summary>
        /// Handle SyncBackgroundWorker_ProgressChanged event.
        /// </summary>        
        private void SyncBackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            OnSyncProgressChanged(e.UserState.ToString(), e.ProgressPercentage);
        }

        /// <summary>
        /// Handle SyncBackgroundWorker_RunWorkerCompleted event.
        /// </summary>        
        private void SyncBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {            
            if (e.Error != null) 
            { 
                OnSyncException(e.Error); 
            }
            else 
            { 
                OnSyncCompleted(e.Result as SyncData); 
            }
        }

        #endregion

        #endregion

        #region Base Class Overrides
        #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)


Written By
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.

Comments and Discussions