|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using GitCredentialManager.Interop.MacOS.Native; |
| 4 | +using static GitCredentialManager.Interop.MacOS.Native.CoreFoundation; |
| 5 | + |
| 6 | +namespace GitCredentialManager.Interop.MacOS; |
| 7 | + |
| 8 | +public class MacOSPreferences |
| 9 | +{ |
| 10 | + private readonly string _appId; |
| 11 | + |
| 12 | + public MacOSPreferences(string appId) |
| 13 | + { |
| 14 | + EnsureArgument.NotNull(appId, nameof(appId)); |
| 15 | + |
| 16 | + _appId = appId; |
| 17 | + } |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Return a <see cref="string"/> typed value from the app preferences. |
| 21 | + /// </summary> |
| 22 | + /// <param name="key">Preference name.</param> |
| 23 | + /// <exception cref="InvalidOperationException">Thrown if the preference is not a string.</exception> |
| 24 | + /// <returns> |
| 25 | + /// <see cref="string"/> or null if the preference with the given key does not exist. |
| 26 | + /// </returns> |
| 27 | + public string GetString(string key) |
| 28 | + { |
| 29 | + return TryGet(key, CFStringToString, out string value) |
| 30 | + ? value |
| 31 | + : null; |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Return a <see cref="int"/> typed value from the app preferences. |
| 36 | + /// </summary> |
| 37 | + /// <param name="key">Preference name.</param> |
| 38 | + /// <exception cref="InvalidOperationException">Thrown if the preference is not an integer.</exception> |
| 39 | + /// <returns> |
| 40 | + /// <see cref="int"/> or null if the preference with the given key does not exist. |
| 41 | + /// </returns> |
| 42 | + public int? GetInteger(string key) |
| 43 | + { |
| 44 | + return TryGet(key, CFNumberToInt32, out int value) |
| 45 | + ? value |
| 46 | + : null; |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Return a <see cref="IDictionary{TKey,TValue}"/> typed value from the app preferences. |
| 51 | + /// </summary> |
| 52 | + /// <param name="key">Preference name.</param> |
| 53 | + /// <exception cref="InvalidOperationException">Thrown if the preference is not a dictionary.</exception> |
| 54 | + /// <returns> |
| 55 | + /// <see cref="IDictionary{TKey,TValue}"/> or null if the preference with the given key does not exist. |
| 56 | + /// </returns> |
| 57 | + public IDictionary<string, string> GetDictionary(string key) |
| 58 | + { |
| 59 | + return TryGet(key, CFDictionaryToDictionary, out IDictionary<string, string> value) |
| 60 | + ? value |
| 61 | + : null; |
| 62 | + } |
| 63 | + |
| 64 | + private bool TryGet<T>(string key, Func<IntPtr, T> converter, out T value) |
| 65 | + { |
| 66 | + IntPtr cfValue = IntPtr.Zero; |
| 67 | + IntPtr keyPtr = IntPtr.Zero; |
| 68 | + IntPtr appIdPtr = CreateAppIdPtr(); |
| 69 | + |
| 70 | + try |
| 71 | + { |
| 72 | + keyPtr = CFStringCreateWithCString(IntPtr.Zero, key, CFStringEncoding.kCFStringEncodingUTF8); |
| 73 | + cfValue = CFPreferencesCopyAppValue(keyPtr, appIdPtr); |
| 74 | + |
| 75 | + if (cfValue == IntPtr.Zero) |
| 76 | + { |
| 77 | + value = default; |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + value = converter(cfValue); |
| 82 | + return true; |
| 83 | + } |
| 84 | + finally |
| 85 | + { |
| 86 | + if (cfValue != IntPtr.Zero) CFRelease(cfValue); |
| 87 | + if (keyPtr != IntPtr.Zero) CFRelease(keyPtr); |
| 88 | + if (appIdPtr != IntPtr.Zero) CFRelease(appIdPtr); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private IntPtr CreateAppIdPtr() |
| 93 | + { |
| 94 | + return CFStringCreateWithCString(IntPtr.Zero, _appId, CFStringEncoding.kCFStringEncodingUTF8); |
| 95 | + } |
| 96 | +} |
0 commit comments