121 lines
5.1 KiB
C#
121 lines
5.1 KiB
C#
using System;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.IO;
|
|
using Elwig.Helpers;
|
|
using Elwig.Helpers.Weighing;
|
|
using System.Collections.Generic;
|
|
using System.Windows.Threading;
|
|
using System.Globalization;
|
|
using System.Threading;
|
|
using System.Windows.Markup;
|
|
|
|
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 string ZwstId { get; private set; }
|
|
public static IEnumerable<IScale> Scales { get; private set; }
|
|
public static ClientParameters Client { get; private set; }
|
|
|
|
public static bool IsPrintingReady => Documents.Html.IsReady && Documents.Pdf.IsReady;
|
|
public static 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;
|
|
Scales = Array.Empty<IScale>();
|
|
}
|
|
|
|
protected override void OnStartup(StartupEventArgs evt) {
|
|
var locale = new CultureInfo("de-AT");
|
|
locale.NumberFormat.CurrencyGroupSeparator = "\u202f";
|
|
locale.NumberFormat.NumberGroupSeparator = "\u202f";
|
|
locale.NumberFormat.PercentGroupSeparator = "\u202f";
|
|
Thread.CurrentThread.CurrentCulture = locale;
|
|
Thread.CurrentThread.CurrentUICulture = locale;
|
|
CultureInfo.DefaultThreadCurrentCulture = locale;
|
|
CultureInfo.DefaultThreadCurrentUICulture = locale;
|
|
FrameworkElement.LanguageProperty.OverrideMetadata(
|
|
typeof(FrameworkElement),
|
|
new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag))
|
|
);
|
|
|
|
Dictionary<string, string> branches = new();
|
|
using (var ctx = new AppDbContext()) {
|
|
if (!ctx.Database.CanConnect()) {
|
|
MessageBox.Show($"Invalid Database:\n\n{Config.DatabaseFile}", "Invalid Database", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
Shutdown();
|
|
} else {
|
|
branches = ctx.Branches.ToDictionary(b => b.Name.ToLower(), b => b.ZwstId);
|
|
}
|
|
}
|
|
Utils.RunBackground("HTML Initialization", () => Documents.Html.Init(PrintingReadyChanged));
|
|
Utils.RunBackground("PDF Initialization", () => Documents.Pdf.Init(PrintingReadyChanged));
|
|
|
|
Client = new();
|
|
|
|
var list = new LinkedList<IScale>();
|
|
foreach (var s in Config.Scales) {
|
|
try {
|
|
var scaleNr = int.Parse(s[0]);
|
|
var type = s[1].ToLower();
|
|
var model = s[2];
|
|
var cnx = s[3];
|
|
var empty = s[4];
|
|
var filling = s[5];
|
|
int? limit = s[6] == null ? null : int.Parse(s[6]);
|
|
if (type == "systec") {
|
|
list.AddLast(new SystecScale(scaleNr, model, cnx, empty, filling, limit));
|
|
} else {
|
|
throw new ArgumentException($"Invalid scale type: \"{type}\"");
|
|
}
|
|
} catch (Exception e) {
|
|
MessageBox.Show($"Unable to create scale {s[0]}:\n\n{e.Message}", "Scale Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
Scales = list;
|
|
|
|
if (Config.Branch != null) {
|
|
if (!branches.ContainsKey(Config.Branch.ToLower())) {
|
|
MessageBox.Show("Invalid branch name in config!", "Invalid Branch Config", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
Shutdown();
|
|
} else {
|
|
ZwstId = branches[Config.Branch.ToLower()];
|
|
}
|
|
} else if (branches.Count == 1) {
|
|
ZwstId = branches.First().Value;
|
|
} else {
|
|
MessageBox.Show("Unable to determine local branch!", "Invalid Branch Config", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
Shutdown();
|
|
}
|
|
|
|
base.OnStartup(evt);
|
|
}
|
|
|
|
protected override void OnExit(ExitEventArgs evt) {
|
|
Utils.RunBackground("PDF Close", () => Documents.Pdf.Close());
|
|
base.OnExit(evt);
|
|
}
|
|
|
|
private void PrintingReadyChanged() {
|
|
Dispatcher.BeginInvoke(OnPrintingReadyChanged, new EventArgs());
|
|
}
|
|
|
|
protected void OnPrintingReadyChanged(EventArgs evt) {
|
|
foreach (Window w in Windows) {
|
|
foreach (var b in ControlUtils.FindAllChildren<Button>(w).Where(b => "Print".Equals(b.Tag))) {
|
|
b.IsEnabled = IsPrintingReady;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|