statusicon: add sync progress stats (one repo at a time only for now). Closes #222

This commit is contained in:
Hylke Bons 2011-12-30 01:44:35 +01:00
parent aaf540a3ff
commit eb65f5e125
6 changed files with 145 additions and 21 deletions

View file

@ -94,6 +94,9 @@ namespace SparkleLib {
double percentage = 1.0; double percentage = 1.0;
Regex progress_regex = new Regex (@"([0-9]+)%", RegexOptions.Compiled); Regex progress_regex = new Regex (@"([0-9]+)%", RegexOptions.Compiled);
DateTime last_change = DateTime.Now;
TimeSpan change_interval = new TimeSpan (0, 0, 0, 1);
while (!this.git.StandardError.EndOfStream) { while (!this.git.StandardError.EndOfStream) {
string line = this.git.StandardError.ReadLine (); string line = this.git.StandardError.ReadLine ();
Match match = progress_regex.Match (line); Match match = progress_regex.Match (line);
@ -107,7 +110,7 @@ namespace SparkleLib {
// the "Receiving objects" stage which we count as the last 80% // the "Receiving objects" stage which we count as the last 80%
if (line.Contains ("|")) if (line.Contains ("|"))
// "Receiving objects" stage // "Receiving objects" stage
number = (number / 100 * 75 + 20); number = (number / 100 * 80 + 20);
else else
// "Compressing objects" stage // "Compressing objects" stage
number = (number / 100 * 20); number = (number / 100 * 20);
@ -116,19 +119,20 @@ namespace SparkleLib {
if (number >= percentage) { if (number >= percentage) {
percentage = number; percentage = number;
// FIXME: for some reason it doesn't go above 95% if (DateTime.Compare (last_change, DateTime.Now.Subtract (change_interval)) < 0) {
base.OnProgressChanged (percentage); base.OnProgressChanged (percentage);
last_change = DateTime.Now;
}
} }
System.Threading.Thread.Sleep (100);
} }
this.git.WaitForExit (); this.git.WaitForExit ();
SparkleHelpers.DebugInfo ("Git", "Exit code " + this.git.ExitCode.ToString ()); SparkleHelpers.DebugInfo ("Git", "Exit code " + this.git.ExitCode.ToString ());
if (this.git.ExitCode != 0) { if (this.git.ExitCode != 0) {
return false; return false;
} else { } else {
InstallConfiguration (); InstallConfiguration ();
InstallExcludeRules (); InstallExcludeRules ();

View file

@ -172,11 +172,66 @@ namespace SparkleLib {
Commit (message); Commit (message);
} }
SparkleGit git = new SparkleGit (LocalPath, "push origin master");
SparkleGit git = new SparkleGit (LocalPath,
"push --progress " + // Redirects progress stats to standarderror
"origin master");
git.StartInfo.RedirectStandardError = true;
git.Start (); git.Start ();
git.StandardOutput.ReadToEnd ();
double percentage = 1.0;
Regex progress_regex = new Regex (@"([0-9]+)%", RegexOptions.Compiled);
DateTime last_change = DateTime.Now;
TimeSpan change_interval = new TimeSpan (0, 0, 0, 1);
while (!git.StandardError.EndOfStream) {
string line = git.StandardError.ReadLine ();
Match match = progress_regex.Match (line);
string speed = "";
double number = 0.0;
if (match.Success) {
number = double.Parse (match.Groups [1].Value);
// The cloning progress consists of two stages: the "Compressing
// objects" stage which we count as 20% of the total progress, and
// the "Writing objects" stage which we count as the last 80%
if (line.StartsWith ("Compressing")) {
// "Compressing objects" stage
number = (number / 100 * 20);
} else {
// "Writing objects" stage
number = (number / 100 * 80 + 20);
if (line.Contains ("|")) {
speed = line.Substring (line.IndexOf ("|") + 1).Trim ();
speed = speed.Replace (", done.", "").Trim ();
speed = speed.Replace ("i", "");
speed = speed.Replace ("KB/s", "ᴋʙ/s");
speed = speed.Replace ("MB/s", "ᴍʙ/s");
}
}
}
if (number >= percentage) {
percentage = number;
if (percentage == 100.0)
percentage = 99.0;
if (DateTime.Compare (last_change, DateTime.Now.Subtract (change_interval)) < 0) {
base.OnSyncProgressChanged (percentage, speed);
last_change = DateTime.Now;
}
}
}
git.WaitForExit (); git.WaitForExit ();
if (git.ExitCode == 0) if (git.ExitCode == 0)
return true; return true;
else else
@ -239,6 +294,7 @@ namespace SparkleLib {
if (value) { if (value) {
if (!File.Exists (unsynced_file_path)) if (!File.Exists (unsynced_file_path))
File.Create (unsynced_file_path).Close (); File.Create (unsynced_file_path).Close ();
} else { } else {
File.Delete (unsynced_file_path); File.Delete (unsynced_file_path);
} }

View file

@ -44,11 +44,13 @@ namespace SparkleLib {
private TimeSpan poll_interval; 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> sizebuffer = new List<double> (); private List<double> sizebuffer = new List<double> ();
private bool has_changed = false; 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 string progress_speed = "";
protected SparkleListenerBase listener; protected SparkleListenerBase listener;
protected SyncStatus status; protected SyncStatus status;
@ -74,6 +76,9 @@ namespace SparkleLib {
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 event SyncProgressChangedEventHandler SyncProgressChanged;
public delegate void NewChangeSetEventHandler (SparkleChangeSet change_set); public delegate void NewChangeSetEventHandler (SparkleChangeSet change_set);
public event NewChangeSetEventHandler NewChangeSet; public event NewChangeSetEventHandler NewChangeSet;
@ -156,6 +161,20 @@ namespace SparkleLib {
} }
public double ProgressPercentage {
get {
return this.progress_percentage;
}
}
public string ProgressSpeed {
get {
return this.progress_speed;
}
}
public virtual string [] UnsyncedFilePaths { public virtual string [] UnsyncedFilePaths {
get { get {
return new string [0]; return new string [0];
@ -297,7 +316,7 @@ namespace SparkleLib {
} }
private bool IsSyncing { public bool IsSyncing {
get { get {
return (Status == SyncStatus.SyncUp || return (Status == SyncStatus.SyncUp ||
Status == SyncStatus.SyncDown || Status == SyncStatus.SyncDown ||
@ -461,6 +480,9 @@ namespace SparkleLib {
} finally { } finally {
this.remote_timer.Start (); this.remote_timer.Start ();
EnableWatching (); EnableWatching ();
this.progress_percentage = 0.0;
this.progress_speed = "";
} }
} }
@ -526,6 +548,9 @@ namespace SparkleLib {
this.remote_timer.Start (); this.remote_timer.Start ();
EnableWatching (); EnableWatching ();
this.progress_percentage = 0.0;
this.progress_speed = "";
} }
@ -596,6 +621,16 @@ namespace SparkleLib {
} }
protected void OnSyncProgressChanged (double progress_percentage, string progress_speed)
{
this.progress_percentage = progress_percentage;
this.progress_speed = progress_speed;
if (SyncProgressChanged != null)
SyncProgressChanged (progress_percentage, progress_speed);
}
// Creates a SHA-1 hash of input // Creates a SHA-1 hash of input
private string SHA1 (string s) private string SHA1 (string s)
{ {

View file

@ -110,7 +110,10 @@ namespace SparkleShare {
case IconState.Syncing: case IconState.Syncing:
StateText = _("Syncing…"); StateText = _("Syncing… " +
Controller.ProgressPercentage + "% " +
Controller.ProgressSpeed);
StateMenuItem.Title = StateText; StateMenuItem.Title = StateText;
if (!Animation.Enabled) if (!Animation.Enabled)

View file

@ -37,6 +37,9 @@ namespace SparkleShare {
public List <SparkleRepoBase> Repositories; public List <SparkleRepoBase> Repositories;
public readonly string SparklePath = SparkleConfig.DefaultConfig.FoldersPath; public readonly string SparklePath = SparkleConfig.DefaultConfig.FoldersPath;
public double ProgressPercentage = 0.0;
public string ProgressSpeed = "";
public event OnQuitWhileSyncingHandler OnQuitWhileSyncing; public event OnQuitWhileSyncingHandler OnQuitWhileSyncing;
public delegate void OnQuitWhileSyncingHandler (); public delegate void OnQuitWhileSyncingHandler ();
@ -586,11 +589,11 @@ namespace SparkleShare {
}; };
repo.SyncStatusChanged += delegate (SyncStatus status) { repo.SyncStatusChanged += delegate (SyncStatus status) {
/* if (status == SyncStatus.SyncUp) { if (status == SyncStatus.Idle) {
foreach (string path in repo.UnsyncedFilePaths) ProgressPercentage = 0.0;
Console.WriteLine (path); ProgressSpeed = "";
} }
*/
if (status == SyncStatus.Idle || if (status == SyncStatus.Idle ||
status == SyncStatus.SyncUp || status == SyncStatus.SyncUp ||
status == SyncStatus.SyncDown || status == SyncStatus.SyncDown ||
@ -600,6 +603,13 @@ namespace SparkleShare {
} }
}; };
repo.SyncProgressChanged += delegate (double percentage, string speed) {
ProgressPercentage = percentage;
ProgressSpeed = speed;
UpdateState ();
};
repo.ChangesDetected += delegate { repo.ChangesDetected += delegate {
UpdateState (); UpdateState ();
}; };

View file

@ -58,6 +58,19 @@ namespace SparkleShare {
} }
public int ProgressPercentage {
get {
return (int) Program.Controller.ProgressPercentage;
}
}
public string ProgressSpeed {
get {
return Program.Controller.ProgressSpeed;
}
}
public SparkleStatusIconController () public SparkleStatusIconController ()
{ {
Program.Controller.FolderListChanged += delegate { Program.Controller.FolderListChanged += delegate {
@ -65,6 +78,7 @@ namespace SparkleShare {
UpdateMenuEvent (CurrentState); UpdateMenuEvent (CurrentState);
}; };
Program.Controller.OnIdle += delegate { Program.Controller.OnIdle += delegate {
if (CurrentState != IconState.Error) if (CurrentState != IconState.Error)
CurrentState = IconState.Idle; CurrentState = IconState.Idle;
@ -73,6 +87,7 @@ namespace SparkleShare {
UpdateMenuEvent (CurrentState); UpdateMenuEvent (CurrentState);
}; };
Program.Controller.OnSyncing += delegate { Program.Controller.OnSyncing += delegate {
CurrentState = IconState.Syncing; CurrentState = IconState.Syncing;
@ -80,6 +95,7 @@ namespace SparkleShare {
UpdateMenuEvent (IconState.Syncing); UpdateMenuEvent (IconState.Syncing);
}; };
Program.Controller.OnError += delegate { Program.Controller.OnError += delegate {
CurrentState = IconState.Error; CurrentState = IconState.Error;