Sfoglia il codice sorgente

Better protect against access issues

Daniel Chalmers 1 anno fa
parent
commit
7a92876620

+ 5 - 3
DesktopClock/MainWindow.xaml

@@ -96,6 +96,8 @@
 						<Style.Triggers>
 							<DataTrigger Binding="{Binding}" Value="{x:Static local:DateFormatExample.Tutorial}">
 								<Setter Property="Command" Value="{Binding DataContext.FormatWizardCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
+
+								<Setter Property="IsEnabled" Value="{x:Static p:Settings.CanBeSaved}" />
 							</DataTrigger>
 						</Style.Triggers>
 
@@ -110,11 +112,11 @@
 
 			<Separator />
 
-			<MenuItem Command="{Binding NewClockCommand}" Header="_New clock..." />
+			<MenuItem Command="{Binding NewClockCommand}" Header="_New clock..." IsEnabled="{x:Static p:Settings.CanBeSaved}" />
 
-			<MenuItem Command="{Binding CountdownWizardCommand}" Header="_Countdown to..." />
+			<MenuItem Command="{Binding CountdownWizardCommand}" Header="_Countdown to..." IsEnabled="{x:Static p:Settings.CanBeSaved}" />
 
-			<MenuItem Command="{Binding OpenSettingsCommand}" Header="Advanced _settings" />
+			<MenuItem Command="{Binding OpenSettingsCommand}" Header="Advanced _settings" IsEnabled="{x:Static p:Settings.CanBeSaved}" />
 
 			<MenuItem Command="{Binding CheckForUpdatesCommand}" Header="Check for _updates" />
 

+ 12 - 4
DesktopClock/MainWindow.xaml.cs

@@ -164,10 +164,15 @@ public partial class MainWindow : Window
     [RelayCommand]
     public void OpenSettings()
     {
-        // Save first so it's up-to-date.
-        Settings.Default.Save();
+        // Save first if we can so it's up-to-date.
+        if (Settings.CanBeSaved)
+            Settings.Default.Save();
+
+        // If it doesn't even exist then it's probably somewhere that requires special access and we shouldn't even be at this point.
+        if (!Settings.Exists)
+            return;
 
-        // Teach user.
+        // Teach user how it works.
         if (!Settings.Default.TipsShown.HasFlag(TeachingTips.AdvancedSettings))
         {
             MessageBox.Show(this,
@@ -340,7 +345,10 @@ public partial class MainWindow : Window
         if (!Settings.CanBeSaved)
         {
             MessageBox.Show(this,
-                $"Settings won't be saved because of an access error. Make sure {Title} is in a folder that can be written to without administrator privileges!",
+                "Settings can't be saved because of an access error.\n\n" +
+                $"Make sure {Title} is in a folder that doesn't require admin privileges, " +
+                "and that you got it from the original source: https://github.com/danielchalmers/DesktopClock.\n\n" +
+                "If the problem still persists, feel free to create a new Issue at the above link with as many details as possible.",
                 Title, MessageBoxButton.OK, MessageBoxImage.Warning);
         }
     }

+ 11 - 0
DesktopClock/Properties/Settings.cs

@@ -42,10 +42,21 @@ public sealed class Settings : INotifyPropertyChanged, IDisposable
 
     public static Settings Default => _default.Value;
 
+    /// <summary>
+    /// The full path to the settings file.
+    /// </summary>
     public static string FilePath { get; private set; }
 
+    /// <summary>
+    /// Can the settings file be saved to?
+    /// </summary>
     public static bool CanBeSaved { get; private set; }
 
+    /// <summary>
+    /// Does the settings file exist on the disk?
+    /// </summary>
+    public static bool Exists => File.Exists(FilePath);
+
     #region "Properties"
 
     public string Format { get; set; } = "{ddd}, {MMM dd}, {h:mm:ss tt}";