Merge branch 'multiplatform' into av/multiplatform-merged-master

This commit is contained in:
Evgeny Poberezkin
2023-07-14 12:01:48 +01:00
19 changed files with 158 additions and 56 deletions
@@ -36,6 +36,7 @@ class SimplexApp: Application(), LifecycleEventObserver {
initHaskell()
initMultiplatform()
tmpDir.deleteRecursively()
withBGApi {
initChatController()
runMigrations()
@@ -13,7 +13,13 @@ import java.net.URI
actual val dataDir: File = androidAppContext.dataDir
actual val tmpDir: File = androidAppContext.getDir("temp", Application.MODE_PRIVATE)
actual val cacheDir: File = androidAppContext.cacheDir
actual val filesDir: File = File(dataDir.absolutePath + File.separator + "files")
actual val appFilesDir: File = File(filesDir.absolutePath + File.separator + "app_files")
actual val coreTmpDir: File = File(filesDir.absolutePath + File.separator + "temp_files")
actual val dbAbsolutePrefixPath: String = dataDir.absolutePath + File.separator + "files"
actual val chatDatabaseFileName: String = "files_chat.db"
actual val agentDatabaseFileName: String = "files_agent.db"
@Composable
actual fun rememberFileChooserLauncher(getContent: Boolean, onResult: (URI?) -> Unit): FileChooserLauncher {
@@ -43,7 +43,7 @@ val errorBitmap: Bitmap = BitmapFactory.decodeByteArray(errorBitmapBytes, 0, err
class CustomTakePicturePreview(var uri: Uri?, var tmpFile: File?): ActivityResultContract<Void?, Uri?>() {
@CallSuper
override fun createIntent(context: Context, input: Void?): Intent {
tmpFile = File.createTempFile("image", ".bmp", File(getAppFilesDirectory()))
tmpFile = File.createTempFile("image", ".bmp", appFilesDir)
// Since the class should return Uri, the file should be deleted somewhere else. And in order to be sure, delegate this to system
tmpFile?.deleteOnExit()
uri = FileProvider.getUriForFile(context, "$APPLICATION_ID.provider", tmpFile!!)
@@ -277,7 +277,7 @@ actual fun getDrawableFromUri(uri: URI, withAlertOnException: Boolean): Any? {
actual suspend fun saveTempImageUncompressed(image: ImageBitmap, asPng: Boolean): File? {
return try {
val ext = if (asPng) "png" else "jpg"
return File(getTempFilesDirectory() + File.separator + generateNewFileName("IMG", ext)).apply {
return File(tmpDir.absolutePath + File.separator + generateNewFileName("IMG", ext)).apply {
outputStream().use { out ->
image.asAndroidBitmap().compress(if (asPng) Bitmap.CompressFormat.PNG else Bitmap.CompressFormat.JPEG, 85, out)
out.flush()
@@ -319,8 +319,8 @@ object ChatController {
try {
if (chatModel.chatRunning.value == true) return
apiSetNetworkConfig(getNetCfg())
apiSetTempFolder(getTempFilesDirectory())
apiSetFilesFolder(getAppFilesDirectory())
apiSetTempFolder(coreTmpDir.absolutePath)
apiSetFilesFolder(appFilesDir.absolutePath)
apiSetXFTPConfig(getXFTPCfg())
val justStarted = apiStartChat()
val users = listUsers()
@@ -30,9 +30,8 @@ val chatController: ChatController = ChatController
suspend fun initChatController(useKey: String? = null, confirmMigrations: MigrationConfirmation? = null, startChat: Boolean = true) {
val dbKey = useKey ?: DatabaseUtils.useDatabaseKey()
val dbAbsolutePathPrefix = getFilesDirectory()
val confirm = confirmMigrations ?: if (appPreferences.confirmDBUpgrades.get()) MigrationConfirmation.Error else MigrationConfirmation.YesUp
val migrated: Array<Any> = chatMigrateInit(dbAbsolutePathPrefix, dbKey, confirm.value)
val migrated: Array<Any> = chatMigrateInit(dbAbsolutePrefixPath, dbKey, confirm.value)
val res: DBMigrationResult = kotlin.runCatching {
json.decodeFromString<DBMigrationResult>(migrated[0] as String)
}.getOrElse { DBMigrationResult.Unknown(migrated[0] as String) }
@@ -9,7 +9,13 @@ import java.net.URI
expect val dataDir: File
expect val tmpDir: File
expect val cacheDir: File
expect val filesDir: File
expect val appFilesDir: File
expect val coreTmpDir: File
expect val dbAbsolutePrefixPath: String
expect val chatDatabaseFileName: String
expect val agentDatabaseFileName: String
fun copyFileToFile(from: File, to: URI, finally: () -> Unit) {
try {
@@ -43,21 +49,8 @@ fun copyBytesToFile(bytes: ByteArrayInputStream, to: URI, finally: () -> Unit) {
}
}
fun getFilesDirectory(): String {
return dataDir.absolutePath + File.separator + "files"
}
// LALAL
fun getTempFilesDirectory(): String {
return getFilesDirectory() + File.separator + "temp_files"
}
fun getAppFilesDirectory(): String {
return getFilesDirectory() + File.separator + "app_files"
}
fun getAppFilePath(fileName: String): String {
return getAppFilesDirectory() + File.separator + fileName
return appFilesDir.absolutePath + File.separator + fileName
}
fun getLoadedFilePath(file: CIFile?): String? {
@@ -25,7 +25,7 @@ import java.util.*
@Composable
fun ChatArchiveView(m: ChatModel, title: String, archiveName: String, archiveTime: Instant) {
val archivePath = getFilesDirectory() + File.separator + archiveName
val archivePath = filesDir.absolutePath + File.separator + archiveName
val saveArchiveLauncher = rememberFileChooserLauncher(false) { to: URI? ->
if (to != null) {
copyFileToFile(File(archivePath), to) {}
@@ -221,9 +221,8 @@ private fun shouldShowRestoreDbButton(prefs: AppPreferences): Boolean {
val startedAt = prefs.encryptionStartedAt.get() ?: return false
/** Just in case there is any small difference between reported Java's [Clock.System.now] and Linux's time on a file */
val safeDiffInTime = 10_000L
// LALAL CHANGE FILENAME
val filesChat = File(dataDir.absolutePath + File.separator + "files_chat.db.bak")
val filesAgent = File(dataDir.absolutePath + File.separator + "files_agent.db.bak")
val filesChat = File(dataDir.absolutePath + File.separator + "${chatDatabaseFileName}.bak")
val filesAgent = File(dataDir.absolutePath + File.separator + "${agentDatabaseFileName}.bak")
return filesChat.exists() &&
filesAgent.exists() &&
startedAt.toEpochMilliseconds() - safeDiffInTime <= filesChat.lastModified() &&
@@ -231,9 +230,8 @@ private fun shouldShowRestoreDbButton(prefs: AppPreferences): Boolean {
}
private fun restoreDb(restoreDbFromBackup: MutableState<Boolean>, prefs: AppPreferences) {
// LALAL CHANGE FILENAME
val filesChatBase = dataDir.absolutePath + File.separator + "files_chat.db"
val filesAgentBase = dataDir.absolutePath + File.separator + "files_agent.db"
val filesChatBase = dataDir.absolutePath + File.separator + chatDatabaseFileName
val filesAgentBase = dataDir.absolutePath + File.separator + agentDatabaseFileName
try {
Files.copy(Path("$filesChatBase.bak"), Path(filesChatBase), StandardCopyOption.REPLACE_EXISTING)
Files.copy(Path("$filesAgentBase.bak"), Path(filesAgentBase), StandardCopyOption.REPLACE_EXISTING)
@@ -54,7 +54,7 @@ fun DatabaseView(
}
}
}
val appFilesCountAndSize = remember { mutableStateOf(directoryFileCountAndSize(getAppFilesDirectory())) }
val appFilesCountAndSize = remember { mutableStateOf(directoryFileCountAndSize(appFilesDir.absolutePath)) }
val importArchiveLauncher = rememberFileChooserLauncher(true) { to: URI? ->
if (to != null) {
importArchiveAlert(m, to, appFilesCountAndSize, progressIndicator)
@@ -475,8 +475,8 @@ private suspend fun exportChatArchive(
val archiveTime = Clock.System.now()
val ts = SimpleDateFormat("yyyy-MM-dd'T'HHmmss", Locale.US).format(Date.from(archiveTime.toJavaInstant()))
val archiveName = "simplex-chat.$ts.zip"
val archivePath = "${getFilesDirectory()}${File.separator}$archiveName"
val config = ArchiveConfig(archivePath, parentTempDirectory = cacheDir.toString())
val archivePath = "${filesDir.absolutePath}${File.separator}$archiveName"
val config = ArchiveConfig(archivePath, parentTempDirectory = tmpDir.toString())
m.controller.apiExportArchive(config)
deleteOldArchive(m)
m.controller.appPrefs.chatArchiveName.set(archiveName)
@@ -490,7 +490,7 @@ private suspend fun exportChatArchive(
private fun deleteOldArchive(m: ChatModel) {
val chatArchiveName = m.controller.appPrefs.chatArchiveName.get()
if (chatArchiveName != null) {
val file = File("${getFilesDirectory()}${File.separator}$chatArchiveName")
val file = File("${filesDir.absolutePath}${File.separator}$chatArchiveName")
val fileDeleted = file.delete()
if (fileDeleted) {
m.controller.appPrefs.chatArchiveName.set(null)
@@ -529,10 +529,10 @@ private fun importArchive(
try {
m.controller.apiDeleteStorage()
try {
val config = ArchiveConfig(archivePath, parentTempDirectory = cacheDir.toString())
val config = ArchiveConfig(archivePath, parentTempDirectory = tmpDir.toString())
val archiveErrors = m.controller.apiImportArchive(config)
DatabaseUtils.ksDatabasePassword.remove()
appFilesCountAndSize.value = directoryFileCountAndSize(getAppFilesDirectory())
appFilesCountAndSize.value = directoryFileCountAndSize(appFilesDir.absolutePath)
if (archiveErrors.isEmpty()) {
operationEnded(m, progressIndicator) {
AlertManager.shared.showAlertMsg(generalGetString(MR.strings.chat_database_imported), text = generalGetString(MR.strings.restart_the_app_to_use_imported_chat_database))
@@ -563,7 +563,7 @@ private fun saveArchiveFromURI(importedArchiveURI: URI): String? {
val inputStream = importedArchiveURI.inputStream()
val archiveName = getFileName(importedArchiveURI)
if (inputStream != null && archiveName != null) {
val archivePath = "$cacheDir${File.separator}$archiveName"
val archivePath = "$tmpDir${File.separator}$archiveName"
val destFile = File(archivePath)
Files.copy(inputStream, destFile.toPath())
archivePath
@@ -632,7 +632,7 @@ private fun afterSetCiTTL(
appFilesCountAndSize: MutableState<Pair<Int, Long>>,
) {
progressIndicator.value = false
appFilesCountAndSize.value = directoryFileCountAndSize(getAppFilesDirectory())
appFilesCountAndSize.value = directoryFileCountAndSize(appFilesDir.absolutePath)
withApi {
try {
val chats = m.controller.apiGetChats()
@@ -655,7 +655,7 @@ private fun deleteFilesAndMediaAlert(appFilesCountAndSize: MutableState<Pair<Int
private fun deleteFiles(appFilesCountAndSize: MutableState<Pair<Int, Long>>) {
deleteAppFiles()
appFilesCountAndSize.value = directoryFileCountAndSize(getAppFilesDirectory())
appFilesCountAndSize.value = directoryFileCountAndSize(appFilesDir.absolutePath)
}
private fun operationEnded(m: ChatModel, progressIndicator: MutableState<Boolean>, alert: () -> Unit) {
@@ -39,9 +39,8 @@ object DatabaseUtils {
}
}
// LALAL CHANGE DB FILE NAME ON DESKTOP
private fun hasDatabase(rootDir: String): Boolean =
File(rootDir + File.separator + "files_chat.db").exists() && File(rootDir + File.separator + "files_agent.db").exists()
File(rootDir + File.separator + chatDatabaseFileName).exists() && File(rootDir + File.separator + agentDatabaseFileName).exists()
fun useDatabaseKey(): String {
Log.d(TAG, "useDatabaseKey ${appPreferences.storeDBPassphrase.get()}")
@@ -209,7 +209,7 @@ fun removeFile(fileName: String): Boolean {
}
fun deleteAppFiles() {
val dir = File(getAppFilesDirectory())
val dir = appFilesDir
try {
dir.list()?.forEach {
removeFile(it)
@@ -43,10 +43,10 @@ private fun applyAppLocale() {
@Suppress("UnsafeDynamicallyLoadedCode")
actual fun initHaskell() {
val libApp = "libapp-lib.${desktopPlatform.libExtension}"
val tmpDir = Files.createTempDirectory("simplex-native-libs").toFile()
tmpDir.deleteOnExit()
copyResources(desktopPlatform.libPath, tmpDir.toPath())
System.load(File(tmpDir, libApp).absolutePath)
val libsTmpDir = File(tmpDir.absolutePath + File.separator + "libs")
copyResources(desktopPlatform.libPath, libsTmpDir.toPath())
System.load(File(libsTmpDir, libApp).absolutePath)
libsTmpDir.deleteRecursively()
initHS()
}
@@ -15,9 +15,15 @@ private fun applicationParentPath(): String = try {
"./"
}
actual val dataDir: File = File(desktopPlatform.configPath)
actual val tmpDir: File = File(System.getProperty("java.io.tmpdir"))
actual val cacheDir: File = tmpDir
actual val dataDir: File = File(desktopPlatform.dataPath)
actual val tmpDir: File = File(System.getProperty("java.io.tmpdir") + File.separator + "simplex").also { it.deleteOnExit() }
actual val filesDir: File = File(dataDir.absolutePath + File.separator + "simplex_v1_files")
actual val appFilesDir: File = filesDir
actual val coreTmpDir: File = File(dataDir.absolutePath + File.separator + "tmp")
actual val dbAbsolutePrefixPath: String = dataDir.absolutePath + File.separator + "simplex_v1"
actual val chatDatabaseFileName: String = "simplex_v1_chat.db"
actual val agentDatabaseFileName: String = "simplex_v1_agent.db"
@Composable
actual fun rememberFileChooserLauncher(getContent: Boolean, onResult: (URI?) -> Unit): FileChooserLauncher =
@@ -4,14 +4,16 @@ import java.io.File
import java.util.*
private val home = System.getProperty("user.home")
private val unixConfigPath = (System.getenv("XDG_CONFIG_HOME") ?: "$home/.config") + "/simplex"
private val unixDataPath = (System.getenv("XDG_DATA_HOME") ?: "$home/.local/share") + "/simplex"
val desktopPlatform = detectDesktopPlatform()
enum class DesktopPlatform(val libPath: String, val libExtension: String, val configPath: String) {
LINUX_X86_64("/libs/linux-x86_64", "so", "$home/.config/simplex"),
LINUX_AARCH64("/libs/aarch64", "so", "$home/.config/simplex"),
WINDOWS_X86_64("/libs/windows-x86_64", "dll", System.getenv("AppData") + File.separator + "simplex"),
MAC_X86_64("/libs/mac-x86_64", "dylib", "$home/.config/simplex"),
MAC_AARCH64("/libs/mac-aarch64", "dylib", "$home/.config/simplex");
enum class DesktopPlatform(val libPath: String, val libExtension: String, val configPath: String, val dataPath: String) {
LINUX_X86_64("/libs/linux-x86_64", "so", unixConfigPath, unixDataPath),
LINUX_AARCH64("/libs/aarch64", "so", unixConfigPath, unixDataPath),
WINDOWS_X86_64("/libs/windows-x86_64", "dll", System.getenv("AppData") + File.separator + "SimpleX", System.getenv("AppData") + File.separator + "SimpleX"),
MAC_X86_64("/libs/mac-x86_64", "dylib", unixConfigPath, unixDataPath),
MAC_AARCH64("/libs/mac-aarch64", "dylib", unixConfigPath, unixDataPath);
}
private fun detectDesktopPlatform(): DesktopPlatform {
@@ -20,7 +20,7 @@ actual fun escapedHtmlToAnnotatedString(text: String, density: Density): Annotat
}
actual fun getAppFileUri(fileName: String): URI =
URI("file:" + getAppFilesDirectory() + File.separator + fileName)
URI("file:" + appFilesDir.absolutePath + File.separator + fileName)
actual fun getLoadedImage(file: CIFile?): ImageBitmap? {
val filePath = getLoadedFilePath(file)
@@ -34,7 +34,7 @@ actual fun getLoadedImage(file: CIFile?): ImageBitmap? {
actual fun getFileName(uri: URI): String? = uri.toPath().toFile().name
actual fun getAppFilePath(uri: URI): String? = getFilesDirectory() + File.separator + "app_files"
actual fun getAppFilePath(uri: URI): String? = uri.path
actual fun getFileSize(uri: URI): Long? = uri.toPath().toFile().length()
@@ -4,5 +4,6 @@ import chat.simplex.common.showApp
fun main() {
initHaskell()
initApp()
tmpDir.deleteRecursively()
return showApp()
}
+25
View File
@@ -0,0 +1,25 @@
#!/bin/bash
OS=linux
ARCH=${1:-`uname -a | rev | cut -d' ' -f2 | rev`}
GHC_VERSION=8.10.7
BUILD_DIR=dist-newstyle/build/$ARCH-$OS/ghc-${GHC_VERSION}/simplex-chat-*
rm -rf $BUILD_DIR
cabal build lib:simplex-chat --ghc-options='-optl-Wl,-rpath,$ORIGIN' --ghc-options="-optl-L$(ghc --print-libdir)/rts -optl-Wl,--as-needed,-lHSrts_thr-ghc$GHC_VERSION"
cd $BUILD_DIR/build
#patchelf --add-needed libHSrts_thr-ghc${GHC_VERSION}.so libHSsimplex-chat-*-inplace-ghc${GHC_VERSION}.so
#patchelf --add-rpath '$ORIGIN' libHSsimplex-chat-*-inplace-ghc${GHC_VERSION}.so
mkdir deps
ldd libHSsimplex-chat-*-inplace-ghc${GHC_VERSION}.so | grep "ghc" | cut -d' ' -f 3 | xargs -I {} cp {} ./deps/
cd -
rm -rf apps/multiplatform/common/src/commonMain/cpp/desktop/libs/$OS-$ARCH/
rm -rf apps/multiplatform/common/src/commonMain/resources/libs/$OS-$ARCH/
rm -rf apps/multiplatform/desktop/build/cmake
mkdir -p apps/multiplatform/common/src/commonMain/cpp/desktop/libs/$OS-$ARCH/
cp -r $BUILD_DIR/build/deps apps/multiplatform/common/src/commonMain/cpp/desktop/libs/$OS-$ARCH/
cp $BUILD_DIR/build/libHSsimplex-chat-*-inplace-ghc${GHC_VERSION}.so apps/multiplatform/common/src/commonMain/cpp/desktop/libs/$OS-$ARCH/
+72
View File
@@ -0,0 +1,72 @@
#!/bin/bash
OS=mac
ARCH="${1:-`uname -a | rev | cut -d' ' -f1 | rev`}"
if [ "$ARCH" == "arm64" ]; then
ARCH=aarch64
fi
LIB_EXT=dylib
LIB=libHSsimplex-chat-*-inplace-ghc*.$LIB_EXT
GHC_LIBS_DIR=$(ghc --print-libdir)
BUILD_DIR=dist-newstyle/build/$ARCH-*/ghc-*/simplex-chat-*
rm -rf $BUILD_DIR
cabal build lib:simplex-chat lib:simplex-chat --ghc-options="-optl-Wl,-rpath,@loader_path -optl-Wl,-L$GHC_LIBS_DIR/rts -optl-lHSrts_thr-ghc8.10.7 -optl-lffi"
cd $BUILD_DIR/build
mkdir deps 2> /dev/null
# It's not included by default for some reason. Compiled lib tries to find system one but it's not always available
cp $GHC_LIBS_DIR/rts/libffi.dylib ./deps
DYLIBS=`otool -L $LIB | grep @rpath | tail -n +2 | cut -d' ' -f 1 | cut -d'/' -f2`
RPATHS=`otool -l $LIB | grep "path "| cut -d' ' -f11`
PROCESSED_LIBS=()
function copy_deps() {
local LIB=$1
if [[ "${PROCESSED_LIBS[*]}" =~ "$LIB" ]]; then
return 0
fi
PROCESSED_LIBS+=$LIB
local DYLIBS=`otool -L $LIB | grep @rpath | tail -n +2 | cut -d' ' -f 1 | cut -d'/' -f2`
local NON_FINAL_RPATHS=`otool -l $LIB | grep "path "| cut -d' ' -f11`
local RPATHS=`otool -l $LIB | grep "path "| cut -d' ' -f11 | sed "s|@loader_path/..|$GHC_LIBS_DIR|"`
cp $LIB ./deps
if [[ "$NON_FINAL_RPATHS" == *"@loader_path/.."* ]]; then
# Need to point the lib to @loader_path instead
install_name_tool -add_rpath @loader_path ./deps/`basename $LIB`
fi
#echo LIB $LIB
#echo DYLIBS ${DYLIBS[@]}
#echo RPATHS ${RPATHS[@]}
for DYLIB in $DYLIBS; do
for RPATH in $RPATHS; do
if [ -f "$RPATH/$DYLIB" ]; then
#echo DEP IS "$RPATH/$DYLIB"
if [ ! -f "deps/$DYLIB" ]; then
cp "$RPATH/$DYLIB" ./deps
fi
copy_deps "$RPATH/$DYLIB"
fi
done
done
}
copy_deps $LIB
rm deps/`basename $LIB`
cd -
rm -rf apps/multiplatform/common/src/commonMain/cpp/desktop/libs/$OS-$ARCH/
rm -rf apps/multiplatform/common/src/commonMain/resources/libs/$OS-$ARCH/
rm -rf apps/multiplatform/desktop/build/cmake
mkdir -p apps/multiplatform/common/src/commonMain/cpp/desktop/libs/$OS-$ARCH/
cp -r $BUILD_DIR/build/deps apps/multiplatform/common/src/commonMain/cpp/desktop/libs/$OS-$ARCH/
cp $BUILD_DIR/build/libHSsimplex-chat-*-inplace-ghc*.$LIB_EXT apps/multiplatform/common/src/commonMain/cpp/desktop/libs/$OS-$ARCH/