Commit c72ce70a authored by Thomas's avatar Thomas
Browse files

Execute all API calls in background and catch errors

parent 2b3974db
using pEp.Extensions; using pEp.Extensions;
using System; using System;
using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks;
namespace pEp.DPE namespace pEp.DPE
{ {
...@@ -18,19 +18,26 @@ namespace pEp.DPE ...@@ -18,19 +18,26 @@ namespace pEp.DPE
/// </summary> /// </summary>
/// <param name="patch">The patch to reject.</param> /// <param name="patch">The patch to reject.</param>
/// <param name="me">The own identity that rejects the patch.</param> /// <param name="me">The own identity that rejects the patch.</param>
public static async void RejectPatch(Patch patch, PEPIdentity me) /// <exception cref="ArgumentNullException" />
/// <exception cref="HttpRequestException" />
/// <returns>The Http response.</returns>
public static async Task<HttpResponseMessage> RejectPatch(Patch patch, PEPIdentity me)
{ {
// POST http://localhost:port/pEpDPE/patches/patch_id/reject // POST http://localhost:port/pEpDPE/patches/patch_id/reject
try try
{ {
HttpResponseMessage response = return await DPEWebClient.httpClient.PostAsync(DPEWebClient.DPE_POST_URL,
await DPEWebClient.httpClient.PostAsync(DPEWebClient.DPE_POST_URL, new StringContent(patch.Id + DPEWebClient.REJECT_PATCH_SUFFIX));
new StringContent(patch.Id + DPEWebClient.REJECT_PATCH_SUFFIX));
Log.Verbose($"SuggestPatch: Patch { patch.Id } rejected. Return status is { Enum.GetName(typeof(HttpStatusCode), response.StatusCode) }");
} }
catch (Exception ex) catch (ArgumentNullException ex)
{ {
Log.Error("RejectPatch: Error rejecting patch. " + ex.ToString()); Log.ErrorAndFailInDebugMode("RejectPatch: Error rejecting patch. " + ex);
throw ex;
}
catch (HttpRequestException ex)
{
Log.Error("RejectPatch: Error rejecting patch. " + ex);
throw ex;
} }
} }
...@@ -39,20 +46,26 @@ namespace pEp.DPE ...@@ -39,20 +46,26 @@ namespace pEp.DPE
/// </summary> /// </summary>
/// <param name="patch">The patch to suggest.</param> /// <param name="patch">The patch to suggest.</param>
/// <param name="me">The own identity that suggests the patch.</param> /// <param name="me">The own identity that suggests the patch.</param>
public static async void SuggestPatch(Patch patch, PEPIdentity me) /// <exception cref="ArgumentNullException" />
/// <exception cref="HttpRequestException" />
/// <returns>The Http response.</returns>
public static async Task<HttpResponseMessage> SuggestPatch(Patch patch, PEPIdentity me)
{ {
// POST http://localhost:port/pEpDPE/patches/ // POST http://localhost:port/pEpDPE/patches/
string xml = patch.Serialize();
try try
{ {
HttpResponseMessage response = string xml = patch.Serialize();
await DPEWebClient.httpClient.PostAsync(DPEWebClient.DPE_POST_URL, return await DPEWebClient.httpClient.PostAsync(DPEWebClient.DPE_POST_URL, new StringContent(xml));
new StringContent(xml)); }
Log.Verbose($"SuggestPatch: New patch posted. Return status is { Enum.GetName(typeof(HttpStatusCode), response.StatusCode) }"); catch (ArgumentNullException ex)
{
Log.ErrorAndFailInDebugMode("SuggestPatch: Error suggesting patch. " + ex);
throw ex;
} }
catch (Exception ex) catch (HttpRequestException ex)
{ {
Log.Error("SuggestPatch: Error suggesting patch. " + ex.ToString()); Log.Error("SuggestPatch: Error suggesting patch. " + ex);
throw ex;
} }
} }
...@@ -61,19 +74,26 @@ namespace pEp.DPE ...@@ -61,19 +74,26 @@ namespace pEp.DPE
/// </summary> /// </summary>
/// <param name="patch">The patch to support.</param> /// <param name="patch">The patch to support.</param>
/// <param name="me">The own identity that supports the patch.</param> /// <param name="me">The own identity that supports the patch.</param>
public static async void SupportPatch(Patch patch, PEPIdentity me) /// <exception cref="ArgumentNullException" />
/// <exception cref="HttpRequestException" />
/// <returns>The Http response.</returns>
public static async Task<HttpResponseMessage> SupportPatch(Patch patch, PEPIdentity me)
{ {
// POST http://localhost:port/pEpDPE/patches/patch_id/support // POST http://localhost:port/pEpDPE/patches/patch_id/support
try try
{ {
HttpResponseMessage response = return await DPEWebClient.httpClient.PostAsync(DPEWebClient.DPE_POST_URL,
await DPEWebClient.httpClient.PostAsync(DPEWebClient.DPE_POST_URL, new StringContent(patch.Id + DPEWebClient.SUPPORT_PATCH_SUFFIX));
new StringContent(patch.Id + DPEWebClient.SUPPORT_PATCH_SUFFIX)); }
Log.Verbose($"SuggestPatch: Patch { patch.Id } supported. Return status is { Enum.GetName(typeof(HttpStatusCode), response.StatusCode) }"); catch (ArgumentNullException ex)
{
Log.ErrorAndFailInDebugMode("SupportPatch: Error supporting patch. " + ex);
throw ex;
} }
catch (Exception ex) catch (HttpRequestException ex)
{ {
Log.Error("SupportPatch: Error supporting patch. " + ex.ToString()); Log.Error("SupportPatch: Error supporting patch. " + ex);
throw ex;
} }
} }
} }
......
...@@ -3,6 +3,9 @@ using pEp.Extensions; ...@@ -3,6 +3,9 @@ using pEp.Extensions;
using System; using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Outlook = Microsoft.Office.Interop.Outlook; using Outlook = Microsoft.Office.Interop.Outlook;
namespace pEp.DPE namespace pEp.DPE
...@@ -14,6 +17,21 @@ namespace pEp.DPE ...@@ -14,6 +17,21 @@ namespace pEp.DPE
private const string PATCH_MESSAGE_SUBJECT = "Configuration changes"; private const string PATCH_MESSAGE_SUBJECT = "Configuration changes";
public const string DPE_MESSAGE_CLASS = "IPM.Note.DPE"; public const string DPE_MESSAGE_CLASS = "IPM.Note.DPE";
public enum PatchStatus
{
Open,
Accepted,
Supported,
Rejected
}
private enum PostAction
{
Reject,
Suggest,
Support
}
private readonly PEPIdentity ownIdentity; private readonly PEPIdentity ownIdentity;
private readonly PatchEvents patchEvents; private readonly PatchEvents patchEvents;
...@@ -56,6 +74,10 @@ namespace pEp.DPE ...@@ -56,6 +74,10 @@ namespace pEp.DPE
ThisAddIn.PEPEngine.ShowNotification("New patch suggested", patch.CommitMessage + " " + patch.Diff); ThisAddIn.PEPEngine.ShowNotification("New patch suggested", patch.CommitMessage + " " + patch.Diff);
} }
#endregion
#region Methods
/// <summary> /// <summary>
/// Creates a mail item that shows a suggested patch. /// Creates a mail item that shows a suggested patch.
/// </summary> /// </summary>
...@@ -88,6 +110,7 @@ namespace pEp.DPE ...@@ -88,6 +110,7 @@ namespace pEp.DPE
{ MapiProperty.PidTagMessageDeliveryTime, DateTime.UtcNow } { MapiProperty.PidTagMessageDeliveryTime, DateTime.UtcNow }
}); });
omi.SetMessageFlag(MapiPropertyValue.EnumPidTagMessageFlags.mfUnsent, false); omi.SetMessageFlag(MapiPropertyValue.EnumPidTagMessageFlags.mfUnsent, false);
omi.SetUserProperty(MailItemExtensions.USER_PROPERTY_KEY_DPE_PATCH_ID, patch.Id);
// Move to inbox and set Received time // Move to inbox and set Received time
mi = omi.Move(Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)); mi = omi.Move(Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox));
...@@ -104,10 +127,6 @@ namespace pEp.DPE ...@@ -104,10 +127,6 @@ namespace pEp.DPE
} }
} }
#endregion
#region Methods
/// <summary> /// <summary>
/// Disposes of all resources. /// Disposes of all resources.
/// </summary> /// </summary>
...@@ -117,17 +136,140 @@ namespace pEp.DPE ...@@ -117,17 +136,140 @@ namespace pEp.DPE
this.patchEvents.Dispose(); this.patchEvents.Dispose();
} }
/// <summary>
/// Gets the corresponding patch mail item.
/// </summary>
/// <param name="patch">The patch that this item belongs to.</param>
/// <returns>The patch mail item or null if not found.</returns>
public Outlook.MailItem GetPatchMailItem(Patch patch)
{
Outlook.Folder inbox = Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
Outlook.Items folderItems = inbox?.Items;
if (folderItems?.Count > 0)
{
Log.Verbose("GetPatchMailItem: " + folderItems.Count + " items found.");
try
{
// First filter out older items
Outlook.Items filteredInboxItems = folderItems.Restrict("[ReceivedTime] >= '" + patch.CreationDate.ToString("g") + "'");
Log.Verbose("GetPatchMailItem: Items since receival of patch: " + filteredInboxItems?.Count ?? "0");
Outlook.UserDefinedProperties up = inbox.UserDefinedProperties;
if (up.Find(MailItemExtensions.USER_PROPERTY_KEY_DPE_PATCH_ID) == null)
{
up.Add(MailItemExtensions.USER_PROPERTY_KEY_DPE_PATCH_ID, Outlook.OlUserPropertyType.olText);
}
// Now find the one with the respective patch id
string filter = string.Format("[{0}] = '{1}'", MailItemExtensions.USER_PROPERTY_KEY_DPE_PATCH_ID, patch.Id);
return filteredInboxItems.Find(filter) as Outlook.MailItem;
}
catch (Exception ex)
{
Log.Error("GetPatchMailItem: Error occured. " + ex);
}
}
else
{
Log.Verbose("GetPatchMailItem: No items were found or folder was null.");
}
return null;
}
/// <summary>
/// Performs the given post action.
/// </summary>
/// <param name="patch">The patch to post.</param>
/// <param name="me">The own identity.</param>
/// <param name="postAction">The POST action to perform.</param>
/// <returns></returns>
private async Task<Exception> PostAsync(Patch patch, PEPIdentity me, PostAction postAction)
{
// Determine the correct method to execute
Task<HttpResponseMessage> postTask = null;
switch (postAction)
{
case PostAction.Reject:
postTask = DPEWebClient.RejectPatch(patch, me);
break;
case PostAction.Suggest:
postTask = DPEWebClient.SuggestPatch(patch, me);
break;
case PostAction.Support:
postTask = DPEWebClient.SupportPatch(patch, me);
break;
default:
Log.ErrorAndFailInDebugMode("PostAsync: Unknown post action.");
break;
}
// Execute the task and return status
return await postTask.ContinueWith((task) =>
{
if (task.Exception != null)
{
Log.Error("ExecuteAsync: Error executing POST of type " + Enum.GetName(typeof(PostAction), postAction));
return task.Exception;
}
else
{
return (task.Result.StatusCode != HttpStatusCode.OK) ?
new Exception(Enum.GetName(typeof(HttpStatusCode), task.Result.StatusCode)) :
null;
}
});
}
/// <summary> /// <summary>
/// Rejects a given patch. /// Rejects a given patch.
/// </summary> /// </summary>
/// <param name="patch">The patch to reject.</param> /// <param name="patch">The patch to reject.</param>
/// <param name="me">The own identity that rejects the patch.</param> /// <param name="me">The own identity that rejects the patch.</param>
public void Reject(Patch patch, PEPIdentity me) public async Task Reject(Patch patch, PEPIdentity me)
{ {
DPEWebClient.RejectPatch(patch, me); if (await this.PostAsync(patch, me, PostAction.Reject) is Exception ex)
{
throw ex;
}
this.UpdatePatchMailItem(patch, PatchStatus.Rejected);
AdapterExtensions.ShowNotification("Patch rejected", patch.CommitMessage); AdapterExtensions.ShowNotification("Patch rejected", patch.CommitMessage);
} }
/// <summary>
/// Updates the patch mail item after a successful change in the patch status.
/// </summary>
/// <param name="patch">The corresponding patch.</param>
/// <param name="patchStatus">The new patch status.</param>
/// <returns>True if the mail item was updated correctly, otherwise false.</returns>
private bool UpdatePatchMailItem(Patch patch, PatchStatus patchStatus)
{
Outlook.MailItem omi = null;
try
{
omi = this.GetPatchMailItem(patch);
// Update the user properties
if (omi != null)
{
omi.SetUserProperty(MailItemExtensions.USER_PROPERTY_KEY_DPE_PATCH_STATUS, (int)patchStatus);
omi.SetUserProperty(MailItemExtensions.USER_PROPERTY_KEY_DPE_PATCH_EDIT_DATE, DateTime.UtcNow);
omi.Save();
}
}
catch (Exception ex)
{
Log.Error("UpdatePatchMailItem: Error occured. " + ex);
return false;
}
return true;
}
/// <summary> /// <summary>
/// Subscribes to the given patch events. /// Subscribes to the given patch events.
/// </summary> /// </summary>
...@@ -144,9 +286,13 @@ namespace pEp.DPE ...@@ -144,9 +286,13 @@ namespace pEp.DPE
/// </summary> /// </summary>
/// <param name="patch">The patch to suggest.</param> /// <param name="patch">The patch to suggest.</param>
/// <param name="me">The own identity that suggests the patch.</param> /// <param name="me">The own identity that suggests the patch.</param>
public void Suggest(Patch patch, PEPIdentity me) public async Task Suggest(Patch patch, PEPIdentity me)
{ {
DPEWebClient.SuggestPatch(patch, me); if (await this.PostAsync(patch, me, PostAction.Suggest) is Exception ex)
{
throw ex;
}
AdapterExtensions.ShowNotification("Patch suggested", patch.CommitMessage); AdapterExtensions.ShowNotification("Patch suggested", patch.CommitMessage);
} }
...@@ -155,9 +301,14 @@ namespace pEp.DPE ...@@ -155,9 +301,14 @@ namespace pEp.DPE
/// </summary> /// </summary>
/// <param name="patch">The patch to support.</param> /// <param name="patch">The patch to support.</param>
/// <param name="me">The own identity that supports the patch.</param> /// <param name="me">The own identity that supports the patch.</param>
public void Support(Patch patch, PEPIdentity me) public async Task Support(Patch patch, PEPIdentity me)
{ {
DPEWebClient.SupportPatch(patch, me); if (await this.PostAsync(patch, me, PostAction.Support) is Exception ex)
{
throw ex;
}
this.UpdatePatchMailItem(patch, PatchStatus.Supported);
AdapterExtensions.ShowNotification("Patch supported", patch.CommitMessage); AdapterExtensions.ShowNotification("Patch supported", patch.CommitMessage);
} }
......
using System; using System;
using System.Threading.Tasks;
namespace pEp.DPE.Interfaces namespace pEp.DPE.Interfaces
{ {
...@@ -6,8 +7,8 @@ namespace pEp.DPE.Interfaces ...@@ -6,8 +7,8 @@ namespace pEp.DPE.Interfaces
{ {
void Subscribe(PatchEvents patchEvents); void Subscribe(PatchEvents patchEvents);
void Unsubscribe(PatchEvents patchEvents); void Unsubscribe(PatchEvents patchEvents);
void Suggest(Patch patch, PEPIdentity me); Task Suggest(Patch patch, PEPIdentity me);
void Support(Patch patch, PEPIdentity me); Task Support(Patch patch, PEPIdentity me);
void Reject(Patch patch, PEPIdentity me); Task Reject(Patch patch, PEPIdentity me);
} }
} }
...@@ -19,6 +19,7 @@ namespace pEp ...@@ -19,6 +19,7 @@ namespace pEp
{ {
public const string USER_PROPERTY_KEY_DPE_PATCH_STATUS = "patchStatus"; public const string USER_PROPERTY_KEY_DPE_PATCH_STATUS = "patchStatus";
public const string USER_PROPERTY_KEY_DPE_PATCH_EDIT_DATE = "patchEditDate"; public const string USER_PROPERTY_KEY_DPE_PATCH_EDIT_DATE = "patchEditDate";
public const string USER_PROPERTY_KEY_DPE_PATCH_ID = "patchId";
public const string USER_PROPERTY_KEY_INSPECTOR_CLOSED = "inspectorClosed"; public const string USER_PROPERTY_KEY_INSPECTOR_CLOSED = "inspectorClosed";
public const string USER_PROPERTY_KEY_IS_INCOMING = "isIncoming"; public const string USER_PROPERTY_KEY_IS_INCOMING = "isIncoming";
public const string USER_PROPERTY_KEY_IS_MIRROR = "isMirror"; public const string USER_PROPERTY_KEY_IS_MIRROR = "isMirror";
......
using pEp.DPE; using pEp.DPE;
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
...@@ -36,12 +35,12 @@ namespace pEp ...@@ -36,12 +35,12 @@ namespace pEp
string xml = omi.HTMLBody; string xml = omi.HTMLBody;
Patch patch = Patch.Deserialize(xml); Patch patch = Patch.Deserialize(xml);
FormControlPatchViewModel.PatchStatus patchStatus = FormControlPatchViewModel.PatchStatus.Open; DistributedPolicyEngine.PatchStatus patchStatus = DistributedPolicyEngine.PatchStatus.Open;
DateTime? editDate = null; DateTime? editDate = null;
// Get patch status if available // Get patch status if available
if ((omi.GetUserProperty(MailItemExtensions.USER_PROPERTY_KEY_DPE_PATCH_STATUS, FormControlPatchViewModel.PatchStatus.Open) is string patchStatusString) && if ((omi.GetUserProperty(MailItemExtensions.USER_PROPERTY_KEY_DPE_PATCH_STATUS, DistributedPolicyEngine.PatchStatus.Open) is string patchStatusString) &&
Enum.TryParse(patchStatusString, out FormControlPatchViewModel.PatchStatus status)) Enum.TryParse(patchStatusString, out DistributedPolicyEngine.PatchStatus status))
{ {
patchStatus = status; patchStatus = status;
} }
...@@ -53,16 +52,10 @@ namespace pEp ...@@ -53,16 +52,10 @@ namespace pEp
editDate = savedEditDate; editDate = savedEditDate;
} }
// Get the patch submitter // Get the patch submitter and set data context
if (PEPIdentity.GetFromIdentity(omi, out PEPIdentity submitter) == Globals.ReturnStatus.Success) if (PEPIdentity.GetFromIdentity(omi, out PEPIdentity submitter) == Globals.ReturnStatus.Success)
{ {
this.FormControlPatchView.DataContext = new FormControlPatchViewModel(patch, submitter, patchStatus, editDate); 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 else
{ {
...@@ -79,55 +72,11 @@ namespace pEp ...@@ -79,55 +72,11 @@ 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 pEp.UI.Models;
using System; using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents; using System.Windows.Documents;
using System.Windows.Media; using System.Windows.Media;
...@@ -7,23 +10,15 @@ namespace pEp.UI.ViewModels ...@@ -7,23 +10,15 @@ 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 Brush _Background = Brushes.White; private Brush _Background = Brushes.White;
private FlowDocument _DisplayDiff = null; private FlowDocument _DisplayDiff = null;
private DateTime? _EditDate = null; private DateTime? _EditDate = null;
private string _Explanation = null; private string _Explanation = null;
private PatchStatus _Status = PatchStatus.Open; private DistributedPolicyEngine.PatchStatus _Status = DistributedPolicyEngine.PatchStatus.Open;
#endregion #endregion
...@@ -93,7 +88,7 @@ namespace pEp.UI.ViewModels ...@@ -93,7 +88,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, p => (this.Status == PatchStatus.Open)); public RelayCommand OKButtonCommand => new RelayCommand(this.SupportPatch, p => (this.Status == DistributedPolicyEngine.PatchStatus.Open));
/// <summary> /// <summary>
/// Gets the OK button text. /// Gets the OK button text.
...@@ -103,7 +98,7 @@ namespace pEp.UI.ViewModels ...@@ -103,7 +98,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, p => (this.Status == PatchStatus.Open)); public RelayCommand RejectButtonCommand => new RelayCommand(this.RejectPatch, p => (this.Status == DistributedPolicyEngine.PatchStatus.Open));
/// <summary> /// <summary>
/// Gets the Reject button text. /// Gets the Reject button text.
...@@ -118,7 +113,7 @@ namespace pEp.UI.ViewModels ...@@ -118,7 +113,7 @@ namespace pEp.UI.ViewModels
/// <summary> /// <summary>
/// Gets or sets the status of this patch. /// Gets or sets the status of this patch.
/// </summary> /// </summary>
public PatchStatus Status { get => this._Status; set => this.SetProperty(ref this._Status, value); } public DistributedPolicyEngine.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.
...@@ -157,7 +152,7 @@ namespace pEp.UI.ViewModels ...@@ -157,7 +152,7 @@ namespace pEp.UI.ViewModels
/// <param name="submitter">The submitter of the patch.</param> /// <param name="submitter">The submitter of the patch.</param>
/// <param name="status">The status of the patch.</param> /// <param name="status">The status of the patch.</param>
/// <param name="editDate">The last time this patch was edited (accepted/supported/rejected).</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) public FormControlPatchViewModel(Patch patch, PEPIdentity submitter, DistributedPolicyEngine.PatchStatus status = DistributedPolicyEngine.PatchStatus.Open, DateTime? editDate = null)
{ {
this.EditDate = editDate; this.EditDate = editDate;
this.patch = patch; this.patch = patch;
...@@ -175,10 +170,24 @@ namespace pEp.UI.ViewModels ...@@ -175,10 +170,24 @@ namespace pEp.UI.ViewModels
/// Rejects this patch. /// Rejects this patch.
/// </summary> /// </summary>
/// <param name="parameter">The command parameter.</param> /// <param name="parameter">The command parameter.</param>
private void RejectPatch(object parameter) private async void RejectPatch(object parameter)
{ {
Globals.ThisAddIn.DistributedPolicyEngine.Reject(this.patch, new PEPIdentity()); try
this.UpdateView(PatchStatus.Rejected); {
await Globals.ThisAddIn.DistributedPolicyEngine.Reject(this.patch, new PEPIdentity());
}
catch (Exception ex)
{
while (ex.InnerException != null)
{
ex = ex.InnerException;
}
CustomMessageBox.ShowDialog(ex.Message, "Error", "OK");
return;
}
this.UpdateView(DistributedPolicyEngine.PatchStatus.Rejected);
} }
/// <summary> /// <summary>
...@@ -194,16 +203,16 @@ namespace pEp.UI.ViewModels ...@@ -194,16 +203,16 @@ namespace pEp.UI.ViewModels
switch (this.Status) switch (this.Status)
{ {
case PatchStatus.Accepted: case DistributedPolicyEngine.PatchStatus.Accepted:
this.Explanation = $"Accepted on { editDate }"; this.Explanation = $"Accepted on { editDate }";
break; break;
case PatchStatus.Supported: case DistributedPolicyEngine.PatchStatus.Supported:
this.Explanation = $"Supported on { editDate }"; this.Explanation = $"Supported on { editDate }";
break; break;
case PatchStatus.Rejected: case DistributedPolicyEngine.PatchStatus.Rejected:
this.Explanation = $"Rejected on { editDate }"; this.Explanation = $"Rejected on { editDate }";
break; break;
case PatchStatus.Open: case DistributedPolicyEngine.PatchStatus.Open:
default: default:
this.Explanation = "New configuration changes pending approval"; this.Explanation = "New configuration changes pending approval";
break; break;
...@@ -214,23 +223,39 @@ namespace pEp.UI.ViewModels ...@@ -214,23 +223,39 @@ namespace pEp.UI.ViewModels
/// Supports this patch. /// Supports this patch.
/// </summary> /// </summary>
/// <param name="parameter">The command parameter.</param> /// <param name="parameter">The command parameter.</param>
private void SupportPatch(object parameter) private async void SupportPatch(object parameter)
{ {
Globals.ThisAddIn.DistributedPolicyEngine.Support(this.patch, new PEPIdentity()); try
this.UpdateView(PatchStatus.Supported); {
await Globals.ThisAddIn.DistributedPolicyEngine.Support(this.patch, new PEPIdentity());
}
catch (Exception ex)
{
Log.Error("SupportPatch: Error occured. " + ex);
while (ex.InnerException != null)
{
ex = ex.InnerException;
}
CustomMessageBox.ShowDialog(ex.Message, "Error", "OK");
return;
}
this.UpdateView(DistributedPolicyEngine.PatchStatus.Supported);
} }
/// <summary> /// <summary>
/// Updates the view according to a new patch status. /// Updates the view according to a new patch status.
/// </summary> /// </summary>
/// <param name="patchStatus">The new patch status.</param> /// <param name="patchStatus">The new patch status.</param>
private void UpdateView(PatchStatus patchStatus) private void UpdateView(DistributedPolicyEngine.PatchStatus patchStatus)
{ {
this.Status = patchStatus; this.Status = patchStatus;
this.EditDate = DateTime.UtcNow; this.EditDate = DateTime.UtcNow;
this.SetExplanation(); this.SetExplanation();
this.DisplayDiff = PatchDialogViewModel.FormatDiff(this.Diff, this.Status == PatchStatus.Open); this.DisplayDiff = PatchDialogViewModel.FormatDiff(this.Diff, this.Status == DistributedPolicyEngine.PatchStatus.Open);
this.Background = (patchStatus == PatchStatus.Open) ? Brushes.White : Brushes.WhiteSmoke; this.Background = (patchStatus == DistributedPolicyEngine.PatchStatus.Open) ? Brushes.White : Brushes.WhiteSmoke;
} }
#endregion #endregion
......
...@@ -621,7 +621,6 @@ namespace pEp.UI.ViewModels ...@@ -621,7 +621,6 @@ namespace pEp.UI.ViewModels
#region Static methods #region Static methods
/// <summary> /// <summary>
/// Formats the diff displaying colors of changes. /// Formats the diff displaying colors of changes.
/// </summary> /// </summary>
......
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