Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add public StartClone() method #325

Merged
merged 13 commits into from
Oct 13, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

- [SIL.Chorus.LibChorus] Add webm as additional audio file type
- [SIL.Chorus] Add ability to clone project without direct user interaction
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"without user interaction": there's no indirect interaction, either.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my mind, the indirect user interaction is them using the wrapping software. They still have to do something to get the clone to happen.


### Changed

Expand Down
14 changes: 13 additions & 1 deletion src/Chorus.Tests/UI/Clone/GetSharedProjectModelTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.IO;
using System.Windows.Forms;
using Chorus.UI.Clone;
using Chorus.VcsDrivers.Mercurial;
using NUnit.Framework;
Expand All @@ -10,6 +11,17 @@ namespace Chorus.Tests.UI.Clone
[TestFixture]
public class GetSharedProjectModelTests
{
[TestCase(DialogResult.OK, "C:\\somewhere", CloneStatus.Created)]
[TestCase(DialogResult.Cancel, "S:\\miles", CloneStatus.Cancelled)]
[TestCase(DialogResult.Abort, "E:\\elsewhere", CloneStatus.NotCreated)]
[TestCase(DialogResult.None, "~/no/where", CloneStatus.NotCreated)]
public void GetResult(DialogResult dialogResult, string cloneLocation, CloneStatus expectedStatus)
{
var result = GetSharedProjectModel.GetResult(dialogResult, cloneLocation);
Assert.That(result.CloneStatus, Is.EqualTo(expectedStatus));
Assert.That(result.ActualLocation, Is.EqualTo(expectedStatus == CloneStatus.Created ? cloneLocation : null));
}

[Test]
public void HasNoExtantRepositories()
{
Expand Down
76 changes: 73 additions & 3 deletions src/Chorus/UI/Clone/GetCloneFromInternetDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,73 @@ public GetCloneFromInternetDialog(GetCloneFromInternetModel model)

}

/// <summary>
/// Confirms with a dialog, then performs a clone operation with the supplied information.
/// </summary>
/// <param name="username">Username for Mercurial authentication</param>
/// <param name="password">Password for Mercurial authentication</param>
/// <param name="projectFolder">The parent directory to put the clone in</param>
/// <param name="projectName">Name for the project on the local machine</param>
/// <param name="projectUri">URI where the project can be found</param>
/// <param name="saveUserSettings">Flag to persist username and password in settings</param>
public static CloneResult ConfirmAndDoClone(string username, string password, string projectFolder, string projectName, string projectUri, bool saveUserSettings = true)
{
var projectNameExtractor = new GetCloneFromInternetModel();
projectNameExtractor.InitFromUri(projectUri);
var proj = string.IsNullOrEmpty(projectNameExtractor.ProjectId) ? projectName : projectNameExtractor.ProjectId;
var host = new Uri(projectUri).GetComponents(UriComponents.SchemeAndServer, UriFormat.SafeUnescaped);

var msg = string.Format(LocalizationManager.GetString("GetCloneFromInternet.DownloadProjectFromHost", "Download {0} from {1}?"),
proj, host);
var caption = LocalizationManager.GetString("GetCloneFromInternet.ConfirmDownload", "Confirm Download");

return MessageBox.Show(msg, caption, MessageBoxButtons.YesNo) == DialogResult.Yes
? DoClone(username, password, projectFolder, projectName, projectUri, saveUserSettings)
: null;
}

/// <summary>
/// Performs a clone operation with the supplied information.
/// </summary>
/// <param name="username">Username for Mercurial authentication</param>
/// <param name="password">Password for Mercurial authentication</param>
/// <param name="projectFolder">The parent directory to put the clone in</param>
/// <param name="projectName">Name for the project on the local machine</param>
/// <param name="projectUri">URI where the project can be found</param>
/// <param name="saveUserSettings">Flag to persist username and password in settings</param>
public static CloneResult DoClone(string username, string password, string projectFolder, string projectName, string projectUri, bool saveUserSettings = true)
{
var model = new GetCloneFromInternetModel(projectFolder);
var oldUser = model.Username;
var oldPass = model.Password;
model.Username = username;
model.Password = password;
model.CustomUrl = projectUri;
model.IsCustomUrl = true;
model.LocalFolderName = projectName;

using (var dialog = new GetCloneFromInternetDialog(model))
{
try
{
dialog.Show();
dialog.StartClone();
Application.Run(dialog);
}
finally
{
if (!saveUserSettings)
{
model.Username = oldUser;
model.Password = oldPass;
model.SaveUserSettings();
}
}

return GetSharedProjectModel.GetResult(dialog.DialogResult, dialog.PathToNewlyClonedFolder);
}
}

private void _backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (_statusProgress.ErrorEncountered)
Expand Down Expand Up @@ -261,21 +328,24 @@ private void _cancelButton_Click(object sender, EventArgs e)
}

private void OnDownloadClick(object sender, EventArgs e)
{
StartClone();
}

private void StartClone()
{
lock (this)
{
_logBox.Clear();
if(_backgroundWorker.IsBusy)
if (_backgroundWorker.IsBusy)
return;
UpdateDisplay(State.MakingClone);
_model.SaveUserSettings();
ThreadSafeUrl = _model.URL;
//_backgroundWorker.RunWorkerAsync(new object[] { ThreadSafeUrl, PathToNewProject, _progress });
_backgroundWorker.RunWorkerAsync(new object[0]);
}
}


public string ThreadSafeUrl
{
get;
Expand Down
92 changes: 35 additions & 57 deletions src/Chorus/UI/Clone/GetSharedProjectModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,20 @@ public CloneResult GetSharedProjectUsing(Form parent, string baseProjectDirForNe
// "Seeing fit' may mean to warn the user they already have some repository, or as a filter to not show ones that already exist.
// What to do with the list of extant repos is left up to a view+model pair.

var result = new CloneResult(null, CloneStatus.NotCreated);

// Select basic source type.
using (var getSharedProjectDlg = new GetSharedProjectDlg())
{
getSharedProjectDlg.InitFromModel(this);
getSharedProjectDlg.ShowDialog(parent);
if (getSharedProjectDlg.DialogResult != DialogResult.OK)
{
return new CloneResult(null, CloneStatus.NotCreated);
return result;
}
}

// Make clone from some source.
string actualCloneLocation = null;
var cloneStatus = CloneStatus.NotCreated;
switch (RepositorySource)
{
case ExtantRepoSource.Internet:
Expand All @@ -99,19 +99,7 @@ public CloneResult GetSharedProjectUsing(Form parent, string baseProjectDirForNe
};
using (var cloneFromInternetDialog = new GetCloneFromInternetDialog(cloneFromInternetModel))
{
switch (cloneFromInternetDialog.ShowDialog(parent))
{
default:
cloneStatus = CloneStatus.NotCreated;
break;
case DialogResult.Cancel:
cloneStatus = CloneStatus.Cancelled;
break;
case DialogResult.OK:
actualCloneLocation = cloneFromInternetDialog.PathToNewlyClonedFolder;
cloneStatus = CloneStatus.Created;
break;
}
result = GetResult(cloneFromInternetDialog.ShowDialog(parent), cloneFromInternetDialog.PathToNewlyClonedFolder);
}
break;

Expand All @@ -125,26 +113,13 @@ public CloneResult GetSharedProjectUsing(Form parent, string baseProjectDirForNe

using (var getCloneFromChorusHubDialog = new GetCloneFromChorusHubDialog(getCloneFromChorusHubModel))
{
switch (getCloneFromChorusHubDialog.ShowDialog(parent))
var dlgResult = getCloneFromChorusHubDialog.ShowDialog(parent);
if (dlgResult == DialogResult.OK && !getCloneFromChorusHubModel.CloneSucceeded)
{
default:
cloneStatus = CloneStatus.NotCreated;
break;
case DialogResult.Cancel:
cloneStatus = CloneStatus.Cancelled;
break;
case DialogResult.OK:
if (getCloneFromChorusHubModel.CloneSucceeded)
{
actualCloneLocation = getCloneFromChorusHubDialog.PathToNewlyClonedFolder;
cloneStatus = CloneStatus.Created;
}
else
{
cloneStatus = CloneStatus.NotCreated;
}
break;
// User clicked OK, but clone failed. Pass anything other than OK or Cancel to get CloneStatus.NotCreated
dlgResult = DialogResult.Abort;
}
result = GetResult(dlgResult, getCloneFromChorusHubDialog.PathToNewlyClonedFolder);
}
break;

Expand All @@ -154,40 +129,43 @@ public CloneResult GetSharedProjectUsing(Form parent, string baseProjectDirForNe
cloneFromUsbDialog.Model.ProjectFilter = projectFilter ?? DefaultProjectFilter;
cloneFromUsbDialog.Model.ReposInUse = existingRepositories;
cloneFromUsbDialog.Model.ExistingProjects = existingProjectNames;
switch (cloneFromUsbDialog.ShowDialog(parent))
{
default:
cloneStatus = CloneStatus.NotCreated;
break;
case DialogResult.Cancel:
cloneStatus = CloneStatus.Cancelled;
break;
case DialogResult.OK:
actualCloneLocation = cloneFromUsbDialog.PathToNewlyClonedFolder;
cloneStatus = CloneStatus.Created;
break;
}
// USB repositories have already been checked for to see if the same repo has already been cloned; return before this check
return GetResult(cloneFromUsbDialog.ShowDialog(parent), cloneFromUsbDialog.PathToNewlyClonedFolder);
}
break;

}
// Warn the user if they already have this by another name.
// Not currently needed for USB, since those have already been checked.
if (RepositorySource != ExtantRepoSource.Usb && cloneStatus == CloneStatus.Created)
if (result.CloneStatus == CloneStatus.Created)
{
var repo = new HgRepository(actualCloneLocation, new NullProgress());
string projectWithExistingRepo;
if (repo.Identifier != null && existingRepositories.TryGetValue(repo.Identifier, out projectWithExistingRepo))
var repo = new HgRepository(result.ActualLocation, new NullProgress());
if (repo.Identifier != null && existingRepositories.TryGetValue(repo.Identifier, out var projectWithExistingRepo))
{
using (var warningDlg = new DuplicateProjectWarningDialog())
warningDlg.Run(projectWithExistingRepo, howToSendReceiveMessageText);
Directory.Delete(actualCloneLocation, true);
actualCloneLocation = null;
cloneStatus = CloneStatus.Cancelled;
Directory.Delete(result.ActualLocation, true);
return new CloneResult(null, CloneStatus.NotCreated);
}
}
return result;
}

internal static CloneResult GetResult(DialogResult dialogResult, string cloneLocation)
{
CloneStatus cloneStatus;
switch (dialogResult)
{
default:
cloneStatus = CloneStatus.NotCreated;
break;
case DialogResult.Cancel:
cloneStatus = CloneStatus.Cancelled;
break;
case DialogResult.OK:
cloneStatus = CloneStatus.Created;
break;
}
return new CloneResult(actualCloneLocation, cloneStatus);

return new CloneResult(cloneStatus == CloneStatus.Created ? cloneLocation : null, cloneStatus);
}

internal static bool DefaultProjectFilter(string path)
Expand Down
6 changes: 3 additions & 3 deletions src/LibChorus/CloneResult.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2015 SIL International
// Copyright (c) 2015 SIL International
// This software is licensed under the MIT License (http://opensource.org/licenses/MIT)
using System;

Expand All @@ -17,9 +17,9 @@ public CloneResult(string actualLocation, CloneStatus cloneStatus)
}

/// <summary>Get the actual location of a clone. (May, or may not, be the same as the desired location.)</summary>
public string ActualLocation { get; private set; }
public string ActualLocation { get; }
/// <summary>Get the status of the clone attempt.</summary>
public CloneStatus CloneStatus { get; private set; }
public CloneStatus CloneStatus { get; }
}

/// <summary>
Expand Down
Loading