Example #1
0
        public ConsoleViewModel(
            ISyncThingManager syncThingManager,
            IConfigurationProvider configurationProvider)
        {
            this.syncThingManager = syncThingManager;
            this.LogMessages      = new Queue <string>();

            // Display log messages 100ms after the previous message, or every 500ms if they're arriving thick and fast
            this.logMessagesBuffer            = new Buffer <string>(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(500));
            this.logMessagesBuffer.Delivered += (o, e) =>
            {
                foreach (var message in e.Items)
                {
                    this.LogMessages.Enqueue(message);
                    if (this.LogMessages.Count > maxLogMessages)
                    {
                        this.LogMessages.Dequeue();
                    }
                }

                this.NotifyOfPropertyChange(() => this.LogMessages);
            };

            this.syncThingManager.MessageLogged += (o, e) =>
            {
                this.logMessagesBuffer.Add(e.LogMessage);
            };
        }
Example #2
0
        public ViewerViewModel(
            IWindowManager windowManager,
            ISyncThingManager syncThingManager,
            IConfigurationProvider configurationProvider,
            IProcessStartProvider processStartProvider)
        {
            this.windowManager         = windowManager;
            this.syncThingManager      = syncThingManager;
            this.processStartProvider  = processStartProvider;
            this.configurationProvider = configurationProvider;

            var configuration = this.configurationProvider.Load();

            this.zoomLevel = configuration.SyncthingWebBrowserZoomLevel;

            this.syncThingManager.StateChanged += (o, e) =>
            {
                this.syncThingState = e.NewState;
                this.RefreshBrowser();
            };

            this.callback = new JavascriptCallbackObject(this.OpenFolder);

            this.Bind(x => x.WebBrowser, (o, e) =>
            {
                if (e.NewValue != null)
                {
                    this.InitializeBrowser(e.NewValue);
                }
            });

            this.SetCulture(configuration);
            configurationProvider.ConfigurationChanged += (o, e) => this.SetCulture(e.NewConfiguration);
        }
        public ViewerViewModel(
            IWindowManager windowManager,
            ISyncThingManager syncThingManager,
            IConfigurationProvider configurationProvider,
            IProcessStartProvider processStartProvider,
            IApplicationPathsProvider pathsProvider)
        {
            this.windowManager = windowManager;
            this.syncThingManager = syncThingManager;
            this.processStartProvider = processStartProvider;
            this.configurationProvider = configurationProvider;
            this.pathsProvider = pathsProvider;

            var configuration = this.configurationProvider.Load();
            this.zoomLevel = configuration.SyncthingWebBrowserZoomLevel;

            this.syncThingManager.StateChanged += (o, e) =>
            {
                this.syncThingState = e.NewState;
                this.RefreshBrowser();
            };

            this.callback = new JavascriptCallbackObject(this.OpenFolder);

            this.Bind(x => x.WebBrowser, (o, e) =>
            {
                if (e.NewValue != null)
                    this.InitializeBrowser(e.NewValue);
            });

            this.SetCulture(configuration);
            configurationProvider.ConfigurationChanged += (o, e) => this.SetCulture(e.NewConfiguration);
        }
Example #4
0
        public AboutViewModel(
            IWindowManager windowManager,
            ISyncThingManager syncThingManager,
            IConfigurationProvider configurationProvider,
            IUpdateManager updateManager,
            Func <ThirdPartyComponentsViewModel> thirdPartyComponentsViewModelFactory,
            IProcessStartProvider processStartProvider)
        {
            this.windowManager    = windowManager;
            this.syncThingManager = syncThingManager;
            this.updateManager    = updateManager;
            this.thirdPartyComponentsViewModelFactory = thirdPartyComponentsViewModelFactory;
            this.processStartProvider = processStartProvider;

            this.Version     = Assembly.GetExecutingAssembly().GetName().Version.ToString(3);
            this.HomepageUrl = Properties.Settings.Default.HomepageUrl;

            this.SyncthingVersion             = this.syncThingManager.Version == null ? Resources.AboutView_UnknownVersion : this.syncThingManager.Version.Version;
            this.syncThingManager.DataLoaded += (o, e) =>
            {
                this.SyncthingVersion = this.syncThingManager.Version == null ? Resources.AboutView_UnknownVersion : this.syncThingManager.Version.Version;
            };

            this.CheckForNewerVersionAsync();
        }
        public NotifyIconViewModel(
            IWindowManager windowManager,
            ISyncThingManager syncThingManager,
            Func <SettingsViewModel> settingsViewModelFactory,
            IProcessStartProvider processStartProvider)
        {
            this.windowManager            = windowManager;
            this.syncThingManager         = syncThingManager;
            this.settingsViewModelFactory = settingsViewModelFactory;
            this.processStartProvider     = processStartProvider;

            this.syncThingManager.StateChanged += (o, e) =>
            {
                this.SyncThingState = e.NewState;
                if (e.NewState != SyncThingState.Running)
                {
                    this.SyncThingSyncing = false; // Just make sure we reset this...
                }
            };
            this.SyncThingState = this.syncThingManager.State;

            this.syncThingManager.TotalConnectionStatsChanged += (o, e) =>
            {
                var stats = e.TotalConnectionStats;
                this.SyncThingSyncing = stats.InBytesPerSecond > 0 || stats.OutBytesPerSecond > 0;
            };

            this.syncThingManager.DataLoaded += (o, e) =>
            {
                this.Folders = new BindableCollection <FolderViewModel>(this.syncThingManager.FetchAllFolders()
                                                                        .Select(x => new FolderViewModel(x, this.processStartProvider))
                                                                        .OrderBy(x => x.FolderId));
            };
        }
        public static async Task StartWithErrorDialogAsync(this ISyncThingManager syncThingManager, IWindowManager windowManager)
        {
            try
            {
                await syncThingManager.StartAsync();
            }
            catch (Win32Exception e)
            {
                if (e.ErrorCode != -2147467259)
                {
                    throw;
                }

                // Possibly "This program is blocked by group policy. For more information, contact your system administrator" caused
                // by e.g. CryptoLocker?
                windowManager.ShowMessageBox(
                    Localizer.Translate("Dialog_SyncthingBlockedByGroupPolicy_Message", e.Message, syncThingManager.ExecutablePath),
                    Localizer.Translate("Dialog_SyncthingBlockedByGroupPolicy_Title"),
                    MessageBoxButton.OK, icon: MessageBoxImage.Error);
            }
            catch (SyncThingDidNotStartCorrectlyException)
            {
                // Haven't translated yet - still debugging
                windowManager.ShowMessageBox(
                    "Syncthing started running, but we were enable to connect to it. Please report this as a bug",
                    "Syncthing didn't start correctly",
                    MessageBoxButton.OK, icon: MessageBoxImage.Error);
            }
        }
        public AboutViewModel(
            IWindowManager windowManager,
            ISyncThingManager syncThingManager,
            IConfigurationProvider configurationProvider,
            IUpdateManager updateManager,
            Func<ThirdPartyComponentsViewModel> thirdPartyComponentsViewModelFactory,
            IProcessStartProvider processStartProvider)
        {
            this.windowManager = windowManager;
            this.syncThingManager = syncThingManager;
            this.updateManager = updateManager;
            this.thirdPartyComponentsViewModelFactory = thirdPartyComponentsViewModelFactory;
            this.processStartProvider = processStartProvider;

            this.Version = Assembly.GetExecutingAssembly().GetName().Version.ToString(3);
            this.HomepageUrl = Properties.Settings.Default.HomepageUrl;

            this.SyncthingVersion = this.syncThingManager.Version == null ? Resources.AboutView_UnknownVersion : this.syncThingManager.Version.Version;
            this.syncThingManager.DataLoaded += (o, e) =>
            {
                this.SyncthingVersion = this.syncThingManager.Version == null ? Resources.AboutView_UnknownVersion : this.syncThingManager.Version.Version;
            };

            this.CheckForNewerVersionAsync();
        }
        public static async Task StartWithErrorDialogAsync(this ISyncThingManager syncThingManager, IWindowManager windowManager)
        {
            try
            {
                await syncThingManager.StartAsync();
            }
            catch (Win32Exception e)
            {
                if (e.ErrorCode != -2147467259)
                {
                    throw;
                }

                // Possibly "This program is blocked by group policy. For more information, contact your system administrator" caused
                // by e.g. CryptoLocker?
                windowManager.ShowMessageBox(
                    Localizer.F(Resources.Dialog_SyncthingBlockedByGroupPolicy_Message, e.Message, syncThingManager.ExecutablePath),
                    Resources.Dialog_SyncthingBlockedByGroupPolicy_Title,
                    MessageBoxButton.OK, icon: MessageBoxImage.Error);
            }
            catch (SyncThingDidNotStartCorrectlyException e)
            {
                windowManager.ShowMessageBox(
                    Localizer.F(Resources.Dialog_SyncthingDidNotStart_Message, e.Message),
                    Resources.Dialog_SyncthingDidNotStart_Title,
                    MessageBoxButton.OK, icon: MessageBoxImage.Error);
            }
        }
        public ConsoleViewModel(
            ISyncThingManager syncThingManager,
            IConfigurationProvider configurationProvider)
        {
            this.syncThingManager = syncThingManager;
            this.LogMessages = new Queue<string>();

            // Display log messages 100ms after the previous message, or every 500ms if they're arriving thick and fast
            this.logMessagesBuffer = new Buffer<string>(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(500));
            this.logMessagesBuffer.Delivered += (o, e) =>
            {
                foreach (var message in e.Items)
                {
                    this.LogMessages.Enqueue(message);
                    if (this.LogMessages.Count > maxLogMessages)
                        this.LogMessages.Dequeue();
                }

                this.NotifyOfPropertyChange(() => this.LogMessages);
            };

            this.syncThingManager.MessageLogged += (o, e) =>
            {
                this.logMessagesBuffer.Add(e.LogMessage);
            };
        }
        public NotifyIconViewModel(
            IWindowManager windowManager,
            ISyncThingManager syncThingManager,
            Func<SettingsViewModel> settingsViewModelFactory,
            IProcessStartProvider processStartProvider,
            FileTransfersTrayViewModel fileTransfersViewModel)
        {
            this.windowManager = windowManager;
            this.syncThingManager = syncThingManager;
            this.settingsViewModelFactory = settingsViewModelFactory;
            this.processStartProvider = processStartProvider;
            this.FileTransfersViewModel = fileTransfersViewModel;

            this.syncThingManager.StateChanged += (o, e) =>
            {
                this.SyncThingState = e.NewState;
                if (e.NewState != SyncThingState.Running)
                    this.SyncThingSyncing = false; // Just make sure we reset this...
            };
            this.SyncThingState = this.syncThingManager.State;

            this.syncThingManager.TotalConnectionStatsChanged += (o, e) =>
            {
                var stats = e.TotalConnectionStats;
                this.SyncThingSyncing = stats.InBytesPerSecond > 0 || stats.OutBytesPerSecond > 0;
            };

            this.syncThingManager.DataLoaded += (o, e) =>
            {
                this.Folders = new BindableCollection<FolderViewModel>(this.syncThingManager.Folders.FetchAll()
                    .Select(x => new FolderViewModel(x, this.processStartProvider))
                    .OrderBy(x => x.FolderId));
            };
        }
Example #11
0
        public NotifyIconManager(
            IViewManager viewManager,
            NotifyIconViewModel viewModel,
            IApplicationState application,
            IApplicationWindowState applicationWindowState,
            ISyncThingManager syncThingManager)
        {
            this.viewManager            = viewManager;
            this.viewModel              = viewModel;
            this.application            = application;
            this.applicationWindowState = applicationWindowState;
            this.syncThingManager       = syncThingManager;

            this.taskbarIcon = (TaskbarIcon)this.application.FindResource("TaskbarIcon");
            // Need to hold off until after the application is started, otherwise the ViewManager won't be set
            this.application.Startup += (o, e) => this.viewManager.BindViewToModel(this.taskbarIcon, this.viewModel);

            this.applicationWindowState.RootWindowActivated   += this.RootViewModelActivated;
            this.applicationWindowState.RootWindowDeactivated += this.RootViewModelDeactivated;
            this.applicationWindowState.RootWindowClosed      += this.RootViewModelClosed;

            this.viewModel.WindowOpenRequested += (o, e) =>
            {
                this.applicationWindowState.EnsureInForeground();
            };
            this.viewModel.WindowCloseRequested += (o, e) =>
            {
                // Always minimize, regardless of settings
                this.application.ShutdownMode = ShutdownMode.OnExplicitShutdown;
                this.applicationWindowState.CloseToTray();
            };
            this.viewModel.ExitRequested += (o, e) => this.application.Shutdown();

            this.syncThingManager.TransferHistory.FolderSynchronizationFinished += this.FolderSynchronizationFinished;

            this.syncThingManager.DeviceConnected += (o, e) =>
            {
                if (this.ShowDeviceConnectivityBalloons &&
                    DateTime.UtcNow - this.syncThingManager.StartedTime > syncedDeadTime)
                {
                    this.taskbarIcon.HideBalloonTip();
                    this.taskbarIcon.ShowBalloonTip(Resources.TrayIcon_Balloon_DeviceConnected_Title, String.Format(Resources.TrayIcon_Balloon_DeviceConnected_Message, e.Device.Name), BalloonIcon.Info);
                }
            };

            this.syncThingManager.DeviceDisconnected += (o, e) =>
            {
                if (this.ShowDeviceConnectivityBalloons &&
                    DateTime.UtcNow - this.syncThingManager.StartedTime > syncedDeadTime)
                {
                    this.taskbarIcon.HideBalloonTip();
                    this.taskbarIcon.ShowBalloonTip(Resources.TrayIcon_Balloon_DeviceDisconnected_Title, String.Format(Resources.TrayIcon_Balloon_DeviceDisconnected_Message, e.Device.Name), BalloonIcon.Info);
                }
            };
        }
        public NotifyIconManager(
            IViewManager viewManager,
            NotifyIconViewModel viewModel,
            IApplicationState application,
            IApplicationWindowState applicationWindowState,
            ISyncThingManager syncThingManager)
        {
            this.viewManager = viewManager;
            this.viewModel = viewModel;
            this.application = application;
            this.applicationWindowState = applicationWindowState;
            this.syncThingManager = syncThingManager;

            this.taskbarIcon = (TaskbarIcon)this.application.FindResource("TaskbarIcon");
            // Need to hold off until after the application is started, otherwise the ViewManager won't be set
            this.application.Startup += (o, e) => this.viewManager.BindViewToModel(this.taskbarIcon, this.viewModel);

            this.applicationWindowState.RootWindowActivated += this.RootViewModelActivated;
            this.applicationWindowState.RootWindowDeactivated += this.RootViewModelDeactivated;
            this.applicationWindowState.RootWindowClosed += this.RootViewModelClosed;

            this.viewModel.WindowOpenRequested += (o, e) =>
            {
                this.applicationWindowState.EnsureInForeground();
            };
            this.viewModel.WindowCloseRequested += (o, e) =>
            {
                // Always minimize, regardless of settings
                this.application.ShutdownMode = ShutdownMode.OnExplicitShutdown;
                this.applicationWindowState.CloseToTray();
            };
            this.viewModel.ExitRequested += (o, e) => this.application.Shutdown();

            this.syncThingManager.TransferHistory.FolderSynchronizationFinished += this.FolderSynchronizationFinished;

            this.syncThingManager.DeviceConnected += (o, e) =>
            {
                if (this.ShowDeviceConnectivityBalloons &&
                    DateTime.UtcNow - this.syncThingManager.StartedTime > syncedDeadTime)
                {
                    this.taskbarIcon.HideBalloonTip();
                    this.taskbarIcon.ShowBalloonTip(Resources.TrayIcon_Balloon_DeviceConnected_Title, String.Format(Resources.TrayIcon_Balloon_DeviceConnected_Message, e.Device.Name), BalloonIcon.Info);
                }
            };

            this.syncThingManager.DeviceDisconnected += (o, e) =>
            {
                if (this.ShowDeviceConnectivityBalloons &&
                    DateTime.UtcNow - this.syncThingManager.StartedTime > syncedDeadTime)
                {
                    this.taskbarIcon.HideBalloonTip();
                    this.taskbarIcon.ShowBalloonTip(Resources.TrayIcon_Balloon_DeviceDisconnected_Title, String.Format(Resources.TrayIcon_Balloon_DeviceDisconnected_Message, e.Device.Name), BalloonIcon.Info);
                }
            };
        }
Example #13
0
        public FileTransfersTrayViewModel(ISyncThingManager syncThingManager, IProcessStartProvider processStartProvider)
        {
            this.syncThingManager     = syncThingManager;
            this.processStartProvider = processStartProvider;

            this.CompletedTransfers  = new BindableCollection <FileTransferViewModel>();
            this.InProgressTransfers = new BindableCollection <FileTransferViewModel>();

            this.CompletedTransfers.CollectionChanged  += (o, e) => { this.NotifyOfPropertyChange(() => this.HasCompletedTransfers); this.NotifyOfPropertyChange(() => this.AnyTransfers); };
            this.InProgressTransfers.CollectionChanged += (o, e) => { this.NotifyOfPropertyChange(() => this.HasInProgressTransfers); this.NotifyOfPropertyChange(() => this.AnyTransfers); };
        }
Example #14
0
        public ShellViewModel(
            IWindowManager windowManager,
            ISyncThingManager syncThingManager,
            IApplicationState application,
            IConfigurationProvider configurationProvider,
            ConsoleViewModel console,
            ViewerViewModel viewer,
            Func<SettingsViewModel> settingsViewModelFactory,
            Func<AboutViewModel> aboutViewModelFactory,
            IProcessStartProvider processStartProvider)
        {
            this.windowManager = windowManager;
            this.syncThingManager = syncThingManager;
            this.application = application;
            this.configurationProvider = configurationProvider;
            this.Console = console;
            this.Viewer = viewer;
            this.settingsViewModelFactory = settingsViewModelFactory;
            this.aboutViewModelFactory = aboutViewModelFactory;
            this.processStartProvider = processStartProvider;

            var configuration = this.configurationProvider.Load();

            this.Console.ConductWith(this);
            this.Viewer.ConductWith(this);

            this.syncThingManager.StateChanged += (o, e) => this.SyncThingState = e.NewState;
            this.syncThingManager.ProcessExitedWithError += (o, e) => this.ShowExitedWithError();

            this.ConsoleHeight = configuration.SyncthingConsoleHeight;
            this.Bind(s => s.ConsoleHeight, (o, e) => this.configurationProvider.AtomicLoadAndSave(c => c.SyncthingConsoleHeight = e.NewValue));

            this.ShowConsole = configuration.SyncthingConsoleHeight > 0;
            this.Bind(s => s.ShowConsole, (o, e) =>
            {
                this.ConsoleHeight = e.NewValue ? Configuration.DefaultSyncthingConsoleHeight : 0.0;
            });

            this.Placement = configuration.WindowPlacement;
            this.Bind(s => s.Placement, (o, e) => this.configurationProvider.AtomicLoadAndSave(c => c.WindowPlacement = e.NewValue));
        }
Example #15
0
        public ShellViewModel(
            IWindowManager windowManager,
            ISyncThingManager syncThingManager,
            IApplicationState application,
            IConfigurationProvider configurationProvider,
            ConsoleViewModel console,
            ViewerViewModel viewer,
            Func <SettingsViewModel> settingsViewModelFactory,
            Func <AboutViewModel> aboutViewModelFactory,
            IProcessStartProvider processStartProvider)
        {
            this.windowManager         = windowManager;
            this.syncThingManager      = syncThingManager;
            this.application           = application;
            this.configurationProvider = configurationProvider;
            this.Console = console;
            this.Viewer  = viewer;
            this.settingsViewModelFactory = settingsViewModelFactory;
            this.aboutViewModelFactory    = aboutViewModelFactory;
            this.processStartProvider     = processStartProvider;

            var configuration = this.configurationProvider.Load();

            this.Console.ConductWith(this);
            this.Viewer.ConductWith(this);

            this.syncThingManager.StateChanged           += (o, e) => this.SyncThingState = e.NewState;
            this.syncThingManager.ProcessExitedWithError += (o, e) => this.ShowExitedWithError();

            this.ConsoleHeight = configuration.SyncthingConsoleHeight;
            this.Bind(s => s.ConsoleHeight, (o, e) => this.configurationProvider.AtomicLoadAndSave(c => c.SyncthingConsoleHeight = e.NewValue));

            this.ShowConsole = configuration.SyncthingConsoleHeight > 0;
            this.Bind(s => s.ShowConsole, (o, e) =>
            {
                this.ConsoleHeight = e.NewValue ? Configuration.DefaultSyncthingConsoleHeight : 0.0;
            });

            this.Placement = configuration.WindowPlacement;
            this.Bind(s => s.Placement, (o, e) => this.configurationProvider.AtomicLoadAndSave(c => c.WindowPlacement = e.NewValue));
        }
        public ConfigurationApplicator(
            IConfigurationProvider configurationProvider,
            IApplicationPathsProvider pathsProvider,
            INotifyIconManager notifyIconManager,
            ISyncThingManager syncThingManager,
            IAutostartProvider autostartProvider,
            IWatchedFolderMonitor watchedFolderMonitor,
            IUpdateManager updateManager)
        {
            this.configurationProvider = configurationProvider;
            this.configurationProvider.ConfigurationChanged += (o, e) => this.ApplyNewConfiguration(e.NewConfiguration);

            this.pathsProvider = pathsProvider;
            this.notifyIconManager = notifyIconManager;
            this.syncThingManager = syncThingManager;
            this.autostartProvider = autostartProvider;
            this.watchedFolderMonitor = watchedFolderMonitor;
            this.updateManager = updateManager;

            this.syncThingManager.DataLoaded += (o, e) => this.LoadFolders();
            this.updateManager.VersionIgnored += (o, e) => this.configurationProvider.AtomicLoadAndSave(config => config.LatestNotifiedVersion = e.IgnoredVersion);
        }
        public ConfigurationApplicator(
            IConfigurationProvider configurationProvider,
            IApplicationPathsProvider pathsProvider,
            INotifyIconManager notifyIconManager,
            ISyncThingManager syncThingManager,
            IAutostartProvider autostartProvider,
            IWatchedFolderMonitor watchedFolderMonitor,
            IUpdateManager updateManager)
        {
            this.configurationProvider = configurationProvider;
            this.configurationProvider.ConfigurationChanged += (o, e) => this.ApplyNewConfiguration(e.NewConfiguration);

            this.pathsProvider        = pathsProvider;
            this.notifyIconManager    = notifyIconManager;
            this.syncThingManager     = syncThingManager;
            this.autostartProvider    = autostartProvider;
            this.watchedFolderMonitor = watchedFolderMonitor;
            this.updateManager        = updateManager;

            this.syncThingManager.DataLoaded  += (o, e) => this.LoadFolders();
            this.updateManager.VersionIgnored += (o, e) => this.configurationProvider.AtomicLoadAndSave(config => config.LatestNotifiedVersion = e.IgnoredVersion);
        }
 public WatchedFolderMonitor(ISyncThingManager syncThingManager)
 {
     this.syncThingManager = syncThingManager;
     this.syncThingManager.Folders.FoldersChanged += (o, e) => this.Reset();
     this.syncThingManager.StateChanged += (o, e) => this.Reset();
 }
 public WatchedFolderMonitor(ISyncThingManager syncThingManager)
 {
     this.syncThingManager = syncThingManager;
     this.syncThingManager.Folders.FoldersChanged += (o, e) => this.Reset();
     this.syncThingManager.StateChanged           += (o, e) => this.Reset();
 }
        public FileTransfersTrayViewModel(ISyncThingManager syncThingManager, IProcessStartProvider processStartProvider)
        {
            this.syncThingManager = syncThingManager;
            this.processStartProvider = processStartProvider;

            this.CompletedTransfers = new BindableCollection<FileTransferViewModel>();
            this.InProgressTransfers = new BindableCollection<FileTransferViewModel>();

            this.CompletedTransfers.CollectionChanged += (o, e) => { this.NotifyOfPropertyChange(() => this.HasCompletedTransfers); this.NotifyOfPropertyChange(() => this.AnyTransfers); };
            this.InProgressTransfers.CollectionChanged += (o, e) => { this.NotifyOfPropertyChange(() => this.HasInProgressTransfers); this.NotifyOfPropertyChange(() => this.AnyTransfers); };
        }
        public SettingsViewModel(
            IConfigurationProvider configurationProvider,
            IAutostartProvider autostartProvider,
            IWindowManager windowManager,
            IProcessStartProvider processStartProvider,
            IAssemblyProvider assemblyProvider,
            IApplicationState applicationState,
            ISyncThingManager syncThingManager)
        {
            this.configurationProvider = configurationProvider;
            this.autostartProvider     = autostartProvider;
            this.windowManager         = windowManager;
            this.processStartProvider  = processStartProvider;
            this.assemblyProvider      = assemblyProvider;
            this.applicationState      = applicationState;
            this.syncThingManager      = syncThingManager;

            this.MinimizeToTray      = this.CreateBasicSettingItem(x => x.MinimizeToTray);
            this.NotifyOfNewVersions = this.CreateBasicSettingItem(x => x.NotifyOfNewVersions);
            this.CloseToTray         = this.CreateBasicSettingItem(x => x.CloseToTray);
            this.ObfuscateDeviceIDs  = this.CreateBasicSettingItem(x => x.ObfuscateDeviceIDs);
            this.UseComputerCulture  = this.CreateBasicSettingItem(x => x.UseComputerCulture);
            this.UseComputerCulture.RequiresSyncTrayzorRestart = true;
            this.DisableHardwareRendering = this.CreateBasicSettingItem(x => x.DisableHardwareRendering);
            this.DisableHardwareRendering.RequiresSyncTrayzorRestart = true;

            this.ShowTrayIconOnlyOnClose = this.CreateBasicSettingItem(x => x.ShowTrayIconOnlyOnClose);
            this.ShowSynchronizedBalloonEvenIfNothingDownloaded = this.CreateBasicSettingItem(x => x.ShowSynchronizedBalloonEvenIfNothingDownloaded);
            this.ShowDeviceConnectivityBalloons = this.CreateBasicSettingItem(x => x.ShowDeviceConnectivityBalloons);

            this.StartSyncThingAutomatically = this.CreateBasicSettingItem(x => x.StartSyncthingAutomatically);
            this.SyncthingPriorityLevel      = this.CreateBasicSettingItem(x => x.SyncthingPriorityLevel);
            this.SyncthingPriorityLevel.RequiresSyncthingRestart = true;
            this.SyncthingUseDefaultHome = this.CreateBasicSettingItem(x => !x.SyncthingUseCustomHome, (x, v) => x.SyncthingUseCustomHome = !v);
            this.SyncthingUseDefaultHome.RequiresSyncthingRestart = true;
            this.SyncThingAddress = this.CreateBasicSettingItem(x => x.SyncthingAddress, new SyncThingAddressValidator());
            this.SyncThingAddress.RequiresSyncthingRestart = true;
            this.SyncThingApiKey = this.CreateBasicSettingItem(x => x.SyncthingApiKey, new SyncThingApiKeyValidator());
            this.SyncThingApiKey.RequiresSyncthingRestart = true;

            this.CanReadAutostart  = this.autostartProvider.CanRead;
            this.CanWriteAutostart = this.autostartProvider.CanWrite;
            if (this.autostartProvider.CanRead)
            {
                var currentSetup = this.autostartProvider.GetCurrentSetup();
                this.StartOnLogon   = currentSetup.AutoStart;
                this.StartMinimized = currentSetup.StartMinimized;
            }

            this.SyncThingCommandLineFlags = this.CreateBasicSettingItem(
                x => String.Join(" ", x.SyncthingCommandLineFlags),
                (x, v) =>
            {
                IEnumerable <KeyValuePair <string, string> > envVars;
                KeyValueStringParser.TryParse(v, out envVars, mustHaveValue: false);
                x.SyncthingCommandLineFlags = envVars.Select(item => KeyValueStringParser.FormatItem(item.Key, item.Value)).ToList();
            }, new SyncThingCommandLineFlagsValidator());
            this.SyncThingCommandLineFlags.RequiresSyncthingRestart = true;


            this.SyncThingEnvironmentalVariables = this.CreateBasicSettingItem(
                x => KeyValueStringParser.Format(x.SyncthingEnvironmentalVariables),
                (x, v) =>
            {
                IEnumerable <KeyValuePair <string, string> > envVars;
                KeyValueStringParser.TryParse(v, out envVars);
                x.SyncthingEnvironmentalVariables = new EnvironmentalVariableCollection(envVars);
            }, new SyncThingEnvironmentalVariablesValidator());
            this.SyncThingEnvironmentalVariables.RequiresSyncthingRestart = true;

            this.SyncthingDenyUpgrade = this.CreateBasicSettingItem(x => x.SyncthingDenyUpgrade);
            this.SyncthingDenyUpgrade.RequiresSyncthingRestart = true;

            var configuration = this.configurationProvider.Load();

            foreach (var settingItem in this.settings)
            {
                settingItem.LoadValue(configuration);
            }

            this.FolderSettings = new BindableCollection <FolderSettings>();
            if (syncThingManager.State == SyncThingState.Running)
            {
                this.FolderSettings.AddRange(configuration.Folders.OrderByDescending(x => x.ID).Select(x => new FolderSettings()
                {
                    FolderName = x.ID,
                    IsWatched  = x.IsWatched,
                    IsNotified = x.NotificationsEnabled,
                }));
            }

            foreach (var folderSetting in this.FolderSettings)
            {
                folderSetting.Bind(s => s.IsWatched, (o, e) => this.UpdateAreAllFoldersWatched());
                folderSetting.Bind(s => s.IsNotified, (o, e) => this.UpdateAreAllFoldersNotified());
            }

            this.PriorityLevels = new BindableCollection <LabelledValue <SyncThingPriorityLevel> >()
            {
                LabelledValue.Create(Resources.SettingsView_Syncthing_ProcessPriority_AboveNormal, SyncThingPriorityLevel.AboveNormal),
                LabelledValue.Create(Resources.SettingsView_Syncthing_ProcessPriority_Normal, SyncThingPriorityLevel.Normal),
                LabelledValue.Create(Resources.SettingsView_Syncthing_ProcessPriority_BelowNormal, SyncThingPriorityLevel.BelowNormal),
                LabelledValue.Create(Resources.SettingsView_Syncthing_ProcessPriority_Idle, SyncThingPriorityLevel.Idle),
            };

            this.Bind(s => s.AreAllFoldersNotified, (o, e) =>
            {
                if (this.updatingFolderSettings)
                {
                    return;
                }

                this.updatingFolderSettings = true;

                foreach (var folderSetting in this.FolderSettings)
                {
                    folderSetting.IsNotified = e.NewValue.GetValueOrDefault(false);
                }

                this.updatingFolderSettings = false;
            });

            this.Bind(s => s.AreAllFoldersWatched, (o, e) =>
            {
                if (this.updatingFolderSettings)
                {
                    return;
                }

                this.updatingFolderSettings = true;

                foreach (var folderSetting in this.FolderSettings)
                {
                    folderSetting.IsWatched = e.NewValue.GetValueOrDefault(false);
                }

                this.updatingFolderSettings = false;
            });

            this.UpdateAreAllFoldersWatched();
            this.UpdateAreAllFoldersNotified();
        }
Example #22
0
        public NotifyIconManager(
            IViewManager viewManager,
            NotifyIconViewModel viewModel,
            IApplicationState application,
            IApplicationWindowState applicationWindowState,
            ISyncThingManager syncThingManager)
        {
            this.viewManager            = viewManager;
            this.viewModel              = viewModel;
            this.application            = application;
            this.applicationWindowState = applicationWindowState;
            this.syncThingManager       = syncThingManager;

            this.taskbarIcon = (TaskbarIcon)this.application.FindResource("TaskbarIcon");
            this.viewManager.BindViewToModel(this.taskbarIcon, this.viewModel);

            this.applicationWindowState.RootWindowActivated   += this.RootViewModelActivated;
            this.applicationWindowState.RootWindowDeactivated += this.RootViewModelDeactivated;
            this.applicationWindowState.RootWindowClosed      += this.RootViewModelClosed;

            this.viewModel.WindowOpenRequested += (o, e) =>
            {
                this.applicationWindowState.EnsureInForeground();
            };
            this.viewModel.WindowCloseRequested += (o, e) =>
            {
                // Always minimize, regardless of settings
                this.application.ShutdownMode = ShutdownMode.OnExplicitShutdown;
                this.applicationWindowState.CloseToTray();
            };
            this.viewModel.ExitRequested += (o, e) => this.application.Shutdown();

            this.syncThingManager.FolderSyncStateChanged += (o, e) =>
            {
                if (this.ShowSynchronizedBalloon &&
                    DateTime.UtcNow - this.syncThingManager.LastConnectivityEventTime > syncedDeadTime &&
                    DateTime.UtcNow - this.syncThingManager.StartedTime > syncedDeadTime &&
                    e.SyncState == FolderSyncState.Idle && e.PrevSyncState == FolderSyncState.Syncing)
                {
                    Application.Current.Dispatcher.CheckAccess(); // Double-check
                    this.taskbarIcon.ShowBalloonTip(Localizer.Translate("TrayIcon_Balloon_FinishedSyncing_Title"), Localizer.Translate("TrayIcon_Balloon_FinishedSyncing_Message", e.Folder.FolderId), BalloonIcon.Info);
                }
            };

            this.syncThingManager.DeviceConnected += (o, e) =>
            {
                if (this.ShowDeviceConnectivityBalloons &&
                    DateTime.UtcNow - this.syncThingManager.StartedTime > syncedDeadTime)
                {
                    this.taskbarIcon.ShowBalloonTip(Localizer.Translate("TrayIcon_Balloon_DeviceConnected_Title"), Localizer.Translate("TrayIcon_Balloon_DeviceConnected_Message", e.Device.Name), BalloonIcon.Info);
                }
            };

            this.syncThingManager.DeviceDisconnected += (o, e) =>
            {
                if (this.ShowDeviceConnectivityBalloons &&
                    DateTime.UtcNow - this.syncThingManager.StartedTime > syncedDeadTime)
                {
                    this.taskbarIcon.ShowBalloonTip(Localizer.Translate("TrayIcon_Balloon_DeviceDisconnected_Title"), Localizer.Translate("TrayIcon_Balloon_DeviceDisconnected_Message", e.Device.Name), BalloonIcon.Info);
                }
            };
        }