PageRenderTime 33ms CodeModel.GetById 15ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/filesystems-objc/YTFS/YTFS_Filesystem.m

http://macfuse.googlecode.com/
Objective C | 125 lines | 88 code | 15 blank | 22 comment | 10 complexity | 3312ef2a1cff78c61173ac7b36adf004 MD5 | raw file
  1// ================================================================
  2// Copyright (C) 2008 Google Inc.
  3// 
  4// Licensed under the Apache License, Version 2.0 (the "License");
  5// you may not use this file except in compliance with the License.
  6// You may obtain a copy of the License at
  7// 
  8//      http://www.apache.org/licenses/LICENSE-2.0
  9// 
 10// Unless required by applicable law or agreed to in writing, software
 11// distributed under the License is distributed on an "AS IS" BASIS,
 12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13// See the License for the specific language governing permissions and
 14// limitations under the License.
 15// ================================================================
 16//
 17//  YTFS_Filesystem.m
 18//  YTFS
 19//
 20//  Created by ted on 12/7/08.
 21//
 22#import "YTFS_Filesystem.h"
 23#import "YTVideo.h"
 24#import "NSImage+IconData.h"
 25#import <MacFUSE/MacFUSE.h>
 26
 27// Category on NSError to  simplify creating an NSError based on posix errno.
 28@interface NSError (POSIX)
 29+ (NSError *)errorWithPOSIXCode:(int)code;
 30@end
 31@implementation NSError (POSIX)
 32+ (NSError *)errorWithPOSIXCode:(int) code {
 33  return [NSError errorWithDomain:NSPOSIXErrorDomain code:code userInfo:nil];
 34}
 35@end
 36
 37@implementation YTFS_Filesystem
 38
 39#pragma mark Directory Contents
 40
 41- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error {
 42  if ([path isEqualToString:@"/"]) {
 43    return [videos_ allKeys];
 44  }
 45  *error = [NSError errorWithPOSIXCode:ENOENT];
 46  return nil;
 47}
 48
 49#pragma mark Getting Attributes
 50
 51- (NSDictionary *)attributesOfItemAtPath:(NSString *)path
 52                                userData:(id)userData
 53                                   error:(NSError **)error {
 54  if ([self videoAtPath:path]) {
 55    return [NSDictionary dictionary];
 56  }
 57  return nil;
 58}
 59
 60#pragma mark File Contents
 61
 62- (NSData *)contentsAtPath:(NSString *)path {
 63  YTVideo* video = [self videoAtPath:path];
 64  if (video) {
 65    return [video xmlData];
 66  }
 67  return nil;
 68}
 69
 70#pragma mark FinderInfo and ResourceFork (Optional)
 71
 72- (NSDictionary *)finderAttributesAtPath:(NSString *)path 
 73                                   error:(NSError **)error {
 74  NSDictionary* attribs = nil;
 75  if ([self videoAtPath:path]) {
 76    NSNumber* finderFlags = [NSNumber numberWithLong:kHasCustomIcon];
 77    attribs = [NSDictionary dictionaryWithObject:finderFlags
 78                                          forKey:kGMUserFileSystemFinderFlagsKey];
 79  }
 80  return attribs;
 81}
 82
 83- (NSDictionary *)resourceAttributesAtPath:(NSString *)path
 84                                     error:(NSError **)error {
 85  NSMutableDictionary* attribs = nil;
 86  YTVideo* video = [self videoAtPath:path];
 87  if (video) {
 88    attribs = [NSMutableDictionary dictionary];
 89    NSURL* url = [video playerURL];
 90    if (url) {
 91      [attribs setObject:url forKey:kGMUserFileSystemWeblocURLKey];
 92    }
 93    url = [video thumbnailURL];
 94    if (url) {
 95      NSImage* image = [[[NSImage alloc] initWithContentsOfURL:url] autorelease];
 96      NSData* icnsData = [image icnsDataWithWidth:256];
 97      [attribs setObject:icnsData forKey:kGMUserFileSystemCustomIconDataKey];
 98    }
 99  }
100  return attribs;  
101}
102
103#pragma mark Init and Dealloc
104
105- (id)initWithVideos:(NSDictionary *)videos {
106  if ((self = [super init])) {
107    videos_ = [videos retain];
108  }
109  return self;
110}
111- (void)dealloc {
112  [videos_ release];
113  [super dealloc];
114}
115
116- (YTVideo *)videoAtPath:(NSString *)path {
117  NSArray* components = [path pathComponents];
118  if ([components count] != 2) {
119    return nil;
120  }
121  YTVideo* video = [videos_ objectForKey:[components objectAtIndex:1]];
122  return video;
123}
124
125@end