forked from mus1cjunk1e/NativaPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.m
717 lines (585 loc) · 24.1 KB
/
Controller.m
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/******************************************************************************
* Nativa - MacOS X UI for rtorrent
* http://www.aramzamzam.net
*
* Copyright Solomenchuk V. 2010.
* Solomenchuk Vladimir <[email protected]>
*
* Licensed under the GPL, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/gpl-3.0.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
#import "Controller.h"
#import "DownloadsController.h"
#import "PreferencesController.h"
#import "TorrentViewController.h"
#import "Torrent.h"
#import "TorrentTableView.h"
#import "ProcessesController.h"
#import <Growl/Growl.h>
#import "DragOverlayWindow.h"
#import "ToolbarControllerAdditions.h"
#import "QuickLookController.h"
#import "GroupsController.h"
#import "TorrentViewController.h"
#import "MoveDataController.h"
#define ACTION_MENU_PRIORITY_HIGH_TAG 101
#define ACTION_MENU_PRIORITY_NORMAL_TAG 102
#define ACTION_MENU_PRIORITY_LOW_TAG 103
#define WINDOW_REGULAR_WIDTH 500.0
#define MENU_BAR_HEIGHT 21
static NSString* DownloadsViewChangedContext = @"DownloadsViewChangedContext";
static NSString* ConnectedContext = @"ConnectedContext";
static NSString* GlobalSpeedLimitChangedContext = @"GlobalSpeedLimitChangedContext";
@interface Controller(Private)
- (NSRect) sizedWindowFrame;
- (NSRect) windowFrameByAddingHeight: (CGFloat) height checkLimits: (BOOL) check;
- (void) updateForExpandCollape;
- (void) connected;
@end
@implementation Controller
+(void) initialize
{
//Create dictionary
NSMutableDictionary* defaultValues = [NSMutableDictionary dictionary];
//Put defaults into dictionary
[defaultValues setObject:[NSNumber numberWithBool:YES]
forKey:NITrashDownloadDescriptorsKey];
[defaultValues setObject:[NSNumber numberWithInteger:3]
forKey:NIRefreshRateKey];
[defaultValues setObject:[NSNumber numberWithInteger:300]
forKey:NIUpdateGlobalsRateKey];
[defaultValues setObject:[NSNumber numberWithBool:YES]
forKey:NIAutoSizeKey];
[defaultValues setObject:[NSNumber numberWithBool:YES]
forKey:NIForceStopKey];
[defaultValues setObject:[NSNumber numberWithBool:YES]
forKey:NIGlobalSpeedLimitMaxAuto];
[defaultValues setObject:[NSNumber numberWithDouble:20]
forKey:NIGlobalSpeedLimitMaxDownload];
[defaultValues setObject:[NSNumber numberWithDouble:20]
forKey:NIGlobalSpeedLimitMaxUpload];
[defaultValues setObject:[NSNumber numberWithDouble:10]
forKey:NIGlobalSpeedLimitMinDownload];
[defaultValues setObject:[NSNumber numberWithDouble:10]
forKey:NIGlobalSpeedLimitMinUpload];
//Register the dictionary of defaults
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
//Growl needs it
[GrowlApplicationBridge setGrowlDelegate:@""];
}
- (id)init
{
if (self = [super init])
{
_defaults = [NSUserDefaults standardUserDefaults];
}
return self;
}
- (void)awakeFromNib
{
[self setupToolbar];
//for QuickLook functionality
[_window makeFirstResponder:_downloadsView];
_overlayWindow = [[DragOverlayWindow alloc] initWithWindow: _window];
//window min height
NSSize contentMinSize = [_window contentMinSize];
contentMinSize.height = [[_window contentView] frame].size.height - [[_downloadsView enclosingScrollView] frame].size.height
+ [_downloadsView rowHeight] + [_downloadsView intercellSpacing].height;
[_window setContentMinSize: contentMinSize];
//[_window setContentBorderThickness: NSMinY([[_downloadsView enclosingScrollView] frame]) forEdge: NSMinYEdge];
//bottom bar for window
//http://iloveco.de/bottom-bars-in-cocoa/
[_window setContentBorderThickness:24.0 forEdge:NSMinYEdge];
//observe notifications
NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
[nc addObserver: self selector: @selector(updateForExpandCollape)
name: @"OutlineExpandCollapse" object: nil];
[nc addObserver: self selector: @selector(setWindowSizeToFit)
name: @"AutoSizeSettingChange" object: nil];
[_viewController addObserver:self
forKeyPath:@"numberOfRowsInView"
options:0
context:&DownloadsViewChangedContext];
[[DownloadsController sharedDownloadsController] addObserver:self
forKeyPath:@"globalDownloadSpeedLimit"
options:0
context:&GlobalSpeedLimitChangedContext];
[[DownloadsController sharedDownloadsController] addObserver:self
forKeyPath:@"globalUploadSpeedLimit"
options:0
context:&GlobalSpeedLimitChangedContext];
[[DownloadsController sharedDownloadsController] addObserver:self
forKeyPath:@"connected"
options:0
context:&ConnectedContext];
[[DownloadsController sharedDownloadsController] addObserver:self
forKeyPath:@"connecting"
options:0
context:&ConnectedContext];
}
-(IBAction)showPreferencePanel:(id)sender;
{
[[PreferencesController sharedPreferencesController] openPreferences:NIPReferencesViewDefault];
}
-(IBAction)removeNoDeleteSelectedTorrents:(id)sender
{
NSArray * torrents = [(TorrentTableView *)_downloadsView selectedTorrents];
for (Torrent *t in torrents)
[[DownloadsController sharedDownloadsController] erase:t withData:NO response:nil];
[_downloadsView deselectAll: nil];
}
-(IBAction)removeDeleteSelectedTorrents:(id)sender
{
NSArray * torrents = [(TorrentTableView *)_downloadsView selectedTorrents];
for (Torrent *t in torrents)
[[DownloadsController sharedDownloadsController] erase:t withData:YES response:nil];
[_downloadsView deselectAll: nil];
}
-(IBAction)stopSelectedTorrents:(id)sender
{
NSArray * torrents = [(TorrentTableView *)_downloadsView selectedTorrents];
for (Torrent *t in torrents)
[[DownloadsController sharedDownloadsController] stop:t force:[_defaults boolForKey:NIForceStopKey] handler:nil];
}
-(IBAction)forceStopSelectedTorrents:(id)sender
{
NSArray * torrents = [(TorrentTableView *)_downloadsView selectedTorrents];
for (Torrent *t in torrents)
[[DownloadsController sharedDownloadsController] stop:t force:YES handler:nil];
}
-(IBAction)forcePauseSelectedTorrents:(id)sender
{
NSArray * torrents = [(TorrentTableView *)_downloadsView selectedTorrents];
for (Torrent *t in torrents)
[[DownloadsController sharedDownloadsController] stop:t force:NO handler:nil];
}
-(IBAction)resumeSelectedTorrents:(id)sender
{
NSArray * torrents = [(TorrentTableView *)_downloadsView selectedTorrents];
for (Torrent *t in torrents)
[[DownloadsController sharedDownloadsController] start:t handler:nil];
}
-(IBAction)checkSelectedTorrents:(id)sender
{
for (Torrent *torrent in [_downloadsView selectedTorrents])
[[DownloadsController sharedDownloadsController] check:torrent response:nil];
}
//opens window for selecting torrent
- (void) openShowSheet: (id) sender
{
NSOpenPanel * panel = [NSOpenPanel openPanel];
[panel setAllowsMultipleSelection: YES];
[panel setCanChooseFiles: YES];
[panel setCanChooseDirectories: NO];
[panel beginSheetForDirectory: nil file: nil types: [NSArray arrayWithObjects: @"org.bittorrent.torrent", @"torrent", nil]
modalForWindow: _window modalDelegate: self didEndSelector: @selector(openSheetClosed:returnCode:contextInfo:)
contextInfo: nil];
}
- (void) openSheetClosed: (NSOpenPanel *) panel returnCode: (NSInteger) code contextInfo: (NSNumber *) useOptions
{
if (code == NSOKButton)
[[DownloadsController sharedDownloadsController] add:[panel filenames]];
}
- (IBAction) toggleQuickLook:(id)sender
{
[QuickLookController show];
}
- (IBAction) revealSelectedTorrents:(id)sender
{
NSArray * torrents = [(TorrentTableView *)_downloadsView selectedTorrents];
for (Torrent *torrent in torrents)
[[DownloadsController sharedDownloadsController] reveal:torrent];
}
- (void) setGroup: (id) sender
{
NSString *group = [[GroupsController groups] nameForIndex:[sender tag]];
if (_menuTorrent == nil)
{
for (Torrent * torrent in [_downloadsView selectedTorrents])
{
[[DownloadsController sharedDownloadsController] setGroup:torrent group:group response:nil];
[_downloadsView deselectAll: nil];
}
}
else
{
[[DownloadsController sharedDownloadsController] setGroup:_menuTorrent group:group response:nil];
}
}
- (void) showGroupMenuForTorrent:(Torrent *) torrent atLocation:(NSPoint) location
{
_menuTorrent = [torrent retain];
[_groupMenu popUpMenuPositioningItem: nil atLocation: location inView: _downloadsView];
[_menuTorrent release];
_menuTorrent = nil;
}
- (NSMenu *) contextRowMenu
{
return _contextRowMenu;
}
- (void) setPriorityForSelectedTorrents: (id) sender
{
TorrentPriority priority;
switch ([sender tag])
{
case ACTION_MENU_PRIORITY_HIGH_TAG:
priority = NITorrentPriorityHigh;
break;
case ACTION_MENU_PRIORITY_NORMAL_TAG:
priority = NITorrentPriorityNormal;
break;
case ACTION_MENU_PRIORITY_LOW_TAG:
priority = NITorrentPriorityLow;
break;
default:
NSAssert1(NO, @"Unknown priority: %d", [sender tag]);
}
NSArray * torrents = [(TorrentTableView *)_downloadsView selectedTorrents];
for (Torrent *torrent in torrents)
[[DownloadsController sharedDownloadsController] setPriority:torrent priority:priority response:nil];
}
- (void) setSpeedLimitGlobal: (id) sender
{
BOOL upload = [sender menu] == _globalUploadSpeedLimitMenu;
// [fDefaults setInteger: [[sender representedObject] intValue] forKey: upload ? @"UploadLimit" : @"DownloadLimit"];
NSInteger limit = [[sender representedObject] intValue]*1024;
if (upload)
[[DownloadsController sharedDownloadsController]
setGlobalUploadSpeedLimit:limit
response:nil];
else
[[DownloadsController sharedDownloadsController]
setGlobalDownloadSpeedLimit:limit
response:nil];
}
-(void) unsetSpeedLimitGlobal: (id) sender
{
BOOL upload = [sender menu] == _globalUploadSpeedLimitMenu;
CGFloat speed = 0;
if (sender == _globalDownloadSpeedLimitMenuItem || sender == _globalUploadSpeedLimitMenuItem)
speed = sender == _globalUploadSpeedLimitMenuItem?_savedGlobalUploadSpeedLimit:_savedGlobalDownloadSpeedLimit;
if (upload)
[[DownloadsController sharedDownloadsController]
setGlobalUploadSpeedLimit:speed
response:nil];
else
[[DownloadsController sharedDownloadsController]
setGlobalDownloadSpeedLimit:speed
response:nil];
}
-(IBAction)moveSelectedTorrentsData:(id)sender
{
[[MoveDataController sharedMoveDataController] openMoveDataWindow:_window torrents:[_downloadsView selectedTorrents]];
}
#pragma mark -
#pragma mark NSMenuDelegate stuff
- (void) menuNeedsUpdate: (NSMenu *) menu
{
if (menu == _groupMenu || menu == _groupMainMenu)
{
[menu removeAllItems];
NSMenu * groupMenu;
groupMenu = [[GroupsController groups] groupMenuWithTarget: self action: @selector(setGroup:) isSmall: NO];
const NSInteger groupMenuCount = [groupMenu numberOfItems];
for (NSInteger i = 0; i < groupMenuCount; i++)
{
NSMenuItem * item = [[groupMenu itemAtIndex: 0] retain];
[groupMenu removeItemAtIndex: 0];
[menu addItem: item];
[item release];
}
}
else if (menu == _globalUploadSpeedLimitMenu || menu == _globalDownloadSpeedLimitMenu)
{
if ([menu numberOfItems] > 4)
return;
const NSInteger speedLimitActionValue[] = { 5, 10, 20, 30, 40, 50, 75, 100, 150, 200, 250, 500, 750, 1000, 1500, 2000, -1 };
NSMenuItem * item;
for (NSInteger i = 0; speedLimitActionValue[i] != -1; i++)
{
item = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: NSLocalizedString(@"%d KB/s",
"Action menu -> upload/download limit"), speedLimitActionValue[i]] action: @selector(setSpeedLimitGlobal:)
keyEquivalent: @""];
[item setTarget: self];
[item setRepresentedObject: [NSNumber numberWithInt: speedLimitActionValue[i]]];
[menu addItem: item];
[item release];
}
if (_savedGlobalDownloadSpeedLimit == 0)
{
_savedGlobalDownloadSpeedLimit = speedLimitActionValue[0]*1024;
[_globalDownloadSpeedLimitMenuItem setTitle: [NSString stringWithFormat: NSLocalizedString(@"Limit (%d KB/s)",
"Action menu -> upload/download limit"),
(NSInteger)_savedGlobalDownloadSpeedLimit/1024]];
}
if (_savedGlobalUploadSpeedLimit==0.0)
{
_savedGlobalUploadSpeedLimit = speedLimitActionValue[0]*1024;
[_globalUploadSpeedLimitMenuItem setTitle: [NSString stringWithFormat: NSLocalizedString(@"Limit (%d KB/s)",
"Action menu -> upload/download limit"),
(NSInteger)_savedGlobalUploadSpeedLimit/1024]];
}
}
else if (menu == _contextRowMenu || menu == _priorityMainMenu)
{
NSArray * torrents = [(TorrentTableView *)_downloadsView selectedTorrents];
NSInteger hp, np, lp;
TorrentPriority pp = [torrents count]>0?[[torrents objectAtIndex:0] priority]:-1;
BOOL allSame = (pp != -1);
for (Torrent *torrent in torrents)
{
if (pp != torrent.priority)
{
allSame = NO;
break;
}
}
if (allSame && [torrents count]>0)
{
const TorrentPriority priority = [[torrents objectAtIndex:0] priority];
hp = priority == NITorrentPriorityHigh ? NSOnState : NSOffState;
np = priority == NITorrentPriorityNormal ? NSOnState : NSOffState;
lp = priority == NITorrentPriorityLow ? NSOnState : NSOffState;
}
else
{
hp = np = lp = NSOffState;
}
NSMenuItem * item = [menu itemWithTag: ACTION_MENU_PRIORITY_HIGH_TAG];
[item setState: hp];
item = [menu itemWithTag: ACTION_MENU_PRIORITY_NORMAL_TAG];
[item setState: np];
item = [menu itemWithTag: ACTION_MENU_PRIORITY_LOW_TAG];
[item setState: lp];
}
else;
}
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
SEL action = [menuItem action];
BOOL canUseTable = [_window isKeyWindow] || [[menuItem menu] supermenu] != [NSApp mainMenu];
if (action == @selector(toggleQuickLook:))
{
for (Torrent * torrent in [_downloadsView selectedTorrents])
if ([[DownloadsController sharedDownloadsController] findLocation:torrent] != nil)
{
//text consistent with Finder
NSString * title = [[QuickLookController sharedQuickLookController] isVisible] ?
NSLocalizedString(@"Close Quick Look", "View menu -> Quick Look")
:NSLocalizedString(@"Quick Look", "View menu -> Quick Look");
[menuItem setTitle: title];
return YES;
}
return NO;
}
//enable pause item
if (action == @selector(stopSelectedTorrents:))
{
if ([_defaults boolForKey:NIForceStopKey])
[menuItem setTitle:NSLocalizedString(@"Stop selected", "View menu -> Quick Look")];
else
[menuItem setTitle:@"Pause selected"];
if (!canUseTable)
return NO;
for (Torrent * torrent in [_downloadsView selectedTorrents])
if (!(torrent.state == NITorrentStatePaused || torrent.state == NITorrentStateStopped))
return YES;
return NO;
}
//enable pause item
if (action == @selector(resumeSelectedTorrents:))
{
if ([_defaults boolForKey:NIForceStopKey])
[menuItem setTitle:NSLocalizedString(@"Start selected", "View menu -> Quick Look")];
else
[menuItem setTitle:@"Resume selected"];
if (!canUseTable)
return NO;
for (Torrent * torrent in [_downloadsView selectedTorrents])
if (torrent.state == NITorrentStatePaused || torrent.state == NITorrentStateStopped)
return YES;
return NO;
}
if (action == @selector(removeNoDeleteSelectedTorrents:)
|| action == @selector(checkSelectedTorrents:)
|| action == @selector(setPriorityForSelectedTorrents:)
|| action == @selector(moveSelectedTorrentsData:))
{
return canUseTable && [_downloadsView numberOfSelectedRows] > 0;
}
if (action == @selector(revealSelectedTorrents:)
|| action == @selector(removeDeleteSelectedTorrents:))
{
if(! (canUseTable && [_downloadsView numberOfSelectedRows] > 0))
return NO;
for (Torrent * torrent in [_downloadsView selectedTorrents])
if ([[DownloadsController sharedDownloadsController] findLocation:torrent] != nil)
return YES;
return NO;
}
if (action == @selector(setGroup:))
{
BOOL checked = NO;
NSInteger index = [menuItem tag];
if (_menuTorrent == nil)
{
for (Torrent * torrent in [_downloadsView selectedTorrents])
{
NSInteger torrentGroupIndex = [[GroupsController groups] groupIndexForTorrent: torrent];
if (index == torrentGroupIndex)
{
checked = YES;
break;
}
}
}
else
{
NSInteger torrentGroupIndex = [[GroupsController groups] groupIndexForTorrent: _menuTorrent];
if (index == torrentGroupIndex)
{
checked = YES;
}
}
[menuItem setState: checked ? NSOnState : NSOffState];
return canUseTable && (_menuTorrent != nil || [_downloadsView numberOfSelectedRows] > 0);
}
return YES;
}
#pragma mark -
#pragma mark NSWindowDelegate
- (NSRect) windowWillUseStandardFrame: (NSWindow *) window defaultFrame: (NSRect) defaultFrame
{
//if auto size is enabled, the current frame shouldn't need to change
NSRect frame = [_defaults boolForKey: NIAutoSizeKey] ? [window frame] : [self sizedWindowFrame];
frame.size.width = WINDOW_REGULAR_WIDTH;
return frame;
}
- (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
{
if (frameSize.width<WINDOW_REGULAR_WIDTH)
frameSize.width = WINDOW_REGULAR_WIDTH;
return frameSize;
}
@end
@implementation Controller(Private)
- (void) setWindowSizeToFit
{
if ([_defaults boolForKey: NIAutoSizeKey])
{
NSScrollView * scrollView = [_downloadsView enclosingScrollView];
[scrollView setHasVerticalScroller: NO];
[_window setFrame: [self sizedWindowFrame] display: YES animate: YES];
[scrollView setHasVerticalScroller: YES];
}
}
- (NSRect) sizedWindowFrame
{
NSInteger groups = [_viewController countGroups];
CGFloat heightChange = (GROUP_SEPARATOR_HEIGHT + [_downloadsView intercellSpacing].height) * groups
+ ([_downloadsView rowHeight] + [_downloadsView intercellSpacing].height) * ([_downloadsView numberOfRows] - groups)
- [[_downloadsView enclosingScrollView] frame].size.height;
return [self windowFrameByAddingHeight: heightChange checkLimits: YES];
}
- (NSRect) windowFrameByAddingHeight: (CGFloat) height checkLimits: (BOOL) check
{
NSScrollView * scrollView = [_downloadsView enclosingScrollView];
//convert pixels to points
NSRect windowFrame = [_window frame];
NSSize windowSize = [scrollView convertSize: windowFrame.size fromView: nil];
windowSize.height += height;
if (check)
{
NSSize minSize = [scrollView convertSize: [_window minSize] fromView: nil];
if (windowSize.height < minSize.height)
windowSize.height = minSize.height;
else
{
NSSize maxSize = [scrollView convertSize: [[_window screen] visibleFrame].size fromView: nil];
CGFloat dockHeight = [[_window screen] frame].size.height - maxSize.height;
CGFloat maxDelta = (windowFrame.origin.y>0?(windowFrame.origin.y-dockHeight+MENU_BAR_HEIGHT):0);
CGFloat maxHeight = windowFrame.size.height+(maxDelta>0?maxDelta:0);
if (maxSize.height > maxHeight)
maxSize.height = maxHeight;
if (windowSize.height > maxSize.height)
windowSize.height = maxSize.height;
}
}
//convert points to pixels
windowSize = [scrollView convertSize: windowSize toView: nil];
windowFrame.origin.y -= (windowSize.height - windowFrame.size.height);
windowFrame.size.height = windowSize.height;
return windowFrame;
}
- (void) updateForExpandCollape
{
[self setWindowSizeToFit];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if (context == &DownloadsViewChangedContext)
{
[self setWindowSizeToFit];
}
else if (context == &GlobalSpeedLimitChangedContext)
{
BOOL isDownloadSpeedLimitSet = [DownloadsController sharedDownloadsController].globalDownloadSpeedLimit>0;
BOOL isUploadSpeedLimitSet = [DownloadsController sharedDownloadsController].globalUploadSpeedLimit>0;
[_globalUploadSpeedNoLimitMenuItem setState:!isUploadSpeedLimitSet?NSOnState:NSOffState];
[_globalUploadSpeedLimitMenuItem setState:isUploadSpeedLimitSet?NSOnState:NSOffState];
[_globalDownloadSpeedNoLimitMenuItem setState:!isDownloadSpeedLimitSet?NSOnState:NSOffState];
[_globalDownloadSpeedLimitMenuItem setState:isDownloadSpeedLimitSet?NSOnState:NSOffState];
if (isDownloadSpeedLimitSet)
{
_savedGlobalDownloadSpeedLimit = [DownloadsController sharedDownloadsController].globalDownloadSpeedLimit;
[_globalDownloadSpeedLimitMenuItem setTitle: [NSString stringWithFormat: NSLocalizedString(@"Limit (%d KB/s)",
"Action menu -> upload/download limit"),
(NSInteger)_savedGlobalDownloadSpeedLimit/1024]];
}
if (isUploadSpeedLimitSet)
{
_savedGlobalUploadSpeedLimit = [DownloadsController sharedDownloadsController].globalUploadSpeedLimit;
[_globalUploadSpeedLimitMenuItem setTitle: [NSString stringWithFormat: NSLocalizedString(@"Limit (%d KB/s)",
"Action menu -> upload/download limit"),
(NSInteger)_savedGlobalUploadSpeedLimit/1024]];
}
}
else if (context == &ConnectedContext)
{
[self connected];
}
else
{
[super observeValueForKeyPath:keyPath
ofObject:object
change:change
context:context];
}
}
- (void) connected
{
if (![NSThread isMainThread])
{
[self performSelectorOnMainThread:@selector(connected) withObject:nil waitUntilDone:NO];
return;
}
if ([DownloadsController sharedDownloadsController].connected)
[_overlayWindow fadeOut];
else if ([DownloadsController sharedDownloadsController].connecting)
[_overlayWindow setImageAndMessage:[NSImage imageNamed: @"Loading.gif"] mainMessage:@"Connecting ..." message:nil];
else
[_overlayWindow setImageAndMessage:[NSImage imageNamed: @"Error-large.png"] mainMessage:@"Disconnected" message:[DownloadsController sharedDownloadsController].lastError];
}
@end