/ src / Ryujinx.UI.Common / Helper / OpenHelper.cs
OpenHelper.cs
  1  using Ryujinx.Common.Logging;
  2  using System;
  3  using System.Diagnostics;
  4  using System.IO;
  5  using System.Runtime.InteropServices;
  6  
  7  namespace Ryujinx.UI.Common.Helper
  8  {
  9      public static partial class OpenHelper
 10      {
 11          [LibraryImport("shell32.dll", SetLastError = true)]
 12          private static partial int SHOpenFolderAndSelectItems(IntPtr pidlFolder, uint cidl, IntPtr apidl, uint dwFlags);
 13  
 14          [LibraryImport("shell32.dll", SetLastError = true)]
 15          private static partial void ILFree(IntPtr pidlList);
 16  
 17          [LibraryImport("shell32.dll", SetLastError = true)]
 18          private static partial IntPtr ILCreateFromPathW([MarshalAs(UnmanagedType.LPWStr)] string pszPath);
 19  
 20          public static void OpenFolder(string path)
 21          {
 22              if (Directory.Exists(path))
 23              {
 24                  Process.Start(new ProcessStartInfo
 25                  {
 26                      FileName = path,
 27                      UseShellExecute = true,
 28                      Verb = "open",
 29                  });
 30              }
 31              else
 32              {
 33                  Logger.Notice.Print(LogClass.Application, $"Directory \"{path}\" doesn't exist!");
 34              }
 35          }
 36  
 37          public static void LocateFile(string path)
 38          {
 39              if (File.Exists(path))
 40              {
 41                  if (OperatingSystem.IsWindows())
 42                  {
 43                      IntPtr pidlList = ILCreateFromPathW(path);
 44                      if (pidlList != IntPtr.Zero)
 45                      {
 46                          try
 47                          {
 48                              Marshal.ThrowExceptionForHR(SHOpenFolderAndSelectItems(pidlList, 0, IntPtr.Zero, 0));
 49                          }
 50                          finally
 51                          {
 52                              ILFree(pidlList);
 53                          }
 54                      }
 55                  }
 56                  else if (OperatingSystem.IsMacOS())
 57                  {
 58                      ObjectiveC.NSString nsStringPath = new(path);
 59                      ObjectiveC.Object nsUrl = new("NSURL");
 60                      var urlPtr = nsUrl.GetFromMessage("fileURLWithPath:", nsStringPath);
 61  
 62                      ObjectiveC.Object nsArray = new("NSArray");
 63                      ObjectiveC.Object urlArray = nsArray.GetFromMessage("arrayWithObject:", urlPtr);
 64  
 65                      ObjectiveC.Object nsWorkspace = new("NSWorkspace");
 66                      ObjectiveC.Object sharedWorkspace = nsWorkspace.GetFromMessage("sharedWorkspace");
 67  
 68                      sharedWorkspace.SendMessage("activateFileViewerSelectingURLs:", urlArray);
 69                  }
 70                  else if (OperatingSystem.IsLinux())
 71                  {
 72                      Process.Start("dbus-send", $"--session --print-reply --dest=org.freedesktop.FileManager1 --type=method_call /org/freedesktop/FileManager1 org.freedesktop.FileManager1.ShowItems array:string:\"file://{path}\" string:\"\"");
 73                  }
 74                  else
 75                  {
 76                      OpenFolder(Path.GetDirectoryName(path));
 77                  }
 78              }
 79              else
 80              {
 81                  Logger.Notice.Print(LogClass.Application, $"File \"{path}\" doesn't exist!");
 82              }
 83          }
 84  
 85          public static void OpenUrl(string url)
 86          {
 87              if (OperatingSystem.IsWindows())
 88              {
 89                  Process.Start(new ProcessStartInfo("cmd", $"/c start {url.Replace("&", "^&")}"));
 90              }
 91              else if (OperatingSystem.IsLinux())
 92              {
 93                  Process.Start("xdg-open", url);
 94              }
 95              else if (OperatingSystem.IsMacOS())
 96              {
 97                  ObjectiveC.NSString nsStringPath = new(url);
 98                  ObjectiveC.Object nsUrl = new("NSURL");
 99                  var urlPtr = nsUrl.GetFromMessage("URLWithString:", nsStringPath);
100  
101                  ObjectiveC.Object nsWorkspace = new("NSWorkspace");
102                  ObjectiveC.Object sharedWorkspace = nsWorkspace.GetFromMessage("sharedWorkspace");
103  
104                  sharedWorkspace.GetBoolFromMessage("openURL:", urlPtr);
105              }
106              else
107              {
108                  Logger.Notice.Print(LogClass.Application, $"Cannot open url \"{url}\" on this platform!");
109              }
110          }
111      }
112  }