From d836ed2da5b097e50a97101a101667182ffc89c2 Mon Sep 17 00:00:00 2001 From: zoff99 Date: Tue, 5 Dec 2023 12:48:02 +0100 Subject: [PATCH 1/3] use a proper system depenable directory for user data --- .../applications/trifa/TRIFAGlobals.java | 4 +-- src/main/kotlin/Main.kt | 1 + .../zoffcc/applications/trifa/MainActivity.kt | 8 ++++++ .../trifa/savepathenabled_state.kt | 27 +++++++++++++++---- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/zoffcc/applications/trifa/TRIFAGlobals.java b/src/main/java/com/zoffcc/applications/trifa/TRIFAGlobals.java index 733cd1eb..92770b23 100644 --- a/src/main/java/com/zoffcc/applications/trifa/TRIFAGlobals.java +++ b/src/main/java/com/zoffcc/applications/trifa/TRIFAGlobals.java @@ -138,9 +138,9 @@ public class TRIFAGlobals static long MESSAGE_V2_MSG_SENT_OK = (Long.MAX_VALUE - 1); static int global_self_connection_status = ToxVars.TOX_CONNECTION.TOX_CONNECTION_NONE.value; - final static String VFS_TMP_FILE_DIR = "./tempdir/files/"; + static String VFS_TMP_FILE_DIR = "./tempdir/files/"; // final static String VFS_TMP_AVATAR_DIR = "/avatar_tempdir/files/"; // TODO: avatar should get their own directory! - public final static String VFS_FILE_DIR = "./datadir/files/"; + public static String VFS_FILE_DIR = "./datadir/files/"; final static String VFS_OWN_AVATAR_DIR = "./datadir/myavatar/"; static String VFS_PREFIX = ""; // only set for normal (unencrypted) storage diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index c3de9c17..c383228b 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -258,6 +258,7 @@ fun App() println("User data dir: " + APPDIRS.getUserDataDir()) println("User data dir (roaming): " + APPDIRS.getUserDataDir(roaming = true)) + savepathstore.updatePath(APPDIRS.getUserDataDir(roaming = true)) println("User config dir: " + APPDIRS.getUserConfigDir()) println("User config dir (roaming): " + APPDIRS.getUserConfigDir(roaming = true)) println("User cache dir: " + APPDIRS.getUserCacheDir()) diff --git a/src/main/kotlin/com/zoffcc/applications/trifa/MainActivity.kt b/src/main/kotlin/com/zoffcc/applications/trifa/MainActivity.kt index b49541a5..40ea640b 100644 --- a/src/main/kotlin/com/zoffcc/applications/trifa/MainActivity.kt +++ b/src/main/kotlin/com/zoffcc/applications/trifa/MainActivity.kt @@ -304,6 +304,14 @@ class MainActivity } catch (_: Exception) { } + + try + { + PrintWriter("toxid.txt", "UTF-8").use { out -> out.write(my_tox_id_temp) } + Log.i(TAG, "also writing toxid to current directory: " + "toxid.txt") + } catch (_: Exception) + { + } } init diff --git a/src/main/kotlin/com/zoffcc/applications/trifa/savepathenabled_state.kt b/src/main/kotlin/com/zoffcc/applications/trifa/savepathenabled_state.kt index cfc623d0..a0641736 100644 --- a/src/main/kotlin/com/zoffcc/applications/trifa/savepathenabled_state.kt +++ b/src/main/kotlin/com/zoffcc/applications/trifa/savepathenabled_state.kt @@ -1,5 +1,9 @@ package com.zoffcc.applications.trifa +import com.zoffcc.applications.trifa.MainActivity.Companion.PREF__database_files_dir +import com.zoffcc.applications.trifa.MainActivity.Companion.PREF__tox_savefile_dir +import com.zoffcc.applications.trifa.TRIFAGlobals.VFS_FILE_DIR +import com.zoffcc.applications.trifa.TRIFAGlobals.VFS_TMP_FILE_DIR import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.MutableStateFlow @@ -52,11 +56,24 @@ fun CoroutineScope.createSavepathStore(): SavepathStore { } launch { channelPath.consumeAsFlow().collect { item -> - mutableStateFlow.value = - state.copy( - savePathEnabled = state.savePathEnabled, - savePath = item - ) + try + { + val dir_file = File(item) + dir_file.mkdirs() + PREF__tox_savefile_dir = item + PREF__database_files_dir = item + VFS_TMP_FILE_DIR = PREF__tox_savefile_dir + File.separator + "/tempdir/files/" + VFS_FILE_DIR = PREF__tox_savefile_dir + File.separator + "/datadir/files/" + mutableStateFlow.value = + state.copy( + savePathEnabled = state.savePathEnabled, + savePath = item + ) + } + catch(e: Exception) + { + Log.i(TAG, "error creating savefile dir: " + item) + } } } From 4a0570a0ad3230f4bfe10a11ff9c190ec0596b6e Mon Sep 17 00:00:00 2001 From: zoff99 Date: Tue, 5 Dec 2023 14:18:13 +0100 Subject: [PATCH 2/3] dont create data dir on every keystroke. create it when the start button ist pressed --- src/main/kotlin/Main.kt | 1 + .../applications/trifa/savepathenabled_state.kt | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index c383228b..f6a1bbeb 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -347,6 +347,7 @@ fun App() start_button_text = "stop" }.start() TrifaToxService.stop_me = false + savepathstore.createPathDirectories() main_init() } }) { diff --git a/src/main/kotlin/com/zoffcc/applications/trifa/savepathenabled_state.kt b/src/main/kotlin/com/zoffcc/applications/trifa/savepathenabled_state.kt index a0641736..f0eee07d 100644 --- a/src/main/kotlin/com/zoffcc/applications/trifa/savepathenabled_state.kt +++ b/src/main/kotlin/com/zoffcc/applications/trifa/savepathenabled_state.kt @@ -19,6 +19,7 @@ data class savepathenabled_state( interface SavepathStore { fun updatePath(p: String) + fun createPathDirectories() fun updateEnabled(e: Boolean) val stateFlow: StateFlow val state get() = stateFlow.value @@ -37,6 +38,18 @@ fun CoroutineScope.createSavepathStore(): SavepathStore { channelPath.send(p) } } + override fun createPathDirectories() + { + try + { + val dir_file = File(PREF__tox_savefile_dir) + dir_file.mkdirs() + } + catch(e: Exception) + { + Log.i(TAG, "error creating savefile directory and parents: " + PREF__tox_savefile_dir) + } + } override fun updateEnabled(e: Boolean) { launch { @@ -59,7 +72,6 @@ fun CoroutineScope.createSavepathStore(): SavepathStore { try { val dir_file = File(item) - dir_file.mkdirs() PREF__tox_savefile_dir = item PREF__database_files_dir = item VFS_TMP_FILE_DIR = PREF__tox_savefile_dir + File.separator + "/tempdir/files/" @@ -72,7 +84,7 @@ fun CoroutineScope.createSavepathStore(): SavepathStore { } catch(e: Exception) { - Log.i(TAG, "error creating savefile dir: " + item) + Log.i(TAG, "error setting savefile dir: " + item) } } } From 24da7b8195a00d55d7c6328c2e9f98dbd605ec7e Mon Sep 17 00:00:00 2001 From: zoff99 Date: Tue, 5 Dec 2023 14:28:36 +0100 Subject: [PATCH 3/3] button to open data directory in settings screen --- .../trifa/savepathenabled_state.kt | 5 +++++ .../briar/desktop/SettingDetails.kt | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/main/kotlin/com/zoffcc/applications/trifa/savepathenabled_state.kt b/src/main/kotlin/com/zoffcc/applications/trifa/savepathenabled_state.kt index f0eee07d..d4405ebc 100644 --- a/src/main/kotlin/com/zoffcc/applications/trifa/savepathenabled_state.kt +++ b/src/main/kotlin/com/zoffcc/applications/trifa/savepathenabled_state.kt @@ -21,6 +21,7 @@ interface SavepathStore { fun updatePath(p: String) fun createPathDirectories() fun updateEnabled(e: Boolean) + fun isEnabled(): Boolean val stateFlow: StateFlow val state get() = stateFlow.value } @@ -56,6 +57,10 @@ fun CoroutineScope.createSavepathStore(): SavepathStore { channelEnabled.send(e) } } + override fun isEnabled(): Boolean + { + return state.savePathEnabled + } init { launch { diff --git a/src/main/kotlin/org/briarproject/briar/desktop/SettingDetails.kt b/src/main/kotlin/org/briarproject/briar/desktop/SettingDetails.kt index 5af895dc..85d7d62c 100644 --- a/src/main/kotlin/org/briarproject/briar/desktop/SettingDetails.kt +++ b/src/main/kotlin/org/briarproject/briar/desktop/SettingDetails.kt @@ -42,6 +42,7 @@ import androidx.compose.material.MaterialTheme import androidx.compose.material.Switch import androidx.compose.material.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember @@ -55,6 +56,8 @@ import com.zoffcc.applications.trifa.HelperFriend import com.zoffcc.applications.trifa.HelperFriend.get_g_opts import com.zoffcc.applications.trifa.HelperFriend.set_g_opts import com.zoffcc.applications.trifa.HelperNotification +import com.zoffcc.applications.trifa.HelperOSFile.show_containing_dir_in_explorer +import com.zoffcc.applications.trifa.MainActivity import com.zoffcc.applications.trifa.MainActivity.Companion.DB_PREF__open_files_directly import com.zoffcc.applications.trifa.TrifaToxService.Companion.orma import global_prefs @@ -62,7 +65,9 @@ import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import org.briarproject.briar.desktop.ui.VerticallyScrollableArea import org.briarproject.briar.desktop.utils.InternationalizationUtils.i18n +import savepathstore import update_bootstrap_nodes_from_internet +import java.io.File @Composable fun SettingDetails() @@ -254,6 +259,21 @@ fun SettingDetails() } // database prefs =================== + val savepathdata by savepathstore.stateFlow.collectAsState() + if (!savepathdata.savePathEnabled) + { + Row(Modifier.wrapContentHeight().fillMaxWidth().padding(start = 15.dp)) { + Button(modifier = Modifier.width(400.dp), + enabled = true, + onClick = { + show_containing_dir_in_explorer(MainActivity.PREF__tox_savefile_dir + File.separator + ".") + }) + { + Text("Open data directory") + } + } + } + Row(Modifier.wrapContentHeight().fillMaxWidth().padding(start = 15.dp)) { Button(modifier = Modifier.width(400.dp), enabled = true,