Esempio n. 1
0
        public NSUrl?[]? GetResourceUrls(string resourceType, string subDirName, string localizationName)
        {
            if (String.IsNullOrEmpty(resourceType))
            {
                throw new ArgumentException(nameof(resourceType));
            }

            if (String.IsNullOrEmpty(localizationName))
            {
                throw new ArgumentException(nameof(localizationName));
            }

            var resourceTypeHandle     = CFString.CreateNative(resourceType);
            var dirNameHandle          = CFString.CreateNative(string.IsNullOrEmpty(subDirName) ? null : subDirName);
            var localizationNameHandle = CFString.CreateNative(localizationName);

            try {
                var rv = CFBundleCopyResourceURLsOfTypeForLocalization(Handle, resourceTypeHandle, dirNameHandle, localizationNameHandle);
                return(CFArray.ArrayFromHandleFunc(rv, (handle) => Runtime.GetNSObject <NSUrl> (handle, true), true));
            } finally {
                CFString.ReleaseNative(resourceTypeHandle);
                CFString.ReleaseNative(dirNameHandle);
                CFString.ReleaseNative(localizationNameHandle);
            }
        }
Esempio n. 2
0
        public static NSUrl?GetResourceUrl(NSUrl bundleUrl, string resourceName, string resourceType, string subDirName)
        {
            if (bundleUrl is null)
            {
                throw new ArgumentNullException(nameof(bundleUrl));
            }

            if (String.IsNullOrEmpty(resourceName))
            {
                throw new ArgumentException(nameof(resourceName));
            }

            if (String.IsNullOrEmpty(resourceType))
            {
                throw new ArgumentException(nameof(resourceType));
            }

            var resourceNameHandle = CFString.CreateNative(resourceName);
            var resourceTypeHandle = CFString.CreateNative(resourceType);
            var dirNameHandle      = CFString.CreateNative(string.IsNullOrEmpty(subDirName) ? null : subDirName);

            try {
                // follows the create rules and therefore we do not need to retain
                var urlHandle = CFBundleCopyResourceURLInDirectory(bundleUrl.Handle, resourceNameHandle, resourceTypeHandle, dirNameHandle);
                return(Runtime.GetNSObject <NSUrl> (urlHandle, true));
            } finally {
                CFString.ReleaseNative(resourceNameHandle);
                CFString.ReleaseNative(resourceTypeHandle);
                CFString.ReleaseNative(dirNameHandle);
            }
        }
Esempio n. 3
0
        public string?GetLocalizedString(string key, string defaultValue, string?tableName)
        {
            if (String.IsNullOrEmpty(key))
            {
                throw new ArgumentException(nameof(key));
            }

            if (String.IsNullOrEmpty(tableName))
            {
                throw new ArgumentException(nameof(tableName));
            }

            // we do allow null and simply use an empty string to avoid the extra check
            if (defaultValue is null)
            {
                defaultValue = string.Empty;
            }

            var keyHandle          = CFString.CreateNative(key);
            var defaultValueHandle = CFString.CreateNative(defaultValue);
            var tableNameHandle    = CFString.CreateNative(tableName);

            try {
                var rv = CFBundleCopyLocalizedString(Handle, keyHandle, defaultValueHandle, tableNameHandle);
                return(CFString.FromHandle(rv, releaseHandle: true));
            } finally {
                CFString.ReleaseNative(keyHandle);
                CFString.ReleaseNative(defaultValueHandle);
                CFString.ReleaseNative(tableNameHandle);
            }
        }
Esempio n. 4
0
        public NSUrl?GetResourceUrl(string resourceName, string resourceType, string subDirName, string localizationName)
        {
            if (String.IsNullOrEmpty(resourceName))
            {
                throw new ArgumentException(nameof(resourceName));
            }

            if (String.IsNullOrEmpty(resourceType))
            {
                throw new ArgumentException(nameof(resourceType));
            }

            if (String.IsNullOrEmpty(localizationName))
            {
                throw new ArgumentException(nameof(localizationName));
            }

            var resourceNameHandle     = CFString.CreateNative(resourceName);
            var resourceTypeHandle     = CFString.CreateNative(resourceType);
            var dirNameHandle          = CFString.CreateNative(string.IsNullOrEmpty(subDirName) ? null : subDirName);
            var localizationNameHandle = CFString.CreateNative(localizationName);

            try {
                var urlHandle = CFBundleCopyResourceURLForLocalization(Handle, resourceNameHandle, resourceTypeHandle, dirNameHandle, localizationNameHandle);
                return(Runtime.GetNSObject <NSUrl> (urlHandle, true));
            } finally {
                CFString.ReleaseNative(resourceNameHandle);
                CFString.ReleaseNative(resourceTypeHandle);
                CFString.ReleaseNative(dirNameHandle);
                CFString.ReleaseNative(localizationNameHandle);
            }
        }
Esempio n. 5
0
        public bool ContainsKey(string key)
        {
            var keyHandle = CFString.CreateNative(key);

            try {
                return(CFDictionaryContainsKey(Handle, keyHandle));
            } finally {
                CFString.ReleaseNative(keyHandle);
            }
        }
Esempio n. 6
0
        public IntPtr GetIntPtrValue(string key)
        {
            var keyHandle = CFString.CreateNative(key);

            try {
                return(CFDictionaryGetValue(Handle, keyHandle));
            } finally {
                CFString.ReleaseNative(keyHandle);
            }
        }
Esempio n. 7
0
        public string?GetStringValue(string key)
        {
            var keyHandle = CFString.CreateNative(key);

            try {
                return(CFString.FromHandle(CFDictionaryGetValue(Handle, keyHandle)));
            } finally {
                CFString.ReleaseNative(keyHandle);
            }
        }
Esempio n. 8
0
        public CFDictionary?GetDictionaryValue(string key)
        {
            var keyHandle = CFString.CreateNative(key);

            try {
                var ptr = CFDictionaryGetValue(Handle, keyHandle);
                return(ptr == IntPtr.Zero ? null : new CFDictionary(ptr, false));
            } finally {
                CFString.ReleaseNative(keyHandle);
            }
        }
Esempio n. 9
0
        static public CFUrl?FromUrlString(string url, CFUrl?baseurl)
        {
            if (url is null)
            {
                throw new ArgumentNullException(nameof(url));
            }
            var strHandle = CFString.CreateNative(url);

            try {
                return(FromStringHandle(strHandle, baseurl));
            } finally {
                CFString.ReleaseNative(strHandle);
            }
        }
Esempio n. 10
0
        public CFNotificationObserverToken AddObserver(string name, INativeObject objectToObserve, Action <string, NSDictionary?> notificationHandler,
                                                       CFNotificationSuspensionBehavior suspensionBehavior = CFNotificationSuspensionBehavior.DeliverImmediately)
        {
            if (darwinnc is not null && darwinnc.Handle == Handle)
            {
                if (name is null)
                {
                    throw new ArgumentNullException(nameof(name), "When using the Darwin Notification Center, the value passed must not be null");
                }
            }

            var strHandle = CFString.CreateNative(name);

            name = name ?? NullNotificationName;
            var token = new CFNotificationObserverToken(name)
            {
                centerHandle   = Handle,
                nameHandle     = strHandle,
                observedObject = objectToObserve.GetHandle(),
                listener       = notificationHandler
            };

            //
            // To allow callbacks to add observers, we duplicate the list of listeners on AddObserver
            // We do the duplication on AddObserver, instead of making a copy on the notification
            // callback, as we expect the notification callback to be a more common operation
            // than the AddObserver operation
            //
            List <CFNotificationObserverToken>?listenersForName;

            lock (listeners){
                if (!listeners.TryGetValue(name, out listenersForName))
                {
                    listenersForName = new List <CFNotificationObserverToken> (1);
                    CFNotificationCenterAddObserver(center: Handle,
                                                    observer: Handle,
                                                    callback: NotificationCallback,
                                                    name: strHandle,
                                                    obj: token.observedObject,
                                                    suspensionBehavior: (IntPtr)suspensionBehavior);
                }
                else
                {
                    listenersForName = new List <CFNotificationObserverToken> (listenersForName);
                }
                listenersForName.Add(token);
                listeners [name] = listenersForName;
            }
            return(token);
        }
Esempio n. 11
0
        public long GetInt64Value(string key)
        {
            long value     = 0;
            var  keyHandle = CFString.CreateNative(key);

            try {
                if (!CFNumberGetValue(CFDictionaryGetValue(Handle, keyHandle), /* kCFNumberSInt64Type */ 4, out value))
                {
                    throw new System.Collections.Generic.KeyNotFoundException(string.Format("Key {0} not found", key));
                }
                return(value);
            } finally {
                CFString.ReleaseNative(keyHandle);
            }
        }
Esempio n. 12
0
        public static CFMessagePort?CreateRemotePort(CFAllocator?allocator, string name)
        {
            if (name is null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            var n = CFString.CreateNative(name);

            try {
                var portHandle = CFMessagePortCreateRemote(allocator.GetHandle(), n);
                return(portHandle == IntPtr.Zero ? null : new CFMessagePort(portHandle, true));
            } finally {
                CFString.ReleaseNative(n);
            }
        }
Esempio n. 13
0
        public NSUrl?GetAuxiliaryExecutableUrl(string executableName)
        {
            if (String.IsNullOrEmpty(executableName))
            {
                throw new ArgumentException(nameof(executableName));
            }
            var executableNameHandle = CFString.CreateNative(executableName);

            try {
                // follows the create rule no need to retain
                var urlHandle = CFBundleCopyAuxiliaryExecutableURL(Handle, executableNameHandle);
                return(Runtime.GetNSObject <NSUrl> (urlHandle, true));
            } finally {
                CFString.ReleaseNative(executableNameHandle);
            }
        }
Esempio n. 14
0
        static public CFUrl?FromFile(string filename)
        {
            if (filename is null)
            {
                throw new ArgumentNullException(nameof(filename));
            }
            var strHandle = CFString.CreateNative(filename);

            try {
                var handle = CFURLCreateWithFileSystemPath(IntPtr.Zero, strHandle, (nint)(long)CFUrlPathStyle.POSIX, false);
                if (handle == IntPtr.Zero)
                {
                    return(null);
                }
                return(new CFUrl(handle, true));
            } finally {
                CFString.ReleaseNative(strHandle);
            }
        }
Esempio n. 15
0
        public static CFBundle[]? GetBundlesFromDirectory(NSUrl directoryUrl, string bundleType)
        {
            if (directoryUrl is null)             // NSUrl cannot be "" by definition
            {
                throw new ArgumentNullException(nameof(directoryUrl));
            }
            if (String.IsNullOrEmpty(bundleType))
            {
                throw new ArgumentException(nameof(bundleType));
            }
            var bundleTypeHandle = CFString.CreateNative(bundleType);

            try {
                var rv = CFBundleCreateBundlesFromDirectory(IntPtr.Zero, directoryUrl.Handle, bundleTypeHandle);
                return(CFArray.ArrayFromHandleFunc(rv, (handle) => new CFBundle(handle, true), true));
            } finally {
                CFString.ReleaseNative(bundleTypeHandle);
            }
        }
Esempio n. 16
0
        public static CFBundle?Get(string bundleID)
        {
            if (String.IsNullOrEmpty(bundleID))
            {
                throw new ArgumentException(nameof(bundleID));
            }
            var bundleIDHandler = CFString.CreateNative(bundleID);

            try {
                var cfBundle = CFBundleGetBundleWithIdentifier(bundleIDHandler);
                if (cfBundle == IntPtr.Zero)
                {
                    return(null);
                }
                // follow the Get rule and retain the obj
                return(new CFBundle(cfBundle, false));
            } finally {
                CFString.ReleaseNative(bundleIDHandler);
            }
        }
Esempio n. 17
0
        internal static CFMessagePort?CreateLocalPort(CFAllocator?allocator, string?name, CFMessagePortCallBack callback, CFMessagePortContext?context)
        {
            var  n = CFString.CreateNative(name);
            bool shouldFreeInfo = false;
            var  contextProxy   = new ContextProxy();

            // a GCHandle is needed because we do not have an handle before calling CFMessagePortCreateLocal
            // and that will call the RetainProxy. So using this (short-lived) GCHandle allow us to find back the
            // original context defined by developer
            var shortHandle = GCHandle.Alloc(contextProxy);

            if (context is not null)
            {
                if (context.Retain is not null)
                {
                    contextProxy.retain = RetainProxy;
                }
                if (context.Release is not null)
                {
                    contextProxy.release = ReleaseProxy;
                }
                if (context.CopyDescription is not null)
                {
                    contextProxy.copyDescription = CopyDescriptionProxy;
                }
                contextProxy.info = (IntPtr)shortHandle;
                lock (messagePortContexts)
                    messagePortContexts.Add(contextProxy.info, context);
            }

            try {
                var portHandle = CFMessagePortCreateLocal(allocator.GetHandle(), n, messageOutputCallback, ref contextProxy, ref shouldFreeInfo);

                // TODO handle should free info
                if (portHandle == IntPtr.Zero)
                {
                    return(null);
                }

                var result = new CFMessagePort(portHandle, true);

                lock (outputHandles)
                    outputHandles.Add(portHandle, callback);

                if (context is not null)
                {
                    lock (messagePortContexts) {
                        messagePortContexts.Remove(contextProxy.info);
                        CFMessagePortGetContext(portHandle, ref contextProxy);
                        messagePortContexts.Add(contextProxy.info, context);
                    }

                    result.contextHandle = contextProxy.info;
                }

                return(result);
            } finally {
                CFString.ReleaseNative(n);

                // we won't need short GCHandle after the Create call
                shortHandle.Free();
            }
        }