LinuxOperatingSystem.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using Avalonia.Controls.ApplicationLifetimes;
  2. using Avalonia.Input;
  3. using Avalonia.Threading;
  4. using PixiEditor.OperatingSystem;
  5. namespace PixiEditor.Linux;
  6. public sealed class LinuxOperatingSystem : IOperatingSystem
  7. {
  8. public string Name { get; } = "Linux";
  9. public string AnalyticsId => "Linux";
  10. public string AnalyticsName => LinuxOSInformation.FromReleaseFile().ToString();
  11. public IInputKeys InputKeys { get; } = new LinuxInputKeys();
  12. public IProcessUtility ProcessUtility { get; } = new LinuxProcessUtility();
  13. public string ExecutableExtension { get; } = string.Empty;
  14. public void OpenUri(string uri)
  15. {
  16. ProcessUtility.Execute($"xdg-open", uri);
  17. }
  18. public void OpenFolder(string path)
  19. {
  20. try
  21. {
  22. ProcessUtility.Execute($"dbus-send", $"--session --dest=org.freedesktop.FileManager1 --type=method_call /org/freedesktop/FileManager1 org.freedesktop.FileManager1.ShowItems array:string:\"file://{path}\" string:\"\"");
  23. }
  24. catch (Exception e)
  25. {
  26. ProcessUtility.Execute($"xdg-open", Path.GetDirectoryName(path));
  27. }
  28. }
  29. public bool HandleNewInstance(Dispatcher? dispatcher, Action<string, bool> openInExistingAction, IApplicationLifetime lifetime)
  30. {
  31. return true;
  32. }
  33. public void HandleActivatedWithFile(FileActivatedEventArgs fileActivatedEventArgs)
  34. {
  35. // TODO: Check if this is executed on Linux at all
  36. }
  37. public void HandleActivatedWithUri(ProtocolActivatedEventArgs openUriEventArgs)
  38. {
  39. // TODO: Check if this is executed on Linux at all
  40. }
  41. class LinuxOSInformation
  42. {
  43. const string FilePath = "/etc/os-release";
  44. private LinuxOSInformation(string? name, string? version, bool available)
  45. {
  46. Name = name;
  47. Version = version;
  48. Available = available;
  49. }
  50. public static LinuxOSInformation FromReleaseFile()
  51. {
  52. if (!File.Exists(FilePath))
  53. {
  54. return new LinuxOSInformation(null, null, false);
  55. }
  56. // Parse /etc/os-release file (e.g. 'NAME="Ubuntu"')
  57. var lines = File.ReadAllLines(FilePath).Select<string, (string Key, string Value)>(x =>
  58. {
  59. var separatorIndex = x.IndexOf('=');
  60. return (x[..separatorIndex], x[(separatorIndex + 1)..]);
  61. }).ToList();
  62. var name = lines.FirstOrDefault(x => x.Key == "NAME").Value.Trim('"');
  63. var version = lines.FirstOrDefault(x => x.Key == "VERSION").Value.Trim('"');
  64. return new LinuxOSInformation(name, version, true);
  65. }
  66. public bool Available { get; }
  67. public string? Name { get; private set; }
  68. public string? Version { get; private set; }
  69. public override string ToString() => $"{Name} {Version}";
  70. }
  71. }