LinuxOperatingSystem.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. var lines = File.ReadAllLines(FilePath).Select<string, (string Key, string Value)>(x =>
  41. {
  42. var separatorIndex = x.IndexOf('=');
  43. return (x[..separatorIndex], x[(separatorIndex + 1)..]);
  44. }).ToList();
  45. var name = lines.FirstOrDefault(x => x.Key == "NAME").Value.Trim('"');
  46. var version = lines.FirstOrDefault(x => x.Key == "VERSION").Value.Trim('"');
  47. return new LinuxOSInformation(name, version, true);
  48. }
  49. public bool Available { get; }
  50. public string? Name { get; private set; }
  51. public string? Version { get; private set; }
  52. public override string ToString() => $"{Name} {Version}";
  53. }
  54. }