private void MainWindow_FormClosing(object sender, FormClosingEventArgs e) { if (SCMS.CurrentSystemUser != null) { if (SCMS.CurrentSystemUser.IsSignedIn) { e.Cancel = VisualBasic.CBool(MsgBoxEx.Ask("Quit from the application?", "Exit") != System.Windows.Forms.DialogResult.Yes); if (!e.Cancel) { SCMS.CleanUp(); SCMS.CurrentSystemUser.LogOut(); SCMS.CurrentSystemUser = null; SCMS.Connection = null; Application.Exit(); } else { _isclosing = false; } } else { Program.StartUpForm.Show(); } } else { if (!e.Cancel) { SCMS.CleanUp(); SCMS.Connection = null; Application.Exit(); } } }
private void mnuLogout_Click(object sender, EventArgs e) { SCMS.CleanUp(); SCMS.CurrentSystemUser.LogOut(); Close(); SCMS.CurrentSystemUser = null; Materia.RefreshAndManageCurrentProcess(); }
private void RunRestoration() { if (!File.Exists(_backupfilename)) { MsgBoxEx.Shout("Could not locate the specified database backup file / restore point.", "Database Restoration"); return; } btnBrowseDrive.Enabled = false; btnBrowseRestorePoint.Enabled = false; btnRestore.Enabled = false; btnCancel.BringToFront(); InitializeEventGrid(); chkCreateRestorePoint.Enabled = false; pctLoad.Show(); pctLoad.BringToFront(); _isrunning = true; _cancelled = false; AddEvent(BackupEvent.Information, "Starting database restoration routines..."); RestorePointInfo _restorepoint = null; if (chkCreateRestorePoint.Checked) { _restorepoint = MakeRestorePoint(); if (_restorepoint == null) { btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); return; } } string _filename = _backupfilename; int _trycounter = 0; if (Path.GetExtension(_filename).ToLower().Replace(".", "").RLTrim() == "scmsiv") { AddEvent(BackupEvent.Information, "Extracting database backup..."); _filename = ""; string _dbtempdir = Application.StartupPath + "\\dbtemp"; if (!Directory.Exists(_dbtempdir)) { try { Directory.CreateDirectory(_dbtempdir); } catch (Exception ex) { SCMS.LogError(this.GetType().Name, ex); AddEvent(BackupEvent.Error, "Could not create temporary backup file extraction directory."); btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); return; } } string _archivedir = _dbtempdir + "\\" + Path.GetFileNameWithoutExtension(_backupfilename); Func <string, string, bool> _extractordelegate = new Func <string, string, bool>(Archiver.Decompress); IAsyncResult _extractorresult = _extractordelegate.BeginInvoke(_backupfilename, _archivedir, null, _extractordelegate); while (!_extractorresult.IsCompleted && !_cancelled) { Thread.Sleep(1); Application.DoEvents(); } if (_cancelled) { AddEvent(BackupEvent.Warning, "Cancelling database restoration..."); _trycounter = 0; while (Directory.Exists(_archivedir) && _trycounter <= 30) { try { Directory.Delete(_archivedir, true); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); AddEvent(BackupEvent.Information, "Cancelled database restoration."); _trycounter = 0; while (_trycounter < 10) { Thread.Sleep(100); Application.DoEvents(); _trycounter += 1; } } else { bool _extracted = _extractordelegate.EndInvoke(_extractorresult); if (_extracted) { if (Directory.Exists(_archivedir)) { string[] _files = Directory.GetFiles(_archivedir); foreach (string _file in _files) { if (Path.GetExtension(_file).ToLower().Replace(".", "").RLTrim() == "sql") { _filename = _file; break; } } if (String.IsNullOrEmpty(_filename.RLTrim())) { AddEvent(BackupEvent.Error, "Could not find any supported database backup file."); _trycounter = 0; while (Directory.Exists(_archivedir) && _trycounter <= 30) { try { Directory.Delete(_archivedir, true); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); return; } } else { AddEvent(BackupEvent.Error, "Could not extract data from database backup."); _trycounter = 0; while (Directory.Exists(_archivedir) && _trycounter <= 30) { try { Directory.Delete(_archivedir, true); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); return; } } else { AddEvent(BackupEvent.Error, "Could not extract data from database backup."); _trycounter = 0; while (Directory.Exists(_archivedir) && _trycounter <= 30) { try { Directory.Delete(_archivedir, true); } catch { } _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); return; } } } if (!String.IsNullOrEmpty(_filename.RLTrim())) { FileInfo _backupfile = new FileInfo(_filename); Func <string, FileInfo, MySqlResult> _restoredelegate = new Func <string, FileInfo, MySqlResult>(MySql.Execute); IAsyncResult _restoreresult = _restoredelegate.BeginInvoke(SCMS.ServerConnection.ToString(), _backupfile, null, _restoredelegate); while (!_restoreresult.IsCompleted && !_cancelled) { Thread.Sleep(1); Application.DoEvents(); } if (_cancelled) { AddEvent(BackupEvent.Warning, "Cancelling database restoration..."); if (_restorepoint != null) { if (File.Exists(_restorepoint.Filename)) { AddEvent(BackupEvent.Information, "Performing roll back from recorded restore point."); FileInfo _restorepointfile = new FileInfo(_restorepoint.Filename); _trycounter = 0; while (_trycounter < 30) { _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } Func <string, FileInfo, MySqlResult> _restorepointdelegate = new Func <string, FileInfo, MySqlResult>(MySql.Execute); IAsyncResult _restorepointresult = _restorepointdelegate.BeginInvoke(SCMS.ServerConnection.ToString(), _restorepointfile, null, _restorepointdelegate); _restorepointresult.WaitToFinish(); MySqlResult _rpresult = _restorepointdelegate.EndInvoke(_restorepointresult); if (_rpresult.Succeeded) { AddEvent(BackupEvent.Success, "Roll back from recorded restore point has been completed."); } else { SCMS.LogError(this.GetType().Name, new Exception(_rpresult.Error)); AddEvent(BackupEvent.Error, "Failed to roll back from restore point."); } } } SCMS.CleanUp(); btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); AddEvent(BackupEvent.Information, "Cancelled database restoration."); _trycounter = 0; while (_trycounter < 30) { Thread.Sleep(100); Application.DoEvents(); _trycounter += 1; } } else { MySqlResult _execresult = _restoredelegate.EndInvoke(_restoreresult); if (_execresult.Succeeded) { AddEvent(BackupEvent.Information, "Finalizing database restoration..."); IAsyncResult _logresult = SCMS.CurrentSystemUser.LogActionAsync(UserAction.RestoreDatabase, "Restored database from file : " + lblPath.Text.ToSqlValidString().Replace("\\\\", "\\") + "."); IAsyncResult _queexresult = Que.BeginExecution(SCMS.ServerConnection.ToString(), "UPDATE `settings` SET `LastRestored` = NOW();"); while (!_logresult.IsCompleted && !_queexresult.IsCompleted) { Thread.Sleep(1); Application.DoEvents(); } QueResult _queresult = Que.EndExecution(_queexresult); _queresult.Dispose(QueResultDisposition.WithAssociatedQue); SCMS.CleanUp(); _isrunning = false; AddEvent(BackupEvent.Success, "Database backup restoration has been completed."); _trycounter = 0; while (_trycounter <= 10) { Thread.Sleep(100); Application.DoEvents(); _trycounter += 1; } MsgBoxEx.Inform("Application will restart for the restored values to fully take effect.", "Database Backup Restoration"); DialogResult = System.Windows.Forms.DialogResult.OK; Close(); } else { SCMS.LogError(this.GetType().Name, new Exception(_execresult.Error)); AddEvent(BackupEvent.Error, "Failed to complete database restoration from the specified database backup / restore point."); if (_restorepoint != null) { if (File.Exists(_restorepoint.Filename)) { AddEvent(BackupEvent.Information, "Performing roll back from recorded restore point."); FileInfo _restorepointfile = new FileInfo(_restorepoint.Filename); _trycounter = 0; while (_trycounter < 15) { _trycounter += 1; Thread.Sleep(100); Application.DoEvents(); } Func <string, FileInfo, MySqlResult> _restorepointdelegate = new Func <string, FileInfo, MySqlResult>(MySql.Execute); IAsyncResult _restorepointresult = _restorepointdelegate.BeginInvoke(SCMS.ServerConnection.ToString(), _restorepointfile, null, _restorepointdelegate); _restorepointresult.WaitToFinish(); MySqlResult _rpresult = _restorepointdelegate.EndInvoke(_restorepointresult); if (_rpresult.Succeeded) { AddEvent(BackupEvent.Success, "Roll back from recorded restore point has been completed."); } else { SCMS.LogError(this.GetType().Name, new Exception(_rpresult.Error)); AddEvent(BackupEvent.Error, "Failed to roll back from restore point."); } } } SCMS.CleanUp(); btnBrowseDrive.Enabled = true; btnBrowseRestorePoint.Enabled = true; btnRestore.Enabled = true; btnCancel.SendToBack(); chkCreateRestorePoint.Enabled = true; pctLoad.Hide(); _isrunning = false; Materia.RefreshAndManageCurrentProcess(); } } } }
private void btnSignIn_Click(object sender, EventArgs e) { if (!btnSignIn.Enabled) { return; } Validator _validator = SCMS.Validators[this]; pnlNotification.Hide(); if (!Materia.Valid(_validator, cboServers, cboServers.SelectedIndex >= 0, "Select a database server.")) { return; } if (!Materia.Valid(_validator, cboCompanies, cboCompanies.SelectedIndex >= 0, "Select access company.")) { return; } SCMS.ServerConnection = null; SCMS.Connection = null; Materia.RefreshAndManageCurrentProcess(); if (String.IsNullOrEmpty(txtUsername.Text.RLTrim()) || String.IsNullOrEmpty(txtPassword.Text.RLTrim())) { pnlNotification.Text = "Invalid account credentials."; pnlNotification.Show(); pnlNotification.BringToFront(); return; } ServerConnectionInfo _info = new ServerConnectionInfo(cboServers.SelectedValue.ToString()); pctLoad.Show(); pctLoad.BringToFront(); btnSignIn.Enabled = false; cboServers.Enabled = false; cboCompanies.Enabled = false; IDbConnection _connection = Database.CreateConnection(_info.ToString()); Func <IDbConnection, bool> _connectdelegate = new Func <IDbConnection, bool>(Materia.CanConnect); IAsyncResult _connectresult = _connectdelegate.BeginInvoke(_connection, null, _connectdelegate); while (!_connectresult.IsCompleted && !_cancelled) { Thread.Sleep(1); Application.DoEvents(); } bool _connected = false; if (_cancelled) { if (!_connectresult.IsCompleted) { try { _connectresult = null; } catch { } finally { Materia.RefreshAndManageCurrentProcess(); } } } else { _connected = _connectdelegate.EndInvoke(_connectresult); if (!_connected) { MsgBoxEx.Alert("Failed to connect to the specified database server.", "Database Connection"); } } if (!_connected) { if (_connection != null) { if (_connection.State == ConnectionState.Open) { try { _connection.Close(); } catch { } } try { _connection.Dispose(); } catch { } _connection = null; Materia.RefreshAndManageCurrentProcess(); } cboServers.Enabled = true; cboCompanies.Enabled = true; btnSignIn.Enabled = true; pctLoad.Hide(); return; } SCMS.Connection = _connection; SCMS.ServerConnection = new ServerConnectionInfo(cboServers.SelectedValue.ToString()); SCMS.CurrentCompany = new CompanyInfo(cboCompanies.SelectedValue.ToString()); Func <IDbConnection, string, DateTime> _getvaluedelegate = new Func <IDbConnection, string, DateTime>(Que.GetValue <DateTime>); IAsyncResult _getvalueresult = _getvaluedelegate.BeginInvoke(_connection, "SELECT MAX(`LastRestored`) AS `Date` FROM `settings`", null, _getvaluedelegate); _getvalueresult.WaitToFinish(); DateTime _dbtimestamp = _getvaluedelegate.EndInvoke(_getvalueresult); DateTime _timestamp = Cache.LastRestoration; if (_dbtimestamp > _timestamp) { Cache.Clear(); Cache.UpdateCacheTimeStamp(_dbtimestamp); } Action <IDbConnection> _inituserdelegate = new Action <IDbConnection>(CreateInitialUser); IAsyncResult _inituserresult = _inituserdelegate.BeginInvoke(SCMS.Connection, null, _inituserdelegate); while (!_inituserresult.IsCompleted && !_cancelled) { Thread.Sleep(1); Application.DoEvents(); } if (_cancelled) { if (!_inituserresult.IsCompleted) { try { _inituserresult = null; } catch { } finally { Materia.RefreshAndManageCurrentProcess(); } } return; } else { Action <IDbConnection> _initcompaniesdelegate = new Action <IDbConnection>(CreateInitialCompanies); IAsyncResult _initcompaniesresult = _initcompaniesdelegate.BeginInvoke(SCMS.Connection, null, _initcompaniesdelegate); while (!_cancelled && !_initcompaniesresult.IsCompleted) { Thread.Sleep(1); Application.DoEvents(); } if (_cancelled) { if (_initcompaniesresult.IsCompleted) { try { _initcompaniesresult = null; } catch { } finally { Materia.RefreshAndManageCurrentProcess(); } } return; } else { SCMS.CurrentSystemUser = null; Materia.RefreshAndManageCurrentProcess(); SystemUserInfo _userinfo = new SystemUserInfo(txtUsername.Text, txtPassword.Text); IAsyncResult _loginasync = _userinfo.LogInAsync(); while (!_loginasync.IsCompleted && !_cancelled) { Thread.Sleep(1); Application.DoEvents(); } if (_cancelled) { if (!_loginasync.IsCompleted) { try { _loginasync = null; } catch { } finally { Materia.RefreshAndManageCurrentProcess(); } } } else { if (_userinfo.IsSignedIn) { SCMS.CurrentSystemUser = _userinfo; InitializerDialog _loader = new InitializerDialog(); _loader.Message = "Gathering application settings for " + SCMS.CurrentCompany.Company + "."; txtPassword.Text = ""; Hide(); _loader.Show(); IAsyncResult _gsresult = GlobalSettings.RefreshAsync(SCMS.CurrentCompany.Company); _gsresult.WaitToFinish(); SCMS.CleanUp(); _loader.Close(); _loader.Dispose(); _loader = null; Materia.RefreshAndManageCurrentProcess(); MainWindow _main = new MainWindow(); _main.Show(); } else { if (_userinfo.IsValidUser && !_userinfo.IsActive) { pnlNotification.Text = "Account needs activation."; pnlNotification.Show(); pnlNotification.BringToFront(); } else { if (_userinfo.IsValidUser && !_userinfo.AccessibleCompanies.Contains(SCMS.CurrentCompany.Company)) { pnlNotification.Text = "Account not allowed in selected company."; pnlNotification.Show(); pnlNotification.BringToFront(); } else { pnlNotification.Text = "Invalid account credentials."; pnlNotification.Show(); pnlNotification.BringToFront(); } } _userinfo = null; Materia.RefreshAndManageCurrentProcess(); } cboCompanies.Enabled = true; cboServers.Enabled = true; btnSignIn.Enabled = true; pctLoad.Hide(); } } } }