diff --git a/lib/config/hive_config.dart b/lib/config/hive_config.dart index 25dee2c8..01118bba 100644 --- a/lib/config/hive_config.dart +++ b/lib/config/hive_config.dart @@ -8,10 +8,13 @@ import 'package:selfprivacy/logic/models/hive/backups_credential.dart'; import 'package:selfprivacy/logic/models/hive/server_details.dart'; import 'package:selfprivacy/logic/models/hive/server_domain.dart'; import 'package:selfprivacy/logic/models/hive/user.dart'; +import 'package:selfprivacy/utils/platform_adapter.dart'; class HiveConfig { static Future init() async { - await Hive.initFlutter(); + final String? storagePath = PlatformAdapter.storagePath; + print('HiveConfig: Custom storage path: $storagePath'); + await Hive.initFlutter(storagePath); Hive.registerAdapter(UserAdapter()); Hive.registerAdapter(ServerHostingDetailsAdapter()); Hive.registerAdapter(ServerDomainAdapter()); diff --git a/lib/logic/cubit/server_installation/server_installation_repository.dart b/lib/logic/cubit/server_installation/server_installation_repository.dart index 5b39463b..dd3388c6 100644 --- a/lib/logic/cubit/server_installation/server_installation_repository.dart +++ b/lib/logic/cubit/server_installation/server_installation_repository.dart @@ -1,9 +1,7 @@ import 'dart:async'; import 'dart:io'; -import 'package:device_info_plus/device_info_plus.dart'; import 'package:easy_localization/easy_localization.dart'; -import 'package:flutter/foundation.dart'; import 'package:hive/hive.dart'; import 'package:pub_semver/pub_semver.dart'; import 'package:selfprivacy/config/get_it_config.dart'; @@ -22,6 +20,7 @@ import 'package:selfprivacy/logic/models/server_basic_info.dart'; import 'package:selfprivacy/logic/models/server_type.dart'; import 'package:selfprivacy/logic/providers/providers_controller.dart'; import 'package:selfprivacy/utils/network_utils.dart'; +import 'package:selfprivacy/utils/platform_adapter.dart'; class IpNotFoundException implements Exception { IpNotFoundException(this.message); @@ -308,40 +307,6 @@ class ServerInstallationRepository { return domain!; } - Future getDeviceName() async { - final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin(); - if (kIsWeb) { - return deviceInfo.webBrowserInfo.then( - (final WebBrowserInfo value) => - '${value.browserName} ${value.platform}', - ); - } else { - if (Platform.isAndroid) { - return deviceInfo.androidInfo.then( - (final AndroidDeviceInfo value) => - '${value.model} ${value.version.release}', - ); - } else if (Platform.isIOS) { - return deviceInfo.iosInfo.then( - (final IosDeviceInfo value) => - '${value.utsname.machine} ${value.systemName} ${value.systemVersion}', - ); - } else if (Platform.isLinux) { - return deviceInfo.linuxInfo - .then((final LinuxDeviceInfo value) => value.prettyName); - } else if (Platform.isMacOS) { - return deviceInfo.macOsInfo.then( - (final MacOsDeviceInfo value) => - '${value.hostName} ${value.computerName}', - ); - } else if (Platform.isWindows) { - return deviceInfo.windowsInfo - .then((final WindowsDeviceInfo value) => value.computerName); - } - } - return 'Unidentified'; - } - Future authorizeByNewDeviceKey( final ServerDomain serverDomain, final String newDeviceKey, @@ -353,7 +318,10 @@ class ServerInstallationRepository { ); final String serverIp = await getServerIpFromDomain(serverDomain); final GenericResult result = await serverApi.authorizeDevice( - DeviceToken(device: await getDeviceName(), token: newDeviceKey), + DeviceToken( + device: await PlatformAdapter.deviceName, + token: newDeviceKey, + ), ); if (result.success) { @@ -390,7 +358,7 @@ class ServerInstallationRepository { ); final String serverIp = await getServerIpFromDomain(serverDomain); final GenericResult result = await serverApi.useRecoveryToken( - DeviceToken(device: await getDeviceName(), token: recoveryKey), + DeviceToken(device: await PlatformAdapter.deviceName, token: recoveryKey), ); if (result.success) { @@ -453,7 +421,10 @@ class ServerInstallationRepository { final GenericResult deviceAuthKey = await serverApi.createDeviceToken(); final GenericResult result = await serverApi.authorizeDevice( - DeviceToken(device: await getDeviceName(), token: deviceAuthKey.data), + DeviceToken( + device: await PlatformAdapter.deviceName, + token: deviceAuthKey.data, + ), ); if (result.success) { diff --git a/lib/ui/pages/providers/providers.dart b/lib/ui/pages/providers/providers.dart index bdae6e07..64b87d34 100644 --- a/lib/ui/pages/providers/providers.dart +++ b/lib/ui/pages/providers/providers.dart @@ -96,7 +96,6 @@ class _ProvidersPageState extends State { onTap: () => context.pushRoute(const DnsDetailsRoute()), ), const SizedBox(height: 16), - // TODO: When backups are fixed, show this card _Card( state: isBackupInitialized ? StateType.stable diff --git a/lib/utils/platform_adapter.dart b/lib/utils/platform_adapter.dart new file mode 100644 index 00000000..fa40d7d9 --- /dev/null +++ b/lib/utils/platform_adapter.dart @@ -0,0 +1,59 @@ +import 'dart:io'; + +import 'package:device_info_plus/device_info_plus.dart'; +import 'package:flutter/foundation.dart'; + +/// SelfPrivacy wrapper for Platform information provider. +class PlatformAdapter { + /// Persistent storage directory for data files. + static String? get storagePath { + String? path; + if (Platform.isLinux) { + // https://wiki.archlinux.org/title/XDG_Base_Directory + path = Platform.environment['XDG_DATA_HOME']; + if (path == null) { + final String home = Platform.environment['HOME'] ?? '.'; + path = '$home/.local/share'; + } + path += '/selfprivacy'; + } + + return path; + } + + /// Running operating environment. + static Future get deviceName async { + final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin(); + if (kIsWeb) { + return deviceInfo.webBrowserInfo.then( + (final WebBrowserInfo value) => + '${value.browserName} ${value.platform}', + ); + } else { + if (Platform.isAndroid) { + return deviceInfo.androidInfo.then( + (final AndroidDeviceInfo value) => + '${value.model} ${value.version.release}', + ); + } else if (Platform.isIOS) { + return deviceInfo.iosInfo.then( + (final IosDeviceInfo value) => + '${value.utsname.machine} ${value.systemName} ${value.systemVersion}', + ); + } else if (Platform.isLinux) { + return deviceInfo.linuxInfo + .then((final LinuxDeviceInfo value) => value.prettyName); + } else if (Platform.isMacOS) { + return deviceInfo.macOsInfo.then( + (final MacOsDeviceInfo value) => + '${value.hostName} ${value.computerName}', + ); + } else if (Platform.isWindows) { + return deviceInfo.windowsInfo + .then((final WindowsDeviceInfo value) => value.computerName); + } + } + + return 'Unidentified'; + } +}