bindings/Cocoa/MYOttoman.mm
author Jens Alfke <jens@mooseyard.com>
Thu Sep 24 21:47:06 2009 -0700 (2009-09-24)
changeset 6 f2cd752db494
permissions -rw-r--r--
Initial Cocoa (Objective-C) API.
     1 //
     2 //  MYOttoman.mm
     3 //  Ottoman
     4 //
     5 //  Created by Jens Alfke on 9/21/09.
     6 //  Copyright 2009 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import "MYOttoman.h"
    10 #import "MYOttoman_internal.h"
    11 extern "C" {
    12 #import "Test.h"
    13 }
    14 
    15 #include "Ottoman.h"
    16 #include "VersionDictionary.h"
    17 #include "File.h"
    18 
    19 
    20 @interface MYOttoman ()
    21 - (void) _versionsChanged;
    22 @end
    23 
    24 
    25 namespace Mooseyard {
    26 
    27     class ObjCOwnedOttoman :public Ottoman {
    28     public:
    29         ObjCOwnedOttoman (MYOttoman *owner)
    30         :_owner(owner)
    31         { }
    32         
    33         ObjCOwnedOttoman (MYOttoman *owner, NSURL *fileURL, bool writeable)
    34         :Ottoman(fileURL.path.fileSystemRepresentation, writeable),
    35         _owner(owner)
    36         { }
    37         
    38     protected:
    39         virtual void versionsChanged() {
    40             [_owner _versionsChanged];
    41         }
    42     private:
    43         MYOttoman *_owner;
    44     };
    45     
    46 }
    47 
    48 
    49 using namespace Mooseyard;
    50 
    51 
    52 static BOOL ErrorToNSError (const File::Error &x, NSError **outError) {
    53     if (outError) {
    54         *outError = [NSError errorWithDomain: NSPOSIXErrorDomain
    55                                         code: x.code
    56                                     userInfo: nil];
    57     }
    58     return NO;
    59 }
    60 
    61 
    62 @interface MYOttoman ()
    63 @property (readonly) Ottoman* ottoman;
    64 @end
    65 
    66 
    67 @implementation MYOttoman
    68 
    69 
    70 - (id) init {
    71     return [self initWithURL: nil writeable: YES error: nil];
    72 }
    73 
    74 - (id) initWithURL: (NSURL*)fileURL
    75          writeable: (BOOL)writeable
    76              error: (NSError**)outError
    77 {
    78     self = [super init];
    79     if (self) {
    80         try {
    81             if (fileURL) {
    82                 NSAssert([fileURL isFileURL], @"MYOttoman only supports file: URLs");
    83                 _ottoman = new ObjCOwnedOttoman(self, fileURL, writeable);
    84             } else {
    85                 _ottoman = new ObjCOwnedOttoman(self);
    86             }
    87         } catch (const File::Error &x) {
    88             ErrorToNSError(x,outError);
    89             [self release];
    90             return nil;
    91         }
    92         
    93         if (writeable)
    94             _currentVersion = [[MYCurrentVersionDictionary alloc]
    95                                         _initWithOverlayDictionary: self.ottoman->currentVersion()];
    96     }
    97     return self;
    98 }
    99 
   100 - (void) dealloc
   101 {
   102     [_lastVersion release];
   103     [_currentVersion release];
   104     delete (Ottoman*)_ottoman;
   105     [super dealloc];
   106 }
   107 
   108 - (void) finalize {
   109     delete (Ottoman*)_ottoman;
   110     [super finalize];
   111 }
   112 
   113 - (void) close {
   114     delete (Ottoman*)_ottoman;
   115     _ottoman = nil;
   116 }
   117 
   118 
   119 - (Ottoman*) ottoman {
   120     Assert(_ottoman, @"MYOttoman has already been closed");
   121     return (Ottoman*) _ottoman;
   122 }
   123 
   124 - (NSURL*) URL {
   125     const char *path = self.ottoman->filename();
   126     return path ?[NSURL fileURLWithPath: [NSString stringWithUTF8String: path]] :nil;
   127 }
   128 
   129 
   130 - (MYVersionDictionary*) lastVersion {
   131     if (!_lastVersion)
   132         _lastVersion = [[MYVersionDictionary alloc] _initWithVersionDictionary: self.ottoman->lastVersion()];
   133     return _lastVersion;
   134 }
   135 
   136 - (MYCurrentVersionDictionary*) currentVersion {
   137     return _currentVersion;
   138 }
   139 
   140 - (void) _versionsChanged {
   141     [_lastVersion autorelease];
   142     _lastVersion = nil;
   143 }
   144 
   145 
   146 - (BOOL) needsSync {
   147     return self.ottoman->needsSync();
   148 }
   149 
   150 
   151 - (BOOL) sync: (NSError**)outError {
   152     if (outError) *outError = nil;
   153     try {
   154         return self.ottoman->sync();
   155     } catch (const File::Error &x) {
   156         return ErrorToNSError(x,outError);
   157     }
   158 }
   159 
   160 - (BOOL) save: (NSError**)outError {
   161     if (outError) *outError = nil;
   162     try {
   163         return self.ottoman->save();
   164     } catch (const File::Error &x) {
   165         return ErrorToNSError(x,outError);
   166     }
   167 }
   168 
   169 - (BOOL) saveAndCompact: (NSError**)outError {
   170     if (outError) *outError = nil;
   171     try {
   172         return self.ottoman->saveAndCompact();
   173     } catch (const File::Error &x) {
   174         return ErrorToNSError(x,outError);
   175     }
   176 }
   177 
   178 - (BOOL) saveAs: (NSURL*)newFileURL
   179          overwriteAllowed: (BOOL)overwriteAllowed
   180          error: (NSError**)outError
   181 {
   182     NSParameterAssert(newFileURL!=nil);
   183     NSAssert([newFileURL isFileURL], @"MYOttoman only supports file: URLs");
   184     if (outError) *outError = nil;
   185     try {
   186         self.ottoman->saveAs(newFileURL.path.fileSystemRepresentation, overwriteAllowed);
   187         return YES;
   188     } catch (const File::Error &x) {
   189         return ErrorToNSError(x,outError);
   190     }
   191 }
   192 
   193 
   194 - (BOOL) scavengeAndRepair: (BOOL)repair error: (NSError**)outError {
   195     if (outError) *outError = nil;
   196     try {
   197         return self.ottoman->scavenge(repair);
   198     } catch (const File::Error &x) {
   199         return ErrorToNSError(x,outError);
   200     }
   201 }
   202 
   203 
   204 @end
   205