<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.IO;
using Microsoft.Win32;
using System.Diagnostics;
using BrendanGrant.Helpers.FileAssociation;
using System.Reflection;

namespace Patchy
{
    /// <summary>
    /// Interaction logic for PreferencesWindow.xaml
    /// </summary>
    public partial class PreferencesWindow : Window
    {
        public PreferencesWindow()
        {
            InitializeComponent();
            InitializeRegistryBoundItems();
        }

        private void InitializeRegistryBoundItems()
        {
            if (!UacHelper.IsProcessElevated)
            {
                registryBoundPreferences.IsEnabled = false;
                elevatePermissionsPanel.Visibility = Visibility.Visible;
                return;
            }
            var torrent = new FileAssociationInfo(".torrent");
            if (torrent.Exists)
                torrentAssociationCheckBox.IsChecked = torrent.ProgID == "Patchy";
            else
                torrentAssociationCheckBox.IsChecked = false;
            // Check magnet link association
            var value = Registry.GetValue(@"HKEY_CLASSES_ROOT\\Magnet", null, null);
            if (value == null)
                magnetAssociationCheckBox.IsChecked = false;
            else
            {
                var shell = (string)Registry.GetValue(@"HKEY_CLASSES_ROOT\Magnet\shell\open\command", null, null);
                magnetAssociationCheckBox.IsChecked = shell == string.Format("\"{0}\" \"%1\"", Assembly.GetEntryAssembly().Location);
            }
            var key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            var startup = key.GetValue("Patchy", null) as string;
            startOnWindowsStartupCheckBox.IsChecked = startup != null;
            if (startup == null)
                startMinimizedCheckBox.IsChecked = false;
            else
                startMinimizedCheckBox.IsChecked = startup.EndsWith("--minimized");
        }

        private void clearTorrentCacheClick(object sender, RoutedEventArgs e)
        {
            App.ClearCacheOnExit = true;
            MessageBox.Show("Cache will be deleted on exit.");
        }

        private void torrentAssociationCheckBoxChecked(object sender, RoutedEventArgs e)
        {
            var torrent = new FileAssociationInfo(".torrent");
            if (torrentAssociationCheckBox.IsChecked.Value)
            {
                torrent.Create("Patchy");
                torrent.ContentType = "application/x-bittorrent";
                torrent.OpenWithList = new[] { "patchy.exe" };
                var program = new ProgramAssociationInfo(torrent.ProgID);
                if (!program.Exists)
                {
                    program.Create("Patchy Torrent Files", new ProgramVerb("Open", string.Format(
                        "\"{0}\" \"%1\"", Path.Combine(Directory.GetCurrentDirectory(), "Patchy.exe"))));
                    program.DefaultIcon = new ProgramIcon(Assembly.GetEntryAssembly().Location);
                }
            }
            else
                torrent.Delete();
        }

        private void magnetAssociationCheckBoxChecked(object sender, RoutedEventArgs e)
        {
            if (magnetAssociationCheckBox.IsChecked.Value)
            {
                var path = string.Format("\"{0}\" \"%1\"", Assembly.GetEntryAssembly().Location);
                var key = Registry.ClassesRoot.CreateSubKey("Magnet");
                key.SetValue(null, "Magnet URI");
                key.SetValue("Content Type", "application/x-magnet");
                key.SetValue("URL Protocol", string.Empty);
                var defaultIcon = key.CreateSubKey("DefaultIcon");
                defaultIcon.SetValue(null, Assembly.GetEntryAssembly().Location);
                var shell = key.CreateSubKey("shell");
                shell.SetValue(null, "open");
                var open = shell.CreateSubKey("open");
                var command = open.CreateSubKey("command");
                command.SetValue(null, path);
                command.Close(); open.Close(); shell.Close(); defaultIcon.Close(); key.Close();
            }
            else
            {
                Registry.ClassesRoot.DeleteSubKeyTree("Magnet");
                Registry.ClassesRoot.Flush();
            }
        }

        private void startOnWindowsStartupChecked(object sender, RoutedEventArgs e)
        {
            var key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            if (startOnWindowsStartupCheckBox.IsChecked.Value)
                key.SetValue("Patchy", Assembly.GetEntryAssembly().Location);
            else
                key.DeleteValue("Patchy");
            key.Close();
        }

        private void startupMinimizedChecked(object sender, RoutedEventArgs e)
        {
            var key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            if (startMinimizedCheckBox.IsChecked.Value)
                key.SetValue("Patchy", Assembly.GetEntryAssembly().Location + "--minimized");
            else
                key.SetValue("Patchy", Assembly.GetEntryAssembly().Location);
            key.Close();
        }
    }
}
<commit_msg>Fixed bug with 'minimize on startup' in start with windows login settings<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.IO;
using Microsoft.Win32;
using System.Diagnostics;
using BrendanGrant.Helpers.FileAssociation;
using System.Reflection;

namespace Patchy
{
    /// <summary>
    /// Interaction logic for PreferencesWindow.xaml
    /// </summary>
    public partial class PreferencesWindow : Window
    {
        public PreferencesWindow()
        {
            InitializeComponent();
            InitializeRegistryBoundItems();
        }

        private void InitializeRegistryBoundItems()
        {
            if (!UacHelper.IsProcessElevated)
            {
                registryBoundPreferences.IsEnabled = false;
                elevatePermissionsPanel.Visibility = Visibility.Visible;
                return;
            }
            var torrent = new FileAssociationInfo(".torrent");
            if (torrent.Exists)
                torrentAssociationCheckBox.IsChecked = torrent.ProgID == "Patchy";
            else
                torrentAssociationCheckBox.IsChecked = false;
            // Check magnet link association
            var value = Registry.GetValue(@"HKEY_CLASSES_ROOT\\Magnet", null, null);
            if (value == null)
                magnetAssociationCheckBox.IsChecked = false;
            else
            {
                var shell = (string)Registry.GetValue(@"HKEY_CLASSES_ROOT\Magnet\shell\open\command", null, null);
                magnetAssociationCheckBox.IsChecked = shell == string.Format("\"{0}\" \"%1\"", Assembly.GetEntryAssembly().Location);
            }
            var key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            var startup = key.GetValue("Patchy", null) as string;
            startOnWindowsStartupCheckBox.IsChecked = startup != null;
            if (startup == null)
                startMinimizedCheckBox.IsChecked = false;
            else
                startMinimizedCheckBox.IsChecked = startup.EndsWith("--minimized");
        }

        private void clearTorrentCacheClick(object sender, RoutedEventArgs e)
        {
            App.ClearCacheOnExit = true;
            MessageBox.Show("Cache will be deleted on exit.");
        }

        private void torrentAssociationCheckBoxChecked(object sender, RoutedEventArgs e)
        {
            var torrent = new FileAssociationInfo(".torrent");
            if (torrentAssociationCheckBox.IsChecked.Value)
            {
                torrent.Create("Patchy");
                torrent.ContentType = "application/x-bittorrent";
                torrent.OpenWithList = new[] { "patchy.exe" };
                var program = new ProgramAssociationInfo(torrent.ProgID);
                if (!program.Exists)
                {
                    program.Create("Patchy Torrent Files", new ProgramVerb("Open", string.Format(
                        "\"{0}\" \"%1\"", Path.Combine(Directory.GetCurrentDirectory(), "Patchy.exe"))));
                    program.DefaultIcon = new ProgramIcon(Assembly.GetEntryAssembly().Location);
                }
            }
            else
                torrent.Delete();
        }

        private void magnetAssociationCheckBoxChecked(object sender, RoutedEventArgs e)
        {
            if (magnetAssociationCheckBox.IsChecked.Value)
            {
                var path = string.Format("\"{0}\" \"%1\"", Assembly.GetEntryAssembly().Location);
                var key = Registry.ClassesRoot.CreateSubKey("Magnet");
                key.SetValue(null, "Magnet URI");
                key.SetValue("Content Type", "application/x-magnet");
                key.SetValue("URL Protocol", string.Empty);
                var defaultIcon = key.CreateSubKey("DefaultIcon");
                defaultIcon.SetValue(null, Assembly.GetEntryAssembly().Location);
                var shell = key.CreateSubKey("shell");
                shell.SetValue(null, "open");
                var open = shell.CreateSubKey("open");
                var command = open.CreateSubKey("command");
                command.SetValue(null, path);
                command.Close(); open.Close(); shell.Close(); defaultIcon.Close(); key.Close();
            }
            else
            {
                Registry.ClassesRoot.DeleteSubKeyTree("Magnet");
                Registry.ClassesRoot.Flush();
            }
        }

        private void startOnWindowsStartupChecked(object sender, RoutedEventArgs e)
        {
            var key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            if (startOnWindowsStartupCheckBox.IsChecked.Value)
            {
                if (startMinimizedCheckBox.IsChecked.Value)
                    key.SetValue("Patchy", "\"" + Assembly.GetEntryAssembly().Location + "\" --minimized");
                else
                    key.SetValue("Patchy", "\"" + Assembly.GetEntryAssembly().Location + "\"");
            }
            else
                key.DeleteValue("Patchy");
            key.Close();
        }

        private void startupMinimizedChecked(object sender, RoutedEventArgs e)
        {
            var key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            if (startMinimizedCheckBox.IsChecked.Value)
                key.SetValue("Patchy", "\"" + Assembly.GetEntryAssembly().Location + "\" --minimized");
            else
                key.SetValue("Patchy", "\"" + Assembly.GetEntryAssembly().Location + "\"");
            key.Close();
        }
    }
}

{'fraction_non_alphanumeric': 0.10991511332808261, 'fraction_numerical': 0.00140019252647239, 'mean_word_length': 1.9947589098532494, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4869613', 'n_tokens_mistral': 2949, 'n_tokens_neox': 2788, 'n_words': 531}