Skip to content

Commit

Permalink
Merge pull request #113 from mortenn/feature/92
Browse files Browse the repository at this point in the history
Implement feature to backup and restore user configuration
  • Loading branch information
mortenn authored Apr 7, 2024
2 parents 53a23c6 + 8fb6c5a commit 6c00a43
Show file tree
Hide file tree
Showing 15 changed files with 399 additions and 129 deletions.
4 changes: 2 additions & 2 deletions src/BrowserPicker.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public App()
try
{
ViewModel = new ApplicationViewModel(arguments, Settings);
if (ViewModel.Url != null)
if (ViewModel.Url.TargetURL != null)
{
BackgroundTasks.Add(ViewModel.Url);
}
Expand Down Expand Up @@ -64,7 +64,7 @@ protected override async void OnStartup(StartupEventArgs e)
long_running_processes = RunLongRunningProcesses();

// Open in configuration mode if user started BrowserPicker directly
if (string.IsNullOrWhiteSpace(ViewModel.Url?.TargetURL))
if (string.IsNullOrWhiteSpace(ViewModel.Url.TargetURL))
{
ShowMainWindow();
return;
Expand Down
2 changes: 1 addition & 1 deletion src/BrowserPicker.App/View/BrowserEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private void Icon_Browse(object sender, RoutedEventArgs e)
Filter = "Executable Files (*.exe)|*.exe|Icon Files (*.ico)|*.ico|JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif|All Files|*.*"
};
if (browser.ShowDialog(this) == true)
Browser!.Model.IconPath = browser.FileName;
Browser.Model.IconPath = browser.FileName;
}


Expand Down
13 changes: 7 additions & 6 deletions src/BrowserPicker.App/View/BrowserList.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ private void ButtonBase_OnClick(object sender, RoutedEventArgs e)

private void Editor_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Enter || e.Key == System.Windows.Input.Key.Return)
if (e.Key is not (System.Windows.Input.Key.Enter or System.Windows.Input.Key.Return))
{
if (DataContext is ApplicationViewModel app)
{
app.EndEdit.Execute(null);
}
e.Handled = true;
return;
}

if (DataContext is ApplicationViewModel app)
{
app.EndEdit.Execute(null);
}
e.Handled = true;
}
}
33 changes: 28 additions & 5 deletions src/BrowserPicker.App/View/Configuration.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
</Grid.RowDefinitions>

<TabControl Background="#77000000" BorderBrush="#77FFFFFF" VerticalAlignment="Stretch">

<TabControl.Resources>
<Style TargetType="{x:Type TabItem}">
<Setter Property="Template">
Expand All @@ -77,7 +77,7 @@
</Setter>
</Style>
</TabControl.Resources>

<TabItem Header="Browsers">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ItemsControl ItemsSource="{Binding ParentViewModel.Choices}">
Expand Down Expand Up @@ -142,16 +142,19 @@
</ItemsControl>
</ScrollViewer>
</TabItem>

<TabItem Header="Behaviour">
<StackPanel Orientation="Vertical">
<StackPanel.Resources>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="Margin" Value="5,0,0,5"/>
</Style>
</StackPanel.Resources>

<CheckBox Content="Turn off transparency" IsChecked="{Binding Settings.DisableTransparency}" Foreground="White" />

<CheckBox Content="Always show browser selection window" IsChecked="{Binding Settings.AlwaysPrompt}" Foreground="White" />

<StackPanel Orientation="Horizontal">
<CheckBox Content="When no default is configured matching the url, use:" IsChecked="{Binding Settings.UseFallbackDefault}" Foreground="White" />
<ComboBox
Expand All @@ -162,12 +165,15 @@
SelectedValue="{Binding Settings.DefaultBrowser}"
Height="22" Margin="5,-4,0,0" />
</StackPanel>

<CheckBox Content="Always ask when no default is matching url" IsChecked="{Binding Settings.AlwaysAskWithoutDefault}" Foreground="White" />
<CheckBox Content="Disable url resolution" IsChecked="{Binding Settings.DisableNetworkAccess}" Foreground="White" />

<CheckBox Content="Ignore defaults when browser is not running" IsChecked="{Binding Settings.AlwaysUseDefaults}" Foreground="White" Checked="CheckBox_Checked" />

<CheckBox Content="Update order in browser list based on usage" IsChecked="{Binding Settings.UseAutomaticOrdering}" Foreground="White" />

<CheckBox Content="Disallow network activity" Foreground="White" IsChecked="{Binding Settings.DisableNetworkAccess}" />
<CheckBox Content="Disallow network activity" Foreground="White" IsChecked="{Binding Settings.DisableNetworkAccess}" ToolTip="Disabled url resolution feature" />

<StackPanel Margin="25,0,0,0">
<StackPanel.Style>
<Style>
Expand Down Expand Up @@ -295,6 +301,23 @@
</Grid>
</TabItem>

<TabItem Header="Backup">
<Grid Margin="10" VerticalAlignment="Top">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Text="Back up or restore your BrowserPicker configuration:" Foreground="White" Margin="5" />
<Button Content="Export BrowserPicker settings" Grid.Row="1" Grid.Column="0" Margin="5" Padding="5" Command="{Binding Backup}" />
<Button Content="Import BrowserPicker settings" Grid.Row="2" Grid.Column="0" Margin="5" Padding="5" Command="{Binding Restore}" />
<ScrollViewer Grid.Row="3" Grid.Column="0" VerticalScrollBarVisibility="Auto" Margin="5">
<TextBlock Text="{Binding Settings.BackupLog}" Foreground="White" TextWrapping="Wrap" />
</ScrollViewer>
</Grid>
</TabItem>

</TabControl>

<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom">
Expand Down
3 changes: 1 addition & 2 deletions src/BrowserPicker.App/View/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using JetBrains.Annotations;
using BrowserPicker.ViewModel;
using System.Linq;
using System.Windows.Controls;

namespace BrowserPicker.View;

Expand Down Expand Up @@ -33,7 +32,7 @@ private void MainWindow_OnKeyUp(object sender, KeyEventArgs e)
if (e.Key == Key.Escape)
Close();

if (ViewModel.Url == null)
if (ViewModel.Url.TargetURL == null)
return;

int n;
Expand Down
25 changes: 9 additions & 16 deletions src/BrowserPicker.App/ViewModel/ApplicationViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public ApplicationViewModel()
{
Url = new UrlHandler();
force_choice = true;
Configuration = new ConfigurationViewModel(App.Settings);
Configuration = new ConfigurationViewModel(App.Settings, this);
Choices = new ObservableCollection<BrowserViewModel>(
WellKnownBrowsers.List.Select(b => new BrowserViewModel(new BrowserModel(b, null, string.Empty), this))
);
}

internal ApplicationViewModel(ConfigurationViewModel config)
{
Url = new UrlHandler(config.Settings, "https://github.com/mortenn/BrowserPicker");
Url = new UrlHandler("https://github.com/mortenn/BrowserPicker", config.Settings.DisableNetworkAccess);
force_choice = true;
Configuration = config;
Choices = new ObservableCollection<BrowserViewModel>(
Expand All @@ -44,24 +44,21 @@ public ApplicationViewModel(IReadOnlyCollection<string> arguments, IBrowserPicke
var options = arguments.Where(arg => arg[0] == '/').ToList();
force_choice = options.Contains("/choose");
var url = arguments.Except(options).FirstOrDefault();
if (url != null)
{
Url = new UrlHandler(settings, url);
}
Url = new UrlHandler(url, url == null || settings.DisableNetworkAccess);
ConfigurationMode = url == null;
Configuration = new ConfigurationViewModel(settings)
Configuration = new ConfigurationViewModel(settings, this)
{
ParentViewModel = this
};
Choices = new ObservableCollection<BrowserViewModel>(settings.BrowserList.Select(m => new BrowserViewModel(m, this)));
}

public UrlHandler? Url { get; }
public UrlHandler Url { get; }

public void Initialize()
{
if (
Url == null
Url.TargetURL == null
|| Keyboard.Modifiers == ModifierKeys.Alt
|| Configuration.AlwaysPrompt
|| ConfigurationMode
Expand Down Expand Up @@ -153,10 +150,6 @@ public string? EditURL
get => edit_url;
set
{
if (Url == null)
{
return;
}
SetProperty(ref edit_url, value);
Url.UnderlyingTargetURL = value!;
}
Expand Down Expand Up @@ -196,7 +189,7 @@ private void PerformCopyUrl()
{
try
{
if (Url?.UnderlyingTargetURL == null)
if (Url.UnderlyingTargetURL == null)
{
return;
}
Expand All @@ -214,13 +207,13 @@ private void PerformCopyUrl()

private void OpenURLEditor()
{
EditURL = Url?.UnderlyingTargetURL;
EditURL = Url.UnderlyingTargetURL;
OnPropertyChanged(nameof(EditURL));
}

private void CloseURLEditor()
{
if (Url == null || edit_url == null)
if (edit_url == null)
{
return;
}
Expand Down
6 changes: 3 additions & 3 deletions src/BrowserPicker.App/ViewModel/BrowserViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ public bool IsRunning

private bool CanLaunch(bool privacy)
{
return !string.IsNullOrWhiteSpace(parent_view_model.Url?.TargetURL) && !(privacy && Model.PrivacyArgs == null);
return !string.IsNullOrWhiteSpace(parent_view_model.Url.TargetURL) && !(privacy && Model.PrivacyArgs == null);
}

private void Launch(bool privacy)
{
if (parent_view_model.Url == null)
if (parent_view_model.Url.TargetURL == null)
{
return;
}
Expand All @@ -148,7 +148,7 @@ private void Launch(bool privacy)
Model.Usage++;
}

if (parent_view_model.Configuration.AutoAddDefault)
if (parent_view_model.Configuration.AutoAddDefault && parent_view_model.Url.HostName != null)
{
try
{
Expand Down
Loading

0 comments on commit 6c00a43

Please sign in to comment.