Skip to content

Commit

Permalink
#137 if no write permissions, use ApplicationData folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben2776 committed Feb 12, 2024
1 parent 8b6ef6e commit 7765b4c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 14 deletions.
40 changes: 30 additions & 10 deletions src/PicView.Core/Config/SettingsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,19 @@ public static async Task LoadSettingsAsync()
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/UserSettings.json");
if (File.Exists(path))
{
await PerformRead(path).ConfigureAwait(false);
}
else
{
// TODO test saving location for macOS https://johnkoerner.com/csharp/special-folder-values-on-windows-versus-mac/
var appData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Ruben2776/PicView/Config/UserSettings.json");
if (File.Exists(appData))
try
{
await PerformRead(appData).ConfigureAwait(false);
await PerformRead(path).ConfigureAwait(false);
}
else
catch (Exception)
{
SetDefaults();
await Retry().ConfigureAwait(false);
}
}
else
{
await Retry().ConfigureAwait(false);
}
}
catch (Exception ex)
{
Expand All @@ -47,6 +45,28 @@ public static async Task LoadSettingsAsync()
#endif
SetDefaults();
}
return;

async Task Retry()
{
// TODO test saving location for macOS https://johnkoerner.com/csharp/special-folder-values-on-windows-versus-mac/
var appData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Ruben2776/PicView/Config/UserSettings.json");
if (File.Exists(appData))
{
try
{
await PerformRead(appData).ConfigureAwait(false);
}
catch (Exception)
{
SetDefaults();
}
}
else
{
SetDefaults();
}
}
}

private static void SetDefaults()
Expand Down
9 changes: 9 additions & 0 deletions src/PicView.Core/Navigation/FileHistory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Runtime;

namespace PicView.Core.Navigation;

Expand Down Expand Up @@ -28,6 +29,14 @@ public FileHistory()
}
catch (Exception e)
{
try
{
_fileLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Ruben2776/PicView/Config/recent.txt");
}
catch (Exception)
{
Trace.WriteLine($"{nameof(FileHistory)} exception, \n{e.Message}");
}
#if DEBUG
Trace.WriteLine($"{nameof(FileHistory)} exception, \n{e.Message}");
#endif
Expand Down
16 changes: 14 additions & 2 deletions src/PicView.WPF/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,20 @@ public partial class App
{
protected override void OnStartup(StartupEventArgs e)
{
ProfileOptimization.SetProfileRoot(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/"));
ProfileOptimization.StartProfile("ProfileOptimization");
Task.Run((() =>
{
try
{
ProfileOptimization.SetProfileRoot(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/"));
ProfileOptimization.StartProfile("ProfileOptimization");
}
catch (Exception)
{
ProfileOptimization.SetProfileRoot(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Ruben2776/PicView/Config/"));
ProfileOptimization.StartProfile("ProfileOptimization");
}
}));

DispatcherUnhandledException += App_DispatcherUnhandledException;
}

Expand Down
5 changes: 4 additions & 1 deletion src/PicView.WPF/ChangeImage/FileHistoryNavigation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ internal static async Task OpenLastFileAsync()
{
if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/recent.txt")) == false)
{
return;
if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Ruben2776/PicView/Config/recent.txt")) == false)
{
return;
}
}

await ConfigureWindows.GetMainWindow.Dispatcher.InvokeAsync(() =>
Expand Down
1 change: 0 additions & 1 deletion src/PicView.WPF/ChangeImage/FileUpdateNavigation.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using PicView.Core.Config;
using PicView.Core.FileHandling;
using PicView.Core.Gallery;
using PicView.Core.Navigation;
using PicView.WPF.FileHandling;
using PicView.WPF.ImageHandling;
using PicView.WPF.PicGallery;
Expand Down

0 comments on commit 7765b4c

Please sign in to comment.