//
// SHBController.m
// SIMBLTutorial
//
// Created by 高山 征大 on 12/17/07.
// Copyright 2007 __MyCompanyName__. All rights reserved.
//
#import <WebKit/WebKit.h>
#import <JSON/JSON.h>
#import "SHBController.h"
@implementation SHBController
- (void) awakeFromNib {
NSLog(@"SHBController:awakeFromNib");
// Menu
NSMenuItem* item;
item = [[NSMenuItem alloc] init];
[item setSubmenu: topMenu];
[topMenu setTitle: @"はてな"];
[[NSApp mainMenu] addItem: item];
[item release];
// Notification
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center addObserver: self
selector: @selector(progressStarted:)
name: WebViewProgressStartedNotification
object: nil];
[center addObserver: self
selector: @selector(progressFinished:)
name: WebViewProgressFinishedNotification
object: nil];
#if 0
[center addObserver: self
selector: @selector(applicationDidFinishLaunching:)
name: NSApplicationDidFinishLaunchingNotification
object: NSApp];
#endif // 0
}
- (void) progressStarted: (NSNotification*) n
{
WebView* webView = [n object];
WebDataSource* source = [[webView mainFrame] provisionalDataSource];
if (! source) {
// source = [[webView mainFrame] provisionalDataSource];
}
NSURL* cur_url = [[source request] URL];
NSLog(@"progressStarted: cur_url = %@", cur_url);
// construct request
NSString *urlString = [NSString stringWithFormat:
@"http://b.hatena.ne.jp/entry/json/%@", cur_url];
NSLog(@"progressStarted: url = %@", urlString);
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:url];
//[urlRequest setHTTPMethod:@"POST"];
//[urlRequest setHTTPBody:[content dataUsingEncoding:NSUTF8StringEncoding]];
// connect it
NSURLConnection *theConnection = [NSURLConnection
connectionWithRequest:urlRequest
delegate:self];
if (theConnection) {
receivedData = [[NSMutableData data] retain];
} else {
NSLog(@"not connected correctly.");
}
}
- (void) progressFinished: (NSNotification*) n
{
WebView* webView = [n object];
//NSURL* url = WebFrameRequestURL([webView mainFrame]);
WebDataSource* source = [[webView mainFrame] provisionalDataSource];
if (! source) {
// source = [[webView mainFrame] provisionalDataSource];
}
NSURL* url = [[source request] URL];
NSLog(@"progressFinished: url = %@", url);
}
- (id) init {
NSLog(@"SHBController %p - init", self);
self = [super init];
if (! self)
return nil;
[NSBundle loadNibNamed: @"Menu.nib" owner: self];
NSLog(@"SHBController:init: nib loaded");
return self;
}
- (IBAction)bookmark:(id)sender {
NSLog(@"bookmark");
}
- (IBAction)diary:(id)sender {
NSLog(@"diary");
}
- (IBAction)haiku:(id)sender {
NSLog(@"haiku");
}
// callbacks
- (void) connection : (NSURLConnection *) connection
didReceiveResponse : (NSURLResponse *) response {
NSDictionary *dicHead = [(NSHTTPURLResponse *)response allHeaderFields];
NSLog(@"didReceiveResponse : %@", [dicHead objectForKey:@"Status"]);
[receivedData setLength:0];
}
- (void) connection : (NSURLConnection *) connection
didReceiveData : (NSData *) data {
[receivedData appendData:data];
}
- (void) connection : (NSURLConnection *) connection
didFailWithError : (NSError *) error {
NSLog(@"didFailWithError 1");
// [connection release];
[receivedData release];
NSLog(@"didFailWithError 2");
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"connectionDidFinishLoading: succeeded to load %d bytes", [receivedData length]);
NSString *result = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"connectionDidFinishLoading: result=%@", result);
//id json = [result JSONValue];
//NSLog(@"connectionDidFinishLoading: id=%@", json);
// [connection release];
[receivedData release];
}
@end