- 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
- GoalBook.Goals
- GoalBook.Infrastructure
- GoalBook.Notes
- GoalBook.Public
- GoalBook.Shell
- App.ico
- App.xaml
- App.xaml.cs
- Bootstrapper.cs
- Commands
- GoalBook.Shell.csproj
- GoalBook.Shell.csproj.user
- Journal.ico
- Journal48.ico
- Misc
- Modules
- Properties
- 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
- Splash.png
- Views
- vssver2.scc
- Windows
- GoalBook.sln
- GoalBook.snk
- GoalBook.Synchronisation
- GoalBook.Tasks
- License.txt
- ReadMe.txt
- SolutionInfo.cs
- vssver2.scc
|
//===============================================================================
// 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.
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.