Ejemplo n.º 1
0
        /// <summary>
        ///   Performs an update check for a <see cref="ApplicationVM" /> instance.
        /// </summary>
        /// <param name="applicationVM">
        ///   The <see cref="ApplicationVM" /> instance requesting the update check.
        /// </param>
        /// <param name="showMinimized">
        ///   A <see cref="bool" /> indicating whether the main view should be shown minimized or not.
        /// </param>
        private void ApplicationVM_RequestShowMainWindow(ApplicationVM applicationVM, bool showMinimized)
        {
            // Make sure that auto termination is no longer requested if a window is shown.
            this.Environment.IsNoAutoTerminateDefined = true;

            if (this.MainWindow == null)
            {
                base.MainWindow = new MainWindow(this.Environment, this.ApplicationViewModel);

                // Register light property bindings which make sure that the MainWindow gets always updated with any changed
                // configuration settings.
                LightPropertyBindingManager propertyBindings = new LightPropertyBindingManager();
                propertyBindings.Register(
                    this.Configuration.General, base.MainWindow, new[] {
                    new LightBoundProperty("DisplayCycleTimeAsIconOverlay"),
                    new LightBoundProperty("MinimizeOnClose"),
                    new LightBoundProperty("WallpaperDoubleClickAction")
                });

                if (showMinimized)
                {
                    base.MainWindow.WindowState = WindowState.Minimized;
                }

                base.MainWindow.Closed += (sender, e) => {
                    MainWindow mainWindow = (MainWindow)sender;
                    propertyBindings.DeregisterAll(null, mainWindow);
                    mainWindow.Dispose();

                    this.WriteConfigFile();
                    GC.Collect();
                };
            }
            else
            {
                if (this.MainWindow.WindowState == WindowState.Minimized)
                {
                    this.MainWindow.WindowState = WindowState.Normal;
                }
            }

            base.MainWindow.Show();
        }
Ejemplo n.º 2
0
        /// <summary>
        ///   Raises the <see cref="System.Windows.Application.Startup" /> event and handles the main initialization process of the
        ///   program.
        /// </summary>
        /// <inheritdoc />
        protected override void OnStartup(StartupEventArgs e)
        {
            if (this.IsDisposed)
            {
                return;
            }
            base.OnStartup(e);

            // Note: Has to be set BEFORE showing the first dialog, because the first shown dialog will be suggested as main window
            // and the application will therefore close when the window closes.
            this.ShutdownMode = ShutdownMode.OnExplicitShutdown;

            // Register global error handler.
            this.DispatcherUnhandledException += this.Application_DispatcherUnhandledException;

            Debug.WriteLine("-- Application init process --");
            Debug.Indent();
            // We want only one instance being run at the same time because multiple instances could result in a mess of autocycles.
            // TODO: If another instance is already running, bring its main window to the front.
            if (!Application.isFirstInstance)
            {
                Debug.WriteLine("Detected another running instance of the application.");
                DialogManager.ShowGeneral_AppAlreadyRunning(null);
                Debug.Unindent();
                this.Shutdown();

                return;
            }

            Debug.WriteLine("First step.");
            Debug.Flush();
            // Check whether the Use Default Settings argument is defined.
            if (!this.Environment.IsUseDefaultSettingsDefined)
            {
                if (!File.Exists(this.Environment.ConfigFilePath))
                {
                    Debug.Write("Configuration file at \"");
                    Debug.Write(this.Environment.ConfigFilePath);
                    Debug.WriteLine("\" not found. Using default settings.");
                    DialogManager.ShowConfig_FileNotFound(this.MainWindow, this.Environment.ConfigFilePath);

                    this.Configuration = new Configuration();
                }
                else
                {
#if !DEBUG
                    try {
                        this.configuration = Configuration.Read(this.Environment.ConfigFilePath);
                    } catch (Exception exception) {
                        if (exception is FileNotFoundException || exception is DirectoryNotFoundException)
                        {
                            DialogManager.ShowConfig_FileNotFound(this.MainWindow, this.Environment.ConfigFilePath);
                        }
                        else if (exception is XmlException || exception is InvalidOperationException)
                        {
                            DialogManager.ShowConfig_InvalidFormat(this.MainWindow, this.Environment.ConfigFilePath, exception.ToString());
                        }
                        else
                        {
                            DialogManager.ShowConfig_UnhandledLoadError(this.MainWindow, this.Environment.ConfigFilePath, exception.ToString());
                        }
                    }
          #else
                    this.Configuration = Configuration.Read(this.Environment.ConfigFilePath);
#endif
                }
            }

            this.propertyBindingManager = new LightPropertyBindingManager();

            Debug.WriteLine("Second step.");
            Debug.Flush();
            this.WallpaperChanger = new WallpaperChanger(this.Environment.AppliedWallpaperFilePath, this.Configuration.General.ScreensSettings);
            this.WallpaperChanger.RequestWallpapers  += (senderLocal, eLocal) => { eLocal.Wallpapers.AddRange(this.Configuration.WallpaperCategories.GetAllWallpapers()); };
            this.WallpaperChanger.AutocycleException += this.WallpaperChanger_AutocycleException;

            // Register light property bindings which make sure that the WallpaperChanger gets always updated with any changed
            // configuration settings related to it.
            this.propertyBindingManager.Register(
                this.Configuration.General, this.WallpaperChanger, new[] {
                new LightBoundProperty("WallpaperChangeType"),
                new LightBoundProperty("AutocycleInterval"),
                new LightBoundProperty("LastActiveListSize"),
                new LightBoundProperty("CycleAfterDisplaySettingsChanged"),
                new LightBoundProperty("ScreensSettings")
            });

            this.WallpaperChangerVM = new WallpaperChangerVM(this.WallpaperChanger);
            this.WallpaperChangerVM.UnhandledCommandException += this.WallpaperChangerVM_UnhandledCommandException;

            this.ApplicationViewModel = new ApplicationVM(
                new WallpaperCategoryCollectionVM(
                    this.Configuration.WallpaperCategories, this.WallpaperCategoryCollectionVM_RequestWallpaperCategoryVM
                    ),
                this.WallpaperChangerVM,
                this.ApplicationVM_RequestShowMainWindow,
                this.ApplicationVM_RequestShowConfiguration,
                this.ApplicationVM_RequestShowChangelog,
                this.ApplicationVM_RequestShowAbout,
                this.ApplicationVM_RequestUpdateCheck,
                this.ApplicationVM_RequestTerminateApplication);

            Debug.WriteLine("Third step.");
            Debug.Flush();
            // Cycle and start autocycling if requested.
            try {
                if (this.Configuration.General.StartAutocyclingAfterStartup)
                {
                    this.WallpaperChanger.StartCycling();
                }

                if (this.Configuration.General.CycleAfterStartup)
                {
                    this.WallpaperChanger.CycleNextRandomly();
                }
            } catch (InvalidOperationException) {
                if (this.WallpaperChanger.IsAutocycling)
                {
                    this.WallpaperChanger.StopCycling();

                    DialogManager.ShowCycle_MissingWallpapers(this.MainWindow, true);
                }
                else
                {
                    DialogManager.ShowCycle_MissingWallpapers(this.MainWindow, false);
                }
            } catch (FileNotFoundException exception) {
                DialogManager.ShowGeneral_FileNotFound(this.MainWindow, exception.FileName);
            }

            // Check whether auto termination is requested.
            if ((this.Configuration.General.TerminateAfterStartup) && (!this.Environment.IsNoAutoTerminateDefined))
            {
                DispatcherTimer autoTerminateTimer = new DispatcherTimer(DispatcherPriority.Background);
                autoTerminateTimer.Interval = new TimeSpan(0, 0, Application.AutoTerminate_Seconds);
                autoTerminateTimer.Tick    += delegate {
                    autoTerminateTimer.Stop();

                    // Make sure that auto termination is still requested. It could have been disabled because the user performed
                    // some actions in the Graphical User Interface.
                    if ((!this.IsDisposed) && (!this.Environment.IsNoAutoTerminateDefined))
                    {
                        this.Shutdown();
                    }
                };

                autoTerminateTimer.Start();
            }
            else
            {
                if (this.Configuration.General.MinimizeAfterStartup)
                {
                    if (this.Configuration.General.MinimizeOnClose)
                    {
                        // If the Main Window should be minimized when closed and minimized after startup, we suggest that the user wants
                        // it to be displayed in the Task Bar after application start.
                        this.ApplicationViewModel.ShowMainCommand.Execute(true);
                    }
                }
                else
                {
                    this.ApplicationViewModel.ShowMainCommand.Execute(false);
                }
            }

            Debug.WriteLine("Fourth step.");
            Debug.Flush();
            this.NotifyIcon = new NotifyIconManager(this.ApplicationViewModel);

            // Register light property bindings which make sure that the NotifyIconManager gets always updated with any changed
            // configuration settings related to it.
            this.propertyBindingManager.Register(
                this.Configuration.General, this.NotifyIcon, new[] {
                new LightBoundProperty("TrayIconSingleClickAction"),
                new LightBoundProperty("TrayIconDoubleClickAction")
            });

            UpdateManager updateManager = new UpdateManager(this.Environment);
            updateManager.VersionCheckSuccessful += (senderLocal, eLocal) => {
                // Execute the usual update handling, but only if there is actually an update available.
                if (eLocal.IsUpdate)
                {
                    this.UpdateManager_VersionCheckSuccessful(senderLocal, eLocal);
                }
            };
            updateManager.VersionCheckError += (senderLocal, eLocal) => {
                if (eLocal.Exception is WebException || eLocal.Exception is FormatException)
                {
                    eLocal.IsHandled = true;
                }

                this.UpdateManager_VersionCheckError(senderLocal, eLocal);
            };
            updateManager.DownloadUpdateSuccessful += this.UpdateManager_DownloadUpdateSuccessful;
            updateManager.DownloadUpdateError      += this.UpdateManager_DownloadUpdateError;
            updateManager.BeginVersionCheck();

            Debug.WriteLine("Initialization succeeded.");
            Debug.Flush();
            Debug.Unindent();
        }