Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 94 lines (73 sloc) 3.317 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
#import "AppCon.h"
#import "tvdb_api_wrapper.h"

@implementation AppCon

-(void)awakeFromNib
{
    // Set TableList's array to blank array
    self.theFiles = [NSMutableArray array];
}

-(NSString*)getNewFileName:(NSString*)old_filename
{
    tvdb_api_wrapper *api = [[tvdb_api_wrapper alloc] init];
    [api autorelease];
    
    // Parse name, get: series_filename, seasno, epno
    NSMutableDictionary *parsed_name = [api parseName:old_filename];
    if(!parsed_name) return nil; // Invalid filename
    
    NSMutableDictionary *seriesinfo = [api getSeriesId:[parsed_name objectForKey:@"file_seriesname"]];
    DebugLog(@"Got series %@ with ID %@",
             [seriesinfo objectForKey:@"name"],
             [seriesinfo objectForKey:@"sid"]);
    
    NSString *epname = [api getEpNameForSid:
                        [NSNumber numberWithLong:[[seriesinfo objectForKey:@"sid"] doubleValue]]
                                     seasno:[parsed_name objectForKey:@"seasno"]
                                       epno:[parsed_name objectForKey:@"epno"]];
    NSString *extention = [parsed_name objectForKey:@"ext"];
    
    NSString *new_filename;
    
    if(epname){
        DebugLog(@"Got episode name: %@", epname);
        new_filename = [NSString stringWithFormat:@"%@ - [%02dx%02d] - %@.%@",
                        [seriesinfo objectForKey:@"name"],
                        [[parsed_name objectForKey:@"seasno"] intValue],
                        [[parsed_name objectForKey:@"epno"] intValue],
                        epname,
                        extention
                        ];
    }
    else
    {
        new_filename = [NSString stringWithFormat:@"%@ - [%02dx%02d].%@",
                        [parsed_name objectForKey:@"file_seriesname"],
                        [[parsed_name objectForKey:@"seasno"] intValue],
                        [[parsed_name objectForKey:@"epno"] intValue],
                        epname,
                        extention
                        ];
        
    }
    
    return new_filename;
}

-(IBAction)renameFilesAction:(id)sender{
    [busy setHidden:NO];
    [busy startAnimation:self];
    
    tvdb_api_wrapper *api = [[tvdb_api_wrapper alloc] init];
    [api autorelease];
    
    for(id cur_file in theFiles){
        // Skip unselected files
        if([[cur_file objectForKey:@"rename"] isEqualTo:[NSNumber numberWithBool:NO]])
            continue;
        
        NSString *old_filepath = [cur_file objectForKey:@"path"];
        NSString *new_filename = [cur_file objectForKey:@"new_filename"];
        
        NSString *new_filepath = [[old_filepath stringByDeletingLastPathComponent]
                                  stringByAppendingPathComponent:new_filename];

        
        DebugLog(@"Renaming..\n%@\n..to..\n%@", old_filepath, new_filepath);

        BOOL worked = [[NSFileManager defaultManager] movePath:old_filepath toPath:new_filepath handler:nil];
        
        if(!worked)
            NSRunAlertPanel( @"PANIC!",
                            [NSString stringWithFormat:@"%@ could not be renamed to %@",
                             old_filepath, new_filepath],
                            @"Oh well", nil, nil );
        
        [busy setHidden:YES];
        [busy stopAnimation:self];
    }
}

@synthesize theFiles;
@end
Something went wrong with that request. Please try again.