Commit 8eeab9ee authored by Thomas's avatar Thomas
Browse files

Save patch status if patch was supported or rejected

parent de2afc87
using pEp.DPE; using pEp.DPE;
using pEp.UI.Models;
using pEp.UI.ViewModels; using pEp.UI.ViewModels;
using System; using System;
using System.ComponentModel;
using Outlook = Microsoft.Office.Interop.Outlook; using Outlook = Microsoft.Office.Interop.Outlook;
namespace pEp namespace pEp
{ {
partial class FormRegionDPE internal partial class FormRegionDPE
{ {
#region Form Region Factory #region Form Region Factory
...@@ -33,10 +33,41 @@ namespace pEp ...@@ -33,10 +33,41 @@ namespace pEp
try try
{ {
omi = this.OutlookItem as Outlook.MailItem; omi = this.OutlookItem as Outlook.MailItem;
string xml = omi.HTMLBody; string xml = omi.HTMLBody;
Patch patch = Patch.Deserialize(xml); Patch patch = Patch.Deserialize(xml);
PEPIdentity.GetFromIdentity(omi, out PEPIdentity submitter); FormControlPatchViewModel.PatchStatus patchStatus = FormControlPatchViewModel.PatchStatus.Open;
this.FormControlPatchView.DataContext = new FormControlPatchViewModel(patch, submitter); DateTime? editDate = null;
// Get patch status if available
if ((omi.GetUserProperty(MailItemExtensions.USER_PROPERTY_KEY_DPE_PATCH_STATUS, FormControlPatchViewModel.PatchStatus.Open) is string patchStatusString) &&
Enum.TryParse(patchStatusString, out FormControlPatchViewModel.PatchStatus status))
{
patchStatus = status;
}
// Get last edit date if available
if ((omi.GetUserProperty(MailItemExtensions.USER_PROPERTY_KEY_DPE_PATCH_EDIT_DATE) is string editDateString) &&
DateTime.TryParse(editDateString, out DateTime savedEditDate))
{
editDate = savedEditDate;
}
// Get the patch submitter
if (PEPIdentity.GetFromIdentity(omi, out PEPIdentity submitter) == Globals.ReturnStatus.Success)
{
this.FormControlPatchView.DataContext = new FormControlPatchViewModel(patch, submitter, patchStatus, editDate);
// Subscribe to property changed event
if (this.FormControlPatchView.DataContext is FormControlPatchViewModel formControlPatchViewModel)
{
formControlPatchViewModel.PropertyChanged += this.FormRegionDPE_PropertyChanged;
}
}
else
{
throw new Exception("FormRegionDPE_FormRegionShowing: Error getting patch submitter.");
}
} }
catch (Exception ex) catch (Exception ex)
{ {
...@@ -48,11 +79,55 @@ namespace pEp ...@@ -48,11 +79,55 @@ namespace pEp
} }
} }
/// <summary>
/// Event handler for when a property in the associated view model changes.
/// </summary>
private void FormRegionDPE_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
// Set patch status and edit date as user properties if being changed
if ((e.PropertyName == nameof(FormControlPatchViewModel.Status)) ||
(e.PropertyName == nameof(FormControlPatchViewModel.EditDate)))
{
Outlook.MailItem omi = null;
try
{
omi = this.OutlookItem as Outlook.MailItem;
FormControlPatchViewModel.PatchStatus patchStatus = (sender as FormControlPatchViewModel).Status;
DateTime? editDate = (sender as FormControlPatchViewModel).EditDate;
if (patchStatus != FormControlPatchViewModel.PatchStatus.Open)
{
omi.SetUserProperty(MailItemExtensions.USER_PROPERTY_KEY_DPE_PATCH_STATUS, Enum.GetName(typeof(FormControlPatchViewModel.PatchStatus), patchStatus));
}
if (editDate != null)
{
omi.SetUserProperty(MailItemExtensions.USER_PROPERTY_KEY_DPE_PATCH_EDIT_DATE, editDate?.ToString("f"));
}
omi.Save();
}
catch (Exception ex)
{
Log.Error("FormRegionDPE_PropertyChanged: Error setting user property. " + ex);
}
finally
{
omi = null;
}
}
}
// Occurs when the form region is closed. // Occurs when the form region is closed.
// Use this.OutlookItem to get a reference to the current Outlook item. // Use this.OutlookItem to get a reference to the current Outlook item.
// Use this.OutlookFormRegion to get a reference to the form region. // Use this.OutlookFormRegion to get a reference to the form region.
private void FormRegionDPE_FormRegionClosed(object sender, EventArgs e) private void FormRegionDPE_FormRegionClosed(object sender, EventArgs e)
{ {
// Unsubscribe from property changed event
if (this.FormControlPatchView.DataContext is FormControlPatchViewModel formControlPatchViewModel)
{
formControlPatchViewModel.PropertyChanged -= this.FormRegionDPE_PropertyChanged;
}
} }
} }
} }
using pEp.DPE; using pEp.DPE;
using System;
using System.Windows.Documents; using System.Windows.Documents;
namespace pEp.UI.ViewModels namespace pEp.UI.ViewModels
{ {
internal class FormControlPatchViewModel : ViewModelBase internal class FormControlPatchViewModel : ViewModelBase
{ {
public enum PatchStatus
{
Open,
Accepted,
Supported,
Rejected
}
#region Fields #region Fields
private readonly Patch patch; private readonly Patch patch;
private PatchStatus _Status = PatchStatus.Open;
private DateTime? _EditDate = null;
private string _Explanation = null;
#endregion #endregion
#region Properties #region Properties
...@@ -49,10 +62,15 @@ namespace pEp.UI.ViewModels ...@@ -49,10 +62,15 @@ namespace pEp.UI.ViewModels
/// </summary> /// </summary>
public FlowDocument DisplayDiff => PatchDialogViewModel.FormatDiff(this.Diff); public FlowDocument DisplayDiff => PatchDialogViewModel.FormatDiff(this.Diff);
/// <summary>
/// Gets or sets the last date the patch was edited.
/// </summary>
public DateTime? EditDate { get => this._EditDate; set => this.SetProperty(ref this._EditDate, value); }
/// <summary> /// <summary>
/// The explanation shown in the UI regarding this patch. /// The explanation shown in the UI regarding this patch.
/// </summary> /// </summary>
public string Explanation { get; } public string Explanation { get => this._Explanation; set => this.SetProperty(ref this._Explanation, value); }
/// <summary> /// <summary>
/// Gets whether the OK button is visible. /// Gets whether the OK button is visible.
...@@ -67,7 +85,7 @@ namespace pEp.UI.ViewModels ...@@ -67,7 +85,7 @@ namespace pEp.UI.ViewModels
/// <summary> /// <summary>
/// The command to accept the patch dialog. /// The command to accept the patch dialog.
/// </summary> /// </summary>
public RelayCommand OKButtonCommand => new RelayCommand(this.SupportPatch); public RelayCommand OKButtonCommand => new RelayCommand(this.SupportPatch, p => (this.Status == PatchStatus.Open));
/// <summary> /// <summary>
/// Gets the OK button text. /// Gets the OK button text.
...@@ -77,7 +95,7 @@ namespace pEp.UI.ViewModels ...@@ -77,7 +95,7 @@ namespace pEp.UI.ViewModels
/// <summary> /// <summary>
/// The command to reject the dialog. /// The command to reject the dialog.
/// </summary> /// </summary>
public RelayCommand RejectButtonCommand => new RelayCommand(this.RejectPatch); public RelayCommand RejectButtonCommand => new RelayCommand(this.RejectPatch, p => (this.Status == PatchStatus.Open));
/// <summary> /// <summary>
/// Gets the Reject button text. /// Gets the Reject button text.
...@@ -89,6 +107,11 @@ namespace pEp.UI.ViewModels ...@@ -89,6 +107,11 @@ namespace pEp.UI.ViewModels
/// </summary> /// </summary>
public PEPIdentity Submitter { get; } public PEPIdentity Submitter { get; }
/// <summary>
/// Gets or sets the status of this patch.
/// </summary>
public PatchStatus Status { get => this._Status; set => this.SetProperty(ref this._Status, value); }
/// <summary> /// <summary>
/// Gets or sets the tag of the patch. /// Gets or sets the tag of the patch.
/// </summary> /// </summary>
...@@ -124,13 +147,17 @@ namespace pEp.UI.ViewModels ...@@ -124,13 +147,17 @@ namespace pEp.UI.ViewModels
/// </summary> /// </summary>
/// <param name="patch">The patch to create the form region with.</param> /// <param name="patch">The patch to create the form region with.</param>
/// <param name="submitter">The submitter of the patch.</param> /// <param name="submitter">The submitter of the patch.</param>
public FormControlPatchViewModel(Patch patch, PEPIdentity submitter) /// <param name="status">The status of the patch.</param>
/// <param name="editDate">The last time this patch was edited (accepted/supported/rejected).</param>
public FormControlPatchViewModel(Patch patch, PEPIdentity submitter, PatchStatus status = PatchStatus.Open, DateTime? editDate = null)
{ {
this.EditDate = editDate;
this.patch = patch; this.patch = patch;
this.Submitter = submitter; this.Submitter = submitter;
this.Explanation = "New configuration changes pending approval"; this.Status = status;
this.IsRejectButtonVisible = true; this.IsRejectButtonVisible = true;
this.OKButtonText = "Support"; this.OKButtonText = "Support";
this.SetExplanation();
} }
#endregion #endregion
...@@ -144,6 +171,38 @@ namespace pEp.UI.ViewModels ...@@ -144,6 +171,38 @@ namespace pEp.UI.ViewModels
private void RejectPatch(object parameter) private void RejectPatch(object parameter)
{ {
Globals.ThisAddIn.DistributedPolicyEngine.Reject(this.patch, new PEPIdentity()); Globals.ThisAddIn.DistributedPolicyEngine.Reject(this.patch, new PEPIdentity());
this.Status = PatchStatus.Rejected;
this.EditDate = DateTime.UtcNow;
this.SetExplanation();
}
/// <summary>
/// Sets the explanation text.
/// </summary>
private void SetExplanation()
{
string editDate = "<n/a>";
if (this.EditDate != null)
{
editDate = ((DateTime)this.EditDate).ToLocalTime().ToString("F");
}
switch (this.Status)
{
case PatchStatus.Accepted:
this.Explanation = $"Accepted on { editDate }";
break;
case PatchStatus.Supported:
this.Explanation = $"Supported on { editDate }";
break;
case PatchStatus.Rejected:
this.Explanation = $"Rejected on { editDate }";
break;
case PatchStatus.Open:
default:
this.Explanation = "New configuration changes pending approval";
break;
}
} }
/// <summary> /// <summary>
...@@ -153,6 +212,9 @@ namespace pEp.UI.ViewModels ...@@ -153,6 +212,9 @@ namespace pEp.UI.ViewModels
private void SupportPatch(object parameter) private void SupportPatch(object parameter)
{ {
Globals.ThisAddIn.DistributedPolicyEngine.Support(this.patch, new PEPIdentity()); Globals.ThisAddIn.DistributedPolicyEngine.Support(this.patch, new PEPIdentity());
this.Status = PatchStatus.Supported;
this.EditDate = DateTime.UtcNow;
this.SetExplanation();
} }
#endregion #endregion
......
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