Skip to content

Commit

Permalink
Merge pull request #3 from Invoices-Manager/dev_01
Browse files Browse the repository at this point in the history
version 1.4.4.0
  • Loading branch information
Schecher1 authored Mar 17, 2023
2 parents 8501e77 + 908e0f2 commit bd759ff
Show file tree
Hide file tree
Showing 18 changed files with 200 additions and 61 deletions.
3 changes: 2 additions & 1 deletion Classes/Enums/LogPrefixEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ enum LogPrefixEnum
Sort_System,
Security_System,
Template_System,
Language_System,


About_View,
Expand All @@ -19,6 +20,6 @@ enum LogPrefixEnum
Invoice_View,
MainWindow_View,
Notebook_View,
Setting_View,
Setting_View
}
}
2 changes: 1 addition & 1 deletion Classes/EnvironmentsVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class EnvironmentsVariable

public static string UILanguage = "English";
public static string[] PossibleUILanguages = { "English", "German" };
public const string PROGRAM_VERSION = "1.4.3.0";
public const string PROGRAM_VERSION = "1.4.4.0";
public const string PROGRAM_LICENSE = "License: Open source"; // Placeholder
public static string ConfigVersion;
public const string PROGRAM_SUPPORTEDFORMAT = ".pdf";
Expand Down
3 changes: 2 additions & 1 deletion Classes/GlobalUsing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
global using System.Threading.Tasks;
global using System.Windows.Data;
global using System.Windows.Input;
global using System.Windows.Threading;
global using System.Windows.Threading;
global using System.Security.Cryptography;
2 changes: 2 additions & 0 deletions Classes/LanguageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public static void Init()
};

Application.Current.Resources.MergedDictionaries.Add(dict);

LoggerSystem.Log(LogStateEnum.Info, LogPrefixEnum.Language_System, "Language system has been initialized.");
}
}
}
73 changes: 60 additions & 13 deletions Core/BackUpSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class BackUpSystem

public void CheckBackUpCount()
{
LoggerSystem.Log(LogStateEnum.Info, LogPrefixEnum.BackUp_System, "CheckBackUpCount has been called.");

try
{
//get all files in the backup folder
Expand All @@ -18,22 +20,35 @@ public void CheckBackUpCount()
//if its zero or negativ => no files to delete
int hasToDeleteCounter = files.Length - EnvironmentsVariable.MaxCountBackUp;

if (hasToDeleteCounter > 0)
//delete the oldest files
for (int i = 0; i < hasToDeleteCounter; i++)
try { File.Delete(files[i]); } catch { }

if (!(hasToDeleteCounter > 0))
return;


LoggerSystem.Log(LogStateEnum.Info, LogPrefixEnum.BackUp_System, "CheckBackUpCount has been found " + hasToDeleteCounter + " files to delete.");
//delete the oldest files
for (int i = 0; i < hasToDeleteCounter; i++)
try
{
File.Delete(files[i]);
LoggerSystem.Log(LogStateEnum.Info, LogPrefixEnum.BackUp_System, "CheckBackUpCount has been deleted the file: (autobackup overflow)" + files[i]);
}
catch { }
}
catch (Exception ex)
{
LoggerSystem.Log(LogStateEnum.Error, LogPrefixEnum.BackUp_System, ex.Message);
LoggerSystem.Log(LogStateEnum.Error, LogPrefixEnum.BackUp_System, "CheckBackUpCount has been failed. err: " + ex.Message);
}
}

public bool BackUp(string backupFilePath, BackUpView backUpMgr = null)
{
bool wasPerformedCorrectly = false;
InvoiceSystem iSys = new InvoiceSystem();


LoggerSystem.Log(LogStateEnum.Info, LogPrefixEnum.BackUp_System, "BackUp has been called.");
DateTime start = DateTime.Now;

try
{
//set the main window for the progress bar
Expand Down Expand Up @@ -125,15 +140,21 @@ public bool BackUp(string backupFilePath, BackUpView backUpMgr = null)
}
catch (Exception ex)
{
LoggerSystem.Log(LogStateEnum.Error, LogPrefixEnum.BackUp_System, ex.Message);
LoggerSystem.Log(LogStateEnum.Error, LogPrefixEnum.BackUp_System, "BackUp has been failed. err: " + ex.Message);
return false;
}

DateTime stop = DateTime.Now;
LoggerSystem.Log(LogStateEnum.Info, LogPrefixEnum.BackUp_System, $"BackUp has been finished. Took: {(stop - start).TotalMilliseconds} ms, that is in seconds: {(stop - start).TotalSeconds} s");

return wasPerformedCorrectly;
}

public bool Restore(string backupFilePath, BackUpView backUpMgr = null)
{
LoggerSystem.Log(LogStateEnum.Info, LogPrefixEnum.BackUp_System, "Restore has been called.");
DateTime start = DateTime.Now;

//set the main window for the progress bar
if (backUpMgr != null)
_backUpMgrView = backUpMgr;
Expand Down Expand Up @@ -277,7 +298,7 @@ public bool Restore(string backupFilePath, BackUpView backUpMgr = null)
}
catch (Exception ex)
{
LoggerSystem.Log(LogStateEnum.Error, LogPrefixEnum.BackUp_System, ex.Message);
LoggerSystem.Log(LogStateEnum.Error, LogPrefixEnum.BackUp_System, "Error while restoring, err: " + ex.Message);
wasPerformedCorrectly = false;
}

Expand All @@ -287,14 +308,20 @@ public bool Restore(string backupFilePath, BackUpView backUpMgr = null)

//delete all temp files
foreach (string file in allTempFiles)
try { File.Delete(file); } catch { }
try
{
LoggerSystem.Log(LogStateEnum.Debug, LogPrefixEnum.BackUp_System, $"Deleting temp file: {file}");
File.Delete(file);
} catch { }

//must be, if the backup is from a older version (v1.2.3.2 or older)
int noteCount = 0;

if (backUp.Notebook != null)
noteCount = backUp.Notebook.Notebook.Count;

DateTime stop = DateTime.Now;
LoggerSystem.Log(LogStateEnum.Info, LogPrefixEnum.BackUp_System, $"Restore has been finished. Took: {(stop - start).TotalMilliseconds} ms, that is in seconds: {(stop - start).TotalSeconds} s");

//show the results
MessageBox.Show($"{backUp.EntityCount - (invoice_AlreadyExistCounter + invoice_WasOverwrittenCounter)} {Application.Current.Resources["invoicesWereRestored"] as string}" + Environment.NewLine +
Expand All @@ -308,11 +335,15 @@ public bool Restore(string backupFilePath, BackUpView backUpMgr = null)
$"{Application.Current.Resources["fromTotal"] as string} {noteCount} {Application.Current.Resources["notes"] as string}",
Application.Current.Resources["recoveryCompleted"] as string, MessageBoxButton.OK);


return wasPerformedCorrectly;
}

public bool SaveAs(string backupFilePath, string newPath)
{
LoggerSystem.Log(LogStateEnum.Info, LogPrefixEnum.BackUp_System, $"SaveAs() has been called");
LoggerSystem.Log(LogStateEnum.Info, LogPrefixEnum.BackUp_System, $"Save as: {backupFilePath} to {newPath}");

//return if the backup not exist
if (!File.Exists(backupFilePath))
{
Expand All @@ -329,13 +360,15 @@ public bool SaveAs(string backupFilePath, string newPath)

//saves the backup to the new path
File.Copy(backupFilePath, newPath);
LoggerSystem.Log(LogStateEnum.Debug, LogPrefixEnum.BackUp_System, "");


return true;
}

public async IAsyncEnumerable<BackUpInfoModel> GetBackUps()
{
LoggerSystem.Log(LogStateEnum.Info, LogPrefixEnum.BackUp_System, $"GetBackUps() has been called");
DateTime start = DateTime.Now;

BackUpSystem buSys = new BackUpSystem();

//get all files in the backup folder
Expand All @@ -354,10 +387,16 @@ public async IAsyncEnumerable<BackUpInfoModel> GetBackUps()
//yield the result
yield return backUpMetaData;
}

DateTime stop = DateTime.Now;
LoggerSystem.Log(LogStateEnum.Info, LogPrefixEnum.BackUp_System, $"GetBackUps() has been finished. Took: {(stop - start).TotalMilliseconds} ms, that is in seconds: {(stop - start).TotalSeconds} s");
}

private BackUpInfoModel GetBackUpMetaData(string file)
{
LoggerSystem.Log(LogStateEnum.Info, LogPrefixEnum.BackUp_System, $"GetBackUpMetaData() has been called");
DateTime start = DateTime.Now;

try
{
LoggerSystem.Log(LogStateEnum.Debug, LogPrefixEnum.BackUp_System, $"Get meta data from backup file: {file}");
Expand Down Expand Up @@ -391,18 +430,23 @@ private BackUpInfoModel GetBackUpMetaData(string file)
//get file path
backUpMetaData.BackUpPath = file;

DateTime stop = DateTime.Now;
LoggerSystem.Log(LogStateEnum.Info, LogPrefixEnum.BackUp_System, $"GetBackUpMetaData() has been finished. Took: {(stop - start).TotalMilliseconds} ms");

return backUpMetaData;
}
catch (Exception ex)
{
LoggerSystem.Log(LogStateEnum.Error, LogPrefixEnum.BackUp_System, $"The BackUp file is corrupted, abort the process! {file} Error: {ex.Message}");
LoggerSystem.Log(LogStateEnum.Error, LogPrefixEnum.BackUp_System, $"The BackUp file is corrupted, abort the process! {file} err: {ex.Message}");

return new BackUpInfoModel { };
}
}

public static string GetFileSize(string path)
{
LoggerSystem.Log(LogStateEnum.Info, LogPrefixEnum.BackUp_System, $"GetFileSize() has been called");

long size = new FileInfo(path).Length;

string[] suf = { "B", "KB", "MB", "GB", "TB", "PB", "EB" }; //Longs run out around EB
Expand All @@ -416,15 +460,18 @@ public static string GetFileSize(string path)

public bool Delete(string backUpPath)
{
LoggerSystem.Log(LogStateEnum.Info, LogPrefixEnum.BackUp_System, $"Delete() has been called");

//delete the backup
try
{
LoggerSystem.Log(LogStateEnum.Debug, LogPrefixEnum.BackUp_System, $"Delete backup file: {backUpPath}");
File.Delete(backUpPath);
return true;
}
catch (Exception ex)
{
LoggerSystem.Log(LogStateEnum.Error, LogPrefixEnum.BackUp_System, ex.Message);
LoggerSystem.Log(LogStateEnum.Error, LogPrefixEnum.BackUp_System, "Could not delete the backup file! err:" + ex.Message);
return false;
}
}
Expand Down
23 changes: 18 additions & 5 deletions Core/ConfigSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public class ConfigSystem
{
public void Init()
{
LoggerSystem.Log(LogStateEnum.Info, LogPrefixEnum.Config_System, "Config init has been started.");

try
{
string json = String.Empty;
Expand Down Expand Up @@ -39,19 +41,21 @@ public void Init()
EnvironmentsVariable.MoneyUnit = config.MoneyUnit;
EnvironmentsVariable.CreateABackupEveryTimeTheProgramStarts = config.CreateABackupEveryTimeTheProgramStarts;
EnvironmentsVariable.MaxCountBackUp = config.MaxCountBackUp;

//if the owner starts this program with an older config version, then by default there is NULL, and the program would crash at init
if (config.ColumnVisibility is not null)
EnvironmentsVariable.ColumnVisibility = config.ColumnVisibility;

Save();
SaveIntoJsonFile();
LoggerSystem.Log(LogStateEnum.Info, LogPrefixEnum.Config_System, "Config init has been finished.");
}
catch (Exception ex)
{
LoggerSystem.Log(LogStateEnum.Error, LogPrefixEnum.Config_System, ex.Message);
LoggerSystem.Log(LogStateEnum.Error, LogPrefixEnum.Config_System, "Config init has been failed. err: " + ex.Message);
}
}

public void Save()
public void SaveIntoJsonFile()
{
ConfigModel config = new ConfigModel()
{
Expand All @@ -66,8 +70,17 @@ public void Save()
MaxCountBackUp = EnvironmentsVariable.MaxCountBackUp,
ColumnVisibility = EnvironmentsVariable.ColumnVisibility
};

File.WriteAllText(EnvironmentsVariable.PathConfig + EnvironmentsVariable.ConfigJsonFileName, JsonConvert.SerializeObject(config, Formatting.Indented));

LoggerSystem.Log(LogStateEnum.Debug, LogPrefixEnum.Config_System, "SaveIntoJsonFile() has been called");

try
{
File.WriteAllText(EnvironmentsVariable.PathConfig + EnvironmentsVariable.ConfigJsonFileName, JsonConvert.SerializeObject(config, Formatting.Indented));
}
catch (Exception ex)
{
LoggerSystem.Log(LogStateEnum.Error, LogPrefixEnum.Config_System, $"Error saving changes to the config file, err: {ex.Message}");
}
}
}
}
Loading

0 comments on commit bd759ff

Please sign in to comment.