Commit 06436df5 authored by Thomas's avatar Thomas
Browse files

Add FormRegion to show patches

parent 56a7ec98
...@@ -35,9 +35,9 @@ namespace pEp ...@@ -35,9 +35,9 @@ namespace pEp
/// </summary> /// </summary>
private static void InitializeManifest(Microsoft.Office.Tools.Outlook.FormRegionManifest manifest, Microsoft.Office.Tools.Outlook.Factory factory) private static void InitializeManifest(Microsoft.Office.Tools.Outlook.FormRegionManifest manifest, Microsoft.Office.Tools.Outlook.Factory factory)
{ {
manifest.FormRegionName = "FormRegionDPE";
manifest.FormRegionType = Microsoft.Office.Tools.Outlook.FormRegionType.Replacement; manifest.FormRegionType = Microsoft.Office.Tools.Outlook.FormRegionType.Replacement;
manifest.Title = "FormRegionDPE"; manifest.Title = "FormRegionDPE";
manifest.FormRegionName = "FormRegionDPE";
} }
...@@ -51,14 +51,36 @@ namespace pEp ...@@ -51,14 +51,36 @@ namespace pEp
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
components = new System.ComponentModel.Container(); this.ElementHostFormControl = new System.Windows.Forms.Integration.ElementHost();
this.FormControlPatchView = new pEp.UI.Views.FormControlPatchView();
this.SuspendLayout();
//
// ElementHostFormControl
//
this.ElementHostFormControl.Dock = System.Windows.Forms.DockStyle.Fill;
this.ElementHostFormControl.Location = new System.Drawing.Point(0, 0);
this.ElementHostFormControl.Name = "ElementHostFormControl";
this.ElementHostFormControl.Size = new System.Drawing.Size(500, 350);
this.ElementHostFormControl.TabIndex = 0;
this.ElementHostFormControl.Child = this.FormControlPatchView;
//
// PatchView
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.ElementHostFormControl);
this.Name = "PatchView";
this.Size = new System.Drawing.Size(500, 350);
this.FormRegionShowing += new System.EventHandler(this.FormRegionDPE_FormRegionShowing); this.FormRegionShowing += new System.EventHandler(this.FormRegionDPE_FormRegionShowing);
this.FormRegionClosed += new System.EventHandler(this.FormRegionDPE_FormRegionClosed); this.FormRegionClosed += new System.EventHandler(this.FormRegionDPE_FormRegionClosed);
this.ResumeLayout(false);
} }
#endregion #endregion
private System.Windows.Forms.Integration.ElementHost ElementHostFormControl;
private UI.Views.FormControlPatchView FormControlPatchView;
public partial class FormRegionDPEFactory : Microsoft.Office.Tools.Outlook.IFormRegionFactory public partial class FormRegionDPEFactory : Microsoft.Office.Tools.Outlook.IFormRegionFactory
{ {
public event Microsoft.Office.Tools.Outlook.FormRegionInitializingEventHandler FormRegionInitializing; public event Microsoft.Office.Tools.Outlook.FormRegionInitializingEventHandler FormRegionInitializing;
......
using System; using pEp.DPE;
using pEp.UI.Models;
using pEp.UI.ViewModels;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
...@@ -28,14 +31,31 @@ namespace pEp ...@@ -28,14 +31,31 @@ namespace pEp
// Occurs before the form region is displayed. // Occurs before the form region is displayed.
// 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_FormRegionShowing(object sender, System.EventArgs e) private void FormRegionDPE_FormRegionShowing(object sender, EventArgs e)
{ {
Outlook.MailItem omi = null;
try
{
omi = this.OutlookItem as Outlook.MailItem;
string xml = omi.HTMLBody;
Patch patch = Patch.Deserialize(xml);
FormControlPatchViewModel formControlPatchViewModel = new FormControlPatchViewModel(patch, PatchDialog.PatchAction.SupportOrRejectPatch);
this.FormControlPatchView.DataContext = formControlPatchViewModel;
}
catch (Exception ex)
{
Log.Error("FormRegionDPE_FormRegionShowing: Error getting Outlook item. " + 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, System.EventArgs e) private void FormRegionDPE_FormRegionClosed(object sender, EventArgs e)
{ {
} }
} }
......
using pEp.DPE;
using pEp.UI.Models;
using System;
using System.Windows.Documents;
using System.Windows.Media;
namespace pEp.UI.ViewModels
{
internal class FormControlPatchViewModel : ViewModelBase
{
#region Fields
private Patch patch;
private FlowDocument _DisplayDiff = null;
private bool _IsCommitMessageValid = false;
private bool _IsDiffValid = false;
private bool _IsUriValid = false;
private bool _IsValid = false;
private RelayCommand _LoadFromFileCommand = null;
#endregion
#region Properties
/// <summary>
/// The commit message of the patch.
/// </summary>
public string CommitMessage
{
get => this.patch?.CommitMessage;
set
{
this.patch.CommitMessage = value;
this.ValidatePatch();
this.OnPropertyChanged();
}
}
/// <summary>
/// The diff of this patch.
/// </summary>
public string Diff
{
get => this.patch?.Diff;
set
{
this.patch.Diff = value;
this.ValidatePatch();
this.OnPropertyChanged();
}
}
/// <summary>
/// Gets the diff of this patch as formatted flow document.
/// </summary>
public FlowDocument DisplayDiff { get => this._DisplayDiff; set => SetProperty(ref this._DisplayDiff, value); }
/// <summary>
/// The explanation shown in the UI regarding this patch.
/// </summary>
public string Explanation { get; }
/// <summary>
/// Gets whether the Cancel button is visible.
/// </summary>
public bool IsCancelButtonVisible { get; } = true;
/// <summary>
/// Gets or sets whether the commit message is valid.
/// </summary>
public bool IsCommitMessageValid
{
get => this._IsCommitMessageValid;
set
{
if (value != this._IsCommitMessageValid)
{
this._IsCommitMessageValid = value;
this.OnPropertyChanged();
}
}
}
/// <summary>
/// Gets or sets whether the diff is valid.
/// </summary>
public bool IsDiffValid
{
get => this._IsDiffValid;
set
{
if (value != this._IsDiffValid)
{
this._IsDiffValid = value;
this.OnPropertyChanged();
}
}
}
/// <summary>
/// Gets whether the patch in this dialog is editable.
/// </summary>
public bool IsEditable { get; } = false;
/// <summary>
/// Gets whether the OK button is visible.
/// </summary>
public bool IsOKButtonVisible { get; } = true;
/// <summary>
/// Gets whether the Reject button is visible.
/// </summary>
public bool IsRejectButtonVisible { get; } = false;
/// <summary>
/// Gets or sets whether the URI is valid.
/// </summary>
public bool IsUriValid
{
get => this._IsUriValid;
set
{
if (value != this._IsUriValid)
{
this._IsUriValid = value;
this.OnPropertyChanged();
}
}
}
/// <summary>
/// Gets or sets whether this patch is valid.
/// </summary>
public bool IsValid
{
get => this._IsValid;
set
{
if (value != this._IsValid)
{
this._IsValid = value;
this.OnPropertyChanged();
}
}
}
/// <summary>
/// Gets the command to load the diff from file.
/// </summary>
public RelayCommand LoadFromFileCommand
{
get
{
if (this._LoadFromFileCommand == null)
{
this._LoadFromFileCommand = new RelayCommand(LoadFromFile);
}
return this._LoadFromFileCommand;
}
}
/// <summary>
/// The command to accept the patch dialog.
/// </summary>
public RelayCommand OKButtonCommand => new RelayCommand(this.SupportPatch, p => this.IsValid);
/// <summary>
/// Gets the OK button text.
/// </summary>
public string OKButtonText { get; } = Properties.Resources.Options_OKText;
/// <summary>
/// The command to reject the dialog.
/// </summary>
public RelayCommand RejectButtonCommand => new RelayCommand(this.RejectPatch);
/// <summary>
/// Gets the Reject button text.
/// </summary>
public string RejectButtonText { get; } = Properties.Resources.SyncWizard_RejectButton;
/// <summary>
/// Gets or sets the tag of the patch.
/// </summary>
public string Tag
{
get => this.patch?.Tag;
set
{
this.patch.Tag = value;
this.OnPropertyChanged();
}
}
/// <summary>
/// Gets or sets the URI of the patch.
/// </summary>
public string Uri
{
get => this.patch?.Uri;
set
{
this.patch.Uri = value;
this.ValidatePatch();
this.OnPropertyChanged();
}
}
#endregion
#region Constructors
public FormControlPatchViewModel(Patch patch, PatchDialog.PatchAction patchAction, bool isEditable = false)
{
this.patch = patch;
this.IsEditable = isEditable;
switch (patchAction)
{
case PatchDialog.PatchAction.EditPatch:
break;
case PatchDialog.PatchAction.NewPatch:
{
this.Explanation = "New patch";
}
break;
case PatchDialog.PatchAction.ShowPatch:
{
this.Explanation = "Patch " + patch.Id;
this.IsCancelButtonVisible = false;
}
break;
case PatchDialog.PatchAction.SupportOrRejectPatch:
{
this.Explanation = "Support or reject patch";
this.IsRejectButtonVisible = true;
this.OKButtonText = "Support";
}
break;
default:
break;
}
this.DisplayDiff = this.FormatDiff();
this.ValidatePatch();
}
#endregion
#region Methods
/// <summary>
/// Formats the diff displaying colors of changes.
/// </summary>
/// <returns>The formatted diff or null if no diff is available.</returns>
private FlowDocument FormatDiff()
{
if (string.IsNullOrEmpty(this.Diff))
{
return null;
}
FlowDocument document = new FlowDocument
{
FontFamily = new FontFamily("Courier New"),
FontSize = 12.0,
Background = Brushes.White,
};
string[] lines = this.Diff?.Replace("\r\n", "\n")?.Split('\n') ?? new string[] { };
foreach (string line in lines)
{
Paragraph paragraph = new Paragraph(new Run(line))
{
Margin = new System.Windows.Thickness(1),
LineHeight = 14.0
};
if (line.StartsWith("-"))
{
paragraph.Background = Globals.ResourceDict["BrushRedBackground"] as Brush;
}
else if (line.StartsWith("+"))
{
paragraph.Background = Globals.ResourceDict["BrushGreenBackground"] as Brush;
}
else if (line.StartsWith("@@"))
{
paragraph.Background = Brushes.LightGray;
}
document.Blocks.Add(paragraph);
}
return document;
}
private void LoadFromFile(object parameter)
{
// Get diff from disk
}
private void RejectPatch(object parameter)
{
Globals.ThisAddIn.DistributedPolicyEngine.Reject(this.patch, new PEPIdentity());
}
private void SupportPatch(object parameter)
{
Globals.ThisAddIn.DistributedPolicyEngine.Support(this.patch, new PEPIdentity());
}
/// <summary>
/// Validates the patch.
/// </summary>
private void ValidatePatch()
{
this.IsCommitMessageValid = !string.IsNullOrEmpty(this.CommitMessage);
this.IsDiffValid = !string.IsNullOrEmpty(this.Diff);
this.IsUriValid = !string.IsNullOrEmpty(this.Uri);
this.IsValid = this.IsCommitMessageValid && this.IsDiffValid && this.IsUriValid;
}
#endregion
}
}
...@@ -37,7 +37,7 @@ namespace pEp.UI.ViewModels ...@@ -37,7 +37,7 @@ namespace pEp.UI.ViewModels
/// </summary> /// </summary>
public string CommitMessage public string CommitMessage
{ {
get => this.Dialog.Patch.CommitMessage; get => this.Dialog.Patch?.CommitMessage;
set set
{ {
this.Dialog.Patch.CommitMessage = value; this.Dialog.Patch.CommitMessage = value;
...@@ -56,7 +56,7 @@ namespace pEp.UI.ViewModels ...@@ -56,7 +56,7 @@ namespace pEp.UI.ViewModels
/// </summary> /// </summary>
public string Diff public string Diff
{ {
get => this.Dialog.Patch.Diff; get => this.Dialog.Patch?.Diff;
set set
{ {
this.Dialog.Patch.Diff = value; this.Dialog.Patch.Diff = value;
...@@ -200,7 +200,7 @@ namespace pEp.UI.ViewModels ...@@ -200,7 +200,7 @@ namespace pEp.UI.ViewModels
/// </summary> /// </summary>
public string Tag public string Tag
{ {
get => this.Dialog.Patch.Tag; get => this.Dialog.Patch?.Tag;
set set
{ {
this.Dialog.Patch.Tag = value; this.Dialog.Patch.Tag = value;
...@@ -213,7 +213,7 @@ namespace pEp.UI.ViewModels ...@@ -213,7 +213,7 @@ namespace pEp.UI.ViewModels
/// </summary> /// </summary>
public string Uri public string Uri
{ {
get => this.Dialog.Patch.Uri; get => this.Dialog.Patch?.Uri;
set set
{ {
this.Dialog.Patch.Uri = value; this.Dialog.Patch.Uri = value;
......
<UserControl x:Class="pEp.UI.Views.FormControlPatchView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:p="clr-namespace:pEp.Properties"
xmlns:ui="clr-namespace:pEp.UI"
xmlns:vm="clr-namespace:pEp.UI.ViewModels"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=vm:PatchDialogViewModel}"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/pEp;component/Resources/Dictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
<ui:InvertBoolConverter x:Key="InvertBool"/>
<BooleanToVisibilityConverter x:Key="BoolToVisibility" />
<ui:ValueConverterGroup x:Key="InvertBoolToVisibility">
<ui:InvertBoolConverter />
<BooleanToVisibilityConverter />
</ui:ValueConverterGroup>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Grid.Column="0"
Grid.Row="0"
Content="{Binding Explanation}"
Margin="10"/>
<Button Grid.Column="1"
Grid.Row="0"
Content="Load from file"
Margin="10"
HorizontalAlignment="Right"
Style="{StaticResource StyleButtonGray}"
Command="{Binding LoadFromFileCommand}"
Visibility="{Binding IsEditable, Converter={StaticResource BoolToVisibility}}"/>
<Label Grid.Column="0"
Grid.Row="1"
Content="Diff*"
Margin="10,10,5,10"/>
<ui:TextBoxWithPlaceholder Grid.Column="1"
Grid.Row="1"
Text="{Binding Diff, UpdateSourceTrigger=PropertyChanged}"
MultiLine="True"
IsReadOnly="{Binding IsEditable, Converter={StaticResource InvertBool}}"
IsValidInput="{Binding IsDiffValid}"
Placeholder="Please add diff"
Height="400"
Width="600"
Margin="5,10,10,10"
Visibility="{Binding IsEditable, Converter={StaticResource BoolToVisibility}}"/>
<FlowDocumentScrollViewer Grid.Column="1"
BorderBrush="Black"
BorderThickness="1"
Grid.Row="1"
Height="400"
Width="600"
Margin="5,10,10,10"
Document="{Binding DisplayDiff}"
Visibility="{Binding IsEditable, Converter={StaticResource InvertBoolToVisibility}}"/>
<Label Grid.Column="0"
Grid.Row="2"
Content="Uri*"
Margin="10,10,5,10"/>
<ui:TextBoxWithPlaceholder Grid.Column="1"
Grid.Row="2"
Text="{Binding Uri}"
IsReadOnly="{Binding IsEditable, Converter={StaticResource InvertBool}}"
IsValidInput="{Binding IsUriValid}"
Placeholder="URI"
MinWidth="400"
Margin="5,10,10,10"/>
<Label Grid.Column="0"
Grid.Row="3"
Content="Commit message*"
Margin="10,10,5,10"/>
<ui:TextBoxWithPlaceholder Grid.Column="1"
Grid.Row="3"
Text="{Binding CommitMessage}"
IsReadOnly="{Binding IsEditable, Converter={StaticResource InvertBool}}"
IsValidInput="{Binding IsCommitMessageValid}"
Placeholder="Commit message"
MinWidth="400"
Margin="5,10,10,10"/>
<Label Grid.Column="0"
Grid.Row="4"
Content="Tag"
Margin="10,10,5,10"/>
<ui:TextBoxWithPlaceholder Grid.Column="1"
Grid.Row="4"
Text="{Binding Tag}"
IsReadOnly="{Binding IsEditable, Converter={StaticResource InvertBool}}"
Placeholder="Tag (optional)"
MinWidth="400"
Margin="5,10,10,10"/>
<StackPanel Grid.Column="0"
Grid.ColumnSpan="2"
Grid.Row="5"
Orientation="Horizontal"
HorizontalAlignment="Center">
<Button Content="{Binding OKButtonText}"
Command="{Binding OKButtonCommand}"
IsDefault="True"
Visibility="{Binding IsOKButtonVisible, Converter={StaticResource BoolToVisibility}}"
Style="{StaticResource StyleButtonGray}"
Margin="10" />
<Button Content="{Binding RejectButtonText}"
Command="{Binding RejectButtonCommand}"
Visibility="{Binding IsRejectButtonVisible, Converter={StaticResource BoolToVisibility}}"
Style="{StaticResource StyleButtonGray}"
Margin="10" />
</StackPanel>
</Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace pEp.UI.Views
{
/// <summary>
/// Interaction logic for FormControlPatchView.xaml
/// </summary>
public partial class FormControlPatchView : UserControl
{
public FormControlPatchView()
{
InitializeComponent();
}
}
}
...@@ -420,7 +420,7 @@ ...@@ -420,7 +420,7 @@
</Compile> </Compile>
<Compile Include="Sequoia.cs" /> <Compile Include="Sequoia.cs" />
<Compile Include="UI\FormRegionDPE.cs"> <Compile Include="UI\FormRegionDPE.cs">
<SubType>Form</SubType> <SubType>UserControl</SubType>
</Compile> </Compile>
<Compile Include="UI\FormRegionDPE.Designer.cs"> <Compile Include="UI\FormRegionDPE.Designer.cs">
<DependentUpon>FormRegionDPE.cs</DependentUpon> <DependentUpon>FormRegionDPE.cs</DependentUpon>
...@@ -433,6 +433,7 @@ ...@@ -433,6 +433,7 @@
<Compile Include="UI\TextBoxWithPlaceholder.xaml.cs"> <Compile Include="UI\TextBoxWithPlaceholder.xaml.cs">
<DependentUpon>TextBoxWithPlaceholder.xaml</DependentUpon> <DependentUpon>TextBoxWithPlaceholder.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="UI\ViewModels\FormControlPatchViewModel.cs" />
<Compile Include="UI\ViewModels\GroupWizardAddMembersViewModel.cs" /> <Compile Include="UI\ViewModels\GroupWizardAddMembersViewModel.cs" />
<Compile Include="UI\ViewModels\GroupWizardNewListViewModel.cs" /> <Compile Include="UI\ViewModels\GroupWizardNewListViewModel.cs" />
<Compile Include="UI\ViewModels\GroupWizardViewModel.cs" /> <Compile Include="UI\ViewModels\GroupWizardViewModel.cs" />
...@@ -528,6 +529,9 @@ ...@@ -528,6 +529,9 @@
</Compile> </Compile>
<Compile Include="UI\ValueConverters.cs" /> <Compile Include="UI\ValueConverters.cs" />
<Compile Include="UI\ViewModels\ViewModelBase.cs" /> <Compile Include="UI\ViewModels\ViewModelBase.cs" />
<Compile Include="UI\Views\FormControlPatchView.xaml.cs">
<DependentUpon>FormControlPatchView.xaml</DependentUpon>
</Compile>
<Compile Include="UI\Views\GroupWizardAddMembersView.xaml.cs"> <Compile Include="UI\Views\GroupWizardAddMembersView.xaml.cs">
<DependentUpon>GroupWizardAddMembersView.xaml</DependentUpon> <DependentUpon>GroupWizardAddMembersView.xaml</DependentUpon>
</Compile> </Compile>
...@@ -723,6 +727,10 @@ ...@@ -723,6 +727,10 @@
<SubType>Designer</SubType> <SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
</Page> </Page>
<Page Include="UI\Views\FormControlPatchView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="UI\Views\GroupWizardAddMembersView.xaml"> <Page Include="UI\Views\GroupWizardAddMembersView.xaml">
<SubType>Designer</SubType> <SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
......
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