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 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
|
/*
GNUstep ProjectCenter - http://www.gnustep.org/experience/ProjectCenter.html
Copyright (C) 2000-2011 Free Software Foundation
Authors: Philippe C.D. Robert
Serg Stoyan
This file is part of GNUstep.
This application 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.
This application 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
Library General Public License for more details.
You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
*/
#import <ProjectCenter/PCDefines.h>
#import <ProjectCenter/PCFileManager.h>
#import <ProjectCenter/PCFileCreator.h>
#import <ProjectCenter/PCProjectManager.h>
#import <ProjectCenter/PCProject.h>
#import <ProjectCenter/PCProjectBrowser.h>
#import <ProjectCenter/PCAddFilesPanel.h>
#import <Protocols/Preferences.h>
#import <ProjectCenter/PCLogController.h>
@implementation PCFileManager
// ===========================================================================
// ==== Class methods
// ===========================================================================
static PCFileManager *_mgr = nil;
+ (PCFileManager *)defaultManager
{
if (_mgr == nil)
{
_mgr = [[self alloc] init];
}
return _mgr;
}
// ===========================================================================
// ==== Init and free
// ===========================================================================
- (id)initWithProjectManager:(PCProjectManager *)aProjectManager
{
if ((self = [super init]))
{
projectManager = aProjectManager;
}
return self;
}
- (void)dealloc
{
#ifdef DEVELOPMENT
NSLog (@"PCFileManager: dealloc");
#endif
if (addFilesPanel)
{
RELEASE(addFilesPanel);
}
[super dealloc];
}
// ===========================================================================
// ==== NSFileManager delegate methods
// ===========================================================================
- (BOOL) fileManager:(NSFileManager *)manager
shouldProceedAfterError:(NSDictionary *)errorDict
{
NSLog(@"FM error is: %@", [errorDict objectForKey:@"Error"]);
return YES;
}
// ===========================================================================
// ==== File handling
// ===========================================================================
- (BOOL)createDirectoriesIfNeededAtPath:(NSString *)path
{
NSString *_path = [NSString stringWithString:path];
NSString *_oldPath = nil;
NSMutableArray *pathArray = [NSMutableArray array];
NSFileManager *fm = [NSFileManager defaultManager];
BOOL isDir;
int i;
/* We stop when we find a file, or when we can't remove any path
* component any more. Else, you may end up in an infinite loop if
* _path = @"".
*/
isDir = NO;
while (_path != nil
&& ![_path isEqualToString: _oldPath]
&& ![fm fileExistsAtPath:_path isDirectory:&isDir])
{
[pathArray addObject:[_path lastPathComponent]];
_oldPath = _path;
_path = [_path stringByDeletingLastPathComponent];
}
if (!isDir)
{
return NO;
}
if ([_path length] != [path length])
{
for (i = [pathArray count]-1; i >= 0; i--)
{
_path =
[_path stringByAppendingPathComponent:[pathArray objectAtIndex:i]];
if ([fm createDirectoryAtPath:_path attributes:nil] == NO)
{
return NO;
}
}
}
return YES;
}
- (BOOL)copyFile:(NSString *)file toFile:(NSString *)toFile
{
NSFileManager *fm = [NSFileManager defaultManager];
NSString *directoryPath = nil;
if (!file)
{
return NO;
}
if (![fm fileExistsAtPath:toFile])
{
directoryPath = [toFile stringByDeletingLastPathComponent];
if ([self createDirectoriesIfNeededAtPath:directoryPath] == NO)
{
NSRunAlertPanel(@"Copy File",
@"Couldn't create directories at path %@",
@"Ok",nil,nil, directoryPath);
return NO;
}
if ([fm copyPath:file toPath:toFile handler:self] == NO)
{
NSRunAlertPanel(@"Copy File",
@"Couldn't copy file %@ to %@",
@"Ok",nil,nil, file, toFile);
return NO;
}
}
return YES;
}
- (BOOL)copyFile:(NSString *)file intoDirectory:(NSString *)directory
{
NSString *path = nil;
if (!file)
{
return NO;
}
path = [directory stringByAppendingPathComponent:[file lastPathComponent]];
if (![self copyFile:file toFile:path])
{ // No need to open aler panel here
return NO;
}
return YES;
}
- (BOOL)copyFile:(NSString *)file
fromDirectory:(NSString *)fromDir
intoDirectory:(NSString *)toDir
{
NSString *path = nil;
if (!file || !fromDir || !toDir)
{
return NO;
}
path = [fromDir stringByAppendingPathComponent:[file lastPathComponent]];
if (![self copyFile:path intoDirectory:toDir])
{
return NO;
}
return YES;
}
- (BOOL)copyFiles:(NSArray *)files intoDirectory:(NSString *)directory
{
NSEnumerator *enumerator = nil;
NSString *file = nil;
if (!files)
{
return NO;
}
enumerator = [files objectEnumerator];
while ((file = [enumerator nextObject]))
{
if ([self copyFile:file intoDirectory:directory] == NO)
{
return NO;
}
}
return YES;
}
- (BOOL)removeDirectoriesIfEmptyAtPath:(NSString *)path
{
NSFileManager *fm = [NSFileManager defaultManager];
while ([[fm directoryContentsAtPath:path] count] == 0)
{
if ([fm removeFileAtPath:path handler:nil] == NO)
{
NSRunAlertPanel(@"Remove Directory",
@"Couldn't remove empty directory at path %@",
@"Ok",nil,nil, path);
return NO;
}
path = [path stringByDeletingLastPathComponent];
}
return YES;
}
- (BOOL)removeFile:(NSString *)file
fromDirectory:(NSString *)directory
removeDirsIfEmpty:(BOOL)removeDirs
{
NSString *path = nil;
NSFileManager *fm = [NSFileManager defaultManager];
if (!file)
{
return NO;
}
path = [directory stringByAppendingPathComponent:file];
if (![fm removeFileAtPath:path handler:nil])
{
NSRunAlertPanel(@"Remove File",
@"Couldn't remove file at path %@",
@"Ok",nil,nil, path);
return NO;
}
if (removeDirs)
{
[self removeDirectoriesIfEmptyAtPath:directory];
}
return YES;
}
- (BOOL)removeFileAtPath:(NSString *)file removeDirsIfEmpty:(BOOL)removeDirs
{
return [self removeFile:[file lastPathComponent]
fromDirectory:[file stringByDeletingLastPathComponent]
removeDirsIfEmpty:removeDirs];
}
- (BOOL)removeFiles:(NSArray *)files
fromDirectory:(NSString *)directory
removeDirsIfEmpty:(BOOL)removeDirs
{
NSEnumerator *filesEnum = nil;
NSString *file = nil;
if (!files)
{
return NO;
}
filesEnum = [files objectEnumerator];
while ((file = [filesEnum nextObject]))
{
if ([self removeFile:file
fromDirectory:directory
removeDirsIfEmpty:removeDirs] == NO)
{
return NO;
}
}
return YES;
}
- (BOOL)moveFile:(NSString *)file intoDirectory:(NSString *)directory
{
if ([self copyFile:file intoDirectory:directory] == YES)
{
[self removeFileAtPath:file removeDirsIfEmpty:YES];
}
else
{
NSRunAlertPanel(@"Move File",
@"Couldn't move file %@ to %@",
@"Ok",nil,nil, file, directory);
return NO;
}
return YES;
}
// ===========================================================================
// ==== Find Executable
// Tries to find the first matching executable tool fromt he given, nil-terminated
// list. Returns the full path for it.
// ===========================================================================
- (NSString*) findExecutableToolFrom: (NSArray*)candidates
{
NSFileManager *manager;
NSEnumerator *pathEnumerator;
NSString *directory;
manager = [NSFileManager defaultManager];
pathEnumerator = [NSSearchPathForDirectoriesInDomains(NSDeveloperDirectory, NSAllDomainsMask, YES) objectEnumerator];
while (nil != (directory = [pathEnumerator nextObject]))
{
NSEnumerator *candidateEnumerator = [candidates objectEnumerator];
NSString *candidate;
while (nil != (candidate = [candidateEnumerator nextObject]))
{
NSString *path = [directory stringByAppendingPathComponent: candidate];
NSLog(@"final candidate path is: %@", path);
if ([manager isExecutableFileAtPath: path])
{
return path;
}
}
}
return nil;
}
@end
@implementation PCFileManager (UInterface)
// ===========================================================================
// ==== Panels
// ===========================================================================
- (id)_panelForOperation:(int)op
title:(NSString *)title
accView:(NSView *)accessoryView
{
id <PCPreferences> prefs = [projectManager prefController];
NSString *lastOpenDir;
id panel;
operation = op;
switch (op)
{
case PCOpenFileOperation:
panel = [NSOpenPanel openPanel];
[panel setCanChooseFiles:YES];
[panel setCanChooseDirectories:NO];
lastOpenDir = [prefs stringForKey:@"FileOpenLastDirectory"];
break;
case PCSaveFileOperation:
panel = [NSSavePanel savePanel];
lastOpenDir = [prefs stringForKey:@"FileSaveLastDirectory"];
break;
case PCOpenProjectOperation:
panel = [NSOpenPanel openPanel];
[panel setAllowsMultipleSelection:NO];
[panel setCanChooseFiles:YES];
[panel setCanChooseDirectories:YES];
lastOpenDir = [prefs stringForKey:@"ProjectOpenLastDirectory"];
break;
case PCOpenDirectoryOperation:
panel = [NSOpenPanel openPanel];
[panel setCanChooseFiles:NO];
[panel setCanChooseDirectories:YES];
lastOpenDir = [prefs stringForKey:@"FileOpenLastDirectory"];
break;
case PCAddFileOperation:
if (addFilesPanel == nil)
{
addFilesPanel = [PCAddFilesPanel addFilesPanel];
[addFilesPanel setTreatsFilePackagesAsDirectories: YES];
}
panel = addFilesPanel;
lastOpenDir = [prefs stringForKey:@"FileAddLastDirectory"];
break;
default:
return nil;
break;
}
if (!lastOpenDir)
{
lastOpenDir = NSHomeDirectory();
}
[panel setDirectory:lastOpenDir];
[panel setDelegate:self];
if (title != nil)
{
[panel setTitle:title];
}
if (accessoryView != nil)
{
[panel setAccessoryView:accessoryView];
}
return panel;
}
- (void)_saveLastDirectoryForPanel:(id)panel
{
id <PCPreferences> prefs = [projectManager prefController];
NSString *key = nil;
switch (operation)
{
case PCOpenFileOperation:
key = @"FileOpenLastDirectory";
break;
case PCSaveFileOperation:
key = @"FileSaveLastDirectory";
break;
case PCOpenProjectOperation:
key = @"ProjectOpenLastDirectory";
break;
case PCAddFileOperation:
key = @"FileAddLastDirectory";
break;
default:
break;
}
if (key != nil)
{
[prefs setString:[panel directory] forKey:key notify:NO];
}
}
- (NSMutableArray *)filesOfTypes:(NSArray *)types
operation:(int)op
multiple:(BOOL)yn
title:(NSString *)title
accView:(NSView *)accessoryView
{
id panel;
NSMutableArray *fileList = [[NSMutableArray alloc] init];
int result = -10;
panel = [self _panelForOperation:op title:title accView:accessoryView];
if (types != nil)
{
[panel setAllowedFileTypes:types];
}
if ((op == PCOpenFileOperation) ||
(op == PCOpenProjectOperation) ||
(op == PCOpenDirectoryOperation))
{
if ((result = [panel runModalForTypes:types]) == NSOKButton)
{
[fileList addObjectsFromArray:[panel filenames]];
}
}
else if (op == PCSaveFileOperation)
{
if ((result = [panel runModal]) == NSOKButton)
{
[fileList addObject:[panel filename]];
}
}
else if (op == PCAddFileOperation)
{
PCProject *project = [projectManager activeProject];
NSString *selectedCategory = nil;
[panel setCategories:[project rootCategories]];
selectedCategory = [[project projectBrowser] nameOfSelectedCategory];
[panel selectCategory:selectedCategory];
if ((result = [panel runModalForTypes:types]) == NSOKButton)
{
[fileList addObjectsFromArray:[panel filenames]];
}
}
if (result == NSOKButton)
{
[self _saveLastDirectoryForPanel:panel];
return [fileList autorelease];
}
return nil;
}
// ============================================================================
// ==== PCAddFilesPanel delegate
// ============================================================================
- (void)categoryChangedTo:(NSString *)category
{
PCProject *project = [projectManager activeProject];
NSArray *fileTypes = nil;
PCProjectBrowser *browser = [project projectBrowser];
NSString *path = [browser path];
[addFilesPanel setTitle:[NSString stringWithFormat:@"Add %@",category]];
fileTypes = [project
fileTypesForCategoryKey:[project keyForCategory:category]];
[addFilesPanel setFileTypes:fileTypes];
// Set project browser path
path = [path stringByDeletingLastPathComponent];
path = [path stringByAppendingPathComponent:category];
[browser setPath:path];
}
// ============================================================================
// ==== NSOpenPanel and NSSavePanel delegate
// ============================================================================
// If file name already in project -- don't show it!
- (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename
{
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir;
PCProject *project = nil;
NSArray *fileTypes = nil;
NSString *category = nil;
NSString *categoryKey = nil;
[fileManager fileExistsAtPath:filename isDirectory:&isDir];
if ([[filename pathExtension] isEqualToString:@"gorm"])
{
isDir = NO;
}
if (sender == addFilesPanel && !isDir)
{
project = [projectManager activeProject];
category = [addFilesPanel selectedCategory];
categoryKey = [project keyForCategory:category];
fileTypes = [project fileTypesForCategoryKey:categoryKey];
// Wrong file extension
if (fileTypes
&& ![fileTypes containsObject:[filename pathExtension]])
{
return NO;
}
// File is already in project
if (![project doesAcceptFile:filename forKey:categoryKey])
{
return NO;
}
}
return YES;
}
// Test if we should accept file name selected or entered
- (BOOL)panel:(id)sender isValidFilename:(NSString *)filename
{
NSFileManager *fm = [NSFileManager defaultManager];
BOOL isDir;
NSEnumerator *e = nil;
NSArray *tempList = nil;
NSString *tempExtension = nil;
if (operation == PCOpenProjectOperation)
{
if ([fm fileExistsAtPath:filename isDirectory:&isDir] && isDir)
{
e = [[sender allowedFileTypes] objectEnumerator];
while ((tempExtension = [e nextObject]) != nil)
{
tempList = [self filesWithExtension:tempExtension
atPath:filename
includeDirs:YES];
if ([tempList count] > 0)
{
return YES;
}
}
return NO;
}
}
return YES;
}
@end
@implementation PCFileManager (Misc)
/**
* Returns YES if the file identified by `filename' is a text file,
* otherwise returns NO.
*
* The test is one by reading the first 512 bytes of the file
* and checking whether at least 90% of the data are printable
* ASCII characters.
*
* Author Saso Kiselkov
*/
- (BOOL)isTextFile:(NSString *)filename
{
NSFileHandle *fh;
NSData *data;
NSUInteger i, printable = 0;
NSString *content;
NSCharacterSet *alpha = [NSCharacterSet alphanumericCharacterSet];
NSCharacterSet *spaces = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSCharacterSet *marks = [NSCharacterSet punctuationCharacterSet];
fh = [NSFileHandle fileHandleForReadingAtPath:filename];
if (fh == nil)
{
return NO;
}
data = [fh readDataOfLength:512];
if ([data length] == 0)
{
return YES;
}
content = [NSString stringWithContentsOfFile: filename];
for (i = 0; i < [content length]; i++)
{
if ([alpha characterIsMember: [content characterAtIndex: i]] ||
[spaces characterIsMember: [content characterAtIndex: i]] ||
[marks characterIsMember: [content characterAtIndex: i]])
{
printable++;
}
}
return (((double) printable / i) > 0.9);
}
- (NSArray *)filesWithExtension:(NSString *)extension
atPath:(NSString *)dirPath
includeDirs:(BOOL)incDirs
{
NSFileManager *fm = [NSFileManager defaultManager];
NSMutableArray *filesList = [[NSMutableArray alloc] init];
NSEnumerator *e = nil;
NSString *temp = nil;
BOOL isDir;
e = [[fm directoryContentsAtPath:dirPath] objectEnumerator];
while ((temp = [e nextObject]) != nil)
{
if ([fm fileExistsAtPath:temp isDirectory:&isDir] && isDir && !incDirs)
{
continue;
}
if ([[temp pathExtension] isEqual:extension])
{
[filesList addObject:[dirPath stringByAppendingPathComponent:temp]];
}
}
return [filesList autorelease];
}
@end
|