Commit 3c22c064 authored by Thomas's avatar Thomas
Browse files

Update the patch model

parent d0c20197
using pEp.Extensions; using pEp.Extensions;
using System; using System;
using System.Collections.Generic;
namespace pEp.DPE namespace pEp.DPE
{ {
public class Patch public class Patch
{ {
public static readonly string REG_FILE_SCHEME = "microsoft.windows.registry:"; public static readonly string REG_FILE_SCHEME = "file:///microsoft.windows.registry";
public enum RejectReason public enum RejectReason
{ {
...@@ -16,48 +17,21 @@ namespace pEp.DPE ...@@ -16,48 +17,21 @@ namespace pEp.DPE
RejectedByUser RejectedByUser
} }
public string Id { get; set; } = Guid.NewGuid().ToString(); internal List<PEPIdentity> ApplyTo { get; } = new List<PEPIdentity>();
public string Uri { get; set; } public string CommitMessage { get; set; }
public string Diff { get; set; } public DateTime CreationDate { get; set; } = DateTime.UtcNow;
public string CommitMessage { get; set; } public string Diff { get; set; }
public string Tag { get; set; } public string Id { get; set; } = Guid.NewGuid().ToString();
public DateTime CreationDate { get; set; } = DateTime.UtcNow; public Uri RootPath { get; set; }
public string Tag { get; set; }
/// <summary> /// <summary>
/// Primary constructor. Needed for de/serialization. /// Primary constructor.
/// </summary> /// </summary>
public Patch() public Patch()
{ {
} }
/// <summary>
/// Constructor for a new patch.
/// </summary>
/// <param name="uri">The location to apply the patch to.</param>
/// <param name="diff">The diff to apply.</param>
/// <param name="commitMessage">The commit message for this patch.</param>
/// <param name="tag">The tag for this patch (optional).</param>
public Patch(string uri, string diff, string commitMessage, string tag = null)
{
this.CommitMessage = commitMessage;
this.Diff = diff;
this.Tag = tag;
this.Uri = uri;
}
/// <summary>
/// Constructor for an existing patch
/// </summary>
/// <param name="uri">The location to apply the patch to.</param>
/// <param name="diff">The diff to apply.</param>
/// <param name="commitMessage">The commit message for this patch.</param>
/// <param name="creationDate">The date where this patch was created.</param>
/// <param name="tag">The tag for this patch (optional).</param>
public Patch(string uri, string diff, string commitMessage, DateTime creationDate, string tag = null) : this(uri, diff, commitMessage, tag)
{
this.CreationDate = creationDate;
}
/// <summary> /// <summary>
/// Deserializes a JSON string into a Patch object. /// Deserializes a JSON string into a Patch object.
/// </summary> /// </summary>
......
...@@ -15,11 +15,6 @@ namespace pEp.UI.Models ...@@ -15,11 +15,6 @@ namespace pEp.UI.Models
SupportOrRejectPatch SupportOrRejectPatch
} }
/// <summary>
/// Gets whether the patch is editable.
/// </summary>
public bool IsEditable { get; } = false;
/// <summary> /// <summary>
/// Get the patch. /// Get the patch.
/// </summary> /// </summary>
...@@ -35,12 +30,10 @@ namespace pEp.UI.Models ...@@ -35,12 +30,10 @@ namespace pEp.UI.Models
/// </summary> /// </summary>
/// <param name="patchAction">The action to perform with this patch.</param> /// <param name="patchAction">The action to perform with this patch.</param>
/// <param name="patch">The patch to be used in this dialog.</param> /// <param name="patch">The patch to be used in this dialog.</param>
/// <param name="isEditable">Whether the patch is editable.</param> public PatchDialog(PatchAction patchAction, Patch patch = null) : base(Dialog.Type.Patch, null)
public PatchDialog(PatchAction patchAction, Patch patch = null, bool isEditable = false) : base(Dialog.Type.Patch, null)
{ {
this.PatchActionType = patchAction; this.PatchActionType = patchAction;
this.Patch = patch; this.Patch = patch ?? new Patch();
this.IsEditable = isEditable;
// Set dialog title // Set dialog title
switch (patchAction) switch (patchAction)
...@@ -66,13 +59,9 @@ namespace pEp.UI.Models ...@@ -66,13 +59,9 @@ namespace pEp.UI.Models
/// <summary> /// <summary>
/// Shows a patch dialog. /// Shows a patch dialog.
/// </summary> /// </summary>
/// <param name="patchAction">The action to perform with the patch.</param> public static void ShowNewPatchDialog()
/// <param name="patch">The patch to manage.</param>
/// <param name="isEditable">Whether the patch can be edited.</param>
public static void ShowDialog(PatchDialog.PatchAction patchAction, Patch patch, bool isEditable = false)
{ {
// Open dialog and retrieve result PatchDialog patchDialog = new PatchDialog(PatchAction.NewPatch);
PatchDialog patchDialog = new PatchDialog(patchAction, patch, isEditable);
new DialogHost(patchDialog).ShowDialog(); new DialogHost(patchDialog).ShowDialog();
} }
} }
......
...@@ -1490,7 +1490,7 @@ namespace pEp ...@@ -1490,7 +1490,7 @@ namespace pEp
/// </summary> /// </summary>
public void ButtonDPE_Click(Office.IRibbonControl control) public void ButtonDPE_Click(Office.IRibbonControl control)
{ {
PatchDialog.ShowDialog(PatchDialog.PatchAction.NewPatch, new DPE.Patch(), true); PatchDialog.ShowNewPatchDialog();
} }
/// <summary> /// <summary>
......
...@@ -141,14 +141,14 @@ namespace pEp.UI.ViewModels ...@@ -141,14 +141,14 @@ namespace pEp.UI.ViewModels
} }
/// <summary> /// <summary>
/// Gets or sets the URI of the patch. /// Gets or sets the root path of the patch.
/// </summary> /// </summary>
public string Uri public string RootPath
{ {
get => this.patch?.Uri; get => this.patch?.RootPath.AbsoluteUri;
set set
{ {
this.patch.Uri = value; this.patch.RootPath = new Uri(value);
this.OnPropertyChanged(); this.OnPropertyChanged();
} }
} }
......
...@@ -106,11 +106,6 @@ namespace pEp.UI.ViewModels ...@@ -106,11 +106,6 @@ namespace pEp.UI.ViewModels
/// </summary> /// </summary>
public bool IsDiffValid { get => this._IsDiffValid; set => this.SetProperty(ref this._IsDiffValid, value); } public bool IsDiffValid { get => this._IsDiffValid; set => this.SetProperty(ref this._IsDiffValid, value); }
/// <summary>
/// Gets whether the patch in this dialog is editable.
/// </summary>
public bool IsEditable => this.Dialog.IsEditable;
/// <summary> /// <summary>
/// Gets or sets whether the view is currently loading. /// Gets or sets whether the view is currently loading.
/// </summary> /// </summary>
...@@ -272,7 +267,7 @@ namespace pEp.UI.ViewModels ...@@ -272,7 +267,7 @@ namespace pEp.UI.ViewModels
this.Dialog.Patch.CreationDate = DateTime.UtcNow; this.Dialog.Patch.CreationDate = DateTime.UtcNow;
// Get a common root URI of all diff files // Get a common root URI of all diff files
this.Dialog.Patch.Uri = this.GetRootUri(); this.Dialog.Patch.RootPath = this.GetRootPath();
// Concatenate all diffs from the selected files // Concatenate all diffs from the selected files
this.Dialog.Patch.Diff = this.UnifyDiffs(); this.Dialog.Patch.Diff = this.UnifyDiffs();
...@@ -552,7 +547,7 @@ namespace pEp.UI.ViewModels ...@@ -552,7 +547,7 @@ namespace pEp.UI.ViewModels
/// Gets a common root URI from all selected files in the dialog. /// Gets a common root URI from all selected files in the dialog.
/// </summary> /// </summary>
/// <returns>The root URI</returns> /// <returns>The root URI</returns>
private string GetRootUri() private Uri GetRootPath()
{ {
string firstFileName = this.ConfigFiles.First()?.FileName; string firstFileName = this.ConfigFiles.First()?.FileName;
if (string.IsNullOrEmpty(firstFileName)) if (string.IsNullOrEmpty(firstFileName))
...@@ -561,16 +556,16 @@ namespace pEp.UI.ViewModels ...@@ -561,16 +556,16 @@ namespace pEp.UI.ViewModels
return null; return null;
} }
string rootUri = Directory.GetParent(firstFileName).FullName.Replace('\\', '/'); string rootPath = Directory.GetParent(firstFileName).FullName.Replace('\\', '/');
foreach (ConfigFile modifiedFile in this.ConfigFiles) foreach (ConfigFile modifiedFile in this.ConfigFiles)
{ {
string folderName = Directory.GetParent(modifiedFile.FileName).FullName.Replace('\\', '/'); string folderName = Directory.GetParent(modifiedFile.FileName).FullName.Replace('\\', '/');
int i = 0; int i = 0;
while ((i < folderName.Length) && while ((i < folderName.Length) &&
(i < rootUri.Length)) (i < rootPath.Length))
{ {
if (folderName[i] != rootUri[i]) if (folderName[i] != rootPath[i])
{ {
break; break;
} }
...@@ -578,10 +573,10 @@ namespace pEp.UI.ViewModels ...@@ -578,10 +573,10 @@ namespace pEp.UI.ViewModels
i++; i++;
} }
rootUri = rootUri.Substring(0, i); rootPath = rootPath.Substring(0, i);
} }
return rootUri; return new Uri(rootPath);
} }
/// <summary> /// <summary>
...@@ -608,8 +603,8 @@ namespace pEp.UI.ViewModels ...@@ -608,8 +603,8 @@ namespace pEp.UI.ViewModels
} }
// Remove root parts of URI // Remove root parts of URI
diff = diff.Replace(PatchDialogViewModel.FILE_A_PREFIX + this.Dialog.Patch.Uri + "/", PatchDialogViewModel.FILE_A_PREFIX); diff = diff.Replace(PatchDialogViewModel.FILE_A_PREFIX + this.Dialog.Patch.RootPath + "/", PatchDialogViewModel.FILE_A_PREFIX);
diff = diff.Replace(PatchDialogViewModel.FILE_B_PREFIX + this.Dialog.Patch.Uri + "/", PatchDialogViewModel.FILE_B_PREFIX); diff = diff.Replace(PatchDialogViewModel.FILE_B_PREFIX + this.Dialog.Patch.RootPath + "/", PatchDialogViewModel.FILE_B_PREFIX);
return diff.TrimEnd('\n'); return diff.TrimEnd('\n');
} }
......
...@@ -142,7 +142,7 @@ ...@@ -142,7 +142,7 @@
Background="{Binding Background}" Background="{Binding Background}"
VerticalAlignment="Center" VerticalAlignment="Center"
Padding="2" Padding="2"
Text="{Binding Uri}" Text="{Binding RootPath}"
IsReadOnly="True" IsReadOnly="True"
Margin="5,10,10,10"/> Margin="5,10,10,10"/>
<Label Grid.Column="0" <Label Grid.Column="0"
......
...@@ -122,15 +122,13 @@ ...@@ -122,15 +122,13 @@
Text="{Binding CommitMessage, UpdateSourceTrigger=PropertyChanged}" Text="{Binding CommitMessage, UpdateSourceTrigger=PropertyChanged}"
IsValidInput="{Binding IsCommitMessageValid}" IsValidInput="{Binding IsCommitMessageValid}"
Placeholder="Commit message" Placeholder="Commit message"
Margin="5,10,10,10" Margin="5,10,10,10" />
Visibility="{Binding IsEditable, Converter={StaticResource BoolToVisibility}}"/>
<ui:TextBoxWithPlaceholder Grid.Column="0" <ui:TextBoxWithPlaceholder Grid.Column="0"
Grid.ColumnSpan="3" Grid.ColumnSpan="3"
Grid.Row="3" Grid.Row="3"
Text="{Binding Tag, UpdateSourceTrigger=PropertyChanged}" Text="{Binding Tag, UpdateSourceTrigger=PropertyChanged}"
Placeholder="Tag (optional)" Placeholder="Tag (optional)"
Margin="5,10,10,10" Margin="5,10,10,10" />
Visibility="{Binding IsEditable, Converter={StaticResource BoolToVisibility}}"/>
<StackPanel Grid.Column="0" <StackPanel Grid.Column="0"
Grid.ColumnSpan="3" Grid.ColumnSpan="3"
Grid.Row="4" Grid.Row="4"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment