Ignore:
Timestamp:
Nov 12, 2006, 8:42:22 PM (15 years ago)
Author:
livings124
Message:

Patch by Brian Webster to automatically download and open torrents by dragging url's onto the window/dock icon.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/Controller.m

    r1080 r1081  
    108108        fTorrents = [[NSMutableArray alloc] initWithCapacity: 10];
    109109        fDisplayedTorrents = [[NSMutableArray alloc] initWithCapacity: 10];
     110        fPendingTorrentDownloads = [[NSMutableDictionary alloc] init];
    110111       
    111112        fDefaults = [NSUserDefaults standardUserDefaults];
     
    141142   
    142143    [fAutoImportedNames release];
     144    [fPendingTorrentDownloads release];
    143145   
    144146    tr_close(fLib);
     
    217219    [[fTableView tableColumnWithIdentifier: @"Torrent"] setDataCell: [[TorrentCell alloc] init]];
    218220
    219     [fTableView registerForDraggedTypes: [NSArray arrayWithObjects: NSFilenamesPboardType,
     221    [fTableView registerForDraggedTypes: [NSArray arrayWithObjects: NSFilenamesPboardType,
     222                                                        NSURLPboardType,
    220223                                                        TORRENT_TABLE_VIEW_DATA_TYPE, nil]];
    221224
     
    347350    fAutoImportedNames = [[NSMutableArray alloc] init];
    348351    [self checkAutoImportDirectory];
     352}
     353
     354- (void) applicationDidFinishLaunching: (NSNotification *) notification
     355{
     356    [NSApp setServicesProvider:self];
     357   
     358    //register for dock icon drags
     359    [[NSAppleEventManager sharedAppleEventManager] setEventHandler: self
     360        andSelector: @selector(handleOpenContentsEvent:replyEvent:)
     361        forEventClass: kCoreEventClass andEventID: kAEOpenContents];
    349362}
    350363
     
    440453}
    441454
     455- (void) handleOpenContentsEvent: (NSAppleEventDescriptor *) event replyEvent: (NSAppleEventDescriptor *) replyEvent
     456{
     457    NSURL * url;
     458    NSString * urlString;
     459
     460    NSAppleEventDescriptor * directObject = [event paramDescriptorForKeyword: keyDirectObject];
     461    if ([directObject descriptorType] == typeAEList)
     462    {
     463        unsigned i;
     464        for (i = 1; i <= [directObject numberOfItems]; i++)
     465            if ((urlString = [[directObject descriptorAtIndex: i] stringValue]))
     466            {
     467                url = [[NSURL alloc] initWithString: urlString];
     468                break;
     469            }
     470    }
     471    else if ((urlString = [directObject stringValue]))
     472        url = [[NSURL alloc] initWithString: urlString];
     473    else;
     474   
     475    if (url)
     476    {
     477        [self openURL: url];
     478        [url release];
     479    }
     480}
     481
     482- (void) openURL: (NSURL *) url
     483{
     484    #warning check for .torrent
     485    NSURLDownload * torrentDownload = [[NSURLDownload alloc] initWithRequest: [NSURLRequest requestWithURL: url]
     486                                        delegate: self];
     487   
     488    NSString * tempPath = [NSTemporaryDirectory() stringByAppendingPathComponent: [[url path] lastPathComponent]];
     489    NSLog(tempPath);[torrentDownload setDestination: tempPath allowOverwrite: NO];
     490    [fPendingTorrentDownloads setObject: tempPath forKey: url];
     491    [torrentDownload release];
     492}
     493
     494- (void) download: (NSURLDownload *)download didFailWithError: (NSError *) error
     495{
     496    [fPendingTorrentDownloads removeObjectForKey: [[download request] URL]];
     497   
     498    NSRunAlertPanel(NSLocalizedString(@"Torrent download failed",
     499        @"Torrent download error -> title"), [NSString stringWithFormat:
     500        NSLocalizedString(@"The torrent could not be downloaded from %@ because an error occurred (%@)",
     501        @"Torrent download failed -> message"), [[[download request] URL] absoluteString],
     502        [error localizedDescription]], NSLocalizedString(@"OK", @"Torrent download failed -> button"), nil, nil);
     503}
     504
     505- (void) downloadDidFinish: (NSURLDownload *) download
     506{
     507    #warning try to open, if not delete
     508   
     509    [self openFiles: [NSArray arrayWithObject: [fPendingTorrentDownloads objectForKey: [[download request] URL]]]
     510            ignoreDownloadFolder: NO];
     511}
     512
    442513- (void) openFiles: (NSArray *) filenames ignoreDownloadFolder: (BOOL) ignore
    443514{
    444     NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"], * torrentPath;
    445    
     515    NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
    446516    if (ignore || [downloadChoice isEqualToString: @"Ask"])
    447517    {
     
    451521   
    452522    Torrent * torrent;
     523    NSString * torrentPath;
    453524    NSEnumerator * enumerator = [filenames objectEnumerator];
    454525    while ((torrentPath = [enumerator nextObject]))
     
    15391610    if (![fDefaults boolForKey: @"AutoImport"])
    15401611        return;
    1541        
     1612       
    15421613    NSString * path = [[fDefaults stringForKey: @"AutoImportDirectory"] stringByExpandingTildeInPath];
    15431614   
     
    16401711            }
    16411712    }
     1713    else if ([[pasteboard types] containsObject: NSURLPboardType])
     1714    {
     1715        [fTableView setDropRow: row dropOperation: NSTableViewDropAbove];
     1716        return NSDragOperationGeneric;
     1717    }
    16421718    else if ([[pasteboard types] containsObject: TORRENT_TABLE_VIEW_DATA_TYPE])
    16431719    {
     
    16661742        [filesToOpen release];
    16671743    }
    1668     else
     1744    else if ([[pasteboard types] containsObject: NSURLPboardType])
     1745    {
     1746        NSURL * url;
     1747        if ((url = [NSURL URLFromPasteboard:pasteboard]))
     1748            [self openURL: url];
     1749    }
     1750    else if ([[pasteboard types] containsObject: TORRENT_TABLE_VIEW_DATA_TYPE])
    16691751    {
    16701752        //remember selected rows if needed
     
    17181800        }
    17191801    }
     1802    else;
    17201803   
    17211804    return YES;
Note: See TracChangeset for help on using the changeset viewer.