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 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
|
/* Finder.m
*
* Copyright (C) 2005-2018 Free Software Foundation, Inc.
*
* Author: Enrico Sersale <enrico@imago.ro>
* Riccardo Mottola <rm@gnu.org>
* Date: January 2005
*
* This file is part of the GNUstep GWorkspace application
*
* This program 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 program 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 this program; if not, write to the Free Software
* Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
*/
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import "Finder.h"
#import "FinderModulesProtocol.h"
#import "FindModuleView.h"
#import "SearchPlacesBox.h"
#import "SearchPlacesCell.h"
#import "SearchResults.h"
#import "LSFolder.h"
#import "FSNodeRep.h"
#import "GWorkspace.h"
#import "GWFunctions.h"
#define WINH (262.0)
#define FMVIEWH (34.0)
#define BORDER (4.0)
#define HMARGIN (12.0)
#define SELECTION 0
#define PLACES 1
#define CELLS_HEIGHT (28.0)
#define CHECKSIZE(sz) \
if (sz.width < 0) sz.width = 0; \
if (sz.height < 0) sz.height = 0
static NSString *nibName = @"Finder";
static Finder *finder = nil;
@implementation Finder
+ (Finder *)finder
{
if (finder == nil) {
finder = [[Finder alloc] init];
}
return finder;
}
- (void)dealloc
{
[nc removeObserver: self];
RELEASE (modules);
RELEASE (fmviews);
RELEASE (currentSelection);
RELEASE (win);
RELEASE (searchResults);
RELEASE (lsFolders);
[super dealloc];
}
- (id)init
{
self = [super init];
if (self) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
id defentry;
NSArray *usedModules;
NSRect rect;
NSSize cs, ms;
NSUInteger i;
if ([NSBundle loadNibNamed: nibName owner: self] == NO) {
NSLog(@"failed to load %@!", nibName);
DESTROY (self);
return self;
}
fmviews = [NSMutableArray new];
modules = nil;
currentSelection = nil;
searchResults = [NSMutableArray new];
lsFolders = [NSMutableArray new];
fm = [NSFileManager defaultManager];
nc = [NSNotificationCenter defaultCenter];
ws = [NSWorkspace sharedWorkspace];
gworkspace = [GWorkspace gworkspace];
[win setTitle: NSLocalizedString(@"Finder", @"")];
[win setDelegate: self];
[win setFrameUsingName: @"finder" force: YES];
[placesBox setFinder: self];
rect = [[(NSBox *)placesBox contentView] bounds];
placesScroll = [[NSScrollView alloc] initWithFrame: rect];
[placesScroll setBorderType: NSBezelBorder];
[placesScroll setHasHorizontalScroller: NO];
[placesScroll setHasVerticalScroller: YES];
[(NSBox *)placesBox setContentView: placesScroll];
RELEASE (placesScroll);
placesMatrix = [[NSMatrix alloc] initWithFrame: NSMakeRect(0, 0, 100, 100)
mode: NSListModeMatrix
prototype: [[SearchPlacesCell new] autorelease]
numberOfRows: 0
numberOfColumns: 0];
[placesMatrix setTarget: self];
[placesMatrix setAction: @selector(placesMatrixAction:)];
[placesMatrix setIntercellSpacing: NSZeroSize];
[placesMatrix setCellSize: NSMakeSize(1, CELLS_HEIGHT)];
[placesMatrix setAutoscroll: YES];
[placesMatrix setAllowsEmptySelection: YES];
cs = [placesScroll contentSize];
ms = [placesMatrix cellSize];
ms.width = cs.width;
CHECKSIZE (ms);
[placesMatrix setCellSize: ms];
[placesScroll setDocumentView: placesMatrix];
RELEASE (placesMatrix);
defentry = [defaults objectForKey: @"saved_places"];
if (defentry && [defentry isKindOfClass: [NSArray class]]) {
for (i = 0; i < [defentry count]; i++) {
NSString *path = [defentry objectAtIndex: i];
if ([fm fileExistsAtPath: path]) {
[self addSearchPlaceWithPath: path];
}
}
}
[removePlaceButt setEnabled: ([[placesMatrix cells] count] != 0)];
[self loadModules];
usedModules = [self usedModules];
for (i = 0; i < [usedModules count]; i++) {
id module = [usedModules objectAtIndex: i];
id fmview = [[FindModuleView alloc] initWithDelegate: self];
[fmview setModule: module];
if ([usedModules count] == [modules count]) {
[fmview setAddEnabled: NO];
}
[[modulesBox contentView] addSubview: [fmview mainBox]];
[fmviews insertObject: fmview atIndex: [fmviews count]];
RELEASE (fmview);
}
for (i = 0; i < [fmviews count]; i++) {
[[fmviews objectAtIndex: i] updateMenuForModules: modules];
}
[recursiveSwitch setState: NSOnState];
defentry = [defaults objectForKey: @"search_res_h"];
if (defentry) {
searchResh = [defentry intValue];
} else {
searchResh = 0;
}
defentry = [defaults objectForKey: @"lsfolders_paths"];
if (defentry) {
for (i = 0; i < [defentry count]; i++) {
NSString *lsfpath = [defentry objectAtIndex: i];
if ([fm fileExistsAtPath: lsfpath]) {
if ([self addLiveSearchFolderWithPath: lsfpath createIndex: NO] != nil) {
GWDebugLog(@"added lsf with path %@", lsfpath);
}
}
}
}
[nc addObserver: self
selector: @selector(fileSystemWillChange:)
name: @"GWFileSystemWillChangeNotification"
object: nil];
[nc addObserver: self
selector: @selector(fileSystemDidChange:)
name: @"GWFileSystemDidChangeNotification"
object: nil];
[nc addObserver: self
selector: @selector(watcherNotification:)
name: @"GWFileWatcherFileDidChangeNotification"
object: nil];
/* Internationalization */
[searchLabel setStringValue: NSLocalizedString(@"Search in:", @"")];
[wherePopUp removeAllItems];
[wherePopUp insertItemWithTitle: NSLocalizedString(@"Current selection", @"")
atIndex: SELECTION];
[wherePopUp insertItemWithTitle: NSLocalizedString(@"Specific places", @"")
atIndex: PLACES];
[addPlaceButt setTitle: NSLocalizedString(@"Add", @"")];
[removePlaceButt setTitle: NSLocalizedString(@"Remove", @"")];
[itemsLabel setStringValue: NSLocalizedString(@"Search for items whose:", @"")];
[recursiveSwitch setTitle: NSLocalizedString(@"Recursive", @"")];
[findButt setTitle: NSLocalizedString(@"Search", @"")];
usesSearchPlaces = [defaults boolForKey: @"uses_search_places"];
if (usesSearchPlaces) {
[wherePopUp selectItemAtIndex: PLACES];
} else {
[wherePopUp selectItemAtIndex: SELECTION];
}
[self chooseSearchPlacesType: wherePopUp];
}
return self;
}
- (void)activate
{
[win makeKeyAndOrderFront: nil];
[self tile];
}
- (void)loadModules
{
NSString *bundlesDir;
NSEnumerator *enumerator;
NSString *path;
NSMutableArray *bundlesPaths;
NSMutableArray *unsortedModules;
NSDictionary *lastUsedModules;
NSArray *usedNames;
NSUInteger index;
NSUInteger i;
bundlesPaths = [NSMutableArray array];
enumerator = [NSSearchPathForDirectoriesInDomains
(NSLibraryDirectory, NSAllDomainsMask, YES) objectEnumerator];
while ((bundlesDir = [enumerator nextObject]) != nil)
{
NSEnumerator *enumerator;
bundlesDir = [bundlesDir stringByAppendingPathComponent: @"Bundles"];
enumerator = [[fm directoryContentsAtPath: bundlesDir] objectEnumerator];
while ((path = [enumerator nextObject])) {
if ([[path pathExtension] isEqual: @"finder"]) {
[bundlesPaths addObject:
[bundlesDir stringByAppendingPathComponent: path]];
}
}
}
unsortedModules = [NSMutableArray array];
for (i = 0; i < [bundlesPaths count]; i++)
{
CREATE_AUTORELEASE_POOL(arp);
NSString *bpath = [bundlesPaths objectAtIndex: i];
NSBundle *bundle = [NSBundle bundleWithPath: bpath];
if (bundle)
{
Class principalClass = [bundle principalClass];
if ([principalClass conformsToProtocol: @protocol(FinderModulesProtocol)]) {
id module = [[principalClass alloc] initInterface];
[unsortedModules addObject: module];
RELEASE ((id)module);
}
}
RELEASE (arp);
}
if ([unsortedModules count] == 0) {
NSRunAlertPanel(NSLocalizedString(@"error", @""),
NSLocalizedString(@"No Finder modules! Quitting now.", @""),
NSLocalizedString(@"OK", @""), nil, nil);
[NSApp terminate: self];
}
lastUsedModules = [[NSUserDefaults standardUserDefaults] objectForKey: @"last_used_modules"];
if ((lastUsedModules == nil) || ([lastUsedModules count] == 0)) {
lastUsedModules = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt: 0], NSLocalizedString(@"name", @""),
[NSNumber numberWithInt: 1], NSLocalizedString(@"kind", @""),
[NSNumber numberWithInt: 2], NSLocalizedString(@"size", @""),
[NSNumber numberWithInt: 3], NSLocalizedString(@"owner", @""),
[NSNumber numberWithInt: 4], NSLocalizedString(@"date created", @""),
[NSNumber numberWithInt: 5], NSLocalizedString(@"date modified", @""),
[NSNumber numberWithInt: 6], NSLocalizedString(@"contents", @""),
nil];
usedNames = [NSArray arrayWithObject: NSLocalizedString(@"name", @"")];
} else {
usedNames = [lastUsedModules allKeys];
}
index = [usedNames count];
for (i = 0; i < [unsortedModules count]; i++) {
id module = [unsortedModules objectAtIndex: i];
NSString *mname = [module moduleName];
NSNumber *num = [lastUsedModules objectForKey: mname];
if (num) {
[module setIndex: [num intValue]];
[module setInUse: [usedNames containsObject: mname]];
} else {
[module setIndex: index];
[module setInUse: NO];
index++;
}
}
modules = [[unsortedModules sortedArrayUsingSelector: @selector(compareModule:)] mutableCopy];
}
- (NSArray *)modules
{
return modules;
}
- (NSArray *)usedModules
{
NSMutableArray *used = [NSMutableArray array];
NSUInteger i;
for (i = 0; i < [modules count]; i++) {
id module = [modules objectAtIndex: i];
if ([module used]) {
[used addObject: module];
}
}
return used;
}
- (id)firstUnusedModule
{
NSUInteger i;
for (i = 0; i < [modules count]; i++) {
id module = [modules objectAtIndex: i];
if ([module used] == NO) {
return module;
}
}
return nil;
}
- (id)moduleWithName:(NSString *)mname
{
NSUInteger i;
for (i = 0; i < [modules count]; i++) {
id module = [modules objectAtIndex: i];
if ([[module moduleName] isEqual: mname]) {
return module;
}
}
return nil;
}
- (void)addModule:(FindModuleView *)aview
{
NSArray *usedModules = [self usedModules];
if ([usedModules count] < [modules count]) {
NSUInteger index = [fmviews indexOfObjectIdenticalTo: aview];
id module = [self firstUnusedModule];
id fmview = [[FindModuleView alloc] initWithDelegate: self];
NSUInteger count;
NSUInteger i;
[module setInUse: YES];
[fmview setModule: module];
[[modulesBox contentView] addSubview: [fmview mainBox]];
[fmviews insertObject: fmview atIndex: index + 1];
RELEASE (fmview);
count = [fmviews count];
for (i = 0; i < count; i++) {
fmview = [fmviews objectAtIndex: i];
[fmview updateMenuForModules: modules];
if (count == [modules count]) {
[fmview setAddEnabled: NO];
}
if (count > 1) {
[fmview setRemoveEnabled: YES];
}
}
[self tile];
}
}
- (void)removeModule:(FindModuleView *)aview
{
if ([fmviews count] > 1) {
NSUInteger count;
NSUInteger i;
[[aview module] setInUse: NO];
[[aview mainBox] removeFromSuperview];
[fmviews removeObject: aview];
count = [fmviews count];
for (i = 0; i < count; i++) {
id fmview = [fmviews objectAtIndex: i];
[fmview updateMenuForModules: modules];
[fmview setAddEnabled: YES];
if (count == 1) {
[fmview setRemoveEnabled: NO];
}
}
[self tile];
}
}
- (void)findModuleView:(FindModuleView *)aview
changeModuleTo:(NSString *)mname
{
id module = [self moduleWithName: mname];
if (module && ([aview module] != module)) {
NSUInteger i;
[[aview module] setInUse: NO];
[module setInUse: YES];
[aview setModule: module];
for (i = 0; i < [fmviews count]; i++) {
[[fmviews objectAtIndex: i] updateMenuForModules: modules];
}
}
}
- (IBAction)chooseSearchPlacesType:(id)sender
{
NSArray *cells = [placesMatrix cells];
NSUInteger i;
usesSearchPlaces = ([sender indexOfSelectedItem] == PLACES);
for (i = 0; i < [cells count]; i++) {
[[cells objectAtIndex: i] setEnabled: usesSearchPlaces];
}
[placesMatrix deselectAllCells];
[placesMatrix setNeedsDisplay: YES];
[addPlaceButt setEnabled: usesSearchPlaces];
[removePlaceButt setEnabled: NO];
}
- (NSDragOperation)draggingEnteredInSearchPlaces:(id <NSDraggingInfo>)sender
{
if (usesSearchPlaces)
{
NSPasteboard *pb = [sender draggingPasteboard];
splacesDndTarget = NO;
if ([[pb types] containsObject: NSFilenamesPboardType])
{
NSArray *sourcePaths = [pb propertyListForType: NSFilenamesPboardType];
NSArray *cells = [placesMatrix cells];
NSUInteger count = [sourcePaths count];
NSUInteger i;
if (count == 0)
return NSDragOperationNone;
for (i = 0; i < [cells count]; i++)
{
SearchPlacesCell *cell = [cells objectAtIndex: i];
if ([sourcePaths containsObject: [cell path]])
return NSDragOperationNone;
}
splacesDndTarget = YES;
return [sender draggingSourceOperationMask];
}
}
return NSDragOperationNone;
}
- (NSDragOperation)draggingUpdatedInSearchPlaces:(id <NSDraggingInfo>)sender
{
if (splacesDndTarget && usesSearchPlaces) {
return [sender draggingSourceOperationMask];
}
return NSDragOperationNone;
}
- (void)concludeDragOperationInSearchPlaces:(id <NSDraggingInfo>)sender
{
if (usesSearchPlaces) {
NSPasteboard *pb = [sender draggingPasteboard];
if ([[pb types] containsObject: NSFilenamesPboardType]) {
NSArray *sourcePaths = [pb propertyListForType: NSFilenamesPboardType];
NSUInteger i;
for (i = 0; i < [sourcePaths count]; i++) {
[self addSearchPlaceWithPath: [sourcePaths objectAtIndex: i]];
}
}
}
splacesDndTarget = NO;
}
- (IBAction)addSearchPlaceFromDialog:(id)sender
{
NSOpenPanel *openPanel;
NSArray *filenames;
NSInteger result;
NSUInteger i;
openPanel = [NSOpenPanel openPanel];
[openPanel setTitle: NSLocalizedString(@"open", @"")];
[openPanel setAllowsMultipleSelection: YES];
[openPanel setCanChooseFiles: YES];
[openPanel setCanChooseDirectories: YES];
result = [openPanel runModalForDirectory: systemRoot()
file: nil
types: nil];
if (result != NSOKButton)
return;
filenames = [openPanel filenames];
for (i = 0; i < [filenames count]; i++)
{
[self addSearchPlaceWithPath: [filenames objectAtIndex: i]];
}
}
- (void)addSearchPlaceWithPath:(NSString *)spath
{
NSArray *cells = [placesMatrix cells];
NSUInteger count = [cells count];
BOOL found = NO;
NSUInteger i;
for (i = 0; i < [cells count]; i++) {
NSString *srchpath = [[cells objectAtIndex: i] path];
if ([srchpath isEqual: spath]) {
found = YES;
break;
}
}
if (found == NO) {
FSNode *node = [FSNode nodeWithPath: spath];
SEL compareSel = [[FSNodeRep sharedInstance] defaultCompareSelector];
SearchPlacesCell *cell;
[placesMatrix insertRow: count];
cell = [placesMatrix cellAtRow: count column: 0];
[cell setNode: node];
[cell setLeaf: YES];
[cell setIcon];
[placesMatrix sortUsingSelector: compareSel];
[self adjustMatrix];
}
}
- (void)placesMatrixAction:(id)sender
{
if ([[placesMatrix cells] count] && usesSearchPlaces) {
[removePlaceButt setEnabled: YES];
}
}
- (IBAction)removeSearchPlaceButtAction:(id)sender
{
NSArray *cells = [placesMatrix selectedCells];
NSUInteger i;
for (i = 0; i < [cells count]; i++) {
[self removeSearchPlaceWithPath: [[cells objectAtIndex: i] path]];
}
}
- (void)removeSearchPlaceWithPath:(NSString *)spath
{
NSArray *cells = [placesMatrix cells];
NSUInteger i;
for (i = 0; i < [cells count]; i++) {
SearchPlacesCell *cell = [cells objectAtIndex: i];
if ([[cell path] isEqual: spath]) {
NSInteger row, col;
[placesMatrix getRow: &row column: &col ofCell: cell];
[placesMatrix removeRow: row];
[self adjustMatrix];
// TODO - STOP SEARCHING IN THIS PATH !
break;
}
}
if ([[placesMatrix cells] count] == 0) {
[removePlaceButt setEnabled: NO];
}
}
- (NSArray *)searchPlacesPaths
{
NSArray *cells = [placesMatrix cells];
if (cells && [cells count]) {
NSMutableArray *paths = [NSMutableArray array];
NSUInteger i;
for (i = 0; i < [cells count]; i++) {
[paths addObject: [[cells objectAtIndex: i] path]];
}
return paths;
}
return [NSArray array];
}
- (NSArray *)selectedSearchPlacesPaths
{
NSArray *cells = [placesMatrix selectedCells];
if (cells && [cells count]) {
NSMutableArray *paths = [NSMutableArray array];
NSUInteger i;
RETAIN (cells);
for (i = 0; i < [cells count]; i++) {
NSString *path = [[cells objectAtIndex: i] path];
if ([fm fileExistsAtPath: path]) {
[paths addObject: path];
} else {
[self removeSearchPlaceWithPath: path];
}
}
RELEASE (cells);
return paths;
}
return nil;
}
- (void)setCurrentSelection:(NSArray *)paths
{
NSString *elmstr = NSLocalizedString(@"elements", @"");
NSString *title;
ASSIGN (currentSelection, paths);
if ([currentSelection count] == 1) {
title = [[currentSelection objectAtIndex: 0] lastPathComponent];
} else {
title = [NSString stringWithFormat: @"%lu %@", (unsigned long)[currentSelection count], elmstr];
}
[[wherePopUp itemAtIndex: SELECTION] setTitle: title];
[wherePopUp setNeedsDisplay: YES];
}
- (IBAction)startFind:(id)sender
{
NSMutableDictionary *criteria = [NSMutableDictionary dictionary];
NSArray *selection;
NSUInteger i;
if (usesSearchPlaces == NO) {
if (currentSelection == nil) {
NSRunAlertPanel(nil,
NSLocalizedString(@"No selection!", @""),
NSLocalizedString(@"OK", @""),
nil,
nil);
return;
} else {
selection = currentSelection;
}
} else {
selection = [self selectedSearchPlacesPaths];
if (selection == nil) {
NSRunAlertPanel(nil,
NSLocalizedString(@"No selection!", @""),
NSLocalizedString(@"OK", @""),
nil,
nil);
return;
}
}
for (i = 0; i < [fmviews count]; i++) {
id module = [[fmviews objectAtIndex: i] module];
NSDictionary *dict = [module searchCriteria];
if (dict) {
[criteria setObject: dict
forKey: NSStringFromClass([module class])];
}
}
if ([criteria count]) {
SearchResults *results = [SearchResults new];
[searchResults addObject: results];
RELEASE (results);
[results activateForSelection: selection
withSearchCriteria: criteria
recursive: ([recursiveSwitch state] == NSOnState)];
} else {
NSRunAlertPanel(nil,
NSLocalizedString(@"No search criteria!", @""),
NSLocalizedString(@"OK", @""),
nil,
nil);
}
}
- (void)stopAllSearchs
{
NSUInteger i;
for (i = 0; i < [searchResults count]; i++) {
SearchResults *results = [searchResults objectAtIndex: i];
[results stopSearch: nil];
if ([[results win] isVisible]) {
[[results win] close];
}
}
}
- (id)resultWithAddress:(unsigned long)address
{
NSUInteger i;
for (i = 0; i < [searchResults count]; i++) {
SearchResults *results = [searchResults objectAtIndex: i];
if ([results memAddress] == address) {
return results;
}
}
return nil;
}
- (void)resultsWindowWillClose:(id)results
{
[searchResults removeObject: results];
}
- (void)setSearchResultsHeight:(int)srh
{
searchResh = srh;
}
- (int)searchResultsHeight
{
return searchResh;
}
- (void)foundSelectionChanged:(NSArray *)selected
{
[gworkspace selectionChanged: selected];
}
- (void)openFoundSelection:(NSArray *)selection
{
NSUInteger i;
for (i = 0; i < [selection count]; i++) {
FSNode *node = [selection objectAtIndex: i];
if ([node isDirectory] || [node isMountPoint]) {
if ([node isApplication]) {
[ws launchApplication: [node path]];
} else if ([node isPackage]) {
[gworkspace openFile: [node path]];
} else {
[gworkspace newViewerAtPath: [node path]];
}
} else if ([node isPlain] || [node isExecutable]) {
[gworkspace openFile: [node path]];
} else if ([node isApplication]) {
[ws launchApplication: [node path]];
}
}
}
- (void)fileSystemWillChange:(NSNotification *)notif
{
NSDictionary *info = [notif object];
NSUInteger i;
for (i = 0; i < [lsFolders count]; i++) {
LSFolder *folder = [lsFolders objectAtIndex: i];
FSNode *node = [folder node];
if ([node involvedByFileOperation: info]) {
[gworkspace removeWatcherForPath: [node path]];
[folder setWatcherSuspended: YES];
}
}
}
- (void)fileSystemDidChange:(NSNotification *)notif
{
CREATE_AUTORELEASE_POOL(arp);
NSDictionary *info = [notif object];
NSString *operation = [info objectForKey: @"operation"];
NSString *source = [info objectForKey: @"source"];
NSString *destination = [info objectForKey: @"destination"];
NSArray *files = [info objectForKey: @"files"];
NSArray *origfiles = [info objectForKey: @"origfiles"];
NSMutableArray *srcpaths = [NSMutableArray array];
NSMutableArray *dstpaths = [NSMutableArray array];
BOOL copy, move, remove;
NSUInteger i, j, count;
if ([operation isEqual: @"GWorkspaceRenameOperation"])
{
srcpaths = [NSMutableArray arrayWithObject: source];
dstpaths = [NSMutableArray arrayWithObject: destination];
}
else
{
if ([operation isEqual: NSWorkspaceDuplicateOperation]
|| [operation isEqual: NSWorkspaceRecycleOperation])
{
for (i = 0; i < [files count]; i++) {
NSString *fname = [origfiles objectAtIndex: i];
[srcpaths addObject: [source stringByAppendingPathComponent: fname]];
fname = [files objectAtIndex: i];
[dstpaths addObject: [destination stringByAppendingPathComponent: fname]];
}
}
else
{
for (i = 0; i < [files count]; i++)
{
NSString *fname = [files objectAtIndex: i];
[srcpaths addObject: [source stringByAppendingPathComponent: fname]];
if (destination != nil)
[dstpaths addObject: [destination stringByAppendingPathComponent: fname]];
}
}
}
copy = ([operation isEqual: NSWorkspaceCopyOperation]
|| [operation isEqual: NSWorkspaceDuplicateOperation]);
move = ([operation isEqual: NSWorkspaceMoveOperation]
|| [operation isEqual: @"GWorkspaceRenameOperation"]);
remove = ([operation isEqual: NSWorkspaceDestroyOperation]
|| [operation isEqual: NSWorkspaceRecycleOperation]);
// Search Places
if (move || remove) {
NSArray *placesPaths = [self searchPlacesPaths];
NSMutableArray *deletedPlaces = [NSMutableArray array];
for (i = 0; i < [srcpaths count]; i++) {
NSString *srcpath = [srcpaths objectAtIndex: i];
for (j = 0; j < [placesPaths count]; j++) {
NSString *path = [placesPaths objectAtIndex: j];
if ([path isEqual: srcpath] || subPathOfPath(srcpath, path)) {
[deletedPlaces addObject: path];
}
}
}
if ([deletedPlaces count]) {
for (i = 0; i < [deletedPlaces count]; i++) {
[self removeSearchPlaceWithPath: [deletedPlaces objectAtIndex: i]];
}
}
}
// LSFolders
count = [lsFolders count];
for (i = 0; i < count; i++) {
LSFolder *folder = [lsFolders objectAtIndex: i];
FSNode *node = [folder node];
FSNode *newnode = nil;
BOOL found = NO;
for (j = 0; j < [srcpaths count]; j++) {
NSString *srcpath = [srcpaths objectAtIndex: j];
NSString *dstpath = [dstpaths objectAtIndex: j];
if (move || copy) {
if ([[node path] isEqual: srcpath]) {
if ([fm fileExistsAtPath: dstpath]) {
newnode = [FSNode nodeWithPath: dstpath];
found = YES;
}
break;
} else if ([node isSubnodeOfPath: srcpath]) {
NSString *newpath = pathRemovingPrefix([node path], srcpath);
newpath = [dstpath stringByAppendingPathComponent: newpath];
if ([fm fileExistsAtPath: newpath]) {
newnode = [FSNode nodeWithPath: newpath];
found = YES;
}
break;
}
} else if (remove) {
if ([[node path] isEqual: srcpath] || [node isSubnodeOfPath: srcpath]) {
found = YES;
break;
}
}
}
[folder setWatcherSuspended: NO];
if (found) {
if (move) {
GWDebugLog(@"moved lsf with path %@ to path %@", [node path], [newnode path]);
[folder setNode: newnode];
} else if (copy) {
[self addLiveSearchFolderWithPath: [newnode path] createIndex: NO];
GWDebugLog(@"added lsf with path %@", [newnode path]);
} else if (remove) {
GWDebugLog(@"removed lsf with path %@", [node path]);
[self removeLiveSearchFolder: folder];
count--;
i--;
}
}
}
RELEASE (arp);
}
- (void)watcherNotification:(NSNotification *)notif
{
NSDictionary *info = [notif object];
NSString *event = [info objectForKey: @"event"];
if ([event isEqual: @"GWWatchedPathDeleted"]) {
LSFolder *folder = [self lsfolderWithPath: [info objectForKey: @"path"]];
if (folder) {
GWDebugLog(@"removed (watcher) lsf with path %@", [[folder node] path]);
[self removeLiveSearchFolder: folder];
}
}
}
- (void)tile
{
NSRect wrect = [win frame];
NSRect mbrect = [modulesBox bounds];
NSUInteger count = [fmviews count];
CGFloat hspace = (count * FMVIEWH) + HMARGIN + BORDER;
NSUInteger i;
if (mbrect.size.height != hspace) {
if (wrect.size.height != WINH) {
wrect.origin.y -= (hspace - mbrect.size.height);
}
wrect.size.height += (hspace - mbrect.size.height);
[win setFrame: wrect display: NO];
}
mbrect = [modulesBox bounds];
for (i = 0; i < count; i++) {
FindModuleView *fmview = [fmviews objectAtIndex: i];
NSBox *fmbox = [fmview mainBox];
NSRect mbr = [fmbox frame];
CGFloat posy = mbrect.size.height - (FMVIEWH * (i + 1)) - BORDER;
if (mbr.origin.y != posy) {
mbr.origin.y = posy;
[fmbox setFrame: mbr];
}
}
}
- (void)adjustMatrix
{
[placesMatrix setCellSize: NSMakeSize([placesScroll contentSize].width, CELLS_HEIGHT)];
[placesMatrix sizeToCells];
[placesMatrix setNeedsDisplay: YES];
}
- (void)updateDefaults
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSArray *cells = [placesMatrix cells];
NSMutableArray *savedPlaces = [NSMutableArray array];
NSMutableArray *lsfpaths = [NSMutableArray array];
NSUInteger i;
if (cells && [cells count]) {
for (i = 0; i < [cells count]; i++) {
NSString *srchpath = [[cells objectAtIndex: i] path];
if ([fm fileExistsAtPath: srchpath]) {
[savedPlaces addObject: srchpath];
}
}
}
[defaults setObject: savedPlaces forKey: @"saved_places"];
[defaults setBool: usesSearchPlaces forKey: @"uses_search_places"];
for (i = 0; i < [fmviews count]; i++) {
FindModuleView *fmview = [fmviews objectAtIndex: i];
id module = [fmview module];
[dict setObject: [NSNumber numberWithInt: i]
forKey: [module moduleName]];
}
[defaults setObject: dict forKey: @"last_used_modules"];
[defaults setObject: [NSNumber numberWithInt: searchResh]
forKey: @"search_res_h"];
for (i = 0; i < [lsFolders count]; i++) {
LSFolder *folder = [lsFolders objectAtIndex: i];
FSNode *node = [folder node];
if ([node isValid]) {
[lsfpaths addObject: [node path]];
}
}
[defaults setObject: lsfpaths forKey: @"lsfolders_paths"];
[win saveFrameUsingName: @"finder"];
}
- (BOOL)windowShouldClose:(id)sender
{
[win saveFrameUsingName: @"finder"];
return YES;
}
@end
@implementation Finder (LSFolders)
- (void)lsfolderDragOperation:(NSData *)opinfo
concludedAtPath:(NSString *)path
{
NSDictionary *dict = [NSUnarchiver unarchiveObjectWithData: opinfo];
unsigned long address = [[dict objectForKey: @"sender"] unsignedLongValue];
SearchResults *results = [self resultWithAddress: address];
if (results) {
[results createLiveSearchFolderAtPath: path];
}
}
- (BOOL)openLiveSearchFolderAtPath:(NSString *)path
{
LSFolder *folder = [self lsfolderWithPath: path];
if (folder == nil) {
folder = [self addLiveSearchFolderWithPath: path createIndex: NO];
}
if (folder) {
[folder updateIfNeeded: nil];
}
return YES;
}
- (LSFolder *)addLiveSearchFolderWithPath:(NSString *)path
createIndex:(BOOL)index
{
LSFolder *folder = [self lsfolderWithPath: path];
if (folder == nil) {
FSNode *node = [FSNode nodeWithPath: path];
folder = [[LSFolder alloc] initForFinder: self
withNode: node
needsIndexing: index];
if (folder) {
[lsFolders addObject: folder];
RELEASE (folder);
}
}
if (index) {
GWDebugLog(@"creating trees for lsf at %@", path);
}
return folder;
}
- (void)removeLiveSearchFolder:(LSFolder *)folder
{
[folder endUpdate];
[folder closeWindow];
[lsFolders removeObject: folder];
}
- (LSFolder *)lsfolderWithNode:(FSNode *)node
{
NSUInteger i;
for (i = 0; i < [lsFolders count]; i++) {
LSFolder *folder = [lsFolders objectAtIndex: i];
if ([[folder node] isEqual: node]) {
return folder;
}
}
return nil;
}
- (LSFolder *)lsfolderWithPath:(NSString *)path
{
NSUInteger i;
for (i = 0; i < [lsFolders count]; i++) {
LSFolder *folder = [lsFolders objectAtIndex: i];
if ([[[folder node] path] isEqual: path]) {
return folder;
}
}
return nil;
}
@end
@implementation NSDictionary (ColumnsSort)
- (int)compareColInfo:(NSDictionary *)dict
{
NSNumber *p1 = [self objectForKey: @"position"];
NSNumber *p2 = [dict objectForKey: @"position"];
return [p1 compare: p2];
}
@end
|