source: trunk/macosx/Controller.m @ 3645

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

use built-in suppression checkbox in alerts under Leopard

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