-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAppController.mm
277 lines (217 loc) · 7.01 KB
/
AppController.mm
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//
// AppController.mm
// iMusicTags
//
// Created by Kevin Chen on 10-7-23.
// Copyright 2010 Kevin Chen's workstation. All rights reserved.
//
#import "AppController.h"
#import "MusicFileInfo.h"
#import "CharacterEncoding.h"
@implementation AppController
@synthesize encoding, window;
- (void)awakeFromNib
{
[window setReleasedWhenClosed:NO];
[tableView registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType,nil]];
NSMutableArray *content = [[NSMutableArray alloc] initWithObjects:
[[CharacterCatalog alloc] initWithValue:C_CATALOG_CHOOSE
description:@""], nil];
[content addObjectsFromArray:[CharacterCatalog catalogs]];
[catalogCtrl setContent:content];
}
- (id)init
{
self = [super init];
if (self) {
encoding = kCFStringEncodingInvalidId;
}
return self;
}
- (IBAction)open:(id)sender
{
NSOpenPanel *panel = [NSOpenPanel openPanel];
[panel setCanChooseFiles:YES];
[panel setAllowsMultipleSelection:YES];
if ([panel runModal] == NSOKButton) {
displayInfo = [[NSMutableArray alloc] init];
fileUrls = [[NSMutableArray alloc] init];
fileSet = [[NSMutableSet alloc] init];
for (NSURL *url in [panel URLs]) {
if(![fileSet containsObject:url]) {
MusicFileInfo *fileInfo = [[MusicFileInfo alloc] initWithUrl:url];
if (fileInfo) {
[displayInfo addObject:fileInfo];
[fileUrls addObject:url];
[fileSet addObject:url];
}
}
}
[tableView reloadData];
[tableView sizeToFit];
}
}
- (NSInteger)numberOfRowsInTableView:(NSTabView *)aTableView
{
return [displayInfo count];
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(NSInteger)rowIndex
{
id theRecord, theValue;
NSParameterAssert((rowIndex >= 0) && (rowIndex < [displayInfo count]));
theRecord = [displayInfo objectAtIndex:rowIndex];
theValue = [theRecord valueForKey:[aTableColumn identifier]];
return theValue;
}
- (void)tableView:(NSTableView *)aTableView
setObjectValue:anObject
forTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
id theRecord;
NSParameterAssert(rowIndex >= 0 && rowIndex < [displayInfo count]);
theRecord = [displayInfo objectAtIndex:rowIndex];
[theRecord setObject:anObject forKey:[aTableColumn identifier]];
return;
}
- (NSDragOperation)tableView:(NSTableView *)aTableView
validateDrop:(id <NSDraggingInfo>)info
proposedRow:(NSInteger)row
proposedDropOperation:(NSTableViewDropOperation)op
{
return NSDragOperationEvery;
}
- (BOOL)tableView:(NSTableView *)aTableView
acceptDrop:(id <NSDraggingInfo>)info
row:(NSInteger)row
dropOperation:(NSTableViewDropOperation)operation
{
NSPasteboard *pboard = [info draggingPasteboard];
if ([[pboard types] containsObject:NSFilenamesPboardType]) {
if (nil == displayInfo) {
displayInfo = [[NSMutableArray alloc] init];
}
if (nil == fileUrls) {
fileUrls = [[NSMutableArray alloc] init];
}
if (nil == fileSet) {
fileSet = [[NSMutableSet alloc] init];
}
for (NSString *path in [pboard propertyListForType:NSFilenamesPboardType])
{
if ([path isEqualToString:@""]) {
continue;
}
NSURL *url = [NSURL fileURLWithPath:path];
if (![fileUrls containsObject:url]) {
MusicFileInfo *fileInfo = [[MusicFileInfo alloc] initWithUrl:url];
if (fileInfo) {
[displayInfo addObject:fileInfo];
[fileUrls addObject:url];
[fileSet addObject:url];
}
}
}
[tableView reloadData];
[tableView sizeToFit];
return YES;
}
return NO;
}
- (IBAction)preview:(id)sender
{
if (encoding == kCFStringEncodingInvalidId) {
NSString *imageName = [[NSBundle mainBundle] pathForResource:@"iMusicTags" ofType:@"icns"];
NSImage* imageObj = [[NSImage alloc] initWithContentsOfFile:imageName];
NSAlert *alert = [NSAlert alertWithMessageText:@"Pick up an Encoding, dude" defaultButton:@"Return"
alternateButton:nil
otherButton:nil
informativeTextWithFormat:@"Please choose an encoding before preview music information"];
[alert setIcon:imageObj];
[alert runModal];
return;
}
displayInfo = [[NSMutableArray alloc] init];
for (NSURL *url in fileUrls) {
[displayInfo addObject:[[MusicFileInfo alloc] initWithUrl:url withEncoding:encoding]];
}
[tableView reloadData];
}
- (IBAction)deleteRows:(id)sender
{
NSIndexSet *selected = [tableView selectedRowIndexes];
if ([selected count] <= 0) {
NSBeep();
return;
}
NSUInteger *indexBuff = nil;
indexBuff = (NSUInteger *)(malloc(sizeof(NSUInteger) * [selected count]));
if (nil == indexBuff) {
return;
}
NSRange range = NSMakeRange([selected firstIndex], [selected lastIndex] + 1);
[selected getIndexes:indexBuff maxCount:[selected count] inIndexRange:&range];
NSUInteger *index = indexBuff;
for (int i = 0; i < [selected count]; i++) {
MusicFileInfo *file = (MusicFileInfo *)[displayInfo objectAtIndex:*(index + i)];
[fileUrls removeObject:file.fileUrl];
[fileSet removeObject:file.fileUrl];
}
[displayInfo removeObjectsAtIndexes:selected];
[tableView reloadData];
[tableView deselectAll:self];
free(indexBuff);
}
- (IBAction)convert:(id)sender
{
if (encoding == kCFStringEncodingInvalidId) {
NSString *imageName = [[NSBundle mainBundle] pathForResource:@"iMusicTags" ofType:@"icns"];
NSImage* imageObj = [[NSImage alloc] initWithContentsOfFile:imageName];
NSAlert *alert = [NSAlert alertWithMessageText:@"Pick up an Encoding, dude" defaultButton:@"Return"
alternateButton:nil
otherButton:nil
informativeTextWithFormat:@"Please choose an encoding before convert music information"];
[alert setIcon:imageObj];
[alert runModal];
return;
}
for (id info in displayInfo) {
[(MusicFileInfo *)info writeTags:encoding];
}
[displayInfo removeAllObjects];
[fileUrls removeAllObjects];
[fileSet removeAllObjects];
[tableView reloadData];
NSBeep();
}
- (IBAction)chooseEncoding:(id)sender
{
NSInteger selectedEncoding = [[[encodingCtrl selection] valueForKey:@"encodingCode"] intValue];
encoding = selectedEncoding;
}
- (IBAction)chooseCatalog:(id)sender {
CharacterEncodingUtil *util = [[CharacterEncodingUtil alloc] init];
NSInteger catalogType = [[[catalogCtrl selection] valueForKey:@"catalogType"] intValue];
NSMutableArray *content = [[NSMutableArray alloc] initWithObjects:
[[CharacterEncoding alloc] initWithEncoding:kCFStringEncodingInvalidId
desciption:@""
type:catalogType], nil];
[content addObjectsFromArray:[util encodings:catalogType]];
[encodingCtrl setContent:content];
}
- (IBAction)newWindow:(id)sender{
[window makeKeyAndOrderFront:self];
}
- (BOOL)applicationShouldHandleReopen:(NSApplication*)theApplication
hasVisibleWindows:(BOOL)flag{
[window orderFront:nil];
return TRUE;
}
- (void)windowWillClose:(NSNotification *)notification {
[fileSet removeAllObjects];
[fileUrls removeAllObjects];
[displayInfo removeAllObjects];
[tableView reloadData];
}
@end