From 79f3473467b2aceba1fcd2c8390aa399eacb75a3 Mon Sep 17 00:00:00 2001 From: Hylke Bons Date: Fri, 26 Aug 2011 22:47:46 +0200 Subject: [PATCH] Allow initial sync to be cancelled by adding a cancel button. Closes #307 --- SparkleLib/Git/SparkleFetcherGit.cs | 24 +++++++++++++++++++----- SparkleLib/SparkleFetcherBase.cs | 7 +++++++ SparkleShare/Mac/SparkleSetup.cs | 11 ++++++++++- SparkleShare/SparkleController.cs | 26 +++++++++++++++++--------- SparkleShare/SparkleSetup.cs | 13 +++++++++---- SparkleShare/SparkleSetupController.cs | 9 +++++++++ 6 files changed, 71 insertions(+), 19 deletions(-) diff --git a/SparkleLib/Git/SparkleFetcherGit.cs b/SparkleLib/Git/SparkleFetcherGit.cs index 0162b451..1c5a7560 100644 --- a/SparkleLib/Git/SparkleFetcherGit.cs +++ b/SparkleLib/Git/SparkleFetcherGit.cs @@ -25,6 +25,9 @@ namespace SparkleLib { // Sets up a fetcher that can get remote folders public class SparkleFetcherGit : SparkleFetcherBase { + private SparkleGit git; + + public SparkleFetcherGit (string server, string remote_folder, string target_folder) : base (server, remote_folder, target_folder) { @@ -73,15 +76,15 @@ namespace SparkleLib { public override bool Fetch () { - SparkleGit git = new SparkleGit (SparkleConfig.DefaultConfig.TmpPath, + this.git = new SparkleGit (SparkleConfig.DefaultConfig.TmpPath, "clone \"" + base.remote_url + "\" " + "\"" + base.target_folder + "\""); - git.Start (); - git.WaitForExit (); + this.git.Start (); + this.git.WaitForExit (); - SparkleHelpers.DebugInfo ("Git", "Exit code " + git.ExitCode.ToString ()); + SparkleHelpers.DebugInfo ("Git", "Exit code " + this.git.ExitCode.ToString ()); - if (git.ExitCode != 0) { + if (this.git.ExitCode != 0) { return false; } else { InstallConfiguration (); @@ -91,6 +94,17 @@ namespace SparkleLib { } + public override void Stop () + { + if (this.git != null) { + this.git.Kill (); + this.git.Dispose (); + } + + base.Stop (); + } + + // Install the user's name and email and some config into // the newly cloned repository private void InstallConfiguration () diff --git a/SparkleLib/SparkleFetcherBase.cs b/SparkleLib/SparkleFetcherBase.cs index 0f2f673c..b9f61143 100644 --- a/SparkleLib/SparkleFetcherBase.cs +++ b/SparkleLib/SparkleFetcherBase.cs @@ -95,6 +95,13 @@ namespace SparkleLib { } + public virtual void Stop () + { + this.thread.Abort (); + this.thread.Join (); + } + + public string RemoteUrl { get { return this.remote_url; diff --git a/SparkleShare/Mac/SparkleSetup.cs b/SparkleShare/Mac/SparkleSetup.cs index 8ef3174e..4fa091e4 100644 --- a/SparkleShare/Mac/SparkleSetup.cs +++ b/SparkleShare/Mac/SparkleSetup.cs @@ -49,7 +49,7 @@ namespace SparkleShare { private NSButtonCell ButtonCellProto; private NSMatrix Matrix; private int ServerType; - private Timer timer; + private Timer timer; public SparkleSetup () : base () @@ -307,7 +307,16 @@ namespace SparkleShare { Enabled = false }; + CancelButton = new NSButton () { + Title = "Cancel" + }; + + CancelButton.Activated += delegate { + Controller.SyncingCancelled (); + }; + Buttons.Add (FinishButton); + Buttons.Add (CancelButton); break; } diff --git a/SparkleShare/SparkleController.cs b/SparkleShare/SparkleController.cs index 423e9179..750a6617 100644 --- a/SparkleShare/SparkleController.cs +++ b/SparkleShare/SparkleController.cs @@ -75,7 +75,9 @@ namespace SparkleShare { public delegate void NotificationRaisedEventHandler (string user_name, string user_email, string message, string repository_path); - + private SparkleFetcherBase fetcher; + + // Short alias for the translations public static string _ (string s) { @@ -977,7 +979,6 @@ namespace SparkleShare { string canonical_name = Path.GetFileNameWithoutExtension (remote_folder); string tmp_folder = Path.Combine (tmp_path, canonical_name); - SparkleFetcherBase fetcher = null; string backend = null; /* if (remote_folder.EndsWith (".hg")) { @@ -991,7 +992,7 @@ namespace SparkleShare { backend = "Scp"; } else {*/ - fetcher = new SparkleFetcherGit (server, remote_folder, tmp_folder); + this.fetcher = new SparkleFetcherGit (server, remote_folder, tmp_folder); backend = "Git"; //} @@ -1011,7 +1012,7 @@ namespace SparkleShare { if (i > 1) target_folder_name += " (" + i + ")"; - fetcher.Finished += delegate { + this.fetcher.Finished += delegate { // Needed to do the moving SparkleHelpers.ClearAttributes (tmp_folder); @@ -1024,7 +1025,7 @@ namespace SparkleShare { SparkleHelpers.DebugInfo ("Controller", "Error moving folder: " + e.Message); } - SparkleConfig.DefaultConfig.AddFolder (target_folder_name, fetcher.RemoteUrl, backend); + SparkleConfig.DefaultConfig.AddFolder (target_folder_name, this.fetcher.RemoteUrl, backend); AddRepository (target_folder_path); if (FolderFetched != null) @@ -1038,25 +1039,32 @@ namespace SparkleShare { if (FolderListChanged != null) FolderListChanged (); - fetcher.Dispose (); + this.fetcher.Dispose (); if (Directory.Exists (tmp_path)) Directory.Delete (tmp_path, true); }; - fetcher.Failed += delegate { + this.fetcher.Failed += delegate { if (FolderFetchError != null) FolderFetchError (); - fetcher.Dispose (); + this.fetcher.Dispose (); if (Directory.Exists (tmp_path)) Directory.Delete (tmp_path, true); }; - fetcher.Start (); + this.fetcher.Start (); + } + + + public void StopFetcher () + { + if (fetcher != null) + fetcher.Stop (); } diff --git a/SparkleShare/SparkleSetup.cs b/SparkleShare/SparkleSetup.cs index b7836f72..f4a33cde 100644 --- a/SparkleShare/SparkleSetup.cs +++ b/SparkleShare/SparkleSetup.cs @@ -298,16 +298,21 @@ namespace SparkleShare { Description = _("This may take a while." + Environment.NewLine) + _("Are you sure it’s not coffee o'clock?"); - Button button = new Button () { + Button finish_button = new Button () { Sensitive = false, Label = _("Finish") }; - button.Clicked += delegate { - Close (); + Button cancel_button = new Button () { + Label = _("Cancel") }; - AddButton (button); + cancel_button.Clicked += delegate { + Controller.SyncingCancelled (); + }; + + AddButton (cancel_button); + AddButton (finish_button); this.progress_bar_pulse_timer.Elapsed += delegate { Application.Invoke (delegate { diff --git a/SparkleShare/SparkleSetupController.cs b/SparkleShare/SparkleSetupController.cs index 8d36bc7d..ad0f9754 100644 --- a/SparkleShare/SparkleSetupController.cs +++ b/SparkleShare/SparkleSetupController.cs @@ -132,6 +132,15 @@ namespace SparkleShare { } + public void SyncingCancelled () + { + SparkleShare.Controller.StopFetcher (); + + if (ChangePageEvent != null) + ChangePageEvent (PageType.Add); + } + + public void FinishedPageCompleted () { this.previous_server = "";