public this repo is viewable by everyone
Description: A Gitk-like application written in RubyCocoa that looks like it belongs on a Mac. See the wiki for downloads and screenshots.
Homepage: http://alternateidea.com
Clone URL: git://github.com/Caged/gitnub.git
Click here to lend your support to: gitnub and make a donation at www.pledgie.com !
commit  7e0f6a4ada7e6778359939bed8187a521a4b12c8
tree    20f060016e8dc5b569463d6feeb5b00635593d6f
parent  c44a7a9f7db2f8b9e7fdd58a9e8b46ac6d2db942
gitnub / ImageLoadOperation.rb
100644 87 lines (72 sloc) 1.905 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
#
# ImageLoadOperation.rb
# gitnub
#
# Created by Kevin Ballard on 3/21/08.
# Copyright (c) 2008 Kevin Ballard. All rights reserved.
#
 
require 'osx/cocoa'
 
class ImageLoadOperation < OSX::NSOperation
  def initWithURL_delegate(url, delegate)
    init
    
    @url = url
    @delegate = delegate
    @executing = false
    @finished = false
    self
  end
  
  def isConcurrent
    true
  end
  
  def start
    request = NSURLRequest.requestWithURL(@url)
    @connection = NSURLConnection.connectionWithRequest_delegate(request, self)
    setExecuting true
  end
  
  def cancel
    super
    @connection.cancel
    setExecuting false
  end
  
  def isExecuting
    @executing
  end
  
  def isFinished
    @finished
  end
  
  # NSURLConnection Delegate methods
  def connection_didFailWithError(connection, error)
    @delegate.imageLoadForURL_didFailWithError(@url, error)
    setExecuting false
    setFinished true
  end
  
  def connection_didReceiveResponse(connection, response)
    length = response.expectedContentLength
    @data = NSMutableData.dataWithCapacity(length < 0 ? 0 : length)
  end
  
  def connection_didReceiveData(connection, data)
    @data.appendData(data)
  end
  
  def connectionDidFinishLoading(connection)
    image = NSImage.alloc.initWithData(@data)
    if image
      @delegate.imageLoadForURL_didFinishLoading(@url, image)
    else
      error = NSError.errorWithDomain_code_userInfo_(
        "GitNub", 0, {NSLocalizedDescriptionKey => "Could not recognize image data"}
      )
      @delegate.imageLoadForURL_didFailWithError(@url, error)
    end
  end
  
  private
  
  def setExecuting(bool)
    self.willChangeValueForKey("isExecuting")
    @executing = bool
    self.didChangeValueForKey("isExecuting")
  end
  
  def setFinished(bool)
    self.willChangeValueForKey("isFinished")
    @finished = bool
    self.didChangeValueForKey("isFinished")
  end
end