diff options
author | Rodrigo Moya <rodrigo.moya@xamarin.com> | 2019-07-18 14:41:28 +0300 |
---|---|---|
committer | Rodrigo Moya <rodrigo.moya@xamarin.com> | 2019-07-25 12:31:12 +0300 |
commit | 4740ce100bad0328d90bdbae638a61d34816e292 (patch) | |
tree | 4ed6c7e201d167ceda0cd92e3c050c1bebf83efc /main | |
parent | c6af3e627a728356c3ae44d2c35806862feaf583 (diff) |
[MacPlatform] Fix retrieval of installed applications
Also, remove some old CoreFoundation bindings which are not needed
anymore, as same code is available in Xamarin.Mac already.
Diffstat (limited to 'main')
-rw-r--r-- | main/src/addins/MacPlatform/MacInterop/CoreFoundation.cs | 70 | ||||
-rw-r--r-- | main/src/addins/MacPlatform/MacPlatform.cs | 26 | ||||
-rw-r--r-- | main/tests/MacPlatform.Tests/CoreFoundationTests.cs | 63 |
3 files changed, 15 insertions, 144 deletions
diff --git a/main/src/addins/MacPlatform/MacInterop/CoreFoundation.cs b/main/src/addins/MacPlatform/MacInterop/CoreFoundation.cs index e2d4ab8949..8fbdcd6814 100644 --- a/main/src/addins/MacPlatform/MacInterop/CoreFoundation.cs +++ b/main/src/addins/MacPlatform/MacInterop/CoreFoundation.cs @@ -150,75 +150,5 @@ namespace MonoDevelop.MacInterop Shell = 0x00000008, All = 0xFFFFFFFF } - - public static IntPtr CreatePathUrl (string path) - { - IntPtr str = IntPtr.Zero; - IntPtr url = IntPtr.Zero; - try { - str = CreateString (path); - if (str == IntPtr.Zero) - throw new Exception ("CreateString failed"); - url = CFURLCreateWithFileSystemPath (IntPtr.Zero, str, CFUrlPathStyle.Posix, false); - if (url == IntPtr.Zero) - throw new Exception ("CFURLCreateWithFileSystemPath failed"); - return url; - } finally { - if (str != IntPtr.Zero) - Release (str); - } - } - - public static string UrlToPath (IntPtr url) - { - IntPtr str = IntPtr.Zero; - try { - str = CFURLCopyFileSystemPath (url, CFUrlPathStyle.Posix); - return str == IntPtr.Zero? null : FetchString (str); - } finally { - if (str != IntPtr.Zero) - Release (str); - } - } - - public static string GetApplicationUrl (string filePath, LSRolesMask roles) - { - IntPtr url = IntPtr.Zero; - try { - url = CreatePathUrl (filePath); - IntPtr appUrl = IntPtr.Zero; - if (LSGetApplicationForURL (url, roles, IntPtr.Zero, ref appUrl) == 0 && appUrl != IntPtr.Zero) - return UrlToPath (appUrl); - return null; - } finally { - if (url != IntPtr.Zero) - Release (url); - } - } - - public static string[] GetApplicationUrls (string filePath, LSRolesMask roles) - { - IntPtr url = IntPtr.Zero; - IntPtr arr = IntPtr.Zero; - try { - url = CreatePathUrl (filePath); - arr = LSCopyApplicationURLsForURL (url, roles); - if (arr == IntPtr.Zero) - return new string[0]; - int count = CFArrayGetCount (arr); - string[] values = new string [count]; - for (int i = 0; i < values.Length; i++ ) { - var u = CFArrayGetValueAtIndex (arr, i); - if (u != IntPtr.Zero) - values[i] = UrlToPath (u); - } - return values; - } finally { - if (url != IntPtr.Zero) - Release (url); - if (arr != IntPtr.Zero) - Release (arr); - } - } } } diff --git a/main/src/addins/MacPlatform/MacPlatform.cs b/main/src/addins/MacPlatform/MacPlatform.cs index 49cadaa103..aa7eea5109 100644 --- a/main/src/addins/MacPlatform/MacPlatform.cs +++ b/main/src/addins/MacPlatform/MacPlatform.cs @@ -980,18 +980,19 @@ namespace MonoDevelop.MacIntegration checkUniqueName.Add ("MonoDevelop"); checkUniqueName.Add (BrandingService.ApplicationName); - string def = MonoDevelop.MacInterop.CoreFoundation.GetApplicationUrl (filename, - MonoDevelop.MacInterop.CoreFoundation.LSRolesMask.All); + var def = global::CoreServices.LaunchServices.GetDefaultApplicationUrlForUrl (NSUrl.FromString (filename)); var apps = new List<DesktopApplication> (); - foreach (var app in MonoDevelop.MacInterop.CoreFoundation.GetApplicationUrls (filename, - MonoDevelop.MacInterop.CoreFoundation.LSRolesMask.All)) { - if (string.IsNullOrEmpty (app) || !checkUniquePath.Add (app)) - continue; - var name = NSFileManager.DefaultManager.DisplayName (app); - if (checkUniqueName.Add (name)) - apps.Add (new MacDesktopApplication (app, name, def != null && def == app)); + var retrievedApps = global::CoreServices.LaunchServices.GetApplicationUrlsForUrl (NSUrl.FromString (filename), global::CoreServices.LSRoles.All); + if (retrievedApps != null) { + foreach (var app in retrievedApps) { + if (string.IsNullOrEmpty (app.Path) || !checkUniquePath.Add (app.Path)) + continue; + if (checkUniqueName.Add (app.LastPathComponent)) { + apps.Add (new MacDesktopApplication (app.Path, app.LastPathComponent, def != null && def == app)); + } + } } apps.Sort ((DesktopApplication a, DesktopApplication b) => { @@ -1012,8 +1013,11 @@ namespace MonoDevelop.MacIntegration public override void Launch (params string[] files) { - foreach (var file in files) - NSWorkspace.SharedWorkspace.OpenFile (file, Id); + NSWorkspace.SharedWorkspace.OpenUrls ( + files.Select (NSUrl.FromString).ToArray (), + NSBundle.FromPath (Id).BundleIdentifier, + NSWorkspaceLaunchOptions.Default, + NSAppleEventDescriptor.DescriptorWithBoolean (true)); } } diff --git a/main/tests/MacPlatform.Tests/CoreFoundationTests.cs b/main/tests/MacPlatform.Tests/CoreFoundationTests.cs index 462af84a36..d685170e24 100644 --- a/main/tests/MacPlatform.Tests/CoreFoundationTests.cs +++ b/main/tests/MacPlatform.Tests/CoreFoundationTests.cs @@ -43,69 +43,6 @@ namespace MacPlatform.Tests MonoDevelop.MacInterop.CoreFoundation.Release (testString); } - - [Test] - public static void TestURL () - { - string test = "http://www.monodevelop.org"; - IntPtr testUrl = MonoDevelop.MacInterop.CoreFoundation.CreatePathUrl (test); - - Assert.AreEqual (MonoDevelop.MacInterop.CoreFoundation.UrlToPath (testUrl), test); - - MonoDevelop.MacInterop.CoreFoundation.Release (testUrl); - } - - static string plistFile = Path.Combine (Util.TestsRootDir, "test-projects", "mac-platform", "Info.plist.in"); - - [Test] - public void TestApplicationUrls () - { - using (var helper = new PListFile ()) { - string [] results = MonoDevelop.MacInterop.CoreFoundation.GetApplicationUrls (helper.FilePath, MonoDevelop.MacInterop.CoreFoundation.LSRolesMask.All); - - Assert.Greater (results.Length, 0); - } - } - - [Test] - public void TestApplicationUrl () - { - using (var helper = new PListFile ()) { - string result = MonoDevelop.MacInterop.CoreFoundation.GetApplicationUrl (helper.FilePath, MonoDevelop.MacInterop.CoreFoundation.LSRolesMask.All); - - Assert.NotNull (result); - } - } - - [Test] - public void TestApplicationUrlOnPListIn () - { - string [] results = MonoDevelop.MacInterop.CoreFoundation.GetApplicationUrls (plistFile, MonoDevelop.MacInterop.CoreFoundation.LSRolesMask.All); - - Assert.AreEqual (0, results.Length); - - string result = MonoDevelop.MacInterop.CoreFoundation.GetApplicationUrl (plistFile, MonoDevelop.MacInterop.CoreFoundation.LSRolesMask.All); - - Assert.Null (result); - } - - class PListFile : IDisposable - { - readonly string dir = MonoDevelop.Core.FileService.CreateTempDirectory (); - public string FilePath { get; } - - public PListFile () - { - FilePath = Path.Combine (dir, "Info.plist"); - File.Copy (plistFile, FilePath, true); - } - - public void Dispose () - { - if (Directory.Exists (dir)) - Directory.Delete (dir, true); - } - } } } |