| 42 | | |
| 43 | | =begin |
| 44 | | // Notification |
| 45 | | NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; |
| 46 | | [center addObserver: self |
| 47 | | selector: @selector(progressStarted:) |
| 48 | | name: WebViewProgressStartedNotification |
| 49 | | object: nil]; |
| 50 | | [center addObserver: self |
| 51 | | selector: @selector(progressFinished:) |
| 52 | | name: WebViewProgressFinishedNotification |
| 53 | | object: nil]; |
| 54 | | |
| 55 | | #if 0 |
| 56 | | [center addObserver: self |
| 57 | | selector: @selector(applicationDidFinishLaunching:) |
| 58 | | name: NSApplicationDidFinishLaunchingNotification |
| 59 | | object: NSApp]; |
| 60 | | #endif // 0 |
| 61 | | =end |
| | 52 | # Notification |
| | 53 | center = NSNotificationCenter.defaultCenter |
| | 54 | center.addObserver_selector_name_object_(self, |
| | 55 | 'progressStarted:', |
| | 56 | OSX::WebViewProgressStartedNotification, |
| | 57 | nil) |
| | 58 | center.addObserver_selector_name_object_(self, |
| | 59 | 'progressFinished:', |
| | 60 | OSX::WebViewProgressFinishedNotification, |
| | 61 | nil) |
| | 72 | |
| | 73 | |
| | 74 | def progressStarted(n) |
| | 75 | @logger.info('progressStarted begin') |
| | 76 | webView = n.object |
| | 77 | source = webView.mainFrame.provisionalDataSource |
| | 78 | unless source |
| | 79 | # source = [[webView mainFrame] provisionalDataSource] |
| | 80 | end |
| | 81 | cur_url = source.request.URL |
| | 82 | |
| | 83 | @logger.info("progressStarted: cur_url = " + cur_url.to_s) |
| | 84 | end |
| | 85 | |
| | 86 | def progressFinished(n) |
| | 87 | @logger.info('progressFinished begin') |
| | 88 | end |
| | 89 | |
| | 90 | =begin |
| | 91 | // construct request |
| | 92 | NSString *urlString = [NSString stringWithFormat: |
| | 93 | @"http://b.hatena.ne.jp/entry/json/%@", cur_url]; |
| | 94 | |
| | 95 | NSLog(@"progressStarted: url = %@", urlString); |
| | 96 | NSURL *url = [NSURL URLWithString:urlString]; |
| | 97 | NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:url]; |
| | 98 | //[urlRequest setHTTPMethod:@"POST"]; |
| | 99 | //[urlRequest setHTTPBody:[content dataUsingEncoding:NSUTF8StringEncoding]]; |
| | 100 | |
| | 101 | // connect it |
| | 102 | NSURLConnection *theConnection = [NSURLConnection |
| | 103 | connectionWithRequest:urlRequest |
| | 104 | delegate:self]; |
| | 105 | if (theConnection) { |
| | 106 | receivedData = [[NSMutableData data] retain]; |
| | 107 | } else { |
| | 108 | NSLog(@"not connected correctly."); |
| | 109 | } |
| | 110 | } |
| | 111 | |
| | 112 | - (void) progressFinished: (NSNotification*) n |
| | 113 | { |
| | 114 | WebView* webView = [n object]; |
| | 115 | //NSURL* url = WebFrameRequestURL([webView mainFrame]); |
| | 116 | WebDataSource* source = [[webView mainFrame] provisionalDataSource]; |
| | 117 | if (! source) { |
| | 118 | // source = [[webView mainFrame] provisionalDataSource]; |
| | 119 | } |
| | 120 | NSURL* url = [[source request] URL]; |
| | 121 | NSLog(@"progressFinished: url = %@", url); |
| | 122 | } |
| | 123 | |
| | 124 | |
| | 125 | - (id) init { |
| | 126 | NSLog(@"SHBController %p - init", self); |
| | 127 | |
| | 128 | self = [super init]; |
| | 129 | if (! self) |
| | 130 | return nil; |
| | 131 | |
| | 132 | [NSBundle loadNibNamed: @"Menu.nib" owner: self]; |
| | 133 | NSLog(@"SHBController:init: nib loaded"); |
| | 134 | return self; |
| | 135 | } |
| | 136 | |
| | 137 | - (IBAction)bookmark:(id)sender { |
| | 138 | NSLog(@"bookmark"); |
| | 139 | } |
| | 140 | |
| | 141 | - (IBAction)diary:(id)sender { |
| | 142 | NSLog(@"diary"); |
| | 143 | } |
| | 144 | |
| | 145 | - (IBAction)haiku:(id)sender { |
| | 146 | NSLog(@"haiku"); |
| | 147 | } |
| | 148 | |
| | 149 | |
| | 150 | // callbacks |
| | 151 | - (void) connection : (NSURLConnection *) connection |
| | 152 | didReceiveResponse : (NSURLResponse *) response { |
| | 153 | NSDictionary *dicHead = [(NSHTTPURLResponse *)response allHeaderFields]; |
| | 154 | NSLog(@"didReceiveResponse : %@", [dicHead objectForKey:@"Status"]); |
| | 155 | [receivedData setLength:0]; |
| | 156 | } |
| | 157 | |
| | 158 | - (void) connection : (NSURLConnection *) connection |
| | 159 | didReceiveData : (NSData *) data { |
| | 160 | [receivedData appendData:data]; |
| | 161 | } |
| | 162 | |
| | 163 | - (void) connection : (NSURLConnection *) connection |
| | 164 | didFailWithError : (NSError *) error { |
| | 165 | NSLog(@"didFailWithError 1"); |
| | 166 | // [connection release]; |
| | 167 | [receivedData release]; |
| | 168 | NSLog(@"didFailWithError 2"); |
| | 169 | } |
| | 170 | |
| | 171 | - (void) connectionDidFinishLoading:(NSURLConnection *)connection { |
| | 172 | NSLog(@"connectionDidFinishLoading: succeeded to load %d bytes", [receivedData length]); |
| | 173 | NSString *result = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding]; |
| | 174 | NSLog(@"connectionDidFinishLoading: result=%@", result); |
| | 175 | //id json = [result JSONValue]; |
| | 176 | //NSLog(@"connectionDidFinishLoading: id=%@", json); |
| | 177 | |
| | 178 | // [connection release]; |
| | 179 | [receivedData release]; |
| | 180 | } |
| | 181 | =end |
| | 182 | |