source: trunk/macosx/Controller.m @ 4178

Last change on this file since 4178 was 4178, checked in by livings124, 15 years ago

experiment with a new bottom button look on tiger

  • Property svn:keywords set to Date Rev Author Id
File size: 134.5 KB
Line 
1/******************************************************************************
2 * $Id: Controller.m 4178 2007-12-15 21:29:47Z livings124 $
3 *
4 * Copyright (c) 2005-2007 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#import <IOKit/IOMessage.h>
26
27#import "Controller.h"
28#import "Torrent.h"
29#import "TorrentCell.h"
30#import "TorrentTableView.h"
31#import "CreatorWindowController.h"
32#import "StatsWindowController.h"
33#import "AboutWindowController.h"
34#import "ButtonToolbarItem.h"
35#import "GroupToolbarItem.h"
36#import "ToolbarSegmentedCell.h"
37#import "NSApplicationAdditions.h"
38#import "NSStringAdditions.h"
39#import "UKKQueue.h"
40#import "ActionMenuSpeedToDisplayLimitTransformer.h"
41#import "ActionMenuRatioToDisplayRatioTransformer.h"
42#import "ExpandedPathToPathTransformer.h"
43#import "ExpandedPathToIconTransformer.h"
44#import "SpeedLimitToTurtleIconTransformer.h"
45
46#import <Sparkle/Sparkle.h>
47
48#define TOOLBAR_CREATE                  @"Toolbar Create"
49#define TOOLBAR_OPEN                    @"Toolbar Open"
50#define TOOLBAR_REMOVE                  @"Toolbar Remove"
51#define TOOLBAR_INFO                    @"Toolbar Info"
52#define TOOLBAR_PAUSE_ALL               @"Toolbar Pause All"
53#define TOOLBAR_RESUME_ALL              @"Toolbar Resume All"
54#define TOOLBAR_PAUSE_RESUME_ALL        @"Toolbar Pause / Resume All"
55#define TOOLBAR_PAUSE_SELECTED          @"Toolbar Pause Selected"
56#define TOOLBAR_RESUME_SELECTED         @"Toolbar Resume Selected"
57#define TOOLBAR_PAUSE_RESUME_SELECTED   @"Toolbar Pause / Resume Selected"
58#define TOOLBAR_FILTER                  @"Toolbar Toggle Filter"
59
60typedef enum
61{
62    TOOLBAR_PAUSE_TAG = 0,
63    TOOLBAR_RESUME_TAG = 1
64} toolbarGroupTag;
65
66#define SORT_DATE       @"Date"
67#define SORT_NAME       @"Name"
68#define SORT_STATE      @"State"
69#define SORT_PROGRESS   @"Progress"
70#define SORT_TRACKER    @"Tracker"
71#define SORT_ORDER      @"Order"
72#define SORT_ACTIVITY   @"Activity"
73
74typedef enum
75{
76    SORT_ORDER_TAG = 0,
77    SORT_DATE_TAG = 1,
78    SORT_NAME_TAG = 2,
79    SORT_PROGRESS_TAG = 3,
80    SORT_STATE_TAG = 4,
81    SORT_TRACKER_TAG = 5,
82    SORT_ACTIVITY_TAG = 6
83} sortTag;
84
85#define FILTER_NONE     @"None"
86#define FILTER_DOWNLOAD @"Download"
87#define FILTER_SEED     @"Seed"
88#define FILTER_PAUSE    @"Pause"
89
90#define FILTER_TYPE_NAME    @"Name"
91#define FILTER_TYPE_TRACKER @"Tracker"
92
93#define FILTER_TYPE_TAG_NAME    401
94#define FILTER_TYPE_TAG_TRACKER 402
95
96#define STATUS_RATIO_TOTAL      @"RatioTotal"
97#define STATUS_RATIO_SESSION    @"RatioSession"
98#define STATUS_TRANSFER_TOTAL   @"TransferTotal"
99#define STATUS_TRANSFER_SESSION @"TransferSession"
100
101typedef enum
102{
103    STATUS_RATIO_TOTAL_TAG = 0,
104    STATUS_RATIO_SESSION_TAG = 1,
105    STATUS_TRANSFER_TOTAL_TAG = 2,
106    STATUS_TRANSFER_SESSION_TAG = 3
107} statusTag;
108
109#define GROWL_DOWNLOAD_COMPLETE @"Download Complete"
110#define GROWL_SEEDING_COMPLETE  @"Seeding Complete"
111#define GROWL_AUTO_ADD          @"Torrent Auto Added"
112#define GROWL_AUTO_SPEED_LIMIT  @"Speed Limit Auto Changed"
113
114#define TORRENT_TABLE_VIEW_DATA_TYPE    @"TorrentTableViewDataType"
115
116#define ROW_HEIGHT_REGULAR      65.0
117#define ROW_HEIGHT_SMALL        40.0
118#define WINDOW_REGULAR_WIDTH    468.0
119
120#define SEARCH_FILTER_MIN_WIDTH 55.0
121#define SEARCH_FILTER_MAX_WIDTH 115.0
122
123#define UPDATE_UI_SECONDS           1.0
124#define AUTO_SPEED_LIMIT_SECONDS    5.0
125
126#define DOCK_SEEDING_TAG        101
127#define DOCK_DOWNLOADING_TAG    102
128
129#define SUPPORT_FOLDER  @"/Library/Application Support/Transmission/Transfers.plist"
130
131#define WEBSITE_URL @"http://transmission.m0k.org/"
132#define FORUM_URL   @"http://transmission.m0k.org/forum/"
133#define DONATE_URL  @"http://transmission.m0k.org/donate.php"
134
135void sleepCallBack(void * controller, io_service_t y, natural_t messageType, void * messageArgument)
136{
137    [(Controller *)controller sleepCallBack: messageType argument: messageArgument];
138}
139
140@implementation Controller
141
142+ (void) initialize
143{
144    //make sure another Transmission.app isn't running already
145    NSString * bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
146    int processIdentifier = [[NSProcessInfo processInfo] processIdentifier];
147
148    NSDictionary * dic;
149    NSEnumerator * enumerator = [[[NSWorkspace sharedWorkspace] launchedApplications] objectEnumerator];
150    while ((dic = [enumerator nextObject]))
151    {
152        if ([[dic objectForKey: @"NSApplicationBundleIdentifier"] isEqualToString: bundleIdentifier]
153            && [[dic objectForKey: @"NSApplicationProcessIdentifier"] intValue] != processIdentifier)
154        {
155            NSAlert * alert = [[NSAlert alloc] init];
156            [alert addButtonWithTitle: NSLocalizedString(@"Quit", "Transmission already running alert -> button")];
157            [alert setMessageText: NSLocalizedString(@"Transmission is already running.",
158                                                    "Transmission already running alert -> title")];
159            [alert setInformativeText: NSLocalizedString(@"There is already a copy of Transmission running. "
160                "This copy cannot be opened until that instance is quit.", "Transmission already running alert -> message")];
161            [alert setAlertStyle: NSWarningAlertStyle];
162           
163            [alert runModal];
164            [alert release];
165           
166            //kill ourselves right away
167            exit(0);
168        }
169    }
170   
171    [[NSUserDefaults standardUserDefaults] registerDefaults: [NSDictionary dictionaryWithContentsOfFile:
172        [[NSBundle mainBundle] pathForResource: @"Defaults" ofType: @"plist"]]];
173   
174    //set custom value transformers
175    ActionMenuSpeedToDisplayLimitTransformer * limitTransformer =
176                        [[[ActionMenuSpeedToDisplayLimitTransformer alloc] init] autorelease];
177    [NSValueTransformer setValueTransformer: limitTransformer forName: @"ActionMenuSpeedToDisplayLimitTransformer"];
178   
179    ActionMenuRatioToDisplayRatioTransformer * ratioTransformer =
180                        [[[ActionMenuRatioToDisplayRatioTransformer alloc] init] autorelease];
181    [NSValueTransformer setValueTransformer: ratioTransformer forName: @"ActionMenuRatioToDisplayRatioTransformer"];
182   
183    ExpandedPathToPathTransformer * pathTransformer =
184                        [[[ExpandedPathToPathTransformer alloc] init] autorelease];
185    [NSValueTransformer setValueTransformer: pathTransformer forName: @"ExpandedPathToPathTransformer"];
186   
187    ExpandedPathToIconTransformer * iconTransformer =
188                        [[[ExpandedPathToIconTransformer alloc] init] autorelease];
189    [NSValueTransformer setValueTransformer: iconTransformer forName: @"ExpandedPathToIconTransformer"];
190   
191    SpeedLimitToTurtleIconTransformer * speedLimitIconTransformer =
192                        [[[SpeedLimitToTurtleIconTransformer alloc] init] autorelease];
193    [NSValueTransformer setValueTransformer: speedLimitIconTransformer forName: @"SpeedLimitToTurtleIconTransformer"];
194}
195
196- (id) init
197{
198    if ((self = [super init]))
199    {
200        fLib = tr_init("macosx");
201       
202        [NSApp setDelegate: self];
203       
204        fTorrents = [[NSMutableArray alloc] init];
205        fDisplayedTorrents = [[NSMutableArray alloc] init];
206       
207        fDefaults = [NSUserDefaults standardUserDefaults];
208       
209        fMessageController = [[MessageWindowController alloc] init];
210        fInfoController = [[InfoWindowController alloc] init];
211        fPrefsController = [[PrefsController alloc] initWithHandle: fLib];
212       
213        fBadger = [[Badger alloc] initWithLib: fLib];
214       
215        fIPCController = [[IPCController alloc] init];
216        [fIPCController setDelegate: self];
217        fRemoteQuit = NO;
218       
219        [GrowlApplicationBridge setGrowlDelegate: self];
220        [[UKKQueue sharedFileWatcher] setDelegate: self];
221    }
222    return self;
223}
224
225- (void) awakeFromNib
226{
227    NSToolbar * toolbar = [[NSToolbar alloc] initWithIdentifier: @"TRMainToolbar"];
228    [toolbar setDelegate: self];
229    [toolbar setAllowsUserCustomization: YES];
230    [toolbar setAutosavesConfiguration: YES];
231    [toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
232    [fWindow setToolbar: toolbar];
233    [toolbar release];
234   
235    [fWindow setDelegate: self]; //do manually to avoid placement issue
236   
237    [fWindow makeFirstResponder: fTableView];
238    [fWindow setExcludedFromWindowsMenu: YES];
239   
240    //set table size
241    if ([fDefaults boolForKey: @"SmallView"])
242        [fTableView setRowHeight: ROW_HEIGHT_SMALL];
243   
244    //window min height
245    NSSize contentMinSize = [fWindow contentMinSize];
246    contentMinSize.height = [[fWindow contentView] frame].size.height - [fScrollView frame].size.height
247                                + [fTableView rowHeight] + [fTableView intercellSpacing].height;
248    [fWindow setContentMinSize: contentMinSize];
249   
250    if ([NSApp isOnLeopardOrBetter])
251    {
252        [fWindow setContentBorderThickness: [[fTableView enclosingScrollView] frame].origin.y forEdge: NSMinYEdge];
253        [[fTotalTorrentsField cell] setBackgroundStyle: NSBackgroundStyleRaised];
254    }
255    else
256    {
257        //bottom bar
258        [fBottomTigerBar setShowOnTiger: YES];
259        [fBottomTigerBar setHidden: NO];
260        [fBottomTigerLine setHidden: NO];
261       
262        [fActionButton setBezelStyle: NSSmallSquareBezelStyle];
263        [fSpeedLimitButton setBezelStyle: NSSmallSquareBezelStyle];
264       
265        //status bar
266        [fStatusBar setShowOnTiger: YES];
267        [fStatusButton setHidden: YES];
268        [fStatusTigerField setHidden: NO];
269        [fStatusTigerImageView setHidden: NO];
270       
271        //filter bar
272        [fNoFilterButton sizeToFit];
273        [fDownloadFilterButton sizeToFit];
274        [fSeedFilterButton sizeToFit];
275        [fPauseFilterButton sizeToFit];
276       
277        NSRect downloadRect = [fDownloadFilterButton frame];
278        downloadRect.origin.x = NSMaxX([fNoFilterButton frame]) + 2.0;
279        [fDownloadFilterButton setFrame: downloadRect];
280       
281        NSRect seedRect = [fSeedFilterButton frame];
282        seedRect.origin.x = NSMaxX(downloadRect) + 2.0;
283        [fSeedFilterButton setFrame: seedRect];
284       
285        NSRect pauseRect = [fPauseFilterButton frame];
286        pauseRect.origin.x = NSMaxX(seedRect) + 2.0;
287        [fPauseFilterButton setFrame: pauseRect];
288    }
289   
290    //set up filter bar
291    NSView * contentView = [fWindow contentView];
292    NSSize windowSize = [contentView convertSize: [fWindow frame].size fromView: nil];
293    [fFilterBar setHidden: YES];
294   
295    NSRect filterBarFrame = [fFilterBar frame];
296    filterBarFrame.size.width = windowSize.width;
297    [fFilterBar setFrame: filterBarFrame];
298   
299    [contentView addSubview: fFilterBar];
300    [fFilterBar setFrameOrigin: NSMakePoint(0, NSMaxY([contentView frame]))];
301
302    [self showFilterBar: [fDefaults boolForKey: @"FilterBar"] animate: NO];
303   
304    //set up status bar
305    [fStatusBar setHidden: YES];
306   
307    [fTotalDLField setToolTip: NSLocalizedString(@"Total download speed", "Status Bar -> speed tooltip")];
308    [fTotalULField setToolTip: NSLocalizedString(@"Total upload speed", "Status Bar -> speed tooltip")];
309   
310    NSRect statusBarFrame = [fStatusBar frame];
311    statusBarFrame.size.width = windowSize.width;
312    [fStatusBar setFrame: statusBarFrame];
313   
314    [contentView addSubview: fStatusBar];
315    [fStatusBar setFrameOrigin: NSMakePoint(0, NSMaxY([contentView frame]))];
316    [self showStatusBar: [fDefaults boolForKey: @"StatusBar"] animate: NO];
317   
318    [fActionButton setToolTip: NSLocalizedString(@"Shortcuts for changing global settings.",
319                                "Main window -> 1st bottom left button (action) tooltip")];
320    [fSpeedLimitButton setToolTip: NSLocalizedString(@"Speed Limit overrides the total bandwidth limits with its own limits.",
321                                "Main window -> 2nd bottom left button (turtle) tooltip")];
322   
323    [fPrefsController setUpdater: fUpdater];
324   
325    [fTableView setTorrents: fDisplayedTorrents];
326    [[fTableView tableColumnWithIdentifier: @"Torrent"] setDataCell: [[[TorrentCell alloc] init] autorelease]];
327   
328    [fTableView registerForDraggedTypes: [NSArray arrayWithObject: TORRENT_TABLE_VIEW_DATA_TYPE]];
329    [fWindow registerForDraggedTypes: [NSArray arrayWithObjects: NSFilenamesPboardType, NSURLPboardType, nil]];
330
331    //register for sleep notifications
332    IONotificationPortRef notify;
333    io_object_t iterator;
334    if ((fRootPort = IORegisterForSystemPower(self, & notify, sleepCallBack, & iterator)) != 0)
335        CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notify), kCFRunLoopCommonModes);
336    else
337        NSLog(@"Could not IORegisterForSystemPower");
338   
339    //load previous transfers
340    NSArray * history = [[NSArray alloc] initWithContentsOfFile: [NSHomeDirectory() stringByAppendingPathComponent: SUPPORT_FOLDER]];
341   
342    //old version saved transfer info in prefs file
343    if (!history)
344    {
345        if ((history = [fDefaults arrayForKey: @"History"]))
346            [history retain];
347        [fDefaults removeObjectForKey: @"History"];
348    }
349   
350    if (history)
351    {
352        Torrent * torrent;
353        NSDictionary * historyItem;
354        NSEnumerator * enumerator = [history objectEnumerator];
355        while ((historyItem = [enumerator nextObject]))
356            if ((torrent = [[Torrent alloc] initWithHistory: historyItem lib: fLib]))
357            {
358                [fTorrents addObject: torrent];
359                [torrent release];
360            }
361       
362        [history release];
363    }
364   
365    //set filter
366    NSString * filterType = [fDefaults stringForKey: @"Filter"];
367   
368    NSButton * currentFilterButton;
369    if ([filterType isEqualToString: FILTER_PAUSE])
370        currentFilterButton = fPauseFilterButton;
371    else if ([filterType isEqualToString: FILTER_SEED])
372        currentFilterButton = fSeedFilterButton;
373    else if ([filterType isEqualToString: FILTER_DOWNLOAD])
374        currentFilterButton = fDownloadFilterButton;
375    else
376    {
377        //safety
378        if (![filterType isEqualToString: FILTER_NONE])
379            [fDefaults setObject: FILTER_NONE forKey: @"Filter"];
380        currentFilterButton = fNoFilterButton;
381    }
382    [currentFilterButton setState: NSOnState];
383   
384    //set filter search type
385    NSString * filterSearchType = [fDefaults stringForKey: @"FilterSearchType"];
386   
387    NSMenu * filterSearchMenu = [[fSearchFilterField cell] searchMenuTemplate];
388    NSString * filterSearchTypeTitle;
389    if ([filterSearchType isEqualToString: FILTER_TYPE_TRACKER])
390        filterSearchTypeTitle = [[filterSearchMenu itemWithTag: FILTER_TYPE_TAG_TRACKER] title];
391    else
392    {
393        //safety
394        if (![filterType isEqualToString: FILTER_TYPE_NAME])
395            [fDefaults setObject: FILTER_TYPE_NAME forKey: @"FilterSearchType"];
396        filterSearchTypeTitle = [[filterSearchMenu itemWithTag: FILTER_TYPE_TAG_NAME] title];
397    }
398    [[fSearchFilterField cell] setPlaceholderString: filterSearchTypeTitle];
399   
400    //observe notifications
401    NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
402   
403    [nc addObserver: self selector: @selector(updateUI)
404                    name: @"UpdateUI" object: nil];
405   
406    [nc addObserver: self selector: @selector(torrentFinishedDownloading:)
407                    name: @"TorrentFinishedDownloading" object: nil];
408   
409    [nc addObserver: self selector: @selector(torrentRestartedDownloading:)
410                    name: @"TorrentRestartedDownloading" object: nil];
411   
412    //avoids need of setting delegate
413    [nc addObserver: self selector: @selector(torrentTableViewSelectionDidChange:)
414                    name: NSTableViewSelectionDidChangeNotification object: fTableView];
415   
416    [nc addObserver: self selector: @selector(prepareForUpdate:)
417                    name: SUUpdaterWillRestartNotification object: nil];
418    fUpdateInProgress = NO;
419   
420    [nc addObserver: self selector: @selector(autoSpeedLimitChange:)
421                    name: @"AutoSpeedLimitChange" object: nil];
422   
423    [nc addObserver: self selector: @selector(changeAutoImport)
424                    name: @"AutoImportSettingChange" object: nil];
425   
426    [nc addObserver: self selector: @selector(setWindowSizeToFit)
427                    name: @"AutoSizeSettingChange" object: nil];
428   
429    [nc addObserver: fWindow selector: @selector(makeKeyWindow)
430                    name: @"MakeWindowKey" object: nil];
431   
432    //check if torrent should now start
433    [nc addObserver: self selector: @selector(torrentStoppedForRatio:)
434                    name: @"TorrentStoppedForRatio" object: nil];
435   
436    [nc addObserver: self selector: @selector(updateTorrentsInQueue)
437                    name: @"UpdateQueue" object: nil];
438   
439    //open newly created torrent file
440    [nc addObserver: self selector: @selector(beginCreateFile:)
441                    name: @"BeginCreateTorrentFile" object: nil];
442   
443    //open newly created torrent file
444    [nc addObserver: self selector: @selector(openCreatedFile:)
445                    name: @"OpenCreatedTorrentFile" object: nil];
446
447    //timer to update the interface every second
448    [self updateUI];
449    fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_UI_SECONDS target: self
450        selector: @selector(updateUI) userInfo: nil repeats: YES];
451    [[NSRunLoop currentRunLoop] addTimer: fTimer forMode: NSModalPanelRunLoopMode];
452    [[NSRunLoop currentRunLoop] addTimer: fTimer forMode: NSEventTrackingRunLoopMode];
453   
454    [self applyFilter: nil];
455   
456    [fWindow makeKeyAndOrderFront: nil];
457   
458    if ([fDefaults boolForKey: @"InfoVisible"])
459        [self showInfo: nil];
460   
461    //timer to auto toggle speed limit
462    [self autoSpeedLimitChange: nil];
463    fSpeedLimitTimer = [NSTimer scheduledTimerWithTimeInterval: AUTO_SPEED_LIMIT_SECONDS target: self
464        selector: @selector(autoSpeedLimit) userInfo: nil repeats: YES];
465    [[NSRunLoop currentRunLoop] addTimer: fSpeedLimitTimer forMode: NSModalPanelRunLoopMode];
466    [[NSRunLoop currentRunLoop] addTimer: fSpeedLimitTimer forMode: NSEventTrackingRunLoopMode];
467}
468
469- (void) applicationDidFinishLaunching: (NSNotification *) notification
470{
471    [NSApp setServicesProvider: self];
472   
473    //register for dock icon drags
474    [[NSAppleEventManager sharedAppleEventManager] setEventHandler: self
475        andSelector: @selector(handleOpenContentsEvent:replyEvent:)
476        forEventClass: kCoreEventClass andEventID: kAEOpenContents];
477   
478    //auto importing
479    [self checkAutoImportDirectory];
480}
481
482- (BOOL) applicationShouldHandleReopen: (NSApplication *) app hasVisibleWindows: (BOOL) visibleWindows
483{
484    if (![fWindow isVisible] && ![[fPrefsController window] isVisible])
485        [fWindow makeKeyAndOrderFront: nil];
486    return NO;
487}
488
489- (NSApplicationTerminateReply) applicationShouldTerminate: (NSApplication *) sender
490{
491    if (!fUpdateInProgress && !fRemoteQuit && [fDefaults boolForKey: @"CheckQuit"])
492    {
493        int active = 0, downloading = 0;
494        Torrent * torrent;
495        NSEnumerator * enumerator = [fTorrents objectEnumerator];
496        while ((torrent = [enumerator nextObject]))
497            if ([torrent isActive])
498            {
499                active++;
500                if (![torrent allDownloaded])
501                    downloading++;
502            }
503       
504        if ([fDefaults boolForKey: @"CheckQuitDownloading"] ? downloading > 0 : active > 0)
505        {
506            NSString * message = active == 1
507                ? NSLocalizedString(@"There is an active transfer. Do you really want to quit?",
508                    "Confirm Quit panel -> message")
509                : [NSString stringWithFormat: NSLocalizedString(@"There are %d active transfers. Do you really want to quit?",
510                    "Confirm Quit panel -> message"), active];
511
512            NSBeginAlertSheet(NSLocalizedString(@"Confirm Quit", "Confirm Quit panel -> title"),
513                                NSLocalizedString(@"Quit", "Confirm Quit panel -> button"),
514                                NSLocalizedString(@"Cancel", "Confirm Quit panel -> button"), nil, fWindow, self,
515                                @selector(quitSheetDidEnd:returnCode:contextInfo:), nil, nil, message);
516            return NSTerminateLater;
517        }
518    }
519   
520    return NSTerminateNow;
521}
522
523- (void) quitSheetDidEnd: (NSWindow *) sheet returnCode: (int) returnCode contextInfo: (void *) contextInfo
524{
525    [NSApp replyToApplicationShouldTerminate: returnCode == NSAlertDefaultReturn];
526}
527
528- (void) applicationWillTerminate: (NSNotification *) notification
529{
530    [fBadger setQuitting];
531   
532    //stop timers and notification checking
533    [[NSNotificationCenter defaultCenter] removeObserver: self];
534   
535    [fTimer invalidate];
536    [fSpeedLimitTimer invalidate];
537    if (fAutoImportTimer)
538    {   
539        if ([fAutoImportTimer isValid])
540            [fAutoImportTimer invalidate];
541        [fAutoImportTimer release];
542    }
543   
544    //remove all torrent downloads
545    if (fPendingTorrentDownloads)
546    {
547        NSEnumerator * downloadEnumerator = [[fPendingTorrentDownloads allValues] objectEnumerator];
548        NSDictionary * downloadDict;
549        NSURLDownload * download;
550        while ((downloadDict = [downloadEnumerator nextObject]))
551        {
552            download = [downloadDict objectForKey: @"Download"];
553            [download cancel];
554            [download release];
555        }
556        [fPendingTorrentDownloads removeAllObjects];
557    }
558   
559    //remove all remaining torrent files in the temporary directory
560    if (fTempTorrentFiles)
561    {
562        NSEnumerator * torrentEnumerator = [fTempTorrentFiles objectEnumerator];
563        NSString * path;
564        while ((path = [torrentEnumerator nextObject]))
565            [[NSFileManager defaultManager] removeFileAtPath: path handler: nil];
566    }
567   
568    //remember window states and close all windows
569    [fDefaults setBool: [[fInfoController window] isVisible] forKey: @"InfoVisible"];
570    [[NSApp windows] makeObjectsPerformSelector: @selector(close)];
571    [self showStatusBar: NO animate: NO];
572    [self showFilterBar: NO animate: NO];
573   
574    //save history
575    [self updateTorrentHistory];
576   
577    [fDisplayedTorrents removeAllObjects];
578    [fTorrents removeAllObjects];
579   
580    //remaining calls the same as dealloc
581    [fInfoController release];
582    [fMessageController release];
583    [fPrefsController release];
584   
585    [fTorrents release];
586    [fDisplayedTorrents release];
587   
588    [fOverlayWindow release];
589    [fIPCController release];
590   
591    [fAutoImportedNames release];
592    [fPendingTorrentDownloads release];
593    [fTempTorrentFiles release];
594   
595    //complete cleanup
596    tr_close(fLib);
597}
598
599- (void) handleOpenContentsEvent: (NSAppleEventDescriptor *) event replyEvent: (NSAppleEventDescriptor *) replyEvent
600{
601    NSString * urlString = nil;
602
603    NSAppleEventDescriptor * directObject = [event paramDescriptorForKeyword: keyDirectObject];
604    if ([directObject descriptorType] == typeAEList)
605    {
606        unsigned i;
607        for (i = 1; i <= [directObject numberOfItems]; i++)
608            if ((urlString = [[directObject descriptorAtIndex: i] stringValue]))
609                break;
610    }
611    else
612        urlString = [directObject stringValue];
613   
614    if (urlString)
615        [self openURL: [[[NSURL alloc] initWithString: urlString] autorelease]];
616}
617
618- (void) download: (NSURLDownload *) download decideDestinationWithSuggestedFilename: (NSString *) suggestedName
619{
620    if ([[suggestedName pathExtension] caseInsensitiveCompare: @"torrent"] != NSOrderedSame)
621    {
622        [download cancel];
623       
624        NSRunAlertPanel(NSLocalizedString(@"Torrent download failed",
625            @"Download not a torrent -> title"), [NSString stringWithFormat:
626            NSLocalizedString(@"It appears that the file \"%@\" from %@ is not a torrent file.",
627            @"Download not a torrent -> message"), suggestedName,
628            [[[[download request] URL] absoluteString] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding]],
629            NSLocalizedString(@"OK", @"Download not a torrent -> button"), nil, nil);
630       
631        [download release];
632    }
633    else
634        [download setDestination: [NSTemporaryDirectory() stringByAppendingPathComponent: [suggestedName lastPathComponent]]
635                    allowOverwrite: NO];
636}
637
638-(void) download: (NSURLDownload *) download didCreateDestination: (NSString *) path
639{
640    if (!fPendingTorrentDownloads)
641        fPendingTorrentDownloads = [[NSMutableDictionary alloc] init];
642   
643    [fPendingTorrentDownloads setObject: [NSDictionary dictionaryWithObjectsAndKeys:
644                    path, @"Path", download, @"Download", nil] forKey: [[download request] URL]];
645}
646
647- (void) download: (NSURLDownload *) download didFailWithError: (NSError *) error
648{
649    NSRunAlertPanel(NSLocalizedString(@"Torrent download failed",
650        @"Torrent download error -> title"), [NSString stringWithFormat:
651        NSLocalizedString(@"The torrent could not be downloaded from %@ because an error occurred (%@).",
652        @"Torrent download failed -> message"),
653        [[[[download request] URL] absoluteString] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding],
654        [error localizedDescription]], NSLocalizedString(@"OK", @"Torrent download failed -> button"), nil, nil);
655   
656    [fPendingTorrentDownloads removeObjectForKey: [[download request] URL]];
657    [download release];
658}
659
660- (void) downloadDidFinish: (NSURLDownload *) download
661{
662    NSString * path = [[fPendingTorrentDownloads objectForKey: [[download request] URL]] objectForKey: @"Path"];
663   
664    [self openFiles: [NSArray arrayWithObject: path] forcePath: nil ignoreDownloadFolder:
665        ![[fDefaults stringForKey: @"DownloadChoice"] isEqualToString: @"Constant"] deleteTorrentFile: TORRENT_FILE_DELETE];
666   
667    [fPendingTorrentDownloads removeObjectForKey: [[download request] URL]];
668    [download release];
669   
670    //delete temp torrent file on quit
671    if (!fTempTorrentFiles)
672        fTempTorrentFiles = [[NSMutableArray alloc] init];
673    [fTempTorrentFiles addObject: path];
674}
675
676- (void) application: (NSApplication *) app openFiles: (NSArray *) filenames
677{
678    [self openFiles: filenames forcePath: nil ignoreDownloadFolder: NO deleteTorrentFile: TORRENT_FILE_DEFAULT];
679}
680
681- (void) openFiles: (NSArray *) filenames forcePath: (NSString *) path ignoreDownloadFolder: (BOOL) ignore
682        deleteTorrentFile: (torrentFileState) deleteTorrent
683{
684    NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
685    if (ignore || (!path && [downloadChoice isEqualToString: @"Ask"]))
686    {
687        [self openFilesAsk: [filenames mutableCopy] deleteTorrentFile: deleteTorrent];
688        return;
689    }
690   
691    #warning make submethod
692    if (!path && [fDefaults boolForKey: @"UseIncompleteDownloadFolder"]
693        && access([[[fDefaults stringForKey: @"IncompleteDownloadFolder"] stringByExpandingTildeInPath] UTF8String], 0))
694    {
695        NSOpenPanel * panel = [NSOpenPanel openPanel];
696       
697        [panel setPrompt: NSLocalizedString(@"Select", "Default incomplete folder cannot be used alert -> prompt")];
698        [panel setAllowsMultipleSelection: NO];
699        [panel setCanChooseFiles: NO];
700        [panel setCanChooseDirectories: YES];
701        [panel setCanCreateDirectories: YES];
702
703        [panel setMessage: NSLocalizedString(@"The incomplete folder cannot be used. Choose a new location or cancel for none.",
704                                        "Default incomplete folder cannot be used alert -> message")];
705       
706        NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:
707                                        filenames, @"Filenames",
708                                        [NSNumber numberWithBool: ignore], @"Ignore",
709                                        [NSNumber numberWithInt: deleteTorrent], @"DeleteTorrent", nil];
710       
711        [panel beginSheetForDirectory: nil file: nil types: nil modalForWindow: fWindow modalDelegate: self
712                didEndSelector: @selector(incompleteChoiceClosed:returnCode:contextInfo:) contextInfo: dict];
713        return;
714    }
715    if (!path && [downloadChoice isEqualToString: @"Constant"]
716        && access([[[fDefaults stringForKey: @"DownloadFolder"] stringByExpandingTildeInPath] UTF8String], 0))
717    {
718        NSOpenPanel * panel = [NSOpenPanel openPanel];
719       
720        [panel setPrompt: NSLocalizedString(@"Select", "Default folder cannot be used alert -> prompt")];
721        [panel setAllowsMultipleSelection: NO];
722        [panel setCanChooseFiles: NO];
723        [panel setCanChooseDirectories: YES];
724        [panel setCanCreateDirectories: YES];
725
726        [panel setMessage: NSLocalizedString(@"The download folder cannot be used. Choose a new location.",
727                                        "Default folder cannot be used alert -> message")];
728       
729        NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:
730                                        filenames, @"Filenames",
731                                        [NSNumber numberWithBool: ignore], @"Ignore",
732                                        [NSNumber numberWithInt: deleteTorrent], @"DeleteTorrent", nil];
733       
734        [panel beginSheetForDirectory: nil file: nil types: nil modalForWindow: fWindow modalDelegate: self
735                didEndSelector: @selector(downloadChoiceClosed:returnCode:contextInfo:) contextInfo: dict];
736        return;
737    }
738   
739    Torrent * torrent;
740    NSString * torrentPath;
741    tr_info info;
742    NSEnumerator * enumerator = [filenames objectEnumerator];
743    while ((torrentPath = [enumerator nextObject]))
744    {
745        NSString * location;
746        if (path)
747            location = path;
748        else if ([downloadChoice isEqualToString: @"Constant"])
749            location = [[fDefaults stringForKey: @"DownloadFolder"] stringByExpandingTildeInPath];
750        else
751            location = [torrentPath stringByDeletingLastPathComponent];
752       
753        if (tr_torrentParse(fLib, [torrentPath UTF8String], NULL, &info) == TR_EDUPLICATE)
754        {
755            [self duplicateOpenAlert: [NSString stringWithUTF8String: info.name]];
756            tr_metainfoFree(&info);
757            continue;
758        }
759        tr_metainfoFree(&info);
760       
761        if (!(torrent = [[Torrent alloc] initWithPath: torrentPath location: location deleteTorrentFile: deleteTorrent lib: fLib]))
762            continue;
763       
764        //add it to the "File > Open Recent" menu
765        [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL: [NSURL fileURLWithPath: torrentPath]];
766       
767        [torrent update];
768        [fTorrents addObject: torrent];
769        [torrent release];
770    }
771
772    [self updateTorrentsInQueue];
773}
774
775- (void) openCreatedFile: (NSNotification *) notification
776{
777    NSDictionary * dict = [notification userInfo];
778    [self openFiles: [NSArray arrayWithObject: [dict objectForKey: @"File"]] forcePath: [dict objectForKey: @"Path"]
779            ignoreDownloadFolder: NO deleteTorrentFile: TORRENT_FILE_SAVE];
780    [dict release];
781}
782
783- (void) incompleteChoiceClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (NSDictionary *) dictionary
784{
785    if (code == NSOKButton)
786        [fDefaults setObject: [[openPanel filenames] objectAtIndex: 0] forKey: @"IncompleteDownloadFolder"];
787    else
788        [fDefaults setBool: NO forKey: @"UseIncompleteDownloadFolder"];
789   
790    [self performSelectorOnMainThread: @selector(openFilesWithDict:) withObject: dictionary waitUntilDone: NO];
791}
792
793- (void) downloadChoiceClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (NSDictionary *) dictionary
794{
795    if (code == NSOKButton)
796    {
797        [fDefaults setObject: [[openPanel filenames] objectAtIndex: 0] forKey: @"DownloadFolder"];
798        [self performSelectorOnMainThread: @selector(openFilesWithDict:) withObject: dictionary waitUntilDone: NO];
799    }
800    else
801        [dictionary release];
802}
803
804- (void) openFilesWithDict: (NSDictionary *) dictionary
805{
806    [self openFiles: [dictionary objectForKey: @"Filenames"] forcePath: nil
807        ignoreDownloadFolder: [[dictionary objectForKey: @"Ignore"] boolValue]
808        deleteTorrentFile: [[dictionary objectForKey: @"DeleteTorrent"] intValue]];
809   
810    [dictionary release];
811}
812
813//called by the main open method to show sheet for choosing download location
814- (void) openFilesAsk: (NSMutableArray *) files deleteTorrentFile: (torrentFileState) deleteTorrent
815{
816    //determine the next file that can be opened
817    NSString * torrentPath;
818    int canAdd;
819    tr_info info;
820    while ([files count] > 0)
821    {
822        torrentPath = [[files objectAtIndex: 0] retain];
823        canAdd = tr_torrentParse(fLib, [torrentPath UTF8String], NULL, &info);
824        if (canAdd == TR_OK)
825            break;
826        else if (canAdd == TR_EDUPLICATE)
827            [self duplicateOpenAlert: [NSString stringWithUTF8String: info.name]];
828        else;
829       
830        tr_metainfoFree(&info);
831        [files removeObjectAtIndex: 0];
832    }
833   
834    //no files left to open
835    if ([files count] <= 0)
836    {
837        [files release];
838        [self updateTorrentHistory];
839        return;
840    }
841   
842    [files removeObjectAtIndex: 0];
843   
844    NSOpenPanel * panel = [NSOpenPanel openPanel];
845
846    [panel setPrompt: NSLocalizedString(@"Select", "Open torrent -> prompt")];
847    [panel setAllowsMultipleSelection: NO];
848    [panel setCanChooseFiles: NO];
849    [panel setCanChooseDirectories: YES];
850    [panel setCanCreateDirectories: YES];
851   
852    [panel setMessage: [NSString stringWithFormat: NSLocalizedString(@"Select the download folder for \"%@\"",
853                        "Open torrent -> select destination folder"), [NSString stringWithUTF8String: info.name]]];
854    tr_metainfoFree(&info);
855   
856    NSDictionary * dictionary = [[NSDictionary alloc] initWithObjectsAndKeys: torrentPath, @"Path",
857                                    files, @"Files", [NSNumber numberWithInt: deleteTorrent], @"DeleteTorrent", nil];
858    [torrentPath release];
859   
860    [panel beginSheetForDirectory: nil file: nil types: nil modalForWindow: fWindow modalDelegate: self
861            didEndSelector: @selector(folderChoiceClosed:returnCode:contextInfo:) contextInfo: dictionary];
862}
863
864- (void) folderChoiceClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (NSDictionary *) dictionary
865{
866    if (code == NSOKButton)
867    {
868        NSString * torrentPath = [dictionary objectForKey: @"Path"];
869        Torrent * torrent = [[Torrent alloc] initWithPath: torrentPath location: [[openPanel filenames] objectAtIndex: 0]
870                            deleteTorrentFile: [[dictionary objectForKey: @"DeleteTorrent"] intValue] lib: fLib];
871       
872        //add it to the "File > Open Recent" menu
873        [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL: [NSURL fileURLWithPath: torrentPath]];
874       
875        [torrent update];
876        [fTorrents addObject: torrent];
877        [torrent release];
878       
879        [self updateTorrentsInQueue];
880    }
881   
882    [self performSelectorOnMainThread: @selector(openFilesAskWithDict:) withObject: dictionary waitUntilDone: NO];
883}
884
885- (void) openFilesAskWithDict: (NSDictionary *) dictionary
886{
887    [self openFilesAsk: [dictionary objectForKey: @"Files"]
888            deleteTorrentFile: [[dictionary objectForKey: @"DeleteTorrent"] intValue]];
889    [dictionary release];
890}
891
892//called on by applescript
893- (void) open: (NSArray *) files
894{
895    [self performSelectorOnMainThread: @selector(openFiles:) withObject: files waitUntilDone: NO];
896}
897
898- (void) openFiles: (NSArray *) filenames
899{
900    [self openFiles: filenames forcePath: nil ignoreDownloadFolder: NO deleteTorrentFile: TORRENT_FILE_DEFAULT];
901}
902
903- (void) openShowSheet: (id) sender
904{
905    NSOpenPanel * panel = [NSOpenPanel openPanel];
906
907    [panel setAllowsMultipleSelection: YES];
908    [panel setCanChooseFiles: YES];
909    [panel setCanChooseDirectories: NO];
910
911    [panel beginSheetForDirectory: nil file: nil types: [NSArray arrayWithObject: @"torrent"]
912        modalForWindow: fWindow modalDelegate: self didEndSelector: @selector(openSheetClosed:returnCode:contextInfo:)
913        contextInfo: [NSNumber numberWithBool: sender == fOpenIgnoreDownloadFolder]];
914}
915
916- (void) openSheetClosed: (NSOpenPanel *) panel returnCode: (int) code contextInfo: (NSNumber *) ignore
917{
918    if (code == NSOKButton)
919    {
920        NSDictionary * dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
921                                        [panel filenames], @"Files", ignore, @"Ignore", nil];
922        [self performSelectorOnMainThread: @selector(openFromSheet:) withObject: dictionary waitUntilDone: NO];
923    }
924}
925
926- (void) openFromSheet: (NSDictionary *) dictionary
927{
928    [self openFiles: [dictionary objectForKey: @"Files"] forcePath: nil
929        ignoreDownloadFolder: [[dictionary objectForKey: @"Ignore"] boolValue] deleteTorrentFile: TORRENT_FILE_DEFAULT];
930   
931    [dictionary release];
932}
933
934- (void) duplicateOpenAlert: (NSString *) name
935{
936    if (![fDefaults boolForKey: @"WarningDuplicate"])
937        return;
938   
939    NSAlert * alert = [[NSAlert alloc] init];
940    [alert setMessageText: [NSString stringWithFormat: NSLocalizedString(@"A transfer of \"%@\" is already running.",
941                            "Open duplicate alert -> title"), name]];
942    [alert setInformativeText:
943            NSLocalizedString(@"The torrent file cannot be opened because it is a duplicate of an already running transfer.",
944                            "Open duplicate alert -> message")];
945    [alert setAlertStyle: NSWarningAlertStyle];
946    [alert addButtonWithTitle: NSLocalizedString(@"OK", "Open duplicate alert -> button")];
947   
948    BOOL onLeopard = [NSApp isOnLeopardOrBetter];
949    if (onLeopard)
950        [alert setShowsSuppressionButton: YES];
951    else
952        [alert addButtonWithTitle: NSLocalizedString(@"Don't Alert Again", "Open duplicate alert -> button")];
953   
954    NSInteger result = [alert runModal];
955    if ((onLeopard ? [[alert suppressionButton] state] == NSOnState : result == NSAlertSecondButtonReturn))
956        [fDefaults setBool: NO forKey: @"WarningDuplicate"];
957    [alert release];
958}
959
960- (void) openURL: (NSURL *) url
961{
962    [[NSURLDownload alloc] initWithRequest: [NSURLRequest requestWithURL: url] delegate: self];
963}
964
965- (void) openURLShowSheet: (id) sender
966{
967    [NSApp beginSheet: fURLSheetWindow modalForWindow: fWindow modalDelegate: self
968            didEndSelector: @selector(urlSheetDidEnd:returnCode:contextInfo:) contextInfo: nil];
969}
970
971- (void) openURLEndSheet: (id) sender
972{
973    [fURLSheetWindow orderOut: sender];
974    [NSApp endSheet: fURLSheetWindow returnCode: 1];
975}
976
977- (void) openURLCancelEndSheet: (id) sender
978{
979    [fURLSheetWindow orderOut: sender];
980    [NSApp endSheet: fURLSheetWindow returnCode: 0];
981}
982
983- (void) urlSheetDidEnd: (NSWindow *) sheet returnCode: (int) returnCode contextInfo: (void *) contextInfo
984{
985    [fURLSheetTextField selectText: self];
986    if (returnCode != 1)
987        return;
988   
989    NSString * urlString = [fURLSheetTextField stringValue];
990    if (![urlString isEqualToString: @""])
991    {
992        if ([urlString rangeOfString: @"://"].location == NSNotFound)
993        {
994            if ([urlString rangeOfString: @"."].location == NSNotFound)
995            {
996                int beforeCom;
997                if ((beforeCom = [urlString rangeOfString: @"/"].location) != NSNotFound)
998                    urlString = [NSString stringWithFormat: @"http://www.%@.com/%@",
999                                    [urlString substringToIndex: beforeCom],
1000                                    [urlString substringFromIndex: beforeCom + 1]];
1001                else
1002                    urlString = [NSString stringWithFormat: @"http://www.%@.com", urlString];
1003            }
1004            else
1005                urlString = [@"http://" stringByAppendingString: urlString];
1006        }
1007       
1008        NSURL * url = [NSURL URLWithString: urlString];
1009        [self performSelectorOnMainThread: @selector(openURL:) withObject: url waitUntilDone: NO];
1010    }
1011}
1012
1013- (void) createFile: (id) sender
1014{
1015    [CreatorWindowController createTorrentFile: fLib];
1016}
1017
1018- (void) resumeSelectedTorrents: (id) sender
1019{
1020    [self resumeTorrents: [fDisplayedTorrents objectsAtIndexes: [fTableView selectedRowIndexes]]];
1021}
1022
1023- (void) resumeAllTorrents: (id) sender
1024{
1025    [self resumeTorrents: fTorrents];
1026}
1027
1028- (void) resumeTorrents: (NSArray *) torrents
1029{
1030    NSEnumerator * enumerator = [torrents objectEnumerator];
1031    Torrent * torrent;
1032    while ((torrent = [enumerator nextObject]))
1033        [torrent setWaitToStart: YES];
1034   
1035    [self updateTorrentsInQueue];
1036}
1037
1038- (void) resumeSelectedTorrentsNoWait:  (id) sender
1039{
1040    [self resumeTorrentsNoWait: [fDisplayedTorrents objectsAtIndexes: [fTableView selectedRowIndexes]]];
1041}
1042
1043- (void) resumeWaitingTorrents: (id) sender
1044{
1045    NSMutableArray * torrents = [NSMutableArray arrayWithCapacity: [fTorrents count]];
1046   
1047    NSEnumerator * enumerator = [fTorrents objectEnumerator];
1048    Torrent * torrent;
1049    while ((torrent = [enumerator nextObject]))
1050        if (![torrent isActive] && [torrent waitingToStart])
1051            [torrents addObject: torrent];
1052   
1053    [self resumeTorrentsNoWait: torrents];
1054}
1055
1056- (void) resumeTorrentsNoWait: (NSArray *) torrents
1057{
1058    //iterate through instead of all at once to ensure no conflicts
1059    NSEnumerator * enumerator = [torrents objectEnumerator];
1060    Torrent * torrent;
1061    while ((torrent = [enumerator nextObject]))
1062        [torrent startTransfer];
1063   
1064    [self updateUI];
1065    [self applyFilter: nil];
1066    [self updateTorrentHistory];
1067}
1068
1069- (void) stopSelectedTorrents: (id) sender
1070{
1071    [self stopTorrents: [fDisplayedTorrents objectsAtIndexes: [fTableView selectedRowIndexes]]];
1072}
1073
1074- (void) stopAllTorrents: (id) sender
1075{
1076    [self stopTorrents: fTorrents];
1077}
1078
1079- (void) stopTorrents: (NSArray *) torrents
1080{
1081    //don't want any of these starting then stopping
1082    NSEnumerator * enumerator = [torrents objectEnumerator];
1083    Torrent * torrent;
1084    while ((torrent = [enumerator nextObject]))
1085        [torrent setWaitToStart: NO];
1086
1087    [torrents makeObjectsPerformSelector: @selector(stopTransfer)];
1088   
1089    [self updateUI];
1090    [self applyFilter: nil];
1091    [self updateTorrentHistory];
1092}
1093
1094- (void) removeTorrents: (NSArray *) torrents deleteData: (BOOL) deleteData deleteTorrent: (BOOL) deleteTorrent
1095{
1096    [torrents retain];
1097    int active = 0, downloading = 0;
1098
1099    if ([fDefaults boolForKey: @"CheckRemove"])
1100    {
1101        Torrent * torrent;
1102        NSEnumerator * enumerator = [torrents objectEnumerator];
1103        while ((torrent = [enumerator nextObject]))
1104            if ([torrent isActive])
1105            {
1106                active++;
1107                if (![torrent isSeeding])
1108                    downloading++;
1109            }
1110
1111        if ([fDefaults boolForKey: @"CheckRemoveDownloading"] ? downloading > 0 : active > 0)
1112        {
1113            NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:
1114                torrents, @"Torrents",
1115                [NSNumber numberWithBool: deleteData], @"DeleteData",
1116                [NSNumber numberWithBool: deleteTorrent], @"DeleteTorrent", nil];
1117
1118            NSString * title, * message;
1119           
1120            int selected = [fTableView numberOfSelectedRows];
1121            if (selected == 1)
1122            {
1123                NSString * torrentName = [[fDisplayedTorrents objectAtIndex: [fTableView selectedRow]] name];
1124               
1125                if (!deleteData && !deleteTorrent)
1126                    title = [NSString stringWithFormat: NSLocalizedString(@"Confirm removal of \"%@\" from the transfer list.",
1127                                "Removal confirm panel -> title"), torrentName];
1128                else if (deleteData && !deleteTorrent)
1129                    title = [NSString stringWithFormat: NSLocalizedString(@"Confirm removal of \"%@\" from the transfer list"
1130                                " and trash data file.", "Removal confirm panel -> title"), torrentName];
1131                else if (!deleteData && deleteTorrent)
1132                    title = [NSString stringWithFormat: NSLocalizedString(@"Confirm removal of \"%@\" from the transfer list"
1133                                " and trash torrent file.", "Removal confirm panel -> title"), torrentName];
1134                else
1135                    title = [NSString stringWithFormat: NSLocalizedString(@"Confirm removal of \"%@\" from the transfer list"
1136                            " and trash both data and torrent files.", "Removal confirm panel -> title"), torrentName];
1137               
1138                message = NSLocalizedString(@"This transfer is active."
1139                            " Once removed, continuing the transfer will require the torrent file.",
1140                            "Removal confirm panel -> message");
1141            }
1142            else
1143            {
1144                if (!deleteData && !deleteTorrent)
1145                    title = [NSString stringWithFormat: NSLocalizedString(@"Confirm removal of %d transfers"
1146                                " from the transfer list.", "Removal confirm panel -> title"), selected];
1147                else if (deleteData && !deleteTorrent)
1148                    title = [NSString stringWithFormat: NSLocalizedString(@"Confirm removal of %d transfers"
1149                                " from the transfer list and trash data file.", "Removal confirm panel -> title"), selected];
1150                else if (!deleteData && deleteTorrent)
1151                    title = [NSString stringWithFormat: NSLocalizedString(@"Confirm removal of %d transfers"
1152                                " from the transfer list and trash torrent file.", "Removal confirm panel -> title"), selected];
1153                else
1154                    title = [NSString stringWithFormat: NSLocalizedString(@"Confirm removal of %d transfers"
1155                                " from the transfer list and trash both data and torrent files.",
1156                                "Removal confirm panel -> title"), selected];
1157               
1158                if (selected == active)
1159                    message = [NSString stringWithFormat: NSLocalizedString(@"There are %d active transfers.",
1160                                "Removal confirm panel -> message part 1"), active];
1161                else
1162                    message = [NSString stringWithFormat: NSLocalizedString(@"There are %d transfers (%d active).",
1163                                "Removal confirm panel -> message part 1"), selected, active];
1164                message = [message stringByAppendingString:
1165                    NSLocalizedString(@" Once removed, continuing the transfers will require the torrent files.",
1166                                "Removal confirm panel -> message part 2")];
1167            }
1168
1169            NSBeginAlertSheet(title, NSLocalizedString(@"Remove", "Removal confirm panel -> button"),
1170                NSLocalizedString(@"Cancel", "Removal confirm panel -> button"), nil, fWindow, self,
1171                nil, @selector(removeSheetDidEnd:returnCode:contextInfo:), dict, message);
1172            return;
1173        }
1174    }
1175   
1176    [self confirmRemoveTorrents: torrents deleteData: deleteData deleteTorrent: deleteTorrent];
1177}
1178
1179- (void) removeSheetDidEnd: (NSWindow *) sheet returnCode: (int) returnCode contextInfo: (NSDictionary *) dict
1180{
1181    NSArray * torrents = [dict objectForKey: @"Torrents"];
1182    BOOL deleteData = [[dict objectForKey: @"DeleteData"] boolValue],
1183        deleteTorrent = [[dict objectForKey: @"DeleteTorrent"] boolValue];
1184    [dict release];
1185   
1186    if (returnCode == NSAlertDefaultReturn)
1187        [self confirmRemoveTorrents: torrents deleteData: deleteData deleteTorrent: deleteTorrent];
1188    else
1189        [torrents release];
1190}
1191
1192- (void) confirmRemoveTorrents: (NSArray *) torrents deleteData: (BOOL) deleteData deleteTorrent: (BOOL) deleteTorrent
1193{
1194    //don't want any of these starting then stopping
1195    NSEnumerator * enumerator = [torrents objectEnumerator];
1196    Torrent * torrent;
1197    while ((torrent = [enumerator nextObject]))
1198        [torrent setWaitToStart: NO];
1199
1200    int lowestOrderValue = -1, currentOrderValue;
1201
1202    enumerator = [torrents objectEnumerator];
1203    while ((torrent = [enumerator nextObject]))
1204    {
1205        [torrent stopTransfer];
1206
1207        if (deleteData)
1208            [torrent trashData];
1209        if (deleteTorrent)
1210            [torrent trashTorrent];
1211       
1212        //determine lowest order value
1213        currentOrderValue = [torrent orderValue];
1214        if (lowestOrderValue < currentOrderValue)
1215            lowestOrderValue = currentOrderValue;
1216       
1217        [torrent closeRemoveTorrent];
1218        [fTorrents removeObject: torrent];
1219        [fDisplayedTorrents removeObject: torrent];
1220    }
1221    [torrents release];
1222
1223    //reset the order values if necessary
1224    if (lowestOrderValue < [fTorrents count])
1225    {
1226        NSSortDescriptor * orderDescriptor = [[[NSSortDescriptor alloc] initWithKey:
1227                                                @"orderValue" ascending: YES] autorelease];
1228        NSArray * descriptors = [[NSArray alloc] initWithObjects: orderDescriptor, nil];
1229
1230        NSArray * tempTorrents = [fTorrents sortedArrayUsingDescriptors: descriptors];
1231        [descriptors release];
1232
1233        int i;
1234        for (i = lowestOrderValue; i < [tempTorrents count]; i++)
1235            [[tempTorrents objectAtIndex: i] setOrderValue: i];
1236    }
1237   
1238    [fTableView deselectAll: nil];
1239   
1240    [self updateUI];
1241    [self applyFilter: nil];
1242    [self updateTorrentHistory];
1243}
1244
1245- (void) removeNoDelete: (id) sender
1246{
1247    [self removeTorrents: [fDisplayedTorrents objectsAtIndexes: [fTableView selectedRowIndexes]]
1248                deleteData: NO deleteTorrent: NO];
1249}
1250
1251- (void) removeDeleteData: (id) sender
1252{
1253    [self removeTorrents: [fDisplayedTorrents objectsAtIndexes: [fTableView selectedRowIndexes]]
1254                deleteData: YES deleteTorrent: NO];
1255}
1256
1257- (void) removeDeleteTorrent: (id) sender
1258{
1259    [self removeTorrents: [fDisplayedTorrents objectsAtIndexes: [fTableView selectedRowIndexes]]
1260                deleteData: NO deleteTorrent: YES];
1261}
1262
1263- (void) removeDeleteDataAndTorrent: (id) sender
1264{
1265    [self removeTorrents: [fDisplayedTorrents objectsAtIndexes: [fTableView selectedRowIndexes]]
1266                deleteData: YES deleteTorrent: YES];
1267}
1268
1269- (void) moveDataFiles: (id) sender
1270{
1271    NSOpenPanel * panel = [NSOpenPanel openPanel];
1272    [panel setPrompt: NSLocalizedString(@"Select", "Move torrent -> prompt")];
1273    [panel setAllowsMultipleSelection: NO];
1274    [panel setCanChooseFiles: NO];
1275    [panel setCanChooseDirectories: YES];
1276    [panel setCanCreateDirectories: YES];
1277   
1278    NSArray * torrents = [[fDisplayedTorrents objectsAtIndexes: [fTableView selectedRowIndexes]] retain];
1279    int count = [torrents count];
1280    if (count == 1)
1281        [panel setMessage: [NSString stringWithFormat: NSLocalizedString(@"Select the new folder for \"%@\".",
1282                            "Move torrent -> select destination folder"), [[torrents objectAtIndex: 0] name]]];
1283    else
1284        [panel setMessage: [NSString stringWithFormat: NSLocalizedString(@"Select the new folder for %d data files.",
1285                            "Move torrent -> select destination folder"), count]];
1286       
1287    [panel beginSheetForDirectory: nil file: nil modalForWindow: fWindow modalDelegate: self
1288        didEndSelector: @selector(moveDataFileChoiceClosed:returnCode:contextInfo:) contextInfo: torrents];
1289}
1290
1291- (void) moveDataFileChoiceClosed: (NSOpenPanel *) panel returnCode: (int) code contextInfo: (NSArray *) torrents
1292{
1293    if (code == NSOKButton)
1294    {
1295        NSEnumerator * enumerator = [torrents objectEnumerator];
1296        Torrent * torrent;
1297        while ((torrent = [enumerator nextObject]))
1298            [torrent moveTorrentDataFileTo: [[panel filenames] objectAtIndex: 0]];
1299    }
1300   
1301    [torrents release];
1302}
1303
1304- (void) copyTorrentFiles: (id) sender
1305{
1306    [self copyTorrentFileForTorrents: [[NSMutableArray alloc] initWithArray:
1307            [fDisplayedTorrents objectsAtIndexes: [fTableView selectedRowIndexes]]]];
1308}
1309
1310- (void) copyTorrentFileForTorrents: (NSMutableArray *) torrents
1311{
1312    if ([torrents count] <= 0)
1313    {
1314        [torrents release];
1315        return;
1316    }
1317
1318    Torrent * torrent = [torrents objectAtIndex: 0];
1319
1320    //warn user if torrent file can't be found
1321    if (![[NSFileManager defaultManager] fileExistsAtPath: [torrent torrentLocation]])
1322    {
1323        NSAlert * alert = [[NSAlert alloc] init];
1324        [alert addButtonWithTitle: NSLocalizedString(@"OK", "Torrent file copy alert -> button")];
1325        [alert setMessageText: [NSString stringWithFormat: NSLocalizedString(@"Copy of \"%@\" Cannot Be Created",
1326                                "Torrent file copy alert -> title"), [torrent name]]];
1327        [alert setInformativeText: [NSString stringWithFormat:
1328                NSLocalizedString(@"The torrent file (%@) cannot be found.", "Torrent file copy alert -> message"),
1329                                    [torrent torrentLocation]]];
1330        [alert setAlertStyle: NSWarningAlertStyle];
1331       
1332        [alert runModal];
1333        [alert release];
1334       
1335        [torrents removeObjectAtIndex: 0];
1336        [self copyTorrentFileForTorrents: torrents];
1337    }
1338    else
1339    {
1340        NSSavePanel * panel = [NSSavePanel savePanel];
1341        [panel setRequiredFileType: @"torrent"];
1342        [panel setCanSelectHiddenExtension: YES];
1343       
1344        [panel beginSheetForDirectory: nil file: [torrent name] modalForWindow: fWindow modalDelegate: self
1345            didEndSelector: @selector(saveTorrentCopySheetClosed:returnCode:contextInfo:) contextInfo: torrents];
1346    }
1347}
1348
1349- (void) saveTorrentCopySheetClosed: (NSSavePanel *) panel returnCode: (int) code contextInfo: (NSMutableArray *) torrents
1350{
1351    //copy torrent to new location with name of data file
1352    if (code == NSOKButton)
1353        [[torrents objectAtIndex: 0] copyTorrentFileTo: [panel filename]];
1354   
1355    [torrents removeObjectAtIndex: 0];
1356    [self performSelectorOnMainThread: @selector(copyTorrentFileForTorrents:) withObject: torrents waitUntilDone: NO];
1357}
1358
1359- (void) revealFile: (id) sender
1360{
1361    NSEnumerator * enumerator = [[fDisplayedTorrents objectsAtIndexes: [fTableView selectedRowIndexes]] objectEnumerator];
1362    Torrent * torrent;
1363    while ((torrent = [enumerator nextObject]))
1364        [torrent revealData];
1365}
1366
1367- (void) announceSelectedTorrents: (id) sender
1368{
1369    NSEnumerator * enumerator = [[fDisplayedTorrents objectsAtIndexes: [fTableView selectedRowIndexes]] objectEnumerator];
1370    Torrent * torrent;
1371    while ((torrent = [enumerator nextObject]))
1372    {
1373        if ([torrent canManualAnnounce])
1374            [torrent manualAnnounce];
1375    }
1376}
1377
1378- (void) resetCacheForSelectedTorrents: (id) sender
1379{
1380    NSEnumerator * enumerator = [[fDisplayedTorrents objectsAtIndexes: [fTableView selectedRowIndexes]] objectEnumerator];
1381    Torrent * torrent;
1382    while ((torrent = [enumerator nextObject]))
1383        [torrent resetCache];
1384   
1385    [self applyFilter: nil];
1386}
1387
1388- (void) showPreferenceWindow: (id) sender
1389{
1390    NSWindow * window = [fPrefsController window];
1391    if (![window isVisible])
1392        [window center];
1393
1394    [window makeKeyAndOrderFront: nil];
1395}
1396
1397- (void) showAboutWindow: (id) sender
1398{
1399    [[AboutWindowController aboutController] showWindow: nil];
1400}
1401
1402- (void) showInfo: (id) sender
1403{
1404    if ([[fInfoController window] isVisible])
1405        [fInfoController close];
1406    else
1407    {
1408        [fInfoController updateInfoStats];
1409        [[fInfoController window] orderFront: nil];
1410    }
1411}
1412
1413- (void) setInfoTab: (id) sender
1414{
1415    if (sender == fNextInfoTabItem)
1416        [fInfoController setNextTab];
1417    else
1418        [fInfoController setPreviousTab];
1419}
1420
1421- (void) showMessageWindow: (id) sender
1422{
1423    [fMessageController showWindow: nil];
1424}
1425
1426- (void) showStatsWindow: (id) sender
1427{
1428    [[StatsWindowController statsWindow: fLib] showWindow: nil];
1429}
1430
1431- (void) updateUI
1432{
1433    [fTorrents makeObjectsPerformSelector: @selector(update)];
1434   
1435    if (![NSApp isHidden])
1436    {
1437        if ([fWindow isVisible])
1438        {
1439            //resort if necessary or just update the table
1440            NSString * sortType = [fDefaults stringForKey: @"Sort"];
1441            if ([sortType isEqualToString: SORT_ORDER] || [sortType isEqualToString: SORT_DATE]
1442                    || [sortType isEqualToString: SORT_NAME])
1443                [fTableView reloadData];
1444            else
1445                [self sortTorrents];
1446           
1447            //update status bar
1448            if (![fStatusBar isHidden])
1449            {
1450                //set rates
1451                float downloadRate, uploadRate;
1452                tr_torrentRates(fLib, & downloadRate, & uploadRate);
1453               
1454                [fTotalDLField setStringValue: [NSString stringForSpeed: downloadRate]];
1455                [fTotalULField setStringValue: [NSString stringForSpeed: uploadRate]];
1456               
1457                //set status button text
1458                NSString * statusLabel = [fDefaults stringForKey: @"StatusLabel"], * statusString;
1459                BOOL total;
1460                if ((total = [statusLabel isEqualToString: STATUS_RATIO_TOTAL]) || [statusLabel isEqualToString: STATUS_RATIO_SESSION])
1461                {
1462                    tr_session_stats stats;
1463                    if (total)
1464                        tr_getCumulativeSessionStats(fLib, &stats);
1465                    else
1466                        tr_getSessionStats(fLib, &stats);
1467                   
1468                    statusString = [NSLocalizedString(@"Ratio: ", "status bar -> status label")
1469                                    stringByAppendingString: [NSString stringForRatio: stats.ratio]];
1470                }
1471                else if ((total = [statusLabel isEqualToString: STATUS_TRANSFER_TOTAL])
1472                            || [statusLabel isEqualToString: STATUS_TRANSFER_SESSION])
1473                {
1474                    tr_session_stats stats;
1475                    if (total)
1476                        tr_getCumulativeSessionStats(fLib, &stats);
1477                    else
1478                        tr_getSessionStats(fLib, &stats);
1479                   
1480                    statusString = [NSString stringWithFormat: NSLocalizedString(@"DL: %@  UL: %@",
1481                        "status bar -> status label (2 spaces between)"),
1482                        [NSString stringForFileSize: stats.downloadedBytes], [NSString stringForFileSize: stats.uploadedBytes]];
1483                }
1484                else
1485                    statusString = @"";
1486               
1487                if ([NSApp isOnLeopardOrBetter])
1488                {
1489                    [fStatusButton setTitle: statusString];
1490                    [fStatusButton sizeToFit];
1491                   
1492                    //width ends up being too long
1493                    NSRect statusFrame = [fStatusButton frame];
1494                    statusFrame.size.width -= 25.0;
1495                    [fStatusButton setFrame: statusFrame];
1496                }
1497                else
1498                    [fStatusTigerField setStringValue: statusString];
1499            }
1500        }
1501
1502        //update non-constant parts of info window
1503        if ([[fInfoController window] isVisible])
1504            [fInfoController updateInfoStats];
1505    }
1506   
1507    //badge dock
1508    [fBadger updateBadge];
1509}
1510
1511- (void) updateTorrentsInQueue
1512{
1513    BOOL download = [fDefaults boolForKey: @"Queue"],
1514        seed = [fDefaults boolForKey: @"QueueSeed"];
1515   
1516    int desiredDownloadActive = [self numToStartFromQueue: YES],
1517        desiredSeedActive = [self numToStartFromQueue: NO];
1518   
1519    //sort torrents by order value
1520    NSArray * sortedTorrents;
1521    if ([fTorrents count] > 1 && (desiredDownloadActive > 0 || desiredSeedActive > 0))
1522    {
1523        NSSortDescriptor * orderDescriptor = [[[NSSortDescriptor alloc] initWithKey:
1524                                                @"orderValue" ascending: YES] autorelease];
1525        NSArray * descriptors = [[NSArray alloc] initWithObjects: orderDescriptor, nil];
1526       
1527        sortedTorrents = [fTorrents sortedArrayUsingDescriptors: descriptors];
1528        [descriptors release];
1529    }
1530    else
1531        sortedTorrents = fTorrents;
1532   
1533    Torrent * torrent;
1534    NSEnumerator * enumerator = [sortedTorrents objectEnumerator];
1535    while ((torrent = [enumerator nextObject]))
1536    {
1537        if (![torrent isActive] && ![torrent isChecking] && [torrent waitingToStart])
1538        {
1539            if (![torrent allDownloaded])
1540            {
1541                if (!download || desiredDownloadActive > 0)
1542                {
1543                    [torrent startTransfer];
1544                    if ([torrent isActive])
1545                        desiredDownloadActive--;
1546                    [torrent update];
1547                }
1548            }
1549            else
1550            {
1551                if (!seed || desiredSeedActive > 0)
1552                {
1553                    [torrent startTransfer];
1554                    if ([torrent isActive])
1555                        desiredSeedActive--;
1556                    [torrent update];
1557                }
1558            }
1559        }
1560    }
1561   
1562    [self updateUI];
1563    [self applyFilter: nil];
1564    [self updateTorrentHistory];
1565}
1566
1567- (int) numToStartFromQueue: (BOOL) downloadQueue
1568{
1569    if (![fDefaults boolForKey: downloadQueue ? @"Queue" : @"QueueSeed"])
1570        return 0;
1571   
1572    int desired = [fDefaults integerForKey: downloadQueue ? @"QueueDownloadNumber" : @"QueueSeedNumber"];
1573       
1574    Torrent * torrent;
1575    NSEnumerator * enumerator = [fTorrents objectEnumerator];
1576    while ((torrent = [enumerator nextObject]))
1577    {
1578        if ([torrent isChecking])
1579        {
1580            desired--;
1581            if (desired <= 0)
1582                return 0;
1583        }
1584        else if ([torrent isActive] && ![torrent isStalled] && ![torrent isError])
1585        {
1586            if ([torrent allDownloaded] != downloadQueue)
1587            {
1588                desired--;
1589                if (desired <= 0)
1590                    return 0;
1591            }
1592        }
1593        else;
1594    }
1595   
1596    return desired;
1597}
1598
1599- (void) torrentFinishedDownloading: (NSNotification *) notification
1600{
1601    Torrent * torrent = [notification object];
1602    if ([torrent isActive])
1603    {
1604        if ([fDefaults boolForKey: @"PlayDownloadSound"])
1605        {
1606            NSSound * sound;
1607            if ((sound = [NSSound soundNamed: [fDefaults stringForKey: @"DownloadSound"]]))
1608                [sound play];
1609        }
1610       
1611        NSDictionary * clickContext = [NSDictionary dictionaryWithObjectsAndKeys: GROWL_DOWNLOAD_COMPLETE, @"Type",
1612                                        [torrent dataLocation] , @"Location", nil];
1613        [GrowlApplicationBridge notifyWithTitle: NSLocalizedString(@"Download Complete", "Growl notification title")
1614                                    description: [torrent name] notificationName: GROWL_DOWNLOAD_COMPLETE
1615                                    iconData: nil priority: 0 isSticky: NO clickContext: clickContext];
1616       
1617        if (![fWindow isMainWindow])
1618            [fBadger incrementCompleted];
1619       
1620        if ([fDefaults boolForKey: @"QueueSeed"] && [self numToStartFromQueue: NO] <= 0)
1621        {
1622            [torrent stopTransfer];
1623            [torrent setWaitToStart: YES];
1624        }
1625    }
1626   
1627    [self updateTorrentsInQueue];
1628}
1629
1630- (void) torrentRestartedDownloading: (NSNotification *) notification
1631{
1632    Torrent * torrent = [notification object];
1633    if ([torrent isActive])
1634    {
1635        if ([fDefaults boolForKey: @"Queue"] && [self numToStartFromQueue: YES] <= 0)
1636        {
1637            [torrent stopTransfer];
1638            [torrent setWaitToStart: YES];
1639        }
1640    }
1641   
1642    [self updateTorrentsInQueue];
1643}
1644
1645- (void) updateTorrentHistory
1646{
1647    NSMutableArray * history = [NSMutableArray arrayWithCapacity: [fTorrents count]];
1648
1649    NSEnumerator * enumerator = [fTorrents objectEnumerator];
1650    Torrent * torrent;
1651    while ((torrent = [enumerator nextObject]))
1652        [history addObject: [torrent history]];
1653
1654    [history writeToFile: [NSHomeDirectory() stringByAppendingPathComponent: SUPPORT_FOLDER] atomically: YES];
1655}
1656
1657- (void) sortTorrents
1658{
1659    //remember selected rows if needed
1660    NSArray * selectedTorrents = nil;
1661    int numSelected = [fTableView numberOfSelectedRows];
1662    if (numSelected > 0 && numSelected < [fDisplayedTorrents count])
1663        selectedTorrents = [fDisplayedTorrents objectsAtIndexes: [fTableView selectedRowIndexes]];
1664
1665    [self sortTorrentsIgnoreSelected]; //actually sort
1666   
1667    //set selected rows if needed
1668    if (selectedTorrents)
1669    {
1670        Torrent * torrent;
1671        NSEnumerator * enumerator = [selectedTorrents objectEnumerator];
1672        NSMutableIndexSet * indexSet = [[NSMutableIndexSet alloc] init];
1673        while ((torrent = [enumerator nextObject]))
1674            [indexSet addIndex: [fDisplayedTorrents indexOfObject: torrent]];
1675       
1676        [fTableView selectRowIndexes: indexSet byExtendingSelection: NO];
1677        [indexSet release];
1678    }
1679}
1680
1681//doesn't remember selected rows
1682- (void) sortTorrentsIgnoreSelected
1683{
1684    NSString * sortType = [fDefaults stringForKey: @"Sort"];
1685    BOOL asc = ![fDefaults boolForKey: @"SortReverse"];
1686   
1687    NSSortDescriptor * nameDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"name"
1688                                            ascending: asc selector: @selector(caseInsensitiveCompare:)] autorelease],
1689                    * orderDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"orderValue"
1690                                            ascending: asc] autorelease];
1691   
1692    NSArray * descriptors;
1693    if ([sortType isEqualToString: SORT_NAME])
1694        descriptors = [[NSArray alloc] initWithObjects: nameDescriptor, orderDescriptor, nil];
1695    else if ([sortType isEqualToString: SORT_STATE])
1696    {
1697        NSSortDescriptor * stateDescriptor = [[[NSSortDescriptor alloc] initWithKey:
1698                                                @"stateSortKey" ascending: !asc] autorelease],
1699                        * progressDescriptor = [[[NSSortDescriptor alloc] initWithKey:
1700                                            @"progress" ascending: !asc] autorelease],
1701                        * ratioDescriptor = [[[NSSortDescriptor alloc] initWithKey:
1702                                            @"ratio" ascending: !asc] autorelease];
1703       
1704        descriptors = [[NSArray alloc] initWithObjects: stateDescriptor, progressDescriptor, ratioDescriptor,
1705                                                            nameDescriptor, orderDescriptor, nil];
1706    }
1707    else if ([sortType isEqualToString: SORT_PROGRESS])
1708    {
1709        NSSortDescriptor * progressDescriptor = [[[NSSortDescriptor alloc] initWithKey:
1710                                            @"progress" ascending: asc] autorelease],
1711                        * ratioProgressDescriptor = [[[NSSortDescriptor alloc] initWithKey:
1712                                            @"progressStopRatio" ascending: asc] autorelease],
1713                        * ratioDescriptor = [[[NSSortDescriptor alloc] initWithKey:
1714                                            @"ratio" ascending: asc] autorelease];
1715       
1716        descriptors = [[NSArray alloc] initWithObjects: progressDescriptor, ratioProgressDescriptor, ratioDescriptor,
1717                                                            nameDescriptor, orderDescriptor, nil];
1718    }
1719    else if ([sortType isEqualToString: SORT_TRACKER])
1720    {
1721        NSSortDescriptor * trackerDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"trackerAddress"
1722                                                ascending: asc selector: @selector(caseInsensitiveCompare:)] autorelease];
1723       
1724        descriptors = [[NSArray alloc] initWithObjects: trackerDescriptor, nameDescriptor, orderDescriptor, nil];
1725    }
1726    else if ([sortType isEqualToString: SORT_ORDER])
1727        descriptors = [[NSArray alloc] initWithObjects: orderDescriptor, nil];
1728    else if ([sortType isEqualToString: SORT_ACTIVITY])
1729    {
1730        NSSortDescriptor * rateDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"totalRate" ascending: !asc]
1731                                                    autorelease];
1732        NSSortDescriptor * activityDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"dateActivityOrAdd" ascending: !asc]
1733                                                    autorelease];
1734       
1735        descriptors = [[NSArray alloc] initWithObjects: rateDescriptor, activityDescriptor, orderDescriptor, nil];
1736    }
1737    else
1738    {
1739        NSSortDescriptor * dateDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"dateAdded" ascending: asc] autorelease];
1740   
1741        descriptors = [[NSArray alloc] initWithObjects: dateDescriptor, orderDescriptor, nil];
1742    }
1743   
1744    [fDisplayedTorrents sortUsingDescriptors: descriptors];
1745    [descriptors release];
1746   
1747    [fTableView reloadData];
1748}
1749
1750- (void) setSort: (id) sender
1751{
1752    NSString * sortType;
1753    switch ([sender tag])
1754    {
1755        case SORT_ORDER_TAG:
1756            sortType = SORT_ORDER;
1757            [fDefaults setBool: NO forKey: @"SortReverse"];
1758            break;
1759        case SORT_DATE_TAG:
1760            sortType = SORT_DATE;
1761            break;
1762        case SORT_NAME_TAG:
1763            sortType = SORT_NAME;
1764            break;
1765        case SORT_PROGRESS_TAG:
1766            sortType = SORT_PROGRESS;
1767            break;
1768        case SORT_STATE_TAG:
1769            sortType = SORT_STATE;
1770            break;
1771        case SORT_TRACKER_TAG:
1772            sortType = SORT_TRACKER;
1773            break;
1774        case SORT_ACTIVITY_TAG:
1775            sortType = SORT_ACTIVITY;
1776            break;
1777        default:
1778            return;
1779    }
1780   
1781    [fDefaults setObject: sortType forKey: @"Sort"];
1782    [self sortTorrents];
1783}
1784
1785- (void) setSortReverse: (id) sender
1786{
1787    [fDefaults setBool: ![fDefaults boolForKey: @"SortReverse"] forKey: @"SortReverse"];
1788    [self sortTorrents];
1789}
1790
1791- (void) applyFilter: (id) sender
1792{
1793    //remember selected rows if needed
1794    NSArray * selectedTorrents = [fTableView numberOfSelectedRows] > 0
1795                ? [fDisplayedTorrents objectsAtIndexes: [fTableView selectedRowIndexes]] : nil;
1796
1797    NSMutableArray * tempTorrents = [NSMutableArray arrayWithCapacity: [fTorrents count]];
1798   
1799    NSString * filterType = [fDefaults stringForKey: @"Filter"];
1800   
1801    int downloading = 0, seeding = 0, paused = 0;
1802    BOOL isDownload = [filterType isEqualToString: FILTER_DOWNLOAD],
1803            isSeed = [filterType isEqualToString: FILTER_SEED],
1804            isPause = [filterType isEqualToString: FILTER_PAUSE];
1805    BOOL filtering = isDownload || isSeed || isPause;
1806   
1807    //get count of each type
1808    NSEnumerator * enumerator = [fTorrents objectEnumerator];
1809    Torrent * torrent;
1810    while ((torrent = [enumerator nextObject]))
1811    {
1812        if ([torrent isActive])
1813        {
1814            if ([torrent isSeeding])
1815            {
1816                seeding++;
1817                if (isSeed)
1818                    [tempTorrents addObject: torrent];
1819            }
1820            else
1821            {
1822                downloading++;
1823                if (isDownload)
1824                    [tempTorrents addObject: torrent];
1825            }
1826        }
1827        else
1828        {
1829            paused++;
1830            if (isPause)
1831                [tempTorrents addObject: torrent];
1832        }
1833    }
1834   
1835    if (!filtering)
1836        [tempTorrents setArray: fTorrents];
1837   
1838    //set button tooltips
1839    [fNoFilterButton setCount: [fTorrents count]];
1840    [fDownloadFilterButton setCount: downloading];
1841    [fSeedFilterButton setCount: seeding];
1842    [fPauseFilterButton setCount: paused];
1843   
1844    NSString * searchString = [fSearchFilterField stringValue];
1845    if ([searchString length] > 0)
1846    {
1847        filtering = YES;
1848       
1849        Torrent * torrent;
1850        BOOL filterTracker = [[fDefaults stringForKey: @"FilterSearchType"] isEqualToString: FILTER_TYPE_TRACKER], remove;
1851       
1852        int i;
1853        for (i = [tempTorrents count]-1; i >= 0; i--)
1854        {
1855            torrent = [tempTorrents objectAtIndex: i];
1856            remove = YES;
1857           
1858            if (filterTracker)
1859            {
1860                NSEnumerator * trackerEnumerator = [[torrent allTrackers] objectEnumerator], * subTrackerEnumerator;
1861                NSArray * subTrackers;
1862                NSString * tracker;
1863                while (remove && (subTrackers = [trackerEnumerator nextObject]))
1864                {
1865                    subTrackerEnumerator = [subTrackers objectEnumerator];
1866                    while ((tracker = [subTrackerEnumerator nextObject]))
1867                    {
1868                        if ([tracker rangeOfString: searchString options: NSCaseInsensitiveSearch].location != NSNotFound)
1869                        {
1870                            remove = NO;
1871                            break;
1872                        }
1873                    }
1874                }
1875            }
1876            else
1877                remove = [[torrent name] rangeOfString: searchString options: NSCaseInsensitiveSearch].location == NSNotFound;
1878           
1879            if (remove)
1880                [tempTorrents removeObjectAtIndex: i];
1881        }
1882    }
1883   
1884    [fDisplayedTorrents setArray: tempTorrents];
1885   
1886    [self sortTorrentsIgnoreSelected];
1887   
1888    //set selected rows if needed
1889    if (selectedTorrents)
1890    {
1891        Torrent * torrent;
1892        NSEnumerator * enumerator = [selectedTorrents objectEnumerator];
1893        NSMutableIndexSet * indexSet = [[NSMutableIndexSet alloc] init];
1894        unsigned index;
1895        while ((torrent = [enumerator nextObject]))
1896            if ((index = [fDisplayedTorrents indexOfObject: torrent]) != NSNotFound)
1897                [indexSet addIndex: index];
1898       
1899        [fTableView selectRowIndexes: indexSet byExtendingSelection: NO];
1900        [indexSet release];
1901    }
1902   
1903    //set status bar torrent count text
1904    NSString * totalTorrentsString;
1905    int totalCount = [fTorrents count];
1906    if (totalCount != 1)
1907        totalTorrentsString = [NSString stringWithFormat: NSLocalizedString(@"%d transfers", "Status bar transfer count"), totalCount];
1908    else
1909        totalTorrentsString = NSLocalizedString(@"1 transfer", "Status bar transfer count");
1910   
1911    if (filtering)
1912        totalTorrentsString = [NSString stringWithFormat: @"%d of %@", [fDisplayedTorrents count], totalTorrentsString];
1913   
1914    [fTotalTorrentsField setStringValue: totalTorrentsString];
1915
1916    [self setWindowSizeToFit];
1917}
1918
1919//resets filter and sorts torrents
1920- (void) setFilter: (id) sender
1921{
1922    NSString * oldFilterType = [fDefaults stringForKey: @"Filter"];
1923   
1924    NSButton * prevFilterButton;
1925    if ([oldFilterType isEqualToString: FILTER_PAUSE])
1926        prevFilterButton = fPauseFilterButton;
1927    else if ([oldFilterType isEqualToString: FILTER_SEED])
1928        prevFilterButton = fSeedFilterButton;
1929    else if ([oldFilterType isEqualToString: FILTER_DOWNLOAD])
1930        prevFilterButton = fDownloadFilterButton;
1931    else
1932        prevFilterButton = fNoFilterButton;
1933   
1934    if (sender != prevFilterButton)
1935    {
1936        [prevFilterButton setState: NSOffState];
1937        [sender setState: NSOnState];
1938
1939        NSString * filterType;
1940        if (sender == fDownloadFilterButton)
1941            filterType = FILTER_DOWNLOAD;
1942        else if (sender == fPauseFilterButton)
1943            filterType = FILTER_PAUSE;
1944        else if (sender == fSeedFilterButton)
1945            filterType = FILTER_SEED;
1946        else
1947            filterType = FILTER_NONE;
1948
1949        [fDefaults setObject: filterType forKey: @"Filter"];
1950    }
1951    else
1952        [sender setState: NSOnState];
1953
1954    [self applyFilter: nil];
1955}
1956
1957- (void) setFilterSearchType: (id) sender
1958{
1959    NSString * oldFilterType = [fDefaults stringForKey: @"FilterSearchType"];
1960   
1961    int prevTag, currentTag = [sender tag];
1962    if ([oldFilterType isEqualToString: FILTER_TYPE_TRACKER])
1963        prevTag = FILTER_TYPE_TAG_TRACKER;
1964    else
1965        prevTag = FILTER_TYPE_TAG_NAME;
1966   
1967    if (currentTag != prevTag)
1968    {
1969        NSString * filterType;
1970        if (currentTag == FILTER_TYPE_TAG_TRACKER)
1971            filterType = FILTER_TYPE_TRACKER;
1972        else
1973            filterType = FILTER_TYPE_NAME;
1974       
1975        [fDefaults setObject: filterType forKey: @"FilterSearchType"];
1976       
1977        [[fSearchFilterField cell] setPlaceholderString: [sender title]];
1978    }
1979   
1980    [self applyFilter: nil];
1981}
1982
1983- (void) switchFilter: (id) sender
1984{
1985    NSString * filterType = [fDefaults stringForKey: @"Filter"];
1986   
1987    NSButton * button;
1988    if ([filterType isEqualToString: FILTER_NONE])
1989        button = sender == fNextFilterItem ? fDownloadFilterButton : fPauseFilterButton;
1990    else if ([filterType isEqualToString: FILTER_DOWNLOAD])
1991        button = sender == fNextFilterItem ? fSeedFilterButton : fNoFilterButton;
1992    else if ([filterType isEqualToString: FILTER_SEED])
1993        button = sender == fNextFilterItem ? fPauseFilterButton : fDownloadFilterButton;
1994    else if ([filterType isEqualToString: FILTER_PAUSE])
1995        button = sender == fNextFilterItem ? fNoFilterButton : fSeedFilterButton;
1996    else
1997        button = fNoFilterButton;
1998   
1999    [self setFilter: button];
2000}
2001
2002- (void) setStatusLabel: (id) sender
2003{
2004    NSString * statusLabel;
2005    switch ([sender tag])
2006    {
2007        case STATUS_RATIO_TOTAL_TAG:
2008            statusLabel = STATUS_RATIO_TOTAL;
2009            break;
2010        case STATUS_RATIO_SESSION_TAG:
2011            statusLabel = STATUS_RATIO_SESSION;
2012            break;
2013        case STATUS_TRANSFER_TOTAL_TAG:
2014            statusLabel = STATUS_TRANSFER_TOTAL;
2015            break;
2016        case STATUS_TRANSFER_SESSION_TAG:
2017            statusLabel = STATUS_TRANSFER_SESSION;
2018            break;
2019        default:
2020            return;
2021    }
2022   
2023    [fDefaults setObject: statusLabel forKey: @"StatusLabel"];
2024    [self updateUI];
2025}
2026
2027- (void) toggleSpeedLimit: (id) sender
2028{
2029    [fDefaults setBool: ![fDefaults boolForKey: @"SpeedLimit"] forKey: @"SpeedLimit"];
2030    [fPrefsController applySpeedSettings: nil];
2031}
2032
2033- (void) autoSpeedLimitChange: (NSNotification *) notification
2034{
2035    if (![fDefaults boolForKey: @"SpeedLimitAuto"])
2036        return;
2037 
2038    NSCalendarDate * onDate = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate:
2039                        [[fDefaults objectForKey: @"SpeedLimitAutoOnDate"] timeIntervalSinceReferenceDate]],
2040        * offDate = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate:
2041                        [[fDefaults objectForKey: @"SpeedLimitAutoOffDate"] timeIntervalSinceReferenceDate]],
2042        * nowDate = [NSCalendarDate calendarDate];
2043   
2044    //check if should be on if within range
2045    int onTime = [onDate hourOfDay] * 60 + [onDate minuteOfHour],
2046        offTime = [offDate hourOfDay] * 60 + [offDate minuteOfHour],
2047        nowTime = [nowDate hourOfDay] * 60 + [nowDate minuteOfHour];
2048   
2049    BOOL shouldBeOn = NO;
2050    if (onTime < offTime)
2051        shouldBeOn = onTime <= nowTime && nowTime < offTime;
2052    else if (onTime > offTime)
2053        shouldBeOn = onTime <= nowTime || nowTime < offTime;
2054    else;
2055   
2056    if ([fDefaults boolForKey: @"SpeedLimit"] != shouldBeOn)
2057        [self toggleSpeedLimit: nil];
2058}
2059
2060- (void) autoSpeedLimit
2061{
2062    if (![fDefaults boolForKey: @"SpeedLimitAuto"])
2063        return;
2064   
2065    //only toggle if within first few seconds of minutes
2066    NSCalendarDate * nowDate = [NSCalendarDate calendarDate];
2067    if ([nowDate secondOfMinute] > AUTO_SPEED_LIMIT_SECONDS)
2068        return;
2069   
2070    NSCalendarDate * offDate = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate:
2071                        [[fDefaults objectForKey: @"SpeedLimitAutoOffDate"] timeIntervalSinceReferenceDate]];
2072   
2073    BOOL toggle;
2074    if ([fDefaults boolForKey: @"SpeedLimit"])
2075        toggle = [nowDate hourOfDay] == [offDate hourOfDay] && [nowDate minuteOfHour] == [offDate minuteOfHour];
2076    else
2077    {
2078        NSCalendarDate * onDate = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate:
2079                        [[fDefaults objectForKey: @"SpeedLimitAutoOnDate"] timeIntervalSinceReferenceDate]];
2080        toggle = ([nowDate hourOfDay] == [onDate hourOfDay] && [nowDate minuteOfHour] == [onDate minuteOfHour])
2081                    && !([onDate hourOfDay] == [offDate hourOfDay] && [onDate minuteOfHour] == [offDate minuteOfHour]);
2082    }
2083   
2084    if (toggle)
2085    {
2086        [self toggleSpeedLimit: nil];
2087       
2088        [GrowlApplicationBridge notifyWithTitle: [fDefaults boolForKey: @"SpeedLimit"]
2089                ? NSLocalizedString(@"Speed Limit Auto Enabled", "Growl notification title")
2090                : NSLocalizedString(@"Speed Limit Auto Disabled", "Growl notification title")
2091            description: NSLocalizedString(@"Bandwidth settings changed", "Growl notification description")
2092            notificationName: GROWL_AUTO_SPEED_LIMIT iconData: nil priority: 0 isSticky: NO clickContext: nil];
2093    }
2094}
2095
2096- (void) setLimitGlobalEnabled: (id) sender
2097{
2098    [fPrefsController applySpeedSettings: nil];
2099}
2100
2101- (void) setQuickLimitGlobal: (id) sender
2102{
2103    [fDefaults setInteger: [[sender title] intValue] forKey: [sender menu] == fUploadMenu ? @"UploadLimit" : @"DownloadLimit"];
2104    [fDefaults setBool: YES forKey: [sender menu] == fUploadMenu ? @"CheckUpload" : @"CheckDownload"];
2105   
2106    [fPrefsController updateLimitFields];
2107    [fPrefsController applySpeedSettings: nil];
2108}
2109
2110- (void) setQuickRatioGlobal: (id) sender
2111{
2112    [fDefaults setBool: YES forKey: @"RatioCheck"];
2113    [fDefaults setFloat: [[sender title] floatValue] forKey: @"RatioLimit"];
2114   
2115    [fPrefsController updateRatioStopField];
2116}
2117
2118- (void) torrentStoppedForRatio: (NSNotification *) notification
2119{
2120    Torrent * torrent = [notification object];
2121   
2122    [self updateTorrentsInQueue];
2123    [fInfoController updateInfoStats];
2124    [fInfoController updateOptions];
2125   
2126    if ([fDefaults boolForKey: @"PlaySeedingSound"])
2127    {
2128        NSSound * sound;
2129        if ((sound = [NSSound soundNamed: [fDefaults stringForKey: @"SeedingSound"]]))
2130            [sound play];
2131    }
2132   
2133    NSDictionary * clickContext = [NSDictionary dictionaryWithObjectsAndKeys: GROWL_SEEDING_COMPLETE, @"Type",
2134                                    [torrent dataLocation], @"Location", nil];
2135    [GrowlApplicationBridge notifyWithTitle: NSLocalizedString(@"Seeding Complete", "Growl notification title")
2136                        description: [torrent name] notificationName: GROWL_SEEDING_COMPLETE
2137                        iconData: nil priority: 0 isSticky: NO clickContext: clickContext];
2138}
2139
2140-(void) watcher: (id<UKFileWatcher>) watcher receivedNotification: (NSString *) notification forPath: (NSString *) path
2141{
2142    if ([notification isEqualToString: UKFileWatcherWriteNotification])
2143    {
2144        if (![fDefaults boolForKey: @"AutoImport"] || ![fDefaults stringForKey: @"AutoImportDirectory"])
2145            return;
2146       
2147        if (fAutoImportTimer)
2148        {
2149            if ([fAutoImportTimer isValid])
2150                [fAutoImportTimer invalidate];
2151            [fAutoImportTimer release];
2152            fAutoImportTimer = nil;
2153        }
2154       
2155        //check again in 10 seconds in case torrent file wasn't complete
2156        fAutoImportTimer = [[NSTimer scheduledTimerWithTimeInterval: 10.0 target: self
2157            selector: @selector(checkAutoImportDirectory) userInfo: nil repeats: NO] retain];
2158       
2159        [self checkAutoImportDirectory];
2160    }
2161}
2162
2163- (void) changeAutoImport
2164{
2165    if (fAutoImportTimer)
2166    {
2167        if ([fAutoImportTimer isValid])
2168            [fAutoImportTimer invalidate];
2169        [fAutoImportTimer release];
2170        fAutoImportTimer = nil;
2171    }
2172   
2173    if (fAutoImportedNames)
2174        [fAutoImportedNames removeAllObjects];
2175    [self checkAutoImportDirectory];
2176}
2177
2178- (void) checkAutoImportDirectory
2179{
2180    NSString * path;
2181    if (![fDefaults boolForKey: @"AutoImport"] || !(path = [fDefaults stringForKey: @"AutoImportDirectory"]))
2182        return;
2183   
2184    path = [path stringByExpandingTildeInPath];
2185   
2186    NSArray * importedNames;
2187    if (!(importedNames = [[NSFileManager defaultManager] directoryContentsAtPath: path]))
2188        return;
2189   
2190    //only check files that have not been checked yet
2191    NSMutableArray * newNames = [importedNames mutableCopy];
2192   
2193    if (fAutoImportedNames)
2194        [newNames removeObjectsInArray: fAutoImportedNames];
2195    else
2196        fAutoImportedNames = [[NSMutableArray alloc] init];
2197    [fAutoImportedNames setArray: importedNames];
2198   
2199    NSString * file;
2200    int i;
2201    for (i = [newNames count] - 1; i >= 0; i--)
2202    {
2203        file = [newNames objectAtIndex: i];
2204        if ([[file pathExtension] caseInsensitiveCompare: @"torrent"] != NSOrderedSame)
2205            [newNames removeObjectAtIndex: i];
2206        else
2207            [newNames replaceObjectAtIndex: i withObject: [path stringByAppendingPathComponent: file]];
2208    }
2209   
2210    BOOL ask = [[fDefaults stringForKey: @"DownloadChoice"] isEqualToString: @"Ask"];
2211   
2212    NSEnumerator * enumerator = [newNames objectEnumerator];
2213    int canAdd, count;
2214    while ((file = [enumerator nextObject]))
2215    {
2216        canAdd = tr_torrentParse(fLib, [file UTF8String], NULL, NULL);
2217        if (canAdd == TR_OK)
2218        {
2219            if (!ask)
2220                count = [fTorrents count];
2221            [self openFiles: [NSArray arrayWithObject: file]];
2222           
2223            //check if torrent was opened
2224            if (!ask && [fTorrents count] > count)
2225                [GrowlApplicationBridge notifyWithTitle: NSLocalizedString(@"Torrent File Auto Added",
2226                    "Growl notification title") description: [file lastPathComponent]
2227                    notificationName: GROWL_AUTO_ADD iconData: nil priority: 0 isSticky: NO clickContext: nil];
2228        }
2229        else if (canAdd == TR_EINVALID)
2230            [fAutoImportedNames removeObject: [file lastPathComponent]];
2231        else;
2232    }
2233   
2234    [newNames release];
2235}
2236
2237- (void) beginCreateFile: (NSNotification *) notification
2238{
2239    if (![fDefaults boolForKey: @"AutoImport"])
2240        return;
2241   
2242    NSString * location = [notification object],
2243            * path = [fDefaults stringForKey: @"AutoImportDirectory"];
2244   
2245    if (location && path && [[[location stringByDeletingLastPathComponent] stringByExpandingTildeInPath]
2246                                    isEqualToString: [path stringByExpandingTildeInPath]])
2247        [fAutoImportedNames addObject: [location lastPathComponent]];
2248}
2249
2250- (int) numberOfRowsInTableView: (NSTableView *) tableview
2251{
2252    return [fDisplayedTorrents count];
2253}
2254
2255- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) tableColumn row: (int) row
2256{
2257    return nil;
2258}
2259
2260- (BOOL) tableView: (NSTableView *) tableView writeRowsWithIndexes: (NSIndexSet *) indexes toPasteboard: (NSPasteboard *) pasteboard
2261{
2262    //only allow reordering of rows if sorting by order
2263    if ([[fDefaults stringForKey: @"Sort"] isEqualToString: SORT_ORDER])
2264    {
2265        [pasteboard declareTypes: [NSArray arrayWithObject: TORRENT_TABLE_VIEW_DATA_TYPE] owner: self];
2266        [pasteboard setData: [NSKeyedArchiver archivedDataWithRootObject: indexes] forType: TORRENT_TABLE_VIEW_DATA_TYPE];
2267        return YES;
2268    }
2269    return NO;
2270}
2271
2272- (NSDragOperation) tableView: (NSTableView *) tableView validateDrop: (id <NSDraggingInfo>) info
2273    proposedRow: (int) row proposedDropOperation: (NSTableViewDropOperation) operation
2274{
2275    NSPasteboard * pasteboard = [info draggingPasteboard];
2276    if ([[pasteboard types] containsObject: TORRENT_TABLE_VIEW_DATA_TYPE])
2277    {
2278        [fTableView setDropRow: row dropOperation: NSTableViewDropAbove];
2279        return NSDragOperationGeneric;
2280    }
2281   
2282    return NSDragOperationNone;
2283}
2284
2285- (BOOL) tableView: (NSTableView *) t acceptDrop: (id <NSDraggingInfo>) info
2286    row: (int) newRow dropOperation: (NSTableViewDropOperation) operation
2287{
2288    NSPasteboard * pasteboard = [info draggingPasteboard];
2289    if ([[pasteboard types] containsObject: TORRENT_TABLE_VIEW_DATA_TYPE])
2290    {
2291        //remember selected rows if needed
2292        NSArray * selectedTorrents = nil;
2293        int numSelected = [fTableView numberOfSelectedRows];
2294        if (numSelected > 0 && numSelected < [fDisplayedTorrents count])
2295            selectedTorrents = [fDisplayedTorrents objectsAtIndexes: [fTableView selectedRowIndexes]];
2296   
2297        NSIndexSet * indexes = [NSKeyedUnarchiver unarchiveObjectWithData:
2298                                [pasteboard dataForType: TORRENT_TABLE_VIEW_DATA_TYPE]];
2299       
2300        //determine where to move them
2301        int i;
2302        for (i = [indexes firstIndex]; i < newRow && i != NSNotFound; i = [indexes indexGreaterThanIndex: i])
2303            newRow--;
2304       
2305        //reinsert into array
2306        int insertIndex = newRow > 0 ? [[fDisplayedTorrents objectAtIndex: newRow-1] orderValue] + 1 : 0;
2307       
2308        //get all torrents to reorder
2309        NSSortDescriptor * orderDescriptor = [[[NSSortDescriptor alloc] initWithKey:
2310                                                @"orderValue" ascending: YES] autorelease];
2311        NSArray * descriptors = [[NSArray alloc] initWithObjects: orderDescriptor, nil];
2312       
2313        NSMutableArray * sortedTorrents = [[fTorrents sortedArrayUsingDescriptors: descriptors] mutableCopy];
2314        [descriptors release];
2315       
2316        //remove objects to reinsert
2317        NSArray * movingTorrents = [[fDisplayedTorrents objectsAtIndexes: indexes] retain];
2318        [sortedTorrents removeObjectsInArray: movingTorrents];
2319       
2320        //insert objects at new location
2321        for (i = 0; i < [movingTorrents count]; i++)
2322            [sortedTorrents insertObject: [movingTorrents objectAtIndex: i] atIndex: insertIndex + i];
2323       
2324        [movingTorrents release];
2325       
2326        //redo order values
2327        i = 0;
2328        for (i = 0; i < [sortedTorrents count]; i++)
2329            [[sortedTorrents objectAtIndex: i] setOrderValue: i];
2330       
2331        [sortedTorrents release];
2332       
2333        [self applyFilter: nil];
2334       
2335        //set selected rows if needed
2336        if (selectedTorrents)
2337        {
2338            Torrent * torrent;
2339            NSEnumerator * enumerator = [selectedTorrents objectEnumerator];
2340            NSMutableIndexSet * indexSet = [[NSMutableIndexSet alloc] init];
2341            while ((torrent = [enumerator nextObject]))
2342                [indexSet addIndex: [fDisplayedTorrents indexOfObject: torrent]];
2343           
2344            [fTableView selectRowIndexes: indexSet byExtendingSelection: NO];
2345            [indexSet release];
2346        }
2347    }
2348   
2349    return YES;
2350}
2351
2352- (void) torrentTableViewSelectionDidChange: (NSNotification *) notification
2353{
2354    [fInfoController setInfoForTorrents: [fDisplayedTorrents objectsAtIndexes: [fTableView selectedRowIndexes]]];
2355}
2356
2357- (NSDragOperation) draggingEntered: (id <NSDraggingInfo>) info
2358{
2359    NSPasteboard * pasteboard = [info draggingPasteboard];
2360    if ([[pasteboard types] containsObject: NSFilenamesPboardType])
2361    {
2362        //check if any torrent files can be added
2363        NSArray * files = [pasteboard propertyListForType: NSFilenamesPboardType];
2364        NSEnumerator * enumerator = [files objectEnumerator];
2365        NSString * file;
2366        BOOL torrent = NO;
2367        int canAdd;
2368        while ((file = [enumerator nextObject]))
2369        {
2370            if ([[file pathExtension] caseInsensitiveCompare: @"torrent"] == NSOrderedSame)
2371            {
2372                switch (canAdd = tr_torrentParse(fLib, [file UTF8String], NULL, NULL))
2373                {
2374                    case TR_OK:
2375                        if (!fOverlayWindow)
2376                            fOverlayWindow = [[DragOverlayWindow alloc] initWithLib: fLib forWindow: fWindow];
2377                        [fOverlayWindow setTorrents: files];
2378                       
2379                        return NSDragOperationCopy;
2380                   
2381                    case TR_EDUPLICATE:
2382                        torrent = YES;
2383                }
2384            }
2385        }
2386       
2387        //create a torrent file if a single file
2388        if (!torrent && [files count] == 1)
2389        {
2390            if (!fOverlayWindow)
2391                fOverlayWindow = [[DragOverlayWindow alloc] initWithLib: fLib forWindow: fWindow];
2392            [fOverlayWindow setFile: [[files objectAtIndex: 0] lastPathComponent]];
2393           
2394            return NSDragOperationCopy;
2395        }
2396    }
2397    else if ([[pasteboard types] containsObject: NSURLPboardType])
2398    {
2399        if (!fOverlayWindow)
2400            fOverlayWindow = [[DragOverlayWindow alloc] initWithLib: fLib forWindow: fWindow];
2401        [fOverlayWindow setURL: [[NSURL URLFromPasteboard: pasteboard] relativeString]];
2402       
2403        return NSDragOperationCopy;
2404    }
2405    else;
2406   
2407    return NSDragOperationNone;
2408}
2409
2410- (void) draggingExited: (id <NSDraggingInfo>) info
2411{
2412    if (fOverlayWindow)
2413        [fOverlayWindow fadeOut];
2414}
2415
2416- (BOOL) performDragOperation: (id <NSDraggingInfo>) info
2417{
2418    if (fOverlayWindow)
2419        [fOverlayWindow fadeOut];
2420   
2421    NSPasteboard * pasteboard = [info draggingPasteboard];
2422    if ([[pasteboard types] containsObject: NSFilenamesPboardType])
2423    {
2424        BOOL torrent = NO, accept = YES;
2425       
2426        //create an array of files that can be opened
2427        NSMutableArray * filesToOpen = [[NSMutableArray alloc] init];
2428        NSArray * files = [pasteboard propertyListForType: NSFilenamesPboardType];
2429        NSEnumerator * enumerator = [files objectEnumerator];
2430        NSString * file;
2431        while ((file = [enumerator nextObject]))
2432        {
2433            if ([[file pathExtension] caseInsensitiveCompare: @"torrent"] == NSOrderedSame)
2434            {
2435                switch(tr_torrentParse(fLib, [file UTF8String], NULL, NULL))
2436                {
2437                    case TR_OK:
2438                        [filesToOpen addObject: file];
2439                        torrent = YES;
2440                        break;
2441                       
2442                    case TR_EDUPLICATE:
2443                        torrent = YES;
2444                }
2445            }
2446        }
2447       
2448        if ([filesToOpen count] > 0)
2449            [self application: NSApp openFiles: filesToOpen];
2450        else
2451        {
2452            if (!torrent && [files count] == 1)
2453                [CreatorWindowController createTorrentFile: fLib forFile: [files objectAtIndex: 0]];
2454            else
2455                accept = NO;
2456        }
2457        [filesToOpen release];
2458       
2459        return accept;
2460    }
2461    else if ([[pasteboard types] containsObject: NSURLPboardType])
2462    {
2463        NSURL * url;
2464        if ((url = [NSURL URLFromPasteboard: pasteboard]))
2465        {
2466            [self openURL: url];
2467            return YES;
2468        }
2469    }
2470    else;
2471   
2472    return NO;
2473}
2474
2475- (void) toggleSmallView: (id) sender
2476{
2477    BOOL makeSmall = ![fDefaults boolForKey: @"SmallView"];
2478    [fDefaults setBool: makeSmall forKey: @"SmallView"];
2479   
2480    [fTableView setRowHeight: makeSmall ? ROW_HEIGHT_SMALL : ROW_HEIGHT_REGULAR];
2481   
2482    //window min height
2483    NSSize contentMinSize = [fWindow contentMinSize],
2484            contentSize = [[fWindow contentView] frame].size;
2485    contentMinSize.height = contentSize.height - [fScrollView frame].size.height
2486                            + [fTableView rowHeight] + [fTableView intercellSpacing].height;
2487    [fWindow setContentMinSize: contentMinSize];
2488   
2489    //resize for larger min height if not set to auto size
2490    if (![fDefaults boolForKey: @"AutoSize"])
2491    {
2492        if (!makeSmall && contentSize.height < contentMinSize.height)
2493        {
2494            NSRect frame = [fWindow frame];
2495            float heightChange = contentMinSize.height - contentSize.height;
2496            frame.size.height += heightChange;
2497            frame.origin.y -= heightChange;
2498           
2499            [fWindow setFrame: frame display: YES];
2500            [fTableView reloadData];
2501        }
2502    }
2503    else
2504        [self setWindowSizeToFit];
2505}
2506
2507- (void) togglePiecesBar: (id) sender
2508{
2509    [fDefaults setBool: ![fDefaults boolForKey: @"PiecesBar"] forKey: @"PiecesBar"];
2510    [fTableView display];
2511}
2512
2513- (void) toggleAvailabilityBar: (id) sender
2514{
2515    [fDefaults setBool: ![fDefaults boolForKey: @"DisplayProgressBarAvailable"] forKey: @"DisplayProgressBarAvailable"];
2516    [fTableView display];
2517}
2518
2519- (void) toggleStatusBar: (id) sender
2520{
2521    [self showStatusBar: [fStatusBar isHidden] animate: YES];
2522    [fDefaults setBool: ![fStatusBar isHidden] forKey: @"StatusBar"];
2523}
2524
2525- (NSRect) windowFrameByAddingHeight: (float) height checkLimits: (BOOL) check
2526{
2527    //convert pixels to points
2528    NSRect windowFrame = [fWindow frame];
2529    NSSize windowSize = [fScrollView convertSize: windowFrame.size fromView: nil];
2530    windowSize.height += height;
2531   
2532    if (check)
2533    {
2534        NSSize minSize = [fScrollView convertSize: [fWindow minSize] fromView: nil];
2535       
2536        if (windowSize.height < minSize.height)
2537            windowSize.height = minSize.height;
2538        else
2539        {
2540            NSSize maxSize = [fScrollView convertSize: [[fWindow screen] visibleFrame].size fromView: nil];
2541            if ([fStatusBar isHidden])
2542                maxSize.height -= [fStatusBar frame].size.height;
2543            if ([fFilterBar isHidden])
2544                maxSize.height -= [fFilterBar frame].size.height;
2545            if (windowSize.height > maxSize.height)
2546                windowSize.height = maxSize.height;
2547        }
2548    }
2549
2550    //convert points to pixels
2551    windowSize = [fScrollView convertSize: windowSize toView: nil];
2552
2553    windowFrame.origin.y -= (windowSize.height - windowFrame.size.height);
2554    windowFrame.size.height = windowSize.height;
2555    return windowFrame;
2556}
2557
2558- (void) showStatusBar: (BOOL) show animate: (BOOL) animate
2559{
2560    if (show != [fStatusBar isHidden])
2561        return;
2562
2563    if (show)
2564        [fStatusBar setHidden: NO];
2565
2566    NSRect frame;
2567    float heightChange = [fStatusBar frame].size.height;
2568    if (!show)
2569        heightChange *= -1;
2570   
2571    //allow bar to show even if not enough room
2572    if (show && ![fDefaults boolForKey: @"AutoSize"])
2573    {
2574        frame = [self windowFrameByAddingHeight: heightChange checkLimits: NO];
2575        float change = [[fWindow screen] visibleFrame].size.height - frame.size.height;
2576        if (change < 0.0)
2577        {
2578            frame = [fWindow frame];
2579            frame.size.height += change;
2580            frame.origin.y -= change;
2581            [fWindow setFrame: frame display: NO animate: NO];
2582        }
2583    }
2584
2585    [self updateUI];
2586   
2587    //set views to not autoresize
2588    unsigned int statsMask = [fStatusBar autoresizingMask];
2589    unsigned int filterMask = [fFilterBar autoresizingMask];
2590    unsigned int scrollMask = [fScrollView autoresizingMask];
2591    [fStatusBar setAutoresizingMask: NSViewNotSizable];
2592    [fFilterBar setAutoresizingMask: NSViewNotSizable];
2593    [fScrollView setAutoresizingMask: NSViewNotSizable];
2594   
2595    frame = [self windowFrameByAddingHeight: heightChange checkLimits: NO];
2596    [fWindow setFrame: frame display: YES animate: animate];
2597   
2598    //re-enable autoresize
2599    [fStatusBar setAutoresizingMask: statsMask];
2600    [fFilterBar setAutoresizingMask: filterMask];
2601    [fScrollView setAutoresizingMask: scrollMask];
2602   
2603    //change min size
2604    NSSize minSize = [fWindow contentMinSize];
2605    minSize.height += heightChange;
2606    [fWindow setContentMinSize: minSize];
2607   
2608    if (!show)
2609        [fStatusBar setHidden: YES];
2610}
2611
2612- (void) toggleFilterBar: (id) sender
2613{
2614    //disable filtering when hiding
2615    if (![fFilterBar isHidden])
2616    {
2617        [fSearchFilterField setStringValue: @""];
2618        [self setFilter: fNoFilterButton];
2619    }
2620
2621    [self showFilterBar: [fFilterBar isHidden] animate: YES];
2622    [fDefaults setBool: ![fFilterBar isHidden] forKey: @"FilterBar"];
2623}
2624
2625- (void) showFilterBar: (BOOL) show animate: (BOOL) animate
2626{
2627    if (show != [fFilterBar isHidden])
2628        return;
2629
2630    if (show)
2631        [fFilterBar setHidden: NO];
2632
2633    NSRect frame;
2634    float heightChange = [fFilterBar frame].size.height;
2635    if (!show)
2636        heightChange *= -1;
2637   
2638    //allow bar to show even if not enough room
2639    if (show && ![fDefaults boolForKey: @"AutoSize"])
2640    {
2641        frame = [self windowFrameByAddingHeight: heightChange checkLimits: NO];
2642        float change = [[fWindow screen] visibleFrame].size.height - frame.size.height;
2643        if (change < 0.0)
2644        {
2645            frame = [fWindow frame];
2646            frame.size.height += change;
2647            frame.origin.y -= change;
2648            [fWindow setFrame: frame display: NO animate: NO];
2649        }
2650    }
2651
2652    //set views to not autoresize
2653    unsigned int filterMask = [fFilterBar autoresizingMask];
2654    unsigned int scrollMask = [fScrollView autoresizingMask];
2655    [fFilterBar setAutoresizingMask: NSViewNotSizable];
2656    [fScrollView setAutoresizingMask: NSViewNotSizable];
2657   
2658    frame = [self windowFrameByAddingHeight: heightChange checkLimits: NO];
2659    [fWindow setFrame: frame display: YES animate: animate];
2660   
2661    //re-enable autoresize
2662    [fFilterBar setAutoresizingMask: filterMask];
2663    [fScrollView setAutoresizingMask: scrollMask];
2664   
2665    //change min size
2666    NSSize minSize = [fWindow contentMinSize];
2667    minSize.height += heightChange;
2668    [fWindow setContentMinSize: minSize];
2669   
2670    if (!show)
2671    {
2672        [fFilterBar setHidden: YES];
2673        [fWindow makeFirstResponder: fTableView];
2674    }
2675}
2676
2677- (void) doNothing: (id) sender {}
2678
2679- (ButtonToolbarItem *) standardToolbarButtonWithIdentifier: (NSString *) ident
2680{
2681    ButtonToolbarItem * item = [[ButtonToolbarItem alloc] initWithItemIdentifier: ident];
2682   
2683    NSButton * button = [[NSButton alloc] initWithFrame: NSZeroRect];
2684    [button setBezelStyle: NSTexturedRoundedBezelStyle];
2685    [button setStringValue: @""];
2686   
2687    [item setView: button];
2688    [button release];
2689   
2690    NSSize buttonSize = NSMakeSize(36.0, 25.0);
2691    [item setMinSize: buttonSize];
2692    [item setMaxSize: buttonSize];
2693   
2694    return [item autorelease];
2695}
2696
2697- (NSToolbarItem *) toolbar: (NSToolbar *) toolbar itemForItemIdentifier: (NSString *) ident willBeInsertedIntoToolbar: (BOOL) flag
2698{
2699    if ([ident isEqualToString: TOOLBAR_CREATE])
2700    {
2701        ButtonToolbarItem * item = [self standardToolbarButtonWithIdentifier: ident];
2702       
2703        [item setLabel: NSLocalizedString(@"Create", "Create toolbar item -> label")];
2704        [item setPaletteLabel: NSLocalizedString(@"Create Torrent File", "Create toolbar item -> palette label")];
2705        [item setToolTip: NSLocalizedString(@"Create torrent file", "Create toolbar item -> tooltip")];
2706        [item setImage: [NSImage imageNamed: @"Create.png"]];
2707        [item setTarget: self];
2708        [item setAction: @selector(createFile:)];
2709        [item setAutovalidates: NO];
2710       
2711        return item;
2712    }
2713    else if ([ident isEqualToString: TOOLBAR_OPEN])
2714    {
2715        ButtonToolbarItem * item = [self standardToolbarButtonWithIdentifier: ident];
2716       
2717        [item setLabel: NSLocalizedString(@"Open", "Open toolbar item -> label")];
2718        [item setPaletteLabel: NSLocalizedString(@"Open Torrent Files", "Open toolbar item -> palette label")];
2719        [item setToolTip: NSLocalizedString(@"Open torrent files", "Open toolbar item -> tooltip")];
2720        [item setImage: [NSImage imageNamed: @"Open.png"]];
2721        [item setTarget: self];
2722        [item setAction: @selector(openShowSheet:)];
2723        [item setAutovalidates: NO];
2724       
2725        return item;
2726    }
2727    else if ([ident isEqualToString: TOOLBAR_REMOVE])
2728    {
2729        ButtonToolbarItem * item = [self standardToolbarButtonWithIdentifier: ident];
2730       
2731        [item setLabel: NSLocalizedString(@"Remove", "Remove toolbar item -> label")];
2732        [item setPaletteLabel: NSLocalizedString(@"Remove Selected", "Remove toolbar item -> palette label")];
2733        [item setToolTip: NSLocalizedString(@"Remove selected transfers", "Remove toolbar item -> tooltip")];
2734        [item setImage: [NSImage imageNamed: @"Remove.png"]];
2735        [item setTarget: self];
2736        [item setAction: @selector(removeNoDelete:)];
2737       
2738        return item;
2739    }
2740    else if ([ident isEqualToString: TOOLBAR_INFO])
2741    {
2742        ButtonToolbarItem * item = [self standardToolbarButtonWithIdentifier: ident];
2743       
2744        [item setLabel: NSLocalizedString(@"Inspector", "Inspector toolbar item -> label")];
2745        [item setPaletteLabel: NSLocalizedString(@"Toggle Inspector", "Inspector toolbar item -> palette label")];
2746        [item setToolTip: NSLocalizedString(@"Toggle the torrent inspector", "Inspector toolbar item -> tooltip")];
2747        [item setImage: [NSImage imageNamed: @"Info.png"]];
2748        [item setTarget: self];
2749        [item setAction: @selector(showInfo:)];
2750       
2751        return item;
2752    }
2753    else if ([ident isEqualToString: TOOLBAR_PAUSE_RESUME_ALL])
2754    {
2755        GroupToolbarItem * groupItem = [[GroupToolbarItem alloc] initWithItemIdentifier: ident];
2756       
2757        NSSegmentedControl * segmentedControl = [[NSSegmentedControl alloc] initWithFrame: NSZeroRect];
2758        [segmentedControl setCell: [[[ToolbarSegmentedCell alloc] init] autorelease]];
2759        [groupItem setView: segmentedControl];
2760        NSSegmentedCell * segmentedCell = (NSSegmentedCell *)[segmentedControl cell];
2761       
2762        [segmentedControl setSegmentCount: 2];
2763        [segmentedCell setTrackingMode: NSSegmentSwitchTrackingMomentary];
2764       
2765        [groupItem setLabel: NSLocalizedString(@"Apply All", "All toolbar item -> label")];
2766        [groupItem setPaletteLabel: NSLocalizedString(@"Pause / Resume All", "All toolbar item -> palette label")];
2767        [groupItem setTarget: self];
2768        [groupItem setAction: @selector(allToolbarClicked:)];
2769       
2770        [groupItem setIdentifiers: [NSArray arrayWithObjects: TOOLBAR_PAUSE_ALL, TOOLBAR_RESUME_ALL, nil]];
2771       
2772        [segmentedCell setTag: TOOLBAR_PAUSE_TAG forSegment: TOOLBAR_PAUSE_TAG];
2773        [segmentedControl setImage: [NSImage imageNamed: @"PauseAll.png"] forSegment: TOOLBAR_PAUSE_TAG];
2774        [segmentedCell setToolTip: NSLocalizedString(@"Pause all transfers",
2775                                    "All toolbar item -> tooltip") forSegment: TOOLBAR_PAUSE_TAG];
2776       
2777        [segmentedCell setTag: TOOLBAR_RESUME_TAG forSegment: TOOLBAR_RESUME_TAG];
2778        [segmentedControl setImage: [NSImage imageNamed: @"ResumeAll.png"] forSegment: TOOLBAR_RESUME_TAG];
2779        [segmentedCell setToolTip: NSLocalizedString(@"Resume all transfers",
2780                                    "All toolbar item -> tooltip") forSegment: TOOLBAR_RESUME_TAG];
2781       
2782        [groupItem createMenu: [NSArray arrayWithObjects: NSLocalizedString(@"Pause All", "All toolbar item -> label"),
2783                                        NSLocalizedString(@"Resume All", "All toolbar item -> label"), nil]];
2784       
2785        NSSize groupSize = NSMakeSize(72.0, 25.0);
2786        [groupItem setMinSize: groupSize];
2787        [groupItem setMaxSize: groupSize];
2788       
2789        [segmentedControl release];
2790        return [groupItem autorelease];
2791    }
2792    else if ([ident isEqualToString: TOOLBAR_PAUSE_RESUME_SELECTED])
2793    {
2794        GroupToolbarItem * groupItem = [[GroupToolbarItem alloc] initWithItemIdentifier: ident];
2795       
2796        NSSegmentedControl * segmentedControl = [[NSSegmentedControl alloc] initWithFrame: NSZeroRect];
2797        [segmentedControl setCell: [[[ToolbarSegmentedCell alloc] init] autorelease]];
2798        [groupItem setView: segmentedControl];
2799        NSSegmentedCell * segmentedCell = (NSSegmentedCell *)[segmentedControl cell];
2800       
2801        [segmentedControl setSegmentCount: 2];
2802        [segmentedCell setTrackingMode: NSSegmentSwitchTrackingMomentary];
2803       
2804        NSSize groupSize = NSMakeSize(72.0, 25.0);
2805        [groupItem setMinSize: groupSize];
2806        [groupItem setMaxSize: groupSize];
2807       
2808        [groupItem setLabel: NSLocalizedString(@"Apply Selected", "Selected toolbar item -> label")];
2809        [groupItem setPaletteLabel: NSLocalizedString(@"Pause / Resume Selected", "Selected toolbar item -> palette label")];
2810        [groupItem setTarget: self];
2811        [groupItem setAction: @selector(selectedToolbarClicked:)];
2812       
2813        [groupItem setIdentifiers: [NSArray arrayWithObjects: TOOLBAR_PAUSE_SELECTED, TOOLBAR_RESUME_SELECTED, nil]];
2814       
2815        [segmentedCell setTag: TOOLBAR_PAUSE_TAG forSegment: TOOLBAR_PAUSE_TAG];
2816        [segmentedControl setImage: [NSImage imageNamed: @"PauseSelected.png"] forSegment: TOOLBAR_PAUSE_TAG];
2817        [segmentedCell setToolTip: NSLocalizedString(@"Pause selected transfers",
2818                                    "Selected toolbar item -> tooltip") forSegment: TOOLBAR_PAUSE_TAG];
2819       
2820        [segmentedCell setTag: TOOLBAR_RESUME_TAG forSegment: TOOLBAR_RESUME_TAG];
2821        [segmentedControl setImage: [NSImage imageNamed: @"ResumeSelected.png"] forSegment: TOOLBAR_RESUME_TAG];
2822        [segmentedCell setToolTip: NSLocalizedString(@"Resume selected transfers",
2823                                    "Selected toolbar item -> tooltip") forSegment: TOOLBAR_RESUME_TAG];
2824       
2825        [groupItem createMenu: [NSArray arrayWithObjects: NSLocalizedString(@"Pause Selected", "Selected toolbar item -> label"),
2826                                        NSLocalizedString(@"Resume Selected", "Selected toolbar item -> label"), nil]];
2827       
2828        [segmentedControl release];
2829        return [groupItem autorelease];
2830    }
2831    else if ([ident isEqualToString: TOOLBAR_FILTER])
2832    {
2833        ButtonToolbarItem * item = [self standardToolbarButtonWithIdentifier: ident];
2834       
2835        [item setLabel: NSLocalizedString(@"Filter", "Filter toolbar item -> label")];
2836        [item setPaletteLabel: NSLocalizedString(@"Toggle Filter", "Filter toolbar item -> palette label")];
2837        [item setToolTip: NSLocalizedString(@"Toggle the filter bar", "Filter toolbar item -> tooltip")];
2838        [item setImage: [NSImage imageNamed: @"Filter.png"]];
2839        [item setTarget: self];
2840        [item setAction: @selector(toggleFilterBar:)];
2841       
2842        return item;
2843    }
2844    else
2845        return nil;
2846}
2847
2848- (void) allToolbarClicked: (id) sender
2849{
2850    int tagValue = [sender isKindOfClass: [NSSegmentedControl class]]
2851                    ? [(NSSegmentedCell *)[sender cell] tagForSegment: [sender selectedSegment]] : [sender tag];
2852    switch (tagValue)
2853    {
2854        case TOOLBAR_PAUSE_TAG:
2855            [self stopAllTorrents: sender];
2856            break;
2857        case TOOLBAR_RESUME_TAG:
2858            [self resumeAllTorrents: sender];
2859            break;
2860    }
2861}
2862
2863- (void) selectedToolbarClicked: (id) sender
2864{
2865    int tagValue = [sender isKindOfClass: [NSSegmentedControl class]]
2866                    ? [(NSSegmentedCell *)[sender cell] tagForSegment: [sender selectedSegment]] : [sender tag];
2867    switch (tagValue)
2868    {
2869        case TOOLBAR_PAUSE_TAG:
2870            [self stopSelectedTorrents: sender];
2871            break;
2872        case TOOLBAR_RESUME_TAG:
2873            [self resumeSelectedTorrents: sender];
2874            break;
2875    }
2876}
2877
2878- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
2879{
2880    return [NSArray arrayWithObjects:
2881            TOOLBAR_CREATE, TOOLBAR_OPEN, TOOLBAR_REMOVE,
2882            TOOLBAR_PAUSE_RESUME_SELECTED, TOOLBAR_PAUSE_RESUME_ALL,
2883            TOOLBAR_FILTER, TOOLBAR_INFO,
2884            NSToolbarSeparatorItemIdentifier,
2885            NSToolbarSpaceItemIdentifier,
2886            NSToolbarFlexibleSpaceItemIdentifier,
2887            NSToolbarCustomizeToolbarItemIdentifier, nil];
2888}
2889
2890- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
2891{
2892    return [NSArray arrayWithObjects:
2893            TOOLBAR_CREATE, TOOLBAR_OPEN, TOOLBAR_REMOVE,
2894            NSToolbarSeparatorItemIdentifier,
2895            TOOLBAR_PAUSE_RESUME_ALL,
2896            NSToolbarFlexibleSpaceItemIdentifier,
2897            TOOLBAR_FILTER, TOOLBAR_INFO, nil];
2898}
2899
2900- (BOOL) validateToolbarItem: (NSToolbarItem *) toolbarItem
2901{
2902    NSString * ident = [toolbarItem itemIdentifier];
2903   
2904    //enable remove item
2905    if ([ident isEqualToString: TOOLBAR_REMOVE])
2906        return [fTableView numberOfSelectedRows] > 0;
2907
2908    //enable pause all item
2909    if ([ident isEqualToString: TOOLBAR_PAUSE_ALL])
2910    {
2911        Torrent * torrent;
2912        NSEnumerator * enumerator = [fTorrents objectEnumerator];
2913        while ((torrent = [enumerator nextObject]))
2914            if ([torrent isActive] || [torrent waitingToStart])
2915                return YES;
2916        return NO;
2917    }
2918
2919    //enable resume all item
2920    if ([ident isEqualToString: TOOLBAR_RESUME_ALL])
2921    {
2922        Torrent * torrent;
2923        NSEnumerator * enumerator = [fTorrents objectEnumerator];
2924        while ((torrent = [enumerator nextObject]))
2925            if (![torrent isActive] && ![torrent waitingToStart])
2926                return YES;
2927        return NO;
2928    }
2929
2930    //enable pause item
2931    if ([ident isEqualToString: TOOLBAR_PAUSE_SELECTED])
2932    {
2933        NSEnumerator * enumerator = [[fDisplayedTorrents objectsAtIndexes: [fTableView selectedRowIndexes]] objectEnumerator];
2934        Torrent * torrent;
2935        while ((torrent = [enumerator nextObject]))
2936            if ([torrent isActive] || [torrent waitingToStart])
2937                return YES;
2938        return NO;
2939    }
2940   
2941    //enable resume item
2942    if ([ident isEqualToString: TOOLBAR_RESUME_SELECTED])
2943    {
2944        NSEnumerator * enumerator = [[fDisplayedTorrents objectsAtIndexes: [fTableView selectedRowIndexes]] objectEnumerator];
2945        Torrent * torrent;
2946        while ((torrent = [enumerator nextObject]))
2947            if (![torrent isActive] && ![torrent waitingToStart])
2948                return YES;
2949        return NO;
2950    }
2951   
2952    //set info image
2953    if ([ident isEqualToString: TOOLBAR_INFO])
2954    {
2955        [toolbarItem setImage: [[fInfoController window] isVisible] ? [NSImage imageNamed: @"InfoBlue"]
2956                                                                    : [NSImage imageNamed: @"Info"]];
2957        return YES;
2958    }
2959   
2960    //set filter image
2961    if ([ident isEqualToString: TOOLBAR_FILTER])
2962    {
2963        [toolbarItem setImage: ![fFilterBar isHidden] ? [NSImage imageNamed: @"FilterBlue"] : [NSImage imageNamed: @"Filter"]];
2964        return YES;
2965    }
2966
2967    return YES;
2968}
2969
2970- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
2971{
2972    SEL action = [menuItem action];
2973   
2974    if (action == @selector(toggleSpeedLimit:))
2975    {
2976        [menuItem setState: [fDefaults boolForKey: @"SpeedLimit"] ? NSOnState : NSOffState];
2977        return YES;
2978    }
2979   
2980    //only enable some items if it is in a context menu or the window is useable
2981    BOOL canUseTable = [fWindow isKeyWindow] || [[menuItem menu] supermenu] != [NSApp mainMenu];
2982
2983    //enable open items
2984    if (action == @selector(openShowSheet:) || action == @selector(openURLShowSheet:))
2985        return [fWindow attachedSheet] == nil;
2986   
2987    //enable sort options
2988    if (action == @selector(setSort:))
2989    {
2990        NSString * sortType;
2991        switch ([menuItem tag])
2992        {
2993            case SORT_ORDER_TAG:
2994                sortType = SORT_ORDER;
2995                break;
2996            case SORT_DATE_TAG:
2997                sortType = SORT_DATE;
2998                break;
2999            case SORT_NAME_TAG:
3000                sortType = SORT_NAME;
3001                break;
3002            case SORT_PROGRESS_TAG:
3003                sortType = SORT_PROGRESS;
3004                break;
3005            case SORT_STATE_TAG:
3006                sortType = SORT_STATE;
3007                break;
3008            case SORT_TRACKER_TAG:
3009                sortType = SORT_TRACKER;
3010                break;
3011            case SORT_ACTIVITY_TAG:
3012                sortType = SORT_ACTIVITY;
3013                break;
3014            default:
3015                sortType = @"";
3016        }
3017       
3018        [menuItem setState: [sortType isEqualToString: [fDefaults stringForKey: @"Sort"]] ? NSOnState : NSOffState];
3019        return [fWindow isVisible];
3020    }
3021   
3022    //enable sort options
3023    if (action == @selector(setStatusLabel:))
3024    {
3025        NSString * statusLabel;
3026        switch ([menuItem tag])
3027        {
3028            case STATUS_RATIO_TOTAL_TAG:
3029                statusLabel = STATUS_RATIO_TOTAL;
3030                break;
3031            case STATUS_RATIO_SESSION_TAG:
3032                statusLabel = STATUS_RATIO_SESSION;
3033                break;
3034            case STATUS_TRANSFER_TOTAL_TAG:
3035                statusLabel = STATUS_TRANSFER_TOTAL;
3036                break;
3037            case STATUS_TRANSFER_SESSION_TAG:
3038                statusLabel = STATUS_TRANSFER_SESSION;
3039                break;
3040            default:
3041                statusLabel = @"";;
3042        }
3043       
3044        [menuItem setState: [statusLabel isEqualToString: [fDefaults stringForKey: @"StatusLabel"]] ? NSOnState : NSOffState];
3045        return YES;
3046    }
3047   
3048    if (action == @selector(toggleSmallView:))
3049    {
3050        [menuItem setState: [fDefaults boolForKey: @"SmallView"] ? NSOnState : NSOffState];
3051        return [fWindow isVisible];
3052    }
3053   
3054    if (action == @selector(togglePiecesBar:))
3055    {
3056        [menuItem setState: [fDefaults boolForKey: @"PiecesBar"] ? NSOnState : NSOffState];
3057        return [fWindow isVisible];
3058    }
3059   
3060    if (action == @selector(toggleAvailabilityBar:))
3061    {
3062        [menuItem setState: [fDefaults boolForKey: @"DisplayProgressBarAvailable"] ? NSOnState : NSOffState];
3063        return [fWindow isVisible];
3064    }
3065
3066    //enable show info
3067    if (action == @selector(showInfo:))
3068    {
3069        NSString * title = [[fInfoController window] isVisible] ? NSLocalizedString(@"Hide Inspector", "View menu -> Inspector")
3070                            : NSLocalizedString(@"Show Inspector", "View menu -> Inspector");
3071        [menuItem setTitle: title];
3072
3073        return YES;
3074    }
3075   
3076    //enable prev/next inspector tab
3077    if (action == @selector(setInfoTab:))
3078        return [[fInfoController window] isVisible];
3079   
3080    //enable toggle status bar
3081    if (action == @selector(toggleStatusBar:))
3082    {
3083        NSString * title = [fStatusBar isHidden] ? NSLocalizedString(@"Show Status Bar", "View menu -> Status Bar")
3084                            : NSLocalizedString(@"Hide Status Bar", "View menu -> Status Bar");
3085        [menuItem setTitle: title];
3086
3087        return [fWindow isVisible];
3088    }
3089   
3090    //enable toggle filter bar
3091    if (action == @selector(toggleFilterBar:))
3092    {
3093        NSString * title = [fFilterBar isHidden] ? NSLocalizedString(@"Show Filter Bar", "View menu -> Filter Bar")
3094                            : NSLocalizedString(@"Hide Filter Bar", "View menu -> Filter Bar");
3095        [menuItem setTitle: title];
3096
3097        return [fWindow isVisible];
3098    }
3099   
3100    //enable prev/next filter button
3101    if (action == @selector(switchFilter:))
3102        return [fWindow isVisible] && ![fFilterBar isHidden];
3103
3104    //enable reveal in finder
3105    if (action == @selector(revealFile:))
3106        return canUseTable && [fTableView numberOfSelectedRows] > 0;
3107
3108    //enable remove items
3109    if (action == @selector(removeNoDelete:) || action == @selector(removeDeleteData:)
3110        || action == @selector(removeDeleteTorrent:) || action == @selector(removeDeleteDataAndTorrent:))
3111    {
3112        BOOL warning = NO,
3113            onlyDownloading = [fDefaults boolForKey: @"CheckRemoveDownloading"],
3114            canDelete = action != @selector(removeDeleteTorrent:) && action != @selector(removeDeleteDataAndTorrent:);
3115       
3116        NSEnumerator * enumerator = [[fDisplayedTorrents objectsAtIndexes: [fTableView selectedRowIndexes]] objectEnumerator];
3117        Torrent * torrent;
3118        while ((torrent = [enumerator nextObject]))
3119        {
3120            if (!warning && [torrent isActive])
3121            {
3122                warning = onlyDownloading ? ![torrent isSeeding] : YES;
3123                if (warning && canDelete)
3124                    break;
3125            }
3126            if (!canDelete && [torrent publicTorrent])
3127            {
3128                canDelete = YES;
3129                if (warning)
3130                    break;
3131            }
3132        }
3133   
3134        //append or remove ellipsis when needed
3135        NSString * title = [menuItem title], * ellipsis = [NSString ellipsis];
3136        if (warning && [fDefaults boolForKey: @"CheckRemove"])
3137        {
3138            if (![title hasSuffix: ellipsis])
3139                [menuItem setTitle: [title stringByAppendingEllipsis]];
3140        }
3141        else
3142        {
3143            if ([title hasSuffix: ellipsis])
3144                [menuItem setTitle: [title substringToIndex: [title rangeOfString: ellipsis].location]];
3145        }
3146       
3147        return canUseTable && canDelete && [fTableView numberOfSelectedRows] > 0;
3148    }
3149
3150    //enable pause all item
3151    if (action == @selector(stopAllTorrents:))
3152    {
3153        Torrent * torrent;
3154        NSEnumerator * enumerator = [fTorrents objectEnumerator];
3155        while ((torrent = [enumerator nextObject]))
3156            if ([torrent isActive] || [torrent waitingToStart])
3157                return YES;
3158        return NO;
3159    }
3160   
3161    //enable resume all item
3162    if (action == @selector(resumeAllTorrents:))
3163    {
3164        Torrent * torrent;
3165        NSEnumerator * enumerator = [fTorrents objectEnumerator];
3166        while ((torrent = [enumerator nextObject]))
3167            if (![torrent isActive] && ![torrent waitingToStart])
3168                return YES;
3169        return NO;
3170    }
3171   
3172    //enable resume all waiting item
3173    if (action == @selector(resumeWaitingTorrents:))
3174    {
3175        if (![fDefaults boolForKey: @"Queue"] && ![fDefaults boolForKey: @"QueueSeed"])
3176            return NO;
3177   
3178        Torrent * torrent;
3179        NSEnumerator * enumerator = [fTorrents objectEnumerator];
3180        while ((torrent = [enumerator nextObject]))
3181            if (![torrent isActive] && [torrent waitingToStart])
3182                return YES;
3183        return NO;
3184    }
3185   
3186    //enable resume selected waiting item
3187    if (action == @selector(resumeSelectedTorrentsNoWait:))
3188    {
3189        if (![fDefaults boolForKey: @"Queue"] && ![fDefaults boolForKey: @"QueueSeed"])
3190            return NO;
3191   
3192        NSEnumerator * enumerator = [[fDisplayedTorrents objectsAtIndexes: [fTableView selectedRowIndexes]] objectEnumerator];
3193        Torrent * torrent;
3194        while ((torrent = [enumerator nextObject]))
3195            if (![torrent isActive])
3196                return YES;
3197        return NO;
3198    }
3199
3200    //enable pause item
3201    if (action == @selector(stopSelectedTorrents:))
3202    {
3203        if (!canUseTable)
3204            return NO;
3205   
3206        NSEnumerator * enumerator = [[fDisplayedTorrents objectsAtIndexes: [fTableView selectedRowIndexes]] objectEnumerator];
3207        Torrent * torrent;
3208        while ((torrent = [enumerator nextObject]))
3209            if ([torrent isActive] || [torrent waitingToStart])
3210                return YES;
3211        return NO;
3212    }
3213   
3214    //enable resume item
3215    if (action == @selector(resumeSelectedTorrents:))
3216    {
3217        if (!canUseTable)
3218            return NO;
3219   
3220        NSEnumerator * enumerator = [[fDisplayedTorrents objectsAtIndexes: [fTableView selectedRowIndexes]] objectEnumerator];
3221        Torrent * torrent;
3222        while ((torrent = [enumerator nextObject]))
3223            if (![torrent isActive] && ![torrent waitingToStart])
3224                return YES;
3225        return NO;
3226    }
3227   
3228    //enable manual announce item
3229    if (action == @selector(announceSelectedTorrents:))
3230    {
3231        if (!canUseTable)
3232            return NO;
3233       
3234        NSEnumerator * enumerator = [[fDisplayedTorrents objectsAtIndexes: [fTableView selectedRowIndexes]] objectEnumerator];
3235        Torrent * torrent;
3236        while ((torrent = [enumerator nextObject]))
3237            if ([torrent canManualAnnounce])
3238                return YES;
3239        return NO;
3240    }
3241   
3242    //enable reset cache item
3243    if (action == @selector(resetCacheForSelectedTorrents:))
3244        return canUseTable && [fTableView numberOfSelectedRows] > 0;
3245   
3246    //enable move torrent file item
3247    if (action == @selector(moveDataFiles:))
3248        return canUseTable && [fTableView numberOfSelectedRows] > 0;
3249   
3250    //enable copy torrent file item
3251    if (action == @selector(copyTorrentFiles:))
3252        return canUseTable && [fTableView numberOfSelectedRows] > 0;
3253   
3254    //enable reverse sort item
3255    if (action == @selector(setSortReverse:))
3256    {
3257        [menuItem setState: [fDefaults boolForKey: @"SortReverse"] ? NSOnState : NSOffState];
3258        return ![[fDefaults stringForKey: @"Sort"] isEqualToString: SORT_ORDER];
3259    }
3260   
3261    //check proper filter search item
3262    if (action == @selector(setFilterSearchType:))
3263    {
3264        NSString * filterType = [fDefaults stringForKey: @"FilterSearchType"];
3265       
3266        BOOL state;
3267        if ([menuItem tag] == FILTER_TYPE_TAG_TRACKER)
3268            state = [filterType isEqualToString: FILTER_TYPE_TRACKER];
3269        else
3270            state = [filterType isEqualToString: FILTER_TYPE_NAME];
3271       
3272        [menuItem setState: state ? NSOnState : NSOffState];
3273        return YES;
3274    }
3275   
3276    return YES;
3277}
3278
3279- (void) sleepCallBack: (natural_t) messageType argument: (void *) messageArgument
3280{
3281    NSEnumerator * enumerator;
3282    Torrent * torrent;
3283    BOOL allowSleep;
3284
3285    switch (messageType)
3286    {
3287        case kIOMessageSystemWillSleep:
3288            //close all connections before going to sleep and remember we should resume when we wake up
3289            [fTorrents makeObjectsPerformSelector: @selector(sleep)];
3290
3291            //wait for running transfers to stop (5 second timeout)
3292            NSDate * start = [NSDate date];
3293            BOOL timeUp = NO;
3294           
3295            enumerator = [fTorrents objectEnumerator];
3296            while (!timeUp && (torrent = [enumerator nextObject]))
3297                while ([torrent isActive] && !(timeUp = [start timeIntervalSinceNow] < -5.0))
3298                {
3299                    usleep(100000);
3300                    [torrent update];
3301                }
3302
3303            IOAllowPowerChange(fRootPort, (long) messageArgument);
3304            break;
3305
3306        case kIOMessageCanSystemSleep:
3307            allowSleep = YES;
3308            if ([fDefaults boolForKey: @"SleepPrevent"])
3309            {
3310                //prevent idle sleep unless no torrents are active
3311                enumerator = [fTorrents objectEnumerator];
3312                while ((torrent = [enumerator nextObject]))
3313                    if ([torrent isActive] && ![torrent isStalled] && ![torrent isError])
3314                    {
3315                        allowSleep = NO;
3316                        break;
3317                    }
3318            }
3319
3320            if (allowSleep)
3321                IOAllowPowerChange(fRootPort, (long) messageArgument);
3322            else
3323                IOCancelPowerChange(fRootPort, (long) messageArgument);
3324            break;
3325
3326        case kIOMessageSystemHasPoweredOn:
3327            //resume sleeping transfers after we wake up
3328            [fTorrents makeObjectsPerformSelector: @selector(wakeUp)];
3329            [self autoSpeedLimitChange: nil];
3330            break;
3331    }
3332}
3333
3334- (NSMenu *) applicationDockMenu: (NSApplication *) sender
3335{
3336    int seeding = 0, downloading = 0;
3337    NSEnumerator * enumerator = [fTorrents objectEnumerator];
3338    Torrent * torrent;
3339    while ((torrent = [enumerator nextObject]))
3340    {
3341        if ([torrent isSeeding])
3342            seeding++;
3343        else if ([torrent isActive])
3344            downloading++;
3345        else;
3346    }
3347   
3348    NSMenuItem * seedingItem = [fDockMenu itemWithTag: DOCK_SEEDING_TAG],
3349            * downloadingItem = [fDockMenu itemWithTag: DOCK_DOWNLOADING_TAG];
3350   
3351    BOOL hasSeparator = seedingItem || downloadingItem;
3352   
3353    if (seeding > 0)
3354    {
3355        NSString * title = [NSString stringWithFormat: NSLocalizedString(@"%d Seeding",
3356                                                        "Dock item - Seeding"), seeding];
3357        if (!seedingItem)
3358        {
3359            seedingItem = [[[NSMenuItem alloc] initWithTitle: title action: nil keyEquivalent: @""] autorelease];
3360            [seedingItem setTag: DOCK_SEEDING_TAG];
3361            [fDockMenu insertItem: seedingItem atIndex: 0];
3362        }
3363        else
3364            [seedingItem setTitle: title];
3365    }
3366    else
3367    {
3368        if (seedingItem)
3369            [fDockMenu removeItem: seedingItem];
3370    }
3371   
3372    if (downloading > 0)
3373    {
3374        NSString * title = [NSString stringWithFormat: NSLocalizedString(@"%d Downloading",
3375                                                        "Dock item - Downloading"), downloading];
3376        if (!downloadingItem)
3377        {
3378            downloadingItem = [[[NSMenuItem alloc] initWithTitle: title action: nil keyEquivalent: @""] autorelease];
3379            [downloadingItem setTag: DOCK_DOWNLOADING_TAG];
3380            [fDockMenu insertItem: downloadingItem atIndex: seeding > 0 ? 1 : 0];
3381        }
3382        else
3383            [downloadingItem setTitle: title];
3384    }
3385    else
3386    {
3387        if (downloadingItem)
3388            [fDockMenu removeItem: downloadingItem];
3389    }
3390   
3391    if (seeding > 0 || downloading > 0)
3392    {
3393        if (!hasSeparator)
3394            [fDockMenu insertItem: [NSMenuItem separatorItem] atIndex: seeding > 0 && downloading > 0 ? 2 : 1];
3395    }
3396    else
3397    {
3398        if (hasSeparator)
3399            [fDockMenu removeItemAtIndex: 0];
3400    }
3401   
3402    return fDockMenu;
3403}
3404
3405- (NSRect) windowWillUseStandardFrame: (NSWindow *) window defaultFrame: (NSRect) defaultFrame
3406{
3407    //if auto size is enabled, the current frame shouldn't need to change
3408    NSRect frame = [fDefaults boolForKey: @"AutoSize"] ? [window frame] : [self sizedWindowFrame];
3409   
3410    frame.size.width = [fDefaults boolForKey: @"SmallView"] ? [fWindow minSize].width : WINDOW_REGULAR_WIDTH;
3411    return frame;
3412}
3413
3414- (void) setWindowSizeToFit
3415{
3416    if ([fDefaults boolForKey: @"AutoSize"])
3417    {
3418        [fScrollView setHasVerticalScroller: NO];
3419        [fWindow setFrame: [self sizedWindowFrame] display: YES animate: YES];
3420        [fScrollView setHasVerticalScroller: YES];
3421    }
3422}
3423
3424- (NSRect) sizedWindowFrame
3425{
3426    float heightChange = [fDisplayedTorrents count] * ([fTableView rowHeight] +
3427            [fTableView intercellSpacing].height) - [fScrollView frame].size.height;
3428    return [self windowFrameByAddingHeight: heightChange checkLimits: YES];
3429}
3430
3431- (void) showMainWindow: (id) sender
3432{
3433    [fWindow makeKeyAndOrderFront: nil];
3434}
3435
3436- (void) windowDidBecomeMain: (NSNotification *) notification
3437{
3438    [fBadger clearCompleted];
3439    [self updateUI];
3440}
3441
3442- (NSSize) windowWillResize: (NSWindow *) sender toSize: (NSSize) proposedFrameSize
3443{
3444    //only resize horizontally if autosize is enabled
3445    if ([fDefaults boolForKey: @"AutoSize"])
3446        proposedFrameSize.height = [fWindow frame].size.height;
3447    return proposedFrameSize;
3448}
3449
3450- (void) windowDidResize: (NSNotification *) notification
3451{
3452    //size search filter to not overlap buttons
3453    float pointX = NSMaxX([fPauseFilterButton frame]) + 5.0;
3454    NSRect oldFrame = [fSearchFilterField frame],
3455            frame = NSMakeRect(pointX, oldFrame.origin.y, NSMaxX(oldFrame) - pointX, oldFrame.size.height);
3456   
3457    BOOL show = frame.size.width >= SEARCH_FILTER_MIN_WIDTH;
3458    if (show)
3459    {
3460        //make sure it is not too long
3461        if (frame.size.width > SEARCH_FILTER_MAX_WIDTH)
3462        {
3463            float different = frame.size.width - SEARCH_FILTER_MAX_WIDTH;
3464            frame.origin.x += different;
3465            frame.size.width -= different;
3466        }
3467        [fSearchFilterField setFrame: frame];
3468    }
3469   
3470    //hide search filter if it overlaps filter buttons
3471    [fSearchFilterField setHidden: !show];
3472}
3473
3474- (void) applicationWillUnhide: (NSNotification *) notification
3475{
3476    [self updateUI];
3477}
3478
3479- (void) linkHomepage: (id) sender
3480{
3481    [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: WEBSITE_URL]];
3482}
3483
3484- (void) linkForums: (id) sender
3485{
3486    [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: FORUM_URL]];
3487}
3488
3489- (void) linkDonate: (id) sender
3490{
3491    [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: DONATE_URL]];
3492}
3493
3494- (void) prepareForUpdate: (NSNotification *) notification
3495{
3496    fUpdateInProgress = YES;
3497}
3498
3499- (NSDictionary *) registrationDictionaryForGrowl
3500{
3501    NSArray * notifications = [NSArray arrayWithObjects: GROWL_DOWNLOAD_COMPLETE, GROWL_SEEDING_COMPLETE,
3502                                                            GROWL_AUTO_ADD, GROWL_AUTO_SPEED_LIMIT, nil];
3503    return [NSDictionary dictionaryWithObjectsAndKeys: notifications, GROWL_NOTIFICATIONS_ALL,
3504                                notifications, GROWL_NOTIFICATIONS_DEFAULT, nil];
3505}
3506
3507- (void) growlNotificationWasClicked: (id) clickContext
3508{
3509    if (!clickContext || ![clickContext isKindOfClass: [NSDictionary class]])
3510        return;
3511   
3512    NSString * type = [clickContext objectForKey: @"Type"], * location;
3513    if (([type isEqualToString: GROWL_DOWNLOAD_COMPLETE] || [type isEqualToString: GROWL_SEEDING_COMPLETE])
3514            && (location = [clickContext objectForKey: @"Location"]))
3515        [[NSWorkspace sharedWorkspace] selectFile: location inFileViewerRootedAtPath: nil];
3516}
3517
3518- (void) ipcQuit
3519{
3520    fRemoteQuit = YES;
3521    [NSApp terminate: self];
3522}
3523
3524- (NSArray *) ipcGetTorrentsByID: (NSArray *) idlist
3525{
3526    if (!idlist)
3527        return fTorrents;
3528   
3529    NSMutableArray * torrents = [NSMutableArray array];
3530   
3531    NSEnumerator * torrentEnum = [fTorrents objectEnumerator], * idEnum;
3532    int torId;
3533    Torrent * torrent;
3534    NSNumber * tempId;
3535    while ((torrent = [torrentEnum nextObject]))
3536    {
3537        torId = [torrent torrentID];
3538       
3539        idEnum = [idlist objectEnumerator];
3540        while ((tempId = [idEnum nextObject]))
3541        {
3542            if ([tempId intValue] == torId)
3543            {
3544                [torrents addObject: torrent];
3545                break;
3546            }
3547        }
3548    }
3549
3550    return torrents;
3551}
3552
3553- (NSArray *) ipcGetTorrentsByHash: (NSArray *) hashlist
3554{
3555    if (!hashlist)
3556        return fTorrents;
3557   
3558    NSMutableArray * torrents = [NSMutableArray array];
3559   
3560    NSEnumerator * torrentEnum = [fTorrents objectEnumerator], * hashEnum;
3561    NSString * torHash, * tempHash;
3562    Torrent * torrent;
3563    while ((torrent = [torrentEnum nextObject]))
3564    {
3565        torHash = [torrent hashString];
3566       
3567        hashEnum = [hashlist objectEnumerator];
3568        while ((tempHash = [hashEnum nextObject]))
3569        {
3570            if ([torHash caseInsensitiveCompare: tempHash] == NSOrderedSame)
3571            {
3572                [torrents addObject: torrent];
3573                break;
3574            }
3575        }
3576    }
3577   
3578    return torrents;
3579}
3580
3581- (BOOL) ipcAddTorrents: (NSArray *) torrents
3582{
3583    int oldCount = [fTorrents count];
3584   
3585    [self openFiles: torrents];
3586   
3587    return [fTorrents count] > oldCount;
3588}
3589
3590- (BOOL) ipcAddTorrentFile: (NSString *) path directory: (NSString *) directory
3591{
3592    int oldCount = [fTorrents count];
3593   
3594    [self openFiles: [NSArray arrayWithObject: path] forcePath: directory ignoreDownloadFolder: NO
3595            deleteTorrentFile: TORRENT_FILE_DEFAULT];
3596   
3597    return [fTorrents count] > oldCount;
3598}
3599
3600- (BOOL) ipcAddTorrentFileAutostart: (NSString *) path directory: (NSString *) directory autostart: (BOOL) autostart
3601{
3602    NSArray * torrents;
3603    if (autostart)
3604        torrents = [fTorrents copy];
3605    BOOL success = [self ipcAddTorrentFile: path directory: directory];
3606   
3607    if (success && autostart)
3608    {
3609        NSEnumerator * enumerator = [torrents objectEnumerator];
3610        Torrent * torrent;
3611        while ((torrent = [enumerator nextObject]))
3612            if (![torrents containsObject: torrent])
3613                break;
3614       
3615        if (torrent)
3616            [torrent startTransfer];
3617        else
3618            success = NO;
3619    }
3620   
3621    [torrents release];
3622    return success;
3623}
3624
3625- (BOOL) ipcAddTorrentData: (NSData *) data directory: (NSString *) directory
3626{
3627    /* 'data' is the contents of a torrent file, 'directory' is the
3628       directory it should download it's files to and may be nil,
3629       should return NO if torrent fails to load */
3630    return NO;
3631}
3632
3633- (BOOL) ipcAddTorrentDataAutostart: (NSData *) path directory: (NSString *) directory autostart: (BOOL) autostart
3634{
3635    /* 'data' is the contents of a torrent file, 'directory' is the
3636       directory it should download it's files to and may be nil,
3637       'autostart' is a boolean indicating if the torrent should be
3638       automatically started (or queued to start, I guess), should
3639       return NO if torrent fails to load */
3640    return NO;
3641}
3642
3643- (BOOL) ipcStartTorrents: (NSArray *) torrents
3644{
3645    if (!torrents)
3646        [self resumeAllTorrents: self];
3647    else
3648        [self resumeTorrents: torrents];
3649
3650    return YES;
3651}
3652
3653- (BOOL) ipcStopTorrents: (NSArray *) torrents
3654{
3655    if (!torrents)
3656        [self stopAllTorrents: self];
3657    else
3658        [self stopTorrents: torrents];
3659
3660    return YES;
3661}
3662
3663- (BOOL) ipcRemoveTorrents: (NSArray *) torrents
3664{
3665    if (!torrents)
3666        torrents = [NSArray arrayWithArray: fTorrents];
3667
3668    [self confirmRemoveTorrents: torrents deleteData: NO deleteTorrent: NO];
3669
3670    return YES;
3671}
3672
3673@end
Note: See TracBrowser for help on using the repository browser.