Example #1
0
        public string GetMimeType(string fileName)
        {
            try
            {
                var extensionWithDot = Path.GetExtension(fileName);
                if (!string.IsNullOrWhiteSpace(extensionWithDot))
                {
                    var extension = extensionWithDot.Substring(1);
                    if (!string.IsNullOrWhiteSpace(extension))
                    {
                        var extensionClassRef = new NSString(UTType.TagClassFilenameExtension);
                        var mimeTypeClassRef  = new NSString(UTType.TagClassMIMEType);

                        var uti      = NativeTools.UTTypeCreatePreferredIdentifierForTag(extensionClassRef.Handle, new NSString(extension).Handle, IntPtr.Zero);
                        var mimeType = NativeTools.UTTypeCopyPreferredTagWithClass(uti, mimeTypeClassRef.Handle);
                        using (var mimeTypeCString = new CoreFoundation.CFString(mimeType))
                        {
                            return(mimeTypeCString);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }

            return("*/*");
        }
Example #2
0
        unsafe public CFDictionary(string[] keys,IntPtr[] values)
        {
            IntPtr[] keyz = new IntPtr[keys.Length];  
            for (int i = 0; i < keys.Length; i++)
            {
                keyz[i] = new CFString(keys[i]);                
            }
            CFDictionaryKeyCallBacks kcall = new CFDictionaryKeyCallBacks();

            CFDictionaryValueCallBacks vcall = new CFDictionaryValueCallBacks();
            base.typeRef = CFLibrary.CFDictionaryCreate(IntPtr.Zero,keyz,values,keys.Length,ref kcall,ref vcall);            
        }
Example #3
0
        public long GetInt64Value(string key)
        {
            long value = 0;

            using (var str = new CFString(key)) {
                if (!CFNumberGetValue(CFDictionaryGetValue(Handle, str.Handle), /* kCFNumberSInt64Type */ 4, out value))
                {
                    throw new System.Collections.Generic.KeyNotFoundException(string.Format("Key {0} not found", key));
                }
                return(value);
            }
        }
 public CFMutableString(CFString theString, nint maxLength = default(nint))
 {
     if (theString == null)
     {
         throw new ArgumentNullException(nameof(theString));
     }
     // NSMallocException Out of memory. We suggest restarting the application. If you have an unsaved document, create a backup copy in Finder, then try to save.
     if (maxLength < 0)
     {
         throw new ArgumentException(nameof(maxLength));
     }
     Handle = CFStringCreateMutableCopy(IntPtr.Zero, maxLength, theString.GetHandle());
 }
Example #5
0
 public static void CreatePairWithSocketToHost(string host, int port,
                                               out CFReadStream?readStream,
                                               out CFWriteStream?writeStream)
 {
     using (var str = new CFString(host)) {
         IntPtr read, write;
         CFStreamCreatePairWithSocketToHost(
             IntPtr.Zero, str.Handle, port, out read, out write);
         // API not annotated (yet?) but it's safe to bet it match CFStreamCreatePairWithSocketToCFHost
         readStream  = read == IntPtr.Zero ? null : new CFReadStream(read, true);
         writeStream = write == IntPtr.Zero ? null : new CFWriteStream(write, true);
     }
 }
Example #6
0
 public override string?ToString()
 {
     if (copyDescription != IntPtr.Zero)
     {
         var ptr = CFReadStreamRef_InvokeCopyDescription(copyDescription, Info);
         if (ptr != IntPtr.Zero)
         {
             // Copy* -> so we must not retain again
             return(CFString.FromHandle(ptr, true));
         }
     }
     return(base.ToString());
 }
Example #7
0
        unsafe public CFDictionary(string[] keys, IntPtr[] values)
        {
            IntPtr[] keyz = new IntPtr[keys.Length];
            for (int i = 0; i < keys.Length; i++)
            {
                keyz[i] = new CFString(keys[i]);
            }
            CFDictionaryKeyCallBacks kcall = new CFDictionaryKeyCallBacks();

            CFDictionaryValueCallBacks vcall = new CFDictionaryValueCallBacks();

            base.typeRef = CFLibrary.CFDictionaryCreate(IntPtr.Zero, keyz, values, keys.Length, ref kcall, ref vcall);
        }
Example #8
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);
            }
        }
Example #9
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);
        }
Example #10
0
 public override string ToString()
 {
     if (copyDescription != IntPtr.Zero)
     {
         var ptr = CFReadStreamRef_InvokeCopyDescription(copyDescription, Info);
         if (ptr != IntPtr.Zero)
         {
             // Copy* -> so we must not retain again
             using (var s = new CFString(ptr, true))
                 return(s.ToString());
         }
     }
     return(base.ToString());
 }
Example #11
0
        public CFPropertyList(string plistlocation) 
        {            
            IntPtr inputfilename;
            inputfilename = new CFString(plistlocation);

            IntPtr ifile_IntPtr = CFLibrary.CFURLCreateWithFileSystemPath(IntPtr.Zero, inputfilename, 2, false);
            IntPtr ifile_CFReadStreamRef = CFLibrary.CFReadStreamCreateWithFile(IntPtr.Zero, ifile_IntPtr);
            if ((CFLibrary.CFReadStreamOpen(ifile_CFReadStreamRef)) == false)
            {
                typeRef = IntPtr.Zero;
            }
            IntPtr PlistRef = CFLibrary.CFPropertyListCreateFromStream(IntPtr.Zero, ifile_CFReadStreamRef, 0, 2, 0, IntPtr.Zero);
            CFLibrary.CFReadStreamClose(ifile_CFReadStreamRef);
            typeRef = PlistRef;
        }
Example #12
0
        public static bool AppValueIsForced(string key, NSString applicationId)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            else if (applicationId == null)
            {
                throw new ArgumentNullException("applicationId");
            }

            using (var cfKey = new CFString(key)) {
                return(CFPreferencesAppValueIsForced(cfKey.Handle, applicationId.Handle));
            }
        }
Example #13
0
        public static void RemoveSuitePreferencesFromApp(NSString applicationId, string suiteId)
        {
            if (applicationId == null)
            {
                throw new ArgumentNullException("applicationId");
            }
            else if (suiteId == null)
            {
                throw new ArgumentNullException("suiteId");
            }

            using (var cfSuiteId = new CFString(suiteId)) {
                CFPreferencesRemoveSuitePreferencesFromApp(applicationId.Handle, cfSuiteId.Handle);
            }
        }
Example #14
0
        public static nint GetAppIntegerValue(string key, NSString applicationId)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            else if (applicationId == null)
            {
                throw new ArgumentNullException("applicationId");
            }

            using (var cfKey = new CFString(key)) {
                return(CFPreferencesGetAppIntegerValue(cfKey.Handle, applicationId.Handle, IntPtr.Zero));
            }
        }
Example #15
0
 public static string[] GetLocalizations(NSUrl bundle)
 {
     if (bundle == null)
     {
         throw new ArgumentNullException("bundle");
     }
     using (var cfArray = new CFArray(CFBundleCopyLocalizationsForURL(bundle.Handle), true)) {
         var cultureInfo = new string [cfArray.Count];
         for (int index = 0; index < cfArray.Count; index++)
         {
             cultureInfo [index] = CFString.FetchString(cfArray.GetValue(index));
         }
         return(cultureInfo);
     }
 }
Example #16
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);
            }
        }
Example #17
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);
            }
        }
Example #18
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);
            }
        }
Example #19
0
 public static CFBundle Get(string bundleID)
 {
     if (String.IsNullOrEmpty(bundleID))
     {
         throw new ArgumentException("bundleID");
     }
     using (var cfBundleId = new CFString(bundleID)) {
         var cfBundle = CFBundleGetBundleWithIdentifier(cfBundleId.Handle);
         if (cfBundle == IntPtr.Zero)
         {
             return(null);
         }
         // follow the Get rule and retain the obj
         return(new CFBundle(cfBundle, false));
     }
 }
Example #20
0
 public NSUrl GetAuxiliaryExecutableUrl(string executableName)
 {
     if (String.IsNullOrEmpty(executableName))
     {
         throw new ArgumentException("executableName");
     }
     using (var cfExecutableName = new CFString(executableName)) {
         // follows the create rule no need to retain
         var urlHandle = CFBundleCopyAuxiliaryExecutableURL(handle, cfExecutableName.Handle);
         if (urlHandle == IntPtr.Zero)
         {
             return(null);
         }
         return(Runtime.GetNSObject <NSUrl> (urlHandle, true));
     }
 }
Example #21
0
        public static void SetAppValue(string key, object value, NSString applicationId)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            else if (applicationId == null)
            {
                throw new ArgumentNullException("applicationId");
            }

            using (var cfKey = new CFString(key)) {
                if (value == null)
                {
                    CFPreferencesSetAppValue(cfKey.Handle, IntPtr.Zero, applicationId.Handle);
                }
                else if (value is string)
                {
                    using (var valueStr = new CFString((string)value)) {
                        CFPreferencesSetAppValue(cfKey.Handle, valueStr.Handle, applicationId.Handle);
                    }

                    return;
                }
                else if (value is NSString || value is CFString ||
                         value is NSData || value is CFData ||
                         value is NSArray || value is CFArray ||
                         value is NSDictionary || value is CFDictionary ||
                         value is NSNumber || value is CFBoolean)
                {
                    CFPreferencesSetAppValue(cfKey.Handle, ((INativeObject)value).Handle, applicationId.Handle);
                    return;
                }

                var nsnumber = NSNumber.FromObject(value);
                if (nsnumber != null)
                {
                    using (nsnumber) {
                        CFPreferencesSetAppValue(cfKey.Handle, nsnumber.Handle, applicationId.Handle);
                    }
                    return;
                }

                throw new ArgumentException("unsupported type: " + value.GetType(), "value");
            }
        }
Example #22
0
        public static string?[]? GetPreferredLocalizations(string[] locArray)
        {
            if (locArray is null)
            {
                throw new ArgumentNullException(nameof(locArray));
            }

            var cfString = new CFString [locArray.Length];

            for (int index = 0; index < locArray.Length; index++)
            {
                cfString [index] = new CFString(locArray [index]);
            }
            using (var cfLocArray = CFArray.FromNativeObjects(cfString)) {
                var rv = CFBundleCopyPreferredLocalizationsFromArray(cfLocArray.Handle);
                return(CFArray.StringArrayFromHandle(rv, true));
            }
        }
Example #23
0
        public NSUrl?[]? GetResourceUrls(string resourceType, string subDirName)
        {
            if (String.IsNullOrEmpty(resourceType))
            {
                throw new ArgumentException(nameof(resourceType));
            }

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

            try {
                var rv = CFBundleCopyResourceURLsOfType(Handle, resourceTypeHandle, dirNameHandle);
                return(CFArray.ArrayFromHandleFunc(rv, (handle) => Runtime.GetNSObject <NSUrl> (handle, true), true));
            } finally {
                CFString.ReleaseNative(resourceTypeHandle);
                CFString.ReleaseNative(dirNameHandle);
            }
        }
Example #24
0
        public CFPropertyList(string plistlocation)
        {
            IntPtr inputfilename;

            inputfilename = new CFString(plistlocation);

            IntPtr ifile_IntPtr          = CFLibrary.CFURLCreateWithFileSystemPath(IntPtr.Zero, inputfilename, 2, false);
            IntPtr ifile_CFReadStreamRef = CFLibrary.CFReadStreamCreateWithFile(IntPtr.Zero, ifile_IntPtr);

            if ((CFLibrary.CFReadStreamOpen(ifile_CFReadStreamRef)) == false)
            {
                typeRef = IntPtr.Zero;
            }
            IntPtr PlistRef = CFLibrary.CFPropertyListCreateFromStream(IntPtr.Zero, ifile_CFReadStreamRef, 0, 2, 0, IntPtr.Zero);

            CFLibrary.CFReadStreamClose(ifile_CFReadStreamRef);
            typeRef = PlistRef;
        }
Example #25
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);
            }
        }
Example #26
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);
            }
        }
Example #27
0
        public NSUrl[] GetResourceUrls(string resourceType, string subDirName)
        {
            if (String.IsNullOrEmpty(resourceType))
            {
                throw new ArgumentException("resourceName");
            }

            using (CFString cfResourceType = new CFString(resourceType),
                   cfSubDir = new CFString(subDirName ?? string.Empty))
                using (var cfArray = new CFArray(CFBundleCopyResourceURLsOfType(handle, cfResourceType.Handle,
                                                                                String.IsNullOrEmpty(subDirName) ? IntPtr.Zero : cfSubDir.Handle), true)) {
                    var result = new NSUrl [cfArray.Count];
                    for (int index = 0; index < cfArray.Count; index++)
                    {
                        result [index] = Runtime.GetNSObject <NSUrl> (cfArray.GetValue(index), true);
                    }
                    return(result);
                }
        }
Example #28
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);
            }
        }
Example #29
0
 public static CFBundle[] GetBundlesFromDirectory(NSUrl directoryUrl, string bundleType)
 {
     if (directoryUrl == null)             // NSUrl cannot be "" by definition
     {
         throw new ArgumentNullException("directoryUrl");
     }
     if (String.IsNullOrEmpty(bundleType))
     {
         throw new ArgumentException("bundleType");
     }
     using (var bundleTypeCFSting = new CFString(bundleType))
         using (var cfBundles = new CFArray(CFBundleCreateBundlesFromDirectory(IntPtr.Zero, directoryUrl.Handle, bundleTypeCFSting.Handle), true)) {
             var managedBundles = new CFBundle [cfBundles.Count];
             for (int index = 0; index < cfBundles.Count; index++)
             {
                 // follow the create rules, therefore we do have ownership of each of the cfbundles
                 managedBundles [index] = new CFBundle(cfBundles.GetValue(index), true);
             }
             return(managedBundles);
         }
 }
Example #30
0
        public NSUrl GetResourceUrl(string resourceName, string resourceType, string subDirName)
        {
            if (String.IsNullOrEmpty(resourceName))
            {
                throw new ArgumentException("resourceName");
            }

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

            using (CFString cfResourceName = new CFString(resourceName),
                   cfResourceType = new CFString(resourceType),
                   cfDirName = (subDirName == null)? new CFString("") : new CFString(subDirName)) {
                // follows the create rules and therefore we do not need to retain
                var urlHandle = CFBundleCopyResourceURL(handle, cfResourceName.Handle, cfResourceType.Handle,
                                                        String.IsNullOrEmpty(subDirName) ? IntPtr.Zero : cfDirName.Handle);
                return(Runtime.GetNSObject <NSUrl> (urlHandle, true));
            }
        }
Example #31
0
        static unsafe public string?[]? StringArrayFromHandle(IntPtr handle)
        {
            if (handle == IntPtr.Zero)
            {
                return(null);
            }

            var c = (int)GetCount(handle);

            if (c == 0)
            {
                return(Array.Empty <string> ());
            }

            var buffer = c <= 256 ? stackalloc IntPtr [c] : new IntPtr [c];

            fixed(void *ptr = buffer)
            CFArrayGetValues(handle, new CFRange(0, c), (IntPtr)ptr);

            string?[] ret = new string [c];
            for (var i = 0; i < c; i++)
                ret [i] = CFString.FromHandle(buffer [i]); }
Example #32
0
        public static string[] GetPreferredLocalizations(string[] locArray)
        {
            if (locArray == null)
            {
                throw new ArgumentNullException("locArray");
            }

            var cfString = new CFString [locArray.Length];

            for (int index = 0; index < locArray.Length; index++)
            {
                cfString [index] = new CFString(locArray [index]);
            }
            using (var cfLocArray = CFArray.FromNativeObjects(cfString))
                using (var cfArray = new CFArray(CFBundleCopyPreferredLocalizationsFromArray(cfLocArray.Handle), true)) {
                    var cultureInfo = new string [cfArray.Count];
                    for (int index = 0; index < cfArray.Count; index++)
                    {
                        cultureInfo [index] = CFString.FetchString(cfArray.GetValue(index));
                    }
                    return(cultureInfo);
                }
        }
 void notification(CFString name, NSDictionary userInfo)
 {
     var evt = NotificationChanged;
     if (evt == null)
         return;
     evt (this,new NoticationEventArgs(name.ToString ()));
 }
Example #34
0
 public static string GetDeviceIdentifier(IntPtr device)
 {
     IntPtr p = AMDeviceCopyDeviceIdentifier(device);
     CFString str = new CFString(p);
     return str.ToString();
 }
Example #35
0
        public static bool InstallApp(IntPtr device, string appPath)
        {
            CFString path = new CFString(appPath);
            IntPtr appUrl = CFLibrary.CFURLCreateWithFileSystemPath(IntPtr.Zero, path.typeRef, 2, false);
            string[] k = new string[1];
            k[0] = "PackageType";
            IntPtr[] v = new IntPtr[1];
            v[0] = new CFString("Developer");
            CFDictionary opts = new CFDictionary(k, v);

            int err = AMDeviceSecureTransferPath(0, device, appUrl, opts, IntPtr.Zero, 0);
            if ( err == NO_ERR )
            {
                return AMDeviceSecureInstallApplication(0, device, appUrl, opts, IntPtr.Zero, 0) == NO_ERR;
            }
            return err == NO_ERR;
        }
Example #36
0
 public static bool UninstallApp(IntPtr device, string bundleId)
 {
     CFString appId = new CFString(bundleId);
     return AMDeviceSecureUninstallApplication(0, device, appId, 0, IntPtr.Zero, 0) == NO_ERR;
 }
Example #37
0
 public static bool UpdateTime(IntPtr device, double time)
 {
     int sec = Convert.ToInt32(time);
     CFString domain = new CFString("NULL");
     CFString key = new CFString("TimeIntervalSince1970");
     IntPtr p = AMDeviceCopyValue(device, domain, key);
     string n = new CFNumber(p).ToString();
     CFNumber value = new CFNumber(sec);
     return AMDeviceSetValue(device, domain, key, value) == NO_ERR;
 }
Example #38
0
 public bool StartService(string svc)
 {
     CFString c = new CFString(svc);
     return MobileDevice.StartService(am_device, c.typeRef, out socket_fd);
 }