diff --git a/LeanderShiftPlannerV2/FileIO/GoogleApiLoader.cs b/LeanderShiftPlannerV2/FileIO/GoogleApiLoader.cs new file mode 100644 index 0000000..1757cdb --- /dev/null +++ b/LeanderShiftPlannerV2/FileIO/GoogleApiLoader.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.IO; +using LeanderShiftPlannerV2.Util; + +namespace LeanderShiftPlannerV2.FileIO; + +public static class GoogleApiLoader +{ + public static bool CheckFileSystem() + { + if (!Directory.Exists(Constants.DataPath)) + Directory.CreateDirectory(Constants.DataPath); + if (!File.Exists(Constants.DataPath + Constants.GoogleApiFilePath)) + { + File.Create(Constants.DataPath + Constants.GoogleApiFilePath).Close(); + File.WriteAllLines(Constants.DataPath + Constants.GoogleApiFilePath, + File.ReadAllLines(Constants.GoogleApiFileResource)); + return false; + } + return true; + } + + public static Dictionary ReadFile() + { + if (!CheckFileSystem()) return new Dictionary(); + Dictionary apiSecrets = new Dictionary(); + + try + { + string[] lines = File.ReadAllLines(Constants.DataPath + Constants.GoogleApiFilePath); + foreach (string line in lines) + { + string[] parts = line.Split(new[] { '=' }, 2); + if (parts.Length == 2) + { + string pattern = parts[0].Trim(); + string value = parts[1].Trim(); + if (!string.IsNullOrEmpty(pattern)) + { + apiSecrets[pattern] = value; + } + } + } + } + catch (Exception ex) + { + //TODO + Console.WriteLine($"Fehler beim Lesen der Datei: {ex.Message}"); + } + + return apiSecrets; + } +} \ No newline at end of file diff --git a/LeanderShiftPlannerV2/LeanderShiftPlannerV2.csproj b/LeanderShiftPlannerV2/LeanderShiftPlannerV2.csproj index 788560e..a5adc0f 100644 --- a/LeanderShiftPlannerV2/LeanderShiftPlannerV2.csproj +++ b/LeanderShiftPlannerV2/LeanderShiftPlannerV2.csproj @@ -8,10 +8,10 @@ Resources/icon/ShiftPlannerIcon.ico true Resources\icon\ShiftPlannerIcon.png - 1.0.4 - 1.0.4 + 1.0.5 + 1.0.5 en - 1.0.1 + 1.0.5 @@ -42,6 +42,9 @@ Always + + Always + diff --git a/LeanderShiftPlannerV2/Resources/templates/googleapi.txt b/LeanderShiftPlannerV2/Resources/templates/googleapi.txt new file mode 100644 index 0000000..7e858d3 --- /dev/null +++ b/LeanderShiftPlannerV2/Resources/templates/googleapi.txt @@ -0,0 +1,2 @@ +GoogleClientId= +GoogleClientSecret= \ No newline at end of file diff --git a/LeanderShiftPlannerV2/Service/TimesheetService.cs b/LeanderShiftPlannerV2/Service/TimesheetService.cs index 39bedb7..479eaf1 100644 --- a/LeanderShiftPlannerV2/Service/TimesheetService.cs +++ b/LeanderShiftPlannerV2/Service/TimesheetService.cs @@ -12,6 +12,7 @@ using LeanderShiftPlannerV2.Util; using Google.Apis.Auth.OAuth2; using Google.Apis.Calendar.v3; using Google.Apis.Services; +using LeanderShiftPlannerV2.FileIO; using Newtonsoft.Json.Linq; namespace LeanderShiftPlannerV2.Service; @@ -21,8 +22,6 @@ public class TimesheetService private readonly AppService _appService; private const string HolidayApiUriPart1 = "https://feiertage-api.de/api/?jahr="; private const string HolidayApiUriPart2 = "&nur_land="; - private const string GoogleClientId = "823920428427-ub5n9a9dkfpuvjlut4eneg2058so49o3.apps.googleusercontent.com"; - private const string GoogleClientSecret = "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"; public TimesheetService(AppService appService) { @@ -34,9 +33,18 @@ public class TimesheetService string[] scopes = { CalendarService.Scope.CalendarReadonly }; const string applicationName = "LeanderShiftPlanner"; + Dictionary secrets = GoogleApiLoader.ReadFile(); + + if (!secrets.ContainsKey(Constants.GoogleClientId) || !secrets.ContainsKey(Constants.GoogleClientSecret)) + return; + if (secrets[Constants.GoogleClientId].Equals(string.Empty) || secrets[Constants.GoogleClientSecret].Equals(string.Empty)) + return; + ClientSecrets clientSecrets = new ClientSecrets(); - clientSecrets.ClientId = GoogleClientId; - clientSecrets.ClientSecret = GoogleClientSecret; + clientSecrets.ClientId = secrets[Constants.GoogleClientId]; + clientSecrets.ClientSecret = secrets[Constants.GoogleClientSecret]; + //clientSecrets.ClientId = "823920428427-ub5n9a9dkfpuvjlut4eneg2058so49o3.apps.googleusercontent.com"; + //clientSecrets.ClientSecret = "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"; UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(clientSecrets, scopes, "user", CancellationToken.None); // Erstelle den CalendarService diff --git a/LeanderShiftPlannerV2/Util/Constants.cs b/LeanderShiftPlannerV2/Util/Constants.cs index 0158daa..f2730fa 100644 --- a/LeanderShiftPlannerV2/Util/Constants.cs +++ b/LeanderShiftPlannerV2/Util/Constants.cs @@ -17,6 +17,10 @@ namespace LeanderShiftPlannerV2.Util public const string FontPath = @"Resources/font/"; public const string FontEmblem = "Emblem.ttf"; public const string FontRoboto = "Roboto.ttf"; + public const string GoogleApiFilePath = @"/googleapi.txt"; + public const string GoogleClientId = "GoogleClientId"; + public const string GoogleClientSecret = "GoogleClientSecret"; + public const string GoogleApiFileResource = @"Resources/templates/googleapi.txt"; // TimeSheets public static readonly string TimeSheetSourceRandom = "Random"; diff --git a/LeanderShiftPlannerV2/View/LeanderShiftPlannerView/LeanderShiftPlannerMainWindow.axaml.cs b/LeanderShiftPlannerV2/View/LeanderShiftPlannerView/LeanderShiftPlannerMainWindow.axaml.cs index c362b6f..31044b8 100644 --- a/LeanderShiftPlannerV2/View/LeanderShiftPlannerView/LeanderShiftPlannerMainWindow.axaml.cs +++ b/LeanderShiftPlannerV2/View/LeanderShiftPlannerView/LeanderShiftPlannerMainWindow.axaml.cs @@ -31,6 +31,7 @@ public partial class LeanderShiftPlannerMainWindow : Window InitializeComponent(); LoadData(); + CreateApiFile(); DeactivateView(); ViewModePersons(); @@ -44,6 +45,11 @@ public partial class LeanderShiftPlannerMainWindow : Window _appService.FilterService.Filters.CollectionChanged += FilterListChanged; } + private void CreateApiFile() + { + GoogleApiLoader.CheckFileSystem(); + } + // ------------------------------------------------------UI-Interactions------------------------------------------------------ private void ButtonAddPerson_Click(object? sender, RoutedEventArgs e)