xpcom/io/CocoaFileUtils.mm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/io/CocoaFileUtils.mm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,139 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +// vim:set ts=2 sts=2 sw=2 et cin:
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "CocoaFileUtils.h"
    1.11 +#include "nsCocoaUtils.h"
    1.12 +#include <Cocoa/Cocoa.h>
    1.13 +#include "nsObjCExceptions.h"
    1.14 +#include "nsDebug.h"
    1.15 +
    1.16 +namespace CocoaFileUtils {
    1.17 +
    1.18 +nsresult RevealFileInFinder(CFURLRef url)
    1.19 +{
    1.20 +  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
    1.21 +
    1.22 +  if (NS_WARN_IF(!url))
    1.23 +    return NS_ERROR_INVALID_ARG;
    1.24 +
    1.25 +  NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
    1.26 +  BOOL success = [[NSWorkspace sharedWorkspace] selectFile:[(NSURL*)url path] inFileViewerRootedAtPath:@""];
    1.27 +  [ap release];
    1.28 +
    1.29 +  return (success ? NS_OK : NS_ERROR_FAILURE);
    1.30 +
    1.31 +  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
    1.32 +}
    1.33 +
    1.34 +nsresult OpenURL(CFURLRef url)
    1.35 +{
    1.36 +  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
    1.37 +
    1.38 +  if (NS_WARN_IF(!url))
    1.39 +    return NS_ERROR_INVALID_ARG;
    1.40 +
    1.41 +  NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
    1.42 +  BOOL success = [[NSWorkspace sharedWorkspace] openURL:(NSURL*)url];
    1.43 +  [ap release];
    1.44 +
    1.45 +  return (success ? NS_OK : NS_ERROR_FAILURE);
    1.46 +
    1.47 +  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
    1.48 +}
    1.49 +
    1.50 +nsresult GetFileCreatorCode(CFURLRef url, OSType *creatorCode)
    1.51 +{
    1.52 +  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
    1.53 +
    1.54 +  if (NS_WARN_IF(!url) || NS_WARN_IF(!creatorCode))
    1.55 +    return NS_ERROR_INVALID_ARG;
    1.56 +
    1.57 +  nsAutoreleasePool localPool;
    1.58 +
    1.59 +  NSString *resolvedPath = [[(NSURL*)url path] stringByResolvingSymlinksInPath];
    1.60 +  if (!resolvedPath) {
    1.61 +    return NS_ERROR_FAILURE;
    1.62 +  }
    1.63 +
    1.64 +  NSDictionary* dict = [[NSFileManager defaultManager] attributesOfItemAtPath:resolvedPath error:nil];
    1.65 +  if (!dict) {
    1.66 +    return NS_ERROR_FAILURE;
    1.67 +  }
    1.68 +
    1.69 +  NSNumber* creatorNum = (NSNumber*)[dict objectForKey:NSFileHFSCreatorCode];
    1.70 +  if (!creatorNum) {
    1.71 +    return NS_ERROR_FAILURE;
    1.72 +  }
    1.73 +
    1.74 +  *creatorCode = [creatorNum unsignedLongValue];
    1.75 +  return NS_OK;
    1.76 +
    1.77 +  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
    1.78 +}
    1.79 +
    1.80 +nsresult SetFileCreatorCode(CFURLRef url, OSType creatorCode)
    1.81 +{
    1.82 +  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
    1.83 +
    1.84 +  if (NS_WARN_IF(!url))
    1.85 +    return NS_ERROR_INVALID_ARG;
    1.86 +
    1.87 +  NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
    1.88 +  NSDictionary* dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:creatorCode] forKey:NSFileHFSCreatorCode];
    1.89 +  BOOL success = [[NSFileManager defaultManager] setAttributes:dict ofItemAtPath:[(NSURL*)url path] error:nil];
    1.90 +  [ap release];
    1.91 +  return (success ? NS_OK : NS_ERROR_FAILURE);
    1.92 +
    1.93 +  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
    1.94 +}
    1.95 +
    1.96 +nsresult GetFileTypeCode(CFURLRef url, OSType *typeCode)
    1.97 +{
    1.98 +  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
    1.99 +
   1.100 +  if (NS_WARN_IF(!url) || NS_WARN_IF(!typeCode))
   1.101 +    return NS_ERROR_INVALID_ARG;
   1.102 +
   1.103 +  nsAutoreleasePool localPool;
   1.104 +
   1.105 +  NSString *resolvedPath = [[(NSURL*)url path] stringByResolvingSymlinksInPath];
   1.106 +  if (!resolvedPath) {
   1.107 +    return NS_ERROR_FAILURE;
   1.108 +  }
   1.109 +
   1.110 +  NSDictionary* dict = [[NSFileManager defaultManager] attributesOfItemAtPath:resolvedPath error:nil];
   1.111 +  if (!dict) {
   1.112 +    return NS_ERROR_FAILURE;
   1.113 +  }
   1.114 +
   1.115 +  NSNumber* typeNum = (NSNumber*)[dict objectForKey:NSFileHFSTypeCode];
   1.116 +  if (!typeNum) {
   1.117 +    return NS_ERROR_FAILURE;
   1.118 +  }
   1.119 +
   1.120 +  *typeCode = [typeNum unsignedLongValue];
   1.121 +  return NS_OK;
   1.122 +
   1.123 +  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
   1.124 +}
   1.125 +
   1.126 +nsresult SetFileTypeCode(CFURLRef url, OSType typeCode)
   1.127 +{
   1.128 +  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
   1.129 +
   1.130 +  if (NS_WARN_IF(!url))
   1.131 +    return NS_ERROR_INVALID_ARG;
   1.132 +
   1.133 +  NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
   1.134 +  NSDictionary* dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:typeCode] forKey:NSFileHFSTypeCode];
   1.135 +  BOOL success = [[NSFileManager defaultManager] setAttributes:dict ofItemAtPath:[(NSURL*)url path] error:nil];
   1.136 +  [ap release];
   1.137 +  return (success ? NS_OK : NS_ERROR_FAILURE);
   1.138 +
   1.139 +  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
   1.140 +}
   1.141 +
   1.142 +} // namespace CocoaFileUtils

mercurial