mirror of
https://github.com/chylex/Discord-History-Tracker.git
synced 2025-08-17 10:31:41 +02:00
Compare commits
68 Commits
v35.2
...
app-json-2
Author | SHA1 | Date | |
---|---|---|---|
9448cd05b6
|
|||
2424a8ac8d
|
|||
e7705587c2
|
|||
bd7904b88e
|
|||
3db599e901
|
|||
69a68b463c
|
|||
b2389dd521
|
|||
610516de1f
|
|||
3a6b83e0ba
|
|||
22958536e7
|
|||
4db8c302d8
|
|||
7e7d140957
|
|||
8c68438fbb
|
|||
f625a39b4d
|
|||
7fd644449c
|
|||
e4a09515b0
|
|||
9ac9f2246f
|
|||
bbc734ba9b
|
|||
6837b05b0d
|
|||
c94808a15f
|
|||
739e87c5ab
|
|||
d463b407f4
|
|||
cd418f4871
|
|||
176a81e055 | |||
![]() |
1cf3e76644 | ||
33f5ab7cce
|
|||
b9a5664740
|
|||
845ac1b0fa
|
|||
1bead42a0e
|
|||
8f1c91b2cc
|
|||
9ae5ece24b
|
|||
053ab5b091
|
|||
71c628fdf8
|
|||
af621b8d46
|
|||
31fe6aed35
|
|||
c25426af55
|
|||
59129ba20a
|
|||
f7bfe052ca
|
|||
c9bb46c8c7
|
|||
73f4c70325
|
|||
de5a8b690b
|
|||
daa2feb445
|
|||
4e94e788bc
|
|||
133ec532d2
|
|||
3d435d0165
|
|||
3e8151e1f3
|
|||
9f98eba9c1
|
|||
6b54a80be1
|
|||
1e6e5c6f92
|
|||
2459c8ee1a
|
|||
d129a60d1c
|
|||
65ecb0177c
|
|||
d51dcb0a84
|
|||
b13b85dedd
|
|||
15e8b9da63
|
|||
9572f0f002
|
|||
2f3b8b974c
|
|||
bff86b09c7
|
|||
5ca7cf09e8
|
|||
a1c93232d0
|
|||
db5f9d65db
|
|||
4cbf387e2a
|
|||
64cf3c9fbb
|
|||
a4ebd5eed6
|
|||
06716330d6
|
|||
1a6346677e
|
|||
261be50463
|
|||
f93f5c8fdd
|
@@ -19,7 +19,7 @@ Folder organization:
|
|||||||
* `lib/` contains utilities required to build the project
|
* `lib/` contains utilities required to build the project
|
||||||
* `web/` contains source code of the [official website](https://dht.chylex.com), which can be used as a template when making your own website
|
* `web/` contains source code of the [official website](https://dht.chylex.com), which can be used as a template when making your own website
|
||||||
|
|
||||||
To start editing source code for the desktop app, install the [.NET 5 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/5.0), and then open `app/DiscordHistoryTracker.sln` in [Visual Studio](https://visualstudio.microsoft.com/downloads/) or [Rider](https://www.jetbrains.com/rider/).
|
To start editing source code for the desktop app, install the [.NET 8 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/8.0), and then open `app/DiscordHistoryTracker.sln` in [Visual Studio](https://visualstudio.microsoft.com/downloads/) or [Rider](https://www.jetbrains.com/rider/).
|
||||||
|
|
||||||
### Building
|
### Building
|
||||||
|
|
||||||
@@ -50,6 +50,6 @@ Run the `app/build.sh` script, and read the [Distribution](#distribution) sectio
|
|||||||
|
|
||||||
#### Distribution
|
#### Distribution
|
||||||
|
|
||||||
The mentioned build scripts will prepare `Release` builds ready for distribution. Once the script finishes, the `app/bin` folder will contain self-contained executables for each major operating system, and a portable version that works on all other systems but requires .NET 5 to be installed.
|
The mentioned build scripts will prepare `Release` builds ready for distribution. Once the script finishes, the `app/bin` folder will contain self-contained executables for each major operating system, and a portable version that works on all other systems but requires .NET 8 to be installed.
|
||||||
|
|
||||||
Note that when building on Windows, the generated `.zip` files for Linux and Mac will not have correct file permissions, so it will not be possible to run them by double-clicking `DiscordHistoryTracker`. I tried using Python to re-create the archives with correct file permissions, but found that Linux `zip` tools could not see them. The only working solution is building the Windows + portable version on Windows, and Linux + Mac version on Linux.
|
Note that when building on Windows, the generated `.zip` files for Linux and Mac will not have correct file permissions, so it will not be possible to run them by double-clicking `DiscordHistoryTracker`. I tried using Python to re-create the archives with correct file permissions, but found that Linux `zip` tools could not see them. The only working solution is building the Windows + portable version on Windows, and Linux + Mac version on Linux.
|
||||||
|
@@ -1,25 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="AvaloniaProject">
|
|
||||||
<option name="projectPerEditor">
|
|
||||||
<map>
|
|
||||||
<entry key="Desktop/App.axaml" value="Desktop/Desktop.csproj" />
|
|
||||||
<entry key="Desktop/Dialogs/CheckBox/CheckBoxDialog.axaml" value="Desktop/Desktop.csproj" />
|
|
||||||
<entry key="Desktop/Dialogs/Message/MessageDialog.axaml" value="Desktop/Desktop.csproj" />
|
|
||||||
<entry key="Desktop/Dialogs/Progress/ProgressDialog.axaml" value="Desktop/Desktop.csproj" />
|
|
||||||
<entry key="Desktop/Main/AboutWindow.axaml" value="Desktop/Desktop.csproj" />
|
|
||||||
<entry key="Desktop/Main/Controls/FilterPanel.axaml" value="Desktop/Desktop.csproj" />
|
|
||||||
<entry key="Desktop/Main/Controls/ServerConfigurationPanel.axaml" value="Desktop/Desktop.csproj" />
|
|
||||||
<entry key="Desktop/Main/Controls/StatusBar.axaml" value="Desktop/Desktop.csproj" />
|
|
||||||
<entry key="Desktop/Main/MainWindow.axaml" value="Desktop/Desktop.csproj" />
|
|
||||||
<entry key="Desktop/Main/Pages/AdvancedPage.axaml" value="Desktop/Desktop.csproj" />
|
|
||||||
<entry key="Desktop/Main/Pages/DatabasePage.axaml" value="Desktop/Desktop.csproj" />
|
|
||||||
<entry key="Desktop/Main/Pages/DebugPage.axaml" value="Desktop/Desktop.csproj" />
|
|
||||||
<entry key="Desktop/Main/Pages/TrackingPage.axaml" value="Desktop/Desktop.csproj" />
|
|
||||||
<entry key="Desktop/Main/Pages/ViewerPage.axaml" value="Desktop/Desktop.csproj" />
|
|
||||||
<entry key="Desktop/Main/Screens/MainContentScreen.axaml" value="Desktop/Desktop.csproj" />
|
|
||||||
<entry key="Desktop/Main/Screens/WelcomeScreen.axaml" value="Desktop/Desktop.csproj" />
|
|
||||||
</map>
|
|
||||||
</option>
|
|
||||||
</component>
|
|
||||||
</project>
|
|
@@ -1,20 +0,0 @@
|
|||||||
using Avalonia;
|
|
||||||
using Avalonia.Controls.ApplicationLifetimes;
|
|
||||||
using Avalonia.Markup.Xaml;
|
|
||||||
using DHT.Desktop.Main;
|
|
||||||
|
|
||||||
namespace DHT.Desktop {
|
|
||||||
sealed class App : Application {
|
|
||||||
public override void Initialize() {
|
|
||||||
AvaloniaXamlLoader.Load(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void OnFrameworkInitializationCompleted() {
|
|
||||||
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) {
|
|
||||||
desktop.MainWindow = new MainWindow(new Arguments(desktop.Args));
|
|
||||||
}
|
|
||||||
|
|
||||||
base.OnFrameworkInitializationCompleted();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,12 +1,13 @@
|
|||||||
<Application xmlns="https://github.com/avaloniaui"
|
<Application xmlns="https://github.com/avaloniaui"
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:common="clr-namespace:DHT.Desktop.Common"
|
|
||||||
xmlns:system="clr-namespace:System;assembly=System.Runtime"
|
xmlns:system="clr-namespace:System;assembly=System.Runtime"
|
||||||
x:Class="DHT.Desktop.App">
|
xmlns:common="clr-namespace:DHT.Desktop.App.Common"
|
||||||
|
x:Class="DHT.Desktop.App.App">
|
||||||
|
|
||||||
<Application.Styles>
|
<Application.Styles>
|
||||||
|
|
||||||
<FluentTheme Mode="Light" />
|
<FluentTheme />
|
||||||
|
<StyleInclude Source="avares://Avalonia.Controls.DataGrid/Themes/Simple.xaml" />
|
||||||
|
|
||||||
<Style Selector="Button, CheckBox, RadioButton, Expander /template/ ToggleButton#ExpanderHeader">
|
<Style Selector="Button, CheckBox, RadioButton, Expander /template/ ToggleButton#ExpanderHeader">
|
||||||
<Setter Property="Cursor" Value="Hand" />
|
<Setter Property="Cursor" Value="Hand" />
|
||||||
@@ -32,79 +33,32 @@
|
|||||||
<Style Selector="TextBox:focus /template/ Border#PART_BorderElement">
|
<Style Selector="TextBox:focus /template/ Border#PART_BorderElement">
|
||||||
<Setter Property="BorderThickness" Value="1" />
|
<Setter Property="BorderThickness" Value="1" />
|
||||||
</Style>
|
</Style>
|
||||||
<Style Selector="TextBox:disabled"><!-- TODO bug in Avalonia (https://github.com/AvaloniaUI/Avalonia/pull/7792) -->
|
<Style Selector="TextBox:error DataValidationErrors">
|
||||||
<Setter Property="Foreground" Value="{DynamicResource TextControlForegroundDisabled}" />
|
<Style.Resources>
|
||||||
</Style>
|
<ControlTemplate x:Key="InlineDataValidationContentTemplate" TargetType="DataValidationErrors">
|
||||||
|
<ContentPresenter Name="PART_ContentPresenter"
|
||||||
<Style Selector="Expander /template/ ToggleButton#ExpanderHeader">
|
Padding="{TemplateBinding Padding}"
|
||||||
<Setter Property="HorizontalContentAlignment" Value="Left" />
|
Background="{TemplateBinding Background}"
|
||||||
<Setter Property="Template">
|
BorderThickness="{TemplateBinding BorderThickness}"
|
||||||
<ControlTemplate>
|
CornerRadius="{TemplateBinding CornerRadius}"
|
||||||
<Border x:Name="ToggleButtonBackground">
|
Content="{TemplateBinding Content}"
|
||||||
<Grid ColumnDefinitions="Auto,*" RowDefinitions="35">
|
ContentTemplate="{TemplateBinding ContentTemplate}" />
|
||||||
<Border x:Name="ExpandCollapseChevronBorder"
|
|
||||||
Grid.Column="0"
|
|
||||||
Width="35"
|
|
||||||
Height="35"
|
|
||||||
Margin="2,0"
|
|
||||||
RenderTransformOrigin="50%,50%">
|
|
||||||
<Path x:Name="ExpandCollapseChevron"
|
|
||||||
HorizontalAlignment="Center"
|
|
||||||
VerticalAlignment="Center"
|
|
||||||
RenderTransformOrigin="50%,50%"
|
|
||||||
Stretch="None"
|
|
||||||
Stroke="{DynamicResource ExpanderChevronForeground}"
|
|
||||||
StrokeThickness="1" />
|
|
||||||
<Border.RenderTransform>
|
|
||||||
<RotateTransform />
|
|
||||||
</Border.RenderTransform>
|
|
||||||
</Border>
|
|
||||||
<ContentPresenter x:Name="PART_ContentPresenter"
|
|
||||||
Grid.Column="1"
|
|
||||||
Margin="0"
|
|
||||||
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
|
|
||||||
VerticalContentAlignment="Center"
|
|
||||||
Background="Transparent"
|
|
||||||
BorderBrush="Transparent"
|
|
||||||
BorderThickness="0"
|
|
||||||
Content="{TemplateBinding Content}"
|
|
||||||
ContentTemplate="{TemplateBinding ContentTemplate}"
|
|
||||||
TextBlock.Foreground="{DynamicResource ExpanderForeground}" />
|
|
||||||
</Grid>
|
|
||||||
</Border>
|
|
||||||
</ControlTemplate>
|
</ControlTemplate>
|
||||||
</Setter>
|
</Style.Resources>
|
||||||
</Style>
|
<Setter Property="Template" Value="{StaticResource InlineDataValidationContentTemplate}" />
|
||||||
<Style Selector="Expander:expanded /template/ ToggleButton#ExpanderHeader /template/ Border#ExpandCollapseChevronBorder">
|
|
||||||
<Style.Animations>
|
|
||||||
<Animation FillMode="Both" Duration="0:0:0.0625">
|
|
||||||
<KeyFrame Cue="0%">
|
|
||||||
<Setter Property="RotateTransform.Angle" Value="180" />
|
|
||||||
</KeyFrame>
|
|
||||||
<KeyFrame Cue="100%">
|
|
||||||
<Setter Property="RotateTransform.Angle" Value="180" />
|
|
||||||
</KeyFrame>
|
|
||||||
</Animation>
|
|
||||||
</Style.Animations>
|
|
||||||
</Style>
|
|
||||||
<Style Selector="Expander:not(:expanded) /template/ ToggleButton#ExpanderHeader /template/ Border#ExpandCollapseChevronBorder">
|
|
||||||
<Style.Animations>
|
|
||||||
<Animation FillMode="Both" Duration="0:0:0.0625">
|
|
||||||
<KeyFrame Cue="0%">
|
|
||||||
<Setter Property="RotateTransform.Angle" Value="0" />
|
|
||||||
</KeyFrame>
|
|
||||||
<KeyFrame Cue="100%">
|
|
||||||
<Setter Property="RotateTransform.Angle" Value="0" />
|
|
||||||
</KeyFrame>
|
|
||||||
</Animation>
|
|
||||||
</Style.Animations>
|
|
||||||
</Style>
|
</Style>
|
||||||
|
|
||||||
|
<Style Selector="Expander">
|
||||||
|
<Setter Property="MinHeight" Value="40" />
|
||||||
|
<Setter Property="HorizontalAlignment" Value="Stretch" />
|
||||||
|
</Style>
|
||||||
|
|
||||||
</Application.Styles>
|
</Application.Styles>
|
||||||
|
|
||||||
<Application.Resources>
|
<Application.Resources>
|
||||||
|
|
||||||
<common:NumberValueConverter x:Key="NumberValueConverter" />
|
<common:NumberValueConverter x:Key="NumberValueConverter" />
|
||||||
|
<common:BytesValueConverter x:Key="BytesValueConverter" />
|
||||||
|
|
||||||
<system:Double x:Key="ControlContentThemeFontSize">14</system:Double>
|
<system:Double x:Key="ControlContentThemeFontSize">14</system:Double>
|
||||||
<CornerRadius x:Key="ControlCornerRadius">0</CornerRadius>
|
<CornerRadius x:Key="ControlCornerRadius">0</CornerRadius>
|
||||||
@@ -152,15 +106,29 @@
|
|||||||
<SolidColorBrush x:Key="TextControlPlaceholderForegroundPointerOver" Color="#AAAAAA" />
|
<SolidColorBrush x:Key="TextControlPlaceholderForegroundPointerOver" Color="#AAAAAA" />
|
||||||
<SolidColorBrush x:Key="TextControlPlaceholderForegroundFocused" Color="#AAAAAA" />
|
<SolidColorBrush x:Key="TextControlPlaceholderForegroundFocused" Color="#AAAAAA" />
|
||||||
<SolidColorBrush x:Key="TextControlPlaceholderForegroundDisabled" Color="#AAAAAA" />
|
<SolidColorBrush x:Key="TextControlPlaceholderForegroundDisabled" Color="#AAAAAA" />
|
||||||
|
|
||||||
<Thickness x:Key="ExpanderHeaderPadding">15,0</Thickness>
|
<Thickness x:Key="ExpanderHeaderPadding">15,0</Thickness>
|
||||||
<Thickness x:Key="ExpanderContentPadding">15</Thickness>
|
<Thickness x:Key="ExpanderContentPadding">12</Thickness>
|
||||||
<SolidColorBrush x:Key="ExpanderBorderBrush" Color="#697DAB" />
|
<SolidColorBrush x:Key="ExpanderHeaderBorderBrush" Color="#697DAB" />
|
||||||
<SolidColorBrush x:Key="ExpanderBackground" Color="#697DAB" />
|
<SolidColorBrush x:Key="ExpanderHeaderBorderBrushPointerOver" Color="#697DAB" />
|
||||||
<SolidColorBrush x:Key="ExpanderForeground" Color="#FFFFFF" />
|
<SolidColorBrush x:Key="ExpanderHeaderBorderBrushPressed" Color="#697DAB" />
|
||||||
|
<SolidColorBrush x:Key="ExpanderHeaderBorderBrushDisabled" Color="#697DAB" />
|
||||||
|
<SolidColorBrush x:Key="ExpanderHeaderBackground" Color="#697DAB" />
|
||||||
|
<SolidColorBrush x:Key="ExpanderHeaderBackgroundPointerOver" Color="#536794" />
|
||||||
|
<SolidColorBrush x:Key="ExpanderHeaderBackgroundPressed" Color="#47587F" />
|
||||||
|
<SolidColorBrush x:Key="ExpanderHeaderBackgroundDisabled" Color="#697DAB" />
|
||||||
|
<SolidColorBrush x:Key="ExpanderHeaderForeground" Color="#FFFFFF" />
|
||||||
|
<SolidColorBrush x:Key="ExpanderHeaderForegroundPointerOver" Color="#FFFFFF" />
|
||||||
|
<SolidColorBrush x:Key="ExpanderHeaderForegroundPressed" Color="#FFFFFF" />
|
||||||
|
<SolidColorBrush x:Key="ExpanderHeaderForegroundDisabled" Color="#FFFFFF" />
|
||||||
|
<SolidColorBrush x:Key="ExpanderChevronBackground" Color="Transparent" />
|
||||||
|
<SolidColorBrush x:Key="ExpanderChevronBackgroundPointerOver" Color="#536794" />
|
||||||
|
<SolidColorBrush x:Key="ExpanderChevronBackgroundPressed" Color="#47587F" />
|
||||||
|
<SolidColorBrush x:Key="ExpanderChevronBackgroundDisabled" Color="Transparent" />
|
||||||
<SolidColorBrush x:Key="ExpanderChevronForeground" Color="#FFFFFF" />
|
<SolidColorBrush x:Key="ExpanderChevronForeground" Color="#FFFFFF" />
|
||||||
<SolidColorBrush x:Key="ExpanderDropDownBorderBrush" Color="#697DAB" />
|
<SolidColorBrush x:Key="ExpanderChevronForegroundPointerOver" Color="#FFFFFF" />
|
||||||
<SolidColorBrush x:Key="ExpanderDropDownBackground" Color="#FFFFFF" />
|
<SolidColorBrush x:Key="ExpanderChevronForegroundPressed" Color="#FFFFFF" />
|
||||||
|
<SolidColorBrush x:Key="ExpanderChevronForegroundDisabled" Color="#FFFFFF" />
|
||||||
|
|
||||||
</Application.Resources>
|
</Application.Resources>
|
||||||
|
|
21
app/Desktop/App/App.axaml.cs
Normal file
21
app/Desktop/App/App.axaml.cs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
using System;
|
||||||
|
using Avalonia;
|
||||||
|
using Avalonia.Controls.ApplicationLifetimes;
|
||||||
|
using Avalonia.Markup.Xaml;
|
||||||
|
using DHT.Desktop.App.Windows;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App;
|
||||||
|
|
||||||
|
sealed class App : Application {
|
||||||
|
public override void Initialize() {
|
||||||
|
AvaloniaXamlLoader.Load(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnFrameworkInitializationCompleted() {
|
||||||
|
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) {
|
||||||
|
desktop.MainWindow = new MainWindow(new Arguments(desktop.Args ?? Array.Empty<string>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
base.OnFrameworkInitializationCompleted();
|
||||||
|
}
|
||||||
|
}
|
53
app/Desktop/App/Common/BytesValueConverter.cs
Normal file
53
app/Desktop/App/Common/BytesValueConverter.cs
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
using System;
|
||||||
|
using System.Globalization;
|
||||||
|
using Avalonia.Data.Converters;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Common;
|
||||||
|
|
||||||
|
sealed class BytesValueConverter : IValueConverter {
|
||||||
|
private sealed class Unit {
|
||||||
|
private readonly string label;
|
||||||
|
private readonly string numberFormat;
|
||||||
|
|
||||||
|
public Unit(string label, int decimalPlaces) {
|
||||||
|
this.label = label;
|
||||||
|
this.numberFormat = "{0:n" + decimalPlaces + "}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Format(double size) {
|
||||||
|
return string.Format(Program.Culture, numberFormat, size) + " " + label;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static readonly Unit[] Units = {
|
||||||
|
new ("B", decimalPlaces: 0),
|
||||||
|
new ("kB", decimalPlaces: 0),
|
||||||
|
new ("MB", decimalPlaces: 1),
|
||||||
|
new ("GB", decimalPlaces: 1),
|
||||||
|
new ("TB", decimalPlaces: 1)
|
||||||
|
};
|
||||||
|
|
||||||
|
private const int Scale = 1000;
|
||||||
|
|
||||||
|
private static string Convert(ulong size) {
|
||||||
|
int power = size == 0L ? 0 : (int) Math.Log(size, Scale);
|
||||||
|
int unit = power >= Units.Length ? Units.Length - 1 : power;
|
||||||
|
return Units[unit].Format(unit == 0 ? size : size / Math.Pow(Scale, unit));
|
||||||
|
}
|
||||||
|
|
||||||
|
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) {
|
||||||
|
if (value is long size and >= 0L) {
|
||||||
|
return Convert((ulong) size);
|
||||||
|
}
|
||||||
|
else if (value is ulong usize) {
|
||||||
|
return Convert(usize);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return "-";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) {
|
||||||
|
throw new NotSupportedException();
|
||||||
|
}
|
||||||
|
}
|
68
app/Desktop/App/Common/DatabaseGui.cs
Normal file
68
app/Desktop/App/Common/DatabaseGui.cs
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Platform.Storage;
|
||||||
|
using DHT.Desktop.App.Dialogs.File;
|
||||||
|
using DHT.Desktop.App.Dialogs.Message;
|
||||||
|
using DHT.Server.Database;
|
||||||
|
using DHT.Server.Database.Exceptions;
|
||||||
|
using DHT.Server.Database.Sqlite;
|
||||||
|
using DHT.Utils.Logging;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Common;
|
||||||
|
|
||||||
|
static class DatabaseGui {
|
||||||
|
private static readonly Log Log = Log.ForType(typeof(DatabaseGui));
|
||||||
|
|
||||||
|
private const string DatabaseFileInitialName = "archive.dht";
|
||||||
|
|
||||||
|
private static readonly IReadOnlyList<FilePickerFileType> DatabaseFileDialogFilter = new List<FilePickerFileType> {
|
||||||
|
FileDialogs.CreateFilter("Discord History Tracker Database", new [] { "dht" })
|
||||||
|
};
|
||||||
|
|
||||||
|
public static async Task<string[]> NewOpenDatabaseFilesDialog(Window window, string? suggestedDirectory) {
|
||||||
|
return await window.StorageProvider.OpenFiles(new FilePickerOpenOptions {
|
||||||
|
Title = "Open Database File",
|
||||||
|
FileTypeFilter = DatabaseFileDialogFilter,
|
||||||
|
SuggestedStartLocation = await FileDialogs.GetSuggestedStartLocation(window, suggestedDirectory),
|
||||||
|
AllowMultiple = true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<string?> NewOpenOrCreateDatabaseFileDialog(Window window, string? suggestedDirectory) {
|
||||||
|
return await window.StorageProvider.SaveFile(new FilePickerSaveOptions {
|
||||||
|
Title = "Open or Create Database File",
|
||||||
|
FileTypeChoices = DatabaseFileDialogFilter,
|
||||||
|
SuggestedFileName = DatabaseFileInitialName,
|
||||||
|
SuggestedStartLocation = await FileDialogs.GetSuggestedStartLocation(window, suggestedDirectory),
|
||||||
|
ShowOverwritePrompt = false
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<IDatabaseFile?> TryOpenOrCreateDatabaseFromPath(string path, Window window, Func<Task<bool>> checkCanUpgradeDatabase) {
|
||||||
|
IDatabaseFile? file = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
file = await SqliteDatabaseFile.OpenOrCreate(path, checkCanUpgradeDatabase);
|
||||||
|
} catch (InvalidDatabaseVersionException ex) {
|
||||||
|
await Dialog.ShowOk(window, "Database Error", "Database '" + Path.GetFileName(path) + "' appears to be corrupted (invalid version: " + ex.Version + ").");
|
||||||
|
} catch (DatabaseTooNewException ex) {
|
||||||
|
await Dialog.ShowOk(window, "Database Error", "Database '" + Path.GetFileName(path) + "' was opened in a newer version of DHT (database version " + ex.DatabaseVersion + ", app version " + ex.CurrentVersion + ").");
|
||||||
|
} catch (Exception ex) {
|
||||||
|
Log.Error(ex);
|
||||||
|
await Dialog.ShowOk(window, "Database Error", "Database '" + Path.GetFileName(path) + "' could not be opened:" + ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<DialogResult.YesNo> ShowCanUpgradeDatabaseDialog(Window window) {
|
||||||
|
return await Dialog.ShowYesNo(window, "Database Upgrade", "This database was created with an older version of DHT. If you proceed, the database will be upgraded and will no longer open in previous versions of DHT.\n\nPlease ensure you have a backup of the database. Do you want to proceed with the upgrade?");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<DialogResult.YesNo> ShowCanUpgradeMultipleDatabaseDialog(Window window) {
|
||||||
|
return await Dialog.ShowYesNo(window, "Database Upgrade", "One or more databases were created with an older version of DHT. If you proceed, these databases will be upgraded and will no longer open in previous versions of DHT. Otherwise, these databases will be skipped.\n\nPlease ensure you have a backup of the databases. Do you want to proceed with the upgrade?");
|
||||||
|
}
|
||||||
|
}
|
15
app/Desktop/App/Common/NumberValueConverter.cs
Normal file
15
app/Desktop/App/Common/NumberValueConverter.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Globalization;
|
||||||
|
using Avalonia.Data.Converters;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Common;
|
||||||
|
|
||||||
|
sealed class NumberValueConverter : IValueConverter {
|
||||||
|
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) {
|
||||||
|
return value == null ? "-" : string.Format(Program.Culture, "{0:n0}", value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) {
|
||||||
|
throw new NotSupportedException();
|
||||||
|
}
|
||||||
|
}
|
19
app/Desktop/App/Common/TextFormat.cs
Normal file
19
app/Desktop/App/Common/TextFormat.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
namespace DHT.Desktop.App.Common;
|
||||||
|
|
||||||
|
static class TextFormat {
|
||||||
|
public static string Format(this int number) {
|
||||||
|
return number.ToString("N0", Program.Culture);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string Format(this long number) {
|
||||||
|
return number.ToString("N0", Program.Culture);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string Pluralize(this int number, string singular) {
|
||||||
|
return number.Format() + "\u00A0" + (number == 1 ? singular : singular + "s");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string Pluralize(this long number, string singular) {
|
||||||
|
return number.Format() + "\u00A0" + (number == 1 ? singular : singular + "s");
|
||||||
|
}
|
||||||
|
}
|
@@ -2,9 +2,9 @@
|
|||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
xmlns:controls="clr-namespace:DHT.Desktop.Main.Controls"
|
xmlns:controls="clr-namespace:DHT.Desktop.App.Controls"
|
||||||
mc:Ignorable="d"
|
mc:Ignorable="d"
|
||||||
x:Class="DHT.Desktop.Main.Controls.ServerConfigurationPanel">
|
x:Class="DHT.Desktop.App.Controls.ServerConfigurationPanel">
|
||||||
|
|
||||||
<Design.DataContext>
|
<Design.DataContext>
|
||||||
<controls:ServerConfigurationPanelModel />
|
<controls:ServerConfigurationPanelModel />
|
||||||
@@ -24,7 +24,7 @@
|
|||||||
<StackPanel>
|
<StackPanel>
|
||||||
<Button Command="{Binding OnClickToggleServerButton}" Content="{Binding ToggleServerButtonText}" IsEnabled="{Binding IsToggleServerButtonEnabled}" />
|
<Button Command="{Binding OnClickToggleServerButton}" Content="{Binding ToggleServerButtonText}" IsEnabled="{Binding IsToggleServerButtonEnabled}" />
|
||||||
<TextBlock TextWrapping="Wrap" Margin="0 15">
|
<TextBlock TextWrapping="Wrap" Margin="0 15">
|
||||||
The following settings determine how the tracking script communicates with this application. If you change them, you will have to copy and apply the tracking script again.
|
The following settings determine how the tracking script communicates with this application. If you change them, you will have to copy/paste the tracking script again.
|
||||||
</TextBlock>
|
</TextBlock>
|
||||||
<WrapPanel>
|
<WrapPanel>
|
||||||
<StackPanel>
|
<StackPanel>
|
11
app/Desktop/App/Controls/ServerConfigurationPanel.axaml.cs
Normal file
11
app/Desktop/App/Controls/ServerConfigurationPanel.axaml.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Controls;
|
||||||
|
|
||||||
|
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
||||||
|
public sealed partial class ServerConfigurationPanel : UserControl {
|
||||||
|
public ServerConfigurationPanel() {
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
}
|
116
app/Desktop/App/Controls/ServerConfigurationPanelModel.cs
Normal file
116
app/Desktop/App/Controls/ServerConfigurationPanelModel.cs
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
using System;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using DHT.Desktop.App.Dialogs.Message;
|
||||||
|
using DHT.Desktop.Server;
|
||||||
|
using DHT.Server.Database;
|
||||||
|
using DHT.Server.Service;
|
||||||
|
using DHT.Utils.Models;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Controls;
|
||||||
|
|
||||||
|
sealed class ServerConfigurationPanelModel : BaseModel, IDisposable {
|
||||||
|
private string inputPort;
|
||||||
|
|
||||||
|
public string InputPort {
|
||||||
|
get => inputPort;
|
||||||
|
set {
|
||||||
|
Change(ref inputPort, value);
|
||||||
|
OnPropertyChanged(nameof(HasMadeChanges));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private string inputToken;
|
||||||
|
|
||||||
|
public string InputToken {
|
||||||
|
get => inputToken;
|
||||||
|
set {
|
||||||
|
Change(ref inputToken, value);
|
||||||
|
OnPropertyChanged(nameof(HasMadeChanges));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool HasMadeChanges => ServerManager.Port.ToString() != InputPort || ServerManager.Token != InputToken;
|
||||||
|
|
||||||
|
private bool isToggleServerButtonEnabled = true;
|
||||||
|
|
||||||
|
public bool IsToggleServerButtonEnabled {
|
||||||
|
get => isToggleServerButtonEnabled;
|
||||||
|
set => Change(ref isToggleServerButtonEnabled, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public string ToggleServerButtonText => serverManager.IsRunning ? "Stop Server" : "Start Server";
|
||||||
|
|
||||||
|
public event EventHandler<StatusBarModel.Status>? ServerStatusChanged;
|
||||||
|
|
||||||
|
private readonly Window window;
|
||||||
|
private readonly ServerManager serverManager;
|
||||||
|
|
||||||
|
[Obsolete("Designer")]
|
||||||
|
public ServerConfigurationPanelModel() : this(null!, new ServerManager(DummyDatabaseFile.Instance)) {}
|
||||||
|
|
||||||
|
public ServerConfigurationPanelModel(Window window, ServerManager serverManager) {
|
||||||
|
this.window = window;
|
||||||
|
this.serverManager = serverManager;
|
||||||
|
this.inputPort = ServerManager.Port.ToString();
|
||||||
|
this.inputToken = ServerManager.Token;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Initialize() {
|
||||||
|
ServerLauncher.ServerStatusChanged += ServerLauncherOnServerStatusChanged;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose() {
|
||||||
|
ServerLauncher.ServerStatusChanged -= ServerLauncherOnServerStatusChanged;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ServerLauncherOnServerStatusChanged(object? sender, EventArgs e) {
|
||||||
|
ServerStatusChanged?.Invoke(this, serverManager.IsRunning ? StatusBarModel.Status.Ready : StatusBarModel.Status.Stopped);
|
||||||
|
OnPropertyChanged(nameof(ToggleServerButtonText));
|
||||||
|
IsToggleServerButtonEnabled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void BeforeServerStart() {
|
||||||
|
IsToggleServerButtonEnabled = false;
|
||||||
|
ServerStatusChanged?.Invoke(this, StatusBarModel.Status.Starting);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void StartServer() {
|
||||||
|
BeforeServerStart();
|
||||||
|
serverManager.Launch();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void StopServer() {
|
||||||
|
IsToggleServerButtonEnabled = false;
|
||||||
|
ServerStatusChanged?.Invoke(this, StatusBarModel.Status.Stopping);
|
||||||
|
serverManager.Stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnClickToggleServerButton() {
|
||||||
|
if (serverManager.IsRunning) {
|
||||||
|
StopServer();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
StartServer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnClickRandomizeToken() {
|
||||||
|
InputToken = ServerUtils.GenerateRandomToken(20);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async void OnClickApplyChanges() {
|
||||||
|
if (!ushort.TryParse(InputPort, out ushort port)) {
|
||||||
|
await Dialog.ShowOk(window, "Invalid Port", "Port must be a number between 0 and 65535.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
BeforeServerStart();
|
||||||
|
serverManager.Relaunch(port, InputToken);
|
||||||
|
OnPropertyChanged(nameof(HasMadeChanges));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnClickCancelChanges() {
|
||||||
|
InputPort = ServerManager.Port.ToString();
|
||||||
|
InputToken = ServerManager.Token;
|
||||||
|
}
|
||||||
|
}
|
@@ -2,9 +2,9 @@
|
|||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
xmlns:controls="clr-namespace:DHT.Desktop.Main.Controls"
|
xmlns:controls="clr-namespace:DHT.Desktop.App.Controls"
|
||||||
mc:Ignorable="d"
|
mc:Ignorable="d"
|
||||||
x:Class="DHT.Desktop.Main.Controls.StatusBar">
|
x:Class="DHT.Desktop.App.Controls.StatusBar">
|
||||||
|
|
||||||
<Design.DataContext>
|
<Design.DataContext>
|
||||||
<controls:StatusBarModel />
|
<controls:StatusBarModel />
|
11
app/Desktop/App/Controls/StatusBar.axaml.cs
Normal file
11
app/Desktop/App/Controls/StatusBar.axaml.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Controls;
|
||||||
|
|
||||||
|
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
||||||
|
public sealed partial class StatusBar : UserControl {
|
||||||
|
public StatusBar() {
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
}
|
34
app/Desktop/App/Controls/StatusBarModel.cs
Normal file
34
app/Desktop/App/Controls/StatusBarModel.cs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
using DHT.Utils.Models;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Controls;
|
||||||
|
|
||||||
|
sealed class StatusBarModel : BaseModel {
|
||||||
|
private Status status = Status.Stopped;
|
||||||
|
|
||||||
|
public Status CurrentStatus {
|
||||||
|
get => status;
|
||||||
|
set {
|
||||||
|
status = value;
|
||||||
|
OnPropertyChanged(nameof(StatusText));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public string StatusText {
|
||||||
|
get {
|
||||||
|
return CurrentStatus switch {
|
||||||
|
Status.Starting => "STARTING",
|
||||||
|
Status.Ready => "READY",
|
||||||
|
Status.Stopping => "STOPPING",
|
||||||
|
Status.Stopped => "STOPPED",
|
||||||
|
_ => ""
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum Status {
|
||||||
|
Starting,
|
||||||
|
Ready,
|
||||||
|
Stopping,
|
||||||
|
Stopped
|
||||||
|
}
|
||||||
|
}
|
@@ -2,16 +2,16 @@
|
|||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
xmlns:namespace="clr-namespace:DHT.Desktop.Dialogs.CheckBox"
|
xmlns:checkBox="clr-namespace:DHT.Desktop.App.Dialogs.CheckBox"
|
||||||
mc:Ignorable="d" d:DesignWidth="500"
|
mc:Ignorable="d" d:DesignWidth="500"
|
||||||
x:Class="DHT.Desktop.Dialogs.CheckBox.CheckBoxDialog"
|
x:Class="DHT.Desktop.App.Dialogs.CheckBox.CheckBoxDialog"
|
||||||
Title="{Binding Title}"
|
Title="{Binding Title}"
|
||||||
Icon="avares://DiscordHistoryTracker/Resources/icon.ico"
|
Icon="avares://DiscordHistoryTracker/Resources/icon.ico"
|
||||||
Width="500" SizeToContent="Height" CanResize="False"
|
Width="500" SizeToContent="Height" CanResize="False"
|
||||||
WindowStartupLocation="CenterOwner">
|
WindowStartupLocation="CenterOwner">
|
||||||
|
|
||||||
<Window.DataContext>
|
<Window.DataContext>
|
||||||
<namespace:CheckBoxDialogModel />
|
<checkBox:CheckBoxDialogModel />
|
||||||
</Window.DataContext>
|
</Window.DataContext>
|
||||||
|
|
||||||
<Window.Styles>
|
<Window.Styles>
|
||||||
@@ -34,8 +34,8 @@
|
|||||||
|
|
||||||
<StackPanel Margin="20">
|
<StackPanel Margin="20">
|
||||||
<ScrollViewer MaxHeight="400">
|
<ScrollViewer MaxHeight="400">
|
||||||
<ItemsControl Items="{Binding Items}">
|
<ItemsRepeater ItemsSource="{Binding Items}">
|
||||||
<ItemsControl.ItemTemplate>
|
<ItemsRepeater.ItemTemplate>
|
||||||
<DataTemplate>
|
<DataTemplate>
|
||||||
<CheckBox IsChecked="{Binding Checked}">
|
<CheckBox IsChecked="{Binding Checked}">
|
||||||
<Label>
|
<Label>
|
||||||
@@ -43,8 +43,8 @@
|
|||||||
</Label>
|
</Label>
|
||||||
</CheckBox>
|
</CheckBox>
|
||||||
</DataTemplate>
|
</DataTemplate>
|
||||||
</ItemsControl.ItemTemplate>
|
</ItemsRepeater.ItemTemplate>
|
||||||
</ItemsControl>
|
</ItemsRepeater>
|
||||||
</ScrollViewer>
|
</ScrollViewer>
|
||||||
<Panel Classes="buttons">
|
<Panel Classes="buttons">
|
||||||
<WrapPanel Classes="left">
|
<WrapPanel Classes="left">
|
21
app/Desktop/App/Dialogs/CheckBox/CheckBoxDialog.axaml.cs
Normal file
21
app/Desktop/App/Dialogs/CheckBox/CheckBoxDialog.axaml.cs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Interactivity;
|
||||||
|
using DHT.Desktop.App.Dialogs.Message;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Dialogs.CheckBox;
|
||||||
|
|
||||||
|
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
||||||
|
public sealed partial class CheckBoxDialog : Window {
|
||||||
|
public CheckBoxDialog() {
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ClickOk(object? sender, RoutedEventArgs e) {
|
||||||
|
Close(DialogResult.OkCancel.Ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ClickCancel(object? sender, RoutedEventArgs e) {
|
||||||
|
Close(DialogResult.OkCancel.Cancel);
|
||||||
|
}
|
||||||
|
}
|
70
app/Desktop/App/Dialogs/CheckBox/CheckBoxDialogModel.cs
Normal file
70
app/Desktop/App/Dialogs/CheckBox/CheckBoxDialogModel.cs
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Linq;
|
||||||
|
using DHT.Utils.Models;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Dialogs.CheckBox;
|
||||||
|
|
||||||
|
class CheckBoxDialogModel : BaseModel {
|
||||||
|
public string Title { get; init; } = "";
|
||||||
|
|
||||||
|
private IReadOnlyList<CheckBoxItem> items = Array.Empty<CheckBoxItem>();
|
||||||
|
|
||||||
|
public IReadOnlyList<CheckBoxItem> Items {
|
||||||
|
get => items;
|
||||||
|
|
||||||
|
protected set {
|
||||||
|
foreach (var item in items) {
|
||||||
|
item.PropertyChanged -= OnItemPropertyChanged;
|
||||||
|
}
|
||||||
|
|
||||||
|
items = value;
|
||||||
|
|
||||||
|
foreach (var item in items) {
|
||||||
|
item.PropertyChanged += OnItemPropertyChanged;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool pauseCheckEvents = false;
|
||||||
|
|
||||||
|
public bool AreAllSelected => Items.All(static item => item.Checked);
|
||||||
|
public bool AreNoneSelected => Items.All(static item => !item.Checked);
|
||||||
|
|
||||||
|
public void SelectAll() => SetAllChecked(true);
|
||||||
|
public void SelectNone() => SetAllChecked(false);
|
||||||
|
|
||||||
|
private void SetAllChecked(bool isChecked) {
|
||||||
|
pauseCheckEvents = true;
|
||||||
|
|
||||||
|
foreach (var item in Items) {
|
||||||
|
item.Checked = isChecked;
|
||||||
|
}
|
||||||
|
|
||||||
|
pauseCheckEvents = false;
|
||||||
|
UpdateBulkButtons();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdateBulkButtons() {
|
||||||
|
OnPropertyChanged(nameof(AreAllSelected));
|
||||||
|
OnPropertyChanged(nameof(AreNoneSelected));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnItemPropertyChanged(object? sender, PropertyChangedEventArgs e) {
|
||||||
|
if (!pauseCheckEvents && e.PropertyName == nameof(CheckBoxItem.Checked)) {
|
||||||
|
UpdateBulkButtons();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class CheckBoxDialogModel<T> : CheckBoxDialogModel {
|
||||||
|
public new IReadOnlyList<CheckBoxItem<T>> Items { get; }
|
||||||
|
|
||||||
|
public IEnumerable<CheckBoxItem<T>> SelectedItems => Items.Where(static item => item.Checked);
|
||||||
|
|
||||||
|
public CheckBoxDialogModel(IEnumerable<CheckBoxItem<T>> items) {
|
||||||
|
this.Items = new List<CheckBoxItem<T>>(items);
|
||||||
|
base.Items = this.Items;
|
||||||
|
}
|
||||||
|
}
|
24
app/Desktop/App/Dialogs/CheckBox/CheckBoxItem.cs
Normal file
24
app/Desktop/App/Dialogs/CheckBox/CheckBoxItem.cs
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
using DHT.Utils.Models;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Dialogs.CheckBox;
|
||||||
|
|
||||||
|
class CheckBoxItem : BaseModel {
|
||||||
|
public string Title { get; init; } = "";
|
||||||
|
public object? Item { get; init; } = null;
|
||||||
|
|
||||||
|
private bool isChecked = false;
|
||||||
|
|
||||||
|
public bool Checked {
|
||||||
|
get => isChecked;
|
||||||
|
set => Change(ref isChecked, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class CheckBoxItem<T> : CheckBoxItem {
|
||||||
|
public new T Item { get; }
|
||||||
|
|
||||||
|
public CheckBoxItem(T item) {
|
||||||
|
this.Item = item;
|
||||||
|
base.Item = item;
|
||||||
|
}
|
||||||
|
}
|
36
app/Desktop/App/Dialogs/File/FileDialogs.cs
Normal file
36
app/Desktop/App/Dialogs/File/FileDialogs.cs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Platform.Storage;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Dialogs.File;
|
||||||
|
|
||||||
|
static class FileDialogs {
|
||||||
|
public static async Task<string[]> OpenFiles(this IStorageProvider storageProvider, FilePickerOpenOptions options) {
|
||||||
|
return (await storageProvider.OpenFilePickerAsync(options)).ToLocalPaths();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<string?> SaveFile(this IStorageProvider storageProvider, FilePickerSaveOptions options) {
|
||||||
|
return (await storageProvider.SaveFilePickerAsync(options))?.ToLocalPath();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static FilePickerFileType CreateFilter(string name, string[] extensions) {
|
||||||
|
return new FilePickerFileType(name) {
|
||||||
|
Patterns = extensions.Select(static ext => "*." + ext).ToArray()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Task<IStorageFolder?> GetSuggestedStartLocation(Window window, string? suggestedDirectory) {
|
||||||
|
return suggestedDirectory == null ? Task.FromResult<IStorageFolder?>(null) : window.StorageProvider.TryGetFolderFromPathAsync(suggestedDirectory);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string ToLocalPath(this IStorageFile file) {
|
||||||
|
return file.TryGetLocalPath() ?? throw new NotSupportedException("Local filesystem is not supported.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string[] ToLocalPaths(this IReadOnlyList<IStorageFile> files) {
|
||||||
|
return files.Select(ToLocalPath).ToArray();
|
||||||
|
}
|
||||||
|
}
|
74
app/Desktop/App/Dialogs/Message/Dialog.cs
Normal file
74
app/Desktop/App/Dialogs/Message/Dialog.cs
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Threading;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Dialogs.Message;
|
||||||
|
|
||||||
|
static class Dialog {
|
||||||
|
public static async Task ShowOk(Window owner, string title, string message) {
|
||||||
|
if (!Dispatcher.UIThread.CheckAccess()) {
|
||||||
|
await Dispatcher.UIThread.InvokeAsync(() => ShowOk(owner, title, message));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await new MessageDialog {
|
||||||
|
DataContext = new MessageDialogModel {
|
||||||
|
Title = title,
|
||||||
|
Message = message,
|
||||||
|
IsOkVisible = true
|
||||||
|
}
|
||||||
|
}.ShowDialog<DialogResult.All>(owner);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<DialogResult.OkCancel> ShowOkCancel(Window owner, string title, string message) {
|
||||||
|
if (!Dispatcher.UIThread.CheckAccess()) {
|
||||||
|
return await Dispatcher.UIThread.InvokeAsync(() => ShowOkCancel(owner, title, message));
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = await new MessageDialog {
|
||||||
|
DataContext = new MessageDialogModel {
|
||||||
|
Title = title,
|
||||||
|
Message = message,
|
||||||
|
IsOkVisible = true,
|
||||||
|
IsCancelVisible = true
|
||||||
|
}
|
||||||
|
}.ShowDialog<DialogResult.All?>(owner);
|
||||||
|
|
||||||
|
return result.ToOkCancel();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<DialogResult.YesNo> ShowYesNo(Window owner, string title, string message) {
|
||||||
|
if (!Dispatcher.UIThread.CheckAccess()) {
|
||||||
|
return await Dispatcher.UIThread.InvokeAsync(() => ShowYesNo(owner, title, message));
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = await new MessageDialog {
|
||||||
|
DataContext = new MessageDialogModel {
|
||||||
|
Title = title,
|
||||||
|
Message = message,
|
||||||
|
IsYesVisible = true,
|
||||||
|
IsNoVisible = true
|
||||||
|
}
|
||||||
|
}.ShowDialog<DialogResult.All?>(owner);
|
||||||
|
|
||||||
|
return result.ToYesNo();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<DialogResult.YesNoCancel> ShowYesNoCancel(Window owner, string title, string message) {
|
||||||
|
if (!Dispatcher.UIThread.CheckAccess()) {
|
||||||
|
return await Dispatcher.UIThread.InvokeAsync(() => ShowYesNoCancel(owner, title, message));
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = await new MessageDialog {
|
||||||
|
DataContext = new MessageDialogModel {
|
||||||
|
Title = title,
|
||||||
|
Message = message,
|
||||||
|
IsYesVisible = true,
|
||||||
|
IsNoVisible = true,
|
||||||
|
IsCancelVisible = true
|
||||||
|
}
|
||||||
|
}.ShowDialog<DialogResult.All?>(owner);
|
||||||
|
|
||||||
|
return result.ToYesNoCancel();
|
||||||
|
}
|
||||||
|
}
|
59
app/Desktop/App/Dialogs/Message/DialogResult.cs
Normal file
59
app/Desktop/App/Dialogs/Message/DialogResult.cs
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Dialogs.Message;
|
||||||
|
|
||||||
|
static class DialogResult {
|
||||||
|
public enum All {
|
||||||
|
Ok,
|
||||||
|
Yes,
|
||||||
|
No,
|
||||||
|
Cancel
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum OkCancel {
|
||||||
|
Closed,
|
||||||
|
Ok,
|
||||||
|
Cancel
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum YesNo {
|
||||||
|
Closed,
|
||||||
|
Yes,
|
||||||
|
No
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum YesNoCancel {
|
||||||
|
Closed,
|
||||||
|
Yes,
|
||||||
|
No,
|
||||||
|
Cancel
|
||||||
|
}
|
||||||
|
|
||||||
|
public static OkCancel ToOkCancel(this All? result) {
|
||||||
|
return result switch {
|
||||||
|
null => OkCancel.Closed,
|
||||||
|
All.Ok => OkCancel.Ok,
|
||||||
|
All.Cancel => OkCancel.Cancel,
|
||||||
|
_ => throw new ArgumentException("Cannot convert dialog result " + result + " to ok/cancel.")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static YesNo ToYesNo(this All? result) {
|
||||||
|
return result switch {
|
||||||
|
null => YesNo.Closed,
|
||||||
|
All.Yes => YesNo.Yes,
|
||||||
|
All.No => YesNo.No,
|
||||||
|
_ => throw new ArgumentException("Cannot convert dialog result " + result + " to yes/no.")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static YesNoCancel ToYesNoCancel(this All? result) {
|
||||||
|
return result switch {
|
||||||
|
null => YesNoCancel.Closed,
|
||||||
|
All.Yes => YesNoCancel.Yes,
|
||||||
|
All.No => YesNoCancel.No,
|
||||||
|
All.Cancel => YesNoCancel.Cancel,
|
||||||
|
_ => throw new ArgumentException("Cannot convert dialog result " + result + " to yes/no/cancel.")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@@ -2,16 +2,16 @@
|
|||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
xmlns:namespace="clr-namespace:DHT.Desktop.Dialogs.Message"
|
xmlns:message="clr-namespace:DHT.Desktop.App.Dialogs.Message"
|
||||||
mc:Ignorable="d" d:DesignWidth="500"
|
mc:Ignorable="d" d:DesignWidth="500"
|
||||||
x:Class="DHT.Desktop.Dialogs.Message.MessageDialog"
|
x:Class="DHT.Desktop.App.Dialogs.Message.MessageDialog"
|
||||||
Title="{Binding Title}"
|
Title="{Binding Title}"
|
||||||
Icon="avares://DiscordHistoryTracker/Resources/icon.ico"
|
Icon="avares://DiscordHistoryTracker/Resources/icon.ico"
|
||||||
Width="500" SizeToContent="Height" CanResize="False"
|
Width="500" SizeToContent="Height" CanResize="False"
|
||||||
WindowStartupLocation="CenterOwner">
|
WindowStartupLocation="CenterOwner">
|
||||||
|
|
||||||
<Window.DataContext>
|
<Window.DataContext>
|
||||||
<namespace:MessageDialogModel />
|
<message:MessageDialogModel />
|
||||||
</Window.DataContext>
|
</Window.DataContext>
|
||||||
|
|
||||||
<Window.Styles>
|
<Window.Styles>
|
28
app/Desktop/App/Dialogs/Message/MessageDialog.axaml.cs
Normal file
28
app/Desktop/App/Dialogs/Message/MessageDialog.axaml.cs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Interactivity;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Dialogs.Message;
|
||||||
|
|
||||||
|
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
||||||
|
public sealed partial class MessageDialog : Window {
|
||||||
|
public MessageDialog() {
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ClickOk(object? sender, RoutedEventArgs e) {
|
||||||
|
Close(DialogResult.All.Ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ClickYes(object? sender, RoutedEventArgs e) {
|
||||||
|
Close(DialogResult.All.Yes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ClickNo(object? sender, RoutedEventArgs e) {
|
||||||
|
Close(DialogResult.All.No);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ClickCancel(object? sender, RoutedEventArgs e) {
|
||||||
|
Close(DialogResult.All.Cancel);
|
||||||
|
}
|
||||||
|
}
|
11
app/Desktop/App/Dialogs/Message/MessageDialogModel.cs
Normal file
11
app/Desktop/App/Dialogs/Message/MessageDialogModel.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
namespace DHT.Desktop.App.Dialogs.Message;
|
||||||
|
|
||||||
|
sealed class MessageDialogModel {
|
||||||
|
public string Title { get; init; } = "";
|
||||||
|
public string Message { get; init; } = "";
|
||||||
|
|
||||||
|
public bool IsOkVisible { get; init; } = false;
|
||||||
|
public bool IsYesVisible { get; init; } = false;
|
||||||
|
public bool IsNoVisible { get; init; } = false;
|
||||||
|
public bool IsCancelVisible { get; init; } = false;
|
||||||
|
}
|
7
app/Desktop/App/Dialogs/Progress/IProgressCallback.cs
Normal file
7
app/Desktop/App/Dialogs/Progress/IProgressCallback.cs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Dialogs.Progress;
|
||||||
|
|
||||||
|
interface IProgressCallback {
|
||||||
|
Task Update(string message, int finishedItems, int totalItems);
|
||||||
|
}
|
@@ -2,18 +2,18 @@
|
|||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
xmlns:namespace="clr-namespace:DHT.Desktop.Dialogs.Progress"
|
xmlns:progress="clr-namespace:DHT.Desktop.App.Dialogs.Progress"
|
||||||
mc:Ignorable="d" d:DesignWidth="500"
|
mc:Ignorable="d" d:DesignWidth="500"
|
||||||
x:Class="DHT.Desktop.Dialogs.Progress.ProgressDialog"
|
x:Class="DHT.Desktop.App.Dialogs.Progress.ProgressDialog"
|
||||||
Title="{Binding Title}"
|
Title="{Binding Title}"
|
||||||
Icon="avares://DiscordHistoryTracker/Resources/icon.ico"
|
Icon="avares://DiscordHistoryTracker/Resources/icon.ico"
|
||||||
Opened="Loaded"
|
Opened="OnOpened"
|
||||||
Closing="OnClosing"
|
Closing="OnClosing"
|
||||||
Width="500" SizeToContent="Height" CanResize="False"
|
Width="500" SizeToContent="Height" CanResize="False"
|
||||||
WindowStartupLocation="CenterOwner">
|
WindowStartupLocation="CenterOwner">
|
||||||
|
|
||||||
<Window.DataContext>
|
<Window.DataContext>
|
||||||
<namespace:ProgressDialogModel />
|
<progress:ProgressDialogModel />
|
||||||
</Window.DataContext>
|
</Window.DataContext>
|
||||||
|
|
||||||
<Window.Styles>
|
<Window.Styles>
|
30
app/Desktop/App/Dialogs/Progress/ProgressDialog.axaml.cs
Normal file
30
app/Desktop/App/Dialogs/Progress/ProgressDialog.axaml.cs
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
using System;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Dialogs.Progress;
|
||||||
|
|
||||||
|
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
||||||
|
public sealed partial class ProgressDialog : Window {
|
||||||
|
private bool isFinished = false;
|
||||||
|
|
||||||
|
public ProgressDialog() {
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnOpened(object? sender, EventArgs e) {
|
||||||
|
if (DataContext is ProgressDialogModel model) {
|
||||||
|
Task.Run(model.StartTask).ContinueWith(OnFinished, TaskScheduler.FromCurrentSynchronizationContext());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnClosing(object? sender, WindowClosingEventArgs e) {
|
||||||
|
e.Cancel = !isFinished;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnFinished(Task task) {
|
||||||
|
isFinished = true;
|
||||||
|
Close();
|
||||||
|
}
|
||||||
|
}
|
65
app/Desktop/App/Dialogs/Progress/ProgressDialogModel.cs
Normal file
65
app/Desktop/App/Dialogs/Progress/ProgressDialogModel.cs
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Avalonia.Threading;
|
||||||
|
using DHT.Desktop.App.Common;
|
||||||
|
using DHT.Utils.Models;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Dialogs.Progress;
|
||||||
|
|
||||||
|
sealed class ProgressDialogModel : BaseModel {
|
||||||
|
public string Title { get; init; } = "";
|
||||||
|
|
||||||
|
private string message = "";
|
||||||
|
|
||||||
|
public string Message {
|
||||||
|
get => message;
|
||||||
|
private set => Change(ref message, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private string items = "";
|
||||||
|
|
||||||
|
public string Items {
|
||||||
|
get => items;
|
||||||
|
private set => Change(ref items, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int progress = 0;
|
||||||
|
|
||||||
|
public int Progress {
|
||||||
|
get => progress;
|
||||||
|
private set => Change(ref progress, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly TaskRunner? task;
|
||||||
|
|
||||||
|
[Obsolete("Designer")]
|
||||||
|
public ProgressDialogModel() {}
|
||||||
|
|
||||||
|
public ProgressDialogModel(TaskRunner task) {
|
||||||
|
this.task = task;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal async Task StartTask() {
|
||||||
|
if (task != null) {
|
||||||
|
await task(new Callback(this));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public delegate Task TaskRunner(IProgressCallback callback);
|
||||||
|
|
||||||
|
private sealed class Callback : IProgressCallback {
|
||||||
|
private readonly ProgressDialogModel model;
|
||||||
|
|
||||||
|
public Callback(ProgressDialogModel model) {
|
||||||
|
this.model = model;
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task IProgressCallback.Update(string message, int finishedItems, int totalItems) {
|
||||||
|
await Dispatcher.UIThread.InvokeAsync(() => {
|
||||||
|
model.Message = message;
|
||||||
|
model.Items = finishedItems.Format() + " / " + totalItems.Format();
|
||||||
|
model.Progress = 100 * finishedItems / totalItems;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
56
app/Desktop/App/Dialogs/TextBox/TextBoxDialog.axaml
Normal file
56
app/Desktop/App/Dialogs/TextBox/TextBoxDialog.axaml
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
<Window xmlns="https://github.com/avaloniaui"
|
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
|
xmlns:textBox="clr-namespace:DHT.Desktop.App.Dialogs.TextBox"
|
||||||
|
mc:Ignorable="d" d:DesignWidth="500"
|
||||||
|
x:Class="DHT.Desktop.App.Dialogs.TextBox.TextBoxDialog"
|
||||||
|
Title="{Binding Title}"
|
||||||
|
Icon="avares://DiscordHistoryTracker/Resources/icon.ico"
|
||||||
|
Width="500" SizeToContent="Height" CanResize="False"
|
||||||
|
WindowStartupLocation="CenterOwner">
|
||||||
|
|
||||||
|
<Window.DataContext>
|
||||||
|
<textBox:TextBoxDialogModel />
|
||||||
|
</Window.DataContext>
|
||||||
|
|
||||||
|
<Window.Styles>
|
||||||
|
<Style Selector="Panel.buttons">
|
||||||
|
<Setter Property="Margin" Value="0 20 0 0" />
|
||||||
|
</Style>
|
||||||
|
<Style Selector="Panel.buttons > WrapPanel.right">
|
||||||
|
<Setter Property="HorizontalAlignment" Value="Right" />
|
||||||
|
</Style>
|
||||||
|
<Style Selector="Panel.buttons Button">
|
||||||
|
<Setter Property="MinWidth" Value="80" />
|
||||||
|
<Setter Property="Margin" Value="8 0 0 0" />
|
||||||
|
</Style>
|
||||||
|
</Window.Styles>
|
||||||
|
|
||||||
|
<StackPanel Margin="20">
|
||||||
|
<ScrollViewer MaxHeight="400">
|
||||||
|
<StackPanel Spacing="10">
|
||||||
|
<TextBlock Text="{Binding Description}" TextWrapping="Wrap" />
|
||||||
|
<ItemsRepeater ItemsSource="{Binding Items}">
|
||||||
|
<ItemsRepeater.ItemTemplate>
|
||||||
|
<DataTemplate>
|
||||||
|
<DockPanel Margin="0 5 25 0">
|
||||||
|
<TextBox Name="Input" Text="{Binding Value}" Width="180" VerticalAlignment="Top" DockPanel.Dock="Right" />
|
||||||
|
<Label Target="Input" VerticalAlignment="Center" DockPanel.Dock="Left">
|
||||||
|
<TextBlock Text="{Binding Title}" TextWrapping="Wrap" />
|
||||||
|
</Label>
|
||||||
|
</DockPanel>
|
||||||
|
</DataTemplate>
|
||||||
|
</ItemsRepeater.ItemTemplate>
|
||||||
|
</ItemsRepeater>
|
||||||
|
</StackPanel>
|
||||||
|
</ScrollViewer>
|
||||||
|
<Panel Classes="buttons">
|
||||||
|
<WrapPanel Classes="right">
|
||||||
|
<Button Click="ClickOk" IsEnabled="{Binding !HasErrors}">OK</Button>
|
||||||
|
<Button Click="ClickCancel">Cancel</Button>
|
||||||
|
</WrapPanel>
|
||||||
|
</Panel>
|
||||||
|
</StackPanel>
|
||||||
|
|
||||||
|
</Window>
|
21
app/Desktop/App/Dialogs/TextBox/TextBoxDialog.axaml.cs
Normal file
21
app/Desktop/App/Dialogs/TextBox/TextBoxDialog.axaml.cs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Interactivity;
|
||||||
|
using DHT.Desktop.App.Dialogs.Message;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Dialogs.TextBox;
|
||||||
|
|
||||||
|
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
||||||
|
public sealed partial class TextBoxDialog : Window {
|
||||||
|
public TextBoxDialog() {
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ClickOk(object? sender, RoutedEventArgs e) {
|
||||||
|
Close(DialogResult.OkCancel.Ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ClickCancel(object? sender, RoutedEventArgs e) {
|
||||||
|
Close(DialogResult.OkCancel.Cancel);
|
||||||
|
}
|
||||||
|
}
|
47
app/Desktop/App/Dialogs/TextBox/TextBoxDialogModel.cs
Normal file
47
app/Desktop/App/Dialogs/TextBox/TextBoxDialogModel.cs
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Linq;
|
||||||
|
using DHT.Utils.Models;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Dialogs.TextBox;
|
||||||
|
|
||||||
|
class TextBoxDialogModel : BaseModel {
|
||||||
|
public string Title { get; init; } = "";
|
||||||
|
public string Description { get; init; } = "";
|
||||||
|
|
||||||
|
private IReadOnlyList<TextBoxItem> items = Array.Empty<TextBoxItem>();
|
||||||
|
|
||||||
|
public IReadOnlyList<TextBoxItem> Items {
|
||||||
|
get => items;
|
||||||
|
|
||||||
|
protected set {
|
||||||
|
foreach (var item in items) {
|
||||||
|
item.ErrorsChanged -= OnItemErrorsChanged;
|
||||||
|
}
|
||||||
|
|
||||||
|
items = value;
|
||||||
|
|
||||||
|
foreach (var item in items) {
|
||||||
|
item.ErrorsChanged += OnItemErrorsChanged;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool HasErrors => Items.Any(static item => !item.IsValid);
|
||||||
|
|
||||||
|
private void OnItemErrorsChanged(object? sender, DataErrorsChangedEventArgs e) {
|
||||||
|
OnPropertyChanged(nameof(HasErrors));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class TextBoxDialogModel<T> : TextBoxDialogModel {
|
||||||
|
public new IReadOnlyList<TextBoxItem<T>> Items { get; }
|
||||||
|
|
||||||
|
public IEnumerable<TextBoxItem<T>> ValidItems => Items.Where(static item => item.IsValid);
|
||||||
|
|
||||||
|
public TextBoxDialogModel(IEnumerable<TextBoxItem<T>> items) {
|
||||||
|
this.Items = new List<TextBoxItem<T>>(items);
|
||||||
|
base.Items = this.Items;
|
||||||
|
}
|
||||||
|
}
|
42
app/Desktop/App/Dialogs/TextBox/TextBoxItem.cs
Normal file
42
app/Desktop/App/Dialogs/TextBox/TextBoxItem.cs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using DHT.Utils.Models;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Dialogs.TextBox;
|
||||||
|
|
||||||
|
class TextBoxItem : BaseModel, INotifyDataErrorInfo {
|
||||||
|
public string Title { get; init; } = "";
|
||||||
|
public object? Item { get; init; } = null;
|
||||||
|
|
||||||
|
public Func<string, bool> ValidityCheck { get; init; } = static _ => true;
|
||||||
|
public bool IsValid => ValidityCheck(Value);
|
||||||
|
|
||||||
|
private string value = string.Empty;
|
||||||
|
|
||||||
|
public string Value {
|
||||||
|
get => this.value;
|
||||||
|
set {
|
||||||
|
Change(ref this.value, value);
|
||||||
|
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(nameof(Value)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable GetErrors(string? propertyName) {
|
||||||
|
if (propertyName == nameof(Value) && !IsValid) {
|
||||||
|
yield return string.Empty;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool HasErrors => !IsValid;
|
||||||
|
public event EventHandler<DataErrorsChangedEventArgs>? ErrorsChanged;
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class TextBoxItem<T> : TextBoxItem {
|
||||||
|
public new T Item { get; }
|
||||||
|
|
||||||
|
public TextBoxItem(T item) {
|
||||||
|
this.Item = item;
|
||||||
|
base.Item = item;
|
||||||
|
}
|
||||||
|
}
|
@@ -2,10 +2,10 @@
|
|||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
xmlns:pages="clr-namespace:DHT.Desktop.Main.Pages"
|
xmlns:pages="clr-namespace:DHT.Desktop.App.Pages"
|
||||||
xmlns:controls="clr-namespace:DHT.Desktop.Main.Controls"
|
xmlns:controls="clr-namespace:DHT.Desktop.App.Controls"
|
||||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||||
x:Class="DHT.Desktop.Main.Pages.AdvancedPage">
|
x:Class="DHT.Desktop.App.Pages.AdvancedPage">
|
||||||
|
|
||||||
<Design.DataContext>
|
<Design.DataContext>
|
||||||
<pages:AdvancedPageModel />
|
<pages:AdvancedPageModel />
|
11
app/Desktop/App/Pages/AdvancedPage.axaml.cs
Normal file
11
app/Desktop/App/Pages/AdvancedPage.axaml.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Pages;
|
||||||
|
|
||||||
|
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
||||||
|
public sealed partial class AdvancedPage : UserControl {
|
||||||
|
public AdvancedPage() {
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
}
|
39
app/Desktop/App/Pages/AdvancedPageModel.cs
Normal file
39
app/Desktop/App/Pages/AdvancedPageModel.cs
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
using System;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using DHT.Desktop.App.Controls;
|
||||||
|
using DHT.Desktop.App.Dialogs.Message;
|
||||||
|
using DHT.Desktop.Server;
|
||||||
|
using DHT.Server.Database;
|
||||||
|
using DHT.Utils.Models;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Pages;
|
||||||
|
|
||||||
|
sealed class AdvancedPageModel : BaseModel, IDisposable {
|
||||||
|
public ServerConfigurationPanelModel ServerConfigurationModel { get; }
|
||||||
|
|
||||||
|
private readonly Window window;
|
||||||
|
private readonly IDatabaseFile db;
|
||||||
|
|
||||||
|
[Obsolete("Designer")]
|
||||||
|
public AdvancedPageModel() : this(null!, DummyDatabaseFile.Instance, new ServerManager(DummyDatabaseFile.Instance)) {}
|
||||||
|
|
||||||
|
public AdvancedPageModel(Window window, IDatabaseFile db, ServerManager serverManager) {
|
||||||
|
this.window = window;
|
||||||
|
this.db = db;
|
||||||
|
|
||||||
|
ServerConfigurationModel = new ServerConfigurationPanelModel(window, serverManager);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Initialize() {
|
||||||
|
ServerConfigurationModel.Initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose() {
|
||||||
|
ServerConfigurationModel.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async void VacuumDatabase() {
|
||||||
|
db.Vacuum();
|
||||||
|
await Dialog.ShowOk(window, "Vacuum Database", "Done.");
|
||||||
|
}
|
||||||
|
}
|
@@ -2,9 +2,9 @@
|
|||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
xmlns:pages="clr-namespace:DHT.Desktop.Main.Pages"
|
xmlns:pages="clr-namespace:DHT.Desktop.App.Pages"
|
||||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||||
x:Class="DHT.Desktop.Main.Pages.DatabasePage">
|
x:Class="DHT.Desktop.App.Pages.DatabasePage">
|
||||||
|
|
||||||
<Design.DataContext>
|
<Design.DataContext>
|
||||||
<pages:DatabasePageModel />
|
<pages:DatabasePageModel />
|
||||||
@@ -23,7 +23,6 @@
|
|||||||
</DockPanel>
|
</DockPanel>
|
||||||
<WrapPanel>
|
<WrapPanel>
|
||||||
<Button Command="{Binding OpenDatabaseFolder}">Open Database Folder</Button>
|
<Button Command="{Binding OpenDatabaseFolder}">Open Database Folder</Button>
|
||||||
<Button Command="{Binding MergeWithDatabase}">Merge with Database(s)...</Button>
|
|
||||||
</WrapPanel>
|
</WrapPanel>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
|
|
11
app/Desktop/App/Pages/DatabasePage.axaml.cs
Normal file
11
app/Desktop/App/Pages/DatabasePage.axaml.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Pages;
|
||||||
|
|
||||||
|
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
||||||
|
public sealed partial class DatabasePage : UserControl {
|
||||||
|
public DatabasePage() {
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
}
|
59
app/Desktop/App/Pages/DatabasePageModel.cs
Normal file
59
app/Desktop/App/Pages/DatabasePageModel.cs
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.IO;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using DHT.Desktop.App.Dialogs.Message;
|
||||||
|
using DHT.Server.Database;
|
||||||
|
using DHT.Utils.Logging;
|
||||||
|
using DHT.Utils.Models;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Pages;
|
||||||
|
|
||||||
|
sealed class DatabasePageModel : BaseModel {
|
||||||
|
private static readonly Log Log = Log.ForType<DatabasePageModel>();
|
||||||
|
|
||||||
|
public IDatabaseFile Db { get; }
|
||||||
|
|
||||||
|
public event EventHandler? DatabaseClosed;
|
||||||
|
|
||||||
|
private readonly Window window;
|
||||||
|
|
||||||
|
[Obsolete("Designer")]
|
||||||
|
public DatabasePageModel() : this(null!, DummyDatabaseFile.Instance) {}
|
||||||
|
|
||||||
|
public DatabasePageModel(Window window, IDatabaseFile db) {
|
||||||
|
this.window = window;
|
||||||
|
this.Db = db;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async void OpenDatabaseFolder() {
|
||||||
|
string file = Db.Path;
|
||||||
|
string? folder = Path.GetDirectoryName(file);
|
||||||
|
|
||||||
|
if (folder == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (Environment.OSVersion.Platform) {
|
||||||
|
case PlatformID.Win32NT:
|
||||||
|
Process.Start("explorer.exe", "/select,\"" + file + "\"");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PlatformID.Unix:
|
||||||
|
Process.Start("xdg-open", new string[] { folder });
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PlatformID.MacOSX:
|
||||||
|
Process.Start("open", new string[] { folder });
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
await Dialog.ShowOk(window, "Feature Not Supported", "This feature is not supported for your operating system.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CloseDatabase() {
|
||||||
|
DatabaseClosed?.Invoke(this, EventArgs.Empty);
|
||||||
|
}
|
||||||
|
}
|
@@ -2,9 +2,9 @@
|
|||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
xmlns:pages="clr-namespace:DHT.Desktop.Main.Pages"
|
xmlns:pages="clr-namespace:DHT.Desktop.App.Pages"
|
||||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||||
x:Class="DHT.Desktop.Main.Pages.TrackingPage">
|
x:Class="DHT.Desktop.App.Pages.TrackingPage">
|
||||||
|
|
||||||
<Design.DataContext>
|
<Design.DataContext>
|
||||||
<pages:TrackingPageModel />
|
<pages:TrackingPageModel />
|
32
app/Desktop/App/Pages/TrackingPage.axaml.cs
Normal file
32
app/Desktop/App/Pages/TrackingPage.axaml.cs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
using System;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Interactivity;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Pages;
|
||||||
|
|
||||||
|
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
||||||
|
public sealed partial class TrackingPage : UserControl {
|
||||||
|
private bool isCopyingScript;
|
||||||
|
|
||||||
|
public TrackingPage() {
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async void CopyTrackingScriptButton_OnClick(object? sender, RoutedEventArgs e) {
|
||||||
|
if (DataContext is TrackingPageModel model) {
|
||||||
|
var originalText = CopyTrackingScript.Content;
|
||||||
|
CopyTrackingScript.MinWidth = CopyTrackingScript.Bounds.Width;
|
||||||
|
|
||||||
|
if (await model.OnClickCopyTrackingScript() && !isCopyingScript) {
|
||||||
|
isCopyingScript = true;
|
||||||
|
CopyTrackingScript.Content = "Script Copied!";
|
||||||
|
|
||||||
|
await Task.Delay(TimeSpan.FromSeconds(2));
|
||||||
|
CopyTrackingScript.Content = originalText;
|
||||||
|
isCopyingScript = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
115
app/Desktop/App/Pages/TrackingPageModel.cs
Normal file
115
app/Desktop/App/Pages/TrackingPageModel.cs
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Web;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using DHT.Desktop.App.Dialogs.Message;
|
||||||
|
using DHT.Desktop.Discord;
|
||||||
|
using DHT.Desktop.Server;
|
||||||
|
using DHT.Utils.Models;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Pages;
|
||||||
|
|
||||||
|
sealed class TrackingPageModel : BaseModel {
|
||||||
|
private bool areDevToolsEnabled;
|
||||||
|
|
||||||
|
private bool AreDevToolsEnabled {
|
||||||
|
get => areDevToolsEnabled;
|
||||||
|
set {
|
||||||
|
Change(ref areDevToolsEnabled, value);
|
||||||
|
OnPropertyChanged(nameof(ToggleAppDevToolsButtonText));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsToggleAppDevToolsButtonEnabled { get; private set; } = true;
|
||||||
|
|
||||||
|
public string ToggleAppDevToolsButtonText {
|
||||||
|
get {
|
||||||
|
if (!IsToggleAppDevToolsButtonEnabled) {
|
||||||
|
return "Unavailable";
|
||||||
|
}
|
||||||
|
|
||||||
|
return AreDevToolsEnabled ? "Disable Ctrl+Shift+I" : "Enable Ctrl+Shift+I";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly Window window;
|
||||||
|
|
||||||
|
[Obsolete("Designer")]
|
||||||
|
public TrackingPageModel() : this(null!) {}
|
||||||
|
|
||||||
|
public TrackingPageModel(Window window) {
|
||||||
|
this.window = window;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Initialize() {
|
||||||
|
bool? devToolsEnabled = await DiscordAppSettings.AreDevToolsEnabled();
|
||||||
|
if (devToolsEnabled.HasValue) {
|
||||||
|
AreDevToolsEnabled = devToolsEnabled.Value;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
IsToggleAppDevToolsButtonEnabled = false;
|
||||||
|
OnPropertyChanged(nameof(IsToggleAppDevToolsButtonEnabled));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> OnClickCopyTrackingScript() {
|
||||||
|
string bootstrap = await Resources.ReadTextAsync("Tracker/bootstrap.js");
|
||||||
|
string script = bootstrap.Replace("= 0; /*[PORT]*/", "= " + ServerManager.Port + ";")
|
||||||
|
.Replace("/*[TOKEN]*/", HttpUtility.JavaScriptStringEncode(ServerManager.Token))
|
||||||
|
.Replace("/*[IMPORTS]*/", await Resources.ReadJoinedAsync("Tracker/scripts/", '\n'))
|
||||||
|
.Replace("/*[CSS-CONTROLLER]*/", await Resources.ReadTextAsync("Tracker/styles/controller.css"))
|
||||||
|
.Replace("/*[CSS-SETTINGS]*/", await Resources.ReadTextAsync("Tracker/styles/settings.css"));
|
||||||
|
|
||||||
|
var clipboard = window.Clipboard;
|
||||||
|
if (clipboard == null) {
|
||||||
|
await Dialog.ShowOk(window, "Copy Tracking Script", "Clipboard is not available on this system.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await clipboard.SetTextAsync(script);
|
||||||
|
return true;
|
||||||
|
} catch {
|
||||||
|
await Dialog.ShowOk(window, "Copy Tracking Script", "An error occurred while copying to clipboard.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async void OnClickToggleAppDevTools() {
|
||||||
|
const string DialogTitle = "Discord App Settings File";
|
||||||
|
|
||||||
|
bool oldState = AreDevToolsEnabled;
|
||||||
|
bool newState = !oldState;
|
||||||
|
|
||||||
|
switch (await DiscordAppSettings.ConfigureDevTools(newState)) {
|
||||||
|
case SettingsJsonResult.Success:
|
||||||
|
AreDevToolsEnabled = newState;
|
||||||
|
await Dialog.ShowOk(window, DialogTitle, "Ctrl+Shift+I was " + (newState ? "enabled." : "disabled.") + " Restart the Discord app for the change to take effect.");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SettingsJsonResult.AlreadySet:
|
||||||
|
await Dialog.ShowOk(window, DialogTitle, "Ctrl+Shift+I is already " + (newState ? "enabled." : "disabled."));
|
||||||
|
AreDevToolsEnabled = newState;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SettingsJsonResult.FileNotFound:
|
||||||
|
await Dialog.ShowOk(window, DialogTitle, "Cannot find the settings file:\n" + DiscordAppSettings.JsonFilePath);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SettingsJsonResult.ReadError:
|
||||||
|
await Dialog.ShowOk(window, DialogTitle, "Cannot read the settings file:\n" + DiscordAppSettings.JsonFilePath);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SettingsJsonResult.InvalidJson:
|
||||||
|
await Dialog.ShowOk(window, DialogTitle, "Unknown format of the settings file:\n" + DiscordAppSettings.JsonFilePath);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SettingsJsonResult.WriteError:
|
||||||
|
await Dialog.ShowOk(window, DialogTitle, "Cannot save the settings file:\n" + DiscordAppSettings.JsonFilePath);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new ArgumentOutOfRangeException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -2,10 +2,10 @@
|
|||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
xmlns:controls="clr-namespace:DHT.Desktop.Main.Controls"
|
xmlns:screens="clr-namespace:DHT.Desktop.App.Screens"
|
||||||
xmlns:screens="clr-namespace:DHT.Desktop.Main.Screens"
|
xmlns:controls="clr-namespace:DHT.Desktop.App.Controls"
|
||||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||||
x:Class="DHT.Desktop.Main.Screens.MainContentScreen">
|
x:Class="DHT.Desktop.App.Screens.MainContentScreen">
|
||||||
|
|
||||||
<Design.DataContext>
|
<Design.DataContext>
|
||||||
<screens:MainContentScreenModel />
|
<screens:MainContentScreenModel />
|
||||||
@@ -73,14 +73,14 @@
|
|||||||
<DockPanel>
|
<DockPanel>
|
||||||
<Border Classes="statusBar" DockPanel.Dock="Bottom">
|
<Border Classes="statusBar" DockPanel.Dock="Bottom">
|
||||||
<DockPanel>
|
<DockPanel>
|
||||||
<TextBlock Classes="invisibleTabItem" DockPanel.Dock="Left">Advanced</TextBlock>
|
<TextBlock Classes="invisibleTabItem" DockPanel.Dock="Left">Attachments</TextBlock>
|
||||||
<controls:StatusBar DataContext="{Binding StatusBarModel}" DockPanel.Dock="Right" />
|
<controls:StatusBar DataContext="{Binding StatusBarModel}" DockPanel.Dock="Right" />
|
||||||
</DockPanel>
|
</DockPanel>
|
||||||
</Border>
|
</Border>
|
||||||
<TabControl x:Name="TabControl" TabStripPlacement="Left" DockPanel.Dock="Top">
|
<TabControl x:Name="TabControl" TabStripPlacement="Left" DockPanel.Dock="Top">
|
||||||
<TabControl.ItemsPanel>
|
<TabControl.ItemsPanel>
|
||||||
<ItemsPanelTemplate>
|
<ItemsPanelTemplate>
|
||||||
<Grid ColumnDefinitions="Auto" RowDefinitions="Auto,Auto,Auto,*,Auto,Auto" />
|
<Grid ColumnDefinitions="Auto" RowDefinitions="Auto,Auto,*,Auto" />
|
||||||
</ItemsPanelTemplate>
|
</ItemsPanelTemplate>
|
||||||
</TabControl.ItemsPanel>
|
</TabControl.ItemsPanel>
|
||||||
<TabItem x:Name="TabDatabase" Header="Database" Classes="first" Grid.Row="0">
|
<TabItem x:Name="TabDatabase" Header="Database" Classes="first" Grid.Row="0">
|
||||||
@@ -93,21 +93,11 @@
|
|||||||
<ContentPresenter Content="{Binding TrackingPage}" Classes="page" />
|
<ContentPresenter Content="{Binding TrackingPage}" Classes="page" />
|
||||||
</ScrollViewer>
|
</ScrollViewer>
|
||||||
</TabItem>
|
</TabItem>
|
||||||
<TabItem x:Name="TabViewer" Header="Viewer" Grid.Row="2">
|
<TabItem x:Name="TabAdvanced" Header="Advanced" Grid.Row="3">
|
||||||
<ScrollViewer>
|
|
||||||
<ContentPresenter Content="{Binding ViewerPage}" Classes="page" />
|
|
||||||
</ScrollViewer>
|
|
||||||
</TabItem>
|
|
||||||
<TabItem x:Name="TabAdvanced" Header="Advanced" Grid.Row="4">
|
|
||||||
<ScrollViewer>
|
<ScrollViewer>
|
||||||
<ContentPresenter Content="{Binding AdvancedPage}" Classes="page" />
|
<ContentPresenter Content="{Binding AdvancedPage}" Classes="page" />
|
||||||
</ScrollViewer>
|
</ScrollViewer>
|
||||||
</TabItem>
|
</TabItem>
|
||||||
<TabItem x:Name="TabDebug" Header="Debug" Grid.Row="5" IsVisible="{Binding HasDebugPage}">
|
|
||||||
<ScrollViewer>
|
|
||||||
<ContentPresenter Content="{Binding DebugPage}" Classes="page" />
|
|
||||||
</ScrollViewer>
|
|
||||||
</TabItem>
|
|
||||||
</TabControl>
|
</TabControl>
|
||||||
</DockPanel>
|
</DockPanel>
|
||||||
|
|
11
app/Desktop/App/Screens/MainContentScreen.axaml.cs
Normal file
11
app/Desktop/App/Screens/MainContentScreen.axaml.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Screens;
|
||||||
|
|
||||||
|
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
||||||
|
public sealed partial class MainContentScreen : UserControl {
|
||||||
|
public MainContentScreen() {
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
}
|
88
app/Desktop/App/Screens/MainContentScreenModel.cs
Normal file
88
app/Desktop/App/Screens/MainContentScreenModel.cs
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using DHT.Desktop.App.Controls;
|
||||||
|
using DHT.Desktop.App.Dialogs.Message;
|
||||||
|
using DHT.Desktop.App.Pages;
|
||||||
|
using DHT.Desktop.Server;
|
||||||
|
using DHT.Server.Database;
|
||||||
|
using DHT.Utils.Logging;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Screens;
|
||||||
|
|
||||||
|
sealed class MainContentScreenModel : IDisposable {
|
||||||
|
private static readonly Log Log = Log.ForType<MainContentScreenModel>();
|
||||||
|
|
||||||
|
public DatabasePage DatabasePage { get; }
|
||||||
|
private DatabasePageModel DatabasePageModel { get; }
|
||||||
|
|
||||||
|
public TrackingPage TrackingPage { get; }
|
||||||
|
private TrackingPageModel TrackingPageModel { get; }
|
||||||
|
|
||||||
|
public AdvancedPage AdvancedPage { get; }
|
||||||
|
private AdvancedPageModel AdvancedPageModel { get; }
|
||||||
|
|
||||||
|
public StatusBarModel StatusBarModel { get; }
|
||||||
|
|
||||||
|
public event EventHandler? DatabaseClosed {
|
||||||
|
add {
|
||||||
|
DatabasePageModel.DatabaseClosed += value;
|
||||||
|
}
|
||||||
|
remove {
|
||||||
|
DatabasePageModel.DatabaseClosed -= value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly Window window;
|
||||||
|
private readonly ServerManager serverManager;
|
||||||
|
|
||||||
|
[Obsolete("Designer")]
|
||||||
|
public MainContentScreenModel() : this(null!, DummyDatabaseFile.Instance) {}
|
||||||
|
|
||||||
|
public MainContentScreenModel(Window window, IDatabaseFile db) {
|
||||||
|
this.window = window;
|
||||||
|
this.serverManager = new ServerManager(db);
|
||||||
|
|
||||||
|
ServerLauncher.ServerManagementExceptionCaught += ServerLauncherOnServerManagementExceptionCaught;
|
||||||
|
|
||||||
|
DatabasePageModel = new DatabasePageModel(window, db);
|
||||||
|
DatabasePage = new DatabasePage { DataContext = DatabasePageModel };
|
||||||
|
|
||||||
|
TrackingPageModel = new TrackingPageModel(window);
|
||||||
|
TrackingPage = new TrackingPage { DataContext = TrackingPageModel };
|
||||||
|
|
||||||
|
AdvancedPageModel = new AdvancedPageModel(window, db, serverManager);
|
||||||
|
AdvancedPage = new AdvancedPage { DataContext = AdvancedPageModel };
|
||||||
|
|
||||||
|
StatusBarModel = new StatusBarModel();
|
||||||
|
|
||||||
|
AdvancedPageModel.ServerConfigurationModel.ServerStatusChanged += OnServerStatusChanged;
|
||||||
|
DatabaseClosed += OnDatabaseClosed;
|
||||||
|
|
||||||
|
StatusBarModel.CurrentStatus = serverManager.IsRunning ? StatusBarModel.Status.Ready : StatusBarModel.Status.Stopped;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Initialize() {
|
||||||
|
await TrackingPageModel.Initialize();
|
||||||
|
AdvancedPageModel.Initialize();
|
||||||
|
serverManager.Launch();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose() {
|
||||||
|
ServerLauncher.ServerManagementExceptionCaught -= ServerLauncherOnServerManagementExceptionCaught;
|
||||||
|
serverManager.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnServerStatusChanged(object? sender, StatusBarModel.Status e) {
|
||||||
|
StatusBarModel.CurrentStatus = e;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnDatabaseClosed(object? sender, EventArgs e) {
|
||||||
|
serverManager.Stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async void ServerLauncherOnServerManagementExceptionCaught(object? sender, Exception ex) {
|
||||||
|
Log.Error(ex);
|
||||||
|
await Dialog.ShowOk(window, "Internal Server Error", ex.Message);
|
||||||
|
}
|
||||||
|
}
|
@@ -2,9 +2,9 @@
|
|||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
xmlns:screens="clr-namespace:DHT.Desktop.Main.Screens"
|
xmlns:screens="clr-namespace:DHT.Desktop.App.Screens"
|
||||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||||
x:Class="DHT.Desktop.Main.Screens.WelcomeScreen">
|
x:Class="DHT.Desktop.App.Screens.WelcomeScreen">
|
||||||
|
|
||||||
<Design.DataContext>
|
<Design.DataContext>
|
||||||
<screens:WelcomeScreenModel />
|
<screens:WelcomeScreenModel />
|
11
app/Desktop/App/Screens/WelcomeScreen.axaml.cs
Normal file
11
app/Desktop/App/Screens/WelcomeScreen.axaml.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Screens;
|
||||||
|
|
||||||
|
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
||||||
|
public sealed partial class WelcomeScreen : UserControl {
|
||||||
|
public WelcomeScreen() {
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
}
|
71
app/Desktop/App/Screens/WelcomeScreenModel.cs
Normal file
71
app/Desktop/App/Screens/WelcomeScreenModel.cs
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using DHT.Desktop.App.Common;
|
||||||
|
using DHT.Desktop.App.Dialogs.Message;
|
||||||
|
using DHT.Desktop.App.Windows;
|
||||||
|
using DHT.Server.Database;
|
||||||
|
using DHT.Utils.Models;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Screens;
|
||||||
|
|
||||||
|
sealed class WelcomeScreenModel : BaseModel, IDisposable {
|
||||||
|
public string Version => Program.Version;
|
||||||
|
|
||||||
|
public IDatabaseFile? Db { get; private set; }
|
||||||
|
public bool HasDatabase => Db != null;
|
||||||
|
|
||||||
|
private readonly Window window;
|
||||||
|
|
||||||
|
private string? dbFilePath;
|
||||||
|
|
||||||
|
[Obsolete("Designer")]
|
||||||
|
public WelcomeScreenModel() : this(null!) {}
|
||||||
|
|
||||||
|
public WelcomeScreenModel(Window window) {
|
||||||
|
this.window = window;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async void OpenOrCreateDatabase() {
|
||||||
|
var path = await DatabaseGui.NewOpenOrCreateDatabaseFileDialog(window, Path.GetDirectoryName(dbFilePath));
|
||||||
|
if (path != null) {
|
||||||
|
await OpenOrCreateDatabaseFromPath(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task OpenOrCreateDatabaseFromPath(string path) {
|
||||||
|
if (Db != null) {
|
||||||
|
Db = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
dbFilePath = path;
|
||||||
|
Db = await DatabaseGui.TryOpenOrCreateDatabaseFromPath(path, window, CheckCanUpgradeDatabase);
|
||||||
|
|
||||||
|
OnPropertyChanged(nameof(Db));
|
||||||
|
OnPropertyChanged(nameof(HasDatabase));
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<bool> CheckCanUpgradeDatabase() {
|
||||||
|
return DialogResult.YesNo.Yes == await DatabaseGui.ShowCanUpgradeDatabaseDialog(window);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CloseDatabase() {
|
||||||
|
Dispose();
|
||||||
|
OnPropertyChanged(nameof(Db));
|
||||||
|
OnPropertyChanged(nameof(HasDatabase));
|
||||||
|
}
|
||||||
|
|
||||||
|
public async void ShowAboutDialog() {
|
||||||
|
await new AboutWindow { DataContext = new AboutWindowModel() }.ShowDialog(this.window);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Exit() {
|
||||||
|
window.Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose() {
|
||||||
|
Db?.Dispose();
|
||||||
|
Db = null;
|
||||||
|
}
|
||||||
|
}
|
@@ -2,16 +2,16 @@
|
|||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
xmlns:main="clr-namespace:DHT.Desktop.Main"
|
xmlns:windows="clr-namespace:DHT.Desktop.App.Windows"
|
||||||
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="295"
|
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="295"
|
||||||
x:Class="DHT.Desktop.Main.AboutWindow"
|
x:Class="DHT.Desktop.App.Windows.AboutWindow"
|
||||||
Title="About Discord History Tracker"
|
Title="About Discord History Tracker"
|
||||||
Icon="avares://DiscordHistoryTracker/Resources/icon.ico"
|
Icon="avares://DiscordHistoryTracker/Resources/icon.ico"
|
||||||
Width="480" Height="295" CanResize="False"
|
Width="480" Height="295" CanResize="False"
|
||||||
WindowStartupLocation="CenterOwner">
|
WindowStartupLocation="CenterOwner">
|
||||||
|
|
||||||
<Design.DataContext>
|
<Design.DataContext>
|
||||||
<main:AboutWindowModel />
|
<windows:AboutWindowModel />
|
||||||
</Design.DataContext>
|
</Design.DataContext>
|
||||||
|
|
||||||
<Window.Styles>
|
<Window.Styles>
|
||||||
@@ -46,7 +46,7 @@
|
|||||||
<TextBlock Grid.Row="0" Grid.Column="1" FontWeight="Bold">License</TextBlock>
|
<TextBlock Grid.Row="0" Grid.Column="1" FontWeight="Bold">License</TextBlock>
|
||||||
<TextBlock Grid.Row="0" Grid.Column="2" FontWeight="Bold">Link</TextBlock>
|
<TextBlock Grid.Row="0" Grid.Column="2" FontWeight="Bold">Link</TextBlock>
|
||||||
|
|
||||||
<TextBlock Grid.Row="2" Grid.Column="0">.NET 5</TextBlock>
|
<TextBlock Grid.Row="2" Grid.Column="0">.NET 8</TextBlock>
|
||||||
<TextBlock Grid.Row="2" Grid.Column="1">MIT</TextBlock>
|
<TextBlock Grid.Row="2" Grid.Column="1">MIT</TextBlock>
|
||||||
<Button Grid.Row="2" Grid.Column="2" Command="{Binding ShowLibraryNetCore}">GitHub</Button>
|
<Button Grid.Row="2" Grid.Column="2" Command="{Binding ShowLibraryNetCore}">GitHub</Button>
|
||||||
|
|
11
app/Desktop/App/Windows/AboutWindow.axaml.cs
Normal file
11
app/Desktop/App/Windows/AboutWindow.axaml.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Windows;
|
||||||
|
|
||||||
|
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
||||||
|
public sealed partial class AboutWindow : Window {
|
||||||
|
public AboutWindow() {
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
}
|
33
app/Desktop/App/Windows/AboutWindowModel.cs
Normal file
33
app/Desktop/App/Windows/AboutWindowModel.cs
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Windows;
|
||||||
|
|
||||||
|
sealed class AboutWindowModel {
|
||||||
|
public void ShowOfficialWebsite() {
|
||||||
|
OpenUrl("https://dht.chylex.com");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ShowSourceCode() {
|
||||||
|
OpenUrl("https://github.com/chylex/Discord-History-Tracker");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ShowLibraryAvalonia() {
|
||||||
|
OpenUrl("https://www.nuget.org/packages/Avalonia");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ShowLibrarySqlite() {
|
||||||
|
OpenUrl("https://www.sqlite.org");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ShowLibrarySqliteAdoNet() {
|
||||||
|
OpenUrl("https://www.nuget.org/packages/Microsoft.Data.Sqlite");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ShowLibraryNetCore() {
|
||||||
|
OpenUrl("https://github.com/dotnet/core");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void OpenUrl(string url) {
|
||||||
|
Process.Start(new ProcessStartInfo { FileName = url, UseShellExecute = true });
|
||||||
|
}
|
||||||
|
}
|
@@ -2,18 +2,18 @@
|
|||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
xmlns:main="clr-namespace:DHT.Desktop.Main"
|
xmlns:windows="clr-namespace:DHT.Desktop.App.Windows"
|
||||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||||
x:Class="DHT.Desktop.Main.MainWindow"
|
x:Class="DHT.Desktop.App.Windows.MainWindow"
|
||||||
Title="{Binding Title}"
|
Title="{Binding Title}"
|
||||||
Icon="avares://DiscordHistoryTracker/Resources/icon.ico"
|
Icon="avares://DiscordHistoryTracker/Resources/icon.ico"
|
||||||
Width="800" Height="500"
|
Width="800" Height="500"
|
||||||
MinWidth="500" MinHeight="275"
|
MinWidth="520" MinHeight="300"
|
||||||
WindowStartupLocation="CenterScreen"
|
WindowStartupLocation="CenterScreen"
|
||||||
Closed="OnClosed">
|
Closed="OnClosed">
|
||||||
|
|
||||||
<Design.DataContext>
|
<Design.DataContext>
|
||||||
<main:MainWindowModel />
|
<windows:MainWindowModel />
|
||||||
</Design.DataContext>
|
</Design.DataContext>
|
||||||
|
|
||||||
<Panel>
|
<Panel>
|
26
app/Desktop/App/Windows/MainWindow.axaml.cs
Normal file
26
app/Desktop/App/Windows/MainWindow.axaml.cs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
using System;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using JetBrains.Annotations;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Windows;
|
||||||
|
|
||||||
|
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
||||||
|
public sealed partial class MainWindow : Window {
|
||||||
|
[UsedImplicitly]
|
||||||
|
public MainWindow() {
|
||||||
|
InitializeComponent();
|
||||||
|
DataContext = new MainWindowModel(this, Arguments.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal MainWindow(Arguments args) {
|
||||||
|
InitializeComponent();
|
||||||
|
DataContext = new MainWindowModel(this, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnClosed(object? sender, EventArgs e) {
|
||||||
|
if (DataContext is IDisposable disposable) {
|
||||||
|
disposable.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
116
app/Desktop/App/Windows/MainWindowModel.cs
Normal file
116
app/Desktop/App/Windows/MainWindowModel.cs
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
using System;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.IO;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using DHT.Desktop.App.Dialogs.Message;
|
||||||
|
using DHT.Desktop.App.Screens;
|
||||||
|
using DHT.Desktop.Server;
|
||||||
|
using DHT.Server.Database;
|
||||||
|
using DHT.Utils.Models;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.App.Windows;
|
||||||
|
|
||||||
|
sealed class MainWindowModel : BaseModel, IDisposable {
|
||||||
|
private const string DefaultTitle = "Discord History Tracker";
|
||||||
|
|
||||||
|
public string Title { get; private set; } = DefaultTitle;
|
||||||
|
|
||||||
|
public UserControl CurrentScreen { get; private set; }
|
||||||
|
|
||||||
|
private readonly WelcomeScreen welcomeScreen;
|
||||||
|
private readonly WelcomeScreenModel welcomeScreenModel;
|
||||||
|
|
||||||
|
private MainContentScreen? mainContentScreen;
|
||||||
|
private MainContentScreenModel? mainContentScreenModel;
|
||||||
|
|
||||||
|
private readonly Window window;
|
||||||
|
|
||||||
|
private IDatabaseFile? db;
|
||||||
|
|
||||||
|
[Obsolete("Designer")]
|
||||||
|
public MainWindowModel() : this(null!, Arguments.Empty) {}
|
||||||
|
|
||||||
|
public MainWindowModel(Window window, Arguments args) {
|
||||||
|
this.window = window;
|
||||||
|
|
||||||
|
welcomeScreenModel = new WelcomeScreenModel(window);
|
||||||
|
welcomeScreen = new WelcomeScreen { DataContext = welcomeScreenModel };
|
||||||
|
CurrentScreen = welcomeScreen;
|
||||||
|
|
||||||
|
welcomeScreenModel.PropertyChanged += WelcomeScreenModelOnPropertyChanged;
|
||||||
|
|
||||||
|
var dbFile = args.DatabaseFile;
|
||||||
|
if (!string.IsNullOrWhiteSpace(dbFile)) {
|
||||||
|
async void OnWindowOpened(object? o, EventArgs eventArgs) {
|
||||||
|
window.Opened -= OnWindowOpened;
|
||||||
|
|
||||||
|
// https://github.com/AvaloniaUI/Avalonia/issues/3071
|
||||||
|
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
|
||||||
|
await Task.Delay(500);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (File.Exists(dbFile)) {
|
||||||
|
await welcomeScreenModel.OpenOrCreateDatabaseFromPath(dbFile);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
await Dialog.ShowOk(window, "Database Error", "Database file not found:\n" + dbFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
window.Opened += OnWindowOpened;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args.ServerPort != null) {
|
||||||
|
ServerManager.Port = args.ServerPort.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args.ServerToken != null) {
|
||||||
|
ServerManager.Token = args.ServerToken;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async void WelcomeScreenModelOnPropertyChanged(object? sender, PropertyChangedEventArgs e) {
|
||||||
|
if (e.PropertyName == nameof(welcomeScreenModel.Db)) {
|
||||||
|
if (mainContentScreenModel != null) {
|
||||||
|
mainContentScreenModel.DatabaseClosed -= MainContentScreenModelOnDatabaseClosed;
|
||||||
|
mainContentScreenModel.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
db?.Dispose();
|
||||||
|
db = welcomeScreenModel.Db;
|
||||||
|
|
||||||
|
if (db == null) {
|
||||||
|
Title = DefaultTitle;
|
||||||
|
mainContentScreenModel = null;
|
||||||
|
mainContentScreen = null;
|
||||||
|
CurrentScreen = welcomeScreen;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Title = Path.GetFileName(db.Path) + " - " + DefaultTitle;
|
||||||
|
mainContentScreenModel = new MainContentScreenModel(window, db);
|
||||||
|
await mainContentScreenModel.Initialize();
|
||||||
|
mainContentScreenModel.DatabaseClosed += MainContentScreenModelOnDatabaseClosed;
|
||||||
|
mainContentScreen = new MainContentScreen { DataContext = mainContentScreenModel };
|
||||||
|
CurrentScreen = mainContentScreen;
|
||||||
|
}
|
||||||
|
|
||||||
|
OnPropertyChanged(nameof(CurrentScreen));
|
||||||
|
OnPropertyChanged(nameof(Title));
|
||||||
|
|
||||||
|
window.Focus();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void MainContentScreenModelOnDatabaseClosed(object? sender, EventArgs e) {
|
||||||
|
welcomeScreenModel.CloseDatabase();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose() {
|
||||||
|
welcomeScreenModel.Dispose();
|
||||||
|
mainContentScreenModel?.Dispose();
|
||||||
|
db?.Dispose();
|
||||||
|
db = null;
|
||||||
|
}
|
||||||
|
}
|
@@ -1,64 +1,64 @@
|
|||||||
using System;
|
using System;
|
||||||
using DHT.Utils.Logging;
|
using DHT.Utils.Logging;
|
||||||
|
|
||||||
namespace DHT.Desktop {
|
namespace DHT.Desktop;
|
||||||
sealed class Arguments {
|
|
||||||
private static readonly Log Log = Log.ForType<Arguments>();
|
|
||||||
|
|
||||||
public static Arguments Empty => new(Array.Empty<string>());
|
sealed class Arguments {
|
||||||
|
private static readonly Log Log = Log.ForType<Arguments>();
|
||||||
|
|
||||||
public string? DatabaseFile { get; }
|
public static Arguments Empty => new(Array.Empty<string>());
|
||||||
public ushort? ServerPort { get; }
|
|
||||||
public string? ServerToken { get; }
|
|
||||||
|
|
||||||
public Arguments(string[] args) {
|
public string? DatabaseFile { get; }
|
||||||
for (int i = 0; i < args.Length; i++) {
|
public ushort? ServerPort { get; }
|
||||||
string key = args[i];
|
public string? ServerToken { get; }
|
||||||
|
|
||||||
switch (key) {
|
public Arguments(string[] args) {
|
||||||
case "-debug":
|
for (int i = 0; i < args.Length; i++) {
|
||||||
Log.IsDebugEnabled = true;
|
string key = args[i];
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
string value;
|
switch (key) {
|
||||||
|
case "-debug":
|
||||||
if (i == 0 && !key.StartsWith('-')) {
|
Log.IsDebugEnabled = true;
|
||||||
value = key;
|
|
||||||
key = "-db";
|
|
||||||
}
|
|
||||||
else if (i >= args.Length - 1) {
|
|
||||||
Log.Warn("Missing value for command line argument: " + key);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
value = args[++i];
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (key) {
|
string value;
|
||||||
case "-db":
|
|
||||||
DatabaseFile = value;
|
|
||||||
continue;
|
|
||||||
|
|
||||||
case "-port": {
|
if (i == 0 && !key.StartsWith('-')) {
|
||||||
if (ushort.TryParse(value, out var port)) {
|
value = key;
|
||||||
ServerPort = port;
|
key = "-db";
|
||||||
}
|
}
|
||||||
else {
|
else if (i >= args.Length - 1) {
|
||||||
Log.Warn("Invalid port number: " + value);
|
Log.Warn("Missing value for command line argument: " + key);
|
||||||
}
|
continue;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
value = args[++i];
|
||||||
|
}
|
||||||
|
|
||||||
continue;
|
switch (key) {
|
||||||
|
case "-db":
|
||||||
|
DatabaseFile = value;
|
||||||
|
continue;
|
||||||
|
|
||||||
|
case "-port": {
|
||||||
|
if (ushort.TryParse(value, out var port)) {
|
||||||
|
ServerPort = port;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Log.Warn("Invalid port number: " + value);
|
||||||
}
|
}
|
||||||
|
|
||||||
case "-token":
|
continue;
|
||||||
ServerToken = value;
|
|
||||||
continue;
|
|
||||||
|
|
||||||
default:
|
|
||||||
Log.Warn("Unknown command line argument: " + key);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case "-token":
|
||||||
|
ServerToken = value;
|
||||||
|
continue;
|
||||||
|
|
||||||
|
default:
|
||||||
|
Log.Warn("Unknown command line argument: " + key);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,66 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using DHT.Desktop.Dialogs.Message;
|
|
||||||
using DHT.Server.Database;
|
|
||||||
using DHT.Server.Database.Exceptions;
|
|
||||||
using DHT.Server.Database.Sqlite;
|
|
||||||
using DHT.Utils.Logging;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Common {
|
|
||||||
static class DatabaseGui {
|
|
||||||
private static readonly Log Log = Log.ForType(typeof(DatabaseGui));
|
|
||||||
|
|
||||||
private const string DatabaseFileInitialName = "archive.dht";
|
|
||||||
|
|
||||||
private static readonly List<FileDialogFilter> DatabaseFileDialogFilter = new() {
|
|
||||||
new FileDialogFilter {
|
|
||||||
Name = "Discord History Tracker Database",
|
|
||||||
Extensions = { "dht" }
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public static OpenFileDialog NewOpenDatabaseFileDialog() {
|
|
||||||
return new OpenFileDialog {
|
|
||||||
Title = "Open Database File",
|
|
||||||
InitialFileName = DatabaseFileInitialName,
|
|
||||||
Filters = DatabaseFileDialogFilter
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public static SaveFileDialog NewOpenOrCreateDatabaseFileDialog() {
|
|
||||||
return new SaveFileDialog {
|
|
||||||
Title = "Open or Create Database File",
|
|
||||||
InitialFileName = DatabaseFileInitialName,
|
|
||||||
Filters = DatabaseFileDialogFilter
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async Task<IDatabaseFile?> TryOpenOrCreateDatabaseFromPath(string path, Window window, Func<Task<bool>> checkCanUpgradeDatabase) {
|
|
||||||
IDatabaseFile? file = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
file = await SqliteDatabaseFile.OpenOrCreate(path, checkCanUpgradeDatabase);
|
|
||||||
} catch (InvalidDatabaseVersionException ex) {
|
|
||||||
await Dialog.ShowOk(window, "Database Error", "Database '" + Path.GetFileName(path) + "' appears to be corrupted (invalid version: " + ex.Version + ").");
|
|
||||||
} catch (DatabaseTooNewException ex) {
|
|
||||||
await Dialog.ShowOk(window, "Database Error", "Database '" + Path.GetFileName(path) + "' was opened in a newer version of DHT (database version " + ex.DatabaseVersion + ", app version " + ex.CurrentVersion + ").");
|
|
||||||
} catch (Exception ex) {
|
|
||||||
Log.Error(ex);
|
|
||||||
await Dialog.ShowOk(window, "Database Error", "Database '" + Path.GetFileName(path) + "' could not be opened:" + ex.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async Task<DialogResult.YesNo> ShowCanUpgradeDatabaseDialog(Window window) {
|
|
||||||
return await Dialog.ShowYesNo(window, "Database Upgrade", "This database was created with an older version of DHT. If you proceed, the database will be upgraded and will no longer open in previous versions of DHT.\n\nPlease ensure you have a backup of the database. Do you want to proceed with the upgrade?");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async Task<DialogResult.YesNo> ShowCanUpgradeMultipleDatabaseDialog(Window window) {
|
|
||||||
return await Dialog.ShowYesNo(window, "Database Upgrade", "One or more databases were created with an older version of DHT. If you proceed, these databases will be upgraded and will no longer open in previous versions of DHT. Otherwise, these databases will be skipped.\n\nPlease ensure you have a backup of the databases. Do you want to proceed with the upgrade?");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,15 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Globalization;
|
|
||||||
using Avalonia.Data.Converters;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Common {
|
|
||||||
sealed class NumberValueConverter : IValueConverter {
|
|
||||||
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) {
|
|
||||||
return value == null ? "-" : string.Format(Program.Culture, "{0:n0}", value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) {
|
|
||||||
throw new NotSupportedException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,19 +0,0 @@
|
|||||||
namespace DHT.Desktop.Common {
|
|
||||||
static class TextFormat {
|
|
||||||
public static string Format(this int number) {
|
|
||||||
return number.ToString("N0", Program.Culture);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string Format(this long number) {
|
|
||||||
return number.ToString("N0", Program.Culture);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string Pluralize(this int number, string singular) {
|
|
||||||
return number.Format() + "\u00A0" + (number == 1 ? singular : singular + "s");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string Pluralize(this long number, string singular) {
|
|
||||||
return number.Format() + "\u00A0" + (number == 1 ? singular : singular + "s");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,7 +1,6 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>WinExe</OutputType>
|
<OutputType>WinExe</OutputType>
|
||||||
<TargetFramework>net5.0</TargetFramework>
|
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<AssemblyName>DiscordHistoryTracker</AssemblyName>
|
<AssemblyName>DiscordHistoryTracker</AssemblyName>
|
||||||
<RootNamespace>DHT.Desktop</RootNamespace>
|
<RootNamespace>DHT.Desktop</RootNamespace>
|
||||||
@@ -21,15 +20,31 @@
|
|||||||
<DebugType>none</DebugType>
|
<DebugType>none</DebugType>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Avalonia" Version="0.10.13" />
|
<PackageReference Include="Avalonia" Version="11.0.0" />
|
||||||
<PackageReference Include="Avalonia.Desktop" Version="0.10.13" />
|
<PackageReference Include="Avalonia.Controls.DataGrid" Version="11.0.0" />
|
||||||
<PackageReference Include="Avalonia.Diagnostics" Version="0.10.13" Condition=" '$(Configuration)' == 'Debug' " />
|
<PackageReference Include="Avalonia.Controls.ItemsRepeater" Version="11.0.0" />
|
||||||
|
<PackageReference Include="Avalonia.Desktop" Version="11.0.0" />
|
||||||
|
<PackageReference Include="Avalonia.Diagnostics" Version="11.0.0" Condition=" '$(Configuration)' == 'Debug' " />
|
||||||
|
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.0.0" />
|
||||||
|
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\Server\Server.csproj" />
|
<ProjectReference Include="..\Server\Server.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="..\Version.cs" Link="Version.cs" />
|
<Compile Include="..\Version.cs" Link="Version.cs" />
|
||||||
|
<Compile Update="App\App.axaml.cs">
|
||||||
|
<DependentUpon>App.axaml</DependentUpon>
|
||||||
|
<SubType>Code</SubType>
|
||||||
|
</Compile>
|
||||||
|
<Compile Update="App\Windows\AboutWindow.axaml.cs">
|
||||||
|
<DependentUpon>AboutWindow.axaml</DependentUpon>
|
||||||
|
<SubType>Code</SubType>
|
||||||
|
</Compile>
|
||||||
|
<Compile Update="App\Windows\MainWindow.axaml.cs">
|
||||||
|
<DependentUpon>MainWindow.axaml</DependentUpon>
|
||||||
|
<SubType>Code</SubType>
|
||||||
|
</Compile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<AvaloniaResource Include="Resources/icon.ico" />
|
<AvaloniaResource Include="Resources/icon.ico" />
|
||||||
@@ -54,6 +69,26 @@
|
|||||||
<Visible>false</Visible>
|
<Visible>false</Visible>
|
||||||
</EmbeddedResource>
|
</EmbeddedResource>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<AdditionalFiles Include="App\Controls\ServerConfigurationPanel.axaml" />
|
||||||
|
<AdditionalFiles Include="App\Controls\StatusBar.axaml" />
|
||||||
|
<AdditionalFiles Include="App\Dialogs\CheckBox\CheckBoxDialog.axaml" />
|
||||||
|
<AdditionalFiles Include="App\Dialogs\Message\MessageDialog.axaml" />
|
||||||
|
<AdditionalFiles Include="App\Dialogs\Progress\ProgressDialog.axaml" />
|
||||||
|
<AdditionalFiles Include="App\Dialogs\TextBox\TextBoxDialog.axaml" />
|
||||||
|
<AdditionalFiles Include="App\Pages\AdvancedPage.axaml" />
|
||||||
|
<AdditionalFiles Include="App\Pages\DatabasePage.axaml" />
|
||||||
|
<AdditionalFiles Include="App\Pages\TrackingPage.axaml" />
|
||||||
|
<AdditionalFiles Include="App\Screens\MainContentScreen.axaml" />
|
||||||
|
<AdditionalFiles Include="App\Screens\WelcomeScreen.axaml" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<UpToDateCheckInput Remove="Main\Pages\AdvancedPage.axaml" />
|
||||||
|
<UpToDateCheckInput Remove="Main\Pages\DatabasePage.axaml" />
|
||||||
|
<UpToDateCheckInput Remove="Main\Pages\TrackingPage.axaml" />
|
||||||
|
<UpToDateCheckInput Remove="Main\Screens\MainContentScreen.axaml" />
|
||||||
|
<UpToDateCheckInput Remove="Main\Screens\WelcomeScreen.axaml" />
|
||||||
|
</ItemGroup>
|
||||||
<Target Name="MinifyResources" BeforeTargets="PrepareForBuild" Condition=" '$(Configuration)' == 'Release' ">
|
<Target Name="MinifyResources" BeforeTargets="PrepareForBuild" Condition=" '$(Configuration)' == 'Release' ">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<MinifiedResourceDir>$(ProjectDir)bin/.res/scripts</MinifiedResourceDir>
|
<MinifiedResourceDir>$(ProjectDir)bin/.res/scripts</MinifiedResourceDir>
|
||||||
|
@@ -1,31 +0,0 @@
|
|||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
using Avalonia;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using Avalonia.Interactivity;
|
|
||||||
using Avalonia.Markup.Xaml;
|
|
||||||
using DHT.Desktop.Dialogs.Message;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Dialogs.CheckBox {
|
|
||||||
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
|
||||||
public sealed class CheckBoxDialog : Window {
|
|
||||||
public CheckBoxDialog() {
|
|
||||||
InitializeComponent();
|
|
||||||
#if DEBUG
|
|
||||||
this.AttachDevTools();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitializeComponent() {
|
|
||||||
AvaloniaXamlLoader.Load(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ClickOk(object? sender, RoutedEventArgs e) {
|
|
||||||
Close(DialogResult.OkCancel.Ok);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ClickCancel(object? sender, RoutedEventArgs e) {
|
|
||||||
Close(DialogResult.OkCancel.Cancel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@@ -1,70 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.ComponentModel;
|
|
||||||
using System.Linq;
|
|
||||||
using DHT.Utils.Models;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Dialogs.CheckBox {
|
|
||||||
class CheckBoxDialogModel : BaseModel {
|
|
||||||
public string Title { get; init; } = "";
|
|
||||||
|
|
||||||
private IReadOnlyList<CheckBoxItem> items = Array.Empty<CheckBoxItem>();
|
|
||||||
|
|
||||||
public IReadOnlyList<CheckBoxItem> Items {
|
|
||||||
get => items;
|
|
||||||
|
|
||||||
protected set {
|
|
||||||
foreach (var item in items) {
|
|
||||||
item.PropertyChanged -= OnItemPropertyChanged;
|
|
||||||
}
|
|
||||||
|
|
||||||
items = value;
|
|
||||||
|
|
||||||
foreach (var item in items) {
|
|
||||||
item.PropertyChanged += OnItemPropertyChanged;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool pauseCheckEvents = false;
|
|
||||||
|
|
||||||
public bool AreAllSelected => Items.All(static item => item.Checked);
|
|
||||||
public bool AreNoneSelected => Items.All(static item => !item.Checked);
|
|
||||||
|
|
||||||
public void SelectAll() => SetAllChecked(true);
|
|
||||||
public void SelectNone() => SetAllChecked(false);
|
|
||||||
|
|
||||||
private void SetAllChecked(bool isChecked) {
|
|
||||||
pauseCheckEvents = true;
|
|
||||||
|
|
||||||
foreach (var item in Items) {
|
|
||||||
item.Checked = isChecked;
|
|
||||||
}
|
|
||||||
|
|
||||||
pauseCheckEvents = false;
|
|
||||||
UpdateBulkButtons();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void UpdateBulkButtons() {
|
|
||||||
OnPropertyChanged(nameof(AreAllSelected));
|
|
||||||
OnPropertyChanged(nameof(AreNoneSelected));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnItemPropertyChanged(object? sender, PropertyChangedEventArgs e) {
|
|
||||||
if (!pauseCheckEvents && e.PropertyName == nameof(CheckBoxItem.Checked)) {
|
|
||||||
UpdateBulkButtons();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sealed class CheckBoxDialogModel<T> : CheckBoxDialogModel {
|
|
||||||
public new IReadOnlyList<CheckBoxItem<T>> Items { get; }
|
|
||||||
|
|
||||||
public IEnumerable<CheckBoxItem<T>> SelectedItems => Items.Where(static item => item.Checked);
|
|
||||||
|
|
||||||
public CheckBoxDialogModel(IEnumerable<CheckBoxItem<T>> items) {
|
|
||||||
this.Items = new List<CheckBoxItem<T>>(items);
|
|
||||||
base.Items = this.Items;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,24 +0,0 @@
|
|||||||
using DHT.Utils.Models;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Dialogs.CheckBox {
|
|
||||||
class CheckBoxItem : BaseModel {
|
|
||||||
public string Title { get; init; } = "";
|
|
||||||
public object? Item { get; init; } = null;
|
|
||||||
|
|
||||||
private bool isChecked = false;
|
|
||||||
|
|
||||||
public bool Checked {
|
|
||||||
get => isChecked;
|
|
||||||
set => Change(ref isChecked, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sealed class CheckBoxItem<T> : CheckBoxItem {
|
|
||||||
public new T Item { get; }
|
|
||||||
|
|
||||||
public CheckBoxItem(T item) {
|
|
||||||
this.Item = item;
|
|
||||||
base.Item = item;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,74 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using Avalonia.Threading;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Dialogs.Message {
|
|
||||||
static class Dialog {
|
|
||||||
public static async Task ShowOk(Window owner, string title, string message) {
|
|
||||||
if (!Dispatcher.UIThread.CheckAccess()) {
|
|
||||||
await Dispatcher.UIThread.InvokeAsync(() => ShowOk(owner, title, message));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
await new MessageDialog {
|
|
||||||
DataContext = new MessageDialogModel {
|
|
||||||
Title = title,
|
|
||||||
Message = message,
|
|
||||||
IsOkVisible = true
|
|
||||||
}
|
|
||||||
}.ShowDialog<DialogResult.All>(owner);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async Task<DialogResult.OkCancel> ShowOkCancel(Window owner, string title, string message) {
|
|
||||||
if (!Dispatcher.UIThread.CheckAccess()) {
|
|
||||||
return await Dispatcher.UIThread.InvokeAsync(() => ShowOkCancel(owner, title, message));
|
|
||||||
}
|
|
||||||
|
|
||||||
var result = await new MessageDialog {
|
|
||||||
DataContext = new MessageDialogModel {
|
|
||||||
Title = title,
|
|
||||||
Message = message,
|
|
||||||
IsOkVisible = true,
|
|
||||||
IsCancelVisible = true
|
|
||||||
}
|
|
||||||
}.ShowDialog<DialogResult.All?>(owner);
|
|
||||||
|
|
||||||
return result.ToOkCancel();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async Task<DialogResult.YesNo> ShowYesNo(Window owner, string title, string message) {
|
|
||||||
if (!Dispatcher.UIThread.CheckAccess()) {
|
|
||||||
return await Dispatcher.UIThread.InvokeAsync(() => ShowYesNo(owner, title, message));
|
|
||||||
}
|
|
||||||
|
|
||||||
var result = await new MessageDialog {
|
|
||||||
DataContext = new MessageDialogModel {
|
|
||||||
Title = title,
|
|
||||||
Message = message,
|
|
||||||
IsYesVisible = true,
|
|
||||||
IsNoVisible = true
|
|
||||||
}
|
|
||||||
}.ShowDialog<DialogResult.All?>(owner);
|
|
||||||
|
|
||||||
return result.ToYesNo();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async Task<DialogResult.YesNoCancel> ShowYesNoCancel(Window owner, string title, string message) {
|
|
||||||
if (!Dispatcher.UIThread.CheckAccess()) {
|
|
||||||
return await Dispatcher.UIThread.InvokeAsync(() => ShowYesNoCancel(owner, title, message));
|
|
||||||
}
|
|
||||||
|
|
||||||
var result = await new MessageDialog {
|
|
||||||
DataContext = new MessageDialogModel {
|
|
||||||
Title = title,
|
|
||||||
Message = message,
|
|
||||||
IsYesVisible = true,
|
|
||||||
IsNoVisible = true,
|
|
||||||
IsCancelVisible = true
|
|
||||||
}
|
|
||||||
}.ShowDialog<DialogResult.All?>(owner);
|
|
||||||
|
|
||||||
return result.ToYesNoCancel();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,59 +0,0 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Dialogs.Message {
|
|
||||||
static class DialogResult {
|
|
||||||
public enum All {
|
|
||||||
Ok,
|
|
||||||
Yes,
|
|
||||||
No,
|
|
||||||
Cancel
|
|
||||||
}
|
|
||||||
|
|
||||||
public enum OkCancel {
|
|
||||||
Closed,
|
|
||||||
Ok,
|
|
||||||
Cancel
|
|
||||||
}
|
|
||||||
|
|
||||||
public enum YesNo {
|
|
||||||
Closed,
|
|
||||||
Yes,
|
|
||||||
No
|
|
||||||
}
|
|
||||||
|
|
||||||
public enum YesNoCancel {
|
|
||||||
Closed,
|
|
||||||
Yes,
|
|
||||||
No,
|
|
||||||
Cancel
|
|
||||||
}
|
|
||||||
|
|
||||||
public static OkCancel ToOkCancel(this All? result) {
|
|
||||||
return result switch {
|
|
||||||
null => OkCancel.Closed,
|
|
||||||
All.Ok => OkCancel.Ok,
|
|
||||||
All.Cancel => OkCancel.Cancel,
|
|
||||||
_ => throw new ArgumentException("Cannot convert dialog result " + result + " to ok/cancel.")
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public static YesNo ToYesNo(this All? result) {
|
|
||||||
return result switch {
|
|
||||||
null => YesNo.Closed,
|
|
||||||
All.Yes => YesNo.Yes,
|
|
||||||
All.No => YesNo.No,
|
|
||||||
_ => throw new ArgumentException("Cannot convert dialog result " + result + " to yes/no.")
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public static YesNoCancel ToYesNoCancel(this All? result) {
|
|
||||||
return result switch {
|
|
||||||
null => YesNoCancel.Closed,
|
|
||||||
All.Yes => YesNoCancel.Yes,
|
|
||||||
All.No => YesNoCancel.No,
|
|
||||||
All.Cancel => YesNoCancel.Cancel,
|
|
||||||
_ => throw new ArgumentException("Cannot convert dialog result " + result + " to yes/no/cancel.")
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,37 +0,0 @@
|
|||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
using Avalonia;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using Avalonia.Interactivity;
|
|
||||||
using Avalonia.Markup.Xaml;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Dialogs.Message {
|
|
||||||
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
|
||||||
public sealed class MessageDialog : Window {
|
|
||||||
public MessageDialog() {
|
|
||||||
InitializeComponent();
|
|
||||||
#if DEBUG
|
|
||||||
this.AttachDevTools();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitializeComponent() {
|
|
||||||
AvaloniaXamlLoader.Load(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ClickOk(object? sender, RoutedEventArgs e) {
|
|
||||||
Close(DialogResult.All.Ok);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ClickYes(object? sender, RoutedEventArgs e) {
|
|
||||||
Close(DialogResult.All.Yes);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ClickNo(object? sender, RoutedEventArgs e) {
|
|
||||||
Close(DialogResult.All.No);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ClickCancel(object? sender, RoutedEventArgs e) {
|
|
||||||
Close(DialogResult.All.Cancel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,11 +0,0 @@
|
|||||||
namespace DHT.Desktop.Dialogs.Message {
|
|
||||||
sealed class MessageDialogModel {
|
|
||||||
public string Title { get; init; } = "";
|
|
||||||
public string Message { get; init; } = "";
|
|
||||||
|
|
||||||
public bool IsOkVisible { get; init; } = false;
|
|
||||||
public bool IsYesVisible { get; init; } = false;
|
|
||||||
public bool IsNoVisible { get; init; } = false;
|
|
||||||
public bool IsCancelVisible { get; init; } = false;
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,7 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Dialogs.Progress {
|
|
||||||
interface IProgressCallback {
|
|
||||||
Task Update(string message, int finishedItems, int totalItems);
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,40 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.ComponentModel;
|
|
||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Avalonia;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using Avalonia.Markup.Xaml;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Dialogs.Progress {
|
|
||||||
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
|
||||||
public sealed class ProgressDialog : Window {
|
|
||||||
private bool isFinished = false;
|
|
||||||
|
|
||||||
public ProgressDialog() {
|
|
||||||
InitializeComponent();
|
|
||||||
#if DEBUG
|
|
||||||
this.AttachDevTools();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitializeComponent() {
|
|
||||||
AvaloniaXamlLoader.Load(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnClosing(object? sender, CancelEventArgs e) {
|
|
||||||
e.Cancel = !isFinished;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Loaded(object? sender, EventArgs e) {
|
|
||||||
if (DataContext is ProgressDialogModel model) {
|
|
||||||
Task.Run(model.StartTask).ContinueWith(OnFinished, TaskScheduler.FromCurrentSynchronizationContext());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnFinished(Task task) {
|
|
||||||
isFinished = true;
|
|
||||||
Close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,65 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Avalonia.Threading;
|
|
||||||
using DHT.Desktop.Common;
|
|
||||||
using DHT.Utils.Models;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Dialogs.Progress {
|
|
||||||
sealed class ProgressDialogModel : BaseModel {
|
|
||||||
public string Title { get; init; } = "";
|
|
||||||
|
|
||||||
private string message = "";
|
|
||||||
|
|
||||||
public string Message {
|
|
||||||
get => message;
|
|
||||||
private set => Change(ref message, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
private string items = "";
|
|
||||||
|
|
||||||
public string Items {
|
|
||||||
get => items;
|
|
||||||
private set => Change(ref items, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
private int progress = 0;
|
|
||||||
|
|
||||||
public int Progress {
|
|
||||||
get => progress;
|
|
||||||
private set => Change(ref progress, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
private readonly TaskRunner? task;
|
|
||||||
|
|
||||||
[Obsolete("Designer")]
|
|
||||||
public ProgressDialogModel() {}
|
|
||||||
|
|
||||||
public ProgressDialogModel(TaskRunner task) {
|
|
||||||
this.task = task;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal async Task StartTask() {
|
|
||||||
if (task != null) {
|
|
||||||
await task(new Callback(this));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public delegate Task TaskRunner(IProgressCallback callback);
|
|
||||||
|
|
||||||
private sealed class Callback : IProgressCallback {
|
|
||||||
private readonly ProgressDialogModel model;
|
|
||||||
|
|
||||||
public Callback(ProgressDialogModel model) {
|
|
||||||
this.model = model;
|
|
||||||
}
|
|
||||||
|
|
||||||
async Task IProgressCallback.Update(string message, int finishedItems, int totalItems) {
|
|
||||||
await Dispatcher.UIThread.InvokeAsync(() => {
|
|
||||||
model.Message = message;
|
|
||||||
model.Items = finishedItems.Format() + " / " + totalItems.Format();
|
|
||||||
model.Progress = 100 * finishedItems / totalItems;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -9,111 +9,111 @@ using DHT.Utils.Logging;
|
|||||||
using static System.Environment.SpecialFolder;
|
using static System.Environment.SpecialFolder;
|
||||||
using static System.Environment.SpecialFolderOption;
|
using static System.Environment.SpecialFolderOption;
|
||||||
|
|
||||||
namespace DHT.Desktop.Discord {
|
namespace DHT.Desktop.Discord;
|
||||||
static class DiscordAppSettings {
|
|
||||||
private static readonly Log Log = Log.ForType(typeof(DiscordAppSettings));
|
|
||||||
|
|
||||||
private const string JsonKeyDevTools = "DANGEROUS_ENABLE_DEVTOOLS_ONLY_ENABLE_IF_YOU_KNOW_WHAT_YOURE_DOING";
|
static class DiscordAppSettings {
|
||||||
|
private static readonly Log Log = Log.ForType(typeof(DiscordAppSettings));
|
||||||
|
|
||||||
public static string JsonFilePath { get; }
|
private const string JsonKeyDevTools = "DANGEROUS_ENABLE_DEVTOOLS_ONLY_ENABLE_IF_YOU_KNOW_WHAT_YOURE_DOING";
|
||||||
private static string JsonBackupFilePath { get; }
|
|
||||||
|
|
||||||
[SuppressMessage("ReSharper", "ConvertIfStatementToConditionalTernaryExpression")]
|
public static string JsonFilePath { get; }
|
||||||
static DiscordAppSettings() {
|
private static string JsonBackupFilePath { get; }
|
||||||
string rootFolder;
|
|
||||||
|
|
||||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
|
[SuppressMessage("ReSharper", "ConvertIfStatementToConditionalTernaryExpression")]
|
||||||
rootFolder = Path.Combine(Environment.GetFolderPath(ApplicationData, DoNotVerify), "Discord");
|
static DiscordAppSettings() {
|
||||||
}
|
string rootFolder;
|
||||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
|
|
||||||
rootFolder = Path.Combine(Environment.GetFolderPath(UserProfile, DoNotVerify), "Library", "Application Support", "Discord");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
rootFolder = Path.Combine(Environment.GetFolderPath(ApplicationData, DoNotVerify), "discord");
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonFilePath = Path.Combine(rootFolder, "settings.json");
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
|
||||||
JsonBackupFilePath = JsonFilePath + ".bak";
|
rootFolder = Path.Combine(Environment.GetFolderPath(ApplicationData, DoNotVerify), "Discord");
|
||||||
|
}
|
||||||
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
|
||||||
|
rootFolder = Path.Combine(Environment.GetFolderPath(UserProfile, DoNotVerify), "Library", "Application Support", "Discord");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
rootFolder = Path.Combine(Environment.GetFolderPath(ApplicationData, DoNotVerify), "discord");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<bool?> AreDevToolsEnabled() {
|
JsonFilePath = Path.Combine(rootFolder, "settings.json");
|
||||||
try {
|
JsonBackupFilePath = JsonFilePath + ".bak";
|
||||||
return AreDevToolsEnabled(await ReadSettingsJson());
|
}
|
||||||
} catch (Exception) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool AreDevToolsEnabled(Dictionary<string, object?> json) {
|
public static async Task<bool?> AreDevToolsEnabled() {
|
||||||
return json.TryGetValue(JsonKeyDevTools, out var value) && value is JsonElement { ValueKind: JsonValueKind.True };
|
try {
|
||||||
}
|
return AreDevToolsEnabled(await ReadSettingsJson());
|
||||||
|
} catch (Exception) {
|
||||||
public static async Task<SettingsJsonResult> ConfigureDevTools(bool enable) {
|
return null;
|
||||||
Dictionary<string, object?> json;
|
|
||||||
|
|
||||||
try {
|
|
||||||
json = await ReadSettingsJson();
|
|
||||||
} catch (FileNotFoundException) {
|
|
||||||
return SettingsJsonResult.FileNotFound;
|
|
||||||
} catch (JsonException) {
|
|
||||||
return SettingsJsonResult.InvalidJson;
|
|
||||||
} catch (Exception e) {
|
|
||||||
Log.Error(e);
|
|
||||||
return SettingsJsonResult.ReadError;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (enable == AreDevToolsEnabled(json)) {
|
|
||||||
return SettingsJsonResult.AlreadySet;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (enable) {
|
|
||||||
json[JsonKeyDevTools] = true;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
json.Remove(JsonKeyDevTools);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (!File.Exists(JsonBackupFilePath)) {
|
|
||||||
File.Copy(JsonFilePath, JsonBackupFilePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
await WriteSettingsJson(json);
|
|
||||||
} catch (Exception e) {
|
|
||||||
Log.Error("An error occurred when writing settings file.");
|
|
||||||
Log.Error(e);
|
|
||||||
|
|
||||||
if (File.Exists(JsonBackupFilePath)) {
|
|
||||||
try {
|
|
||||||
File.Move(JsonBackupFilePath, JsonFilePath, true);
|
|
||||||
Log.Info("Restored settings file from backup.");
|
|
||||||
} catch (Exception e2) {
|
|
||||||
Log.Error("Cannot restore settings file from backup.");
|
|
||||||
Log.Error(e2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return SettingsJsonResult.WriteError;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
File.Delete(JsonBackupFilePath);
|
|
||||||
} catch (Exception e) {
|
|
||||||
Log.Error("Cannot delete backup file.");
|
|
||||||
Log.Error(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return SettingsJsonResult.Success;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async Task<Dictionary<string, object?>> ReadSettingsJson() {
|
|
||||||
await using var stream = new FileStream(JsonFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
|
|
||||||
return await JsonSerializer.DeserializeAsync<Dictionary<string, object?>?>(stream) ?? throw new JsonException();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async Task WriteSettingsJson(Dictionary<string, object?> json) {
|
|
||||||
await using var stream = new FileStream(JsonFilePath, FileMode.Truncate, FileAccess.Write, FileShare.None);
|
|
||||||
await JsonSerializer.SerializeAsync(stream, json, new JsonSerializerOptions { WriteIndented = true });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static bool AreDevToolsEnabled(Dictionary<string, object?> json) {
|
||||||
|
return json.TryGetValue(JsonKeyDevTools, out var value) && value is JsonElement { ValueKind: JsonValueKind.True };
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<SettingsJsonResult> ConfigureDevTools(bool enable) {
|
||||||
|
Dictionary<string, object?> json;
|
||||||
|
|
||||||
|
try {
|
||||||
|
json = await ReadSettingsJson();
|
||||||
|
} catch (FileNotFoundException) {
|
||||||
|
return SettingsJsonResult.FileNotFound;
|
||||||
|
} catch (JsonException) {
|
||||||
|
return SettingsJsonResult.InvalidJson;
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.Error(e);
|
||||||
|
return SettingsJsonResult.ReadError;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (enable == AreDevToolsEnabled(json)) {
|
||||||
|
return SettingsJsonResult.AlreadySet;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (enable) {
|
||||||
|
json[JsonKeyDevTools] = true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
json.Remove(JsonKeyDevTools);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!File.Exists(JsonBackupFilePath)) {
|
||||||
|
File.Copy(JsonFilePath, JsonBackupFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
await WriteSettingsJson(json);
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.Error("An error occurred when writing settings file.");
|
||||||
|
Log.Error(e);
|
||||||
|
|
||||||
|
if (File.Exists(JsonBackupFilePath)) {
|
||||||
|
try {
|
||||||
|
File.Move(JsonBackupFilePath, JsonFilePath, true);
|
||||||
|
Log.Info("Restored settings file from backup.");
|
||||||
|
} catch (Exception e2) {
|
||||||
|
Log.Error("Cannot restore settings file from backup.");
|
||||||
|
Log.Error(e2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return SettingsJsonResult.WriteError;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
File.Delete(JsonBackupFilePath);
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.Error("Cannot delete backup file.");
|
||||||
|
Log.Error(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return SettingsJsonResult.Success;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task<Dictionary<string, object?>> ReadSettingsJson() {
|
||||||
|
await using var stream = new FileStream(JsonFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||||
|
return await JsonSerializer.DeserializeAsync<Dictionary<string, object?>?>(stream) ?? throw new JsonException();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task WriteSettingsJson(Dictionary<string, object?> json) {
|
||||||
|
await using var stream = new FileStream(JsonFilePath, FileMode.Truncate, FileAccess.Write, FileShare.None);
|
||||||
|
await JsonSerializer.SerializeAsync(stream, json, new JsonSerializerOptions { WriteIndented = true });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,10 +1,10 @@
|
|||||||
namespace DHT.Desktop.Discord {
|
namespace DHT.Desktop.Discord;
|
||||||
enum SettingsJsonResult {
|
|
||||||
Success,
|
enum SettingsJsonResult {
|
||||||
AlreadySet,
|
Success,
|
||||||
FileNotFound,
|
AlreadySet,
|
||||||
ReadError,
|
FileNotFound,
|
||||||
InvalidJson,
|
ReadError,
|
||||||
WriteError
|
InvalidJson,
|
||||||
}
|
WriteError
|
||||||
}
|
}
|
||||||
|
@@ -1,20 +0,0 @@
|
|||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
using Avalonia;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using Avalonia.Markup.Xaml;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main {
|
|
||||||
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
|
||||||
public sealed class AboutWindow : Window {
|
|
||||||
public AboutWindow() {
|
|
||||||
InitializeComponent();
|
|
||||||
#if DEBUG
|
|
||||||
this.AttachDevTools();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitializeComponent() {
|
|
||||||
AvaloniaXamlLoader.Load(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,33 +0,0 @@
|
|||||||
using System.Diagnostics;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main {
|
|
||||||
sealed class AboutWindowModel {
|
|
||||||
public void ShowOfficialWebsite() {
|
|
||||||
OpenUrl("https://dht.chylex.com");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ShowSourceCode() {
|
|
||||||
OpenUrl("https://github.com/chylex/Discord-History-Tracker");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ShowLibraryAvalonia() {
|
|
||||||
OpenUrl("https://www.nuget.org/packages/Avalonia");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ShowLibrarySqlite() {
|
|
||||||
OpenUrl("https://www.sqlite.org");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ShowLibrarySqliteAdoNet() {
|
|
||||||
OpenUrl("https://www.nuget.org/packages/Microsoft.Data.Sqlite");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ShowLibraryNetCore() {
|
|
||||||
OpenUrl("https://github.com/dotnet/core");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void OpenUrl(string url) {
|
|
||||||
Process.Start(new ProcessStartInfo { FileName = url, UseShellExecute = true });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,60 +0,0 @@
|
|||||||
<UserControl xmlns="https://github.com/avaloniaui"
|
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
|
||||||
xmlns:controls="clr-namespace:DHT.Desktop.Main.Controls"
|
|
||||||
mc:Ignorable="d"
|
|
||||||
x:Class="DHT.Desktop.Main.Controls.FilterPanel">
|
|
||||||
|
|
||||||
<Design.DataContext>
|
|
||||||
<controls:FilterPanelModel />
|
|
||||||
</Design.DataContext>
|
|
||||||
|
|
||||||
<UserControl.Styles>
|
|
||||||
<Style Selector="WrapPanel > StackPanel">
|
|
||||||
<Setter Property="Margin" Value="0 20 40 0" />
|
|
||||||
<Setter Property="Spacing" Value="4" />
|
|
||||||
</Style>
|
|
||||||
<Style Selector="WrapPanel > StackPanel:nth-last-child(1)">
|
|
||||||
<Setter Property="Margin" Value="0 20 0 0" />
|
|
||||||
</Style>
|
|
||||||
<Style Selector="Grid > Label">
|
|
||||||
<Setter Property="VerticalAlignment" Value="Center" />
|
|
||||||
</Style>
|
|
||||||
<Style Selector="Grid > CalendarDatePicker">
|
|
||||||
<Setter Property="CornerRadius" Value="0" />
|
|
||||||
<Setter Property="HorizontalAlignment" Value="Stretch" />
|
|
||||||
<Setter Property="HorizontalContentAlignment" Value="Center" />
|
|
||||||
<Setter Property="VerticalAlignment" Value="Center" />
|
|
||||||
<Setter Property="VerticalContentAlignment" Value="Center" />
|
|
||||||
<Setter Property="IsTodayHighlighted" Value="True" />
|
|
||||||
<Setter Property="SelectedDateFormat" Value="Short" />
|
|
||||||
</Style>
|
|
||||||
<Style Selector="Button">
|
|
||||||
<Setter Property="Margin" Value="0 0 0 8" />
|
|
||||||
</Style>
|
|
||||||
</UserControl.Styles>
|
|
||||||
|
|
||||||
<WrapPanel>
|
|
||||||
<StackPanel>
|
|
||||||
<CheckBox IsChecked="{Binding FilterByDate}">Filter by Date</CheckBox>
|
|
||||||
<Grid ColumnDefinitions="Auto, 4, 125" RowDefinitions="Auto, 4, Auto" Margin="4 0">
|
|
||||||
<Label Grid.Row="0" Grid.Column="0">From:</Label>
|
|
||||||
<CalendarDatePicker Grid.Row="0" Grid.Column="2" x:Name="StartDatePicker" IsEnabled="{Binding FilterByDate}" SelectedDateChanged="CalendarDatePicker_OnSelectedDateChanged" />
|
|
||||||
<Label Grid.Row="2" Grid.Column="0">To:</Label>
|
|
||||||
<CalendarDatePicker Grid.Row="2" Grid.Column="2" x:Name="EndDatePicker" IsEnabled="{Binding FilterByDate}" SelectedDateChanged="CalendarDatePicker_OnSelectedDateChanged" />
|
|
||||||
</Grid>
|
|
||||||
</StackPanel>
|
|
||||||
<StackPanel>
|
|
||||||
<CheckBox IsChecked="{Binding FilterByChannel}">Filter by Channel</CheckBox>
|
|
||||||
<Button Command="{Binding OpenChannelFilterDialog}" IsEnabled="{Binding FilterByChannel}">Select Channels...</Button>
|
|
||||||
<TextBlock Text="{Binding ChannelFilterLabel}" />
|
|
||||||
</StackPanel>
|
|
||||||
<StackPanel>
|
|
||||||
<CheckBox IsChecked="{Binding FilterByUser}">Filter by User</CheckBox>
|
|
||||||
<Button Command="{Binding OpenUserFilterDialog}" IsEnabled="{Binding FilterByUser}">Select Users...</Button>
|
|
||||||
<TextBlock Text="{Binding UserFilterLabel}" />
|
|
||||||
</StackPanel>
|
|
||||||
</WrapPanel>
|
|
||||||
|
|
||||||
</UserControl>
|
|
@@ -1,34 +0,0 @@
|
|||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using Avalonia.Markup.Xaml;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Controls {
|
|
||||||
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
|
||||||
public sealed class FilterPanel : UserControl {
|
|
||||||
private CalendarDatePicker StartDatePicker => this.FindControl<CalendarDatePicker>("StartDatePicker");
|
|
||||||
private CalendarDatePicker EndDatePicker => this.FindControl<CalendarDatePicker>("EndDatePicker");
|
|
||||||
|
|
||||||
public FilterPanel() {
|
|
||||||
InitializeComponent();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitializeComponent() {
|
|
||||||
AvaloniaXamlLoader.Load(this);
|
|
||||||
|
|
||||||
var culture = Program.Culture;
|
|
||||||
foreach (var picker in new CalendarDatePicker[] { StartDatePicker, EndDatePicker }) {
|
|
||||||
picker.FirstDayOfWeek = culture.DateTimeFormat.FirstDayOfWeek;
|
|
||||||
picker.SelectedDateFormat = CalendarDatePickerFormat.Custom;
|
|
||||||
picker.CustomDateFormatString = culture.DateTimeFormat.ShortDatePattern;
|
|
||||||
picker.Watermark = culture.DateTimeFormat.ShortDatePattern;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void CalendarDatePicker_OnSelectedDateChanged(object? sender, SelectionChangedEventArgs e) {
|
|
||||||
if (DataContext is FilterPanelModel model) {
|
|
||||||
model.StartDate = StartDatePicker.SelectedDate;
|
|
||||||
model.EndDate = EndDatePicker.SelectedDate;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,241 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.ComponentModel;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using DHT.Desktop.Common;
|
|
||||||
using DHT.Desktop.Dialogs.CheckBox;
|
|
||||||
using DHT.Desktop.Dialogs.Message;
|
|
||||||
using DHT.Server.Data;
|
|
||||||
using DHT.Server.Data.Filters;
|
|
||||||
using DHT.Server.Database;
|
|
||||||
using DHT.Utils.Models;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Controls {
|
|
||||||
sealed class FilterPanelModel : BaseModel, IDisposable {
|
|
||||||
private static readonly HashSet<string> FilterProperties = new () {
|
|
||||||
nameof(FilterByDate),
|
|
||||||
nameof(StartDate),
|
|
||||||
nameof(EndDate),
|
|
||||||
nameof(FilterByChannel),
|
|
||||||
nameof(IncludedChannels),
|
|
||||||
nameof(FilterByUser),
|
|
||||||
nameof(IncludedUsers)
|
|
||||||
};
|
|
||||||
|
|
||||||
public event PropertyChangedEventHandler? FilterPropertyChanged;
|
|
||||||
|
|
||||||
public bool HasAnyFilters => FilterByDate || FilterByChannel || FilterByUser;
|
|
||||||
|
|
||||||
private bool filterByDate = false;
|
|
||||||
private DateTime? startDate = null;
|
|
||||||
private DateTime? endDate = null;
|
|
||||||
private bool filterByChannel = false;
|
|
||||||
private HashSet<ulong>? includedChannels = null;
|
|
||||||
private bool filterByUser = false;
|
|
||||||
private HashSet<ulong>? includedUsers = null;
|
|
||||||
|
|
||||||
public bool FilterByDate {
|
|
||||||
get => filterByDate;
|
|
||||||
set => Change(ref filterByDate, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public DateTime? StartDate {
|
|
||||||
get => startDate;
|
|
||||||
set => Change(ref startDate, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public DateTime? EndDate {
|
|
||||||
get => endDate;
|
|
||||||
set => Change(ref endDate, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool FilterByChannel {
|
|
||||||
get => filterByChannel;
|
|
||||||
set => Change(ref filterByChannel, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public HashSet<ulong> IncludedChannels {
|
|
||||||
get => includedChannels ?? db.GetAllChannels().Select(static channel => channel.Id).ToHashSet();
|
|
||||||
set => Change(ref includedChannels, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool FilterByUser {
|
|
||||||
get => filterByUser;
|
|
||||||
set => Change(ref filterByUser, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public HashSet<ulong> IncludedUsers {
|
|
||||||
get => includedUsers ?? db.GetAllUsers().Select(static user => user.Id).ToHashSet();
|
|
||||||
set => Change(ref includedUsers, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
private string channelFilterLabel = "";
|
|
||||||
|
|
||||||
public string ChannelFilterLabel {
|
|
||||||
get => channelFilterLabel;
|
|
||||||
set => Change(ref channelFilterLabel, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
private string userFilterLabel = "";
|
|
||||||
|
|
||||||
public string UserFilterLabel {
|
|
||||||
get => userFilterLabel;
|
|
||||||
set => Change(ref userFilterLabel, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
private readonly Window window;
|
|
||||||
private readonly IDatabaseFile db;
|
|
||||||
|
|
||||||
[Obsolete("Designer")]
|
|
||||||
public FilterPanelModel() : this(null!, DummyDatabaseFile.Instance) {}
|
|
||||||
|
|
||||||
public FilterPanelModel(Window window, IDatabaseFile db) {
|
|
||||||
this.window = window;
|
|
||||||
this.db = db;
|
|
||||||
|
|
||||||
UpdateChannelFilterLabel();
|
|
||||||
UpdateUserFilterLabel();
|
|
||||||
|
|
||||||
PropertyChanged += OnPropertyChanged;
|
|
||||||
db.Statistics.PropertyChanged += OnDbStatisticsChanged;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose() {
|
|
||||||
db.Statistics.PropertyChanged -= OnDbStatisticsChanged;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnPropertyChanged(object? sender, PropertyChangedEventArgs e) {
|
|
||||||
if (e.PropertyName != null && FilterProperties.Contains(e.PropertyName)) {
|
|
||||||
FilterPropertyChanged?.Invoke(sender, e);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (e.PropertyName is nameof(FilterByChannel) or nameof(IncludedChannels)) {
|
|
||||||
UpdateChannelFilterLabel();
|
|
||||||
}
|
|
||||||
else if (e.PropertyName is nameof(FilterByUser) or nameof(IncludedUsers)) {
|
|
||||||
UpdateUserFilterLabel();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnDbStatisticsChanged(object? sender, PropertyChangedEventArgs e) {
|
|
||||||
if (e.PropertyName == nameof(DatabaseStatistics.TotalChannels)) {
|
|
||||||
UpdateChannelFilterLabel();
|
|
||||||
}
|
|
||||||
else if (e.PropertyName == nameof(DatabaseStatistics.TotalUsers)) {
|
|
||||||
UpdateUserFilterLabel();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async void OpenChannelFilterDialog() {
|
|
||||||
var servers = db.GetAllServers().ToDictionary(static server => server.Id);
|
|
||||||
var items = new List<CheckBoxItem<ulong>>();
|
|
||||||
var included = IncludedChannels;
|
|
||||||
|
|
||||||
foreach (var channel in db.GetAllChannels()) {
|
|
||||||
var channelId = channel.Id;
|
|
||||||
var channelName = channel.Name;
|
|
||||||
|
|
||||||
string title;
|
|
||||||
if (servers.TryGetValue(channel.Server, out var server)) {
|
|
||||||
var titleBuilder = new StringBuilder();
|
|
||||||
var serverType = server.Type;
|
|
||||||
|
|
||||||
titleBuilder.Append('[')
|
|
||||||
.Append(ServerTypes.ToString(serverType))
|
|
||||||
.Append("] ");
|
|
||||||
|
|
||||||
if (serverType == ServerType.DirectMessage) {
|
|
||||||
titleBuilder.Append(channelName);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
titleBuilder.Append(server.Name)
|
|
||||||
.Append(" - ")
|
|
||||||
.Append(channelName);
|
|
||||||
}
|
|
||||||
|
|
||||||
title = titleBuilder.ToString();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
title = channelName;
|
|
||||||
}
|
|
||||||
|
|
||||||
items.Add(new CheckBoxItem<ulong>(channelId) {
|
|
||||||
Title = title,
|
|
||||||
Checked = included.Contains(channelId)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
var result = await OpenIdFilterDialog(window, "Included Channels", items);
|
|
||||||
if (result != null) {
|
|
||||||
IncludedChannels = result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async void OpenUserFilterDialog() {
|
|
||||||
var items = new List<CheckBoxItem<ulong>>();
|
|
||||||
var included = IncludedUsers;
|
|
||||||
|
|
||||||
foreach (var user in db.GetAllUsers()) {
|
|
||||||
var name = user.Name;
|
|
||||||
var discriminator = user.Discriminator;
|
|
||||||
|
|
||||||
items.Add(new CheckBoxItem<ulong>(user.Id) {
|
|
||||||
Title = discriminator == null ? name : name + " #" + discriminator,
|
|
||||||
Checked = included.Contains(user.Id)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
var result = await OpenIdFilterDialog(window, "Included Users", items);
|
|
||||||
if (result != null) {
|
|
||||||
IncludedUsers = result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void UpdateChannelFilterLabel() {
|
|
||||||
long total = db.Statistics.TotalChannels;
|
|
||||||
long included = FilterByChannel ? IncludedChannels.Count : total;
|
|
||||||
ChannelFilterLabel = "Selected " + included.Format() + " / " + total.Pluralize("channel") + ".";
|
|
||||||
}
|
|
||||||
|
|
||||||
private void UpdateUserFilterLabel() {
|
|
||||||
long total = db.Statistics.TotalUsers;
|
|
||||||
long included = FilterByUser ? IncludedUsers.Count : total;
|
|
||||||
UserFilterLabel = "Selected " + included.Format() + " / " + total.Pluralize("user") + ".";
|
|
||||||
}
|
|
||||||
|
|
||||||
public MessageFilter CreateFilter() {
|
|
||||||
MessageFilter filter = new();
|
|
||||||
|
|
||||||
if (FilterByDate) {
|
|
||||||
filter.StartDate = StartDate;
|
|
||||||
filter.EndDate = EndDate?.AddDays(1).AddMilliseconds(-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (FilterByChannel) {
|
|
||||||
filter.ChannelIds = new HashSet<ulong>(IncludedChannels);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (FilterByUser) {
|
|
||||||
filter.UserIds = new HashSet<ulong>(IncludedUsers);
|
|
||||||
}
|
|
||||||
|
|
||||||
return filter;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async Task<HashSet<ulong>?> OpenIdFilterDialog(Window window, string title, List<CheckBoxItem<ulong>> items) {
|
|
||||||
items.Sort(static (item1, item2) => item1.Title.CompareTo(item2.Title));
|
|
||||||
|
|
||||||
var model = new CheckBoxDialogModel<ulong>(items) {
|
|
||||||
Title = title
|
|
||||||
};
|
|
||||||
|
|
||||||
var dialog = new CheckBoxDialog { DataContext = model };
|
|
||||||
var result = await dialog.ShowDialog<DialogResult.OkCancel>(window);
|
|
||||||
|
|
||||||
return result == DialogResult.OkCancel.Ok ? model.SelectedItems.Select(static item => item.Item).ToHashSet() : null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,16 +0,0 @@
|
|||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using Avalonia.Markup.Xaml;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Controls {
|
|
||||||
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
|
||||||
public sealed class ServerConfigurationPanel : UserControl {
|
|
||||||
public ServerConfigurationPanel() {
|
|
||||||
InitializeComponent();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitializeComponent() {
|
|
||||||
AvaloniaXamlLoader.Load(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,116 +0,0 @@
|
|||||||
using System;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using DHT.Desktop.Dialogs.Message;
|
|
||||||
using DHT.Desktop.Server;
|
|
||||||
using DHT.Server.Database;
|
|
||||||
using DHT.Server.Service;
|
|
||||||
using DHT.Utils.Models;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Controls {
|
|
||||||
sealed class ServerConfigurationPanelModel : BaseModel, IDisposable {
|
|
||||||
private string inputPort;
|
|
||||||
|
|
||||||
public string InputPort {
|
|
||||||
get => inputPort;
|
|
||||||
set {
|
|
||||||
Change(ref inputPort, value);
|
|
||||||
OnPropertyChanged(nameof(HasMadeChanges));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private string inputToken;
|
|
||||||
|
|
||||||
public string InputToken {
|
|
||||||
get => inputToken;
|
|
||||||
set {
|
|
||||||
Change(ref inputToken, value);
|
|
||||||
OnPropertyChanged(nameof(HasMadeChanges));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool HasMadeChanges => ServerManager.Port.ToString() != InputPort || ServerManager.Token != InputToken;
|
|
||||||
|
|
||||||
private bool isToggleServerButtonEnabled = true;
|
|
||||||
|
|
||||||
public bool IsToggleServerButtonEnabled {
|
|
||||||
get => isToggleServerButtonEnabled;
|
|
||||||
set => Change(ref isToggleServerButtonEnabled, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public string ToggleServerButtonText => serverManager.IsRunning ? "Stop Server" : "Start Server";
|
|
||||||
|
|
||||||
public event EventHandler<StatusBarModel.Status>? ServerStatusChanged;
|
|
||||||
|
|
||||||
private readonly Window window;
|
|
||||||
private readonly ServerManager serverManager;
|
|
||||||
|
|
||||||
[Obsolete("Designer")]
|
|
||||||
public ServerConfigurationPanelModel() : this(null!, new ServerManager(DummyDatabaseFile.Instance)) {}
|
|
||||||
|
|
||||||
public ServerConfigurationPanelModel(Window window, ServerManager serverManager) {
|
|
||||||
this.window = window;
|
|
||||||
this.serverManager = serverManager;
|
|
||||||
this.inputPort = ServerManager.Port.ToString();
|
|
||||||
this.inputToken = ServerManager.Token;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Initialize() {
|
|
||||||
ServerLauncher.ServerStatusChanged += ServerLauncherOnServerStatusChanged;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose() {
|
|
||||||
ServerLauncher.ServerStatusChanged -= ServerLauncherOnServerStatusChanged;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ServerLauncherOnServerStatusChanged(object? sender, EventArgs e) {
|
|
||||||
ServerStatusChanged?.Invoke(this, serverManager.IsRunning ? StatusBarModel.Status.Ready : StatusBarModel.Status.Stopped);
|
|
||||||
OnPropertyChanged(nameof(ToggleServerButtonText));
|
|
||||||
IsToggleServerButtonEnabled = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void BeforeServerStart() {
|
|
||||||
IsToggleServerButtonEnabled = false;
|
|
||||||
ServerStatusChanged?.Invoke(this, StatusBarModel.Status.Starting);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void StartServer() {
|
|
||||||
BeforeServerStart();
|
|
||||||
serverManager.Launch();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void StopServer() {
|
|
||||||
IsToggleServerButtonEnabled = false;
|
|
||||||
ServerStatusChanged?.Invoke(this, StatusBarModel.Status.Stopping);
|
|
||||||
serverManager.Stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnClickToggleServerButton() {
|
|
||||||
if (serverManager.IsRunning) {
|
|
||||||
StopServer();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
StartServer();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnClickRandomizeToken() {
|
|
||||||
InputToken = ServerUtils.GenerateRandomToken(20);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async void OnClickApplyChanges() {
|
|
||||||
if (!ushort.TryParse(InputPort, out ushort port)) {
|
|
||||||
await Dialog.ShowOk(window, "Invalid Port", "Port must be a number between 0 and 65535.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
BeforeServerStart();
|
|
||||||
serverManager.Relaunch(port, InputToken);
|
|
||||||
OnPropertyChanged(nameof(HasMadeChanges));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnClickCancelChanges() {
|
|
||||||
InputPort = ServerManager.Port.ToString();
|
|
||||||
InputToken = ServerManager.Token;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,16 +0,0 @@
|
|||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using Avalonia.Markup.Xaml;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Controls {
|
|
||||||
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
|
||||||
public sealed class StatusBar : UserControl {
|
|
||||||
public StatusBar() {
|
|
||||||
InitializeComponent();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitializeComponent() {
|
|
||||||
AvaloniaXamlLoader.Load(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,45 +0,0 @@
|
|||||||
using System;
|
|
||||||
using DHT.Server.Database;
|
|
||||||
using DHT.Utils.Models;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Controls {
|
|
||||||
sealed class StatusBarModel : BaseModel {
|
|
||||||
public DatabaseStatistics DatabaseStatistics { get; }
|
|
||||||
|
|
||||||
private Status status = Status.Stopped;
|
|
||||||
|
|
||||||
public Status CurrentStatus {
|
|
||||||
get => status;
|
|
||||||
set {
|
|
||||||
status = value;
|
|
||||||
OnPropertyChanged(nameof(StatusText));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public string StatusText {
|
|
||||||
get {
|
|
||||||
return CurrentStatus switch {
|
|
||||||
Status.Starting => "STARTING",
|
|
||||||
Status.Ready => "READY",
|
|
||||||
Status.Stopping => "STOPPING",
|
|
||||||
Status.Stopped => "STOPPED",
|
|
||||||
_ => ""
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Obsolete("Designer")]
|
|
||||||
public StatusBarModel() : this(new DatabaseStatistics()) {}
|
|
||||||
|
|
||||||
public StatusBarModel(DatabaseStatistics databaseStatistics) {
|
|
||||||
this.DatabaseStatistics = databaseStatistics;
|
|
||||||
}
|
|
||||||
|
|
||||||
public enum Status {
|
|
||||||
Starting,
|
|
||||||
Ready,
|
|
||||||
Stopping,
|
|
||||||
Stopped
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,35 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
using Avalonia;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using Avalonia.Markup.Xaml;
|
|
||||||
using JetBrains.Annotations;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main {
|
|
||||||
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
|
||||||
public sealed class MainWindow : Window {
|
|
||||||
[UsedImplicitly]
|
|
||||||
public MainWindow() {
|
|
||||||
InitializeComponent(Arguments.Empty);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal MainWindow(Arguments args) {
|
|
||||||
InitializeComponent(args);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitializeComponent(Arguments args) {
|
|
||||||
AvaloniaXamlLoader.Load(this);
|
|
||||||
DataContext = new MainWindowModel(this, args);
|
|
||||||
|
|
||||||
#if DEBUG
|
|
||||||
this.AttachDevTools();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnClosed(object? sender, EventArgs e) {
|
|
||||||
if (DataContext is IDisposable disposable) {
|
|
||||||
disposable.Dispose();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,116 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.ComponentModel;
|
|
||||||
using System.IO;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using DHT.Desktop.Dialogs.Message;
|
|
||||||
using DHT.Desktop.Main.Screens;
|
|
||||||
using DHT.Desktop.Server;
|
|
||||||
using DHT.Server.Database;
|
|
||||||
using DHT.Utils.Models;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main {
|
|
||||||
sealed class MainWindowModel : BaseModel, IDisposable {
|
|
||||||
private const string DefaultTitle = "Discord History Tracker";
|
|
||||||
|
|
||||||
public string Title { get; private set; } = DefaultTitle;
|
|
||||||
|
|
||||||
public UserControl CurrentScreen { get; private set; }
|
|
||||||
|
|
||||||
private readonly WelcomeScreen welcomeScreen;
|
|
||||||
private readonly WelcomeScreenModel welcomeScreenModel;
|
|
||||||
|
|
||||||
private MainContentScreen? mainContentScreen;
|
|
||||||
private MainContentScreenModel? mainContentScreenModel;
|
|
||||||
|
|
||||||
private readonly Window window;
|
|
||||||
|
|
||||||
private IDatabaseFile? db;
|
|
||||||
|
|
||||||
[Obsolete("Designer")]
|
|
||||||
public MainWindowModel() : this(null!, Arguments.Empty) {}
|
|
||||||
|
|
||||||
public MainWindowModel(Window window, Arguments args) {
|
|
||||||
this.window = window;
|
|
||||||
|
|
||||||
welcomeScreenModel = new WelcomeScreenModel(window);
|
|
||||||
welcomeScreen = new WelcomeScreen { DataContext = welcomeScreenModel };
|
|
||||||
CurrentScreen = welcomeScreen;
|
|
||||||
|
|
||||||
welcomeScreenModel.PropertyChanged += WelcomeScreenModelOnPropertyChanged;
|
|
||||||
|
|
||||||
var dbFile = args.DatabaseFile;
|
|
||||||
if (!string.IsNullOrWhiteSpace(dbFile)) {
|
|
||||||
async void OnWindowOpened(object? o, EventArgs eventArgs) {
|
|
||||||
window.Opened -= OnWindowOpened;
|
|
||||||
|
|
||||||
// https://github.com/AvaloniaUI/Avalonia/issues/3071
|
|
||||||
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
|
|
||||||
await Task.Delay(500);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (File.Exists(dbFile)) {
|
|
||||||
await welcomeScreenModel.OpenOrCreateDatabaseFromPath(dbFile);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
await Dialog.ShowOk(window, "Database Error", "Database file not found:\n" + dbFile);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
window.Opened += OnWindowOpened;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (args.ServerPort != null) {
|
|
||||||
ServerManager.Port = args.ServerPort.Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (args.ServerToken != null) {
|
|
||||||
ServerManager.Token = args.ServerToken;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async void WelcomeScreenModelOnPropertyChanged(object? sender, PropertyChangedEventArgs e) {
|
|
||||||
if (e.PropertyName == nameof(welcomeScreenModel.Db)) {
|
|
||||||
if (mainContentScreenModel != null) {
|
|
||||||
mainContentScreenModel.DatabaseClosed -= MainContentScreenModelOnDatabaseClosed;
|
|
||||||
mainContentScreenModel.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
db?.Dispose();
|
|
||||||
db = welcomeScreenModel.Db;
|
|
||||||
|
|
||||||
if (db == null) {
|
|
||||||
Title = DefaultTitle;
|
|
||||||
mainContentScreenModel = null;
|
|
||||||
mainContentScreen = null;
|
|
||||||
CurrentScreen = welcomeScreen;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Title = Path.GetFileName(db.Path) + " - " + DefaultTitle;
|
|
||||||
mainContentScreenModel = new MainContentScreenModel(window, db);
|
|
||||||
await mainContentScreenModel.Initialize();
|
|
||||||
mainContentScreenModel.DatabaseClosed += MainContentScreenModelOnDatabaseClosed;
|
|
||||||
mainContentScreen = new MainContentScreen { DataContext = mainContentScreenModel };
|
|
||||||
CurrentScreen = mainContentScreen;
|
|
||||||
}
|
|
||||||
|
|
||||||
OnPropertyChanged(nameof(CurrentScreen));
|
|
||||||
OnPropertyChanged(nameof(Title));
|
|
||||||
|
|
||||||
window.Focus();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void MainContentScreenModelOnDatabaseClosed(object? sender, EventArgs e) {
|
|
||||||
welcomeScreenModel.CloseDatabase();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose() {
|
|
||||||
welcomeScreenModel.Dispose();
|
|
||||||
mainContentScreenModel?.Dispose();
|
|
||||||
db?.Dispose();
|
|
||||||
db = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,16 +0,0 @@
|
|||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using Avalonia.Markup.Xaml;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Pages {
|
|
||||||
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
|
||||||
public sealed class AdvancedPage : UserControl {
|
|
||||||
public AdvancedPage() {
|
|
||||||
InitializeComponent();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitializeComponent() {
|
|
||||||
AvaloniaXamlLoader.Load(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,39 +0,0 @@
|
|||||||
using System;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using DHT.Desktop.Dialogs.Message;
|
|
||||||
using DHT.Desktop.Main.Controls;
|
|
||||||
using DHT.Desktop.Server;
|
|
||||||
using DHT.Server.Database;
|
|
||||||
using DHT.Utils.Models;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Pages {
|
|
||||||
sealed class AdvancedPageModel : BaseModel, IDisposable {
|
|
||||||
public ServerConfigurationPanelModel ServerConfigurationModel { get; }
|
|
||||||
|
|
||||||
private readonly Window window;
|
|
||||||
private readonly IDatabaseFile db;
|
|
||||||
|
|
||||||
[Obsolete("Designer")]
|
|
||||||
public AdvancedPageModel() : this(null!, DummyDatabaseFile.Instance, new ServerManager(DummyDatabaseFile.Instance)) {}
|
|
||||||
|
|
||||||
public AdvancedPageModel(Window window, IDatabaseFile db, ServerManager serverManager) {
|
|
||||||
this.window = window;
|
|
||||||
this.db = db;
|
|
||||||
|
|
||||||
ServerConfigurationModel = new ServerConfigurationPanelModel(window, serverManager);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Initialize() {
|
|
||||||
ServerConfigurationModel.Initialize();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose() {
|
|
||||||
ServerConfigurationModel.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async void VacuumDatabase() {
|
|
||||||
db.Vacuum();
|
|
||||||
await Dialog.ShowOk(window, "Vacuum Database", "Done.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,16 +0,0 @@
|
|||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using Avalonia.Markup.Xaml;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Pages {
|
|
||||||
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
|
||||||
public sealed class DatabasePage : UserControl {
|
|
||||||
public DatabasePage() {
|
|
||||||
InitializeComponent();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitializeComponent() {
|
|
||||||
AvaloniaXamlLoader.Load(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,157 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using System.IO;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using DHT.Desktop.Common;
|
|
||||||
using DHT.Desktop.Dialogs.Message;
|
|
||||||
using DHT.Desktop.Dialogs.Progress;
|
|
||||||
using DHT.Server.Database;
|
|
||||||
using DHT.Utils.Logging;
|
|
||||||
using DHT.Utils.Models;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Pages {
|
|
||||||
sealed class DatabasePageModel : BaseModel {
|
|
||||||
private static readonly Log Log = Log.ForType<DatabasePageModel>();
|
|
||||||
|
|
||||||
public IDatabaseFile Db { get; }
|
|
||||||
|
|
||||||
public event EventHandler? DatabaseClosed;
|
|
||||||
|
|
||||||
private readonly Window window;
|
|
||||||
|
|
||||||
[Obsolete("Designer")]
|
|
||||||
public DatabasePageModel() : this(null!, DummyDatabaseFile.Instance) {}
|
|
||||||
|
|
||||||
public DatabasePageModel(Window window, IDatabaseFile db) {
|
|
||||||
this.window = window;
|
|
||||||
this.Db = db;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async void OpenDatabaseFolder() {
|
|
||||||
string file = Db.Path;
|
|
||||||
string? folder = Path.GetDirectoryName(file);
|
|
||||||
|
|
||||||
if (folder == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (Environment.OSVersion.Platform) {
|
|
||||||
case PlatformID.Win32NT:
|
|
||||||
Process.Start("explorer.exe", "/select,\"" + file + "\"");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case PlatformID.Unix:
|
|
||||||
Process.Start("xdg-open", new string[] { folder });
|
|
||||||
break;
|
|
||||||
|
|
||||||
case PlatformID.MacOSX:
|
|
||||||
Process.Start("open", new string[] { folder });
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
await Dialog.ShowOk(window, "Feature Not Supported", "This feature is not supported for your operating system.");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async void MergeWithDatabase() {
|
|
||||||
var fileDialog = DatabaseGui.NewOpenDatabaseFileDialog();
|
|
||||||
fileDialog.Directory = Path.GetDirectoryName(Db.Path);
|
|
||||||
fileDialog.AllowMultiple = true;
|
|
||||||
|
|
||||||
string[]? paths = await fileDialog.ShowAsync(window);
|
|
||||||
if (paths == null || paths.Length == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ProgressDialog progressDialog = new ProgressDialog();
|
|
||||||
progressDialog.DataContext = new ProgressDialogModel(async callback => await MergeWithDatabaseFromPaths(Db, paths, progressDialog, callback)) {
|
|
||||||
Title = "Database Merge"
|
|
||||||
};
|
|
||||||
|
|
||||||
await progressDialog.ShowDialog(window);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void CloseDatabase() {
|
|
||||||
DatabaseClosed?.Invoke(this, EventArgs.Empty);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async Task MergeWithDatabaseFromPaths(IDatabaseFile target, string[] paths, ProgressDialog dialog, IProgressCallback callback) {
|
|
||||||
int total = paths.Length;
|
|
||||||
|
|
||||||
DialogResult.YesNo? upgradeResult = null;
|
|
||||||
|
|
||||||
async Task<bool> CheckCanUpgradeDatabase() {
|
|
||||||
upgradeResult ??= total > 1
|
|
||||||
? await DatabaseGui.ShowCanUpgradeMultipleDatabaseDialog(dialog)
|
|
||||||
: await DatabaseGui.ShowCanUpgradeDatabaseDialog(dialog);
|
|
||||||
|
|
||||||
return DialogResult.YesNo.Yes == upgradeResult;
|
|
||||||
}
|
|
||||||
|
|
||||||
var oldStatistics = target.Statistics.Clone();
|
|
||||||
var oldMessageCount = target.CountMessages();
|
|
||||||
|
|
||||||
int successful = 0;
|
|
||||||
int finished = 0;
|
|
||||||
|
|
||||||
foreach (string path in paths) {
|
|
||||||
await callback.Update(Path.GetFileName(path), finished, total);
|
|
||||||
++finished;
|
|
||||||
|
|
||||||
if (!File.Exists(path)) {
|
|
||||||
await Dialog.ShowOk(dialog, "Database Error", "Database '" + Path.GetFileName(path) + "' no longer exists.");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
IDatabaseFile? db = await DatabaseGui.TryOpenOrCreateDatabaseFromPath(path, dialog, CheckCanUpgradeDatabase);
|
|
||||||
if (db == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
target.AddFrom(db);
|
|
||||||
} catch (Exception ex) {
|
|
||||||
Log.Error(ex);
|
|
||||||
await Dialog.ShowOk(dialog, "Database Error", "Database '" + Path.GetFileName(path) + "' could not be merged: " + ex.Message);
|
|
||||||
continue;
|
|
||||||
} finally {
|
|
||||||
db.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
++successful;
|
|
||||||
}
|
|
||||||
|
|
||||||
await callback.Update("Done", finished, total);
|
|
||||||
|
|
||||||
if (successful == 0) {
|
|
||||||
await Dialog.ShowOk(dialog, "Database Merge", "Nothing was merged.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var newStatistics = target.Statistics;
|
|
||||||
long newServers = newStatistics.TotalServers - oldStatistics.TotalServers;
|
|
||||||
long newChannels = newStatistics.TotalChannels - oldStatistics.TotalChannels;
|
|
||||||
long newMessages = target.CountMessages() - oldMessageCount;
|
|
||||||
|
|
||||||
StringBuilder message = new StringBuilder();
|
|
||||||
message.Append("Processed ");
|
|
||||||
|
|
||||||
if (successful == total) {
|
|
||||||
message.Append(successful.Pluralize("database file"));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
message.Append(successful.Format()).Append(" out of ").Append(total.Pluralize("database file"));
|
|
||||||
}
|
|
||||||
|
|
||||||
message.Append(" and added:\n\n \u2022 ");
|
|
||||||
message.Append(newServers.Pluralize("server")).Append("\n \u2022 ");
|
|
||||||
message.Append(newChannels.Pluralize("channel")).Append("\n \u2022 ");
|
|
||||||
message.Append(newMessages.Pluralize("message"));
|
|
||||||
|
|
||||||
await Dialog.ShowOk(dialog, "Database Merge", message.ToString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,45 +0,0 @@
|
|||||||
<UserControl xmlns="https://github.com/avaloniaui"
|
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
|
||||||
xmlns:pages="clr-namespace:DHT.Desktop.Main.Pages"
|
|
||||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
|
||||||
x:Class="DHT.Desktop.Main.Pages.DebugPage">
|
|
||||||
|
|
||||||
<Design.DataContext>
|
|
||||||
<pages:DebugPageModel />
|
|
||||||
</Design.DataContext>
|
|
||||||
|
|
||||||
<UserControl.Styles>
|
|
||||||
<Style Selector="TextBox">
|
|
||||||
<Setter Property="FontFamily" Value="Consolas,Courier" />
|
|
||||||
<Setter Property="FontSize" Value="15" />
|
|
||||||
</Style>
|
|
||||||
<Style Selector="WrapPanel > StackPanel">
|
|
||||||
<Setter Property="Orientation" Value="Vertical" />
|
|
||||||
<Setter Property="Margin" Value="0 0 10 10" />
|
|
||||||
</Style>
|
|
||||||
</UserControl.Styles>
|
|
||||||
|
|
||||||
<StackPanel Orientation="Vertical" Spacing="10">
|
|
||||||
<Expander Header="Generate Random Data" IsExpanded="True">
|
|
||||||
<WrapPanel>
|
|
||||||
<StackPanel>
|
|
||||||
<Label Target="Channels">Channels</Label>
|
|
||||||
<TextBox x:Name="Channels" Width="100" Text="{Binding GenerateChannels}" />
|
|
||||||
</StackPanel>
|
|
||||||
<StackPanel>
|
|
||||||
<Label Target="Users">Users</Label>
|
|
||||||
<TextBox x:Name="Users" Width="100" Text="{Binding GenerateUsers}" />
|
|
||||||
</StackPanel>
|
|
||||||
<StackPanel>
|
|
||||||
<Label Target="Messages">Messages</Label>
|
|
||||||
<TextBox x:Name="Messages" Width="100" Text="{Binding GenerateMessages}" />
|
|
||||||
</StackPanel>
|
|
||||||
<StackPanel VerticalAlignment="Bottom">
|
|
||||||
<Button Command="{Binding OnClickAddRandomDataToDatabase}">Add to Database</Button>
|
|
||||||
</StackPanel>
|
|
||||||
</WrapPanel>
|
|
||||||
</Expander>
|
|
||||||
</StackPanel>
|
|
||||||
</UserControl>
|
|
@@ -1,16 +0,0 @@
|
|||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using Avalonia.Markup.Xaml;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Pages {
|
|
||||||
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
|
||||||
public sealed class DebugPage : UserControl {
|
|
||||||
public DebugPage() {
|
|
||||||
InitializeComponent();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitializeComponent() {
|
|
||||||
AvaloniaXamlLoader.Load(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,185 +0,0 @@
|
|||||||
#if DEBUG
|
|
||||||
using System;
|
|
||||||
using System.Collections.Immutable;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using DHT.Desktop.Dialogs.Message;
|
|
||||||
using DHT.Desktop.Dialogs.Progress;
|
|
||||||
using DHT.Server.Data;
|
|
||||||
using DHT.Server.Database;
|
|
||||||
using DHT.Server.Service;
|
|
||||||
using DHT.Utils.Models;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Pages {
|
|
||||||
sealed class DebugPageModel : BaseModel {
|
|
||||||
public string GenerateChannels { get; set; } = "0";
|
|
||||||
public string GenerateUsers { get; set; } = "0";
|
|
||||||
public string GenerateMessages { get; set; } = "0";
|
|
||||||
|
|
||||||
private readonly Window window;
|
|
||||||
private readonly IDatabaseFile db;
|
|
||||||
|
|
||||||
[Obsolete("Designer")]
|
|
||||||
public DebugPageModel() : this(null!, DummyDatabaseFile.Instance) {}
|
|
||||||
|
|
||||||
public DebugPageModel(Window window, IDatabaseFile db) {
|
|
||||||
this.window = window;
|
|
||||||
this.db = db;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async void OnClickAddRandomDataToDatabase() {
|
|
||||||
if (!int.TryParse(GenerateChannels, out int channels) || channels < 1) {
|
|
||||||
await Dialog.ShowOk(window, "Generate Random Data", "Amount of channels must be at least 1!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!int.TryParse(GenerateUsers, out int users) || users < 1) {
|
|
||||||
await Dialog.ShowOk(window, "Generate Random Data", "Amount of users must be at least 1!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!int.TryParse(GenerateMessages, out int messages) || messages < 1) {
|
|
||||||
await Dialog.ShowOk(window, "Generate Random Data", "Amount of messages must be at least 1!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ProgressDialog progressDialog = new ProgressDialog {
|
|
||||||
DataContext = new ProgressDialogModel(async callback => await GenerateRandomData(channels, users, messages, callback)) {
|
|
||||||
Title = "Generating Random Data"
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
await progressDialog.ShowDialog(window);
|
|
||||||
}
|
|
||||||
|
|
||||||
private const int BatchSize = 500;
|
|
||||||
|
|
||||||
private async Task GenerateRandomData(int channelCount, int userCount, int messageCount, IProgressCallback callback) {
|
|
||||||
int batchCount = (messageCount + BatchSize - 1) / BatchSize;
|
|
||||||
await callback.Update("Adding messages in batches of " + BatchSize, 0, batchCount);
|
|
||||||
|
|
||||||
var rand = new Random();
|
|
||||||
var server = new DHT.Server.Data.Server {
|
|
||||||
Id = RandomId(rand),
|
|
||||||
Name = RandomName("s"),
|
|
||||||
Type = ServerType.Server
|
|
||||||
};
|
|
||||||
|
|
||||||
var channels = Enumerable.Range(0, channelCount).Select(i => new Channel {
|
|
||||||
Id = RandomId(rand),
|
|
||||||
Server = server.Id,
|
|
||||||
Name = RandomName("c"),
|
|
||||||
ParentId = null,
|
|
||||||
Position = i,
|
|
||||||
Topic = RandomText(rand, 10),
|
|
||||||
Nsfw = rand.Next(4) == 0
|
|
||||||
}).ToArray();
|
|
||||||
|
|
||||||
var users = Enumerable.Range(0, userCount).Select(_ => new User {
|
|
||||||
Id = RandomId(rand),
|
|
||||||
Name = RandomName("u"),
|
|
||||||
AvatarUrl = null,
|
|
||||||
Discriminator = rand.Next(0, 9999).ToString()
|
|
||||||
}).ToArray();
|
|
||||||
|
|
||||||
db.AddServer(server);
|
|
||||||
db.AddUsers(users);
|
|
||||||
|
|
||||||
foreach (var channel in channels) {
|
|
||||||
db.AddChannel(channel);
|
|
||||||
}
|
|
||||||
|
|
||||||
var now = DateTimeOffset.Now;
|
|
||||||
int batchIndex = 0;
|
|
||||||
|
|
||||||
while (messageCount > 0) {
|
|
||||||
int hourOffset = batchIndex;
|
|
||||||
|
|
||||||
var messages = Enumerable.Range(0, Math.Min(messageCount, BatchSize)).Select(i => {
|
|
||||||
DateTimeOffset time = now.AddHours(hourOffset).AddMinutes((i * 60.0) / BatchSize);
|
|
||||||
DateTimeOffset? edit = rand.Next(100) == 0 ? time.AddSeconds(rand.Next(1, 60)) : null;
|
|
||||||
|
|
||||||
var timeMillis = time.ToUnixTimeMilliseconds();
|
|
||||||
var editMillis = edit?.ToUnixTimeMilliseconds();
|
|
||||||
|
|
||||||
return new Message {
|
|
||||||
Id = (ulong) timeMillis,
|
|
||||||
Sender = RandomBiasedIndex(rand, users).Id,
|
|
||||||
Channel = RandomBiasedIndex(rand, channels).Id,
|
|
||||||
Text = RandomText(rand, 100),
|
|
||||||
Timestamp = timeMillis,
|
|
||||||
EditTimestamp = editMillis,
|
|
||||||
RepliedToId = null,
|
|
||||||
Attachments = ImmutableArray<Attachment>.Empty,
|
|
||||||
Embeds = ImmutableArray<Embed>.Empty,
|
|
||||||
Reactions = ImmutableArray<Reaction>.Empty
|
|
||||||
};
|
|
||||||
}).ToArray();
|
|
||||||
|
|
||||||
db.AddMessages(messages);
|
|
||||||
|
|
||||||
messageCount -= BatchSize;
|
|
||||||
await callback.Update("Adding messages in batches of " + BatchSize, ++batchIndex, batchCount);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static ulong RandomId(Random rand) {
|
|
||||||
ulong h = unchecked((ulong) rand.Next());
|
|
||||||
ulong l = unchecked((ulong) rand.Next());
|
|
||||||
return (h << 32) | l;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string RandomName(string prefix) {
|
|
||||||
return prefix + "-" + ServerUtils.GenerateRandomToken(5);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static T RandomBiasedIndex<T>(Random rand, T[] options) {
|
|
||||||
return options[(int) Math.Floor(options.Length * rand.NextDouble() * rand.NextDouble())];
|
|
||||||
}
|
|
||||||
|
|
||||||
private static readonly string[] RandomWords = {
|
|
||||||
"apple", "apricot", "artichoke", "arugula", "asparagus", "avocado",
|
|
||||||
"banana", "bean", "beechnut", "beet", "blackberry", "blackcurrant", "blueberry", "boysenberry", "bramble", "broccoli",
|
|
||||||
"cabbage", "cacao", "cantaloupe", "caper", "carambola", "carrot", "cauliflower", "celery", "chard", "cherry", "chokeberry", "citron", "clementine", "coconut", "corn", "crabapple", "cranberry", "cucumber", "currant",
|
|
||||||
"daikon", "date", "dewberry", "durian",
|
|
||||||
"edamame", "eggplant", "elderberry", "endive",
|
|
||||||
"fig",
|
|
||||||
"garlic", "ginger", "gooseberry", "grape", "grapefruit", "guava",
|
|
||||||
"honeysuckle", "horseradish", "huckleberry",
|
|
||||||
"jackfruit", "jicama",
|
|
||||||
"kale", "kiwi", "kohlrabi", "kumquat",
|
|
||||||
"leek", "lemon", "lentil", "lettuce", "lime",
|
|
||||||
"mandarin", "mango", "mushroom", "myrtle",
|
|
||||||
"nectarine", "nut",
|
|
||||||
"olive", "okra", "onion", "orange",
|
|
||||||
"papaya", "parsnip", "pawpaw", "peach", "pear", "pea", "pepper", "persimmon", "pineapple", "plum", "plantain", "pomegranate", "pomelo", "potato", "prune", "pumpkin",
|
|
||||||
"quandong", "quinoa",
|
|
||||||
"radicchio", "radish", "raisin", "raspberry", "redcurrant", "rhubarb", "rutabaga",
|
|
||||||
"spinach", "strawberry", "squash",
|
|
||||||
"tamarind", "tangerine", "tomatillo", "tomato", "turnip",
|
|
||||||
"vanilla",
|
|
||||||
"watercress", "watermelon",
|
|
||||||
"yam",
|
|
||||||
"zucchini"
|
|
||||||
};
|
|
||||||
|
|
||||||
private static string RandomText(Random rand, int maxWords) {
|
|
||||||
int wordCount = 1 + (int) Math.Floor(maxWords * Math.Pow(rand.NextDouble(), 3));
|
|
||||||
return string.Join(' ', Enumerable.Range(0, wordCount).Select(_ => RandomWords[rand.Next(RandomWords.Length)]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
using DHT.Utils.Models;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Pages {
|
|
||||||
sealed class DebugPageModel : BaseModel {
|
|
||||||
public string GenerateChannels { get; set; } = "0";
|
|
||||||
public string GenerateUsers { get; set; } = "0";
|
|
||||||
public string GenerateMessages { get; set; } = "0";
|
|
||||||
|
|
||||||
public void OnClickAddRandomDataToDatabase() {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
@@ -1,38 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using Avalonia.Interactivity;
|
|
||||||
using Avalonia.Markup.Xaml;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Pages {
|
|
||||||
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
|
||||||
public sealed class TrackingPage : UserControl {
|
|
||||||
private bool isCopyingScript;
|
|
||||||
|
|
||||||
public TrackingPage() {
|
|
||||||
InitializeComponent();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitializeComponent() {
|
|
||||||
AvaloniaXamlLoader.Load(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async void CopyTrackingScriptButton_OnClick(object? sender, RoutedEventArgs e) {
|
|
||||||
if (DataContext is TrackingPageModel model) {
|
|
||||||
var button = this.FindControl<Button>("CopyTrackingScript");
|
|
||||||
var originalText = button.Content;
|
|
||||||
button.MinWidth = button.Bounds.Width;
|
|
||||||
|
|
||||||
if (await model.OnClickCopyTrackingScript() && !isCopyingScript) {
|
|
||||||
isCopyingScript = true;
|
|
||||||
button.Content = "Script Copied!";
|
|
||||||
|
|
||||||
await Task.Delay(TimeSpan.FromSeconds(2));
|
|
||||||
button.Content = originalText;
|
|
||||||
isCopyingScript = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,117 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.Web;
|
|
||||||
using Avalonia;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using DHT.Desktop.Dialogs.Message;
|
|
||||||
using DHT.Desktop.Discord;
|
|
||||||
using DHT.Desktop.Server;
|
|
||||||
using DHT.Utils.Models;
|
|
||||||
using static DHT.Desktop.Program;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Pages {
|
|
||||||
sealed class TrackingPageModel : BaseModel {
|
|
||||||
private bool areDevToolsEnabled;
|
|
||||||
|
|
||||||
private bool AreDevToolsEnabled {
|
|
||||||
get => areDevToolsEnabled;
|
|
||||||
set {
|
|
||||||
Change(ref areDevToolsEnabled, value);
|
|
||||||
OnPropertyChanged(nameof(ToggleAppDevToolsButtonText));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool IsToggleAppDevToolsButtonEnabled { get; private set; } = true;
|
|
||||||
|
|
||||||
public string ToggleAppDevToolsButtonText {
|
|
||||||
get {
|
|
||||||
if (!IsToggleAppDevToolsButtonEnabled) {
|
|
||||||
return "Unavailable";
|
|
||||||
}
|
|
||||||
|
|
||||||
return AreDevToolsEnabled ? "Disable Ctrl+Shift+I" : "Enable Ctrl+Shift+I";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private readonly Window window;
|
|
||||||
|
|
||||||
[Obsolete("Designer")]
|
|
||||||
public TrackingPageModel() : this(null!) {}
|
|
||||||
|
|
||||||
public TrackingPageModel(Window window) {
|
|
||||||
this.window = window;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task Initialize() {
|
|
||||||
bool? devToolsEnabled = await DiscordAppSettings.AreDevToolsEnabled();
|
|
||||||
if (devToolsEnabled.HasValue) {
|
|
||||||
AreDevToolsEnabled = devToolsEnabled.Value;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
IsToggleAppDevToolsButtonEnabled = false;
|
|
||||||
OnPropertyChanged(nameof(IsToggleAppDevToolsButtonEnabled));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<bool> OnClickCopyTrackingScript() {
|
|
||||||
string bootstrap = await Resources.ReadTextAsync("Tracker/bootstrap.js");
|
|
||||||
string script = bootstrap.Replace("= 0; /*[PORT]*/", "= " + ServerManager.Port + ";")
|
|
||||||
.Replace("/*[TOKEN]*/", HttpUtility.JavaScriptStringEncode(ServerManager.Token))
|
|
||||||
.Replace("/*[IMPORTS]*/", await Resources.ReadJoinedAsync("Tracker/scripts/", '\n'))
|
|
||||||
.Replace("/*[CSS-CONTROLLER]*/", await Resources.ReadTextAsync("Tracker/styles/controller.css"))
|
|
||||||
.Replace("/*[CSS-SETTINGS]*/", await Resources.ReadTextAsync("Tracker/styles/settings.css"));
|
|
||||||
|
|
||||||
var clipboard = Application.Current?.Clipboard;
|
|
||||||
if (clipboard == null) {
|
|
||||||
await Dialog.ShowOk(window, "Copy Tracking Script", "Clipboard is not available on this system.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
await clipboard.SetTextAsync(script);
|
|
||||||
return true;
|
|
||||||
} catch {
|
|
||||||
await Dialog.ShowOk(window, "Copy Tracking Script", "An error occurred while copying to clipboard.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async void OnClickToggleAppDevTools() {
|
|
||||||
const string DialogTitle = "Discord App Settings File";
|
|
||||||
|
|
||||||
bool oldState = AreDevToolsEnabled;
|
|
||||||
bool newState = !oldState;
|
|
||||||
|
|
||||||
switch (await DiscordAppSettings.ConfigureDevTools(newState)) {
|
|
||||||
case SettingsJsonResult.Success:
|
|
||||||
AreDevToolsEnabled = newState;
|
|
||||||
await Dialog.ShowOk(window, DialogTitle, "Ctrl+Shift+I was " + (newState ? "enabled." : "disabled.") + " Restart the Discord app for the change to take effect.");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SettingsJsonResult.AlreadySet:
|
|
||||||
await Dialog.ShowOk(window, DialogTitle, "Ctrl+Shift+I is already " + (newState ? "enabled." : "disabled."));
|
|
||||||
AreDevToolsEnabled = newState;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SettingsJsonResult.FileNotFound:
|
|
||||||
await Dialog.ShowOk(window, DialogTitle, "Cannot find the settings file:\n" + DiscordAppSettings.JsonFilePath);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SettingsJsonResult.ReadError:
|
|
||||||
await Dialog.ShowOk(window, DialogTitle, "Cannot read the settings file:\n" + DiscordAppSettings.JsonFilePath);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SettingsJsonResult.InvalidJson:
|
|
||||||
await Dialog.ShowOk(window, DialogTitle, "Unknown format of the settings file:\n" + DiscordAppSettings.JsonFilePath);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SettingsJsonResult.WriteError:
|
|
||||||
await Dialog.ShowOk(window, DialogTitle, "Cannot save the settings file:\n" + DiscordAppSettings.JsonFilePath);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
throw new ArgumentOutOfRangeException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,40 +0,0 @@
|
|||||||
<UserControl xmlns="https://github.com/avaloniaui"
|
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
|
||||||
xmlns:pages="clr-namespace:DHT.Desktop.Main.Pages"
|
|
||||||
xmlns:controls="clr-namespace:DHT.Desktop.Main.Controls"
|
|
||||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
|
||||||
x:Class="DHT.Desktop.Main.Pages.ViewerPage"
|
|
||||||
AttachedToVisualTree="OnAttachedToVisualTree"
|
|
||||||
DetachedFromVisualTree="OnDetachedFromVisualTree">
|
|
||||||
|
|
||||||
<Design.DataContext>
|
|
||||||
<pages:ViewerPageModel />
|
|
||||||
</Design.DataContext>
|
|
||||||
|
|
||||||
<UserControl.Styles>
|
|
||||||
<Style Selector="Expander">
|
|
||||||
<Setter Property="Margin" Value="0 25 0 0" />
|
|
||||||
</Style>
|
|
||||||
</UserControl.Styles>
|
|
||||||
|
|
||||||
<StackPanel Orientation="Vertical">
|
|
||||||
<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
|
|
||||||
<Button Command="{Binding OnClickOpenViewer}" Margin="0 0 5 0">Open Viewer</Button>
|
|
||||||
<Button Command="{Binding OnClickSaveViewer}" Margin="5 0 0 0">Save Viewer</Button>
|
|
||||||
</StackPanel>
|
|
||||||
<TextBlock Text="{Binding ExportedMessageText}" Margin="0 20 0 0" />
|
|
||||||
<controls:FilterPanel DataContext="{Binding FilterModel}" />
|
|
||||||
<Expander Header="Database Tools">
|
|
||||||
<StackPanel Orientation="Vertical" Spacing="10">
|
|
||||||
<StackPanel Orientation="Vertical" Spacing="4">
|
|
||||||
<RadioButton GroupName="DatabaseToolFilterMode" IsEnabled="{Binding HasFilters}" IsChecked="{Binding DatabaseToolFilterModeKeep}">Keep Only Messages Matching Filters</RadioButton>
|
|
||||||
<RadioButton GroupName="DatabaseToolFilterMode" IsEnabled="{Binding HasFilters}" IsChecked="{Binding DatabaseToolFilterModeRemove}">Remove Messages Matching Filters</RadioButton>
|
|
||||||
</StackPanel>
|
|
||||||
<Button IsEnabled="{Binding HasFilters}" Command="{Binding OnClickApplyFiltersToDatabase}">Apply Filters to Database</Button>
|
|
||||||
</StackPanel>
|
|
||||||
</Expander>
|
|
||||||
</StackPanel>
|
|
||||||
|
|
||||||
</UserControl>
|
|
@@ -1,29 +0,0 @@
|
|||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
using Avalonia;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using Avalonia.Markup.Xaml;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Pages {
|
|
||||||
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
|
||||||
public sealed class ViewerPage : UserControl {
|
|
||||||
public ViewerPage() {
|
|
||||||
InitializeComponent();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitializeComponent() {
|
|
||||||
AvaloniaXamlLoader.Load(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e) {
|
|
||||||
if (DataContext is ViewerPageModel model) {
|
|
||||||
model.SetPageVisible(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnDetachedFromVisualTree(object? sender, VisualTreeAttachmentEventArgs e) {
|
|
||||||
if (DataContext is ViewerPageModel model) {
|
|
||||||
model.SetPageVisible(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,144 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.ComponentModel;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using System.IO;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.Web;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using DHT.Desktop.Common;
|
|
||||||
using DHT.Desktop.Dialogs.Message;
|
|
||||||
using DHT.Desktop.Main.Controls;
|
|
||||||
using DHT.Server.Data.Filters;
|
|
||||||
using DHT.Server.Database;
|
|
||||||
using DHT.Server.Database.Export;
|
|
||||||
using DHT.Utils.Models;
|
|
||||||
using static DHT.Desktop.Program;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Pages {
|
|
||||||
sealed class ViewerPageModel : BaseModel, IDisposable {
|
|
||||||
public string ExportedMessageText { get; private set; } = "";
|
|
||||||
|
|
||||||
public bool DatabaseToolFilterModeKeep { get; set; } = true;
|
|
||||||
public bool DatabaseToolFilterModeRemove { get; set; } = false;
|
|
||||||
|
|
||||||
private bool hasFilters = false;
|
|
||||||
|
|
||||||
public bool HasFilters {
|
|
||||||
get => hasFilters;
|
|
||||||
set => Change(ref hasFilters, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
private FilterPanelModel FilterModel { get; }
|
|
||||||
|
|
||||||
private readonly Window window;
|
|
||||||
private readonly IDatabaseFile db;
|
|
||||||
|
|
||||||
private bool isPageVisible = false;
|
|
||||||
|
|
||||||
[Obsolete("Designer")]
|
|
||||||
public ViewerPageModel() : this(null!, DummyDatabaseFile.Instance) {}
|
|
||||||
|
|
||||||
public ViewerPageModel(Window window, IDatabaseFile db) {
|
|
||||||
this.window = window;
|
|
||||||
this.db = db;
|
|
||||||
|
|
||||||
FilterModel = new FilterPanelModel(window, db);
|
|
||||||
FilterModel.FilterPropertyChanged += OnFilterPropertyChanged;
|
|
||||||
db.Statistics.PropertyChanged += OnDbStatisticsChanged;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose() {
|
|
||||||
db.Statistics.PropertyChanged -= OnDbStatisticsChanged;
|
|
||||||
FilterModel.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SetPageVisible(bool isPageVisible) {
|
|
||||||
this.isPageVisible = isPageVisible;
|
|
||||||
if (isPageVisible) {
|
|
||||||
UpdateStatistics();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnFilterPropertyChanged(object? sender, PropertyChangedEventArgs e) {
|
|
||||||
UpdateStatistics();
|
|
||||||
HasFilters = FilterModel.HasAnyFilters;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnDbStatisticsChanged(object? sender, PropertyChangedEventArgs e) {
|
|
||||||
if (isPageVisible && e.PropertyName == nameof(DatabaseStatistics.TotalMessages)) {
|
|
||||||
UpdateStatistics();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void UpdateStatistics() {
|
|
||||||
var filter = FilterModel.CreateFilter();
|
|
||||||
var allMessagesCount = db.Statistics.TotalMessages?.Format() ?? "?";
|
|
||||||
var filteredMessagesCount = filter.IsEmpty ? allMessagesCount : db.CountMessages(filter).Format();
|
|
||||||
|
|
||||||
ExportedMessageText = "Will export " + filteredMessagesCount + " out of " + allMessagesCount + " message(s).";
|
|
||||||
OnPropertyChanged(nameof(ExportedMessageText));
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task<string> GenerateViewerContents() {
|
|
||||||
string json = ViewerJsonExport.Generate(db, FilterModel.CreateFilter());
|
|
||||||
|
|
||||||
string index = await Resources.ReadTextAsync("Viewer/index.html");
|
|
||||||
string viewer = index.Replace("/*[JS]*/", await Resources.ReadJoinedAsync("Viewer/scripts/", '\n'))
|
|
||||||
.Replace("/*[CSS]*/", await Resources.ReadJoinedAsync("Viewer/styles/", '\n'))
|
|
||||||
.Replace("/*[ARCHIVE]*/", HttpUtility.JavaScriptStringEncode(json));
|
|
||||||
return viewer;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async void OnClickOpenViewer() {
|
|
||||||
string rootPath = Path.Combine(Path.GetTempPath(), "DiscordHistoryTracker");
|
|
||||||
string filenameBase = Path.GetFileNameWithoutExtension(db.Path) + "-" + DateTime.Now.ToString("yyyy-MM-dd");
|
|
||||||
string fullPath = Path.Combine(rootPath, filenameBase + ".html");
|
|
||||||
int counter = 0;
|
|
||||||
|
|
||||||
while (File.Exists(fullPath)) {
|
|
||||||
++counter;
|
|
||||||
fullPath = Path.Combine(rootPath, filenameBase + "-" + counter + ".html");
|
|
||||||
}
|
|
||||||
|
|
||||||
Directory.CreateDirectory(rootPath);
|
|
||||||
await File.WriteAllTextAsync(fullPath, await GenerateViewerContents());
|
|
||||||
|
|
||||||
Process.Start(new ProcessStartInfo(fullPath) { UseShellExecute = true });
|
|
||||||
}
|
|
||||||
|
|
||||||
public async void OnClickSaveViewer() {
|
|
||||||
var dialog = new SaveFileDialog {
|
|
||||||
Title = "Save Viewer",
|
|
||||||
InitialFileName = "archive.html",
|
|
||||||
Directory = Path.GetDirectoryName(db.Path),
|
|
||||||
Filters = new List<FileDialogFilter> {
|
|
||||||
new() {
|
|
||||||
Name = "Discord History Viewer",
|
|
||||||
Extensions = { "html" }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.ShowAsync(window);
|
|
||||||
|
|
||||||
string? path = await dialog;
|
|
||||||
if (!string.IsNullOrEmpty(path)) {
|
|
||||||
await File.WriteAllTextAsync(path, await GenerateViewerContents());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async void OnClickApplyFiltersToDatabase() {
|
|
||||||
var filter = FilterModel.CreateFilter();
|
|
||||||
|
|
||||||
if (DatabaseToolFilterModeKeep) {
|
|
||||||
if (DialogResult.YesNo.Yes == await Dialog.ShowYesNo(window, "Keep Matching Messages in This Database", db.CountMessages(filter).Pluralize("message") + " will be kept, and the rest will be removed from this database. This action cannot be undone. Proceed?")) {
|
|
||||||
db.RemoveMessages(filter, MessageFilterRemovalMode.KeepMatching);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (DatabaseToolFilterModeRemove) {
|
|
||||||
if (DialogResult.YesNo.Yes == await Dialog.ShowYesNo(window, "Remove Matching Messages in This Database", db.CountMessages(filter).Pluralize("message") + " will be removed from this database. This action cannot be undone. Proceed?")) {
|
|
||||||
db.RemoveMessages(filter, MessageFilterRemovalMode.RemoveMatching);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,16 +0,0 @@
|
|||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using Avalonia.Markup.Xaml;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Screens {
|
|
||||||
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
|
||||||
public sealed class MainContentScreen : UserControl {
|
|
||||||
public MainContentScreen() {
|
|
||||||
InitializeComponent();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitializeComponent() {
|
|
||||||
AvaloniaXamlLoader.Load(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,112 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using DHT.Desktop.Dialogs.Message;
|
|
||||||
using DHT.Desktop.Main.Controls;
|
|
||||||
using DHT.Desktop.Main.Pages;
|
|
||||||
using DHT.Desktop.Server;
|
|
||||||
using DHT.Server.Database;
|
|
||||||
using DHT.Server.Service;
|
|
||||||
using DHT.Utils.Logging;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Screens {
|
|
||||||
sealed class MainContentScreenModel : IDisposable {
|
|
||||||
private static readonly Log Log = Log.ForType<MainContentScreenModel>();
|
|
||||||
|
|
||||||
public DatabasePage DatabasePage { get; }
|
|
||||||
private DatabasePageModel DatabasePageModel { get; }
|
|
||||||
|
|
||||||
public TrackingPage TrackingPage { get; }
|
|
||||||
private TrackingPageModel TrackingPageModel { get; }
|
|
||||||
|
|
||||||
public ViewerPage ViewerPage { get; }
|
|
||||||
private ViewerPageModel ViewerPageModel { get; }
|
|
||||||
|
|
||||||
public AdvancedPage AdvancedPage { get; }
|
|
||||||
private AdvancedPageModel AdvancedPageModel { get; }
|
|
||||||
|
|
||||||
public DebugPage? DebugPage { get; }
|
|
||||||
|
|
||||||
#if DEBUG
|
|
||||||
public bool HasDebugPage => true;
|
|
||||||
private DebugPageModel DebugPageModel { get; }
|
|
||||||
#else
|
|
||||||
public bool HasDebugPage => false;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
public StatusBarModel StatusBarModel { get; }
|
|
||||||
|
|
||||||
public event EventHandler? DatabaseClosed {
|
|
||||||
add {
|
|
||||||
DatabasePageModel.DatabaseClosed += value;
|
|
||||||
}
|
|
||||||
remove {
|
|
||||||
DatabasePageModel.DatabaseClosed -= value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private readonly Window window;
|
|
||||||
private readonly ServerManager serverManager;
|
|
||||||
|
|
||||||
[Obsolete("Designer")]
|
|
||||||
public MainContentScreenModel() : this(null!, DummyDatabaseFile.Instance) {}
|
|
||||||
|
|
||||||
public MainContentScreenModel(Window window, IDatabaseFile db) {
|
|
||||||
this.window = window;
|
|
||||||
this.serverManager = new ServerManager(db);
|
|
||||||
|
|
||||||
ServerLauncher.ServerManagementExceptionCaught += ServerLauncherOnServerManagementExceptionCaught;
|
|
||||||
|
|
||||||
DatabasePageModel = new DatabasePageModel(window, db);
|
|
||||||
DatabasePage = new DatabasePage { DataContext = DatabasePageModel };
|
|
||||||
|
|
||||||
TrackingPageModel = new TrackingPageModel(window);
|
|
||||||
TrackingPage = new TrackingPage { DataContext = TrackingPageModel };
|
|
||||||
|
|
||||||
ViewerPageModel = new ViewerPageModel(window, db);
|
|
||||||
ViewerPage = new ViewerPage { DataContext = ViewerPageModel };
|
|
||||||
|
|
||||||
AdvancedPageModel = new AdvancedPageModel(window, db, serverManager);
|
|
||||||
AdvancedPage = new AdvancedPage { DataContext = AdvancedPageModel };
|
|
||||||
|
|
||||||
#if DEBUG
|
|
||||||
DebugPageModel = new DebugPageModel(window, db);
|
|
||||||
DebugPage = new DebugPage { DataContext = DebugPageModel };
|
|
||||||
#else
|
|
||||||
DebugPage = null;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
StatusBarModel = new StatusBarModel(db.Statistics);
|
|
||||||
|
|
||||||
AdvancedPageModel.ServerConfigurationModel.ServerStatusChanged += OnServerStatusChanged;
|
|
||||||
DatabaseClosed += OnDatabaseClosed;
|
|
||||||
|
|
||||||
StatusBarModel.CurrentStatus = serverManager.IsRunning ? StatusBarModel.Status.Ready : StatusBarModel.Status.Stopped;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task Initialize() {
|
|
||||||
await TrackingPageModel.Initialize();
|
|
||||||
AdvancedPageModel.Initialize();
|
|
||||||
serverManager.Launch();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose() {
|
|
||||||
ServerLauncher.ServerManagementExceptionCaught -= ServerLauncherOnServerManagementExceptionCaught;
|
|
||||||
ViewerPageModel.Dispose();
|
|
||||||
serverManager.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnServerStatusChanged(object? sender, StatusBarModel.Status e) {
|
|
||||||
StatusBarModel.CurrentStatus = e;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnDatabaseClosed(object? sender, EventArgs e) {
|
|
||||||
serverManager.Stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
private async void ServerLauncherOnServerManagementExceptionCaught(object? sender, Exception ex) {
|
|
||||||
Log.Error(ex);
|
|
||||||
await Dialog.ShowOk(window, "Internal Server Error", ex.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,16 +0,0 @@
|
|||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using Avalonia.Markup.Xaml;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Screens {
|
|
||||||
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
|
||||||
public sealed class WelcomeScreen : UserControl {
|
|
||||||
public WelcomeScreen() {
|
|
||||||
InitializeComponent();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitializeComponent() {
|
|
||||||
AvaloniaXamlLoader.Load(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,73 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Avalonia.Controls;
|
|
||||||
using DHT.Desktop.Common;
|
|
||||||
using DHT.Desktop.Dialogs.Message;
|
|
||||||
using DHT.Server.Database;
|
|
||||||
using DHT.Utils.Models;
|
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Screens {
|
|
||||||
sealed class WelcomeScreenModel : BaseModel, IDisposable {
|
|
||||||
public string Version => Program.Version;
|
|
||||||
|
|
||||||
public IDatabaseFile? Db { get; private set; }
|
|
||||||
public bool HasDatabase => Db != null;
|
|
||||||
|
|
||||||
private readonly Window window;
|
|
||||||
|
|
||||||
private string? dbFilePath;
|
|
||||||
|
|
||||||
[Obsolete("Designer")]
|
|
||||||
public WelcomeScreenModel() : this(null!) {}
|
|
||||||
|
|
||||||
public WelcomeScreenModel(Window window) {
|
|
||||||
this.window = window;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async void OpenOrCreateDatabase() {
|
|
||||||
var dialog = DatabaseGui.NewOpenOrCreateDatabaseFileDialog();
|
|
||||||
dialog.Directory = Path.GetDirectoryName(dbFilePath);
|
|
||||||
|
|
||||||
string? path = await dialog.ShowAsync(window);
|
|
||||||
if (!string.IsNullOrWhiteSpace(path)) {
|
|
||||||
await OpenOrCreateDatabaseFromPath(path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task OpenOrCreateDatabaseFromPath(string path) {
|
|
||||||
if (Db != null) {
|
|
||||||
Db = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
dbFilePath = path;
|
|
||||||
Db = await DatabaseGui.TryOpenOrCreateDatabaseFromPath(path, window, CheckCanUpgradeDatabase);
|
|
||||||
|
|
||||||
OnPropertyChanged(nameof(Db));
|
|
||||||
OnPropertyChanged(nameof(HasDatabase));
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task<bool> CheckCanUpgradeDatabase() {
|
|
||||||
return DialogResult.YesNo.Yes == await DatabaseGui.ShowCanUpgradeDatabaseDialog(window);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void CloseDatabase() {
|
|
||||||
Dispose();
|
|
||||||
OnPropertyChanged(nameof(Db));
|
|
||||||
OnPropertyChanged(nameof(HasDatabase));
|
|
||||||
}
|
|
||||||
|
|
||||||
public async void ShowAboutDialog() {
|
|
||||||
await new AboutWindow { DataContext = new AboutWindowModel() }.ShowDialog(this.window);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Exit() {
|
|
||||||
window.Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose() {
|
|
||||||
Db?.Dispose();
|
|
||||||
Db = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,39 +1,40 @@
|
|||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Avalonia;
|
using Avalonia;
|
||||||
using DHT.Utils.Resources;
|
using DHT.Desktop.Resources;
|
||||||
|
|
||||||
namespace DHT.Desktop {
|
namespace DHT.Desktop;
|
||||||
static class Program {
|
|
||||||
public static string Version { get; }
|
|
||||||
public static CultureInfo Culture { get; }
|
|
||||||
public static ResourceLoader Resources { get; }
|
|
||||||
|
|
||||||
static Program() {
|
static class Program {
|
||||||
var assembly = Assembly.GetExecutingAssembly();
|
public static string Version { get; }
|
||||||
|
public static CultureInfo Culture { get; }
|
||||||
Version = assembly.GetName().Version?.ToString() ?? "";
|
public static ResourceLoader Resources { get; }
|
||||||
while (Version.EndsWith(".0")) {
|
|
||||||
Version = Version[..^2];
|
|
||||||
}
|
|
||||||
|
|
||||||
Culture = CultureInfo.CurrentCulture;
|
static Program() {
|
||||||
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
|
var assembly = Assembly.GetExecutingAssembly();
|
||||||
CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
|
|
||||||
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
|
Version = assembly.GetName().Version?.ToString() ?? "";
|
||||||
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;
|
while (Version.EndsWith(".0")) {
|
||||||
|
Version = Version[..^2];
|
||||||
Resources = new ResourceLoader(assembly);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Main(string[] args) {
|
Culture = CultureInfo.CurrentCulture;
|
||||||
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
|
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
|
||||||
}
|
CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
|
||||||
|
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
|
||||||
|
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;
|
||||||
|
|
||||||
private static AppBuilder BuildAvaloniaApp() {
|
Resources = new ResourceLoader(assembly);
|
||||||
return AppBuilder.Configure<App>()
|
}
|
||||||
.UsePlatformDetect()
|
|
||||||
.LogToTrace();
|
public static void Main(string[] args) {
|
||||||
}
|
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static AppBuilder BuildAvaloniaApp() {
|
||||||
|
return AppBuilder.Configure<App.App>()
|
||||||
|
.UsePlatformDetect()
|
||||||
|
.WithInterFont()
|
||||||
|
.LogToTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
48
app/Desktop/Resources/ResourceLoader.cs
Normal file
48
app/Desktop/Resources/ResourceLoader.cs
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.Resources;
|
||||||
|
|
||||||
|
public sealed class ResourceLoader {
|
||||||
|
private readonly Assembly assembly;
|
||||||
|
|
||||||
|
public ResourceLoader(Assembly assembly) {
|
||||||
|
this.assembly = assembly;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Stream GetEmbeddedStream(string filename) {
|
||||||
|
Stream? stream = null;
|
||||||
|
foreach (var embeddedName in assembly.GetManifestResourceNames()) {
|
||||||
|
if (embeddedName.Replace('\\', '/') == filename) {
|
||||||
|
stream = assembly.GetManifestResourceStream(embeddedName);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return stream ?? throw new ArgumentException("Missing embedded resource: " + filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<string> ReadTextAsync(Stream stream) {
|
||||||
|
using var reader = new StreamReader(stream, Encoding.UTF8);
|
||||||
|
return await reader.ReadToEndAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> ReadTextAsync(string filename) {
|
||||||
|
return await ReadTextAsync(GetEmbeddedStream(filename));
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> ReadJoinedAsync(string path, char separator) {
|
||||||
|
StringBuilder joined = new();
|
||||||
|
|
||||||
|
foreach (var embeddedName in assembly.GetManifestResourceNames()) {
|
||||||
|
if (embeddedName.Replace('\\', '/').StartsWith(path)) {
|
||||||
|
joined.Append(await ReadTextAsync(assembly.GetManifestResourceStream(embeddedName)!)).Append(separator);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return joined.ToString(0, Math.Max(0, joined.Length - 1));
|
||||||
|
}
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user