297 lines
10 KiB
C#
297 lines
10 KiB
C#
///
|
|
/// Copyright (c) 2015 Sensus Metering Systems
|
|
///
|
|
using System;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
using System.Windows.Forms;
|
|
using log4net;
|
|
using Config.Entities;
|
|
using ResultsBrowser.Forms;
|
|
|
|
namespace ResultsBrowser
|
|
{
|
|
public class Program
|
|
{
|
|
public const string HomeDir = "C:\\Tbf\\"; /// Contains subdirectories Results, Logs, Images, ...
|
|
|
|
/// log4net
|
|
static readonly ILog log = LogManager.GetLogger(typeof(Program));
|
|
const string log4netConfigFName = "log4netConfig.xml";
|
|
|
|
/// Local settings
|
|
public static LocalSettings LocalSettings;
|
|
public const string LocalSettingsFileName = "ResultsBrowser.xml";
|
|
public const string LocalSettingsBackupName = "ResultsBrowser.backup.xml";
|
|
|
|
public static System.Globalization.CultureInfo AltCulture;
|
|
|
|
|
|
/// <summary>
|
|
/// Main window object.
|
|
/// </summary>
|
|
public static MainWnd MainWnd;
|
|
|
|
|
|
/// <summary>
|
|
/// The main entry point for the application.
|
|
/// </summary>
|
|
[STAThread]
|
|
static void Main()
|
|
{
|
|
log.Fatal("--------------------------------------------------------------------------------");
|
|
|
|
/// Local program configuration directory including the trailing backslash
|
|
string locAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) +
|
|
Path.DirectorySeparatorChar + "TestBenchFramework" + Path.DirectorySeparatorChar;
|
|
|
|
/// Check whether local application data directory exists.
|
|
/// If not, create it and copy the initial configuration to this directory.
|
|
/// This is done only once after a clean program installation on the first program start-up
|
|
try
|
|
{
|
|
if (!Directory.Exists(locAppDataDir))
|
|
{
|
|
Directory.CreateDirectory(locAppDataDir);
|
|
Environment.CurrentDirectory = locAppDataDir;
|
|
|
|
string sampleConfig = Application.StartupPath + Path.DirectorySeparatorChar + "SampleConfig" + Path.DirectorySeparatorChar;
|
|
if (File.Exists(sampleConfig + LocalSettingsFileName)) { File.Copy(sampleConfig + LocalSettingsFileName, LocalSettingsFileName); }
|
|
if (File.Exists(sampleConfig + log4netConfigFName)) { File.Copy(sampleConfig + log4netConfigFName, log4netConfigFName); }
|
|
if (File.Exists(sampleConfig + Config.Data.SQLiteDbFName)) { File.Copy(sampleConfig + Config.Data.SQLiteDbFName, Config.Data.SQLiteDbFName); }
|
|
|
|
File.SetAttributes(LocalSettingsFileName, FileAttributes.Normal);
|
|
File.SetAttributes(log4netConfigFName, FileAttributes.Normal);
|
|
File.SetAttributes(Config.Data.SQLiteDbFName, FileAttributes.Normal);
|
|
}
|
|
else
|
|
{
|
|
Environment.CurrentDirectory = locAppDataDir;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
log.Error("A problem when creating the local application data directory and preparing the initial configuration", e);
|
|
return;
|
|
}
|
|
|
|
/// Configue and start logging
|
|
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(locAppDataDir + log4netConfigFName));
|
|
log.Info("Entering application.");
|
|
|
|
///
|
|
/// Load the local settings
|
|
///
|
|
LocalSettings = LocalSettings.Load(Program.LocalSettingsFileName);
|
|
if (LocalSettings == null || LocalSettings.TestBenches == null)
|
|
{
|
|
/// Loading local seetings from regular config file failed. Use the backup
|
|
LocalSettings = LocalSettings.Load(Program.LocalSettingsBackupName);
|
|
if (LocalSettings == null || LocalSettings.TestBenches == null)
|
|
{
|
|
if (MessageBox.Show("No program settings found. Do you want to use default settings?",
|
|
"Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
|
{
|
|
return;
|
|
}
|
|
|
|
/// Create new default settings and save them
|
|
LocalSettings = new LocalSettings();
|
|
LocalSettings.Save();
|
|
}
|
|
else
|
|
{
|
|
LocalSettings.Save(); /// Save the settings to overwrite the wrong file
|
|
}
|
|
}
|
|
else
|
|
{
|
|
/// Loading local seetings from the regular config file was successful. Update the backup
|
|
File.Copy(Program.LocalSettingsFileName, Program.LocalSettingsBackupName, true);
|
|
}
|
|
|
|
try
|
|
{
|
|
string culture = LocalSettings.Language.Replace('_', '-');
|
|
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(culture);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
string msg = e.Message;
|
|
MessageBox.Show("Selected language not supported.\nUsing English.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
|
|
System.Threading.Thread.CurrentThread.CurrentUICulture =
|
|
new System.Globalization.CultureInfo("en");
|
|
}
|
|
|
|
//AltCulture = null;
|
|
AltCulture = new System.Globalization.CultureInfo("SK");
|
|
|
|
/// This belongs to Application.Run(new MainWnd()) ...
|
|
/// and should be executed before opening 'loginDlg'
|
|
Application.EnableVisualStyles();
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
|
|
|
/// Ensure that there is at least one bench in the configuration
|
|
while (Program.LocalSettings.BenchesCount == 0)
|
|
{
|
|
log.Warn("No test benches in the local configuration -> display an appropriate dialog.");
|
|
|
|
/// Ask what to do, ask for the password, open 'DatabaseSetingsDlg' and continue on OK
|
|
if ((new Forms.NoBenchOrDatabaseDlg { Message = "No test bench in the program configuration." }.ShowDialog() != DialogResult.OK) ||
|
|
(new LoginDlg(true).ShowDialog() == DialogResult.Cancel) ||
|
|
(new BenchesDlg().ShowDialog() == DialogResult.Cancel))
|
|
{
|
|
return; /// Exit program
|
|
}
|
|
|
|
log.Warn("Loading the local settings again.");
|
|
Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsFileName);
|
|
if (Program.LocalSettings == null) return; /// Fatal error
|
|
}
|
|
|
|
/// User login
|
|
bool retryLogin = true; /// true = stay in a login loop
|
|
do
|
|
{
|
|
LoginDlgWithBenchSelection loginDlgBench;
|
|
if (LocalSettings.LastBenchName != null)
|
|
{
|
|
loginDlgBench = new LoginDlgWithBenchSelection(LocalSettings.LastBenchName);
|
|
}
|
|
else
|
|
{
|
|
loginDlgBench = new LoginDlgWithBenchSelection();
|
|
}
|
|
DialogResult dr = loginDlgBench.ShowDialog();
|
|
|
|
if (dr == DialogResult.Cancel)
|
|
{
|
|
return; /// Login canceled --> Exit application
|
|
}
|
|
else
|
|
{
|
|
for (int i = 0; i < LocalSettings.BenchesCount; i++)
|
|
{
|
|
if (LocalSettings.TestBenches[i].BenchName == loginDlgBench.BenchName)
|
|
{
|
|
User loadedUser = null;
|
|
try
|
|
{
|
|
bool authorized = false;
|
|
|
|
if (User.IsPowerUser(loginDlgBench.User, loginDlgBench.Password))
|
|
{
|
|
loadedUser = new User(loginDlgBench.User);
|
|
authorized = loadedUser.Authorize(loginDlgBench.User, loginDlgBench.Password);
|
|
}
|
|
|
|
if (!authorized)
|
|
{
|
|
/// Load user from the UsersAndGroupsDB database
|
|
loadedUser = User.LoadUserByName(loginDlgBench.User,
|
|
LocalSettings.TestBenches[i].ProceduresDBSettings);
|
|
if (loadedUser != null)
|
|
{
|
|
authorized = loadedUser.Authorize(loginDlgBench.User, loginDlgBench.Password);
|
|
}
|
|
}
|
|
|
|
if (authorized)
|
|
{
|
|
log.InfoFormat("Password accepted, bench '{0}' parameters loaded.", LocalSettings.TestBenches[i].BenchName);
|
|
|
|
/// Copy the selected bench settings to CurrentBench.
|
|
/// Clone() guarantees that current bench settings wont be modified
|
|
/// when user modifies the database settings in DatabaseSettingsDlg.
|
|
Config.Data.CurrentBench = (Config.DatabaseSettings)LocalSettings.TestBenches[i].Clone();
|
|
|
|
/// Save the selection to local settings
|
|
LocalSettings.LastBenchName = Config.Data.CurrentBench.BenchName;
|
|
LocalSettings.Save();
|
|
|
|
Results.DB.DbType = Config.Data.CurrentBench.WaterMetersDBSettings.DbType;
|
|
Results.DB.ConnectionString = Config.Data.CurrentBench.WaterMetersDBSettings.ConnectionString;
|
|
|
|
retryLogin = false;
|
|
break;
|
|
}
|
|
|
|
if (retryLogin)
|
|
{
|
|
/// Close Fluent NHibernate
|
|
MessageBox.Show("Invalid user, password, or bench", "Error", MessageBoxButtons.OK);
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
/// Database connect failed, ask what to do
|
|
switch ((new Forms.NoBenchOrDatabaseDlg { Message = "Cannot connect to the database.", ShowRetry = true }).ShowDialog())
|
|
{
|
|
case DialogResult.Abort:
|
|
return; /// Exit program
|
|
///
|
|
case DialogResult.Retry:
|
|
break; /// Retry the loop
|
|
///
|
|
default:
|
|
/// Open 'DatabaseSetingsDlg' and retry DB connect on OK
|
|
if (new LoginDlg(true).ShowDialog() == DialogResult.Cancel ||
|
|
new BenchesDlg().ShowDialog() == DialogResult.Cancel)
|
|
{
|
|
return; /// Exit program
|
|
}
|
|
Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsFileName);
|
|
if (Program.LocalSettings == null) return; /// Fatal error
|
|
break; /// Retry the loop
|
|
}
|
|
}
|
|
} // if benchname is OK
|
|
} // for all benches in local settings loop
|
|
} // not Canceled
|
|
} //do
|
|
while (retryLogin);
|
|
|
|
|
|
try
|
|
{
|
|
/// Open the main application window
|
|
log.Info("Creating the main window");
|
|
MainWnd = new MainWnd();
|
|
log.Info("Opening the main window");
|
|
Application.Run(MainWnd);
|
|
log.Info("The main window was closed");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogException(log, "Exception in Application.Run(MainWnd)", e);
|
|
}
|
|
finally
|
|
{
|
|
/// Save local settings
|
|
LocalSettings.Save();
|
|
}
|
|
|
|
/// Close Fluent NHibernate
|
|
/// Todo
|
|
|
|
log.Info("Exiting application.");
|
|
}
|
|
|
|
static void MyHandler(object sender, UnhandledExceptionEventArgs args)
|
|
{
|
|
LogException(log, "Unhandled exception", (Exception)args.ExceptionObject);
|
|
}
|
|
|
|
static void LogException(ILog log, string description, Exception e)
|
|
{
|
|
log.FatalFormat("---------------( {0} )---------------", description);
|
|
log.FatalFormat("Message : {0}", e.Message);
|
|
if (e.InnerException != null)
|
|
{
|
|
log.FatalFormat("InnerMessage : {0}", e.InnerException.Message);
|
|
}
|
|
log.FatalFormat("StackTrace : {0}{1}", Environment.NewLine, e.StackTrace);
|
|
log.Fatal("--------------------------------------");
|
|
}
|
|
}
|
|
} |