-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathExceptionsPref.m
263 lines (248 loc) · 10.3 KB
/
ExceptionsPref.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//
// ExceptionsPref.m
// Hachidori
//
// Created by 高町なのは on 2014/11/16.
// Copyright 2014-2015 MAL Updater OS X Group and James Moy All rights reserved. Code licensed under New BSD License
//
#import "ExceptionsPref.h"
#import <DetectionKit/DetectionKit.h>
#import "ExceptionsCache.h"
@interface ExceptionsPref ()
@end
@implementation ExceptionsPref
@synthesize arraycontroller;
@synthesize ignorearraycontroller;
@synthesize ignorefilenamearraycontroller;
@synthesize tb;
@synthesize iftb;
@synthesize detectedtitle;
@synthesize fsdialog;
@dynamic managedObjectContext;
- (NSManagedObjectContext *)managedObjectContext {
AppDelegate *appDelegate = (AppDelegate *)[NSApplication sharedApplication].delegate;
return appDelegate.managedObjectContext;
}
- (instancetype)init
{
return [super initWithNibName:@"ExceptionsPref" bundle:nil];
}
#pragma mark -
#pragma mark MASPreferencesViewController
- (NSString *)viewIdentifier
{
return @"ExceptionsPreferences";
}
- (NSImage *)toolbarItemImage
{
if (@available(macOS 11.0, *)) {
return [NSImage imageWithSystemSymbolName:@"square.and.pencil" accessibilityDescription:nil];
} else {
// Fallback on earlier versions
return [NSImage imageNamed:@"rules"];
}
}
- (NSString *)toolbarItemLabel
{
return NSLocalizedString(@"Exceptions", @"Toolbar item name for the Exceptions spreference pane");
}
#pragma mark Anime Exceptions List Functions
- (IBAction)addTitle:(id)sender{
//Obtain Detected Title from Media File
NSOpenPanel * op = [NSOpenPanel openPanel];
op.allowedFileTypes = @[@"mkv", @"mp4", @"avi", @"ogm", @"rm", @"rmvb", @"wmv", @"divx", @"mov", @"flv", @"mpg", @"3gp"];
[op setMessage:NSLocalizedString(@"Please select a media file you want to create an exception for.",nil)];
[op beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger result) {
if (result == NSFileHandlingPanelCancelButton) {
return;
}
//Close Open Window
[op orderOut:nil];
NSDictionary * d = [[Recognition alloc] recognize:op.URL.path];
detectedtitle = d[@"title"];
_detectedseason = ((NSNumber *)d[@"season"]).intValue;
if ([self checkifexists:detectedtitle offset:0 correcttitle:nil]) {
// Exists, don't do anything
return;
}
fsdialog = [FixSearchDialog new];
[fsdialog setCorrection:false];
fsdialog.searchquery = detectedtitle;
[NSApp beginSheet:fsdialog.window
modalForWindow:self.view.window modalDelegate:self
didEndSelector:@selector(correctionDidEnd:returnCode:contextInfo:)
contextInfo:(void *)nil];
}];
}
- (void)correctionDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo {
if (returnCode == 1) {
// Check if correct title exists
if ([self checkifexists:detectedtitle offset:0 correcttitle:fsdialog.selectedtitle]) {
// Exists, don't do anything
return;
}
// Add to Exceptions
[ExceptionsCache addtoExceptions:detectedtitle correcttitle:fsdialog.selectedtitle aniid:fsdialog.selectedaniid.stringValue threshold:fsdialog.selectedtotalepisodes offset:0 detectedSeason:_detectedseason];
//Check Cache
[ExceptionsCache checkandRemovefromCache:detectedtitle detectedSeason:_detectedseason withService:0];
}
// Refetch Exceptions Data
[arraycontroller fetch:self];
fsdialog = nil;
detectedtitle = nil;
}
- (IBAction)removeSlection:(id)sender{
//Remove Selected Object
[arraycontroller removeObject:arraycontroller.selectedObjects[0]];
}
- (IBAction)importList:(id)sender{
// Set Open Dialog to get json file.
NSOpenPanel * op = [NSOpenPanel openPanel];
op.allowedFileTypes = @[@"json", NSLocalizedString(@"JSON file",nil)];
[op setMessage:NSLocalizedString(@"Please select an exceptions list to import.",nil)];
[op beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger result) {
if (result == NSFileHandlingPanelCancelButton) {
return;
}
NSURL *Url = op.URL;
// read the file
NSString * str = [NSString stringWithContentsOfURL:Url
encoding:NSUTF8StringEncoding
error:NULL];
NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSArray * a = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
for (NSDictionary *d in a) {
NSString * detectedtitlea = d[@"detectedtitle"];
int detectseason;
if (d[@"detectedseason"]) {
detectseason = ((NSNumber *)d[@"detectedseason"]).intValue;
}
else {
detectseason = 1;
}
int doffset;
if (d[@"offset"]) {
doffset = ((NSNumber *)d[@"offset"]).intValue;
}
else {
doffset = 0;
}
BOOL exists = [self checkifexists:detectedtitlea offset:doffset correcttitle:(NSString *)d[@"correcttitle"]];
// Check to see if it exists on the list already
if (exists) {
//Check next title
continue;
}
else {
// Add to Exceptions List
int threshold;
if (d[@"threshold"]) {
threshold = ((NSNumber *)d[@"threshold"]).intValue;
}
else {
threshold = 0;
}
[ExceptionsCache addtoExceptions:d[@"detectedtitle"] correcttitle:d[@"correcttitle"] aniid:d[@"showid"] threshold:threshold offset:doffset detectedSeason:detectseason];
//Check Cache
[ExceptionsCache checkandRemovefromCache:(NSString *)d[@"detectedtitle"] detectedSeason:_detectedseason withService:0];
}
// Refetch Exceptions Data
[arraycontroller fetch:self];
}
}];
}
- (IBAction)exportList:(id)sender{
// Save the json file containing titles
NSSavePanel * sp = [NSSavePanel savePanel];
sp.allowedFileTypes = @[@"json", NSLocalizedString(@"JSON file",nil)];
[sp setMessage:NSLocalizedString(@"Where do you want to save your exception list?",nil)];
[sp setNameFieldStringValue:NSLocalizedString(@"Exceptions List.json",nil)];
[sp beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger result) {
if (result == NSFileHandlingPanelCancelButton) {
return;
}
NSURL *url = sp.URL;
//Create JSON string from array controller
NSError *error;
NSMutableArray * jsonOutput = [[NSMutableArray alloc] init];
for (NSManagedObject * o in arraycontroller.arrangedObjects) {
@autoreleasepool {
NSDictionary *d = @{@"detectedtitle": [o valueForKey:@"detectedTitle"], @"correcttitle": [o valueForKey:@"correctTitle"], @"showid": [o valueForKey:@"id"], @"offset": [o valueForKey:@"episodeOffset"], @"threshold": [o valueForKey:@"episodethreshold"], @"detectedseason": [o valueForKey:@"detectedSeason"]};
[jsonOutput addObject:d];
}
}
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonOutput
options:0
error:&error];
if (!jsonData) {
return;
} else {
NSString *JSONString = [[NSString alloc] initWithBytes:jsonData.bytes length:jsonData.length encoding:NSUTF8StringEncoding];
//write JSON to file
BOOL wresult = [JSONString writeToURL:url
atomically:YES
encoding:NSUTF8StringEncoding
error:&error];
if (! wresult) {
NSLog(@"Export Failed: %@", error);
}
}
}];
}
#pragma mark Misc Functions
- (IBAction)getHelp:(id)sender{
//Show Help
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://help.malupdaterosx.moe/hachidori/correction-exception-help/"]];
}
- (BOOL)checkifexists:(NSString *) title offset:(int)offset correcttitle:(NSString *)ctitle{
// Checks if a title is already on the exception list
NSArray * a = arraycontroller.arrangedObjects;
for (NSManagedObject * entry in a) {
int eoffset = ((NSNumber *)[entry valueForKey:@"episodeOffset"]).intValue;
if ([title isEqualToString:(NSString *)[entry valueForKey:@"detectedTitle"]] && eoffset == offset) {
if (!ctitle) {
return true;
}
else if (ctitle && [ctitle isEqualToString:(NSString *)[entry valueForKey:@"correctTitle"]]) {
return true;
}
}
}
return false;
}
#pragma mark Ignore List
- (IBAction)addDirectory:(id)sender{
//Selects directory to ignore
NSOpenPanel * op = [NSOpenPanel openPanel];
[op setCanChooseDirectories:YES];
[op setCanCreateDirectories:NO];
[op setCanChooseFiles:NO];
[op setMessage:NSLocalizedString(@"Please a directory for Hachidori to ignore.",nil)];
[op beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger result) {
if (result == NSFileHandlingPanelCancelButton) {
return;
}
NSDictionary * entry = @{@"directory": op.URL.path};
[ignorearraycontroller addObject:entry];
}];
}
- (IBAction)removeDirectory:(id)sender{
//Remove Selected Object
[ignorearraycontroller removeObject:ignorearraycontroller.selectedObjects[0]];
}
#pragma mark Title Ignore
- (IBAction)addFifleNameIgnoreRule:(id)sender{
NSDictionary * entry = @{@"rule": @"", @"rulesource": @"All Sources"};
[ignorefilenamearraycontroller addObject:entry];
// Selection Workaround
int c = (int) [NSArray arrayWithArray:ignorefilenamearraycontroller.arrangedObjects].count;
if(c > 0) {
[iftb editColumn:0 row:c-1 withEvent:nil select:YES];
}
}
- (IBAction)removeFileNameIgnoreRule:(id)sender{
//Remove Selected Object
[ignorefilenamearraycontroller removeObject:ignorefilenamearraycontroller.selectedObjects[0]];
}
@end