-
Notifications
You must be signed in to change notification settings - Fork 8
/
FileHandler.m
118 lines (104 loc) · 4.26 KB
/
FileHandler.m
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// FileHandler.m
// WhatSub
//
// Created by Marcin Grabda on 9/22/10.
// Copyright 2010 Marcin Grabda. All rights reserved.
//
#import "FileHandler.h"
#import "FileArchiver.h"
#import "SubtitlesConverter.h"
#import "SubtitlesDownloader.h"
#import "NapiProjektEngine.h"
#import "AppController.h"
#import "AppPreferences.h"
@implementation FileHandler
- (id)init
{
self = [super init];
if (self)
{
subtitlesExtensions = [AppPreferences typeExtensionsForName:@"Subtitles"];
movieExtensions = [AppPreferences typeExtensionsForName:@"Movie"];
}
return self;
}
- (void)startProcessingFiles:(NSArray*)files
{
[progressIndicator setUsesThreadedAnimation:YES];
[progressIndicator startAnimation:self];
NSWindow* mainWindow = [[AppController instance] mainWindow];
[NSApp beginSheet:loadingWindow modalForWindow:mainWindow modalDelegate:nil didEndSelector:nil contextInfo:nil];
NSMutableString* errorMessage = [NSMutableString string];
for (NSString* file in files) {
@try
{
[self processFile:file];
}
@catch (NSException* e)
{
[errorMessage appendString:[[e reason] stringByAppendingString:@"\n"]];
}
}
[progressIndicator stopAnimation:self];
[NSApp endSheet:loadingWindow];
[loadingWindow orderOut:self];
if ([errorMessage length] > 0)
{
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"OK"];
[alert setMessageText:@"Oops..."];
[alert setInformativeText:errorMessage];
[alert setAlertStyle:NSWarningAlertStyle];
[alert beginSheetModalForWindow:mainWindow modalDelegate:nil didEndSelector:nil contextInfo:nil];
}
else
{
BOOL closeApp = [AppPreferences isClosingAppAfterProcessingEnabled];
if (closeApp)
{
[NSApp terminate:self];
}
}
}
- (void)processFile:(NSString*)pathToFile
{
NSString* outputFilePath = nil;
NSString* extension = [pathToFile pathExtension];
NSStringEncoding outputEncoding = [AppPreferences getOutputEncoding];
NSNumber* defaultFrameRate = [AppPreferences getDefaultFrameRate];
BOOL archive = [AppPreferences isArchivingIfFileExistsEnabled];
SubtitlesConverter* converter = [[SubtitlesConverter alloc] initWithSupportedMovieExtensions:movieExtensions
andDefaultFrameRate:defaultFrameRate];
FileArchiver* archiver = [[FileArchiver alloc] init];
if ([subtitlesExtensions containsObject:extension])
{
outputFilePath = [[pathToFile stringByDeletingPathExtension] stringByAppendingPathExtension:@"srt"];
if (archive) [archiver archiveFileIfExists:outputFilePath];
[converter convert:pathToFile toFile:outputFilePath forMovie:nil withEncoding:outputEncoding];
}
else if ([movieExtensions containsObject:extension])
{
NSString* outputFormat = [AppPreferences getOutputFormat];
NSString* user = [AppPreferences getNPUsername];
NSString* pass = [AppPreferences getNPPassword];
NSString* lang = [AppPreferences getNPLanguageCode];
NapiProjektEngine* engine = [[NapiProjektEngine alloc] initWithUser:user password:pass language:lang];
SubtitlesDownloader* downloader = [[SubtitlesDownloader alloc] initWithEngine:engine];
NSString* downloadedTmpFilePath = [downloader download:pathToFile];
if ([outputFormat isEqualToString:@"SRT"])
{
outputFilePath = [[pathToFile stringByDeletingPathExtension] stringByAppendingPathExtension:@"srt"];
if (archive) [archiver archiveFileIfExists:outputFilePath];
[converter convert:downloadedTmpFilePath toFile:outputFilePath forMovie:pathToFile withEncoding:outputEncoding];
}
else if ([outputFormat isEqualToString:@"TXT"])
{
outputFilePath = [[pathToFile stringByDeletingPathExtension] stringByAppendingPathExtension:@"txt"];
if (archive) [archiver archiveFileIfExists:outputFilePath];
[converter convertWithoutProcessing:downloadedTmpFilePath toFile:outputFilePath withEncoding:outputEncoding];
}
}
NSLog(@"Subtitles written to %@", outputFilePath);
}
@end