Repository navigation
Expand file tree
/
Copy pathappController.m
More file actions
executable file
·390 lines (347 loc) · 13.4 KB
/
Copy pathappController.m
File metadata and controls
executable file
·390 lines (347 loc) · 13.4 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
This file is part of LineBreak.
LineBreak is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
LineBreak is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LineBreak; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Copyright (c) 2001-2008 Josh Aas.
*/
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import "TextTools.h"
#import "ChoosePanel.h"
#import "appController.h"
#import "ProtectionPanel.h"
#import "Prefs.h"
#define FileManager [NSFileManager defaultManager]
#define WorkSpace [NSWorkspace sharedWorkspace]
@implementation appController
-(id)init {
if ((self = [super init])) {
// set up the custom cursor image now so there is no delay on the first drag
// use a copy since we don't want to resize the original
NSImage *appIcon = [[[NSImage imageNamed:@"NSApplicationIcon"] copy] autorelease];
[appIcon setSize:NSMakeSize(32,32)];
mDragDestCursor = [[NSCursor alloc] initWithImage:appIcon hotSpot:NSMakePoint(16,16)];
mAppIsLaunching = YES;
mWasLaunchedWithDocument = NO;
mFilesToProcess = [[NSMutableArray alloc] initWithCapacity:5];
}
return self;
}
-(void)dealloc {
[mFilesToProcess release];
[mDragDestCursor release];
[super dealloc];
}
+(void)initialize {
NSMutableDictionary *defaultPrefs = [NSMutableDictionary dictionary];
[defaultPrefs setObject:@"0" forKey:ConvertToKey];
[defaultPrefs setObject:@"YES" forKey:ChooseOnDropKey];
[defaultPrefs setObject:@"NO" forKey:HandleFoldersKey];
[defaultPrefs setObject:@"NO" forKey:RecursiveFolderHandlingKey];
[defaultPrefs setObject:@"NO" forKey:PreserveModDates];
// This could be done in a plist but its unnecessary for now
[defaultPrefs setObject:[[NSArray arrayWithObjects:@"doc", @"jpg", @"jpeg", @"png", @"bmp",
@"pdf", @"psd", @"gif", @"tiff", @"tif", @"exe", @"app", @"nib", @"jar", @"zip", @"sit",
@"class", @"xls", @"mp3", @"mpg", @"mov", @"ppt", @"m4p", @"mv4", @"wav", @"rtf", @"rtfd",
@"tar", @"gz", @"tgz", @"mpeg", @"dmg", @"sitx", nil] sortedArrayUsingSelector:@selector(compare:)]
forKey:NonTextFileExtensionsKey];
[defaultPrefs setObject:[[NSArray arrayWithObjects:@"html", @"shtml", @"htm", @"txt", @"pl", @"cgi",
@"php", @"php3", @"php4", @"js", @"xml", nil] sortedArrayUsingSelector:@selector(compare:)] forKey:TextFileExtensionsKey];
// The two options are "only" or "exclude"
[defaultPrefs setObject:@"exclude" forKey:ExcludeOrOnlyFileExtensions];
[defaultPrefs setObject:@"YES" forKey:ProtectionOn];
[preferences registerDefaults:defaultPrefs];
}
-(void)awakeFromNib {
NSWindow *mainWindow = [mTypeSelectPopup window];
// Set Up Interface Elements
[mTypeSelectPopup selectItemAtIndex:[preferences integerForKey:ConvertToKey]];
[preferences boolForKey:ChooseOnDropKey] ? [mAskCheckBox setState:NSOnState] : [mAskCheckBox setState:NSOffState];
[preferences boolForKey:RecursiveFolderHandlingKey] ? [mRecursiveCheckBox setState:NSOnState] : [mRecursiveCheckBox setState:NSOffState];
if ([preferences boolForKey:HandleFoldersKey]) {
[mFoldersCheckBox setState:NSOnState];
[mRecursiveCheckBox setEnabled:YES];
}
else {
[mFoldersCheckBox setState:NSOffState];
[mRecursiveCheckBox setEnabled:NO];
}
if ([preferences boolForKey:ProtectionOn]) {
[mProtectionCheckBox setState:NSOnState];
[mSetProtectionButton setEnabled:YES];
}
else {
[mProtectionCheckBox setState:NSOffState];
[mSetProtectionButton setEnabled:NO];
}
if ([preferences boolForKey:PreserveModDates]) {
[mModDatesMenuItem setState:NSOnState];
}
else {
[mModDatesMenuItem setState:NSOffState];
}
// set up the main window as a drag destination
[mainWindow registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
// bring the main window forward
[mainWindow makeKeyAndOrderFront:self];
}
-(BOOL)shouldConvert:(NSString*)file {
int i;
BOOL convertFlag = YES;
NSArray *extensions;
// Check HFS Type - we don't do this any more
// but I left the code here in case you want to
/*
NSString *HFSFileType = NSHFSTypeOfFile(file);
if (HFSFileType != nil) {
if (!([HFSFileType isEqualToString:NSFileTypeForHFSTypeCode('TEXT')] || [HFSFileType isEqualToString:NSFileTypeForHFSTypeCode(nil)])) {
return NO;
}
}
*/
// Make sure we can write to the file
if (![FileManager isWritableFileAtPath:file]) {
return NO;
}
// See if protection settings block it
if ([preferences boolForKey:ProtectionOn]) {
if ([[preferences stringForKey:ExcludeOrOnlyFileExtensions] isEqualToString:@"exclude"]) {
convertFlag = YES;
extensions = [preferences arrayForKey:NonTextFileExtensionsKey];
for (i = 0; i < [extensions count]; i++) {
if ([[file pathExtension] caseInsensitiveCompare:[extensions objectAtIndex:i]] == NSOrderedSame) {
convertFlag = NO;
break;
}
}
}
else {
convertFlag = NO;
extensions = [preferences arrayForKey:TextFileExtensionsKey];
for (i = 0; i < [extensions count]; i++) {
if ([[file pathExtension] caseInsensitiveCompare:[extensions objectAtIndex:i]] == NSOrderedSame) {
convertFlag = YES;
break;
}
}
}
}
return convertFlag;
}
// Returns whether or not the file was actually converted
-(void)convertFile:(NSString*)file toFormat:(int)type {
// preserve modification date if the user wants to
NSDictionary *fileAttrs = nil;
if ([preferences boolForKey:PreserveModDates]) {
fileAttrs = [FileManager fileAttributesAtPath:file traverseLink:YES];
}
if ([self shouldConvert:file]) {
if (type == jaUnixLBFormat) {
[TextTools convertFileToUnix:file];
}
else if (type == jaMacLBFormat) {
[TextTools convertFileToMac:file];
}
else if (type == jaDOSLBFormat) {
[TextTools convertFileToDOS:file];
}
}
if (fileAttrs != nil) {
// only change the NSFileModificationDate instead of all attributes
[FileManager changeFileAttributes:[NSDictionary dictionaryWithObject:[fileAttrs objectForKey:NSFileModificationDate] forKey:NSFileModificationDate] atPath:file];
}
}
-(NSArray*)getDirPaths:(NSString*)directory {
int i;
NSMutableArray *dirContents = [[[NSMutableArray alloc] init] autorelease];
[dirContents addObjectsFromArray:[FileManager directoryContentsAtPath:directory]];
// Don't deal with invisible files, prevents .DS_Store corruption
for (i = 0; i < [dirContents count]; i++) {
if ([[dirContents objectAtIndex:i] characterAtIndex:0] == '.') {
[dirContents removeObjectAtIndex:i];
}
}
for (i = 0; i < [dirContents count]; i++) {
[dirContents replaceObjectAtIndex:i withObject:[NSString pathWithComponents:
[NSArray arrayWithObjects:directory, [dirContents objectAtIndex:i], nil]]];
}
return dirContents;
}
-(void)processDirRecursively:(NSString*)directory toFormat:(int)type {
int i;
BOOL isDir = NO;
NSArray *dirFiles = [self getDirPaths:directory];
for (i = 0; i < [dirFiles count]; i++) {
if (![WorkSpace isFilePackageAtPath:[dirFiles objectAtIndex:i]]) {
if ([FileManager fileExistsAtPath:[dirFiles objectAtIndex:i] isDirectory:&isDir] && isDir) {
[self processDirRecursively:[dirFiles objectAtIndex:i] toFormat:type];
}
else {
[self convertFile:[dirFiles objectAtIndex:i] toFormat:type];
}
}
}
}
-(void)processDirNonRecursively:(NSString*)directory toFormat:(int)type {
int i;
NSArray *dirContents;
BOOL isDir = NO;
dirContents = [self getDirPaths:directory];
for (i = 0; i < [dirContents count]; i++) {
if (![WorkSpace isFilePackageAtPath:[dirContents objectAtIndex:i]]) {
if (!([FileManager fileExistsAtPath:[dirContents objectAtIndex:i] isDirectory:&isDir] && isDir)) {
[self convertFile:[dirContents objectAtIndex:i] toFormat:type];
}
}
}
}
-(void)handleConversions:(NSArray*)filesToConvert toFormat:(int)type {
int i;
BOOL isDir = NO;
[mProgressBar setIndeterminate:YES];
[mProgressBar setUsesThreadedAnimation:YES];
[mProgressBar startAnimation:self];
for (i = 0; i < [filesToConvert count]; i++) {
if (![WorkSpace isFilePackageAtPath:[filesToConvert objectAtIndex:i]]) {
if ([FileManager fileExistsAtPath:[filesToConvert objectAtIndex:i] isDirectory:&isDir] && isDir) {
if ([preferences boolForKey:HandleFoldersKey]) {
if ([preferences boolForKey:RecursiveFolderHandlingKey]) {
[self processDirRecursively:[filesToConvert objectAtIndex:i] toFormat:type];
}
else {
[self processDirNonRecursively:[filesToConvert objectAtIndex:i] toFormat:type];
}
}
}
else {
[self convertFile:[filesToConvert objectAtIndex:i] toFormat:type];
}
}
}
[mProgressBar stopAnimation:self];
[mProgressBar setIndeterminate:NO];
}
// if file is not nil, tell user what its type is before conversion
-(int)chooseTypeOnDrop:(NSString*)file {
if ([preferences boolForKey:ChooseOnDropKey]) {
// run the choose panel
return [[ChoosePanel sharedInstance] askUserForLineBreakType:file];
}
return [preferences integerForKey:ConvertToKey];
}
-(void)convertFilesOnDrop {
int format;
// if there is only a single file to deal with, tell the user what its type is
if ([mFilesToProcess count] == 1) {
format = [self chooseTypeOnDrop:[mFilesToProcess objectAtIndex:0]];
}
else {
format = [self chooseTypeOnDrop:nil];
}
// When format is "3", the user chose the cancel button.
// When format is "-1", there was an error opening a file.
if (format == jaUnixLBFormat ||
format == jaMacLBFormat ||
format == jaDOSLBFormat) {
[self handleConversions:mFilesToProcess toFormat:format];
}
[mFilesToProcess removeAllObjects];
// quit if it was a quick drag-drop job
if (mWasLaunchedWithDocument) {
[NSApp terminate:self];
}
}
/*
DELEGATES
*/
#pragma mark -
-(void)applicationWillFinishLaunching:(NSNotification*)aNotification {
mAppIsLaunching = YES;
}
-(BOOL)application:(NSApplication*)anApplication openFile:(NSString*)aFileName {
// If app is launching the app was opened with a document
mWasLaunchedWithDocument = mAppIsLaunching;
[mFilesToProcess addObject:aFileName];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(convertFilesOnDrop) object:nil];
[self performSelector:@selector(convertFilesOnDrop) withObject:nil afterDelay:0.2];
return YES;
}
-(void)applicationDidFinishLaunching:(NSNotification*)aNotification {
mAppIsLaunching = NO;
}
-(NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
return NSDragOperationGeneric;
}
-(unsigned int)draggingUpdated:(id <NSDraggingInfo>)sender {
[mDragDestCursor set];
return NSDragOperationGeneric;
}
-(BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender {
return YES;
}
-(BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
[mFilesToProcess addObjectsFromArray:[[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType]];
[self convertFilesOnDrop];
return YES;
}
/*
ACTIONS
*/
#pragma mark -
// Convert document via open panel
-(IBAction)convertViaOpenPanel:(id)sender {
NSOpenPanel *oPanel = [NSOpenPanel openPanel];
[oPanel setCanChooseDirectories:[preferences boolForKey:HandleFoldersKey]];
[oPanel setAllowsMultipleSelection:YES];
[oPanel setResolvesAliases:YES];
if ([oPanel runModalForDirectory:NSHomeDirectory() file:nil types:nil] == NSOKButton) {
[self handleConversions:[oPanel filenames] toFormat:[preferences integerForKey:ConvertToKey]];
}
}
// Set preferences based on what got selected.
// UNIX is 0, Mac is 1, Dos is 2
-(IBAction)setTypePrefs:(id)sender {
[preferences setInteger:[sender indexOfSelectedItem] forKey:ConvertToKey];
}
// Change the prefs for checkbox elements
-(IBAction)setCheckboxPrefs:(id)sender {
if (sender == mAskCheckBox) {
[preferences setBool:[mAskCheckBox state] forKey:ChooseOnDropKey];
}
if (sender == mFoldersCheckBox) {
[preferences setBool:[mFoldersCheckBox state] forKey:HandleFoldersKey];
[mRecursiveCheckBox setEnabled:[mFoldersCheckBox state]];
}
if (sender == mRecursiveCheckBox) {
[preferences setBool:[mRecursiveCheckBox state] forKey:RecursiveFolderHandlingKey];
}
if (sender == mProtectionCheckBox) {
[preferences setBool:[mProtectionCheckBox state] forKey:ProtectionOn];
[mSetProtectionButton setEnabled:[mProtectionCheckBox state]];
}
}
// Brings up the protection panel.
-(IBAction)showProtectionPanel:(id)sender {
[[ProtectionPanel sharedInstance] showPanel:sender];
}
// Toggle the preservation of modification dates
-(IBAction)toggleModDates:(id)sender {
if ([sender state] == NSOffState) {
[preferences setBool:YES forKey:PreserveModDates];
[sender setState:NSOnState];
}
else {
[preferences setBool:NO forKey:PreserveModDates];
[sender setState:NSOffState];
}
}
@end