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-07-16 22:15:56 +00:00
|
|
|
using System.Security.Cryptography;
|
|
|
|
using System.Text;
|
2011-05-19 16:05:58 +00:00
|
|
|
using System.Text.RegularExpressions;
|
2011-11-13 00:57:00 +00:00
|
|
|
using System.Threading;
|
2011-05-19 16:05:58 +00:00
|
|
|
using System.Timers;
|
|
|
|
|
|
|
|
namespace SparkleLib {
|
|
|
|
|
|
|
|
public enum SyncStatus {
|
|
|
|
Idle,
|
|
|
|
SyncUp,
|
|
|
|
SyncDown,
|
|
|
|
Error
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public abstract class SparkleRepoBase {
|
2012-05-19 15:17:09 +00:00
|
|
|
|
|
|
|
private string identifier;
|
2012-05-20 13:28:14 +00:00
|
|
|
private TimeSpan short_interval = new TimeSpan (0, 0, 5, 0);
|
|
|
|
private TimeSpan long_interval = new TimeSpan (0, 0, 15, 0);
|
2012-02-09 01:46:25 +00:00
|
|
|
private TimeSpan poll_interval;
|
2011-06-14 22:14:03 +00:00
|
|
|
private SparkleWatcher watcher;
|
2012-02-06 13:58:43 +00:00
|
|
|
private SparkleListenerBase listener;
|
2011-11-13 18:04:13 +00:00
|
|
|
private System.Timers.Timer local_timer = new System.Timers.Timer () { Interval = 0.25 * 1000 };
|
2012-05-20 13:28:14 +00:00
|
|
|
private System.Timers.Timer remote_timer = new System.Timers.Timer () { Interval = 5 * 1000 };
|
2012-02-06 13:58:43 +00:00
|
|
|
private DateTime last_poll = DateTime.Now;
|
|
|
|
private List<double> size_buffer = new List<double> ();
|
|
|
|
private Object change_lock = new Object ();
|
|
|
|
private Object watch_lock = new Object ();
|
|
|
|
private double progress_percentage = 0.0;
|
|
|
|
private string progress_speed = "";
|
2012-02-09 01:46:25 +00:00
|
|
|
private bool has_changed = false;
|
|
|
|
private bool is_buffering = false;
|
|
|
|
private bool server_online = true;
|
2012-02-06 13:58:43 +00:00
|
|
|
private SyncStatus status;
|
2011-05-19 16:05:58 +00:00
|
|
|
|
2011-12-15 15:15:29 +00:00
|
|
|
|
2011-05-19 16:05:58 +00:00
|
|
|
public delegate void SyncStatusChangedEventHandler (SyncStatus new_status);
|
|
|
|
public event SyncStatusChangedEventHandler SyncStatusChanged;
|
|
|
|
|
2012-02-09 01:46:25 +00:00
|
|
|
public delegate void ProgressChangedEventHandler (double percentage, string speed);
|
2012-02-09 11:54:18 +00:00
|
|
|
public event ProgressChangedEventHandler ProgressChanged;
|
2011-12-30 00:44:35 +00:00
|
|
|
|
2011-07-23 21:23:14 +00:00
|
|
|
public delegate void NewChangeSetEventHandler (SparkleChangeSet change_set);
|
2011-05-19 16:05:58 +00:00
|
|
|
public event NewChangeSetEventHandler NewChangeSet;
|
2011-05-22 14:46:15 +00:00
|
|
|
|
|
|
|
public delegate void ConflictResolvedEventHandler ();
|
2011-05-19 16:05:58 +00:00
|
|
|
public event ConflictResolvedEventHandler ConflictResolved;
|
2011-05-22 14:46:15 +00:00
|
|
|
|
|
|
|
public delegate void ChangesDetectedEventHandler ();
|
2011-05-19 16:05:58 +00:00
|
|
|
public event ChangesDetectedEventHandler ChangesDetected;
|
|
|
|
|
|
|
|
|
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-02-09 01:46:25 +00:00
|
|
|
|
2012-03-25 23:41:52 +00:00
|
|
|
public abstract string ComputeIdentifier ();
|
2012-02-09 01:46:25 +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 ();
|
|
|
|
public abstract List<SparkleChangeSet> GetChangeSets (int count);
|
|
|
|
|
2012-04-29 14:19:36 +00:00
|
|
|
public List<SparkleChangeSet> GetChangeSets () {
|
|
|
|
return GetChangeSets (30);
|
|
|
|
}
|
2012-02-09 01:46:25 +00:00
|
|
|
|
2012-02-08 19:42:29 +00:00
|
|
|
public bool ServerOnline {
|
|
|
|
get {
|
|
|
|
return this.server_online;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public SyncStatus Status {
|
|
|
|
get {
|
|
|
|
return this.status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public double ProgressPercentage {
|
|
|
|
get {
|
|
|
|
return this.progress_percentage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string ProgressSpeed {
|
|
|
|
get {
|
|
|
|
return this.progress_speed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual string [] UnsyncedFilePaths {
|
|
|
|
get {
|
|
|
|
return new string [0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsSyncing {
|
|
|
|
get {
|
|
|
|
return (Status == SyncStatus.SyncUp ||
|
|
|
|
Status == SyncStatus.SyncDown ||
|
|
|
|
this.is_buffering);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsBuffering {
|
|
|
|
get {
|
|
|
|
return this.is_buffering;
|
|
|
|
}
|
|
|
|
}
|
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");
|
|
|
|
|
|
|
|
if (File.Exists (id_path))
|
|
|
|
this.identifier = File.ReadAllText (id_path).Trim ();
|
2012-05-19 15:15:20 +00:00
|
|
|
|
|
|
|
if (this.identifier != null && this.identifier.Length > 0) {
|
|
|
|
return this.identifier;
|
|
|
|
|
|
|
|
} else {
|
2012-05-19 15:17:09 +00:00
|
|
|
this.identifier = ComputeIdentifier ();
|
|
|
|
File.WriteAllText (id_path, this.identifier);
|
|
|
|
File.SetAttributes (id_path, FileAttributes.Hidden);
|
2012-05-19 15:15:20 +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-01-29 20:33:12 +00:00
|
|
|
public SparkleRepoBase (string path)
|
2011-05-19 16:05:58 +00:00
|
|
|
{
|
2012-01-25 18:30:29 +00:00
|
|
|
LocalPath = path;
|
|
|
|
Name = Path.GetFileName (LocalPath);
|
2012-04-29 14:19:36 +00:00
|
|
|
RemoteUrl = new Uri (SparkleConfig.DefaultConfig.GetUrlForFolder (Name));
|
2012-01-25 18:30:29 +00:00
|
|
|
|
2011-06-12 01:56:01 +00:00
|
|
|
this.poll_interval = this.short_interval;
|
2011-05-19 16:05:58 +00:00
|
|
|
|
|
|
|
SyncStatusChanged += delegate (SyncStatus status) {
|
|
|
|
this.status = status;
|
|
|
|
};
|
|
|
|
|
2012-04-22 16:13:40 +00:00
|
|
|
this.identifier = Identifier;
|
|
|
|
|
2011-06-23 12:56:25 +00:00
|
|
|
if (CurrentRevision == null)
|
2011-05-19 16:05:58 +00:00
|
|
|
CreateInitialChangeSet ();
|
2012-04-29 14:19:36 +00:00
|
|
|
|
|
|
|
ChangeSets = GetChangeSets ();
|
2011-06-28 19:06:04 +00:00
|
|
|
CreateWatcher ();
|
2012-05-14 22:35:59 +00:00
|
|
|
|
|
|
|
new Thread (
|
|
|
|
new ThreadStart (delegate {
|
|
|
|
CreateListener ();
|
|
|
|
})
|
|
|
|
).Start ();
|
2011-06-28 19:06:04 +00:00
|
|
|
|
2011-05-19 16:05:58 +00:00
|
|
|
this.local_timer.Elapsed += delegate (object o, ElapsedEventArgs args) {
|
|
|
|
CheckForChanges ();
|
|
|
|
};
|
|
|
|
|
|
|
|
this.remote_timer.Elapsed += delegate {
|
2011-06-09 20:20:57 +00:00
|
|
|
bool time_to_poll = (DateTime.Compare (this.last_poll,
|
|
|
|
DateTime.Now.Subtract (this.poll_interval)) < 0);
|
|
|
|
|
2011-06-09 22:59:56 +00:00
|
|
|
if (time_to_poll) {
|
2011-06-09 20:20:57 +00:00
|
|
|
this.last_poll = DateTime.Now;
|
|
|
|
|
2012-02-06 14:02:41 +00:00
|
|
|
if (HasRemoteChanges)
|
2011-05-19 16:05:58 +00:00
|
|
|
SyncDownBase ();
|
|
|
|
}
|
|
|
|
|
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
|
2011-12-03 11:40:03 +00:00
|
|
|
if (HasUnsyncedChanges && !IsSyncing && this.server_online)
|
2011-05-19 16:05:58 +00:00
|
|
|
SyncUpBase ();
|
|
|
|
};
|
2012-01-18 16:59:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Initialize ()
|
|
|
|
{
|
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-02-08 19:42:29 +00:00
|
|
|
if (HasLocalChanges) {
|
2011-05-20 00:55:49 +00:00
|
|
|
DisableWatching ();
|
2011-05-19 16:05:58 +00:00
|
|
|
SyncUpBase ();
|
2011-05-20 00:55:49 +00:00
|
|
|
|
2011-05-19 16:05:58 +00:00
|
|
|
while (HasUnsyncedChanges)
|
|
|
|
SyncUpBase ();
|
2011-11-13 00:27:25 +00:00
|
|
|
|
2011-05-20 00:55:49 +00:00
|
|
|
EnableWatching ();
|
|
|
|
}
|
2011-06-28 19:54:47 +00:00
|
|
|
|
|
|
|
this.remote_timer.Start ();
|
|
|
|
this.local_timer.Start ();
|
2011-05-20 00:55:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-22 14:46:15 +00:00
|
|
|
protected void OnConflictResolved ()
|
|
|
|
{
|
|
|
|
if (ConflictResolved != null)
|
|
|
|
ConflictResolved ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-20 00:55:49 +00:00
|
|
|
// Disposes all resourses of this object
|
|
|
|
public void Dispose ()
|
|
|
|
{
|
|
|
|
this.remote_timer.Dispose ();
|
|
|
|
this.local_timer.Dispose ();
|
|
|
|
this.listener.Dispose ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-19 16:05:58 +00:00
|
|
|
// Starts a timer when something changes
|
2011-06-14 22:14:03 +00:00
|
|
|
public void OnFileActivity (FileSystemEventArgs args)
|
2011-05-19 16:05:58 +00:00
|
|
|
{
|
2011-06-22 15:02:03 +00:00
|
|
|
// Check the watcher for the occasions where this
|
|
|
|
// method is called directly
|
|
|
|
if (!this.watcher.EnableRaisingEvents)
|
|
|
|
return;
|
|
|
|
|
2011-12-29 11:44:18 +00:00
|
|
|
string relative_path = args.FullPath.Replace (LocalPath, "");
|
|
|
|
|
|
|
|
foreach (string exclude_path in ExcludePaths) {
|
|
|
|
if (relative_path.Contains (exclude_path))
|
|
|
|
return;
|
|
|
|
}
|
2011-05-19 16:05:58 +00:00
|
|
|
|
2011-05-29 12:53:09 +00:00
|
|
|
WatcherChangeTypes wct = args.ChangeType;
|
2011-05-19 16:05:58 +00:00
|
|
|
|
2012-02-08 19:42:29 +00:00
|
|
|
if (HasLocalChanges) {
|
2011-05-19 16:05:58 +00:00
|
|
|
this.is_buffering = true;
|
|
|
|
|
2011-07-20 23:02:21 +00:00
|
|
|
// We want to disable wathcing temporarily, but
|
|
|
|
// not stop the local timer
|
|
|
|
this.watcher.EnableRaisingEvents = false;
|
|
|
|
|
2011-05-19 16:05:58 +00:00
|
|
|
// Only fire the event if the timer has been stopped.
|
|
|
|
// This prevents multiple events from being raised whilst "buffering".
|
|
|
|
if (!this.has_changed) {
|
|
|
|
if (ChangesDetected != null)
|
|
|
|
ChangesDetected ();
|
|
|
|
}
|
|
|
|
|
2011-05-29 12:53:09 +00:00
|
|
|
SparkleHelpers.DebugInfo ("Event", "[" + Name + "] " + wct.ToString () + " '" + args.Name + "'");
|
|
|
|
SparkleHelpers.DebugInfo ("Event", "[" + Name + "] Changes found, checking if settled.");
|
2011-07-20 23:02:21 +00:00
|
|
|
|
2011-05-19 16:05:58 +00:00
|
|
|
this.remote_timer.Stop ();
|
|
|
|
|
|
|
|
lock (this.change_lock) {
|
|
|
|
this.has_changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-20 00:55:49 +00:00
|
|
|
private void SyncUpBase ()
|
2011-05-19 16:05:58 +00:00
|
|
|
{
|
|
|
|
try {
|
2011-06-14 13:25:04 +00:00
|
|
|
DisableWatching ();
|
2011-05-19 16:05:58 +00:00
|
|
|
this.remote_timer.Stop ();
|
|
|
|
|
|
|
|
SparkleHelpers.DebugInfo ("SyncUp", "[" + Name + "] Initiated");
|
|
|
|
|
2011-05-20 00:55:49 +00:00
|
|
|
if (SyncStatusChanged != null)
|
|
|
|
SyncStatusChanged (SyncStatus.SyncUp);
|
2011-05-19 16:05:58 +00:00
|
|
|
|
|
|
|
if (SyncUp ()) {
|
|
|
|
SparkleHelpers.DebugInfo ("SyncUp", "[" + Name + "] Done");
|
|
|
|
|
|
|
|
HasUnsyncedChanges = false;
|
|
|
|
|
|
|
|
if (SyncStatusChanged != null)
|
|
|
|
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 {
|
|
|
|
SparkleHelpers.DebugInfo ("SyncUp", "[" + Name + "] Error");
|
|
|
|
|
|
|
|
HasUnsyncedChanges = true;
|
|
|
|
SyncDownBase ();
|
2011-12-03 11:40:03 +00:00
|
|
|
DisableWatching ();
|
2011-05-22 17:06:28 +00:00
|
|
|
|
2011-12-03 11:40:03 +00:00
|
|
|
if (this.server_online && SyncUp ()) {
|
2011-05-22 17:06:28 +00:00
|
|
|
HasUnsyncedChanges = false;
|
2011-05-22 17:12:40 +00:00
|
|
|
|
|
|
|
if (SyncStatusChanged != null)
|
|
|
|
SyncStatusChanged (SyncStatus.Idle);
|
|
|
|
|
2012-02-11 19:23:38 +00:00
|
|
|
this.listener.Announce (new SparkleAnnouncement (Identifier, CurrentRevision));
|
2011-05-22 17:12:40 +00:00
|
|
|
|
|
|
|
} else {
|
2011-12-03 11:40:03 +00:00
|
|
|
this.server_online = false;
|
|
|
|
|
2011-05-22 17:12:40 +00:00
|
|
|
if (SyncStatusChanged != null)
|
|
|
|
SyncStatusChanged (SyncStatus.Error);
|
|
|
|
}
|
2011-05-19 16:05:58 +00:00
|
|
|
}
|
2011-05-22 16:51:48 +00:00
|
|
|
|
2011-05-19 16:05:58 +00:00
|
|
|
} finally {
|
|
|
|
this.remote_timer.Start ();
|
2011-06-14 13:25:04 +00:00
|
|
|
EnableWatching ();
|
2011-12-30 00:44:35 +00:00
|
|
|
|
|
|
|
this.progress_percentage = 0.0;
|
|
|
|
this.progress_speed = "";
|
2011-05-19 16:05:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void SyncDownBase ()
|
|
|
|
{
|
|
|
|
SparkleHelpers.DebugInfo ("SyncDown", "[" + Name + "] Initiated");
|
|
|
|
this.remote_timer.Stop ();
|
2011-06-14 13:25:04 +00:00
|
|
|
DisableWatching ();
|
2011-05-19 16:05:58 +00:00
|
|
|
|
|
|
|
if (SyncStatusChanged != null)
|
|
|
|
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 ()) {
|
|
|
|
SparkleHelpers.DebugInfo ("SyncDown", "[" + Name + "] Done");
|
|
|
|
this.server_online = true;
|
|
|
|
|
2011-11-12 17:47:39 +00:00
|
|
|
if (!pre_sync_revision.Equals (CurrentRevision)) {
|
2012-04-29 14:19:36 +00:00
|
|
|
if (ChangeSets != null &&
|
2012-05-27 18:24:12 +00:00
|
|
|
ChangeSets.Count > 0) {
|
|
|
|
|
|
|
|
bool emit_change_event = true;
|
|
|
|
foreach (SparkleChange change in ChangeSets [0].Changes)
|
|
|
|
if (change.Path.EndsWith (".sparkleshare"))
|
|
|
|
emit_change_event = false;
|
2012-05-19 15:17:09 +00:00
|
|
|
|
2012-05-27 18:24:12 +00:00
|
|
|
if (NewChangeSet != null && emit_change_event)
|
2012-04-29 14:19:36 +00:00
|
|
|
NewChangeSet (ChangeSets [0]);
|
2011-08-26 20:00:33 +00:00
|
|
|
}
|
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-05-19 15:17:09 +00:00
|
|
|
if (SyncStatusChanged != null)
|
|
|
|
SyncStatusChanged (SyncStatus.SyncUp);
|
|
|
|
|
2011-05-22 14:46:15 +00:00
|
|
|
SyncUp ();
|
2012-05-19 15:17:09 +00:00
|
|
|
HasUnsyncedChanges = false;
|
|
|
|
}
|
|
|
|
|
2012-03-16 01:53:23 +00:00
|
|
|
if (SyncStatusChanged != null)
|
|
|
|
SyncStatusChanged (SyncStatus.Idle);
|
2011-05-22 17:49:04 +00:00
|
|
|
|
2011-05-19 16:05:58 +00:00
|
|
|
} else {
|
|
|
|
SparkleHelpers.DebugInfo ("SyncDown", "[" + Name + "] Error");
|
|
|
|
this.server_online = false;
|
|
|
|
|
|
|
|
if (SyncStatusChanged != null)
|
|
|
|
SyncStatusChanged (SyncStatus.Error);
|
|
|
|
}
|
|
|
|
|
2012-03-29 00:44:26 +00:00
|
|
|
this.progress_percentage = 0.0;
|
|
|
|
this.progress_speed = "";
|
|
|
|
|
2011-05-19 16:05:58 +00:00
|
|
|
if (SyncStatusChanged != null)
|
|
|
|
SyncStatusChanged (SyncStatus.Idle);
|
|
|
|
|
|
|
|
this.remote_timer.Start ();
|
2011-06-14 13:25:04 +00:00
|
|
|
EnableWatching ();
|
2012-05-19 15:17:09 +00:00
|
|
|
}
|
2011-05-19 16:05:58 +00:00
|
|
|
|
|
|
|
|
2012-02-08 19:42:29 +00:00
|
|
|
private void CreateWatcher ()
|
2011-05-19 16:05:58 +00:00
|
|
|
{
|
2012-02-08 19:42:29 +00:00
|
|
|
this.watcher = new SparkleWatcher (LocalPath);
|
|
|
|
this.watcher.ChangeEvent += delegate (FileSystemEventArgs args) {
|
|
|
|
OnFileActivity (args);
|
|
|
|
};
|
2011-05-19 16:05:58 +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) {
|
|
|
|
this.poll_interval = this.long_interval;
|
|
|
|
|
2012-05-27 23:14:45 +00:00
|
|
|
new Thread (
|
|
|
|
new ThreadStart (delegate {
|
|
|
|
if (!IsSyncing && HasRemoteChanges)
|
|
|
|
SyncDownBase ();
|
|
|
|
})
|
|
|
|
).Start ();
|
2011-11-13 18:04:13 +00:00
|
|
|
}
|
2011-05-19 16:05:58 +00:00
|
|
|
|
2012-02-08 19:42:29 +00:00
|
|
|
// Stop polling when the connection to the irc channel is succesful
|
|
|
|
this.listener.Connected += delegate {
|
|
|
|
this.poll_interval = this.long_interval;
|
|
|
|
this.last_poll = DateTime.Now;
|
2011-05-19 16:05:58 +00:00
|
|
|
|
2012-02-08 19:42:29 +00:00
|
|
|
if (!IsSyncing) {
|
2012-01-12 00:22:15 +00:00
|
|
|
|
2012-02-08 19:42:29 +00:00
|
|
|
// Check for changes manually one more time
|
|
|
|
if (HasRemoteChanges)
|
|
|
|
SyncDownBase ();
|
2012-01-12 00:22:15 +00:00
|
|
|
|
2012-02-08 19:42:29 +00:00
|
|
|
// Push changes that were made since the last disconnect
|
|
|
|
if (HasUnsyncedChanges)
|
|
|
|
SyncUpBase ();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-04-11 18:07:34 +00:00
|
|
|
// Start polling when the connection to the channel is lost
|
2012-02-08 19:42:29 +00:00
|
|
|
this.listener.Disconnected += delegate {
|
|
|
|
this.poll_interval = this.short_interval;
|
|
|
|
SparkleHelpers.DebugInfo (Name, "Falling back to polling");
|
|
|
|
};
|
|
|
|
|
|
|
|
// Fetch changes when there is a message in the irc channel
|
|
|
|
this.listener.Received += delegate (SparkleAnnouncement announcement) {
|
|
|
|
string identifier = Identifier;
|
|
|
|
|
|
|
|
if (announcement.FolderIdentifier.Equals (identifier) &&
|
|
|
|
!announcement.Message.Equals (CurrentRevision)) {
|
|
|
|
|
|
|
|
while (this.IsSyncing)
|
|
|
|
System.Threading.Thread.Sleep (100);
|
|
|
|
|
|
|
|
SparkleHelpers.DebugInfo ("Listener", "Syncing due to announcement");
|
|
|
|
SyncDownBase ();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (announcement.FolderIdentifier.Equals (identifier))
|
|
|
|
SparkleHelpers.DebugInfo ("Listener", "Not syncing, message is for current revision");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Start listening
|
|
|
|
if (!this.listener.IsConnected && !this.listener.IsConnecting)
|
|
|
|
this.listener.Connect ();
|
2011-05-28 23:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-08 19:42:29 +00:00
|
|
|
private void CheckForChanges ()
|
2011-06-20 22:55:18 +00:00
|
|
|
{
|
2012-02-08 19:42:29 +00:00
|
|
|
lock (this.change_lock) {
|
|
|
|
if (this.has_changed) {
|
|
|
|
if (this.size_buffer.Count >= 4)
|
|
|
|
this.size_buffer.RemoveAt (0);
|
2011-06-20 22:55:18 +00:00
|
|
|
|
2012-02-08 19:42:29 +00:00
|
|
|
DirectoryInfo dir_info = new DirectoryInfo (LocalPath);
|
|
|
|
this.size_buffer.Add (CalculateSize (dir_info));
|
2011-06-20 22:55:18 +00:00
|
|
|
|
2012-02-08 19:42:29 +00:00
|
|
|
if (this.size_buffer.Count >= 4 &&
|
|
|
|
this.size_buffer [0].Equals (this.size_buffer [1]) &&
|
|
|
|
this.size_buffer [1].Equals (this.size_buffer [2]) &&
|
|
|
|
this.size_buffer [2].Equals (this.size_buffer [3])) {
|
2011-06-20 22:55:18 +00:00
|
|
|
|
2012-02-08 19:42:29 +00:00
|
|
|
SparkleHelpers.DebugInfo ("Local", "[" + Name + "] Changes have settled.");
|
|
|
|
this.is_buffering = false;
|
|
|
|
this.has_changed = false;
|
2011-07-16 22:15:56 +00:00
|
|
|
|
2012-02-08 19:42:29 +00:00
|
|
|
DisableWatching ();
|
|
|
|
while (HasLocalChanges)
|
|
|
|
SyncUpBase ();
|
|
|
|
EnableWatching ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-22 19:38:14 +00:00
|
|
|
|
2011-07-16 22:15:56 +00:00
|
|
|
|
2012-02-08 19:42:29 +00:00
|
|
|
protected void DisableWatching ()
|
|
|
|
{
|
|
|
|
lock (this.watch_lock) {
|
|
|
|
this.watcher.EnableRaisingEvents = false;
|
|
|
|
this.local_timer.Stop ();
|
|
|
|
}
|
|
|
|
}
|
2011-07-17 00:22:39 +00:00
|
|
|
|
|
|
|
|
2012-02-08 19:42:29 +00:00
|
|
|
protected void EnableWatching ()
|
|
|
|
{
|
|
|
|
lock (this.watch_lock) {
|
|
|
|
this.watcher.EnableRaisingEvents = true;
|
|
|
|
this.local_timer.Start ();
|
|
|
|
}
|
2011-06-22 19:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-15 23:45:17 +00:00
|
|
|
private DateTime progress_last_change = DateTime.Now;
|
|
|
|
private TimeSpan progress_change_interval = new TimeSpan (0, 0, 0, 1);
|
|
|
|
|
2012-02-09 01:46:25 +00:00
|
|
|
protected void OnProgressChanged (double progress_percentage, string progress_speed)
|
2011-12-30 00:44:35 +00:00
|
|
|
{
|
2012-01-15 23:45:17 +00:00
|
|
|
if (DateTime.Compare (this.progress_last_change,
|
|
|
|
DateTime.Now.Subtract (this.progress_change_interval)) < 0) {
|
|
|
|
|
2012-02-09 11:54:18 +00:00
|
|
|
if (ProgressChanged != null) {
|
2012-01-15 23:45:17 +00:00
|
|
|
if (progress_percentage == 100.0)
|
|
|
|
progress_percentage = 99.0;
|
2011-12-30 14:00:15 +00:00
|
|
|
|
2012-01-15 23:45:17 +00:00
|
|
|
this.progress_percentage = progress_percentage;
|
|
|
|
this.progress_speed = progress_speed;
|
|
|
|
this.progress_last_change = DateTime.Now;
|
2011-12-30 00:44:35 +00:00
|
|
|
|
2012-02-09 11:54:18 +00:00
|
|
|
ProgressChanged (progress_percentage, progress_speed);
|
2012-01-15 23:45:17 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-30 00:44:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-08 19:42:29 +00:00
|
|
|
// Create an initial change set when the
|
|
|
|
// user has fetched an empty remote folder
|
2012-04-22 12:32:55 +00:00
|
|
|
public virtual void CreateInitialChangeSet ()
|
2012-02-08 19:42:29 +00:00
|
|
|
{
|
|
|
|
string file_path = Path.Combine (LocalPath, "SparkleShare.txt");
|
2012-04-22 12:32:55 +00:00
|
|
|
string n = Environment.NewLine;
|
|
|
|
|
|
|
|
File.WriteAllText (file_path,
|
|
|
|
"Congratulations, you've successfully created a SparkleShare repository!" + n +
|
|
|
|
"" + n +
|
|
|
|
"Any files you add or change in this folder will be automatically synced to " + n +
|
2012-04-29 14:19:36 +00:00
|
|
|
RemoteUrl + " and everyone connected to it." + n +
|
2012-04-22 12:32:55 +00:00
|
|
|
"" + n +
|
|
|
|
"SparkleShare is a Free and Open Source software program that helps people " + n +
|
|
|
|
"collaborate and share files. If you like what we do, please consider a small " + n +
|
|
|
|
"donation to support the project: http://sparkleshare.org/support-us/" + n +
|
|
|
|
"" + n +
|
|
|
|
"Have fun! :)" + n
|
|
|
|
);
|
2012-02-08 19:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-16 22:15:56 +00:00
|
|
|
// Creates a SHA-1 hash of input
|
|
|
|
private string SHA1 (string s)
|
|
|
|
{
|
|
|
|
SHA1 sha1 = new SHA1CryptoServiceProvider ();
|
|
|
|
Byte[] bytes = ASCIIEncoding.Default.GetBytes (s);
|
|
|
|
Byte[] encoded_bytes = sha1.ComputeHash (bytes);
|
|
|
|
return BitConverter.ToString (encoded_bytes).ToLower ().Replace ("-", "");
|
|
|
|
}
|
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;
|
|
|
|
}
|
2011-05-19 16:05:58 +00:00
|
|
|
}
|
|
|
|
}
|