Skip to content

Commit

Permalink
Settings implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Neurofibromin committed Jun 12, 2024
1 parent a03dde4 commit bf0cdf6
Show file tree
Hide file tree
Showing 24 changed files with 535 additions and 180 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/prerelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
os: [ ubuntu-latest ]
dotnet-version: [ '8.0.x' ]
runs-on: ${{ matrix.os }}
if: false
if: true
steps:
- uses: actions/checkout@v3
- name: Setup .NET Core SDK ${{ matrix.dotnet-version }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ releases.txt
*.AppImage
*.deb
*.rpm
todo.txt

################################################################################
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
Expand Down
2 changes: 2 additions & 0 deletions App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
RequestedThemeVariant="Default">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->

<!-- TODO: aero-theme and theme switcher? -->

<Application.DataTemplates>
<local:ViewLocator />
</Application.DataTemplates>
Expand Down
34 changes: 34 additions & 0 deletions App.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Data.Core;
using System.Runtime.InteropServices;
using Avalonia.Data.Core.Plugins;
using Avalonia.Markup.Xaml;
using bookmark_dlp.Models;
Expand Down Expand Up @@ -38,6 +39,38 @@ public override async void OnFrameworkInitializationCompleted()
MessageBus.ButtonClicked += async (sender, buttonText) =>
{
await Console.Out.WriteLineAsync(buttonText);
switch (buttonText)
{
case "Appdata/local":
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
string configpath_osx = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Personal), "bookmark-dlp/bookmark-dlp.conf");
AppSettings.configloc = configpath_osx;
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
string configpath_windows = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "bookmark-dlp\\bookmark-dlp.conf");
AppSettings.configloc = configpath_windows;
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
string configpath_linux = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "bookmark-dlp/bookmark-dlp.conf");
AppSettings.configloc = configpath_linux;
}
break;
case "No config":
AppSettings.configloc = null;
break;
case "local dir":
AppSettings.configloc = Path.Combine(Directory.GetCurrentDirectory(), "bookmark-dlp.conf");
break;
default:
AppSettings.configloc = null;
throw new Exception("Should not have happened");
Environment.Exit(1);
}
var mainWindowVM = new MainWindowViewModel();
var MainWindow = new MainWindow
{
Expand All @@ -52,6 +85,7 @@ public override async void OnFrameworkInitializationCompleted()
else
{
Console.WriteLine("Config was found");
Console.WriteLine("Location: " + Methods.ConfigFileLocation());
var mainWindowVM = new MainWindowViewModel();
var MainWindow = new MainWindow
{
Expand Down
3 changes: 3 additions & 0 deletions CoreLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
using System.ComponentModel;
using Microsoft.Data.Sqlite;

//TODO: Migrate from Newtonsoft.Json to System.Text.Json


namespace bookmark_dlp
{
public class CoreLogic
Expand Down
64 changes: 50 additions & 14 deletions Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public static string Yt_dlp_pathfinder(string rootdir)
string ytdlp_path = ""; //checks is yt-dlp binary is present in root or if it is on path, returns ytdlp_path so it can be written into the script files
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
if (File.Exists(Path.Combine(rootdir, "yt-dlp.exe")))
if (rootdir != null && File.Exists(Path.Combine(rootdir, "yt-dlp.exe")))
{
//Console.WriteLine(Path.Combine(rootdir, "yt-dlp.exe") + " found");
ytdlp_path = Path.Combine(rootdir, "yt-dlp.exe");
Expand Down Expand Up @@ -330,9 +330,9 @@ public static string Yt_dlp_pathfinder(string rootdir)
string[] filenames = {"yt-dlp", "yt-dlp_linux", "yt-dlp_linux_aarch64", "yt-dlp_linux_armv7l" };
foreach (string filename in filenames)
{
if (File.Exists(Path.Combine(rootdir, filename)))
if (rootdir != null && File.Exists(Path.Combine(rootdir, filename)))
{
Console.WriteLine(Path.Combine(rootdir, filename) + " found");
// Console.WriteLine(Path.Combine(rootdir, filename) + " found");
ytdlp_path = Path.Combine(rootdir, filename);
break;
}
Expand Down Expand Up @@ -379,9 +379,9 @@ public static string Yt_dlp_pathfinder(string rootdir)
string[] filenames = { "yt-dlp", "yt-dlp_macos", "yt-dlp_macos_legacy" };
foreach (string filename in filenames)
{
if (File.Exists(Path.Combine(rootdir, filename)))
if (rootdir != null && File.Exists(Path.Combine(rootdir, filename)))
{
Console.WriteLine(Path.Combine(rootdir, filename) + " found");
// Console.WriteLine(Path.Combine(rootdir, filename) + " found");
ytdlp_path = Path.Combine(rootdir, filename);
}
}
Expand Down Expand Up @@ -613,19 +613,55 @@ public static bool Wantcomplex()
return wantcomplex;
}

public static bool IsConfigPresent()
public static string? ConfigFileLocation()
{
string configpath_local = Path.Combine(Directory.GetCurrentDirectory(), "bookmark-dlp.conf");
string configpath_windows = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "bookmark-dlp\\bookmark-dlp.conf");
string configpath_linux = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "bookmark-dlp/bookmark-dlp.conf");
string configpath_osx = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Personal), "bookmark-dlp/bookmark-dlp.conf");

if (File.Exists(configpath_local)){ return true; }
if (File.Exists(configpath_windows)) { return true; }
if (File.Exists(configpath_linux)) { return true; }
if (File.Exists(configpath_osx)) { return true; }
return false;
if (File.Exists(configpath_local))
{
return configpath_local;
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
string configpath_osx = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Personal), "bookmark-dlp/bookmark-dlp.conf");
if (File.Exists(configpath_osx)) { return configpath_osx; }
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
string configpath_windows = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "bookmark-dlp\\bookmark-dlp.conf");
if (File.Exists(configpath_windows)) { return configpath_windows; }
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
string configpath_linux = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "bookmark-dlp/bookmark-dlp.conf");
if (File.Exists(configpath_linux)) { return configpath_linux; }
}

return null; //no config paths were found
}

public static bool IsConfigPresent()
{
/*
string configpath_local = Path.Combine(Directory.GetCurrentDirectory(), "bookmark-dlp.conf");
string configpath_windows = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "bookmark-dlp\\bookmark-dlp.conf");
string configpath_linux = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "bookmark-dlp/bookmark-dlp.conf");
string configpath_osx = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Personal), "bookmark-dlp/bookmark-dlp.conf");
if (File.Exists(configpath_local)){ return true; }
if (File.Exists(configpath_windows)) { return true; }
if (File.Exists(configpath_linux)) { return true; }
if (File.Exists(configpath_osx)) { return true; }
*/
if (ConfigFileLocation() == null)
{
return false;
}
else
{
return true;
}
}

}
187 changes: 187 additions & 0 deletions Models/AppSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.JavaScript;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace bookmark_dlp.Models
{
public sealed class AppSettings
{
/// <summary>
/// Singleton!
/// </summary>
public static SettingsStruct _settings = new SettingsStruct();
public static readonly SettingsStruct defaultsettings = new SettingsStruct
{
htmlImportUsed = false,
htmlfilelocation = "",
outputfolder = Directory.GetCurrentDirectory(),
ytdlp_executable_not_found = true,
downloadPlaylists = false,
downloadShorts = false,
downloadChannels = false,
concurrent_downloads = false,
cookies_autoextract = false,
yt_dlp_binary_path = null,
};
public static string? configloc = Methods.ConfigFileLocation();
private static AppSettings _instance = new AppSettings();


/*
public static AppSettings Instance
{
get
{
if (_instance == null)
_instance = new AppSettings();
return _instance;
}
}*/

//Constructor
protected AppSettings()

Check warning on line 47 in Models/AppSettings.cs

View workflow job for this annotation

GitHub Actions / pupnet (ubuntu-latest, 8.0.x)

'AppSettings.AppSettings()': new protected member declared in sealed type

Check warning on line 47 in Models/AppSettings.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0.x)

'AppSettings.AppSettings()': new protected member declared in sealed type

Check warning on line 47 in Models/AppSettings.cs

View workflow job for this annotation

GitHub Actions / release (ubuntu-latest, 8.0.x)

'AppSettings.AppSettings()': new protected member declared in sealed type
{
configloc = Methods.ConfigFileLocation();
if (File.Exists(configloc))
{
string jsonimportstring = File.ReadAllText(configloc);
// Console.WriteLine("importing: " + jsonimportstring);
// Console.WriteLine("\n");
try
{
SettingsStruct imported = JsonConvert.DeserializeObject<SettingsStruct>(jsonimportstring);
if (imported == null) { throw new NullReferenceException(); }
_settings = imported;
Console.WriteLine("Config import successful");
// Console.WriteLine("afterimport: " + JsonConvert.SerializeObject(_settings));
}
catch
{
Console.WriteLine("Settings could not be deserialized, fallback to default settings. Not overwriting corrupt file!");
_settings = defaultsettings;
configloc = null; //to protect file from overwrite
}

}
else
{
Console.WriteLine("Config file doesnt exist, going with defaults");
_settings = defaultsettings; //no config file, so set configs to default value
configloc = null;
// Console.WriteLine(_settings.ytdlp_executable_not_found); // + " " + _settings.Ytdlp_executable_not_found);
}
// Console.WriteLine(_settings.downloadShorts);
}

public static AppSettings GetAppSettings()
{
return _instance;
}



public static void SaveToFile()
{
//if configloc already exists overwrite it, if not create it
string jsonstringexport = JsonConvert.SerializeObject(_settings);
if (configloc != null) //only save to file if a config file is present or was chosen
{
File.Delete(configloc);
StreamWriter write = new StreamWriter(configloc);
// Console.WriteLine("exporting: " + jsonstringexport);
// Console.WriteLine("\n");
write.Write(jsonstringexport);
write.Close();
}
}

//Finalizer
~AppSettings()
{
if(configloc != null){ SaveToFile();} //only save to file if a config file is present or was chosen

}

public static string GetJsonStringRepresentation()
{
string a = JsonConvert.SerializeObject(_settings);
return a;
}

}

public partial class SettingsStruct : ObservableObject
{
public SettingsStruct(string chtmlfilelocation, bool chtmlImportUsed, string coutputfolder, bool cytdlp_executable_not_found,
bool cdownloadPlaylists, bool cdownloadShorts, bool cdownloadChannels, bool cconcurrent_downloads, bool ccookies_autoextract, string cyt_dlp_binary_path)
{
htmlfilelocation = chtmlfilelocation;
htmlImportUsed = chtmlImportUsed;
outputfolder = coutputfolder;
ytdlp_executable_not_found = cytdlp_executable_not_found;
downloadPlaylists = cdownloadPlaylists;
downloadShorts = cdownloadShorts;
downloadChannels = cdownloadChannels;
concurrent_downloads = cconcurrent_downloads;
cookies_autoextract = ccookies_autoextract;
yt_dlp_binary_path = cyt_dlp_binary_path;
}

public SettingsStruct(SettingsStruct other)
{
htmlfilelocation = other.htmlfilelocation;
htmlImportUsed = other.htmlImportUsed;
outputfolder = other.outputfolder;
ytdlp_executable_not_found = other.ytdlp_executable_not_found;
downloadPlaylists = other.downloadPlaylists;
downloadShorts = other.downloadShorts;
downloadChannels = other.downloadChannels;
concurrent_downloads = other.concurrent_downloads;
cookies_autoextract = other.cookies_autoextract;
yt_dlp_binary_path = other.yt_dlp_binary_path;
}

public SettingsStruct()
{
htmlfilelocation = null;
htmlImportUsed = false;
outputfolder = null;
ytdlp_executable_not_found = true;
downloadPlaylists = false;
downloadShorts = false;
downloadChannels = false;
concurrent_downloads = false;
cookies_autoextract = false;
yt_dlp_binary_path = null;
}


[ObservableProperty]
public string htmlfilelocation;
[ObservableProperty]
public bool htmlImportUsed;
[ObservableProperty]
public string outputfolder;


[ObservableProperty]
public bool ytdlp_executable_not_found;
[ObservableProperty]
public bool downloadPlaylists;
[ObservableProperty]
public bool downloadShorts;
[ObservableProperty]
public bool downloadChannels;
[ObservableProperty]
public bool concurrent_downloads;
[ObservableProperty]
public bool cookies_autoextract;
[ObservableProperty]
public string? yt_dlp_binary_path;
}
}
20 changes: 0 additions & 20 deletions Models/Config.cs

This file was deleted.

Loading

0 comments on commit bf0cdf6

Please sign in to comment.