source: trunk/macosx/Controller.m @ 4148

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

better hiding of bottom texture on 10.5

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