-
Notifications
You must be signed in to change notification settings - Fork 217
/
BrowserController+Activity.mm
233 lines (171 loc) · 7.78 KB
/
BrowserController+Activity.mm
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*=========================================================================
Program: OsiriX
Copyright (c) OsiriX Team
All rights reserved.
Distributed under GNU - LGPL
See http://www.osirix-viewer.com/copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
=========================================================================*/
#import "BrowserController+Activity.h"
#import "ThreadsManager.h"
#import "ThreadCell.h"
#import "NSImage+N2.h"
#import "N2Operators.h"
#import "NSThread+N2.h"
#import <mach/mach_port.h>
#import <IOKit/IOKitLib.h>
#import <IOKit/storage/IOBlockStorageDriver.h>
#import <algorithm>
#import "NSUserDefaultsController+OsiriX.h"
#import "AppController.h"
@interface BrowserActivityHelper : NSObject {
BrowserController* _browser;
NSMutableArray* _cells;
}
-(id)initWithBrowser:(BrowserController*)browser;
@end
@implementation BrowserController (Activity)
-(void)awakeActivity {
// tableView = [BrowserController currentBrowser]._activityTableView;
// statusLabel = [BrowserController currentBrowser]._activityTableView;
_activityHelper = [[BrowserActivityHelper alloc] initWithBrowser:self];
[_activityTableView setDelegate: _activityHelper];
// _manager = [manager retain];
// AupdateStatsThread = [[NSThread alloc] initWithTarget:self selector:@selector(updateStatsThread:) object:NULL];
// [AupdateStatsThread start];
[_activityTableView bind:@"content" toObject:[ThreadsManager defaultManager].threadsController withKeyPath:@"arrangedObjects" options:NULL];
[[_activityTableView tableColumnWithIdentifier:@"all"] bind:@"value" toObject:[ThreadsManager defaultManager].threadsController withKeyPath:@"arrangedObjects" options:NULL];
//[NSThread detachNewThreadSelector:@selector(testThread_creator:) toTarget:self withObject:NULL];
}
/*-(void)testThread_creator:(id)t {
NSAutoreleasePool* pool = [NSAutoreleasePool new];
[[ThreadsManager defaultManager] addThreadAndStart:[NSThread currentThread]];
[NSThread currentThread].name = @"CreatorZ joinZyX";
[[NSThread currentThread] setSupportsCancel:YES];
int c = 0;
while (YES) { // ![[NSThread currentThread] isCancelled]
c++;
[NSThread detachNewThreadSelector:@selector(testThread_dummy:) toTarget:self withObject:NULL];
[[NSThread currentThread] setStatus:[NSString stringWithFormat:@"So far, I jungled %d threads..", c]];
[NSThread sleepForTimeInterval:CGFloat(random()%1000)/1000*2];
}
[pool release];
}
-(void)testThread_dummy:(id)obj {
NSAutoreleasePool* pool = [NSAutoreleasePool new];
[[ThreadsManager defaultManager] addThreadAndStart:[NSThread currentThread]];
[NSThread sleepForTimeInterval:0.5];
[pool release];
}*/
-(void)deallocActivity {
// [AupdateStatsThread cancel];
// [AupdateStatsThread release];
[[ThreadsManager defaultManager] removeObserver:_activityHelper forKeyPath:@"threads"];
[_activityHelper release];
[super dealloc];
}
-(NSTableView*)_activityTableView {
return _activityTableView;
}
@end
@implementation ThreadsTableView
-(void)selectRowIndexes:(NSIndexSet*)indexes byExtendingSelection:(BOOL)extend {
}
-(void)mouseDown:(NSEvent*)evt {
}
-(void)rightMouseDown:(NSEvent*)evt {
}
@end
@implementation BrowserActivityHelper
static NSString* const BrowserActivityHelperContext = @"BrowserActivityHelperContext";
-(id)initWithBrowser:(BrowserController*)browser {
if ((self = [super init])) {
_browser = browser; // no retaining here
_cells = [[NSMutableArray alloc] init];
// we observe the threads array so we can release cells when they're not needed anymore
[ThreadsManager.defaultManager.threadsController addObserver:self forKeyPath:@"arrangedObjects" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld|NSKeyValueObservingOptionInitial context:BrowserActivityHelperContext];
}
return self;
}
-(void)dealloc {
[_cells release];
[super dealloc];
}
-(NSCell*)cellForThread:(NSThread*)thread {
for (ThreadCell* cell in _cells)
if (cell.thread == thread)
return cell;
return nil;
}
-(void)_observeValueForKeyPathOfObjectChangeContext:(NSArray*)args {
[self observeValueForKeyPath:[args objectAtIndex:0] ofObject:[args objectAtIndex:1] change:[args objectAtIndex:2] context:[[args objectAtIndex:3] pointerValue]];
}
-(void)observeValueForKeyPath:(NSString*)keyPath ofObject:(NSArrayController*)object change:(NSDictionary*)change context:(void*)context {
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:@selector(_observeValueForKeyPathOfObjectChangeContext:) withObject:[NSArray arrayWithObjects: keyPath, object, change, [NSValue valueWithPointer:context], nil] waitUntilDone:NO];
return;
}
if (context == BrowserActivityHelperContext) {
@synchronized (ThreadsManager.defaultManager.threadsController) {
// we are looking for removed threads
NSMutableArray* threadsThatHaveCellsToRemove = [[[_cells valueForKey:@"thread"] mutableCopy] autorelease];
[threadsThatHaveCellsToRemove removeObjectsInArray:object.arrangedObjects];
for (NSThread* thread in threadsThatHaveCellsToRemove) {
ThreadCell* cell = (ThreadCell*)[self cellForThread:thread];
if (cell) {
[cell cleanup];
[cell retain];
[_cells removeObject:cell];
[_browser._activityTableView reloadData];
[cell autorelease];
}
}
return;
}
}
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
-(NSCell*)tableView:(NSTableView*)tableView dataCellForTableColumn:(NSTableColumn*)tableColumn row:(NSInteger)row {
@try {
@synchronized (ThreadsManager.defaultManager.threadsController) {
id cell = [self cellForThread: [[ThreadsManager defaultManager] threadAtIndex:row]];
if (cell == nil) {
[_cells addObject: cell = [[[ThreadCell alloc] initWithThread:[[ThreadsManager defaultManager] threadAtIndex:row] manager:ThreadsManager.defaultManager view:_browser._activityTableView] autorelease]];
}
return cell;
}
} @catch (...) {
}
return NULL;
}
-(void)tableView:(NSTableView*)tableView willDisplayCell:(ThreadCell*)cell forTableColumn:(NSTableColumn*)tableColumn row:(NSInteger)row {
NSRect frame;
if (tableColumn) frame = [tableView frameOfCellAtColumn:[tableView.tableColumns indexOfObject:tableColumn] row:row];
else frame = [tableView rectOfRow:row];
@synchronized (ThreadsManager.defaultManager.threadsController)
{
if( [_cells containsObject: cell])
{
// cancel
if (![cell.cancelButton superview])
[tableView addSubview:cell.cancelButton];
NSRect cancelFrame = NSMakeRect(frame.origin.x+frame.size.width-15-5, frame.origin.y+5, 15, 15);
if (!NSEqualRects(cell.cancelButton.frame, cancelFrame))
[cell.cancelButton setFrame:cancelFrame];
// progress
if (![cell.progressIndicator superview]) {
[tableView addSubview:cell.progressIndicator];
// [self.progressIndicator startAnimation:self];
}
NSRect progressFrame;
if ([AppController hasMacOSXLion])
progressFrame = NSMakeRect(frame.origin.x+3, frame.origin.y+27, frame.size.width-6, frame.size.height-32);
else progressFrame = NSMakeRect(frame.origin.x+1, frame.origin.y+26, frame.size.width-2, frame.size.height-28);
if (!NSEqualRects(cell.progressIndicator.frame, progressFrame))
[cell.progressIndicator setFrame:progressFrame];
}
}
}
@end