More cleanup of SparkleLib API

This commit is contained in:
Hylke Bons 2012-02-09 02:46:25 +01:00
parent 3370a283ea
commit f2b9e7bce7
5 changed files with 64 additions and 92 deletions

View file

@ -79,8 +79,8 @@ namespace SparkleLib {
} }
base.target_folder = target_folder; TargetFolder = target_folder;
base.remote_url = uri.ToString (); RemoteUrl = uri.ToString ();
} }
@ -89,7 +89,7 @@ namespace SparkleLib {
this.git = new SparkleGit (SparkleConfig.DefaultConfig.TmpPath, this.git = new SparkleGit (SparkleConfig.DefaultConfig.TmpPath,
"clone " + "clone " +
"--progress " + // Redirects progress stats to standarderror "--progress " + // Redirects progress stats to standarderror
"\"" + base.remote_url + "\" " + "\"" + base.target_folder + "\""); "\"" + RemoteUrl + "\" " + "\"" + TargetFolder + "\"");
this.git.StartInfo.RedirectStandardError = true; this.git.StartInfo.RedirectStandardError = true;
this.git.Start (); this.git.Start ();
@ -139,35 +139,36 @@ namespace SparkleLib {
} else { } else {
InstallConfiguration (); InstallConfiguration ();
InstallExcludeRules (); InstallExcludeRules ();
AddWarnings ();
return true; return true;
} }
} }
public override string [] Warnings { private void AddWarnings ()
get { {
SparkleGit git = new SparkleGit (SparkleConfig.DefaultConfig.TmpPath, SparkleGit git = new SparkleGit (SparkleConfig.DefaultConfig.TmpPath,
"config --global core.excludesfile"); "config --global core.excludesfile");
git.Start (); git.Start ();
// Reading the standard output HAS to go before // Reading the standard output HAS to go before
// WaitForExit, or it will hang forever on output > 4096 bytes // WaitForExit, or it will hang forever on output > 4096 bytes
string output = git.StandardOutput.ReadToEnd ().Trim (); string output = git.StandardOutput.ReadToEnd ().Trim ();
git.WaitForExit (); git.WaitForExit ();
if (string.IsNullOrEmpty (output)) { if (string.IsNullOrEmpty (output)) {
return null; return;
} else { } else {
return new string [] { Warnings = new string [] {
string.Format ("You seem to have configured a system gitignore file. " + string.Format ("You seem to have configured a system gitignore file. " +
"This may interfere with SparkleShare.\n({0})", output) "This may interfere with SparkleShare.\n({0})", output)
}; };
}
} }
} }
public override void Stop () public override void Stop ()
{ {
if (this.git != null && !this.git.HasExited) { if (this.git != null && !this.git.HasExited) {
@ -175,7 +176,7 @@ namespace SparkleLib {
this.git.Dispose (); this.git.Dispose ();
} }
base.Stop (); Dispose ();
} }
@ -183,7 +184,7 @@ namespace SparkleLib {
// the newly cloned repository // the newly cloned repository
private void InstallConfiguration () private void InstallConfiguration ()
{ {
string repo_config_file_path = SparkleHelpers.CombineMore (base.target_folder, ".git", "config"); string repo_config_file_path = SparkleHelpers.CombineMore (TargetFolder, ".git", "config");
string config = String.Join (Environment.NewLine, File.ReadAllLines (repo_config_file_path)); string config = String.Join (Environment.NewLine, File.ReadAllLines (repo_config_file_path));
string n = Environment.NewLine; string n = Environment.NewLine;
@ -219,7 +220,7 @@ namespace SparkleLib {
private void InstallExcludeRules () private void InstallExcludeRules ()
{ {
DirectoryInfo info = Directory.CreateDirectory ( DirectoryInfo info = Directory.CreateDirectory (
SparkleHelpers.CombineMore (this.target_folder, ".git", "info")); SparkleHelpers.CombineMore (TargetFolder, ".git", "info"));
// File that lists the files we want git to ignore // File that lists the files we want git to ignore
string exclude_rules_file_path = Path.Combine (info.FullName, "exclude"); string exclude_rules_file_path = Path.Combine (info.FullName, "exclude");

View file

@ -244,7 +244,7 @@ namespace SparkleLib {
if (number >= percentage) { if (number >= percentage) {
percentage = number; percentage = number;
base.OnSyncProgressChanged (percentage, speed); base.OnProgressChanged (percentage, speed);
} }
} }
@ -301,7 +301,7 @@ namespace SparkleLib {
if (number >= percentage) { if (number >= percentage) {
percentage = number; percentage = number;
base.OnSyncProgressChanged (percentage, speed); base.OnProgressChanged (percentage, speed);
} }
} }

View file

@ -35,18 +35,21 @@ namespace SparkleLib {
public event FailedEventHandler Failed; public event FailedEventHandler Failed;
public event ProgressChangedEventHandler ProgressChanged; public event ProgressChangedEventHandler ProgressChanged;
public string [] ExcludeRules; public abstract bool Fetch ();
public abstract void Stop ();
protected string target_folder; public string TargetFolder;
protected string remote_url; public string RemoteUrl;
public string [] ExcludeRules;
public string [] Warnings;
private Thread thread; private Thread thread;
public SparkleFetcherBase (string server, string remote_folder, string target_folder) public SparkleFetcherBase (string server, string remote_folder, string target_folder)
{ {
this.target_folder = target_folder; TargetFolder = target_folder;
this.remote_url = server + "/" + remote_folder; RemoteUrl = server + "/" + remote_folder;
ExcludeRules = new string [] { ExcludeRules = new string [] {
// gedit and emacs // gedit and emacs
@ -119,22 +122,18 @@ namespace SparkleLib {
} }
public abstract bool Fetch ();
public abstract string [] Warnings { get; }
// Clones the remote repository // Clones the remote repository
public void Start () public void Start ()
{ {
SparkleHelpers.DebugInfo ("Fetcher", "[" + this.target_folder + "] Fetching folder: " + this.remote_url); SparkleHelpers.DebugInfo ("Fetcher", "[" + TargetFolder + "] Fetching folder: " + RemoteUrl);
if (Started != null) if (Started != null)
Started (); Started ();
if (Directory.Exists (this.target_folder)) if (Directory.Exists (TargetFolder))
Directory.Delete (this.target_folder, true); Directory.Delete (TargetFolder, true);
string host = GetHost (this.remote_url); string host = GetHost (RemoteUrl);
if (String.IsNullOrEmpty (host)) { if (String.IsNullOrEmpty (host)) {
if (Failed != null) if (Failed != null)
@ -168,20 +167,6 @@ namespace SparkleLib {
} }
public virtual void Stop ()
{
this.thread.Abort ();
this.thread.Join ();
}
public string RemoteUrl {
get {
return this.remote_url;
}
}
public void Dispose () public void Dispose ()
{ {
if (this.thread != null) { if (this.thread != null) {

View file

@ -38,45 +38,28 @@ namespace SparkleLib {
private TimeSpan short_interval = new TimeSpan (0, 0, 3, 0); private TimeSpan short_interval = new TimeSpan (0, 0, 3, 0);
private TimeSpan long_interval = new TimeSpan (0, 0, 10, 0); private TimeSpan long_interval = new TimeSpan (0, 0, 10, 0);
private TimeSpan poll_interval;
private SparkleWatcher watcher; private SparkleWatcher watcher;
private SparkleListenerBase listener; private SparkleListenerBase listener;
private TimeSpan poll_interval;
private System.Timers.Timer local_timer = new System.Timers.Timer () { Interval = 0.25 * 1000 }; private System.Timers.Timer local_timer = new System.Timers.Timer () { Interval = 0.25 * 1000 };
private System.Timers.Timer remote_timer = new System.Timers.Timer () { Interval = 10 * 1000 }; private System.Timers.Timer remote_timer = new System.Timers.Timer () { Interval = 10 * 1000 };
private DateTime last_poll = DateTime.Now; private DateTime last_poll = DateTime.Now;
private List<double> size_buffer = new List<double> (); private List<double> size_buffer = new List<double> ();
private bool has_changed = false;
private Object change_lock = new Object (); private Object change_lock = new Object ();
private Object watch_lock = new Object (); private Object watch_lock = new Object ();
private double progress_percentage = 0.0; private double progress_percentage = 0.0;
private string progress_speed = ""; private string progress_speed = "";
private bool has_changed = false;
private bool is_buffering = false;
private bool server_online = true;
private SyncStatus status; private SyncStatus status;
private bool is_buffering = false;
private bool server_online = true;
public readonly string LocalPath;
public readonly string Name;
public readonly Uri Url;
public abstract bool HasLocalChanges { get; }
public abstract string Identifier { get; }
public abstract string CurrentRevision { get; }
public abstract bool SyncUp ();
public abstract bool SyncDown ();
public abstract bool HasUnsyncedChanges { get; set; }
public abstract bool HasRemoteChanges { get; }
public abstract List<string> ExcludePaths { get; }
public abstract double Size { get; }
public abstract double HistorySize { get; }
public delegate void SyncStatusChangedEventHandler (SyncStatus new_status); public delegate void SyncStatusChangedEventHandler (SyncStatus new_status);
public event SyncStatusChangedEventHandler SyncStatusChanged; public event SyncStatusChangedEventHandler SyncStatusChanged;
public delegate void SyncProgressChangedEventHandler (double percentage, string speed); public delegate void ProgressChangedEventHandler (double percentage, string speed);
public event SyncProgressChangedEventHandler SyncProgressChanged; public event ProgressChangedEventHandler SyncProgressChanged;
public delegate void NewChangeSetEventHandler (SparkleChangeSet change_set); public delegate void NewChangeSetEventHandler (SparkleChangeSet change_set);
public event NewChangeSetEventHandler NewChangeSet; public event NewChangeSetEventHandler NewChangeSet;
@ -91,41 +74,53 @@ namespace SparkleLib {
public event ChangesDetectedEventHandler ChangesDetected; public event ChangesDetectedEventHandler ChangesDetected;
public readonly string LocalPath;
public readonly string Name;
public readonly Uri Url;
public abstract string Identifier { get; }
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);
public bool ServerOnline { public bool ServerOnline {
get { get {
return this.server_online; return this.server_online;
} }
} }
public SyncStatus Status { public SyncStatus Status {
get { get {
return this.status; return this.status;
} }
} }
public double ProgressPercentage { public double ProgressPercentage {
get { get {
return this.progress_percentage; return this.progress_percentage;
} }
} }
public string ProgressSpeed { public string ProgressSpeed {
get { get {
return this.progress_speed; return this.progress_speed;
} }
} }
public virtual string [] UnsyncedFilePaths { public virtual string [] UnsyncedFilePaths {
get { get {
return new string [0]; return new string [0];
} }
} }
public bool IsSyncing { public bool IsSyncing {
get { get {
return (Status == SyncStatus.SyncUp || return (Status == SyncStatus.SyncUp ||
@ -134,7 +129,6 @@ namespace SparkleLib {
} }
} }
public bool IsBuffering { public bool IsBuffering {
get { get {
return this.is_buffering; return this.is_buffering;
@ -213,11 +207,6 @@ namespace SparkleLib {
} }
public virtual List<SparkleChangeSet> GetChangeSets (int count) {
return null;
}
// Disposes all resourses of this object // Disposes all resourses of this object
public void Dispose () public void Dispose ()
{ {
@ -585,7 +574,7 @@ namespace SparkleLib {
private DateTime progress_last_change = DateTime.Now; private DateTime progress_last_change = DateTime.Now;
private TimeSpan progress_change_interval = new TimeSpan (0, 0, 0, 1); private TimeSpan progress_change_interval = new TimeSpan (0, 0, 0, 1);
protected void OnSyncProgressChanged (double progress_percentage, string progress_speed) protected void OnProgressChanged (double progress_percentage, string progress_speed)
{ {
if (DateTime.Compare (this.progress_last_change, if (DateTime.Compare (this.progress_last_change,
DateTime.Now.Subtract (this.progress_change_interval)) < 0) { DateTime.Now.Subtract (this.progress_change_interval)) < 0) {
@ -614,8 +603,7 @@ namespace SparkleLib {
writer.WriteLine ("Congratulations, you've successfully created a SparkleShare repository!"); writer.WriteLine ("Congratulations, you've successfully created a SparkleShare repository!");
writer.WriteLine (""); writer.WriteLine ("");
writer.WriteLine ("Any files you add or change in this folder will be automatically synced to "); writer.WriteLine ("Any files you add or change in this folder will be automatically synced to ");
writer.WriteLine (SparkleConfig.DefaultConfig.GetUrlForFolder (Name) + " and everyone connected to it."); writer.WriteLine (Url + " and everyone connected to it.");
// TODO: Url property? ^
writer.WriteLine (""); writer.WriteLine ("");
writer.WriteLine ("SparkleShare is a Free and Open Source software program that helps people "); writer.WriteLine ("SparkleShare is a Free and Open Source software program that helps people ");
@ -649,8 +637,6 @@ namespace SparkleLib {
double size = 0; double size = 0;
// Ignore the temporary 'rebase-apply' and '.tmp' directories. This prevents potential
// crashes when files are being queried whilst the files have already been deleted.
if (ExcludePaths.Contains (parent.Name)) if (ExcludePaths.Contains (parent.Name))
return 0; return 0;

View file

@ -615,7 +615,7 @@ namespace SparkleShare {
} }
}; };
repo.SyncProgressChanged += delegate (double percentage, string speed) { repo.ProgressChanged += delegate (double percentage, string speed) {
ProgressPercentage = percentage; ProgressPercentage = percentage;
ProgressSpeed = speed; ProgressSpeed = speed;