Ejemplo n.º 1
0
 /// <summary>
 /// Try to create a new <see cref="PowerPoint"/> object, handle exceptions
 /// </summary>
 private static PowerPoint CreatePowerpoint(string templatePath, string outputPath)
 {
     try
     {
         PowerPoint powerpoint = new PowerPoint(templatePath, outputPath);
         return(powerpoint);
     }
     catch (FileNotFoundException ex)
     {
         Dialogs.GenericWarning($"Het bestand '{ex.FileName}' is niet gevonden. Controleer het bestandspad en probeer opnieuw.");
     }
     catch (DirectoryNotFoundException)
     {
         Dialogs.GenericWarning($"Het bestandspad '{templatePath}' of '{outputPath}' bestaat niet. Controleer het bestandspad " +
                                $"en probeer opnieuw");
     }
     catch (IOException ex) when((ex.HResult & 0x0000FFFF) == 32)
     {
         Dialogs.GenericWarning($"Het bestand '{outputPath}' kon niet worden bewerkt omdat het geopend is in een ander " +
                                "programma. Sluit het bestand en probeer opnieuw.");
     }
     catch (Exception ex) when(ex is IOException ||
                               ex is UnauthorizedAccessException ||
                               ex is NotSupportedException ||
                               ex is System.Security.SecurityException)
     {
         Dialogs.GenericWarning($"'{templatePath}' of '{outputPath}' kon niet worden geopend.\n\n" +
                                $"De volgende foutmelding werd gegeven: {ex.Message}");
     }
     return(null);
 }
Ejemplo n.º 2
0
 private void ButtonRemoveDatagridviewRow(object sender, EventArgs e)
 {
     if (dataGridView.SelectedRows.Count == 1)
     {
         try
         {
             dataGridView.Rows.Remove(dataGridView.SelectedRows[0]);
         }
         catch (InvalidOperationException exception)
         {
             Dialogs.GenericWarning("De rij kan niet worden verwijderd." +
                                    $"De volgende foutmelding werd gegeven: {exception.Message}");
         }
     }
     else if (dataGridView.CurrentCell != null)
     {
         try
         {
             dataGridView.Rows.Remove(dataGridView.CurrentCell.OwningRow);
         }
         catch (InvalidOperationException exception)
         {
             Dialogs.GenericWarning("De rij kan niet worden verwijderd. " +
                                    $"De volgende foutmelding werd gegeven: {exception.Message}");
         }
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Data class containing information about a service
        /// </summary>
        public static (Service current, Service next) GetCurrentAndNext(DateTime datetime)
        {
            // Initialize some values
            JsonElement[] services;
            Service       current = new Service();
            Service       next    = new Service();

            // Load the file contents
            if (!Program.TryGetFileContents(Settings.Instance.PathServicesJson, out string servicesFile))
            {
                return(current, next);
            }

            // Parse the file contents
            try
            {
                JsonDocumentOptions options = new JsonDocumentOptions()
                {
                    AllowTrailingCommas = true,
                    CommentHandling     = JsonCommentHandling.Skip
                };
                services = JsonDocument.Parse(servicesFile, options).RootElement.EnumerateArray().ToArray();
            }
            catch (Exception ex) when(ex is JsonException || ex is InvalidOperationException)
            {
                Dialogs.GenericWarning($"'{Settings.Instance.PathServicesJson}'" +
                                       $" heeft niet de juiste structuur.\n\nDe volgende foutmelding werd gegeven: {ex.Message}");
                return(current, next);
            }

            // Iterate over all services in servicesjson and find the one which matches datetime
            for (int i = 0; i < services.Length; i++)
            {
                if (services[i].GetProperty("Datetime").GetDateTime() == datetime)
                {
                    current = JsonSerializer.Deserialize <Service>(services[i].GetRawText());

                    // Try to get the next service if possible
                    try
                    {
                        next = JsonSerializer.Deserialize <Service>(services[i + 1].GetRawText());
                    }
                    catch (IndexOutOfRangeException)
                    {
                        next = new Service();
                    }

                    break;
                }
            }

            return(current, next);
        }
Ejemplo n.º 4
0
        /// <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 geopend 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}");
                return(false);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Helper function for loading files and handling exceptions
        /// </summary>
        /// <param name="path">Path to the file that has to be read</param>
        /// <param name="filecontents">The contents of the file, or an empty string if reading failed</param>
        /// <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.");
                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" +
                                       $"De volgende foutmelding werd gegeven: {ex.Message}");
                return(false);
            }
        }
Ejemplo n.º 6
0
        /// <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))
            {
                Dialogs.GenericWarning("De outputfolder bestaat niet, " +
                                       "selecteer een bestaande folder in de instellingen");
                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);

            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;
        }