| 26 | | [[NSApp mainMenu] addItem: item]; |
| 27 | | [item release]; |
| | 26 | [[NSApp mainMenu] addItem: item]; |
| | 27 | [item release]; |
| | 28 | |
| | 29 | // Notification |
| | 30 | NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; |
| | 31 | [center addObserver: self |
| | 32 | selector: @selector(progressStarted:) |
| | 33 | name: WebViewProgressStartedNotification |
| | 34 | object: nil]; |
| | 35 | [center addObserver: self |
| | 36 | selector: @selector(progressFinished:) |
| | 37 | name: WebViewProgressFinishedNotification |
| | 38 | object: nil]; |
| | 39 | |
| | 40 | #if 0 |
| | 41 | [center addObserver: self |
| | 42 | selector: @selector(applicationDidFinishLaunching:) |
| | 43 | name: NSApplicationDidFinishLaunchingNotification |
| | 44 | object: NSApp]; |
| | 45 | #endif // 0 |
| | 48 | - (void) progressStarted: (NSNotification*) n |
| | 49 | { |
| | 50 | WebView* webView = [n object]; |
| | 51 | WebDataSource* source = [[webView mainFrame] provisionalDataSource]; |
| | 52 | if (! source) { |
| | 53 | // source = [[webView mainFrame] provisionalDataSource]; |
| | 54 | } |
| | 55 | NSURL* cur_url = [[source request] URL]; |
| | 56 | |
| | 57 | NSLog(@"progressStarted: cur_url = %@", cur_url); |
| | 58 | |
| | 59 | // construct request |
| | 60 | NSString *urlString = [NSString stringWithFormat: |
| | 61 | @"http://b.hatena.ne.jp/entry/json/%@", cur_url]; |
| | 62 | |
| | 63 | NSLog(@"progressStarted: url = %@", urlString); |
| | 64 | NSURL *url = [NSURL URLWithString:urlString]; |
| | 65 | NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:url]; |
| | 66 | //[urlRequest setHTTPMethod:@"POST"]; |
| | 67 | //[urlRequest setHTTPBody:[content dataUsingEncoding:NSUTF8StringEncoding]]; |
| | 68 | |
| | 69 | // connect it |
| | 70 | NSURLConnection *theConnection = [NSURLConnection |
| | 71 | connectionWithRequest:urlRequest |
| | 72 | delegate:self]; |
| | 73 | if (theConnection) { |
| | 74 | receivedData = [[NSMutableData data] retain]; |
| | 75 | } else { |
| | 76 | NSLog(@"not connected correctly."); |
| | 77 | } |
| | 78 | } |
| | 79 | |
| | 80 | - (void) progressFinished: (NSNotification*) n |
| | 81 | { |
| | 82 | WebView* webView = [n object]; |
| | 83 | //NSURL* url = WebFrameRequestURL([webView mainFrame]); |
| | 84 | WebDataSource* source = [[webView mainFrame] provisionalDataSource]; |
| | 85 | if (! source) { |
| | 86 | // source = [[webView mainFrame] provisionalDataSource]; |
| | 87 | } |
| | 88 | NSURL* url = [[source request] URL]; |
| | 89 | NSLog(@"progressFinished: url = %@", url); |
| | 90 | } |
| | 91 | |
| | 92 | |
| | 117 | |
| | 118 | // callbacks |
| | 119 | - (void) connection : (NSURLConnection *) connection |
| | 120 | didReceiveResponse : (NSURLResponse *) response { |
| | 121 | NSDictionary *dicHead = [(NSHTTPURLResponse *)response allHeaderFields]; |
| | 122 | NSLog(@"didReceiveResponse : %@", [dicHead objectForKey:@"Status"]); |
| | 123 | [receivedData setLength:0]; |
| | 124 | } |
| | 125 | |
| | 126 | - (void) connection : (NSURLConnection *) connection |
| | 127 | didReceiveData : (NSData *) data { |
| | 128 | [receivedData appendData:data]; |
| | 129 | } |
| | 130 | |
| | 131 | - (void) connection : (NSURLConnection *) connection |
| | 132 | didFailWithError : (NSError *) error { |
| | 133 | NSLog(@"didFailWithError 1"); |
| | 134 | // [connection release]; |
| | 135 | [receivedData release]; |
| | 136 | NSLog(@"didFailWithError 2"); |
| | 137 | } |
| | 138 | |
| | 139 | - (void) connectionDidFinishLoading:(NSURLConnection *)connection { |
| | 140 | NSLog(@"connectionDidFinishLoading: succeeded to load %d bytes", [receivedData length]); |
| | 141 | NSString *result = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding]; |
| | 142 | NSLog(@"connectionDidFinishLoading: result=%@", result); |
| | 143 | id json = [result JSONValue]; |
| | 144 | NSLog(@"connectionDidFinishLoading: id=%@", json); |
| | 145 | |
| | 146 | // [connection release]; |
| | 147 | [receivedData release]; |
| | 148 | } |
| | 149 | |
| | 150 | |