Skip to content

Commit

Permalink
finally proper output format handling added (& cleanup)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcingrabda committed Jun 1, 2011
1 parent 2a3aa08 commit 8c43b79
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 41 deletions.
10 changes: 5 additions & 5 deletions AppController.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#import "DropFileView.h"
#import "AppPreferencesController.h"
#import "FileHandler.h"
#import "AppUtil.h"
#import "AppPreferences.h"

@implementation AppController

Expand All @@ -24,8 +24,8 @@ - (id)init
if (self)
{
instance = self;
allowedFileTypes = [[AppUtil typeExtensionsForName:@"Movie"] arrayByAddingObjectsFromArray:
[AppUtil typeExtensionsForName:@"Subtitles"]];
allowedFileTypes = [[AppPreferences typeExtensionsForName:@"Movie"] arrayByAddingObjectsFromArray:
[AppPreferences typeExtensionsForName:@"Subtitles"]];
}
return self;
}
Expand Down Expand Up @@ -63,9 +63,9 @@ - (void)filesDragged:(DropFileView*)sender
NSMutableArray* filteredFiles = [NSMutableArray new];
NSMutableArray* updatedAllowedFileTypes = [[NSMutableArray alloc] initWithArray:allowedFileTypes];

if (![AppUtil isSRTOnlyConversionAllowed])
if (![AppPreferences isSRTOnlyConversionAllowed])
{
[updatedAllowedFileTypes removeObjectsInArray:[AppUtil typeExtensionsForName:@"Subtitles"]];
[updatedAllowedFileTypes removeObjectsInArray:[AppPreferences typeExtensionsForName:@"Subtitles"]];
}

for (NSString* file in files)
Expand Down
24 changes: 24 additions & 0 deletions AppPreferences.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// AppUtil.h
// WhatSub
//
// Created by Marcin Grabda on 5/4/11.
// Copyright 2011 burningtomato.com. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface AppPreferences : NSObject {
@private
}

+ (NSArray*)typeExtensionsForName:(NSString*)typeName;
+ (NSString*)getNPUsername;
+ (NSString*)getNPPassword;
+ (NSString*)getNPLanguageCode;
+ (NSString*)getOutputFormat;
+ (NSStringEncoding)getOutputEncoding;
+ (BOOL)isSRTOnlyConversionAllowed;

@end
85 changes: 85 additions & 0 deletions AppPreferences.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//
// AppUtil.m
// WhatSub
//
// Created by Marcin Grabda on 5/4/11.
// Copyright 2011 burningtomato.com. All rights reserved.
//

#import "AppPreferences.h"


@implementation AppPreferences

- (id)init
{
self = [super init];
if (self) {
}
return self;
}

- (void)dealloc
{
[super dealloc];
}

+ (NSArray*)typeExtensionsForName:(NSString*)typeName
{
int i;
NSArray *typeList = [[[NSBundle mainBundle] infoDictionary]
objectForKey:@"CFBundleDocumentTypes"];
for (i=0; i<[typeList count]; i++) {
if ([[[typeList objectAtIndex:i] objectForKey:@"CFBundleTypeName"]
isEqualToString:typeName])
return [[typeList objectAtIndex:i] objectForKey:@"CFBundleTypeExtensions"];
}
return nil;
}

+ (NSString*)getNPUsername
{
return [[NSUserDefaults standardUserDefaults] valueForKey:@"NPUsername"];
}

+ (NSString*)getNPPassword
{
return [[NSUserDefaults standardUserDefaults] valueForKey:@"NPPassword"];
}

+ (NSString*)getNPLanguageCode
{
NSString* language = [[NSUserDefaults standardUserDefaults] valueForKey:@"NPLanguage"];
NSDictionary* languages = [NSDictionary dictionaryWithObjectsAndKeys:
@"PL", @"Polish",
@"EN", @"English", nil];
return [languages objectForKey:language];
}

+ (NSString*)getOutputFormat
{
return [[NSUserDefaults standardUserDefaults] valueForKey:@"OutputFormat"];
}

+ (NSStringEncoding)getOutputEncoding
{
NSString* encoding = [[NSUserDefaults standardUserDefaults] valueForKey:@"OutputEncoding"];
NSDictionary* encodings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithUnsignedInteger:NSUTF8StringEncoding], @"UTF-8",
[NSNumber numberWithUnsignedInteger:NSUTF16StringEncoding], @"UTF-16",
[NSNumber numberWithUnsignedInteger:NSISOLatin1StringEncoding], @"ISO-8859-1",
[NSNumber numberWithUnsignedInteger:NSWindowsCP1252StringEncoding], @"CP1252",
[NSNumber numberWithUnsignedInteger:NSISOLatin2StringEncoding], @"ISO-8859-2",
[NSNumber numberWithUnsignedInteger:NSWindowsCP1250StringEncoding], @"CP1250",
nil];
NSNumber* encodingNumber = [encodings objectForKey:encoding];
return [encodingNumber unsignedIntegerValue];
}

+ (BOOL)isSRTOnlyConversionAllowed
{
NSNumber* wrappedBool = [[NSUserDefaults standardUserDefaults] valueForKey:@"AllowSRTOnlyConversion"];
return [wrappedBool boolValue];
}

@end
4 changes: 2 additions & 2 deletions FileHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

@interface FileHandler : NSObject {
@private
NSArray* subtitlesFiles;
NSArray* movieFiles;
NSArray* subtitlesExtensions;
NSArray* movieExtensions;
SubtitlesConverter* converter;
SubtitlesDownloader* downloader;
IBOutlet NSWindow* loadingWindow;
Expand Down
27 changes: 20 additions & 7 deletions FileHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#import "SubtitlesConverter.h"
#import "SubtitlesDownloader.h"
#import "AppController.h"
#import "AppUtil.h"
#import "AppPreferences.h"

@implementation FileHandler

Expand All @@ -19,8 +19,8 @@ - (id)init
self = [super init];
if (self)
{
subtitlesFiles = [AppUtil typeExtensionsForName:@"Subtitles"];
movieFiles = [AppUtil typeExtensionsForName:@"Movie"];
subtitlesExtensions = [AppPreferences typeExtensionsForName:@"Subtitles"];
movieExtensions = [AppPreferences typeExtensionsForName:@"Movie"];
converter = [[SubtitlesConverter alloc] init];
downloader = [[SubtitlesDownloader alloc] init];
}
Expand Down Expand Up @@ -64,13 +64,26 @@ - (void)startProcessingFiles:(NSArray*)files
- (void)processFile:(NSString*)pathToFile
{
NSString* extension = [pathToFile pathExtension];
if ([subtitlesFiles containsObject:extension])
if ([subtitlesExtensions containsObject:extension])
{
[converter convert:pathToFile];
NSString* outputFilePath = [[pathToFile stringByDeletingPathExtension] stringByAppendingPathExtension:@"srt"];
[converter convert:pathToFile toFile:outputFilePath];
}
else if ([movieFiles containsObject:extension])
else if ([movieExtensions containsObject:extension])
{
[downloader download:pathToFile];
NSString* outputFormat = [AppPreferences getOutputFormat];
NSString* downloadedFilePath = [downloader download:pathToFile];
if ([outputFormat isEqualToString:@"SRT"])
{
NSString* outputFilePath = [[pathToFile stringByDeletingPathExtension] stringByAppendingPathExtension:@"srt"];
[converter convert:downloadedFilePath toFile:outputFilePath];
}
else if ([outputFormat isEqualToString:@"TXT"])
{
NSString* outputFilePath = [[pathToFile stringByDeletingPathExtension] stringByAppendingPathExtension:@"txt"];
NSFileManager* fileManager = [NSFileManager new];
[fileManager copyItemAtPath:downloadedFilePath toPath:outputFilePath error:NULL];
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions NapiProjektEngine.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#import "NapiProjektEngine.h"
#import <openssl/md5.h>
#import "AppUtil.h"
#import "AppPreferences.h"

@implementation NapiProjektEngine

Expand Down Expand Up @@ -56,9 +56,9 @@ - (NSString*)getURLForHash:(NSString*)hash token:(NSString*)token
/* TODO move to configuration file... */
NSString* urlFormatString =
@"http://napiprojekt.pl/unit_napisy/dl.php?l=%@&f=%@&t=%@&v=other&kolejka=false&nick=%@&pass=%@";
NSString* langCode = [AppUtil getNPLanguageCode];
NSString* nickname = [AppUtil getNPUsername];
NSString* password = [AppUtil getNPPassword];
NSString* langCode = [AppPreferences getNPLanguageCode];
NSString* nickname = [AppPreferences getNPUsername];
NSString* password = [AppPreferences getNPPassword];

return [NSString stringWithFormat:urlFormatString, langCode, hash, token, nickname, password];
}
Expand Down
2 changes: 1 addition & 1 deletion SubtitlesConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@private
}

- (void)convert:(NSString*)pathToFile;
- (void)convert:(NSString*)pathToFile toFile:(NSString*)outputFilePath;
- (NSArray*)readFile:(NSString*)pathToFile;
- (NSArray*)processTMPlayer:(NSArray*)lines;
- (NSArray*)processMicroDVD:(NSArray*)lines forMovie:(NSString*)pathToFile;
Expand Down
14 changes: 7 additions & 7 deletions SubtitlesConverter.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
#import "SubtitlesConverter.h"
#import "FrameRateCalculator.h"
#import "RegexKitLite.h"
#import "AppUtil.h"
#import "AppPreferences.h"
#import "AppController.h"

@implementation SubtitlesConverter

NSString* const TMP_REGEX = @"^(\\d+):(\\d+):(\\d+):(.*)";
NSString* const MDVD_REGEX = @"^\\{(\\d+)\\}\\{(\\d+)\\}(.*)";
NSString* const MPL2_REGEX = @"^\\[(\\d+)\\]\\[(\\d+)\\](.*)";

- (void)convert:(NSString*)pathToFile
- (void)convert:(NSString*)pathToFile toFile:(NSString*)outputFilePath
{
NSLog(@"Processing file '%@'...", pathToFile);
NSArray* fileContents = [self readFile:pathToFile];
Expand Down Expand Up @@ -47,10 +48,8 @@ - (void)convert:(NSString*)pathToFile
}

if (subRipArray != nil)
{
//TODO check if the file exists
NSString* srtFilePath = [[pathToFile stringByDeletingPathExtension] stringByAppendingPathExtension:@"srt"];
[self printSubRip:subRipArray toFile:srtFilePath];
{
[self printSubRip:subRipArray toFile:outputFilePath];
}
}

Expand Down Expand Up @@ -82,6 +81,7 @@ - (NSArray*)readFile:(NSString*)pathToFile
@throw e;
}
}

NSLog(@"Encoding guessed: %@", [NSString localizedNameOfStringEncoding:encoding]);

// try LF first
Expand Down Expand Up @@ -205,7 +205,7 @@ - (void)printSubRip:(NSArray *)input toFile:(NSString *)srtFilePath
[entireText appendString:linePrint];
}

NSStringEncoding encoding = [AppUtil getOutputEncoding];
NSStringEncoding encoding = [AppPreferences getOutputEncoding];
NSData *data = [entireText dataUsingEncoding:encoding allowLossyConversion:YES];
[data writeToFile:srtFilePath atomically:YES];
}
Expand Down
2 changes: 1 addition & 1 deletion SubtitlesDownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
NapiProjektEngine* engine;
}

- (void)download:(NSString*)pathToFile;
- (NSString*)download:(NSString*)pathToFile;

@end
17 changes: 9 additions & 8 deletions SubtitlesDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ - (id)init
return self;
}

- (void)download:(NSString*)pathToFile
- (NSString*)download:(NSString*)pathToFile
{
NSString* hash = nil;
NSData* fileData = [engine retrieveSubtitlesForMovieInPath:pathToFile hash:&hash];
Expand All @@ -46,14 +46,15 @@ - (void)download:(NSString*)pathToFile
[task waitUntilExit];

NSString* subtitlesFileName = [p7zipOutputFilePath stringByDeletingPathExtension];
NSString* sourcePath = [subtitlesFileName stringByAppendingPathExtension:@"txt"];
NSString* workingDirectory = [pathToFile stringByDeletingPathExtension];
NSString* destinationPath = [workingDirectory stringByAppendingPathExtension:@"txt"];

NSError* error;
NSFileManager* fileManager = [[NSFileManager alloc] init];
[fileManager copyItemAtPath:sourcePath toPath:destinationPath error:&error];
NSString* filePath = [subtitlesFileName stringByAppendingPathExtension:@"txt"];
return filePath;
}
else
{
NSString* reason = @"Corrupted subtitles file";
NSException* e = [NSException exceptionWithName:@"SubtitlesException" reason:reason userInfo:nil];
@throw e;
}
}

@end
12 changes: 6 additions & 6 deletions WhatSub.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
04C34A691110C8930080BDEB /* RegexKitLite.m in Sources */ = {isa = PBXBuildFile; fileRef = 04C34A681110C8930080BDEB /* RegexKitLite.m */; };
04C34C8111123FEB0080BDEB /* NapiProjektEngine.m in Sources */ = {isa = PBXBuildFile; fileRef = 04C34C8011123FEB0080BDEB /* NapiProjektEngine.m */; };
04D04000110F6DB1005DA3B0 /* SubtitlesConverter.m in Sources */ = {isa = PBXBuildFile; fileRef = 04D03FFF110F6DB1005DA3B0 /* SubtitlesConverter.m */; };
04EBB4FD1371B6E500CC15DF /* AppUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = 04EBB4FC1371B6E400CC15DF /* AppUtil.m */; };
04EBB4FD1371B6E500CC15DF /* AppPreferences.m in Sources */ = {isa = PBXBuildFile; fileRef = 04EBB4FC1371B6E400CC15DF /* AppPreferences.m */; };
04F72E38126F68BC007C0205 /* AppPreferencesController.m in Sources */ = {isa = PBXBuildFile; fileRef = 04F72E37126F68BC007C0205 /* AppPreferencesController.m */; };
04F72EF8126F759D007C0205 /* Defaults.plist in Resources */ = {isa = PBXBuildFile; fileRef = 04F72EF7126F759D007C0205 /* Defaults.plist */; };
04F931600FB8579E003FFCB3 /* FrameRateCalculator.m in Sources */ = {isa = PBXBuildFile; fileRef = 04F9315F0FB8579E003FFCB3 /* FrameRateCalculator.m */; };
Expand Down Expand Up @@ -56,8 +56,8 @@
04C34C8011123FEB0080BDEB /* NapiProjektEngine.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NapiProjektEngine.m; sourceTree = "<group>"; };
04D03FFE110F6DB1005DA3B0 /* SubtitlesConverter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SubtitlesConverter.h; sourceTree = "<group>"; };
04D03FFF110F6DB1005DA3B0 /* SubtitlesConverter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SubtitlesConverter.m; sourceTree = "<group>"; };
04EBB4FB1371B6E400CC15DF /* AppUtil.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = AppUtil.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
04EBB4FC1371B6E400CC15DF /* AppUtil.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = AppUtil.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
04EBB4FB1371B6E400CC15DF /* AppPreferences.h */ = {isa = PBXFileReference; fileEncoding = 4; lineEnding = 0; path = AppPreferences.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
04EBB4FC1371B6E400CC15DF /* AppPreferences.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = AppPreferences.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
04F72E37126F68BC007C0205 /* AppPreferencesController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppPreferencesController.m; sourceTree = "<group>"; };
04F72EF7126F759D007C0205 /* Defaults.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Defaults.plist; sourceTree = "<group>"; };
04F9315E0FB8579E003FFCB3 /* FrameRateCalculator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FrameRateCalculator.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -133,8 +133,8 @@
children = (
04F9315E0FB8579E003FFCB3 /* FrameRateCalculator.h */,
04F9315F0FB8579E003FFCB3 /* FrameRateCalculator.m */,
04EBB4FB1371B6E400CC15DF /* AppUtil.h */,
04EBB4FC1371B6E400CC15DF /* AppUtil.m */,
04EBB4FB1371B6E400CC15DF /* AppPreferences.h */,
04EBB4FC1371B6E400CC15DF /* AppPreferences.m */,
);
name = Helper;
sourceTree = "<group>";
Expand Down Expand Up @@ -310,7 +310,7 @@
0471C884124AB368001A567D /* FileHandler.m in Sources */,
04F72E38126F68BC007C0205 /* AppPreferencesController.m in Sources */,
04AFD19513702BBB00AFE116 /* SubtitlesDownloader.m in Sources */,
04EBB4FD1371B6E500CC15DF /* AppUtil.m in Sources */,
04EBB4FD1371B6E500CC15DF /* AppPreferences.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit 8c43b79

Please sign in to comment.