public
Fork of Caged/gitnub
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/bricooke/gitnub.git
Search Repo:
Implement async load of gravatar images
kballard (author)
Fri Mar 21 20:22:11 -0700 2008
commit  5ff35a947aa9cb8ee3decf08011292d1c1de8599
tree    4641b08565a56d0d822152974af0afcf5d803672
parent  d054f6d8a2f5bd2bf912f92b2acc122e959526c1
...
10
11
12
 
 
 
 
 
13
14
15
16
...
21
22
23
24
 
 
 
25
26
27
 
 
 
 
28
29
30
...
105
106
107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
109
110
...
10
11
12
13
14
15
16
17
18
19
20
21
...
26
27
28
 
29
30
31
32
 
 
33
34
35
36
37
38
39
...
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
0
@@ -10,6 +10,11 @@
0
 require 'md5'
0
 require 'cgi'
0
 
0
+def gravatar_url(email, size=36)
0
+ hash = MD5.hexdigest(email.downcase)
0
+ NSURL.URLWithString("http://www.gravatar.com/avatar.php?gravatar_id=#{hash}&size=#{size}")
0
+end
0
+
0
 class CommitsController < OSX::NSObject
0
   ib_outlet :commits_table
0
   ib_outlet :branch_select
0
0
@@ -21,10 +26,14 @@
0
     @current_commit_offset = 0
0
     @offset = 50
0
     @active_commit = nil
0
- @branch = :master
0
+ @branch = :master
0
+ @icon_queue = NSOperationQueue.alloc.init
0
+ @icon_url_map = {}
0
     @icons = Hash.new do |hash, email|
0
- gravatar = NSURL.URLWithString("http://www.gravatar.com/avatar.php?gravatar_id=#{MD5.hexdigest(email.downcase)}&size=36")
0
- hash[email] = NSImage.alloc.initWithContentsOfURL(gravatar)
0
+ url = gravatar_url(email)
0
+ @icon_url_map[url] = email
0
+ @icon_queue.addOperation(ImageLoadOperation.alloc.initWithURL_delegate(url, self))
0
+ hash[email] = NSImage.imageNamed(NSImageNameUser)
0
     end
0
     
0
     if(fetch_git_repository)
0
@@ -105,6 +114,22 @@
0
   
0
   def webView_contextMenuItemsForElement_defaultMenuItems(view, element, defaultMenuItems)
0
     nil
0
+ end
0
+
0
+ def imageLoadForURL_didFinishLoading(url, image)
0
+ email = @icon_url_map[url]
0
+ @icons[email] = image
0
+ # indices = NSMutableIndexSet.indexSet
0
+ # @commits.each_with_index do |commit, idx|
0
+ # if commit.author.email == email
0
+ # indices.addIndex(idx)
0
+ # end
0
+ # end
0
+ @commits_table.setNeedsDisplay(true)
0
+ end
0
+
0
+ def imageLoadForURL_didFailWithError(url, error)
0
+ STDERR.puts "Async image load failed for URL: #{url}\n#{error}"
0
   end
0
 
0
   def select_latest_commit
...
7
8
9
 
10
11
12
...
52
53
54
 
55
56
57
...
104
105
106
 
107
108
109
...
232
233
234
235
 
236
237
238
...
269
270
271
 
272
273
274
...
367
368
369
370
 
371
372
373
...
7
8
9
10
11
12
13
...
53
54
55
56
57
58
59
...
106
107
108
109
110
111
112
...
235
236
237
 
238
239
240
241
...
272
273
274
275
276
277
278
...
371
372
373
 
374
375
376
377
0
@@ -7,6 +7,7 @@
0
   objects = {
0
 
0
 /* Begin PBXBuildFile section */
0
+ 0A4023590D949E0E00659E01 /* ImageLoadOperation.rb in Resources */ = {isa = PBXBuildFile; fileRef = 0A4023580D949E0E00659E01 /* ImageLoadOperation.rb */; };
0
     0ABA3B1B0D89E53E004B6667 /* nub in Resources */ = {isa = PBXBuildFile; fileRef = 0ABA3B1A0D89E53E004B6667 /* nub */; };
0
     283254680D8B792500D99366 /* lib in Resources */ = {isa = PBXBuildFile; fileRef = 283254650D8B792500D99366 /* lib */; };
0
     285BB47C0D8306C60027980C /* mime-types in Resources */ = {isa = PBXBuildFile; fileRef = 285BB4690D8306C60027980C /* mime-types */; };
0
@@ -52,6 +53,7 @@
0
 
0
 /* Begin PBXFileReference section */
0
     089C165DFE840E0CC02AAC07 /* English */ = {isa = PBXFileReference; fileEncoding = 10; lastKnownFileType = text.plist.strings; name = English; path = English.lproj/InfoPlist.strings; sourceTree = "<group>"; };
0
+ 0A4023580D949E0E00659E01 /* ImageLoadOperation.rb */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.ruby; path = ImageLoadOperation.rb; sourceTree = "<group>"; };
0
     0ABA3B1A0D89E53E004B6667 /* nub */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = nub; sourceTree = "<group>"; };
0
     1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = /System/Library/Frameworks/Cocoa.framework; sourceTree = "<absolute>"; };
0
     283254650D8B792500D99366 /* lib */ = {isa = PBXFileReference; lastKnownFileType = folder; path = lib; sourceTree = "<group>"; };
0
@@ -104,6 +106,7 @@
0
       children = (
0
         285BB5450D849EA60027980C /* ApplicationController.rb */,
0
         285BB5460D849EA60027980C /* CommitsController.rb */,
0
+ 0A4023580D949E0E00659E01 /* ImageLoadOperation.rb */,
0
         285BB5470D849EA60027980C /* InfoWindowController.rb */,
0
         4D922CA60D8889DB002A5539 /* CommitSummaryCell.h */,
0
         4D922CA70D8889DB002A5539 /* CommitSummaryCell.m */,
0
@@ -232,7 +235,7 @@
0
 /* Begin PBXProject section */
0
     29B97313FDCFA39411CA2CEA /* Project object */ = {
0
       isa = PBXProject;
0
- buildConfigurationList = 4D7A7B570ABF745500C2D4C0 /* Build configuration list for PBXProject "gitnub" */;
0
+ buildConfigurationList = 4D7A7B570ABF745500C2D4C0 /* Build configuration list for PBXProject "GitNub" */;
0
       compatibilityVersion = "Xcode 2.4";
0
       hasScannedForEncodings = 0;
0
       mainGroup = 29B97314FDCFA39411CA2CEA /* GitNub */;
0
@@ -269,6 +272,7 @@
0
         288CB41D0D878A1C0092B5CC /* Info.xib in Resources */,
0
         0ABA3B1B0D89E53E004B6667 /* nub in Resources */,
0
         283254680D8B792500D99366 /* lib in Resources */,
0
+ 0A4023590D949E0E00659E01 /* ImageLoadOperation.rb in Resources */,
0
       );
0
       runOnlyForDeploymentPostprocessing = 0;
0
     };
0
@@ -367,7 +371,7 @@
0
 /* End XCBuildConfiguration section */
0
 
0
 /* Begin XCConfigurationList section */
0
- 4D7A7B570ABF745500C2D4C0 /* Build configuration list for PBXProject "gitnub" */ = {
0
+ 4D7A7B570ABF745500C2D4C0 /* Build configuration list for PBXProject "GitNub" */ = {
0
       isa = XCConfigurationList;
0
       buildConfigurations = (
0
         4D7A7B580ABF745500C2D4C0 /* Debug */,
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -1 +1,88 @@
0
+#
0
+# ImageLoadOperation.rb
0
+# gitnub
0
+#
0
+# Created by Kevin Ballard on 3/21/08.
0
+# Copyright (c) 2008 Kevin Ballard. All rights reserved.
0
+#
0
+
0
+require 'osx/cocoa'
0
+
0
+class ImageLoadOperation < OSX::NSOperation
0
+ def initWithURL_delegate(url, delegate)
0
+ init
0
+
0
+ @url = url
0
+ @delegate = delegate
0
+ @executing = false
0
+ @finished = false
0
+ self
0
+ end
0
+
0
+ def isConcurrent
0
+ true
0
+ end
0
+
0
+ def start
0
+ request = NSURLRequest.requestWithURL(@url)
0
+ @connection = NSURLConnection.connectionWithRequest_delegate(request, self)
0
+ setExecuting true
0
+ end
0
+
0
+ def cancel
0
+ super
0
+ @connection.cancel
0
+ setExecuting false
0
+ end
0
+
0
+ def isExecuting
0
+ @executing
0
+ end
0
+
0
+ def isFinished
0
+ @finished
0
+ end
0
+
0
+ # NSURLConnection Delegate methods
0
+ def connection_didFailWithError(connection, error)
0
+ @delegate.imageLoadForURL_didFailWithError(@url, error)
0
+ setExecuting false
0
+ setFinished true
0
+ end
0
+
0
+ def connection_didReceiveResponse(connection, response)
0
+ length = response.expectedContentLength
0
+ @data = NSMutableData.dataWithCapacity(length < 0 ? 0 : length)
0
+ end
0
+
0
+ def connection_didReceiveData(connection, data)
0
+ @data.appendData(data)
0
+ end
0
+
0
+ def connectionDidFinishLoading(connection)
0
+ image = NSImage.alloc.initWithData(@data)
0
+ if image
0
+ @delegate.imageLoadForURL_didFinishLoading(@url, image)
0
+ else
0
+ error = NSError.errorWithDomain_code_userInfo_(
0
+ "GitNub", 0, {NSLocalizedDescriptionKey => "Could not recognize image data"}
0
+ )
0
+ @delegate.imageLoadForURL_didFailWithError(@url, error)
0
+ end
0
+ end
0
+
0
+ private
0
+
0
+ def setExecuting(bool)
0
+ self.willChangeValueForKey("isExecuting")
0
+ @executing = bool
0
+ self.didChangeValueForKey("isExecuting")
0
+ end
0
+
0
+ def setFinished(bool)
0
+ self.willChangeValueForKey("isFinished")
0
+ @finished = bool
0
+ self.didChangeValueForKey("isFinished")
0
+ end
0
+end

Comments

    No one has commented yet.