Show
Ignore:
Timestamp:
12/18/07 03:08:00 (13 months ago)
Author:
mootoh
Message:

lang/objective-c/SafariHatenaBookmark: notification replaced by RubyCocoa?.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/objective-c/SafariHatenaBookmark/shb_controller.rb

    r3246 r3249  
    88 
    99require 'osx/cocoa' 
     10OSX.require_framework "WebKit" 
    1011 
    1112class SHBController < OSX::NSObject 
     
    1314 
    1415  ib_outlet :top_menu 
    15 =begin 
    16   ib_action :bookmark, :diary, :haiku 
     16  ib_action :bookmark 
     17  ib_action :diary 
     18  ib_action :haiku 
    1719 
    1820  def bookmark 
     21    @logger.info('bookmark !') 
    1922  end 
    2023 
    2124  def diary 
     25    @logger.info('diary !') 
    2226  end 
    2327 
    2428  def haiku 
     29    @logger.info('haiku !') 
    2530  end 
    26 =end 
    27   def self.instance 
    28     @@instance ||= self.alloc.init 
     31 
     32  def self.instance(logger) 
     33    @@instance ||= self.alloc.init_with_logger(logger) 
     34  end 
     35 
     36  def init_with_logger(logger) 
     37    @logger = logger 
     38    init 
    2939  end 
    3040 
     
    3747 
    3848  def awakeFromNib 
    39   # Menu 
    40   install_menu 
     49    # Menu 
     50    install_menu 
    4151 
    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) 
    6262  end 
    6363 
    6464  def install_menu(menu_image = nil) 
    6565    item = @host_app.mainMenu. 
    66       objc_send(:addItemWithTitle, " hatena", 
     66      objc_send(:addItemWithTitle, " はてな", 
    6767                :action, nil, :keyEquivalent, "") 
    68     @top_menu.setTitle("hatena") 
     68    @top_menu.setTitle("はてな") 
    6969    item.setSubmenu(@top_menu) 
    7070    item.setImage(menu_image) if menu_image 
    7171  end 
     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 
     152didReceiveResponse : (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 
     159didReceiveData : (NSData *) data { 
     160  [receivedData appendData:data]; 
     161} 
     162 
     163- (void) connection : (NSURLConnection *) connection  
     164didFailWithError : (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 
    72183end