| 1 | require 'osx/cocoa' |
|---|
| 2 | include OSX |
|---|
| 3 | |
|---|
| 4 | require "drb/drb" |
|---|
| 5 | |
|---|
| 6 | require "uri" |
|---|
| 7 | class CHMInternalURLProtocol < NSURLProtocol |
|---|
| 8 | #+ (void)registerContainer:(CHMContainer *)container; |
|---|
| 9 | #+ (void)unregisterContainer:(CHMContainer *)container; |
|---|
| 10 | #+ (CHMContainer *)containerForUniqueId:(NSString *)uniqueId; |
|---|
| 11 | #+ (NSURL *)URLWithPath:(NSString *)path inContainer:(CHMContainer *)container; |
|---|
| 12 | |
|---|
| 13 | SCHEME = "chm-internal" |
|---|
| 14 | |
|---|
| 15 | def self.url_for(doc, path) |
|---|
| 16 | path.gsub!(/\\/, "/") |
|---|
| 17 | path = Pathname.new(path).cleanpath.to_s |
|---|
| 18 | url = URI("#{SCHEME}://obj:#{doc.object_id}/") + path |
|---|
| 19 | # log url |
|---|
| 20 | NSURL.URLWithString_relativeToURL(url.to_s, "#{SCHEME}://obj:#{doc.object_id}/") |
|---|
| 21 | end |
|---|
| 22 | |
|---|
| 23 | # protocol |
|---|
| 24 | |
|---|
| 25 | #+ (BOOL)canHandleURL:(NSURL *)anURL; |
|---|
| 26 | def self.canHandleURL(url) |
|---|
| 27 | # log "canhandle #{url}" |
|---|
| 28 | return false unless url |
|---|
| 29 | url.scheme == SCHEME |
|---|
| 30 | end |
|---|
| 31 | |
|---|
| 32 | #+ (BOOL)canInitWithRequest:(NSURLRequest *)request |
|---|
| 33 | def self.canInitWithRequest(req) |
|---|
| 34 | # log "canInitWithRequest: #{req.URL.absoluteString}" |
|---|
| 35 | canHandleURL(req.URL) |
|---|
| 36 | end |
|---|
| 37 | |
|---|
| 38 | #+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request |
|---|
| 39 | def self.canonicalRequestForRequest(req) |
|---|
| 40 | req |
|---|
| 41 | end |
|---|
| 42 | |
|---|
| 43 | #-(NSURLRequest *)request |
|---|
| 44 | # def request |
|---|
| 45 | # super_request |
|---|
| 46 | # end |
|---|
| 47 | |
|---|
| 48 | #-(void)startLoading |
|---|
| 49 | def startLoading |
|---|
| 50 | log "startLoading #{request.URL.absoluteString}" |
|---|
| 51 | url = request.URL |
|---|
| 52 | chm = ObjectSpace._id2ref(url.port.to_s.to_i) |
|---|
| 53 | |
|---|
| 54 | log url.path.to_s |
|---|
| 55 | raise "JS" if url.path.to_s =~ /\.js$/ |
|---|
| 56 | text = url.parameterString ? chm.retrieve_object("#{url.path};#{url.parameterString}") \ |
|---|
| 57 | : chm.retrieve_object("#{url.path}") |
|---|
| 58 | raise "empty" if text.empty? |
|---|
| 59 | data = NSData.dataWithBytes_length(text, text.length) |
|---|
| 60 | |
|---|
| 61 | response = NSURLResponse.alloc.objc_send( |
|---|
| 62 | :initWithURL, url, |
|---|
| 63 | :MIMEType, "text/html", |
|---|
| 64 | :expectedContentLength, data.length, |
|---|
| 65 | :textEncodingName, nil |
|---|
| 66 | ) |
|---|
| 67 | self.client.objc_send( |
|---|
| 68 | :URLProtocol, self, |
|---|
| 69 | :didReceiveResponse, response, |
|---|
| 70 | :cacheStoragePolicy, NSURLCacheStorageNotAllowed |
|---|
| 71 | ) |
|---|
| 72 | self.client.URLProtocol_didLoadData(self, data) |
|---|
| 73 | self.client.URLProtocolDidFinishLoading(self) |
|---|
| 74 | rescue => e |
|---|
| 75 | log "#{e}->#{url.path.to_s}" |
|---|
| 76 | self.client.objc_send( |
|---|
| 77 | :URLProtocol, self, |
|---|
| 78 | :didFailWithError, NSError.objc_send( |
|---|
| 79 | :errorWithDomain, NSURLErrorDomain, |
|---|
| 80 | :code, 0, |
|---|
| 81 | :userInfo, nil |
|---|
| 82 | ) |
|---|
| 83 | ) |
|---|
| 84 | end |
|---|
| 85 | |
|---|
| 86 | #-(void)stopLoading |
|---|
| 87 | def stopLoading |
|---|
| 88 | # log "stopLoading" |
|---|
| 89 | end |
|---|
| 90 | |
|---|
| 91 | #-(NSCachedURLResponse *)cachedResponse |
|---|
| 92 | |
|---|
| 93 | end |
|---|
| 94 | |
|---|
| 95 | require "chm" |
|---|
| 96 | require "pathname" |
|---|
| 97 | |
|---|
| 98 | class AppController < NSObject |
|---|
| 99 | |
|---|
| 100 | ib_action :about do |sender| |
|---|
| 101 | path = Pathname.new NSBundle.mainBundle.resourcePath.to_s |
|---|
| 102 | OSX::NSApp.orderFrontStandardAboutPanelWithOptions({ |
|---|
| 103 | # 'Credits' => nil, |
|---|
| 104 | 'Copyright' => 'GPL by cho45(さとう) see README/COPYING', |
|---|
| 105 | 'Version' => (File.read(path + 'VERSION') rescue "0/Unpackaged Test"), |
|---|
| 106 | 'ApplicationVersion' => '0' |
|---|
| 107 | }) |
|---|
| 108 | end |
|---|
| 109 | |
|---|
| 110 | ib_action :find do |sender| |
|---|
| 111 | # やってもらう |
|---|
| 112 | cd = NSDocumentController.sharedDocumentController.currentDocument |
|---|
| 113 | cd.windowControllers.first.performFindPanelAction(sender) |
|---|
| 114 | end |
|---|
| 115 | |
|---|
| 116 | def awakeFromNib |
|---|
| 117 | end |
|---|
| 118 | |
|---|
| 119 | def applicationWillFinishLaunching(n) |
|---|
| 120 | r = NSURLProtocol.registerClass CHMInternalURLProtocol |
|---|
| 121 | log "Register: #{r}" |
|---|
| 122 | |
|---|
| 123 | @config = ChemrConfig.instance |
|---|
| 124 | |
|---|
| 125 | WebPreferences.standardPreferences.userStyleSheetEnabled = true |
|---|
| 126 | WebPreferences.standardPreferences.userStyleSheetLocation = NSURL.URLWithString(@config.userstyle) |
|---|
| 127 | |
|---|
| 128 | eval(@config.initrc, binding) |
|---|
| 129 | end |
|---|
| 130 | |
|---|
| 131 | def applicationWillTerminate(n) |
|---|
| 132 | NSURLProtocol.unregisterClass CHMInternalURLProtocol |
|---|
| 133 | log "Unregister" |
|---|
| 134 | end |
|---|
| 135 | |
|---|
| 136 | end |
|---|
| 137 | |
|---|