Skip to content

Commit

Permalink
Merge pull request #10 from Sionkerk-Houten/development
Browse files Browse the repository at this point in the history
DPI awareness, second presentation image replacing, new ServiceElement type, fixed escaped characters in relative path
  • Loading branch information
asdfCYBER authored Apr 22, 2021
2 parents ca745ce + 04c02f7 commit 2bb7e07
Show file tree
Hide file tree
Showing 13 changed files with 463 additions and 270 deletions.
6 changes: 6 additions & 0 deletions PPTXcreator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
<PropertyGroup>
<ApplicationIcon>Resources\Icon.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup />
<PropertyGroup />
<PropertyGroup>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down Expand Up @@ -117,6 +122,7 @@
<DependentUpon>Window.cs</DependentUpon>
</EmbeddedResource>
<None Include="app.config" />
<None Include="app.manifest" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
4 changes: 3 additions & 1 deletion PastorInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ public class PastorInfo
public string TitleName { get; set; } = "";

[JsonIgnore]
public string Title
public string Title
{
get
{
// Split the string at the first space and return the first part
if (!TitleName.Contains(" ")) return "titel";
if (TitleName == "Nog niet bekend") return "";
return TitleName.Split(new char[] { ' ' }, 2)[0];
}
set => Title = value;
Expand All @@ -26,6 +27,7 @@ public string Name
{
// Split the string at the first space and return the second part
if (!TitleName.Contains(" ")) return "naam";
if (TitleName == "Nog niet bekend") return TitleName;
return TitleName.Split(new char[] { ' ' }, 2)[1];
}
}
Expand Down
36 changes: 30 additions & 6 deletions PowerPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void ReplaceKeywords(Dictionary<string, string> keywords)
/// </summary>
/// <param name="run">The run to replace</param>
/// <param name="elements">The ServiceElements to replace the run by</param>
public void ReplaceMultilineKeywords(Run run, List<ServiceElement> elements)
public void ReplaceMultilineKeywords(Run run, IEnumerable<ServiceElement> elements)
{
// Get the paragraph and textbody the run is a child of
Paragraph par = (Paragraph)run.Parent;
Expand Down Expand Up @@ -102,10 +102,13 @@ public void ReplaceMultilineKeywords(Run run, List<ServiceElement> elements)
}

/// <summary>
/// Replace '[zingen]' and '[lezen]' text in the document with songs and readings
/// Replace placeholder text in the document with songs and readings
/// </summary>
public void ReplaceMultilineKeywords(List<ServiceElement> songs, List<ServiceElement> readings)
public void ReplaceMultilineKeywords(IEnumerable<ServiceElement> songs, IEnumerable<ServiceElement> readings)
{
// Prevent removing the paragraph if there are no songs/readings
if (readings.Count() == 0) readings = readings.Append(new ServiceElement());

// Loop over all Drawing.Run elements in the document
foreach (SlidePart slidePart in Slides)
{
Expand Down Expand Up @@ -138,13 +141,25 @@ private void ReplaceImage(string imagePath, SlidePart slidePart)
{
// Get the ImagePart by id, and replace the image
ImagePart imagePart = (ImagePart)slidePart.GetPartById(rId);
FileStream imageStream = File.OpenRead(imagePath);
FileStream imageStream;
if (!Program.TryGetFileStream(imagePath, out 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"/>
/// with the image at <paramref name="imagePath"/>.
Expand Down Expand Up @@ -184,6 +199,15 @@ public SlidePart DuplicateFirstSlide()
Presentation.SlideId targetSlideId = idList.AppendChild(new Presentation.SlideId());
targetSlideId.Id = maxId + 1;
targetSlideId.RelationshipId = PresPart.GetIdOfPart(targetSlidePart);

// Copy all ImageParts from the source slide to the new one
IEnumerable<ImagePart> imageParts = sourceSlidePart.ImageParts;
foreach (ImagePart img in imageParts)
{
string rId = sourceSlidePart.GetIdOfPart(img);
ImagePart newImagePart = targetSlidePart.AddImagePart(img.ContentType, rId);
newImagePart.FeedData(img.GetStream(FileMode.Open));
}

PresPart.Presentation.Save();
return targetSlidePart;
Expand All @@ -195,11 +219,11 @@ public SlidePart DuplicateFirstSlide()
/// </summary>
/// <param name="keywords">A dictionary containing replaceable strings
/// and what they should be replaced by</param>
public void DuplicateAndReplace(Dictionary<string, string> keywords)
public void DuplicateAndReplace(Dictionary<string, string> keywords, bool ShowQR)
{
SlidePart slidePart = DuplicateFirstSlide();
ReplaceKeywords(keywords, slidePart.Slide);
// ReplaceImage can also be used if necessary
if (!ShowQR) RemoveImage(slidePart);
}

/// <summary>
Expand Down
38 changes: 34 additions & 4 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,55 @@ private static void CrashHandlerDomain(object sender, UnhandledExceptionEventArg
/// <returns>Whether the function succeeded</returns>
public static bool TryGetFileContents(string path, out string filecontents)
{
filecontents = "";

try
{
filecontents = File.ReadAllText(path);
return true;
}
catch (Exception ex) when (ex is DirectoryNotFoundException || ex is FileNotFoundException)
{
Dialogs.GenericWarning($"{path} kon niet worden geopgend omdat het bestand niet gevonden is.");
filecontents = "";
Dialogs.GenericWarning($"'{path}' kon niet worden geopgend omdat het bestand niet gevonden is.");
return false;
}
catch (Exception ex) when (ex is IOException
|| ex is UnauthorizedAccessException
|| ex is NotSupportedException
|| ex is System.Security.SecurityException)
{
Dialogs.GenericWarning($"{path} kon niet worden geopend.\n\n" +
Dialogs.GenericWarning($"'{path}' kon niet worden geopend.\n\n" +
$"De volgende foutmelding werd gegeven: {ex.Message}");
return false;
}
}

/// <summary>
/// Helper function for opening a filestream and handling exceptions
/// </summary>
/// <param name="path">Path to the file that has to be read</param>
/// <param name="stream">The filestream, or null if reading failed</param>
/// <returns>Whether the function succeeded</returns>
public static bool TryGetFileStream(string path, out FileStream stream)
{
stream = null;

try
{
stream = File.OpenRead(path);
return true;
}
catch (IOException ex) when (ex is DirectoryNotFoundException || ex is FileNotFoundException)
{
Dialogs.GenericWarning($"'{path}' kon niet worden geopgend omdat het bestand niet gevonden is.");
return false;
}
catch (Exception ex) when (ex is IOException
|| ex is UnauthorizedAccessException
|| ex is NotSupportedException)
{
Dialogs.GenericWarning($"'{path}' kon niet worden geopend.\n\n" +
$"De volgende foutmelding werd gegeven: {ex.Message}");
filecontents = "";
return false;
}
}
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.11.0.0")]
[assembly: AssemblyFileVersion("0.11.0.0")]
[assembly: AssemblyVersion("0.12.0.0")]
[assembly: AssemblyFileVersion("0.12.0.0")]
[assembly: NeutralResourcesLanguage("nl")]
2 changes: 1 addition & 1 deletion Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static (Service current, Service next) GetCurrentAndNext(DateTime datetim
}
catch (Exception ex) when (ex is JsonException || ex is InvalidOperationException)
{
Dialogs.GenericWarning($"{Settings.Instance.PathServicesJson}" +
Dialogs.GenericWarning($"'{Settings.Instance.PathServicesJson}'" +
$" heeft niet de juiste structuur.\n\nDe volgende foutmelding werd gegeven: {ex.Message}");
return (current, next);
}
Expand Down
24 changes: 23 additions & 1 deletion ServiceElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ namespace PPTXcreator
{
public enum ElementType
{
None,
Reading,
PsalmOB,
PsalmWK,
SongWK,
SongOTH,
SongOther
}

Expand All @@ -27,7 +29,9 @@ public class ServiceElement
/// Appears in the second line in the presentation during the service
/// </summary>
public string Subtitle { get; }
public bool IsSong { get => Type != ElementType.Reading; }
public bool IsSong { get => Type > ElementType.Reading; }
public bool IsReading { get => Type == ElementType.Reading; }
public bool ShowQR { get; set; } = false;

/// <summary>
/// Construct a ServiceElement instance from a DataGridViewRow
Expand Down Expand Up @@ -61,14 +65,32 @@ public ServiceElement(DataGridViewRow row)
if (string.IsNullOrWhiteSpace(songname)) Subtitle = "";
else Subtitle = songname.Trim();
break;
case "Lied (OTH)":
Type = ElementType.SongOTH;
Title = "OTH " + FormatTitle(selection);
if (string.IsNullOrWhiteSpace(songname)) Subtitle = "";
else Subtitle = songname.Trim();
break;
case "Lied (Overig)":
Type = ElementType.SongOther;
if (string.IsNullOrWhiteSpace(songname)) Title = "";
else Title = songname.Trim();
break;
default:
Type = ElementType.None;
Title = "";
Subtitle = "";
break;
}
}

public ServiceElement()
{
Type = ElementType.None;
Title = "";
Subtitle = "";
}

private string ReplaceLastComma(string input)
{
int lastComma = input.LastIndexOf(",");
Expand Down
7 changes: 2 additions & 5 deletions Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ public string PathQRImage
[JsonPropertyName("QR-afbeeldingen bewerken")]
public bool EnableEditQR { get; set; } = true;

[JsonPropertyName("QR opslaan in de outputfolder")]
public bool EnableExportQR { get; set; } = true;

public KeywordSettings Keywords { get; set; } = new KeywordSettings();

/// <summary>
Expand All @@ -110,7 +107,7 @@ public static void Load()
catch (JsonException ex)
{
Dialogs.GenericWarning("Instellingen konden niet worden geladen vanwege ongeldige waarden in " +
$"{SettingsPath}. Standaardwaarden worden gebruikt.\n\nVolledige error: {ex.Message}");
$"'{SettingsPath}'. Standaardwaarden worden gebruikt.\n\nVolledige error: {ex.Message}");
}
}

Expand Down Expand Up @@ -146,7 +143,7 @@ public static string GetPath(string path)
Uri assemblyPath = new Uri(AppContext.BaseDirectory);
Uri targetPath = new Uri(Path.GetFullPath(path));
string relativePath = assemblyPath.MakeRelativeUri(targetPath).ToString();
relativePath = relativePath.Replace('/', '\\'); // URIs use forward slashes
relativePath = Uri.UnescapeDataString(relativePath).Replace("/", "\\"); // URIs use forward slashes

if (relativePath.StartsWith("..")) return path;
else return relativePath;
Expand Down
Loading

0 comments on commit 2bb7e07

Please sign in to comment.