Changeset 539


Ignore:
Timestamp:
Jul 7, 2006, 1:03:52 AM (16 years ago)
Author:
livings124
Message:

Monitor a folder for torrent files, adding them as they are added (by polling the folder). Growl notifications for import, and only try to reimport the same torrent if the setting is changed, the app is restarted, or the torrent file is moved to a different folder then moved back.

Original code from Steve (brilla on the forums).

Location:
trunk/macosx
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/Controller.h

    r530 r539  
    7272    NSTimer                     * fTimer;
    7373   
     74    NSTimer                     * fAutoImportTimer;
     75    NSArray              * fAutoImportedNames;
     76   
    7477    BOOL                        fHasGrowl;
    7578    Badger                      * fBadger;
     
    143146- (void) reloadInspectorSettings: (NSNotification *) notification;
    144147
     148- (void) checkAutoImportDirectory: (NSTimer *) t;
     149- (void) autoImportChange: (NSNotification *) notification;
     150
    145151- (void) sleepCallBack: (natural_t) messageType argument:
    146152                        (void *) messageArgument;
     
    156162- (void) linkForums:        (id) sender;
    157163
    158 - (void) notifyGrowl:       (NSString *) file;
     164- (void) notifyGrowl: (NSString * ) title message: (NSString *) message notification: (NSString *) notification;
    159165- (void) growlRegister;
    160166
  • trunk/macosx/Controller.m

    r538 r539  
    9090    [fBadger release];
    9191    [fSortType release];
     92    [fAutoImportedNames release];
    9293   
    9394    tr_close( fLib );
     
    244245    [nc addObserver: self selector: @selector(reloadInspectorSettings:)
    245246                    name: @"TorrentSettingChange" object: nil];
    246 
    247     //timer to update the interface
     247   
     248    //reset auto import
     249    [nc addObserver: self selector: @selector(autoImportChange:)
     250                    name: @"AutoImportSettingChange" object: nil];
     251
     252    //timer to update the interface every second
    248253    fCompleted = 0;
    249254    [self updateUI: nil];
    250255    fTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self
    251         selector: @selector( updateUI: ) userInfo: nil repeats: YES];
     256        selector: @selector(updateUI:) userInfo: nil repeats: YES];
    252257    [[NSRunLoop currentRunLoop] addTimer: fTimer forMode: NSModalPanelRunLoopMode];
    253258    [[NSRunLoop currentRunLoop] addTimer: fTimer forMode: NSEventTrackingRunLoopMode];
     259   
     260    //timer for auto import, will check every 60 seconds
     261    fAutoImportedNames = [[NSArray alloc] init];
     262   
     263    [self checkAutoImportDirectory: nil];
     264    fAutoImportTimer = [NSTimer scheduledTimerWithTimeInterval: 5.0 target: self
     265        selector: @selector(checkAutoImportDirectory:) userInfo: nil repeats: YES];
     266    [[NSRunLoop currentRunLoop] addTimer: fAutoImportTimer forMode: NSDefaultRunLoopMode];
    254267   
    255268    [self sortTorrents];
     
    311324- (void) applicationWillTerminate: (NSNotification *) notification
    312325{
    313     //stop updating the interface
     326    //stop timers
     327    [fAutoImportTimer invalidate];
    314328    [fTimer invalidate];
    315329   
     
    457471}
    458472
    459 - (void) openFromSheet: (NSArray *) filenames
    460 {
    461     [self application: NSApp openFiles: filenames];
    462 }
    463 
    464473- (void) openSheetClosed: (NSOpenPanel *) panel returnCode: (int) code
    465474    contextInfo: (void *) info
     
    468477        [self performSelectorOnMainThread: @selector(openFromSheet:)
    469478                    withObject: [panel filenames] waitUntilDone: NO];
     479}
     480
     481- (void) openFromSheet: (NSArray *) filenames
     482{
     483    [self application: NSApp openFiles: filenames];
    470484}
    471485
     
    772786       
    773787            //notifications
    774             [self notifyGrowl: [torrent name]];
     788            [self notifyGrowl: @"Download Complete" message: [torrent name] notification: @"Download Complete"];
    775789            if (![fWindow isKeyWindow])
    776790                fCompleted++;
     
    11281142}
    11291143
     1144- (void) checkAutoImportDirectory: (NSTimer *) timer
     1145{
     1146    if (![fDefaults boolForKey: @"AutoImport"])
     1147        return;
     1148       
     1149    NSString * path = [[fDefaults stringForKey: @"AutoImportDirectory"] stringByExpandingTildeInPath];
     1150   
     1151    //if folder cannot be found simply give up
     1152    NSArray * allFileNames;
     1153    if (!(allFileNames = [[NSFileManager defaultManager] directoryContentsAtPath: path]))
     1154        return;
     1155
     1156    //try to add files that haven't already been added
     1157    NSMutableArray * newFileNames = [NSMutableArray arrayWithArray: allFileNames];
     1158    [newFileNames removeObjectsInArray: fAutoImportedNames];
     1159   
     1160    //save the current list of files
     1161    [fAutoImportedNames release];
     1162    fAutoImportedNames = [allFileNames retain];
     1163   
     1164    NSEnumerator * enumerator = [newFileNames objectEnumerator];
     1165    NSString * file;
     1166    unsigned oldCount;
     1167    while ((file = [enumerator nextObject]))
     1168        if ([[file pathExtension] caseInsensitiveCompare: @"torrent"] == NSOrderedSame)
     1169        {
     1170            NSString * fullPath = [path stringByAppendingPathComponent: file];
     1171           
     1172            oldCount = [fTorrents count];
     1173            [self openFromSheet: [NSArray arrayWithObject: fullPath]];
     1174           
     1175            //import only actually happened if the torrent array is larger
     1176            if (oldCount < [fTorrents count])
     1177                [self notifyGrowl: [file stringByAppendingString: @" Imported"] message: @"Torrent file imported"
     1178                                notification: @"Import Performed"];
     1179        }
     1180}
     1181
     1182- (void) autoImportChange: (NSNotification *) notification
     1183{
     1184    [fAutoImportedNames release];
     1185    fAutoImportedNames = [[NSArray alloc] init];
     1186   
     1187    [self checkAutoImportDirectory: nil];
     1188}
     1189
    11301190- (int) numberOfRowsInTableView: (NSTableView *) t
    11311191{
     
    17011761}
    17021762
    1703 - (void) notifyGrowl: (NSString * ) file
     1763- (void) notifyGrowl: (NSString * ) title message: (NSString *) message notification: (NSString *) notification
    17041764{
    17051765    if (!fHasGrowl)
     
    17101770         "  if exists application process \"GrowlHelperApp\" then\n"
    17111771         "    tell application \"GrowlHelperApp\"\n "
    1712          "      notify with name \"Download Complete\""
    1713          "        title \"Download Complete\""
     1772         "      notify with name \"%@\""
     1773         "        title \"%@\""
    17141774         "        description \"%@\""
    17151775         "        application name \"Transmission\"\n"
    17161776         "    end tell\n"
    17171777         "  end if\n"
    1718          "end tell", file];
     1778         "end tell", notification, title, message];
    17191779   
    17201780    NSAppleScript * appleScript = [[NSAppleScript alloc] initWithSource: growlScript];
     
    17301790        return;
    17311791
    1732     NSString * growlScript = [NSString stringWithFormat:
    1733         @"tell application \"System Events\"\n"
     1792    NSString * growlScript = @"tell application \"System Events\"\n"
    17341793         "  if exists application process \"GrowlHelperApp\" then\n"
    17351794         "    tell application \"GrowlHelperApp\"\n"
    17361795         "      register as application \"Transmission\" "
    1737          "        all notifications {\"Download Complete\"}"
    1738          "        default notifications {\"Download Complete\"}"
     1796         "        all notifications {\"Download Complete\", \"Import Performed\"}"
     1797         "        default notifications {\"Download Complete\", \"Import Performed\"}"
    17391798         "        icon of application \"Transmission\"\n"
    17401799         "    end tell\n"
    17411800         "  end if\n"
    1742          "end tell"];
     1801         "end tell";
    17431802
    17441803    NSAppleScript * appleScript = [[NSAppleScript alloc] initWithSource: growlScript];
  • trunk/macosx/Defaults.plist

    r530 r539  
    33<plist version="1.0">
    44<dict>
     5        <key>AutoImport</key>
     6        <false/>
     7        <key>AutoImportDirectory</key>
     8        <string>~/Desktop</string>
    59        <key>BadgeDownloadRate</key>
    610        <false/>
  • trunk/macosx/English.lproj/PrefsWindow.nib/classes.nib

    r490 r539  
    66            ACTIONS = {
    77                folderSheetShow = id;
     8                importFolderSheetShow = id;
     9                setAutoImport = id;
    810                setBadge = id;
    911                setDownloadLocation = id;
     
    2325            LANGUAGE = ObjC;
    2426            OUTLETS = {
     27                fAutoImportCheck = NSButton;
    2528                fBadgeDownloadRateCheck = NSButton;
    2629                fBadgeUploadRateCheck = NSButton;
     
    3235                fFolderPopUp = NSPopUpButton;
    3336                fGeneralView = NSView;
     37                fImportFolderPopUp = NSPopUpButton;
    3438                fNetworkView = NSView;
    3539                fPortField = NSTextField;
  • trunk/macosx/English.lproj/PrefsWindow.nib/info.nib

    r528 r539  
    1010                <string>275 334 554 217 0 0 1152 842 </string>
    1111                <key>28</key>
    12                 <string>299 451 554 254 0 0 1152 842 </string>
     12                <string>443 473 554 254 0 0 1440 878 </string>
    1313                <key>41</key>
    14                 <string>299 429 554 297 0 0 1152 842 </string>
     14                <string>241 434 554 342 0 0 1440 878 </string>
    1515                <key>66</key>
    1616                <string>299 526 554 104 0 0 1152 842 </string>
     
    1818        <key>IBFramework Version</key>
    1919        <string>446.1</string>
     20<<<<<<< .mine
     21=======
    2022        <key>IBOpenObjects</key>
    2123        <array>
    2224                <integer>153</integer>
    2325        </array>
     26>>>>>>> .r537
    2427        <key>IBSystem Version</key>
    2528        <string>8J135</string>
  • trunk/macosx/PrefsController.h

    r492 r539  
    3434    IBOutlet NSView         * fGeneralView, * fTransfersView, * fBandwidthView, * fNetworkView;
    3535   
    36     IBOutlet NSPopUpButton  * fFolderPopUp;
     36    IBOutlet NSPopUpButton  * fFolderPopUp, * fImportFolderPopUp;
    3737    IBOutlet NSButton       * fQuitCheck, * fRemoveCheck,
    3838                            * fQuitDownloadingCheck, * fRemoveDownloadingCheck,
    3939                            * fBadgeDownloadRateCheck, * fBadgeUploadRateCheck,
    40                             * fCopyTorrentCheck, * fDeleteOriginalTorrentCheck;
     40                            * fCopyTorrentCheck, * fDeleteOriginalTorrentCheck,
     41                            * fAutoImportCheck;
     42                           
    4143    IBOutlet NSPopUpButton  * fUpdatePopUp;
    4244
     
    5557    IBOutlet SUUpdater      * fUpdater;
    5658
    57     NSString                * fDownloadFolder;
     59    NSString                * fDownloadFolder, * fImportFolder;
    5860    NSUserDefaults          * fDefaults;
    5961}
     
    8385- (void) enableSpeedLimit: (BOOL) enable;
    8486
     87- (void) setAutoImport: (id) sender;
     88- (void) importFolderSheetShow: (id) sender;
     89
    8590- (void) setRatio:          (id) sender;
    8691- (void) setRatioCheck:     (id) sender;
  • trunk/macosx/PrefsController.m

    r507 r539  
    5858- (void) updatePopUp;
    5959
     60- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info;
     61- (void) updateImportPopUp;
     62
    6063@end
    6164
     
    9295    //set download folder
    9396    NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"];
    94     fDownloadFolder = [[fDefaults stringForKey: @"DownloadFolder"] stringByExpandingTildeInPath];
    95     [fDownloadFolder retain];
    96 
     97    fDownloadFolder = [[[fDefaults stringForKey: @"DownloadFolder"] stringByExpandingTildeInPath] retain];
    9798    if ([downloadChoice isEqualToString: @"Constant"])
    9899        [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
     
    102103        [fFolderPopUp selectItemAtIndex: DOWNLOAD_ASK];
    103104    [self updatePopUp];
    104 
     105   
     106    //set auto import
     107    fImportFolder = [[[fDefaults stringForKey: @"AutoImportDirectory"] stringByExpandingTildeInPath] retain];
     108    [self updateImportPopUp];
     109 
     110    BOOL autoImport = [fDefaults boolForKey: @"AutoImport"];
     111    [fAutoImportCheck setState: autoImport];
     112    [fImportFolderPopUp setEnabled: autoImport];
     113   
    105114    //set bind port
    106115    int bindPort = [fDefaults integerForKey: @"BindPort"];
     
    598607    [panel beginSheetForDirectory: nil file: nil types: nil
    599608        modalForWindow: [self window] modalDelegate: self didEndSelector:
    600         @selector( folderSheetClosed:returnCode:contextInfo: )
    601         contextInfo: nil];
     609        @selector(folderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
     610}
     611
     612- (void) setAutoImport: (id) sender
     613{
     614    int state = [fAutoImportCheck state];
     615    [fDefaults setBool: state forKey: @"AutoImport"];
     616    [fImportFolderPopUp setEnabled: state];
     617   
     618    [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
     619}
     620
     621- (void) importFolderSheetShow: (id) sender
     622{
     623    NSOpenPanel * panel = [NSOpenPanel openPanel];
     624
     625    [panel setPrompt: @"Select"];
     626    [panel setAllowsMultipleSelection: NO];
     627    [panel setCanChooseFiles: NO];
     628    [panel setCanChooseDirectories: YES];
     629    [panel setCanCreateDirectories: YES];
     630
     631    [panel beginSheetForDirectory: nil file: nil types: nil
     632        modalForWindow: [self window] modalDelegate: self didEndSelector:
     633        @selector(importFolderSheetClosed:returnCode:contextInfo:) contextInfo: nil];
    602634}
    603635
     
    653685   {
    654686       [fDownloadFolder release];
    655        fDownloadFolder = [[openPanel filenames] objectAtIndex: 0];
    656        [fDownloadFolder retain];
     687       fDownloadFolder = [[[openPanel filenames] objectAtIndex: 0] retain];
    657688
    658689       [fFolderPopUp selectItemAtIndex: DOWNLOAD_FOLDER];
     
    688719}
    689720
     721- (void) importFolderSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
     722{
     723   if (code == NSOKButton)
     724   {
     725       [fImportFolder release];
     726       fImportFolder = [[[openPanel filenames] objectAtIndex: 0] retain];
     727
     728       [fDefaults setObject: fImportFolder forKey: @"AutoImportDirectory"];
     729
     730       [self updateImportPopUp];
     731       
     732       [[NSNotificationCenter defaultCenter] postNotificationName: @"AutoImportSettingChange" object: self];
     733   }
     734   [fImportFolderPopUp selectItemAtIndex: 0];
     735}
     736
     737- (void) updateImportPopUp
     738{
     739    //get and resize the icon
     740    NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFile: fImportFolder];
     741    [icon setScalesWhenResized: YES];
     742    [icon setSize: NSMakeSize(16.0, 16.0)];
     743
     744    //update menu item
     745    NSMenuItem * menuItem = (NSMenuItem *) [fImportFolderPopUp itemAtIndex: 0];
     746    [menuItem setTitle: [fImportFolder lastPathComponent]];
     747    [menuItem setImage: icon];
     748}
     749
    690750@end
Note: See TracChangeset for help on using the changeset viewer.