forked from DRossyCPNV/LifeProManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
107 lines (86 loc) · 3.23 KB
/
Copy pathProgram.cs
File metadata and controls
107 lines (86 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/// <file>Program.cs</file>
/// <author>Laurent Barraud, David Rossy and Julien Terrapon</author>
/// <version>1.8.3</version>
/// <date>July 5th, 2026</date>
using Microsoft.Data.Sqlite;
using System;
using System.IO;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace LifeProManager
{
static class Program
{
[DllImport("kernel32.dll")]
public static extern bool AllocConsole();
public static ApplicationContext appContext = null!;
public static DBConnection DbConn = null!;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
ApplyLocalization();
// Initializes database before creating the DBConnection
InitializeDatabase();
// Creates the global DB connection after the DB is ready
DbConn = new DBConnection();
// Starts the application
appContext = new ApplicationContext(new frmMain());
Application.Run(appContext);
}
private static void ApplyLocalization()
{
string lang = Properties.Settings.Default.appLanguageCode;
if (!string.IsNullOrEmpty(lang))
{
LocalizationManager.SetLanguage(lang);
}
}
/// <summary>
/// Initializes the database by checking if it exists and creating it if necessary.
/// If the database exists, it checks its integrity and recreates it if it's invalid.
/// </summary>
private static void InitializeDatabase()
{
string dbFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"LifeProManager");
Directory.CreateDirectory(dbFolder);
string dbPath = Path.Combine(dbFolder, "LPM_DB.db");
// If the database file does not exist
if (!File.Exists(dbPath))
{
// Creates the empty database file
using (var conn = new SqliteConnection($"Data Source={dbPath}"))
{
conn.Open();
}
// Creates tables using a temporary DBConnection
new DBConnection().CreateTablesAndInsertInitialData();
return;
}
// If the database file exists, check its integrity
DbConn = new DBConnection();
bool dbValid = DbConn.CheckDBIntegrity();
if (!dbValid)
{
new DBConnection().CreateTablesAndInsertInitialData();
}
}
public static void SwitchMainForm(Form newForm)
{
if (appContext.MainForm is Form oldForm)
{
newForm.StartPosition = FormStartPosition.Manual;
newForm.Left = oldForm.Left;
newForm.Top = oldForm.Top;
newForm.Width = oldForm.Width;
newForm.Height = oldForm.Height;
newForm.WindowState = oldForm.WindowState;
appContext.MainForm = newForm;
newForm.Show();
oldForm.Close();
}
}
}
}