Skip to content
This repository was archived by the owner on Apr 6, 2024. It is now read-only.

Commit e210a74

Browse files
committed
Backup quick and dirty - fixes #83
1 parent ed4dc11 commit e210a74

File tree

8 files changed

+87
-9
lines changed

8 files changed

+87
-9
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ <h3 lang="de"><i class="fa fa-arrow-right w3-text-green w3-large"></i> Gesamtein
9595
// there is a newer version of the application available.
9696
updateApplication();
9797
const $ = require( 'jquery' );
98-
loadPage();
98+
loadPage();
9999
</script>
100100
</body>
101101
</html>

main.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ function createWindow () {
7777
if (!mainWindow.isMaximized()) {
7878
var size = mainWindow.getSize();
7979
JSONhandler.storePreference( "windowSize", size[0] + "x" + size[1] );
80-
}
80+
}
81+
82+
if (JSONhandler.readPreference("backup")) {
83+
JSONhandler.createBackup();
84+
}
8185
});
8286

8387
mainWindow.on( 'maximize', function() {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
"electron-installer-dmg": "^0.2.1",
2828
"electron-packager": "^12.2.0",
2929
"jsdoc": "^3.6.3",
30-
"npm": "^6.10.1"
30+
"npm": "^6.10.3"
3131
},
3232
"dependencies": {
3333
"chart.js": "^2.8.0",
34-
"compare-versions": "^3.5.0",
34+
"compare-versions": "^3.5.1",
3535
"electron-is-running-in-asar": "^1.0.0",
3636
"electron-json-storage": "^4.1.6",
3737
"jquery": "^3.4.1",

scripts/JSONhandler.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const os = require( 'os' );
1616
// A default settings.json object.
1717
const defaultObj = {"windowSize":"1920x1080","fullscreen":false,"language":"en",
1818
"path": storage.getDefaultDataPath() + path.sep + "data",
19-
"currency":"Euro","chartType":"pie"};
19+
"currency":"Euro","chartType":"pie","backup":false,"backupFail":false};
2020
// A default mainStorage.json object.
2121
const defaultStorageObj = {"budgets":[["checking account", 0.0]],"currentDate":getCurrentDate(),
2222
"allTimeEarnings":[["checking account", 0.0]],
@@ -32,6 +32,7 @@ var currentFile = readPreference( "path" ) + path.sep + getCurrentFileName();
3232
module.exports.readPreference = ( name ) => readPreference( name );
3333
module.exports.storePreference = ( name, value ) => storePreference( name, value );
3434
module.exports.initStorage = () => initStorage();
35+
module.exports.createBackup = () => createBackup();
3536

3637
/**
3738
* This function initializes the storage. This means, we create a mainStorage.json
@@ -594,4 +595,30 @@ function readJSONFile( filename ) {
594595
function writePDF( pdfPath, data ) {
595596
pdfPath = pdfPath[0] + path.sep + selectedMonth + ".pdf";
596597
fs.writeFileSync( pdfPath, data );
598+
}
599+
600+
/**
601+
* Creates a backup of the data.
602+
*/
603+
function createBackup() {
604+
var allFiles = fs.readdirSync(readPreference("path"));
605+
// Iterate over all the files and create a copy of them.
606+
for (var i = 0; i < allFiles.length; i++) {
607+
if (allFiles[i].endsWith(".json")) {
608+
try { // (Cross disk will cause an error)
609+
var data = fs.readFileSync(readPreference("path") + path.sep + allFiles[i]);
610+
// Only write if file is new or it was changed.
611+
if (fs.existsSync(readPreference("backupPath") + path.sep + allFiles[i])) {
612+
if (!Buffer.from(data).equals(fs.readFileSync(readPreference("backupPath") + path.sep + allFiles[i]))) {
613+
fs.writeFileSync(readPreference("backupPath") + path.sep + allFiles[i], data);
614+
}
615+
} else {
616+
fs.writeFileSync(readPreference("backupPath") + path.sep + allFiles[i], data);
617+
}
618+
} catch (err) {
619+
storePreference("backupFail", true);
620+
break;
621+
}
622+
}
623+
}
597624
}

scripts/controller/index.controller.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ function loadPage() {
1818
// We will always set the language first.
1919
setLanguage( readPreference( "language" ) );
2020
// Display all the other content.
21-
updateView();
21+
updateView();
22+
23+
if (readPreference("backupFail")) {
24+
dialog.showErrorBox("Backup failed", "The backup was not successful last time. Please note that cross-disk is not supported.");
25+
storePreference("backupFail", false);
26+
}
2227
}
2328

2429
/**

scripts/controller/settings.controller.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,27 @@ function setChartType( name ) {
112112
// Update the view to display the new chart type.
113113
updateView();
114114
}
115+
116+
/**
117+
* Acitivates or deactivates backups.
118+
*/
119+
function setBackupMode() {
120+
if ($("#backup").is(":checked")) {
121+
storePreference("backup", true);
122+
123+
var newPath = dialog.showOpenDialog({
124+
properties: ['openDirectory']
125+
});
126+
if (newPath !== null && newPath !== undefined) {
127+
storePreference("backupPath", newPath[0]);
128+
} else {
129+
storePreference("backup", false);
130+
dialog.showErrorBox("Invalid input", "Please select a path!");
131+
$( "#backup" )[0].checked = false;
132+
}
133+
} else {
134+
storePreference("backup", false);
135+
}
136+
137+
updateView();
138+
}

scripts/view/settings.view.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ function updateView() {
4646
// Maybe we need to display checkboxes as checked, if they were previously selected.
4747
if ( readPreference( "fullscreen" ) === true ) {
4848
$( "#fullscreen" )[0].checked = true;
49-
}
49+
}
50+
if ( readPreference( "backup" ) === true ) {
51+
$( "#backup" )[0].checked = true;
52+
}
5053
// Display currently selected path.
5154
$( "#currentPath" ).text( readPreference( "path" ) );
5255
// Display currenctly selected currency.

sites/settings.html

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ <h5 lang="de"><i class="far fa-money-bill-alt w3-text-green w3-large"></i> Währ
103103
<h5 lang="en"><i class="fas fa-chart-pie w3-text-red w3-large"></i> Chart type </h5>
104104
<h5 lang="de"><i class="fas fa-chart-pie w3-text-red w3-large"></i> Diagrammart </h5>
105105
<!-- dropdown menu to select the chart type -->
106-
<div class="w3-dropdown-hover" style="margin-bottom:50px;">
106+
<div class="w3-dropdown-hover">
107107
<button class="w3-button w3-white w3-round-xlarge" id="currentChartType"></button>
108108
<div class="w3-dropdown-content w3-bar-block w3-border">
109109
<a href="#" onclick="setChartType('pie');" class="w3-bar-item w3-button w3-bar-block w3-border" lang="en"> Pie chart </a>
@@ -116,7 +116,22 @@ <h5 lang="de"><i class="fas fa-chart-pie w3-text-red w3-large"></i> Diagrammart
116116
<a href="#" onclick="setChartType('doughnut');" class="w3-bar-item w3-button w3-bar-block w3-border" lang="de"> Ringdiagramm </a>
117117
</div>
118118
</div>
119-
</div>
119+
</div>
120+
<br>
121+
<hr style="border-color:black;">
122+
<div class="w3-row-padding" style="margin:0 -16px; margin-bottom:50px;">
123+
<h5 lang="en"><i class="fas fa-database w3-text-blue w3-large"></i> Backup </h5>
124+
<h5 lang="de"><i class="fas fa-database w3-text-blue w3-large"></i> Backup </h5>
125+
<input type="checkbox" onclick="setBackupMode()" id="backup">
126+
<span lang="en"> Activate Backup </span>
127+
<span lang="de"> Backup aktivieren </span>
128+
<div class="tooltip">
129+
<i class="fas fa-info-circle"></i>
130+
<span class="tooltiptext" lang="de"> Erstellt bei jeder Änderung eine Kopie der Daten. </span>
131+
<span class="tooltiptext" lang="en"> Creates a copy of the data at every change. </span>
132+
</div>
133+
<div id="backupDropdownDiv"></div>
134+
</div>
120135
</div>
121136
<!-- JavaScript initialization -->
122137
<script>

0 commit comments

Comments
 (0)