/filesystems-objc/YTFS/YTFS_Controller.m

http://macfuse.googlecode.com/ · Objective C · 86 lines · 54 code · 10 blank · 22 comment · 0 complexity · c6fdc9175b496d8ddeec16be8f8ffe72 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_Controller.m
  18. // YTFS
  19. //
  20. // Created by ted on 12/7/08.
  21. //
  22. #import "YTFS_Controller.h"
  23. #import "YTFS_Filesystem.h"
  24. #import "YTVideo.h"
  25. #import <MacFUSE/MacFUSE.h>
  26. @implementation YTFS_Controller
  27. - (void)mountFailed:(NSNotification *)notification {
  28. NSDictionary* userInfo = [notification userInfo];
  29. NSError* error = [userInfo objectForKey:kGMUserFileSystemErrorKey];
  30. NSLog(@"kGMUserFileSystem Error: %@, userInfo=%@", error, [error userInfo]);
  31. NSRunAlertPanel(@"Mount Failed", [error localizedDescription], nil, nil, nil);
  32. [[NSApplication sharedApplication] terminate:nil];
  33. }
  34. - (void)didMount:(NSNotification *)notification {
  35. NSDictionary* userInfo = [notification userInfo];
  36. NSString* mountPath = [userInfo objectForKey:kGMUserFileSystemMountPathKey];
  37. NSString* parentPath = [mountPath stringByDeletingLastPathComponent];
  38. [[NSWorkspace sharedWorkspace] selectFile:mountPath
  39. inFileViewerRootedAtPath:parentPath];
  40. }
  41. - (void)didUnmount:(NSNotification*)notification {
  42. [[NSApplication sharedApplication] terminate:nil];
  43. }
  44. - (void)applicationDidFinishLaunching:(NSNotification *)notification {
  45. // Pump up our url cache.
  46. NSURLCache* cache = [NSURLCache sharedURLCache];
  47. [cache setDiskCapacity:(1024 * 1024 * 500)];
  48. [cache setMemoryCapacity:(1024 * 1024 * 40)];
  49. NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
  50. [center addObserver:self selector:@selector(mountFailed:)
  51. name:kGMUserFileSystemMountFailed object:nil];
  52. [center addObserver:self selector:@selector(didMount:)
  53. name:kGMUserFileSystemDidMount object:nil];
  54. [center addObserver:self selector:@selector(didUnmount:)
  55. name:kGMUserFileSystemDidUnmount object:nil];
  56. NSString* mountPath = @"/Volumes/YTFS";
  57. fs_delegate_ =
  58. [[YTFS_Filesystem alloc] initWithVideos:[YTVideo fetchTopRatedVideos]];
  59. fs_ = [[GMUserFileSystem alloc] initWithDelegate:fs_delegate_ isThreadSafe:YES];
  60. NSMutableArray* options = [NSMutableArray array];
  61. NSString* volArg =
  62. [NSString stringWithFormat:@"volicon=%@",
  63. [[NSBundle mainBundle] pathForResource:@"YTFS" ofType:@"icns"]];
  64. [options addObject:volArg];
  65. [options addObject:@"volname=YTFS"];
  66. [options addObject:@"rdonly"];
  67. [fs_ mountAtPath:mountPath withOptions:options];
  68. }
  69. - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
  70. [[NSNotificationCenter defaultCenter] removeObserver:self];
  71. [fs_ unmount];
  72. [fs_ release];
  73. [fs_delegate_ release];
  74. return NSTerminateNow;
  75. }
  76. @end