forked from dgrijalva/gitx
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathPBGitXProtocol.m
109 lines (88 loc) · 3.13 KB
/
PBGitXProtocol.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//
// PBGitXProtocol.m
// GitX
//
// Created by Pieter de Bie on 01-11-08.
// Copyright 2008 Pieter de Bie. All rights reserved.
//
#import "PBGitXProtocol.h"
@implementation PBGitXProtocol
- (id)initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id <NSURLProtocolClient>)client
{
if ((self = [super initWithRequest:request cachedResponse:cachedResponse client:client]))
{
}
return self;
}
+ (BOOL) canInitWithRequest:(NSURLRequest *)request
{
return [[[[request URL] scheme] lowercaseString] isEqualToString:@"gitx"];
}
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
{
return request;
}
-(void)startLoading
{
NSURL *url = [[self request] URL];
PBGitRepository *repo = [[self request] repository];
if(!repo) {
[[self client] URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:0 userInfo:nil]];
return;
}
if ([[url host] isEqualToString:@"app"]) {
NSString *app=[[url path] substringFromIndex:1];
NSString *appPath=[[NSWorkspace sharedWorkspace] fullPathForApplication:app];
DLog(@"app=%@ appPath=%@",app,appPath);
if(appPath){
NSImage *icon = [[NSWorkspace sharedWorkspace] iconForFile:appPath];
DLog(@"icon=%@",icon);
[[self client] URLProtocol:self didLoadData:[icon TIFFRepresentation]];
[[self client] URLProtocolDidFinishLoading:self];
}else{
[[self client] URLProtocol:self didFailWithError:[NSError errorWithDomain:@"gitx" code:404 userInfo:nil]];
}
}else {
NSString *path=[[url path] substringFromIndex:1];
NSString *v=@"";
if ([[path substringToIndex:5] isEqualToString:@"prev/"]) {
path=[path substringFromIndex:5];
v=@"^";
}
NSString *specifier = [NSString stringWithFormat:@"%@%@:%@", [url host], v,path];
handle = [repo handleInWorkDirForArguments:[NSArray arrayWithObjects:@"cat-file", @"blob", specifier, nil]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishFileLoad:) name:NSFileHandleReadToEndOfFileCompletionNotification object:handle];
[handle readToEndOfFileInBackgroundAndNotify];
NSURLResponse *response = [[NSURLResponse alloc] initWithURL:[[self request] URL]
MIMEType:nil
expectedContentLength:-1
textEncodingName:nil];
[[self client] URLProtocol:self
didReceiveResponse:response
cacheStoragePolicy:NSURLCacheStorageNotAllowed];
}
}
- (void) didFinishFileLoad:(NSNotification *)notification
{
NSData *data = [[notification userInfo] valueForKey:NSFileHandleNotificationDataItem];
[[self client] URLProtocol:self didLoadData:data];
[[self client] URLProtocolDidFinishLoading:self];
}
- (void) stopLoading
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
@implementation NSURLRequest (PBGitXProtocol)
- (PBGitRepository *) repository
{
return [NSURLProtocol propertyForKey:@"PBGitRepository" inRequest:self];
}
@end
@implementation NSMutableURLRequest (PBGitXProtocol)
@dynamic repository;
- (void) setRepository:(PBGitRepository *)repository
{
[NSURLProtocol setProperty:repository forKey:@"PBGitRepository" inRequest:self];
}
@end