Skip to content

Commit

Permalink
Merge pull request #11 from Sionkerk-Houten/development
Browse files Browse the repository at this point in the history
Remove theme is not filled in, fix DPI-related and output folder path crashes, add Package target and workflow
  • Loading branch information
asdfCYBER authored Apr 28, 2021
2 parents 2bb7e07 + 9f998a4 commit a1f5ec6
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 20 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Package

on:
workflow_dispatch:
push:
branches:
- main

jobs:
build:

runs-on: windows-latest

steps:
- uses: actions/checkout@v2
name: Checkout source

- name: Setup MSBuild path
uses: microsoft/setup-msbuild@v1.0.2

- name: Setup NuGet
uses: NuGet/setup-nuget@v1.0.5

- name: Get NuGet packages
run: nuget restore PPTXcreator.sln

- name: Build and package solution
run: msbuild PPTXcreator.sln /t:Build;Package /p:Configuration=Release

- name: Upload artifacts
uses: actions/upload-artifact@v2
with:
name: PPTXcreator
path: bin\Release\
retention-days: 7
3 changes: 3 additions & 0 deletions KeywordSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class KeywordSettings
[JsonPropertyName("Thema")]
public string Theme { get; set; } = "[thema]";

[JsonPropertyName("Thema header identifier")]
public string ThemeHeaderIdentifier { get; set; } = "thema van de preek";

[JsonPropertyName("Collectedoel 1")]
public string Collection1 { get; set; } = "[collectedoel 1]";

Expand Down
16 changes: 16 additions & 0 deletions PPTXcreator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>false</SignAssembly>
Expand Down Expand Up @@ -77,8 +78,10 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Numerics" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down Expand Up @@ -161,6 +164,9 @@
<PackageReference Include="DocumentFormat.OpenXml">
<Version>2.12.3</Version>
</PackageReference>
<PackageReference Include="ILMerge">
<Version>3.0.41</Version>
</PackageReference>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces">
<Version>5.0.0</Version>
</PackageReference>
Expand Down Expand Up @@ -198,6 +204,16 @@
<Version>4.5.0</Version>
</PackageReference>
</ItemGroup>
<Target Name="Package" Condition="'$(Configuration)' == 'Release'">
<ItemGroup>
<ILMergeFiles Include="bin\Release\*.dll" />
</ItemGroup>
<Exec Command="$(ILMergeConsolePath) bin\Release\PPTXcreator.exe /out:bin\Release\PPTXcreator.exe @(ILMergeFiles, ' ') /log /ndebug" />
<Delete Files="@(ILMergeFiles);bin\Release\PPTXcreator.pdb" />
<WriteLinesToFile File="bin\Release\settings.json" Lines="{}" Overwrite="true" />
<MakeDir Directories="bin\Publish\" />
<ZipDirectory DestinationFile="bin\Publish\PPTXcreator.zip" SourceDirectory="bin\Release" Overwrite="true" />
</Target>
<ItemGroup>
<Content Include="Resources\Icon.ico" />
</ItemGroup>
Expand Down
51 changes: 37 additions & 14 deletions PowerPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,27 @@ public void ReplaceMultilineKeywords(IEnumerable<ServiceElement> songs, IEnumera
}
}

/// <summary>
/// Removes the runs containing <see cref="KeywordSettings.ThemeHeaderIdentifier"/>
/// </summary>
/// <param name="slide"></param>
public void RemoveThemeRuns()
{
// Loop over slides
foreach (SlidePart slide in Slides)
{
// Loop over text in the slide
foreach (Text text in slide.Slide.Descendants<Text>())
{
if (text.Text.ToLower().Contains(Settings.Instance.Keywords.ThemeHeaderIdentifier)
|| text.Text.Contains(Settings.Instance.Keywords.Theme))
{
text.Parent.Remove();
}
}
}
}

/// <summary>
/// Replaces the image with description <see cref="Settings.QRdescription"/>
/// with the image at <paramref name="imagePath"/>.
Expand All @@ -141,27 +162,15 @@ private void ReplaceImage(string imagePath, SlidePart slidePart)
{
// Get the ImagePart by id, and replace the image
ImagePart imagePart = (ImagePart)slidePart.GetPartById(rId);
FileStream imageStream;
if (!Program.TryGetFileStream(imagePath, out imageStream)) return;
if (!Program.TryGetFileStream(imagePath, out FileStream imageStream)) return;
imagePart.FeedData(imageStream);
imageStream.Close();
}
}
}

private void RemoveImage(SlidePart slidePart)
{
// Loop over all picture objects
foreach (Presentation.Picture pic in slidePart.Slide.Descendants<Presentation.Picture>())
{
// Remove the image if the description matches the ImageDescription setting
string description = pic.NonVisualPictureProperties.NonVisualDrawingProperties.Description;
if (description == Settings.Instance.ImageDescription) pic.Remove();
}
}

/// <summary>
/// Replaces the image with description <see cref="Settings.QRdescription"/>
/// Replaces the image with description <see cref="Settings.ImageDescription"/>
/// with the image at <paramref name="imagePath"/>.
/// </summary>
public void ReplaceImage(string imagePath)
Expand All @@ -175,6 +184,20 @@ public void ReplaceImage(string imagePath)
}
}

/// <summary>
/// Removes the image with description <see cref="Settings.ImageDescription"/>
/// </summary>
private void RemoveImage(SlidePart slidePart)
{
// Loop over all picture objects
foreach (Presentation.Picture pic in slidePart.Slide.Descendants<Presentation.Picture>())
{
// Remove the image if the description matches the ImageDescription setting
string description = pic.NonVisualPictureProperties.NonVisualDrawingProperties.Description;
if (description == Settings.Instance.ImageDescription) pic.Remove();
}
}

/// <summary>
/// Copy the first slide of this document and paste it at the end,
/// and make the copied slide visible if it was hidden
Expand Down
2 changes: 1 addition & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static bool TryGetFileStream(string path, out FileStream stream)
}
catch (IOException ex) when (ex is DirectoryNotFoundException || ex is FileNotFoundException)
{
Dialogs.GenericWarning($"'{path}' kon niet worden geopgend omdat het bestand niet gevonden is.");
Dialogs.GenericWarning($"'{path}' kon niet worden geopend omdat het bestand niet gevonden is.");
return false;
}
catch (Exception ex) when (ex is IOException
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.12.0.0")]
[assembly: AssemblyFileVersion("0.12.0.0")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: NeutralResourcesLanguage("nl")]
6 changes: 5 additions & 1 deletion Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public string PathServicesJson
set => pathServicesJson = GetPath(value);
}

private string pathOutputFolder = "./output";
private string pathOutputFolder = AppContext.BaseDirectory;
[JsonPropertyName("Outputfolder")]
public string PathOutputFolder
{
Expand Down Expand Up @@ -140,8 +140,12 @@ ex is IOException
/// </summary>
public static string GetPath(string path)
{
if (string.IsNullOrWhiteSpace(path)) return path;

Uri assemblyPath = new Uri(AppContext.BaseDirectory);
Uri targetPath = new Uri(Path.GetFullPath(path));
if (assemblyPath == targetPath) return path;

string relativePath = assemblyPath.MakeRelativeUri(targetPath).ToString();
relativePath = Uri.UnescapeDataString(relativePath).Replace("/", "\\"); // URIs use forward slashes

Expand Down
37 changes: 35 additions & 2 deletions Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Window()
private void ButtonSelectQR(object sender, EventArgs e)
{
string path = Dialogs.SelectFile(
"JPEG (*.jpeg)|*.jpeg|PNG (*.png)|*.png|Alle bestanden (*.*)|*.*",
"Afbeelding (*.jpeg; *.jpg; *.png)|*.jpeg;*.jpg;*.png|Alle bestanden (*.*)|*.*",
"Selecteer de QR-code"
);

Expand Down Expand Up @@ -284,6 +284,11 @@ private void ButtonGotoSettingsTab(object sender, EventArgs e)
private void ButtonNextTab(object sender, EventArgs e)
{
int index = tabControl.SelectedIndex;
// I have no idea why this prevents a crash when using multiple screens at different scaling
if (index == 1 && dataGridView.CurrentCell is null)
{
dataGridView.Rows[0].Cells[1].Selected = true;
}
tabControl.SelectTab(index + 1);
}

Expand All @@ -303,6 +308,9 @@ private void CheckBoxAutoPopulateChanged(object sender, EventArgs e)
Settings.Instance.EnableAutoPopulate = ((CheckBox)sender).Checked;
}

/// <summary>
/// Get a list of ServiceElements from the DataGridView
/// </summary>
private List<ServiceElement> GetServiceElements()
{
List<ServiceElement> elements = new List<ServiceElement>();
Expand Down Expand Up @@ -374,8 +382,12 @@ private bool CheckValidInputs()
return true;
}

/// <summary>
/// Create the three presentations with information from the user interface
/// </summary>
public void CreatePresentations(object sender, EventArgs e)
{
// Check if fields are filled in and if the output folder is right
if (!CheckValidInputs()) return;
if (!Directory.Exists(Settings.Instance.PathOutputFolder))
{
Expand All @@ -384,47 +396,68 @@ public void CreatePresentations(object sender, EventArgs e)
return;
}

// Get the filled in information from the window
KeywordSettings tags = Settings.Instance.Keywords;
Dictionary<string, string> keywords = GetFormKeywords();
List<ServiceElement> elements = GetServiceElements();
string filenamepart = GetFilenamePart(dateTimePickerCurrent);

// Create the presentation before the service
PowerPoint beforeService = CreatePowerpoint(Settings.Instance.PathTemplateBefore,
Settings.Instance.PathOutputFolder + $"/voor {filenamepart}.pptx");

if (beforeService == null) return;
if (string.IsNullOrWhiteSpace(keywords[tags.Theme]))
beforeService.RemoveThemeRuns();

beforeService.ReplaceKeywords(keywords);
beforeService.ReplaceImage(textBoxQRPath.Text);
beforeService.ReplaceMultilineKeywords(
from ServiceElement element in elements where element.IsSong select element,
from ServiceElement element in elements where element.IsReading select element
);

beforeService.SaveClose();

// Create the presentation during the service
PowerPoint duringService = CreatePowerpoint(Settings.Instance.PathTemplateDuring,
Settings.Instance.PathOutputFolder + $"/tijdens {filenamepart}.pptx");

if (duringService == null) return;

duringService.ReplaceKeywords(keywords);
duringService.ReplaceImage(textBoxQRPath.Text);
KeywordSettings tags = Settings.Instance.Keywords;

foreach (ServiceElement element in elements)
{
duringService.DuplicateAndReplace(new Dictionary<string, string> {
{ tags.ServiceElementTitle, element.Title },
{ tags.ServiceElementSubtitle, element.Subtitle }
}, element.ShowQR);
}

duringService.SaveClose();

// Create the presentation after the service
PowerPoint afterService = CreatePowerpoint(Settings.Instance.PathTemplateAfter,
Settings.Instance.PathOutputFolder + $"/na {filenamepart}.pptx");

if (afterService == null) return;

afterService.ReplaceKeywords(keywords);
afterService.ReplaceImage(textBoxQRPath.Text);

afterService.SaveClose();

// Done, message the user
Dialogs.GenericInformation("Voltooid", $"De presentaties zijn gemaakt " +
$"en staan in de folder '{Settings.Instance.PathOutputFolder}'.");
Settings.Instance.NextService = dateTimePickerNext.Value;
}

/// <summary>
/// Try to create a new <see cref="PowerPoint"/> object, handle exceptions
/// </summary>
private static PowerPoint CreatePowerpoint(string templatePath, string outputPath)
{
try
Expand Down

0 comments on commit a1f5ec6

Please sign in to comment.