public RemoteConnectionsSyncronizer(IConnectionsUpdateChecker updateChecker) { _updateChecker = updateChecker; _updateTimer = new Timer(3000); _connectionsLoader = new ConnectionsLoader { UseDatabase = mRemoteNG.Settings.Default.UseSQLServer }; _connectionsSaver = new ConnectionsSaver { SaveFormat = ConnectionsSaver.Format.SQL }; SetEventListeners(); }
public static void LoadConnections(bool withDialog = false, bool update = false) { var connectionsLoader = new ConnectionsLoader(); try { // disable sql update checking while we are loading updates RemoteConnectionsSyncronizer?.Disable(); if (!Settings.Default.UseSQLServer) { if (withDialog) { var loadDialog = Controls.ConnectionsLoadDialog(); if (loadDialog.ShowDialog() != DialogResult.OK) return; connectionsLoader.ConnectionFileName = loadDialog.FileName; } else { connectionsLoader.ConnectionFileName = GetStartupConnectionFileName(); } CreateBackupFile(Convert.ToString(connectionsLoader.ConnectionFileName)); } connectionsLoader.UseDatabase = Settings.Default.UseSQLServer; ConnectionTreeModel = connectionsLoader.LoadConnections(false); Windows.TreeForm.ConnectionTreeModel = ConnectionTreeModel; if (Settings.Default.UseSQLServer) { LastSqlUpdate = DateTime.Now; } else { if (connectionsLoader.ConnectionFileName == GetDefaultStartupConnectionFileName()) { Settings.Default.LoadConsFromCustomLocation = false; } else { Settings.Default.LoadConsFromCustomLocation = true; Settings.Default.CustomConsPath = connectionsLoader.ConnectionFileName; } } // re-enable sql update checking after updates are loaded RemoteConnectionsSyncronizer?.Enable(); } catch (Exception ex) { if (Settings.Default.UseSQLServer) { MessageCollector.AddExceptionMessage(Language.strLoadFromSqlFailed, ex); var commandButtons = string.Join("|", Language.strCommandTryAgain, Language.strCommandOpenConnectionFile, string.Format(Language.strCommandExitProgram, Application.ProductName)); CTaskDialog.ShowCommandBox(Application.ProductName, Language.strLoadFromSqlFailed, Language.strLoadFromSqlFailedContent, MiscTools.GetExceptionMessageRecursive(ex), "", "", commandButtons, false, ESysIcons.Error, ESysIcons.Error); switch (CTaskDialog.CommandButtonResult) { case 0: LoadConnections(withDialog, update); return; case 1: Settings.Default.UseSQLServer = false; LoadConnections(true, update); return; default: Application.Exit(); return; } } if (ex is FileNotFoundException && !withDialog) { MessageCollector.AddExceptionMessage(string.Format(Language.strConnectionsFileCouldNotBeLoadedNew, connectionsLoader.ConnectionFileName), ex, MessageClass.InformationMsg); NewConnections(Convert.ToString(connectionsLoader.ConnectionFileName)); return; } MessageCollector.AddExceptionMessage(string.Format(Language.strConnectionsFileCouldNotBeLoaded, connectionsLoader.ConnectionFileName), ex); if (connectionsLoader.ConnectionFileName != GetStartupConnectionFileName()) { LoadConnections(withDialog, update); } else { MessageBox.Show(frmMain.Default, string.Format(Language.strErrorStartupConnectionFileLoad, Environment.NewLine, Application.ProductName, GetStartupConnectionFileName(), MiscTools.GetExceptionMessageRecursive(ex)), @"Could not load startup file.", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } } }
public static void NewConnections(string filename) { try { var connectionsLoader = new ConnectionsLoader(); if (filename == GetDefaultStartupConnectionFileName()) { Settings.Default.LoadConsFromCustomLocation = false; } else { Settings.Default.LoadConsFromCustomLocation = true; Settings.Default.CustomConsPath = filename; } var dirname = GetDirectoryName(filename); if(dirname != null) Directory.CreateDirectory(dirname); // Use File.Open with FileMode.CreateNew so that we don't overwrite an existing file var fileStream = File.Open(filename, FileMode.CreateNew, FileAccess.Write, FileShare.None); using (var xmlTextWriter = new XmlTextWriter(fileStream, System.Text.Encoding.UTF8)) { xmlTextWriter.Formatting = Formatting.Indented; xmlTextWriter.Indentation = 4; xmlTextWriter.WriteStartDocument(); xmlTextWriter.WriteStartElement("Connections"); // Do not localize xmlTextWriter.WriteAttributeString("Name", Language.strConnections); xmlTextWriter.WriteAttributeString("Export", "", "False"); xmlTextWriter.WriteAttributeString("Protected", "", "GiUis20DIbnYzWPcdaQKfjE2H5jh//L5v4RGrJMGNXuIq2CttB/d/BxaBP2LwRhY"); xmlTextWriter.WriteAttributeString("ConfVersion", "", "2.5"); xmlTextWriter.WriteEndElement(); xmlTextWriter.WriteEndDocument(); } // Load config connectionsLoader.ConnectionFileName = filename; ConnectionTreeModel = connectionsLoader.LoadConnections(false); Windows.TreeForm.ConnectionTreeModel = ConnectionTreeModel; } catch (Exception ex) { MessageCollector.AddExceptionMessage(Language.strCouldNotCreateNewConnectionsFile, ex); } }