This repository has been archived by the owner on Sep 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TableListCon.m
146 lines (127 loc) · 4.85 KB
/
TableListCon.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
#import "TableListCon.h"
#import "tvdb_api_wrapper.h"
@implementation TableListCon
- (void)awakeFromNib
{
[tableView setDraggingSourceOperationMask:NSDragOperationLink forLocal:NO];
[tableView setDraggingSourceOperationMask:(NSDragOperationCopy | NSDragOperationMove) forLocal:YES];
[tableView registerForDraggedTypes:[NSArray arrayWithObjects:
NSFilenamesPboardType, nil]];
[tableView setAllowsMultipleSelection:YES];
}
- (BOOL)tableView:(NSTableView *)aTableView
writeRowsWithIndexes:(NSIndexSet *)rowIndexes
toPasteboard:(NSPasteboard *)pboard
{
return YES;
}
- (NSDragOperation)tableView:(NSTableView*)tv
validateDrop:(id <NSDraggingInfo>)info
proposedRow:(int)row
proposedDropOperation:(NSTableViewDropOperation)op
{
NSDragOperation dragOp = NSDragOperationCopy;
return dragOp;
}
- (void)addFileToList:(NSString*)path
{
NSString *old_filename = [[path pathComponents] lastObject];
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; // 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
];
}
NSString *displaystr = [NSString stringWithFormat:@"Old: %@\nNew: %@",
old_filename,
new_filename
];
// Generate dict of new file
NSDictionary *cfile = [NSMutableDictionary dictionaryWithObjectsAndKeys:
displaystr, @"displaystr",
[NSNumber numberWithBool:YES], @"rename",
path, @"path",
old_filename, @"old_filename",
new_filename, @"new_filename",
parsed_name, @"parsed_name",
nil];
// Add it to the tableView's array controller
[ArrayCon addObject:cfile];
}
- (void)addDirectoryToList:(NSString*)path
{
NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager]
enumeratorAtPath:path];
NSString *pname;
while (pname = [direnum nextObject])
{
if ([[[pname pathComponents] lastObject] hasPrefix:@"."])
{
// hidden dot-file/folder, skip this and sub-dirs
[direnum skipDescendents];
}
else
{
[self addFileToList:pname];
}
}
}
- (BOOL)tableView:(NSTableView*)tv
acceptDrop:(id<NSDraggingInfo>)info
row:(int)row
dropOperation:(NSTableViewDropOperation)op
{
[busy setHidden:NO];
[busy startAnimation:self];
NSPasteboard *pboard = [info draggingPasteboard];
if([[pboard types] containsObject:NSFilenamesPboardType])
{
NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
for(id file in files)
{
BOOL isDir;
if([[NSFileManager defaultManager]
fileExistsAtPath:file isDirectory:&isDir] && isDir)
[self addDirectoryToList:file];
else
[self addFileToList:file];
}
// Done all files
[busy setHidden:YES];
[busy stopAnimation:self];
return YES;
}
[busy setHidden:YES];
[busy stopAnimation:self];
return NO;
}
@end