151 lines
6.5 KiB
C#
151 lines
6.5 KiB
C#
///
|
|
/// Copyright (c) 2017-2019 Sensus Metering Systems
|
|
///
|
|
using System;
|
|
using System.IO;
|
|
using System.Windows.Forms;
|
|
using log4net;
|
|
|
|
namespace UserManagement
|
|
{
|
|
static class Program
|
|
{
|
|
static readonly ILog log; /// log4net
|
|
|
|
/// Program information
|
|
public static string Version; /// version string
|
|
public static DateTime BuildDateTime; /// date and time of program build
|
|
|
|
/// Local settings
|
|
public static readonly string ExecutableDirectory;
|
|
public static readonly string ConfigDirectory;
|
|
public static readonly string LogDirectory;
|
|
public static readonly string LocalSettingsFileName;
|
|
public static readonly string LocalSettingsBackupName;
|
|
public static LocalSettings LocalSettings;
|
|
|
|
/// <summary>
|
|
/// Initializes static variables above and starts logging.
|
|
/// </summary>
|
|
static Program()
|
|
{
|
|
/// Program version string
|
|
System.Reflection.Assembly thisAssembly = System.Reflection.Assembly.GetExecutingAssembly();
|
|
Version ver = thisAssembly.GetName().Version;
|
|
Version = string.Format("{0}.{1}.{2}", ver.Major, ver.Minor, ver.Build);
|
|
BuildDateTime = new System.IO.FileInfo(thisAssembly.Location).LastWriteTime;
|
|
|
|
/// Local program configuration directory including the trailing backslash
|
|
ExecutableDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
|
|
ConfigDirectory = Path.Combine(ExecutableDirectory, "..\\Cfg\\");
|
|
LogDirectory = Path.Combine(ExecutableDirectory, "..\\Logs\\");
|
|
LocalSettingsFileName = ConfigDirectory + "config.xml";
|
|
LocalSettingsBackupName = ConfigDirectory + "config.backup.xml";
|
|
|
|
/// Configue and start logging
|
|
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(ConfigDirectory + "log4netConfig.xml"));
|
|
log = LogManager.GetLogger(typeof(Program));
|
|
log.Fatal("------------------------------------------------------------------------------------------------");
|
|
log.Fatal("started");
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// The main entry point for the application.
|
|
/// </summary>
|
|
[STAThread]
|
|
static void Main()
|
|
{
|
|
/// This belongs to Application.Run(new MainWnd()) ...
|
|
/// and should be executed before opening 'loginDlg'
|
|
Application.EnableVisualStyles();
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
|
|
|
bool authorized = false;
|
|
do
|
|
{
|
|
///
|
|
/// Load the local settings
|
|
///
|
|
Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsFileName);
|
|
if (Program.LocalSettings == null)
|
|
{
|
|
/// Loading local seetings from regular config file failed. Use the backup
|
|
Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsBackupName);
|
|
if (Program.LocalSettings == null)
|
|
{
|
|
MessageBox.Show("Na tomto počítači nebol tento program ani raz spustený." +
|
|
Environment.NewLine + "Použijú sa základné nastavenia.",
|
|
"Upozornenie", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
Program.LocalSettings = new LocalSettings();
|
|
Directory.CreateDirectory(ConfigDirectory);
|
|
Program.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
|
|
{
|
|
Users.DB.ConnectionString = Program.LocalSettings.ConnectionString;
|
|
Users.DB.DbType = Common.DBType.MySql;
|
|
Users.DB.LoadSharedData();
|
|
|
|
authorized = true; /// No LoginDlg, authorized when reading users from the DB is successfull
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
MessageBox.Show(string.Format("Cannot read a list of users from the database:{0}{1}", Environment.NewLine, exc.Message),
|
|
"Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
|
|
if (new DatabaseSettingsDlg().ShowDialog() == DialogResult.Cancel) return; /// 'OK' = stay in the loop, 'Cancel' = exit loop
|
|
}
|
|
}
|
|
while (!authorized);
|
|
|
|
|
|
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");
|
|
}
|
|
|
|
|
|
try
|
|
{
|
|
Users.Forms.UserManagementDlg dlg = new Users.Forms.UserManagementDlg(Users.DB.CreateSession(), false);
|
|
|
|
string connStr = LocalSettings.ConnectionString;
|
|
int len = connStr.ToUpper().IndexOf("; UID=");
|
|
if (len == -1) len = connStr.ToUpper().IndexOf("; USER=");
|
|
if (len > 0) dlg.TitleExtension = connStr.Substring(0, len);
|
|
Application.Run(dlg);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
log.FatalFormat("Program havaroval:{0}{1}Stack trace:{0}{2}", Environment.NewLine, exc.Message, exc.StackTrace);
|
|
|
|
MessageBox.Show("Program havaroval:" +
|
|
Environment.NewLine + Environment.NewLine + exc.Message +
|
|
((exc.InnerException == null) ? string.Empty : (Environment.NewLine + exc.InnerException.Message)) +
|
|
Environment.NewLine + exc.StackTrace,
|
|
"Pozor!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
}
|
|
}
|
|
}
|
|
}
|