From 1d86d14a209ddc6a4a713459e67ecf0af07d0ca9 Mon Sep 17 00:00:00 2001 From: Ayase <166034664+AyaseFile@users.noreply.github.com> Date: Thu, 3 Oct 2024 21:40:49 +0800 Subject: [PATCH] fix: open files on linux (#765) --- ui/flutter/lib/util/file_explorer.dart | 40 ++++++++++++++------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/ui/flutter/lib/util/file_explorer.dart b/ui/flutter/lib/util/file_explorer.dart index 060e9fd2..95ebfb17 100644 --- a/ui/flutter/lib/util/file_explorer.dart +++ b/ui/flutter/lib/util/file_explorer.dart @@ -21,29 +21,33 @@ class FileExplorer { } else if (Platform.isMacOS) { Process.run('open', ['-R', filePath]); } else if (Platform.isLinux) { - if (await _isUbuntuOrDebian()) { - Process.run('xdg-open', [filePath]); - } else if (await _isCentOS()) { - Process.run('nautilus', ['--select', filePath]); + _linuxOpen(filePath); + } + } + + static Future _linuxOpen(String filePath) async { + if (await Process.run('which', ['xdg-open']) + .then((value) => value.exitCode == 0)) { + final result = await Process.run('xdg-open', [filePath]); + if (result.exitCode != 0) { + _openWithFileManager(filePath); } + } else { + _openWithFileManager(filePath); } } - static Future _isUbuntuOrDebian() async { - final result = await Process.run('lsb_release', ['-i']); - if (result.exitCode != 0) { - return false; + static Future _openWithFileManager(String filePath) async { + final desktop = Platform.environment['XDG_CURRENT_DESKTOP']; + if (desktop == null) { + throw Exception('XDG_CURRENT_DESKTOP is not set'); } - final output = result.stdout.toString().toLowerCase(); - return output.contains('ubuntu') || output.contains('debian'); - } - - static Future _isCentOS() async { - final result = await Process.run('cat', ['/etc/os-release']); - if (result.exitCode != 0) { - return false; + if (desktop == 'GNOME') { + await Process.run('nautilus', ['--select', filePath]); + } else if (desktop == 'KDE') { + await Process.run('dolphin', ['--select', filePath]); + } else { + throw Exception('Unsupported desktop environment'); } - final output = result.stdout.toString().toLowerCase(); - return output.contains('centos'); } }