3
3
// found in the LICENSE file.
4
4
import 'dart:io' ;
5
5
import 'dart:async' ;
6
+ import 'dart:ffi' ;
7
+ import 'package:ffi/ffi.dart' ;
6
8
7
9
import 'package:xdg_directories/xdg_directories.dart' as xdg;
8
10
import 'package:path/path.dart' as path;
9
11
import 'package:path_provider_platform_interface/path_provider_platform_interface.dart' ;
10
12
13
+ // GApplication* g_application_get_default();
14
+ typedef g_application_get_default_c = IntPtr Function ();
15
+ typedef g_application_get_default_dart = int Function ();
16
+
17
+ // const gchar* g_application_get_application_id(GApplication* application);
18
+ typedef g_application_get_application_id_c = Pointer <Utf8 > Function (IntPtr );
19
+ typedef g_application_get_application_id_dart = Pointer <Utf8 > Function (int );
20
+
11
21
/// The linux implementation of [PathProviderPlatform]
12
22
///
13
23
/// This class implements the `package:path_provider` functionality for linux
@@ -22,11 +32,43 @@ class PathProviderLinux extends PathProviderPlatform {
22
32
return Future .value ("/tmp" );
23
33
}
24
34
35
+ // Gets the application ID set in GApplication.
36
+ String _getApplicationId () {
37
+ DynamicLibrary gio;
38
+ try {
39
+ gio = DynamicLibrary .open ('libgio-2.0.so' );
40
+ } on ArgumentError {
41
+ return null ;
42
+ }
43
+ var g_application_get_default = gio.lookupFunction<
44
+ g_application_get_default_c,
45
+ g_application_get_default_dart> ('g_application_get_default' );
46
+ var app = g_application_get_default ();
47
+ if (app == 0 ) return null ;
48
+
49
+ var g_application_get_application_id = gio.lookupFunction<
50
+ g_application_get_application_id_c,
51
+ g_application_get_application_id_dart> (
52
+ 'g_application_get_application_id' );
53
+ var app_id = g_application_get_application_id (app);
54
+ if (app_id == null ) return null ;
55
+
56
+ return Utf8 .fromUtf8 (app_id);
57
+ }
58
+
59
+ // Gets the unique ID for this application.
60
+ Future <String > _getId () async {
61
+ var appId = _getApplicationId ();
62
+ if (appId != null ) return appId;
63
+
64
+ // Fall back to using the executable name.
65
+ return path.basenameWithoutExtension (
66
+ await File ('/proc/self/exe' ).resolveSymbolicLinks ());
67
+ }
68
+
25
69
@override
26
70
Future <String > getApplicationSupportPath () async {
27
- final processName = path.basenameWithoutExtension (
28
- await File ('/proc/self/exe' ).resolveSymbolicLinks ());
29
- final directory = Directory (path.join (xdg.dataHome.path, processName));
71
+ final directory = Directory (path.join (xdg.dataHome.path, await _getId ()));
30
72
// Creating the directory if it doesn't exist, because mobile implementations assume the directory exists
31
73
if (! await directory.exists ()) {
32
74
await directory.create (recursive: true );
0 commit comments