Beispiel #1
0
        protected void CheckPreviousBackup()
        {
            string backupFilePath = GetSystemSettingsBackupPath();

            if (File.Exists(backupFilePath))
            {
                // backup file exists
                DateTime date    = File.GetLastWriteTime(backupFilePath);
                string   message = string.Format(Resources.CommandBase_Message_SystemSettingsAreNotRestored, date);
                bool?    answer  = Prompt(message, threeState: true);

                // process depending on answer
                //   true (Yes): restore the backup
                //   false (No): do not restore the backup, but delete it
                //   null (Cancel): do nothing
                if (answer.HasValue)
                {
                    if (answer.Value)
                    {
                        // restore the backup
                        JsonObjectData data = JsonObjectData.Load(backupFilePath, createIfNotExist: false);
                        if (data != null)
                        {
                            SystemSettingsSwitcher switcher = this.ComponentFactory.CreateSystemSettingsSwitcher(this, null);
                            switcher.Restore(data, systemSessionEnding: false);
                        }
                    }

                    // delete backup file
                    File.Delete(backupFilePath);
                }
            }

            return;
        }
Beispiel #2
0
            public bool Stop(bool systemSessionEnding, int millisecondsTimeout = 0)
            {
                // restore the system settings
                SystemSettingsSwitcher switcher = this.switcher;

                this.switcher = null;
                SystemSettings backup = this.backup;

                this.backup = null;
                if (backup != null)
                {
                    Debug.Assert(switcher != null);
                    try {
                        switcher.Restore(backup, systemSessionEnding);
                        this.Owner.DeleteSystemSettingsBackup();
                    } catch (Exception exception) {
                        this.Owner.ShowRestoreSystemSettingsErrorMessage(exception.Message);
                        // continue
                    }
                }

                // stop and dispose the proxy
                Proxy proxy = this.proxy;

                this.proxy = null;

                bool stopConfirmed = false;

                if (proxy == null)
                {
                    stopConfirmed = true;
                }
                else
                {
                    try {
                        stopConfirmed = proxy.Stop(millisecondsTimeout);
                    } finally {
                        DisposableUtil.DisposeSuppressingErrors(proxy);
                    }
                }

                // update credentials if necessary
                IEnumerable <CredentialSettings> credentials = null;

                lock (this.credentialsLocker) {
                    if (this.isCredentialsDirty)
                    {
                        this.isCredentialsDirty = false;
                        credentials             = this.dictionary.Select(pair => CloneSettings(pair.Value)).ToArray();
                    }
                    this.dictionary = null;
                }
                if (credentials != null)
                {
                    if (this.commandSettings != null)
                    {
                        this.commandSettings.Credentials = credentials;
                    }
                    if (this.saveCredentials)
                    {
                        Action saveTask = () => {
                            try {
                                this.Owner.UpdateSettingsFile((s) => { s.Credentials = credentials; }, null);
                            } catch (Exception exception) {
                                string message = string.Format(Resources.CommandBase_Message_FailToSaveCredentials, exception.Message);
                                this.Owner.ShowErrorMessage(message);
                            }
                        };

                        // launch save task
                        Task.Run(saveTask);
                    }
                }
                this.saveCredentials = false;

                // checks
                Debug.Assert(this.proxy == null);
                Debug.Assert(this.switcher == null);
                Debug.Assert(this.backup == null);
                Debug.Assert(this.saveCredentials == false);

                return(stopConfirmed);
            }