Commit d3d2dd8c authored by Thomas's avatar Thomas
Browse files

Add method to create Patch mail item and set Received time

parent 336dd68c
using pEp.DPE.Interfaces; using pEp.DPE.Interfaces;
using pEp.Extensions;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace pEp.DPE namespace pEp.DPE
{ {
internal class DistributedPolicyEngine : IDistributedPolicyEngine internal class DistributedPolicyEngine : IDistributedPolicyEngine
{ {
private readonly static string DPE_FOLDER = Path.Combine(Globals.PEPUserFolder, "DPE"); private readonly static string DPE_FOLDER = Path.Combine(Globals.PEPUserFolder, "DPE");
private readonly static string PATCH_EXTENSION = ".patch"; private const string PATCH_EXTENSION = ".patch";
private const string PATCH_MESSAGE_SUBJECT = "Configuration changes";
public const string DPE_MESSAGE_CLASS = "IPM.Note.DPE";
private readonly PEPIdentity ownIdentity;
private readonly PatchEvents patchEvents; private readonly PatchEvents patchEvents;
/// <summary> /// <summary>
...@@ -19,10 +25,11 @@ namespace pEp.DPE ...@@ -19,10 +25,11 @@ namespace pEp.DPE
{ {
this.patchEvents = new PatchEvents(); this.patchEvents = new PatchEvents();
this.Subscribe(this.patchEvents); this.Subscribe(this.patchEvents);
this.ownIdentity = new PEPIdentity(Globals.ThisAddIn.Settings.AccountSettingsList.First().SmtpAddress);
} }
#region Event handlers #region Event handlers
/// <summary> /// <summary>
/// Event handler for when a patch has been accepted. /// Event handler for when a patch has been accepted.
/// </summary> /// </summary>
...@@ -46,10 +53,58 @@ namespace pEp.DPE ...@@ -46,10 +53,58 @@ namespace pEp.DPE
{ {
// Save the patch and show a notification // Save the patch and show a notification
Patch patch = e.Patch; Patch patch = e.Patch;
this.SavePatch(patch); this.CreatePatchMailItem(patch, e.Submitter);
ThisAddIn.PEPEngine.ShowNotification("New patch suggested", patch.CommitMessage + " " + patch.Diff); ThisAddIn.PEPEngine.ShowNotification("New patch suggested", patch.CommitMessage + " " + patch.Diff);
} }
/// <summary>
/// Creates a mail item that shows a suggested patch.
/// </summary>
/// <param name="patch">The patch to show.</param>
/// <param name="submitter">The submitter of the patch.</param>
private void CreatePatchMailItem(Patch patch, PEPIdentity submitter)
{
Outlook.MailItem omi = null;
Outlook.MailItem mi = null;
try
{
// Use a PEPMessage to define the message to create
PEPMessage patchMessage = new PEPMessage
{
From = submitter,
LongMsgFormattedHtml = patch.Serialize(),
ShortMsg = DistributedPolicyEngine.PATCH_MESSAGE_SUBJECT,
};
patchMessage.To.Add(this.ownIdentity);
// Create the mail item and apply the PEPMessage
omi = Globals.ThisAddIn.Application.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
patchMessage.ApplyTo(omi, false, true, true);
// Add custom message class, set time, and mark as Sent (so that it won't appear as draft)
MapiHelper.SetProperties(omi, new MAPIProperties
{
{ MapiProperty.PidTagMessageClass, DistributedPolicyEngine.DPE_MESSAGE_CLASS },
{ MapiProperty.PidTagMessageDeliveryTime, DateTime.UtcNow }
});
omi.SetMessageFlag(MapiPropertyValue.EnumPidTagMessageFlags.mfUnsent, false);
// Move to inbox and set Received time
mi = omi.Move(Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox));
mi.Save();
}
catch (Exception ex)
{
Log.Error("CreatePatchMailItem: Error creating patch mail item. " + ex);
}
finally
{
omi = null;
mi = null;
}
}
#endregion #endregion
#region Methods #region Methods
...@@ -115,9 +170,9 @@ namespace pEp.DPE ...@@ -115,9 +170,9 @@ namespace pEp.DPE
/// <param name="patchEvents">The patch events to unsubscribe from.</param> /// <param name="patchEvents">The patch events to unsubscribe from.</param>
public void Unsubscribe(PatchEvents patchEvents) public void Unsubscribe(PatchEvents patchEvents)
{ {
patchEvents.PatchAccepted += PatchEvents_PatchAccepted; patchEvents.PatchAccepted -= PatchEvents_PatchAccepted;
patchEvents.PatchRejected += PatchEvents_PatchRejected; patchEvents.PatchRejected -= PatchEvents_PatchRejected;
patchEvents.PatchSuggested += PatchEvents_PatchSuggested; patchEvents.PatchSuggested -= PatchEvents_PatchSuggested;
} }
/// <summary> /// <summary>
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
{ {
internal interface IPatchEvents internal interface IPatchEvents
{ {
void Suggested(Patch patch); void Suggested(Patch patch, PEPIdentity submitter);
void Rejected(Patch patch, Patch.RejectReason rejectReason); void Rejected(Patch patch, Patch.RejectReason rejectReason);
void Accepted(Patch patch); void Accepted(Patch patch);
} }
......
...@@ -4,7 +4,8 @@ namespace pEp.DPE ...@@ -4,7 +4,8 @@ namespace pEp.DPE
{ {
public class PatchEventArgs : EventArgs public class PatchEventArgs : EventArgs
{ {
public Patch Patch { get; set; } public Patch Patch { get; set; }
public Patch.RejectReason Reason { get; set; } = Patch.RejectReason.None; internal PEPIdentity Submitter { get; set; }
public Patch.RejectReason Reason { get; set; } = Patch.RejectReason.None;
} }
} }
...@@ -33,7 +33,7 @@ namespace pEp.DPE ...@@ -33,7 +33,7 @@ namespace pEp.DPE
Patch patch; Patch patch;
if ((patch = Patch.Deserialize(e.Request)) != null) if ((patch = Patch.Deserialize(e.Request)) != null)
{ {
this.Suggested(patch); this.Suggested(patch, new PEPIdentity("patchadmin@pep.security"));
} }
} }
...@@ -72,9 +72,9 @@ namespace pEp.DPE ...@@ -72,9 +72,9 @@ namespace pEp.DPE
/// Raises the Patch suggested event. /// Raises the Patch suggested event.
/// </summary> /// </summary>
/// <param name="patch">The patch that has been suggested.</param> /// <param name="patch">The patch that has been suggested.</param>
public void Suggested(Patch patch) public void Suggested(Patch patch, PEPIdentity submitter)
{ {
this.PatchSuggested?.Invoke(this, new PatchEventArgs { Patch = patch }); this.PatchSuggested?.Invoke(this, new PatchEventArgs { Patch = patch, Submitter = submitter });
} }
#endregion #endregion
......
...@@ -578,6 +578,8 @@ namespace pEp ...@@ -578,6 +578,8 @@ namespace pEp
[FieldOffset(0)] [FieldOffset(0)]
public double at; public double at;
[FieldOffset(0)] [FieldOffset(0)]
public System.Runtime.InteropServices.ComTypes.FILETIME ft;
[FieldOffset(0)]
public IntPtr lpszA; public IntPtr lpszA;
[FieldOffset(0)] [FieldOffset(0)]
public IntPtr lpszW; public IntPtr lpszW;
...@@ -1455,6 +1457,23 @@ namespace pEp ...@@ -1455,6 +1457,23 @@ namespace pEp
} }
} }
break; break;
case MapiProperty.MapiDataType.PtypTime:
{
try
{
long fileTime = ((DateTime)value).ToFileTimeUtc();
sPropValue.Value.ft = new System.Runtime.InteropServices.ComTypes.FILETIME
{
dwLowDateTime = (int)(fileTime & 0xFFFFFFFF),
dwHighDateTime = (int)(fileTime >> 32)
};
}
catch (Exception ex)
{
throw new Exception(string.Format("Error converting to Filetime. Property tag: {0}. Value: {1}. Exception: {2}.", property.DaslName, value?.ToString(), ex.ToString()));
}
}
break;
default: default:
{ {
throw new Exception(string.Format("Error creating SPropValue. Data type {0} not supported.", Enum.GetName(typeof(MapiProperty.MapiDataType), property.DataType))); throw new Exception(string.Format("Error creating SPropValue. Data type {0} not supported.", Enum.GetName(typeof(MapiProperty.MapiDataType), property.DataType)));
...@@ -1769,6 +1788,23 @@ namespace pEp ...@@ -1769,6 +1788,23 @@ namespace pEp
} }
} }
break; break;
case MapiProperty.MapiDataType.PtypTime:
{
try
{
long fileTime = ((DateTime)value).ToFileTimeUtc();
sPropValue.Value.ft = new System.Runtime.InteropServices.ComTypes.FILETIME
{
dwLowDateTime = (int)(fileTime & 0xFFFFFFFF),
dwHighDateTime = (int)(fileTime >> 32)
};
}
catch (Exception ex)
{
throw new Exception(string.Format("Error converting to Filetime. Property tag: {0}. Value: {1}. Exception: {2}.", mapiProperty.DaslName, value?.ToString(), ex.ToString()));
}
}
break;
default: default:
{ {
throw new Exception(string.Format("Error creating SPropValue. Data type {0} not supported.", Enum.GetName(typeof(MapiProperty.MapiDataType), mapiProperty.DataType))); throw new Exception(string.Format("Error creating SPropValue. Data type {0} not supported.", Enum.GetName(typeof(MapiProperty.MapiDataType), mapiProperty.DataType)));
......
...@@ -10,7 +10,7 @@ namespace pEp ...@@ -10,7 +10,7 @@ namespace pEp
{ {
#region Form Region Factory #region Form Region Factory
[Microsoft.Office.Tools.Outlook.FormRegionMessageClass("IPM.Note.DPE")] [Microsoft.Office.Tools.Outlook.FormRegionMessageClass(DistributedPolicyEngine.DPE_MESSAGE_CLASS)]
[Microsoft.Office.Tools.Outlook.FormRegionName("pEpForOutlook.FormRegionDPE")] [Microsoft.Office.Tools.Outlook.FormRegionName("pEpForOutlook.FormRegionDPE")]
public partial class FormRegionDPEFactory public partial class FormRegionDPEFactory
{ {
......
...@@ -283,7 +283,7 @@ namespace pEp.UI.ViewModels ...@@ -283,7 +283,7 @@ namespace pEp.UI.ViewModels
{ {
FontFamily = new FontFamily("Courier New"), FontFamily = new FontFamily("Courier New"),
FontSize = 12.0, FontSize = 12.0,
Background = Brushes.White, Background = Brushes.White
}; };
string[] lines = this.Diff?.Replace("\r\n", "\n")?.Split('\n') ?? new string[] { }; string[] lines = this.Diff?.Replace("\r\n", "\n")?.Split('\n') ?? new string[] { };
......
...@@ -50,12 +50,12 @@ ...@@ -50,12 +50,12 @@
Grid.Row="1" Grid.Row="1"
Grid.ColumnSpan="2" Grid.ColumnSpan="2"
Content="{Binding Submitter.DisplayString}" Content="{Binding Submitter.DisplayString}"
Margin="10,2" /> Margin="10,1" />
<Label Grid.Column="0" <Label Grid.Column="0"
Grid.Row="2" Grid.Row="2"
Grid.ColumnSpan="2" Grid.ColumnSpan="2"
Content="{Binding CreationDateString}" Content="{Binding CreationDateString}"
Margin="10,2" /> Margin="10,1" />
<Image Grid.Column="2" <Image Grid.Column="2"
Grid.Row="0" Grid.Row="0"
Grid.RowSpan="3" Grid.RowSpan="3"
...@@ -85,8 +85,6 @@ ...@@ -85,8 +85,6 @@
IsReadOnly="{Binding IsEditable, Converter={StaticResource InvertBool}}" IsReadOnly="{Binding IsEditable, Converter={StaticResource InvertBool}}"
IsValidInput="{Binding IsDiffValid}" IsValidInput="{Binding IsDiffValid}"
Placeholder="Please add diff" Placeholder="Please add diff"
Height="400"
Width="600"
Margin="5,10,10,10" Margin="5,10,10,10"
Visibility="{Binding IsEditable, Converter={StaticResource BoolToVisibility}}"/> Visibility="{Binding IsEditable, Converter={StaticResource BoolToVisibility}}"/>
<FlowDocumentScrollViewer Grid.Column="1" <FlowDocumentScrollViewer Grid.Column="1"
...@@ -94,8 +92,6 @@ ...@@ -94,8 +92,6 @@
Grid.Row="3" Grid.Row="3"
BorderBrush="Black" BorderBrush="Black"
BorderThickness="1" BorderThickness="1"
MinHeight="400"
MinWidth="600"
Margin="5,10,10,10" Margin="5,10,10,10"
Document="{Binding DisplayDiff}" Document="{Binding DisplayDiff}"
Visibility="{Binding IsEditable, Converter={StaticResource InvertBoolToVisibility}}"/> Visibility="{Binding IsEditable, Converter={StaticResource InvertBoolToVisibility}}"/>
......
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