using Microsoft.Data.Sqlite; using Microsoft.Win32; using System; using System.Collections.Generic; using System.Windows; namespace Elwig.Services { public static class InteractionService { public static Func? Override; public static Action? NextInformation; public static Action? NextWarning; public static Action? NextError; public static Func? NextContinue; public static Func? NextConfirmation; public static Func? NextQuestion; public static Func? NextSave; public static readonly Dictionary ExtensionFilters = new() { ["pdf"] = "PDF-Datei (*.pdf)|*.pdf", ["ods"] = "OpenDocument Format Spreadsheet (*.ods)|*.ods", ["vcf"] = "vCard-Datei (*.vcf)|*.vcf", ["xml"] = "EBICS-Datei (*.xml)|*.xml", ["csv"] = "CSV-Datei (*.csv)|*.csv", ["sql.zip"] = "Komprimierte SQL-Datei (*.sql.zip)|*.sql.zip", ["elwig.zip"] = "Elwig-Export-Datei (*.elwig.zip)|*.elwig.zip", }; public static void ShowInformation(string title, string text) { if (NextInformation != null) { NextInformation(title, text); NextInformation = null; } else if (Override != null) { Override("information", title, text); } else { MessageBox.Show(text, title, MessageBoxButton.OK, MessageBoxImage.Information); } } public static bool AskContinue(string title, string text) { if (NextContinue != null) { var r = NextContinue(title, text); NextContinue = null; return r; } else if (Override != null) { return Override("continue", title, text) != null; } else { return MessageBox.Show(text, title, MessageBoxButton.OKCancel, MessageBoxImage.Warning, MessageBoxResult.Cancel) == MessageBoxResult.OK; } } public static bool? AskContinueYesNo(string title, string text) { if (NextContinue != null) { var r = NextContinue(title, text); NextContinue = null; return r; } else if (Override != null) { return Override("continue", title, text) != null; } else { var r = MessageBox.Show(text, title, MessageBoxButton.YesNoCancel, MessageBoxImage.Warning, MessageBoxResult.Cancel); return r == MessageBoxResult.Yes ? true : r == MessageBoxResult.No ? false : null; } } public static bool AskConfirmation(string title, string text) { if (NextConfirmation != null) { var r = NextConfirmation(title, text); NextConfirmation = null; return r; } else if (Override != null) { return Override("confirm", title, text) != null; } else { return MessageBox.Show(text, title, MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No) == MessageBoxResult.Yes; } } public static bool AskQuestion(string title, string text, bool defaultResult) { if (NextQuestion != null) { var r = NextQuestion(title, text); NextQuestion = null; return r; } else if (Override != null) { return Override("question", title, text) != null; } else { return MessageBox.Show(text, title, MessageBoxButton.YesNo, MessageBoxImage.Question, defaultResult ? MessageBoxResult.Yes : MessageBoxResult.No) == MessageBoxResult.Yes; } } public static void ShowWarning(string title, string text) { if (NextWarning != null) { NextWarning(title, text); NextWarning = null; } else if (Override != null) { Override("warning", title, text); } else { MessageBox.Show(text, title, MessageBoxButton.OK, MessageBoxImage.Warning); } } public static void ShowError(string title, string text) { if (NextError != null) { NextError(title, text); NextError = null; } else if (Override != null) { Override("error", title, text); } else { MessageBox.Show(text, title, MessageBoxButton.OK, MessageBoxImage.Error); } } public static void ShowException(Exception exc, bool showExcType = false, bool isError = true) { ShowException("Fehler", exc, showExcType, isError); } public static void ShowException(string title, Exception exc, bool showExcType = false, bool isError = true) { ShowException(title, null, exc, showExcType, isError); } public static void ShowException(string title, string? text, Exception exc, bool showExcType = false, bool isError = true) { text = text == null ? "" : text + (text.EndsWith('.') || text.EndsWith('!') || text.EndsWith('?') ? "" : ":") + "\n\n"; if ((exc.InnerException ?? exc) is SqliteException sql) { if (sql.SqliteErrorCode == 5 || sql.SqliteErrorCode == 6) { // SQLITE_BUSY / SQLITE_LOCKED text += "Die Datenbank wurde gerade von einem anderen Gerät/Prozess verwendet. Bitte noch einmal versuchen!\n\n" + sql.Message; } else if (sql.SqliteErrorCode == 19) { // SQLITE_CONSTRAINT switch (sql.SqliteExtendedErrorCode) { case 1555: // SQLITE_CONSTRAINT_PRIMARYKEY - indicating that a PRIMARY KEY constraint failed case 2067: // SQLITE_CONSTRAINT_UNIQUE - indicating that a UNIQUE constraint failed case 2579: // SQLITE_CONSTRAINT_ROWID - indicating that a rowid is not unique text += "Dieser Datensatz existiert bereits!\n\n"; break; case 787: // SQLITE_CONSTRAINT_FOREIGNKEY - indicating that a foreign key constraint failed case 1299: // SQLITE_CONSTRAINT_NOTNULL - indicating that a NOT NULL constraint failed case 275: // SQLITE_CONSTRAINT_CHECK - indicating that a CHECK constraint failed case 1811: // SQLITE_CONSTRAINT_TRIGGER - indicating that a RAISE function within a trigger fired, causing the SQL statement to abort case 531: // SQLITE_CONSTRAINT_COMMITHOOK - indicating that a commit hook callback returned non-zero that thus caused the SQL statement to be rolled back text += "Der Datensatz ist inkonsistent!\n\n"; break; } text += sql.Message; } else { text += exc.Message + (showExcType ? $" ({exc.GetType().Name})" : ""); if (exc.InnerException != null) text += "\n\n" + exc.InnerException.Message; } } else { text += exc.Message + (showExcType ? $" ({exc.GetType().Name})" : ""); if (exc.InnerException != null) text += "\n\n" + exc.InnerException.Message; } if (isError) { ShowError(title, text); } else { ShowWarning(title, text); } } public static void ShowDbException(string title, Exception exc) { ShowException(title, "Der Eintrag konnte nicht in der Datenbank aktualisiert werden!", exc); } public static bool AskException(string title, string? text, Exception exc) { text = text == null ? "" : text + (text.EndsWith('.') || text.EndsWith('!') || text.EndsWith('?') ? "" : ":") + "\n\n"; text += exc.Message; if (exc.InnerException != null) text += "\n\n" + exc.InnerException.Message; if (Override != null) { return Override("error", title, text) != null; } else { return MessageBox.Show(text, title, MessageBoxButton.YesNo, MessageBoxImage.Error, MessageBoxResult.No) == MessageBoxResult.Yes; } } public static string? SaveFile(string title, string defaultFileName, string extension) { if (NextSave != null) { var r = NextSave(title, $"{defaultFileName}.{extension}"); NextSave = null; return r; } else if (Override != null) { return Override("save", title, $"{defaultFileName}.{extension}"); } else { var d = new SaveFileDialog() { FileName = $"{defaultFileName}.{extension}", DefaultExt = extension, Filter = ExtensionFilters.GetValueOrDefault(extension, ""), Title = $"{title} speichern unter - Elwig", AddExtension = !extension.Contains('.'), }; return d.ShowDialog() == true ? d.FileName : null; } } } }