Add config file
This commit is contained in:
@ -1,9 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.IO;
|
||||
@ -12,16 +9,27 @@ using Elwig.Helpers;
|
||||
namespace Elwig {
|
||||
public partial class App : Application {
|
||||
|
||||
public static readonly string DataPath = @"C:\ProgramData\Elwig\";
|
||||
public static readonly string ExePath = @"C:\Program Files\Elwig\";
|
||||
public static readonly Config Config = new(DataPath + "config.ini");
|
||||
|
||||
public static bool IsPrintingReady => Documents.Html.IsReady && Documents.Pdf.IsReady;
|
||||
public static System.Windows.Threading.Dispatcher MainDispatcher { get; private set; }
|
||||
|
||||
public App() : base() {
|
||||
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
||||
Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "Elwig"));
|
||||
Directory.CreateDirectory(DataPath);
|
||||
MainDispatcher = Dispatcher;
|
||||
}
|
||||
|
||||
protected override void OnStartup(StartupEventArgs evt) {
|
||||
using (var ctx = new AppDbContext()) {
|
||||
if (!ctx.Database.CanConnect()) {
|
||||
MessageBox.Show($"Invalid Database:\n\n{Config.DatabasePath}", "Invalid Database", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
Shutdown();
|
||||
}
|
||||
}
|
||||
Utils.RunBackground("HTML Initialization", () => Documents.Html.Init(PrintingReadyChanged));
|
||||
Utils.RunBackground("PDF Initialization", () => Documents.Pdf.Init(PrintingReadyChanged));
|
||||
base.OnStartup(evt);
|
||||
|
@ -8,6 +8,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Elwig.Documents {
|
||||
public static class Html {
|
||||
|
||||
private static RazorLightEngine? Engine = null;
|
||||
public static bool IsReady => Engine != null;
|
||||
|
||||
|
@ -18,14 +18,14 @@ using Elwig.Windows;
|
||||
namespace Elwig.Documents {
|
||||
public static class Pdf {
|
||||
|
||||
private static readonly string CHROMIUM = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
|
||||
private static readonly string Chromium = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
|
||||
private static IBrowser? Browser = null;
|
||||
public static bool IsReady => Browser != null;
|
||||
|
||||
public static async Task Init(Action evtHandler) {
|
||||
Browser = await Puppeteer.LaunchAsync(new LaunchOptions {
|
||||
Headless = true,
|
||||
ExecutablePath = CHROMIUM,
|
||||
ExecutablePath = Chromium,
|
||||
});
|
||||
evtHandler();
|
||||
}
|
||||
|
@ -14,15 +14,15 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="ini-parser" Version="2.5.2" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Razor" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="6.0.14" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="7.0.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.3" />
|
||||
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1587.40" />
|
||||
<PackageReference Include="ModernWpfUI" Version="0.9.6" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="6.0.16" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="7.0.5" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.5" />
|
||||
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1722.45" />
|
||||
<PackageReference Include="PdfSharp" Version="1.50.5147" />
|
||||
<PackageReference Include="PuppeteerSharp" Version="9.0.2" />
|
||||
<PackageReference Include="PuppeteerSharp" Version="9.1.0" />
|
||||
<PackageReference Include="RazorLight" Version="2.3.1" />
|
||||
<PackageReference Include="System.Text.Encoding.CodePages" Version="7.0.0" />
|
||||
</ItemGroup>
|
||||
|
@ -3,6 +3,7 @@ using Elwig.Models;
|
||||
|
||||
namespace Elwig.Helpers {
|
||||
public class AppDbContext : DbContext {
|
||||
|
||||
public DbSet<Country> Countries { get; set; }
|
||||
public DbSet<Member> Members { get; set; }
|
||||
public DbSet<BillingAddress> BillingAddresses { get; set; }
|
||||
@ -22,7 +23,7 @@ namespace Elwig.Helpers {
|
||||
public DbSet<WineVar> WineVarieties { get; set; }
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
|
||||
optionsBuilder.UseSqlite("Data Source=\"C:\\Users\\lorenz\\Desktop\\wgprod.sqlite3\"; foreign keys=true");
|
||||
optionsBuilder.UseSqlite($"Data Source=\"{App.Config.DatabasePath}\"; Foreign Keys=True; Mode=ReadWrite; Cache=Default");
|
||||
optionsBuilder.UseLazyLoadingProxies();
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
|
37
Elwig/Helpers/Config.cs
Normal file
37
Elwig/Helpers/Config.cs
Normal file
@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using IniParser;
|
||||
using IniParser.Model;
|
||||
|
||||
namespace Elwig.Helpers {
|
||||
public class Config {
|
||||
|
||||
private readonly string FileName;
|
||||
public string DatabasePath;
|
||||
|
||||
public Config(string filename) {
|
||||
FileName = filename;
|
||||
Read();
|
||||
}
|
||||
|
||||
public void Read() {
|
||||
var parser = new FileIniDataParser();
|
||||
IniData? ini = null;
|
||||
try {
|
||||
ini = parser.ReadFile(FileName, Encoding.UTF8);
|
||||
} catch {}
|
||||
|
||||
if (ini == null || !ini.TryGetKey("database.path", out string db)) {
|
||||
DatabasePath = App.DataPath + "database.sqlite3";
|
||||
} else if (db.Length > 1 && db[1] == ':') {
|
||||
DatabasePath = db;
|
||||
} else {
|
||||
DatabasePath = App.DataPath + db;
|
||||
}
|
||||
}
|
||||
|
||||
public void Write() {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
@ -19,7 +19,6 @@ namespace Elwig.Windows {
|
||||
}
|
||||
|
||||
private void Window_Loaded(object sender, RoutedEventArgs evt) {
|
||||
Context.Countries.Load();
|
||||
Button4.IsEnabled = App.IsPrintingReady;
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,4 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ModernWpf.Controls;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
|
Reference in New Issue
Block a user