Skip to content

Commit e9ce6fd

Browse files
committed
feat: use XDG_DATA_HOME for Flatpak data directory
closes #1249
1 parent 51db6c7 commit e9ce6fd

5 files changed

Lines changed: 62 additions & 17 deletions

File tree

flatpak/com.cypherstack.campfire.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ finish-args:
1010
- --socket=fallback-x11
1111
- --socket=wayland
1212
- --device=dri
13-
- --filesystem=~/.campfire
1413
- --talk-name=org.freedesktop.secrets
1514
- --talk-name=org.freedesktop.Notifications
1615

flatpak/com.cypherstack.stackduo.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ finish-args:
1010
- --socket=fallback-x11
1111
- --socket=wayland
1212
- --device=dri
13-
- --filesystem=~/.stackduo
1413
- --talk-name=org.freedesktop.secrets
1514
- --talk-name=org.freedesktop.Notifications
1615

flatpak/com.cypherstack.stackwallet.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ finish-args:
1010
- --socket=fallback-x11
1111
- --socket=wayland
1212
- --device=dri
13-
- --filesystem=~/.stackwallet
1413
- --talk-name=org.freedesktop.secrets
1514
- --talk-name=org.freedesktop.Notifications
1615

lib/main.dart

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import 'package:google_fonts/google_fonts.dart';
2424
import 'package:keyboard_dismisser/keyboard_dismisser.dart';
2525
import 'package:logger/logger.dart';
2626
import 'package:mobile_app_privacy/mobile_app_privacy.dart';
27+
import 'package:path/path.dart' as path;
2728
import 'package:path_provider/path_provider.dart';
2829
import 'package:window_size/window_size.dart';
2930

@@ -98,6 +99,44 @@ void main(List<String> args) async {
9899

99100
if (Util.isDesktop && args.length == 2 && args.first == "-d") {
100101
StackFileSystem.setDesktopOverrideDir(args.last);
102+
} else if (Platform.isLinux) {
103+
// Flatpak detection: use XDG_DATA_HOME instead of ~/.stackwallet.
104+
final flatpakId = Platform.environment['FLATPAK_ID'];
105+
if (flatpakId != null || File('/.flatpak-info').existsSync()) {
106+
// Resolve the persistent data root. Prefer XDG_DATA_HOME when set.
107+
// Otherwise fall back to $HOME/.local/share, but only if HOME is
108+
// available. If neither is set we leave the data dir unchanged and
109+
// let StackFileSystem use its default, rather than crashing.
110+
final home = Platform.environment['HOME'];
111+
final xdgDataHome = Platform.environment['XDG_DATA_HOME'] ??
112+
(home != null ? path.join(home, '.local', 'share') : null);
113+
114+
if (xdgDataHome != null) {
115+
final flatpakDataDir =
116+
path.join(xdgDataHome, AppConfig.appDefaultDataDirName);
117+
118+
// Migration: move legacy data from $HOME/.stackwallet into the new
119+
// location, but only when the legacy dir exists and the new one does
120+
// not. Best-effort: never crash if the move fails.
121+
if (home != null) {
122+
final legacyDir = Directory(
123+
path.join(home, '.${AppConfig.appDefaultDataDirName}'),
124+
);
125+
final newDir = Directory(flatpakDataDir);
126+
if (legacyDir.existsSync() && !newDir.existsSync()) {
127+
try {
128+
await Directory(xdgDataHome).create(recursive: true);
129+
await legacyDir.rename(newDir.path);
130+
} catch (_) {
131+
// If rename fails (e.g. different filesystem), fall back to
132+
// using the new path anyway. The user can manually move data.
133+
}
134+
}
135+
}
136+
137+
StackFileSystem.setDesktopOverrideDir(flatpakDataDir);
138+
}
139+
}
101140
}
102141

103142
final loadCoinlibFuture = loadCoinlib();

lib/utilities/stack_file_system.dart

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -213,23 +213,32 @@ abstract class StackFileSystem {
213213
}
214214
}
215215

216-
final appDocsDir = await getApplicationDocumentsDirectory();
217-
const logsDirName = "${AppConfig.prefix}_Logs";
218216
final Directory logsDir;
219217

220-
if (Platform.isIOS) {
221-
logsDir = Directory(path.join(appDocsDir.path, "logs"));
222-
} else if (Platform.isMacOS || Platform.isLinux || Platform.isWindows) {
223-
// TODO check this is correct for macos
224-
logsDir = Directory(path.join(appDocsDir.path, logsDirName));
225-
} else if (Platform.isAndroid) {
226-
// final dir = await wtfAndroidDocumentsPath();
227-
// final logsDirPath = path.join(dir.path, logsDirName);
228-
// logsDir = Directory(logsDirPath);
229-
230-
logsDir = Directory(path.join(appDocsDir.path, "logs"));
218+
// When a desktop override dir is set (e.g. the Flatpak XDG_DATA_HOME
219+
// location or the -d launch flag) keep logs under that persistent root
220+
// so they follow the data directory rather than landing in the
221+
// application documents directory.
222+
if (_overrideDesktopDirPath != null) {
223+
logsDir = Directory(path.join(_overrideDesktopDirPath!, "logs"));
231224
} else {
232-
throw Exception("Unsupported Platform");
225+
final appDocsDir = await getApplicationDocumentsDirectory();
226+
const logsDirName = "${AppConfig.prefix}_Logs";
227+
228+
if (Platform.isIOS) {
229+
logsDir = Directory(path.join(appDocsDir.path, "logs"));
230+
} else if (Platform.isMacOS || Platform.isLinux || Platform.isWindows) {
231+
// TODO check this is correct for macos
232+
logsDir = Directory(path.join(appDocsDir.path, logsDirName));
233+
} else if (Platform.isAndroid) {
234+
// final dir = await wtfAndroidDocumentsPath();
235+
// final logsDirPath = path.join(dir.path, logsDirName);
236+
// logsDir = Directory(logsDirPath);
237+
238+
logsDir = Directory(path.join(appDocsDir.path, "logs"));
239+
} else {
240+
throw Exception("Unsupported Platform");
241+
}
233242
}
234243

235244
if (!logsDir.existsSync()) {

0 commit comments

Comments
 (0)