From 63bdd1263639d6f75fb142b5ab828cd885609a5d Mon Sep 17 00:00:00 2001
From: HGEpro <tstudio2007@gmail.com>
Date: Tue, 29 Mar 2022 20:34:09 +0200
Subject: [PATCH] fixing bugs (failed)

---
 app/public/index.html      | 10 ----------
 app/src-tauri/Cargo.toml   |  2 +-
 app/src-tauri/src/main.rs  |  1 +
 app/src-tauri/src/utils.rs | 12 ++++++++++++
 app/src/App.svelte         | 23 +++++++++++++++++++----
 app/src/configmanager.js   |  6 ++++--
 scripts/install.sh         |  3 +++
 7 files changed, 40 insertions(+), 17 deletions(-)

diff --git a/app/public/index.html b/app/public/index.html
index bb9d03f..cf8dfe1 100644
--- a/app/public/index.html
+++ b/app/public/index.html
@@ -5,16 +5,6 @@
 	<meta name='viewport' content='width=device-width,initial-scale=1'>
 
 	<title>LibreHomework</title>
-	<script>
-		const invoke = window.__TAURI__.invoke;
-		console.log(invoke);
-
-		document.addEventListener('DOMContentLoaded', () => {
-  
-  			invoke('close_splashscreen')
-		})
-
-	</script>
 	<link rel='icon' type='image/png' href='/favicon.png'>
 	<link rel='stylesheet' href='/global.css'>
 	<link rel='stylesheet' href='/build/bundle.css'>
diff --git a/app/src-tauri/Cargo.toml b/app/src-tauri/Cargo.toml
index c28797c..bc421d4 100755
--- a/app/src-tauri/Cargo.toml
+++ b/app/src-tauri/Cargo.toml
@@ -23,4 +23,4 @@ reqwest = {version = "0.11", features = ["json"] }
 
 [features]
 default = [ "custom-protocol" ]
-custom-protocol = [ "tauri/custom-protocol" ]
\ No newline at end of file
+custom-protocol = [ "tauri/custom-protocol" ]
diff --git a/app/src-tauri/src/main.rs b/app/src-tauri/src/main.rs
index 7f3ea3d..1cafbfd 100755
--- a/app/src-tauri/src/main.rs
+++ b/app/src-tauri/src/main.rs
@@ -58,6 +58,7 @@ fn main() {
       write_config_file,
       read_config_file,
       close_splashscreen,
+      get_syslang,
     ])
     .build(tauri::generate_context!())
     .expect("Error while running LibreHomework");
diff --git a/app/src-tauri/src/utils.rs b/app/src-tauri/src/utils.rs
index 3534c62..4168b57 100755
--- a/app/src-tauri/src/utils.rs
+++ b/app/src-tauri/src/utils.rs
@@ -9,6 +9,18 @@ pub fn stop_app(msg: &str) -> ! {
     std::process::exit(1)
 }
 
+#[tauri::command]
+pub fn get_syslang() -> String {
+
+  let locale = if let Ok(lang) = std::env::var("LANG") {
+    lang[..2].to_string()
+  } else {
+    "en".to_string()
+  };
+  return locale
+
+}
+
 #[tauri::command]
 pub fn read_config_file() -> Option<String> {
     let mut file = File::open(config_dir()?.join("LibreHomework/config.json")).ok()?;
diff --git a/app/src/App.svelte b/app/src/App.svelte
index 174e098..8b51c1a 100755
--- a/app/src/App.svelte
+++ b/app/src/App.svelte
@@ -40,11 +40,24 @@ let timedLockSwitchIsDisabled = false;
 let settings = {};
 
 async function setup() {
-	let lang = JSON.parse(await conf.readConfig()).misc.lang || "es";
+	try {
+		let lang = await conf.readConfig().misc.lang
+	} catch (e) {
+		console.log(e);
+		await conf.writeConfig(defaultConfig);
+		let lang = await conf.readConfig().misc.lang
+	}
+	
 	currentLang = lang;
-	return locales[lang];
+	try {
+		console.log(locales[lang])
+		return locales[lang];
+	} catch (e) {
+		console.log(locales["en"])
+		return locales["en"];
+	}
 }
-
+/*
 async function getLangs() {
 	let langs = [];
 	langs.push( JSON.parse(await conf.readConfig()).misc.lang );
@@ -53,6 +66,7 @@ async function getLangs() {
 }
 
 window.getlangs = getLangs;
+*/
 
 function saveSettings() {
 	let newSettings = defaultConfig;
@@ -326,7 +340,7 @@ function saveSettings() {
 			<div class="empty bg-dark">
 				<div class="empty-icon">
 					<i class="icon icon-3x icon-cross"></i>
-				</div>
+					</div>
 				<p class="empty-title h5">{dict.tasks_error}</p>
 			</div>
 		{/await}
@@ -366,6 +380,7 @@ function saveSettings() {
 				<i class="icon icon-3x icon-cross"></i>
 			</div>
 			<p class="empty-title h5">Could not load language data</p>
+			<p>{error.message}</p>
 			<p class="empty-subtitle">Try searching for this issue on our GitHub repo. If you can not find a GitHub issue around this, please create one.</p>
 		</div>
 	{/await}
diff --git a/app/src/configmanager.js b/app/src/configmanager.js
index e24e3dc..509ce09 100644
--- a/app/src/configmanager.js
+++ b/app/src/configmanager.js
@@ -1,6 +1,8 @@
 import { invoke } from "@tauri-apps/api/tauri";
 
-export const defaultConfig = { "misc": {"lang": "es"}, "colors": {"primary": "#3942ed", "secondary": "5056c7"}}
+let syslang = invoke("get_syslang").then(lang => {return lang});
+
+export const defaultConfig = {"misc": {"lang": syslang}, "colors": {"primary": "#3942ed", "secondary": "5056c7"}}
 
 export class ConfigManager {
     async initDefaultConfig() {
@@ -13,7 +15,7 @@ export class ConfigManager {
     }
 
     async writeConfig(config) {
-        // Actually this is not very safe, it might lead to a little self-XSS vuln, but anyway... 😳
+
         return await invoke("write_config_file", {"contents": JSON.stringify(
             config, null, 4)});
     }
diff --git a/scripts/install.sh b/scripts/install.sh
index e036b4b..b1981b4 100755
--- a/scripts/install.sh
+++ b/scripts/install.sh
@@ -17,6 +17,7 @@ select bin in "${bin_types[@]}"; do
             curl -L -s $source -o "LibreHomework-Source_${LATVER}.zip"
             echo "Extracting zip file..."
             unzip "LibreHomework-Source_${LATVER}.zip" > /dev/null
+            rm "LibreHomework-Source_${LATVER}.zip"
             echo "Done, you can now compile it by typing cd LibreHomework-Source_${LATVER} && npm i && npm run tauri build"
             break
             ;;
@@ -25,6 +26,7 @@ select bin in "${bin_types[@]}"; do
             curl -L -s $deb -o "LibreHomework_${LATVER}.deb"
             echo "Enter the password to install the package:"
             sudo dpkg -i "LibreHomework_${LATVER}.deb" > /dev/null
+            rm "LibreHomework_${LATVER}.deb"
             echo "Done, you can now launch it from the menu or the command line with libre-homework"
             break
             ;;
@@ -46,6 +48,7 @@ done
 echo "Do you want to download, install and configure the daemon? (y/n)"
 read daemon
 if [ $daemon = "y" ]; then
+
     echo "Downloading daemon executable..."
     curl -L -s "https://github.com/HGEpro/LibreHomework/releases/download/${LATVER}/librehomework-daemon_v1.0.0_amd64" -o "librehomework-daemon_${LATVER}_amd64"
     sudo mv "librehomework-daemon_${LATVER}_amd64" /usr/bin/librehomeworkd
-- 
GitLab