mootoh / safarihatenabookmark

Safari SIMBL Plugin that incorporates Hatena Bookmark, a Japanese Social Bookmark.

This URL has Read+Write access

safarihatenabookmark / SHBController.m
100644 151 lines (121 sloc) 4.052 kb
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// 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