<commit_before>﻿using SyncTrayzor.NotifyIcon;
using SyncTrayzor.Properties;
using SyncTrayzor.Services.UpdateChecker;
using SyncTrayzor.SyncThing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SyncTrayzor.Services
{
    public class ConfigurationApplicator
    {
        private readonly IConfigurationProvider configurationProvider;

        private readonly INotifyIconManager notifyIconManager;
        private readonly ISyncThingManager syncThingManager;
        private readonly AutostartProvider autostartProvider;
        private readonly IWatchedFolderMonitor watchedFolderMonitor;
        private readonly IGithubApiClient githubApiClient;
        private readonly IUpdateChecker updateChecker;

        public ConfigurationApplicator(
            IConfigurationProvider configurationProvider,
            INotifyIconManager notifyIconManager,
            ISyncThingManager syncThingManager,
            AutostartProvider autostartProvider,
            IWatchedFolderMonitor watchedFolderMonitor,
            IGithubApiClient githubApiClient,
            IUpdateChecker updateChecker)
        {
            this.configurationProvider = configurationProvider;
            this.configurationProvider.ConfigurationChanged += (o, e) => this.ApplyNewConfiguration(e.NewConfiguration);

            this.notifyIconManager = notifyIconManager;
            this.syncThingManager = syncThingManager;
            this.autostartProvider = autostartProvider;
            this.watchedFolderMonitor = watchedFolderMonitor;
            this.githubApiClient = githubApiClient;
            this.updateChecker = updateChecker;

            // Do this before signing up for DataLoaded, so any changes we make don't trigger us again
            this.UpdateConfigOnInit();

            this.syncThingManager.DataLoaded += (o, e) => this.LoadFolders();
            this.updateChecker.VersionIgnored += (o, e) =>
            {
                var config = this.configurationProvider.Load();
                config.LatestNotifiedVersion = e.IgnoredVersion;
                this.configurationProvider.Save(config);
            };
        }

        private void UpdateConfigOnInit()
        {
            // If the user's manually updated the registry themselves, update our config to match
            var config = this.configurationProvider.Load();
            var autostartConfig = this.autostartProvider.GetCurrentSetup();
            // We only know enough to change StartMinimized if autostartConfig.AutoStart is strue
            if (config.StartOnLogon != autostartConfig.AutoStart || (autostartConfig.AutoStart && config.StartMinimized != autostartConfig.StartMinimized))
            {
                if (autostartConfig.AutoStart)
                    config.StartMinimized = autostartConfig.StartMinimized;
                config.StartOnLogon = autostartConfig.AutoStart;
                this.configurationProvider.Save(config);
            }
        }

        public void ApplyConfiguration()
        {
            this.githubApiClient.SetConnectionDetails(Settings.Default.GithubApiUrl);
            this.ApplyNewConfiguration(this.configurationProvider.Load());
        }

        private void ApplyNewConfiguration(Configuration configuration)
        {
            this.notifyIconManager.CloseToTray = configuration.CloseToTray;
            this.notifyIconManager.ShowOnlyOnClose = configuration.ShowTrayIconOnlyOnClose;
            this.notifyIconManager.ShowSynchronizedBalloon = configuration.ShowSynchronizedBalloon;

            this.syncThingManager.Address = new Uri(configuration.SyncthingAddress);
            this.syncThingManager.ExecutablePath = configuration.SyncthingPath;
            this.syncThingManager.ApiKey = configuration.SyncthingApiKey;

            this.autostartProvider.SetAutoStart(new AutostartConfiguration() { AutoStart = configuration.StartOnLogon, StartMinimized = configuration.StartMinimized });

            this.watchedFolderMonitor.WatchedFolderIDs = configuration.Folders.Where(x => x.IsWatched).Select(x => x.ID);

            this.updateChecker.LatestIgnoredVersion = configuration.LatestNotifiedVersion;
        }

        private void LoadFolders()
        {
            var configuration = this.configurationProvider.Load();

            foreach (var newKey in this.syncThingManager.Folders.Keys.Except(configuration.Folders.Select(x => x.ID)))
            {
                configuration.Folders.Add(new FolderConfiguration(newKey, true));
            }

            configuration.Folders = configuration.Folders.Where(x => this.syncThingManager.Folders.Keys.Contains(x.ID)).ToList();

            this.configurationProvider.Save(configuration);
        }
    }
}
<commit_msg>Don't update autostart config from registry if it's a debug build<commit_after>﻿using SyncTrayzor.NotifyIcon;
using SyncTrayzor.Properties;
using SyncTrayzor.Services.UpdateChecker;
using SyncTrayzor.SyncThing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SyncTrayzor.Services
{
    public class ConfigurationApplicator
    {
        private readonly IConfigurationProvider configurationProvider;

        private readonly INotifyIconManager notifyIconManager;
        private readonly ISyncThingManager syncThingManager;
        private readonly AutostartProvider autostartProvider;
        private readonly IWatchedFolderMonitor watchedFolderMonitor;
        private readonly IGithubApiClient githubApiClient;
        private readonly IUpdateChecker updateChecker;

        public ConfigurationApplicator(
            IConfigurationProvider configurationProvider,
            INotifyIconManager notifyIconManager,
            ISyncThingManager syncThingManager,
            AutostartProvider autostartProvider,
            IWatchedFolderMonitor watchedFolderMonitor,
            IGithubApiClient githubApiClient,
            IUpdateChecker updateChecker)
        {
            this.configurationProvider = configurationProvider;
            this.configurationProvider.ConfigurationChanged += (o, e) => this.ApplyNewConfiguration(e.NewConfiguration);

            this.notifyIconManager = notifyIconManager;
            this.syncThingManager = syncThingManager;
            this.autostartProvider = autostartProvider;
            this.watchedFolderMonitor = watchedFolderMonitor;
            this.githubApiClient = githubApiClient;
            this.updateChecker = updateChecker;

            // Do this before signing up for DataLoaded, so any changes we make don't trigger us again
            this.UpdateConfigOnInit();

            this.syncThingManager.DataLoaded += (o, e) => this.LoadFolders();
            this.updateChecker.VersionIgnored += (o, e) =>
            {
                var config = this.configurationProvider.Load();
                config.LatestNotifiedVersion = e.IgnoredVersion;
                this.configurationProvider.Save(config);
            };
        }

        private void UpdateConfigOnInit()
        {
#if !DEBUG
            this.UpdateAutostart();
#endif
        }

        private void UpdateAutostart()
        {
            // If the user's manually updated the registry themselves, update our config to match
            var config = this.configurationProvider.Load();
            var autostartConfig = this.autostartProvider.GetCurrentSetup();
            // We only know enough to change StartMinimized if autostartConfig.AutoStart is strue
            if (config.StartOnLogon != autostartConfig.AutoStart || (autostartConfig.AutoStart && config.StartMinimized != autostartConfig.StartMinimized))
            {
                if (autostartConfig.AutoStart)
                    config.StartMinimized = autostartConfig.StartMinimized;
                config.StartOnLogon = autostartConfig.AutoStart;
                this.configurationProvider.Save(config);
            }
        }

        public void ApplyConfiguration()
        {
            this.githubApiClient.SetConnectionDetails(Settings.Default.GithubApiUrl);
            this.ApplyNewConfiguration(this.configurationProvider.Load());
        }

        private void ApplyNewConfiguration(Configuration configuration)
        {
            this.notifyIconManager.CloseToTray = configuration.CloseToTray;
            this.notifyIconManager.ShowOnlyOnClose = configuration.ShowTrayIconOnlyOnClose;
            this.notifyIconManager.ShowSynchronizedBalloon = configuration.ShowSynchronizedBalloon;

            this.syncThingManager.Address = new Uri(configuration.SyncthingAddress);
            this.syncThingManager.ExecutablePath = configuration.SyncthingPath;
            this.syncThingManager.ApiKey = configuration.SyncthingApiKey;

            this.autostartProvider.SetAutoStart(new AutostartConfiguration() { AutoStart = configuration.StartOnLogon, StartMinimized = configuration.StartMinimized });

            this.watchedFolderMonitor.WatchedFolderIDs = configuration.Folders.Where(x => x.IsWatched).Select(x => x.ID);

            this.updateChecker.LatestIgnoredVersion = configuration.LatestNotifiedVersion;
        }

        private void LoadFolders()
        {
            var configuration = this.configurationProvider.Load();

            foreach (var newKey in this.syncThingManager.Folders.Keys.Except(configuration.Folders.Select(x => x.ID)))
            {
                configuration.Folders.Add(new FolderConfiguration(newKey, true));
            }

            configuration.Folders = configuration.Folders.Where(x => this.syncThingManager.Folders.Keys.Contains(x.ID)).ToList();

            this.configurationProvider.Save(configuration);
        }
    }
}

{'fraction_non_alphanumeric': 0.07030290626279165, 'fraction_numerical': 0.0, 'mean_word_length': 2.7501918649270913, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '2762389', 'n_tokens_mistral': 2430, 'n_tokens_neox': 2376, 'n_words': 445}