2011-06-01 23:08:05 +00:00
|
|
|
// SparkleShare, a collaboration and sharing tool.
|
2011-05-19 16:05:58 +00:00
|
|
|
// Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com>
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
2011-11-13 00:57:00 +00:00
|
|
|
using System.Threading;
|
2012-06-10 10:57:31 +00:00
|
|
|
|
|
|
|
using Timers = System.Timers;
|
2011-05-19 16:05:58 +00:00
|
|
|
|
|
|
|
namespace SparkleLib {
|
|
|
|
|
|
|
|
public enum SyncStatus {
|
|
|
|
Idle,
|
|
|
|
SyncUp,
|
|
|
|
SyncDown,
|
|
|
|
Error
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-18 18:40:06 +00:00
|
|
|
public enum ErrorStatus {
|
|
|
|
None,
|
|
|
|
HostUnreachable,
|
|
|
|
HostIdentityChanged,
|
|
|
|
AuthenticationFailed,
|
|
|
|
DiskSpaceExcedeed
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-19 16:05:58 +00:00
|
|
|
public abstract class SparkleRepoBase {
|
2012-06-10 10:57:31 +00:00
|
|
|
|
2012-08-19 10:43:41 +00:00
|
|
|
public static bool UseCustomWatcher = false;
|
|
|
|
|
2012-06-10 10:57:31 +00:00
|
|
|
public abstract string CurrentRevision { get; }
|
|
|
|
public abstract double Size { get; }
|
|
|
|
public abstract double HistorySize { get; }
|
|
|
|
public abstract List<string> ExcludePaths { get; }
|
|
|
|
public abstract bool HasUnsyncedChanges { get; set; }
|
|
|
|
public abstract bool HasLocalChanges { get; }
|
|
|
|
public abstract bool HasRemoteChanges { get; }
|
|
|
|
public abstract bool SyncUp ();
|
|
|
|
public abstract bool SyncDown ();
|
2012-10-15 22:45:03 +00:00
|
|
|
public abstract List<SparkleChangeSet> GetChangeSets ();
|
2012-10-16 10:03:20 +00:00
|
|
|
public abstract List<SparkleChangeSet> GetChangeSets (string path);
|
2012-10-14 13:21:22 +00:00
|
|
|
public abstract void RestoreFile (string path, string revision, string target_file_path);
|
2011-12-15 15:15:29 +00:00
|
|
|
|
2012-07-18 10:35:22 +00:00
|
|
|
public event SyncStatusChangedEventHandler SyncStatusChanged = delegate { };
|
2011-05-19 16:05:58 +00:00
|
|
|
public delegate void SyncStatusChangedEventHandler (SyncStatus new_status);
|
|
|
|
|
2012-07-18 10:35:22 +00:00
|
|
|
public event ProgressChangedEventHandler ProgressChanged = delegate { };
|
2012-02-09 01:46:25 +00:00
|
|
|
public delegate void ProgressChangedEventHandler (double percentage, string speed);
|
2011-12-30 00:44:35 +00:00
|
|
|
|
2012-07-18 10:35:22 +00:00
|
|
|
public event NewChangeSetEventHandler NewChangeSet = delegate { };
|
2011-07-23 21:23:14 +00:00
|
|
|
public delegate void NewChangeSetEventHandler (SparkleChangeSet change_set);
|
2011-05-22 14:46:15 +00:00
|
|
|
|
2012-07-18 10:35:22 +00:00
|
|
|
public event Action ConflictResolved = delegate { };
|
|
|
|
public event Action ChangesDetected = delegate { };
|
2011-05-22 14:46:15 +00:00
|
|
|
|
2011-05-19 16:05:58 +00:00
|
|
|
|
2012-02-09 01:46:25 +00:00
|
|
|
public readonly string LocalPath;
|
|
|
|
public readonly string Name;
|
2012-04-29 14:19:36 +00:00
|
|
|
public readonly Uri RemoteUrl;
|
|
|
|
public List<SparkleChangeSet> ChangeSets { get; protected set; }
|
2012-06-10 10:57:31 +00:00
|
|
|
public SyncStatus Status { get; private set; }
|
2012-09-18 18:40:06 +00:00
|
|
|
public ErrorStatus Error { get; protected set; }
|
2012-06-10 10:57:31 +00:00
|
|
|
public bool IsBuffering { get; private set; }
|
|
|
|
public double ProgressPercentage { get; private set; }
|
|
|
|
public string ProgressSpeed { get; private set; }
|
2012-02-09 01:46:25 +00:00
|
|
|
|
2012-05-19 15:17:09 +00:00
|
|
|
public string Identifier {
|
|
|
|
get {
|
|
|
|
if (this.identifier != null)
|
2012-05-19 15:15:20 +00:00
|
|
|
return this.identifier;
|
|
|
|
|
2012-05-19 15:17:09 +00:00
|
|
|
string id_path = Path.Combine (LocalPath, ".sparkleshare");
|
2012-06-10 10:57:31 +00:00
|
|
|
|
2012-05-19 15:17:09 +00:00
|
|
|
if (File.Exists (id_path))
|
|
|
|
this.identifier = File.ReadAllText (id_path).Trim ();
|
2012-05-19 15:15:20 +00:00
|
|
|
|
2012-07-05 11:33:32 +00:00
|
|
|
if (!string.IsNullOrEmpty (this.identifier)) {
|
2012-05-19 15:15:20 +00:00
|
|
|
return this.identifier;
|
|
|
|
|
|
|
|
} else {
|
2012-07-14 10:45:54 +00:00
|
|
|
string config_identifier = this.local_config.GetIdentifierForFolder (Name);
|
2012-07-05 11:33:32 +00:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty (config_identifier))
|
|
|
|
this.identifier = config_identifier;
|
|
|
|
else
|
|
|
|
this.identifier = SparkleFetcherBase.CreateIdentifier ();
|
2012-06-10 22:56:39 +00:00
|
|
|
|
2012-05-19 15:17:09 +00:00
|
|
|
File.WriteAllText (id_path, this.identifier);
|
|
|
|
File.SetAttributes (id_path, FileAttributes.Hidden);
|
2012-05-19 15:15:20 +00:00
|
|
|
|
2012-07-28 13:58:09 +00:00
|
|
|
SparkleLogger.LogInfo ("Local", Name + " | Assigned identifier: " + this.identifier);
|
2012-06-10 22:56:39 +00:00
|
|
|
|
2012-05-19 15:17:09 +00:00
|
|
|
return this.identifier;
|
2012-05-19 15:15:20 +00:00
|
|
|
}
|
2012-05-19 15:17:09 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-08 19:42:29 +00:00
|
|
|
|
2012-06-10 10:57:31 +00:00
|
|
|
public virtual string [] UnsyncedFilePaths {
|
|
|
|
get {
|
|
|
|
return new string [0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-14 15:18:29 +00:00
|
|
|
protected SparkleConfig local_config;
|
2012-06-10 10:57:31 +00:00
|
|
|
|
2012-07-14 15:18:29 +00:00
|
|
|
|
|
|
|
private string identifier;
|
2012-06-10 10:57:31 +00:00
|
|
|
private SparkleListenerBase listener;
|
2012-07-25 09:25:52 +00:00
|
|
|
private SparkleWatcher watcher;
|
2012-07-17 13:09:57 +00:00
|
|
|
private TimeSpan poll_interval = PollInterval.Short;
|
|
|
|
private DateTime last_poll = DateTime.Now;
|
|
|
|
private DateTime progress_last_change = DateTime.Now;
|
|
|
|
private TimeSpan progress_change_interval = new TimeSpan (0, 0, 0, 1);
|
2012-08-20 09:56:21 +00:00
|
|
|
private Timers.Timer remote_timer = new Timers.Timer () { Interval = 5000 };
|
2012-06-10 10:57:31 +00:00
|
|
|
|
|
|
|
private bool is_syncing {
|
|
|
|
get {
|
|
|
|
return (Status == SyncStatus.SyncUp || Status == SyncStatus.SyncDown || IsBuffering);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-10 22:56:39 +00:00
|
|
|
private static class PollInterval {
|
2012-07-14 15:18:29 +00:00
|
|
|
public static readonly TimeSpan Short = new TimeSpan (0, 0, 5, 0);
|
|
|
|
public static readonly TimeSpan Long = new TimeSpan (0, 0, 15, 0);
|
2012-06-10 22:56:39 +00:00
|
|
|
}
|
|
|
|
|
2012-02-08 19:42:29 +00:00
|
|
|
|
2012-07-14 10:45:54 +00:00
|
|
|
public SparkleRepoBase (string path, SparkleConfig config)
|
2011-05-19 16:05:58 +00:00
|
|
|
{
|
2012-09-18 18:40:06 +00:00
|
|
|
Status = SyncStatus.Idle;
|
|
|
|
Error = ErrorStatus.None;
|
2012-07-14 10:45:54 +00:00
|
|
|
this.local_config = config;
|
2012-07-17 13:09:57 +00:00
|
|
|
LocalPath = path;
|
|
|
|
Name = Path.GetFileName (LocalPath);
|
|
|
|
RemoteUrl = new Uri (this.local_config.GetUrlForFolder (Name));
|
|
|
|
IsBuffering = false;
|
|
|
|
this.identifier = Identifier;
|
|
|
|
ChangeSets = GetChangeSets ();
|
2011-05-19 16:05:58 +00:00
|
|
|
|
2012-10-21 20:16:38 +00:00
|
|
|
string identifier_file_path = Path.Combine (LocalPath, ".sparkleshare");
|
|
|
|
File.SetAttributes (identifier_file_path, FileAttributes.Hidden);
|
|
|
|
|
2011-05-19 16:05:58 +00:00
|
|
|
SyncStatusChanged += delegate (SyncStatus status) {
|
2012-06-10 10:57:31 +00:00
|
|
|
Status = status;
|
2011-05-19 16:05:58 +00:00
|
|
|
};
|
|
|
|
|
2012-08-19 10:43:41 +00:00
|
|
|
if (!UseCustomWatcher)
|
|
|
|
this.watcher = new SparkleWatcher (LocalPath);
|
|
|
|
|
2012-07-15 13:59:04 +00:00
|
|
|
new Thread (() => CreateListener ()).Start ();
|
2011-06-28 19:06:04 +00:00
|
|
|
|
2011-05-19 16:05:58 +00:00
|
|
|
this.remote_timer.Elapsed += delegate {
|
2012-07-19 20:05:29 +00:00
|
|
|
if (this.is_syncing || IsBuffering)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int time_comparison = DateTime.Compare (this.last_poll, DateTime.Now.Subtract (this.poll_interval));
|
|
|
|
bool time_to_poll = (time_comparison < 0);
|
2011-06-09 20:20:57 +00:00
|
|
|
|
2012-06-10 22:56:39 +00:00
|
|
|
if (time_to_poll && !is_syncing) {
|
2011-06-09 20:20:57 +00:00
|
|
|
this.last_poll = DateTime.Now;
|
|
|
|
|
2012-09-29 10:15:23 +00:00
|
|
|
if (HasRemoteChanges) {
|
2012-09-29 10:35:36 +00:00
|
|
|
this.poll_interval = PollInterval.Long;
|
2011-05-19 16:05:58 +00:00
|
|
|
SyncDownBase ();
|
2012-09-29 10:15:23 +00:00
|
|
|
}
|
2011-05-19 16:05:58 +00:00
|
|
|
}
|
|
|
|
|
2011-05-22 17:37:36 +00:00
|
|
|
// In the unlikely case that we haven't synced up our
|
|
|
|
// changes or the server was down, sync up again
|
2012-09-18 18:40:06 +00:00
|
|
|
if (HasUnsyncedChanges && !is_syncing && Error == ErrorStatus.None)
|
2011-05-19 16:05:58 +00:00
|
|
|
SyncUpBase ();
|
|
|
|
};
|
2012-01-18 16:59:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Initialize ()
|
|
|
|
{
|
2012-08-19 10:43:41 +00:00
|
|
|
if (!UseCustomWatcher)
|
|
|
|
this.watcher.ChangeEvent += OnFileActivity;
|
2012-07-25 09:25:52 +00:00
|
|
|
|
2011-05-19 16:05:58 +00:00
|
|
|
// Sync up everything that changed
|
2011-05-20 00:55:49 +00:00
|
|
|
// since we've been offline
|
2012-10-16 11:21:44 +00:00
|
|
|
new Thread (() => {
|
|
|
|
if (!this.is_syncing && (HasLocalChanges || HasUnsyncedChanges)) {
|
2011-05-19 16:05:58 +00:00
|
|
|
SyncUpBase ();
|
2011-06-28 19:54:47 +00:00
|
|
|
|
2012-10-16 11:21:44 +00:00
|
|
|
while (HasLocalChanges && !this.is_syncing)
|
|
|
|
SyncUpBase ();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.remote_timer.Start ();
|
|
|
|
|
|
|
|
}).Start ();
|
2011-05-20 00:55:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-22 22:09:36 +00:00
|
|
|
private Object buffer_lock = new Object ();
|
|
|
|
|
2011-06-14 22:14:03 +00:00
|
|
|
public void OnFileActivity (FileSystemEventArgs args)
|
2011-05-19 16:05:58 +00:00
|
|
|
{
|
2012-07-14 12:35:55 +00:00
|
|
|
if (IsBuffering)
|
|
|
|
return;
|
2012-06-28 13:53:30 +00:00
|
|
|
|
2012-10-14 18:57:13 +00:00
|
|
|
if (args != null) {
|
|
|
|
foreach (string exclude_path in ExcludePaths) {
|
|
|
|
if (args.FullPath.Contains (exclude_path))
|
|
|
|
return;
|
|
|
|
}
|
2012-07-14 12:35:55 +00:00
|
|
|
}
|
2011-05-19 16:05:58 +00:00
|
|
|
|
2012-09-22 22:09:36 +00:00
|
|
|
lock (this.buffer_lock) {
|
|
|
|
if (IsBuffering || !HasLocalChanges)
|
|
|
|
return;
|
|
|
|
|
|
|
|
IsBuffering = true;
|
|
|
|
}
|
2011-05-19 16:05:58 +00:00
|
|
|
|
2012-09-22 22:09:36 +00:00
|
|
|
ChangesDetected ();
|
2012-08-19 10:43:41 +00:00
|
|
|
|
|
|
|
if (!UseCustomWatcher)
|
|
|
|
this.watcher.Disable ();
|
2012-07-25 09:25:52 +00:00
|
|
|
|
2012-07-28 13:58:09 +00:00
|
|
|
SparkleLogger.LogInfo ("Local", Name + " | Activity detected, waiting for it to settle...");
|
2011-07-20 23:02:21 +00:00
|
|
|
|
2012-07-14 12:35:55 +00:00
|
|
|
List<double> size_buffer = new List<double> ();
|
2011-05-19 16:05:58 +00:00
|
|
|
|
2012-07-14 12:35:55 +00:00
|
|
|
do {
|
|
|
|
if (size_buffer.Count >= 4)
|
|
|
|
size_buffer.RemoveAt (0);
|
2012-06-10 00:16:28 +00:00
|
|
|
|
2012-07-14 12:35:55 +00:00
|
|
|
DirectoryInfo info = new DirectoryInfo (LocalPath);
|
|
|
|
size_buffer.Add (CalculateSize (info));
|
2012-06-10 00:16:28 +00:00
|
|
|
|
2012-07-14 12:35:55 +00:00
|
|
|
if (size_buffer.Count >= 4 &&
|
|
|
|
size_buffer [0].Equals (size_buffer [1]) &&
|
|
|
|
size_buffer [1].Equals (size_buffer [2]) &&
|
|
|
|
size_buffer [2].Equals (size_buffer [3])) {
|
2012-06-10 00:16:28 +00:00
|
|
|
|
2012-07-28 13:58:09 +00:00
|
|
|
SparkleLogger.LogInfo ("Local", Name + " | Activity has settled");
|
2012-07-14 12:35:55 +00:00
|
|
|
IsBuffering = false;
|
2012-07-13 12:19:46 +00:00
|
|
|
|
2012-07-14 12:35:55 +00:00
|
|
|
if (HasLocalChanges) {
|
|
|
|
do {
|
|
|
|
SyncUpBase ();
|
2012-07-13 12:19:46 +00:00
|
|
|
|
2012-07-14 12:35:55 +00:00
|
|
|
} while (HasLocalChanges);
|
2012-06-10 00:16:28 +00:00
|
|
|
|
2012-07-14 12:35:55 +00:00
|
|
|
} else {
|
2012-07-18 10:35:22 +00:00
|
|
|
SyncStatusChanged (SyncStatus.Idle);
|
2012-07-14 12:35:55 +00:00
|
|
|
}
|
2012-06-10 00:16:28 +00:00
|
|
|
|
2012-07-14 12:35:55 +00:00
|
|
|
} else {
|
|
|
|
Thread.Sleep (500);
|
2011-05-19 16:05:58 +00:00
|
|
|
}
|
2012-06-28 13:53:30 +00:00
|
|
|
|
2012-07-14 12:35:55 +00:00
|
|
|
} while (IsBuffering);
|
2012-07-25 09:25:52 +00:00
|
|
|
|
2012-08-19 10:43:41 +00:00
|
|
|
if (!UseCustomWatcher)
|
|
|
|
this.watcher.Enable ();
|
2011-05-19 16:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-10 10:57:31 +00:00
|
|
|
protected void OnConflictResolved ()
|
|
|
|
{
|
2012-07-18 10:35:22 +00:00
|
|
|
ConflictResolved ();
|
2012-06-10 10:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-17 13:09:57 +00:00
|
|
|
protected void OnProgressChanged (double progress_percentage, string progress_speed)
|
|
|
|
{
|
2012-09-19 18:57:57 +00:00
|
|
|
if (progress_percentage < 1)
|
|
|
|
return;
|
|
|
|
|
2012-07-17 13:09:57 +00:00
|
|
|
// Only trigger the ProgressChanged event once per second
|
|
|
|
if (DateTime.Compare (this.progress_last_change, DateTime.Now.Subtract (this.progress_change_interval)) >= 0)
|
|
|
|
return;
|
|
|
|
|
2012-07-18 10:35:22 +00:00
|
|
|
if (progress_percentage == 100.0)
|
|
|
|
progress_percentage = 99.0;
|
2012-07-17 13:09:57 +00:00
|
|
|
|
2012-07-18 10:35:22 +00:00
|
|
|
ProgressPercentage = progress_percentage;
|
|
|
|
ProgressSpeed = progress_speed;
|
|
|
|
this.progress_last_change = DateTime.Now;
|
2012-07-17 13:09:57 +00:00
|
|
|
|
2012-07-18 10:35:22 +00:00
|
|
|
ProgressChanged (progress_percentage, progress_speed);
|
2012-07-17 13:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-20 00:55:49 +00:00
|
|
|
private void SyncUpBase ()
|
2011-05-19 16:05:58 +00:00
|
|
|
{
|
2012-08-19 10:43:41 +00:00
|
|
|
if (!UseCustomWatcher)
|
|
|
|
this.watcher.Disable ();
|
2012-07-25 09:25:52 +00:00
|
|
|
|
2012-07-28 13:58:09 +00:00
|
|
|
SparkleLogger.LogInfo ("SyncUp", Name + " | Initiated");
|
2012-07-14 15:18:29 +00:00
|
|
|
HasUnsyncedChanges = true;
|
|
|
|
|
2012-07-18 10:35:22 +00:00
|
|
|
SyncStatusChanged (SyncStatus.SyncUp);
|
2012-07-14 15:18:29 +00:00
|
|
|
|
|
|
|
if (SyncUp ()) {
|
2012-07-28 13:58:09 +00:00
|
|
|
SparkleLogger.LogInfo ("SyncUp", Name + " | Done");
|
2012-07-19 20:21:38 +00:00
|
|
|
ChangeSets = GetChangeSets ();
|
|
|
|
|
2012-07-25 09:25:52 +00:00
|
|
|
HasUnsyncedChanges = false;
|
2012-09-29 10:35:36 +00:00
|
|
|
this.poll_interval = PollInterval.Long;
|
2012-07-25 09:25:52 +00:00
|
|
|
|
2012-07-19 20:21:38 +00:00
|
|
|
SyncStatusChanged (SyncStatus.Idle);
|
2012-07-14 15:18:29 +00:00
|
|
|
this.listener.Announce (new SparkleAnnouncement (Identifier, CurrentRevision));
|
2011-05-19 16:05:58 +00:00
|
|
|
|
2012-07-14 15:18:29 +00:00
|
|
|
} else {
|
2012-07-28 13:58:09 +00:00
|
|
|
SparkleLogger.LogInfo ("SyncUp", Name + " | Error");
|
2012-07-14 15:18:29 +00:00
|
|
|
SyncDownBase ();
|
2011-05-19 16:05:58 +00:00
|
|
|
|
2012-08-19 10:43:41 +00:00
|
|
|
if (!UseCustomWatcher)
|
|
|
|
this.watcher.Disable ();
|
2012-07-25 09:25:52 +00:00
|
|
|
|
2012-09-18 18:40:06 +00:00
|
|
|
if (Error == ErrorStatus.None && SyncUp ()) {
|
2011-05-19 16:05:58 +00:00
|
|
|
HasUnsyncedChanges = false;
|
|
|
|
|
2012-07-25 09:25:52 +00:00
|
|
|
SyncStatusChanged (SyncStatus.Idle);
|
2012-02-11 19:23:38 +00:00
|
|
|
this.listener.Announce (new SparkleAnnouncement (Identifier, CurrentRevision));
|
2011-05-22 16:51:48 +00:00
|
|
|
|
2011-05-19 16:05:58 +00:00
|
|
|
} else {
|
2012-09-29 10:35:36 +00:00
|
|
|
this.poll_interval = PollInterval.Short;
|
2012-07-18 10:35:22 +00:00
|
|
|
SyncStatusChanged (SyncStatus.Error);
|
2011-05-19 16:05:58 +00:00
|
|
|
}
|
2012-07-14 15:18:29 +00:00
|
|
|
}
|
2011-05-22 16:51:48 +00:00
|
|
|
|
2012-07-14 15:18:29 +00:00
|
|
|
ProgressPercentage = 0.0;
|
|
|
|
ProgressSpeed = "";
|
2012-07-25 09:25:52 +00:00
|
|
|
|
2012-08-19 10:43:41 +00:00
|
|
|
if (!UseCustomWatcher)
|
|
|
|
this.watcher.Enable ();
|
2011-05-19 16:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void SyncDownBase ()
|
|
|
|
{
|
2012-08-19 10:43:41 +00:00
|
|
|
if (!UseCustomWatcher)
|
|
|
|
this.watcher.Disable ();
|
2012-07-25 09:25:52 +00:00
|
|
|
|
2012-07-28 13:58:09 +00:00
|
|
|
SparkleLogger.LogInfo ("SyncDown", Name + " | Initiated");
|
2011-05-19 16:05:58 +00:00
|
|
|
|
2012-07-18 10:35:22 +00:00
|
|
|
SyncStatusChanged (SyncStatus.SyncDown);
|
2011-11-07 15:39:33 +00:00
|
|
|
string pre_sync_revision = CurrentRevision;
|
2011-11-12 17:47:39 +00:00
|
|
|
|
2011-05-19 16:05:58 +00:00
|
|
|
if (SyncDown ()) {
|
2012-07-28 13:58:09 +00:00
|
|
|
SparkleLogger.LogInfo ("SyncDown", Name + " | Done");
|
2012-09-18 18:40:06 +00:00
|
|
|
Error = ErrorStatus.None;
|
2011-05-19 16:05:58 +00:00
|
|
|
|
2012-10-21 20:16:38 +00:00
|
|
|
string identifier_file_path = Path.Combine (LocalPath, ".sparkleshare");
|
|
|
|
File.SetAttributes (identifier_file_path, FileAttributes.Hidden);
|
|
|
|
|
2012-07-19 20:21:38 +00:00
|
|
|
ChangeSets = GetChangeSets ();
|
2012-05-27 18:24:12 +00:00
|
|
|
|
2012-07-19 20:21:38 +00:00
|
|
|
if (!pre_sync_revision.Equals (CurrentRevision) && ChangeSets != null && ChangeSets.Count > 0) {
|
|
|
|
bool emit_change_event = true;
|
|
|
|
|
|
|
|
foreach (SparkleChange change in ChangeSets [0].Changes) {
|
|
|
|
if (change.Path.EndsWith (".sparkleshare")) {
|
|
|
|
emit_change_event = false;
|
|
|
|
break;
|
2012-07-18 10:35:22 +00:00
|
|
|
}
|
2011-08-26 20:00:33 +00:00
|
|
|
}
|
2012-07-19 20:21:38 +00:00
|
|
|
|
|
|
|
if (emit_change_event)
|
|
|
|
NewChangeSet (ChangeSets [0]);
|
2011-07-23 21:23:14 +00:00
|
|
|
}
|
2011-06-03 00:33:41 +00:00
|
|
|
|
2011-07-24 18:22:17 +00:00
|
|
|
// There could be changes from a resolved
|
|
|
|
// conflict. Tries only once, then lets
|
|
|
|
// the timer try again periodically
|
2012-03-16 01:53:23 +00:00
|
|
|
if (HasUnsyncedChanges) {
|
2012-07-18 10:35:22 +00:00
|
|
|
SyncStatusChanged (SyncStatus.SyncUp);
|
2012-05-19 15:17:09 +00:00
|
|
|
|
2011-05-22 14:46:15 +00:00
|
|
|
SyncUp ();
|
2012-05-19 15:17:09 +00:00
|
|
|
HasUnsyncedChanges = false;
|
|
|
|
}
|
2012-07-18 10:35:22 +00:00
|
|
|
|
|
|
|
SyncStatusChanged (SyncStatus.Idle);
|
2011-05-22 17:49:04 +00:00
|
|
|
|
2011-05-19 16:05:58 +00:00
|
|
|
} else {
|
2012-07-28 13:58:09 +00:00
|
|
|
SparkleLogger.LogInfo ("SyncDown", Name + " | Error");
|
2011-05-19 16:05:58 +00:00
|
|
|
|
2012-07-19 20:21:38 +00:00
|
|
|
ChangeSets = GetChangeSets ();
|
2012-07-18 10:35:22 +00:00
|
|
|
SyncStatusChanged (SyncStatus.Error);
|
2011-05-19 16:05:58 +00:00
|
|
|
}
|
|
|
|
|
2012-06-10 10:57:31 +00:00
|
|
|
ProgressPercentage = 0.0;
|
|
|
|
ProgressSpeed = "";
|
2012-03-29 00:44:26 +00:00
|
|
|
|
2012-08-19 10:43:41 +00:00
|
|
|
if (!UseCustomWatcher)
|
|
|
|
this.watcher.Enable ();
|
2012-07-25 09:25:52 +00:00
|
|
|
|
2012-07-18 10:35:22 +00:00
|
|
|
SyncStatusChanged (SyncStatus.Idle);
|
2012-06-10 10:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-08 19:42:29 +00:00
|
|
|
private void CreateListener ()
|
2011-05-19 16:05:58 +00:00
|
|
|
{
|
2012-02-08 19:42:29 +00:00
|
|
|
this.listener = SparkleListenerFactory.CreateListener (Name, Identifier);
|
|
|
|
|
|
|
|
if (this.listener.IsConnected) {
|
2012-06-10 10:57:31 +00:00
|
|
|
this.poll_interval = PollInterval.Long;
|
2012-02-08 19:42:29 +00:00
|
|
|
|
2012-07-15 13:59:04 +00:00
|
|
|
new Thread (() => {
|
2012-09-22 21:43:40 +00:00
|
|
|
if (!this.is_syncing && !HasLocalChanges && HasRemoteChanges)
|
2012-07-15 13:59:04 +00:00
|
|
|
SyncDownBase ();
|
|
|
|
|
|
|
|
}).Start ();
|
2011-11-13 18:04:13 +00:00
|
|
|
}
|
2011-05-19 16:05:58 +00:00
|
|
|
|
2012-07-05 11:33:32 +00:00
|
|
|
this.listener.Connected += ListenerConnectedDelegate;
|
|
|
|
this.listener.Disconnected += ListenerDisconnectedDelegate;
|
|
|
|
this.listener.AnnouncementReceived += ListenerAnnouncementReceivedDelegate;
|
2011-05-19 16:05:58 +00:00
|
|
|
|
2012-07-05 11:33:32 +00:00
|
|
|
// Start listening
|
|
|
|
if (!this.listener.IsConnected && !this.listener.IsConnecting)
|
|
|
|
this.listener.Connect ();
|
|
|
|
}
|
2012-01-12 00:22:15 +00:00
|
|
|
|
2012-02-08 19:42:29 +00:00
|
|
|
|
2012-07-05 11:33:32 +00:00
|
|
|
// Stop polling when the connection to the irc channel is succesful
|
|
|
|
private void ListenerConnectedDelegate ()
|
|
|
|
{
|
|
|
|
this.poll_interval = PollInterval.Long;
|
|
|
|
this.last_poll = DateTime.Now;
|
|
|
|
|
2012-09-22 21:43:40 +00:00
|
|
|
if (!this.is_syncing) {
|
2012-07-05 11:33:32 +00:00
|
|
|
// Check for changes manually one more time
|
|
|
|
if (HasRemoteChanges)
|
|
|
|
SyncDownBase ();
|
|
|
|
|
|
|
|
// Push changes that were made since the last disconnect
|
|
|
|
if (HasUnsyncedChanges)
|
|
|
|
SyncUpBase ();
|
|
|
|
}
|
|
|
|
}
|
2012-02-08 19:42:29 +00:00
|
|
|
|
|
|
|
|
2012-07-05 11:33:32 +00:00
|
|
|
// Start polling when the connection to the channel is lost
|
|
|
|
private void ListenerDisconnectedDelegate ()
|
|
|
|
{
|
|
|
|
this.poll_interval = PollInterval.Short;
|
2012-07-28 13:58:09 +00:00
|
|
|
SparkleLogger.LogInfo (Name, "Falling back to polling");
|
2012-07-05 11:33:32 +00:00
|
|
|
}
|
2012-02-08 19:42:29 +00:00
|
|
|
|
|
|
|
|
2012-07-05 11:33:32 +00:00
|
|
|
// Fetch changes when there is an announcement
|
|
|
|
private void ListenerAnnouncementReceivedDelegate (SparkleAnnouncement announcement)
|
|
|
|
{
|
|
|
|
string identifier = Identifier;
|
2012-02-08 19:42:29 +00:00
|
|
|
|
2012-07-05 11:33:32 +00:00
|
|
|
if (announcement.FolderIdentifier.Equals (identifier) &&
|
|
|
|
!announcement.Message.Equals (CurrentRevision)) {
|
2012-02-08 19:42:29 +00:00
|
|
|
|
2012-07-05 11:33:32 +00:00
|
|
|
while (this.is_syncing)
|
2012-07-08 21:53:44 +00:00
|
|
|
Thread.Sleep (100);
|
2012-07-05 11:33:32 +00:00
|
|
|
|
2012-09-08 09:48:01 +00:00
|
|
|
SparkleLogger.LogInfo (Name, "Syncing due to announcement");
|
2012-07-05 11:33:32 +00:00
|
|
|
SyncDownBase ();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (announcement.FolderIdentifier.Equals (identifier))
|
2012-09-08 09:48:01 +00:00
|
|
|
SparkleLogger.LogInfo (Name, "Not syncing, message is for current revision");
|
2012-07-05 11:33:32 +00:00
|
|
|
}
|
2011-05-28 23:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-08 19:42:29 +00:00
|
|
|
// Recursively gets a folder's size in bytes
|
|
|
|
private double CalculateSize (DirectoryInfo parent)
|
|
|
|
{
|
|
|
|
if (!Directory.Exists (parent.ToString ()))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
double size = 0;
|
|
|
|
|
|
|
|
if (ExcludePaths.Contains (parent.Name))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
try {
|
2012-05-20 13:28:14 +00:00
|
|
|
foreach (FileInfo file in parent.GetFiles ()) {
|
2012-02-08 19:42:29 +00:00
|
|
|
if (!file.Exists)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
size += file.Length;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (DirectoryInfo directory in parent.GetDirectories ())
|
|
|
|
size += CalculateSize (directory);
|
|
|
|
|
|
|
|
} catch (Exception) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
2012-06-10 10:57:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
public void Dispose ()
|
|
|
|
{
|
2012-07-05 11:33:32 +00:00
|
|
|
this.remote_timer.Stop ();
|
2012-06-10 10:57:31 +00:00
|
|
|
this.remote_timer.Dispose ();
|
2012-07-05 11:33:32 +00:00
|
|
|
|
|
|
|
this.listener.Connected -= ListenerConnectedDelegate;
|
|
|
|
this.listener.Disconnected -= ListenerDisconnectedDelegate;
|
|
|
|
this.listener.AnnouncementReceived -= ListenerAnnouncementReceivedDelegate;
|
2012-07-15 10:05:58 +00:00
|
|
|
|
2012-08-19 10:43:41 +00:00
|
|
|
if (!UseCustomWatcher)
|
|
|
|
this.watcher.Dispose ();
|
2012-06-10 10:57:31 +00:00
|
|
|
}
|
2011-05-19 16:05:58 +00:00
|
|
|
}
|
|
|
|
}
|