LinuxOperatingSystem.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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; }
  12. public IProcessUtility ProcessUtility { get; }
  13. public void OpenUri(string uri)
  14. {
  15. throw new NotImplementedException();
  16. }
  17. public void OpenFolder(string path)
  18. {
  19. throw new NotImplementedException();
  20. }
  21. public bool HandleNewInstance(Dispatcher? dispatcher, Action<string> openInExistingAction, IApplicationLifetime lifetime)
  22. {
  23. return true;
  24. }
  25. class LinuxOSInformation
  26. {
  27. const string FilePath = "/etc/os-release";
  28. private LinuxOSInformation(string? name, string? version, bool available)
  29. {
  30. Name = name;
  31. Version = version;
  32. Available = available;
  33. }
  34. public static LinuxOSInformation FromReleaseFile()
  35. {
  36. if (!File.Exists(FilePath))
  37. {
  38. return new LinuxOSInformation(null, null, false);
  39. }
  40. // Parse /etc/os-release file (e.g. 'NAME="Ubuntu"')
  41. var lines = File.ReadAllLines(FilePath).Select<string, (string Key, string Value)>(x =>
  42. {
  43. var separatorIndex = x.IndexOf('=');
  44. return (x[..separatorIndex], x[(separatorIndex + 1)..]);
  45. }).ToList();
  46. var name = lines.FirstOrDefault(x => x.Key == "NAME").Value.Trim('"');
  47. var version = lines.FirstOrDefault(x => x.Key == "VERSION").Value.Trim('"');
  48. return new LinuxOSInformation(name, version, true);
  49. }
  50. public bool Available { get; }
  51. public string? Name { get; private set; }
  52. public string? Version { get; private set; }
  53. public override string ToString() => $"{Name} {Version}";
  54. }
  55. }