GitIndex: add commit notifications
[GitX.git] / PBGitXProtocol.m
blobdcffeeaad271677a43c4334c4ca2fc1d733ee7c8
1 //
2 //  PBGitXProtocol.m
3 //  GitX
4 //
5 //  Created by Pieter de Bie on 01-11-08.
6 //  Copyright 2008 Pieter de Bie. All rights reserved.
7 //
9 #import "PBGitXProtocol.h"
12 @implementation PBGitXProtocol
14 + (BOOL) canInitWithRequest:(NSURLRequest *)request
16         return [[[request URL] scheme] isEqualToString:@"GitX"];
19 + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
21     return request;
24 -(void)startLoading
26     NSURL *url = [[self request] URL];
27         PBGitRepository *repo = [[self request] repository];
29         if(!repo) {
30                 [[self client] URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:0 userInfo:nil]];
31                 return;
32     }
34         NSString *specifier = [NSString stringWithFormat:@"%@:%@", [url host], [[url path] substringFromIndex:1]];
35         handle = [repo handleInWorkDirForArguments:[NSArray arrayWithObjects:@"cat-file", @"blob", specifier, nil]];
36         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishFileLoad:) name:NSFileHandleReadToEndOfFileCompletionNotification object:handle];
37         [handle readToEndOfFileInBackgroundAndNotify];
39     NSURLResponse *response = [[NSURLResponse alloc] initWithURL:[[self request] URL]
40                                                                                                                 MIMEType:nil
41                                                                                    expectedContentLength:-1
42                                                                                                 textEncodingName:nil];
44     [[self client] URLProtocol:self
45                         didReceiveResponse:response
46                         cacheStoragePolicy:NSURLCacheStorageNotAllowed];
49 - (void) didFinishFileLoad:(NSNotification *)notification
51         NSData *data = [[notification userInfo] valueForKey:NSFileHandleNotificationDataItem];
52     [[self client] URLProtocol:self didLoadData:data];
53     [[self client] URLProtocolDidFinishLoading:self];
56 - (void) stopLoading
58         [[NSNotificationCenter defaultCenter] removeObserver:self];
61 @end
63 @implementation NSURLRequest (PBGitXProtocol)
65 - (PBGitRepository *) repository
67         return [NSURLProtocol propertyForKey:@"PBGitRepository" inRequest:self];
69 @end
71 @implementation NSMutableURLRequest (PBGitXProtocol)
72 @dynamic repository;
74 - (void) setRepository:(PBGitRepository *)repository
76         [NSURLProtocol setProperty:repository forKey:@"PBGitRepository" inRequest:self];
79 @end