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
|
/* MDFinder.m
*
* Copyright (C) 2007-2018 Free Software Foundation, Inc.
*
* Author: Enrico Sersale <enrico@fibernet.ro>
* Date: January 2007
*
* 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 "MDFinder.h"
#import "MDKWindow.h"
#import "MDKQuery.h"
#import "FSNode.h"
static MDFinder *mdfinder = nil;
@implementation MDFinder
+ (MDFinder *)mdfinder;
{
if (mdfinder == nil) {
mdfinder = [[MDFinder alloc] init];
}
return mdfinder;
}
- (void)dealloc
{
DESTROY (workspaceApp);
RELEASE (mdkwindows);
RELEASE (lastSaveDir);
TEST_RELEASE (startAppWin);
[super dealloc];
}
- (id)init
{
self = [super init];
if (self) {
mdkwindows = [NSMutableArray new];
activeWindow = nil;
fm = [NSFileManager defaultManager];
nc = [NSNotificationCenter defaultCenter];
}
return self;
}
- (void)applicationWillFinishLaunching:(NSNotification *)aNotification
{
NSMenu *mainMenu = [NSMenu new];
NSMenu *menu;
NSMenu *windows, *services;
id<NSMenuItem> menuItem;
// Info
menuItem = addItemToMenu(mainMenu, @"Info", @"", nil, @"");
menu = AUTORELEASE ([NSMenu new]);
[mainMenu setSubmenu: menu forItem: menuItem];
addItemToMenu(menu, @"Activate context help", @"", @"activateContextHelp:", @";");
// File
menuItem = addItemToMenu(mainMenu, @"File", @"", nil, @"");
menu = AUTORELEASE ([NSMenu new]);
[mainMenu setSubmenu: menu forItem: menuItem];
addItemToMenu(menu, @"New", @"", @"newQuery:", @"n");
addItemToMenu(menu, @"Open...", @"", @"openQuery:", @"o");
addItemToMenu(menu, @"Save", @"", @"saveQuery:", @"s");
addItemToMenu(menu, @"Save as...", @"", @"saveQueryAs:", @"");
// Edit
menuItem = addItemToMenu(mainMenu, @"Edit", @"", nil, @"");
menu = AUTORELEASE ([NSMenu new]);
[mainMenu setSubmenu: menu forItem: menuItem];
addItemToMenu(menu, @"Cut", @"", @"cut:", @"x");
addItemToMenu(menu, @"Copy", @"", @"copy:", @"c");
addItemToMenu(menu, @"Paste", @"", @"paste:", @"v");
// Windows
menuItem = addItemToMenu(mainMenu, @"Windows", @"", nil, @"");
windows = AUTORELEASE ([NSMenu new]);
[mainMenu setSubmenu: windows forItem: menuItem];
addItemToMenu(windows, @"Arrange in Front", @"", nil, @"");
addItemToMenu(windows, @"Miniaturize Window", @"", nil, @"");
addItemToMenu(windows, @"Close Window", @"", @"closeMainWin:", @"w");
// Services
menuItem = addItemToMenu(mainMenu, @"Services", @"", nil, @"");
services = AUTORELEASE ([NSMenu new]);
[mainMenu setSubmenu: services forItem: menuItem];
// Hide
addItemToMenu(mainMenu, @"Hide", @"", @"hide:", @"h");
// Print
addItemToMenu(mainMenu, @"Print...", @"", @"print:", @"p");
// Quit
addItemToMenu(mainMenu, @"Quit", @"", @"terminate:", @"q");
[mainMenu update];
[NSApp setServicesMenu: services];
[NSApp setWindowsMenu: windows];
[NSApp setMainMenu: mainMenu];
RELEASE (mainMenu);
workspaceApp = nil;
startAppWin = [[StartAppWin alloc] init];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL isdir;
lastSaveDir = [defaults stringForKey: @"last_save_dir"];
if (lastSaveDir && [fm fileExistsAtPath: lastSaveDir isDirectory: &isdir] && isdir) {
RETAIN (lastSaveDir);
} else {
ASSIGN (lastSaveDir, NSHomeDirectory());
}
}
- (void)applicationDidBecomeActive:(NSNotification *)aNotification
{
}
- (BOOL)application:(NSApplication *)application
openFile:(NSString *)fileName
{
MDKWindow *window = [self windowWithSavedPath: fileName];
if (window) {
[NSApp activateIgnoringOtherApps: YES];
[window activate];
} else {
window = [[MDKWindow alloc] initWithContentsOfFile: fileName
windowRect: [self frameForNewWindow]
delegate: self];
if (window) {
[mdkwindows addObject: window];
RELEASE (window);
[NSApp activateIgnoringOtherApps: YES];
[window activate];
} else {
NSString *msg = NSLocalizedString(@"Invalid query description.", @"");
NSRunAlertPanel(nil,
[NSString stringWithFormat: @"%@: %@", fileName, msg],
NSLocalizedString(@"Ok", @""),
nil,
nil);
return NO;
}
}
return YES;
}
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)app
{
NSUInteger canterminate = NSTerminateNow;
NSUInteger i;
for (i = 0; i < [mdkwindows count]; i++) {
MDKWindow *window = [mdkwindows objectAtIndex: i];
MDKQuery *query = [window currentQuery];
if ([query isGathering] || [query waitingStart]) {
[window stopCurrentQuery];
canterminate = NSTerminateCancel;
}
}
if (canterminate == NSTerminateNow) {
for (i = 0; i < [mdkwindows count]; i++) {
MDKWindow *window = [mdkwindows objectAtIndex: i];
if (([window savePath] != nil) && ([window isSaved] == NO)) {
canterminate = NSTerminateCancel;
break;
}
}
if (canterminate == NSTerminateCancel) {
canterminate = !(NSRunAlertPanel(nil,
NSLocalizedString(@"You have unsaved queries", @""),
NSLocalizedString(@"Cancel", @""),
NSLocalizedString(@"Quit Anyway", @""),
nil));
}
}
if (canterminate == NSTerminateNow) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject: lastSaveDir forKey: @"last_save_dir"];
[defaults synchronize];
}
return canterminate;
}
- (MDKWindow *)windowWithSavedPath:(NSString *)path
{
int i;
for (i= 0; i < [mdkwindows count]; i++) {
MDKWindow *window = [mdkwindows objectAtIndex: i];
NSString *savePath = [window savePath];
if (savePath && [savePath isEqual: path]) {
return window;
}
}
return nil;
}
- (NSRect)frameForNewWindow
{
NSRect scr = [[NSScreen mainScreen] visibleFrame];
NSRect wrect = NSZeroRect;
int i;
#define MARGIN 200
#define SHIFT 100
scr.origin.x += MARGIN;
scr.origin.y += MARGIN;
scr.size.width -= (MARGIN * 2);
scr.size.height -= (MARGIN * 2);
for (i = [mdkwindows count] - 1; i >= 0; i--) {
MDKWindow *mdkwin = [mdkwindows objectAtIndex: i];
NSRect wr = [[mdkwin window] frame];
wrect = NSMakeRect(wr.origin.x + SHIFT,
wr.origin.y - wr.size.height - SHIFT,
wr.size.width,
wr.size.height);
if (NSContainsRect(scr, wrect) == NO) {
wrect = NSMakeRect(scr.origin.x,
scr.size.height - wr.size.height,
wr.size.width,
wr.size.height);
break;
}
}
return wrect;
}
- (void)connectWorkspaceApp
{
if (workspaceApp == nil) {
workspaceApp = [NSConnection rootProxyForConnectionWithRegisteredName: @"GWorkspace"
host: @""];
if (workspaceApp == nil) {
int i;
[startAppWin showWindowWithTitle: @"MDFinder"
appName: @"GWorkspace"
operation: NSLocalizedString(@"starting:", @"")
maxProgValue: 80.0];
[[NSWorkspace sharedWorkspace] launchApplication: @"GWorkspace"];
for (i = 1; i <= 80; i++) {
[startAppWin updateProgressBy: 1.0];
[[NSRunLoop currentRunLoop] runUntilDate:
[NSDate dateWithTimeIntervalSinceNow: 0.1]];
workspaceApp = [NSConnection rootProxyForConnectionWithRegisteredName: @"GWorkspace"
host: @""];
if (workspaceApp) {
[startAppWin updateProgressBy: 80.0 - i];
break;
}
}
[[startAppWin win] close];
}
if (workspaceApp) {
RETAIN (workspaceApp);
[workspaceApp setProtocolForProxy: @protocol(WorkspaceAppProtocol)];
[nc addObserver: self
selector: @selector(workspaceAppConnectionDidDie:)
name: NSConnectionDidDieNotification
object: [workspaceApp connectionForProxy]];
} else {
NSRunAlertPanel(nil,
NSLocalizedString(@"unable to contact GWorkspace!", @""),
NSLocalizedString(@"Ok", @""),
nil,
nil);
}
}
}
- (void)workspaceAppConnectionDidDie:(NSNotification *)notif
{
[nc removeObserver: self
name: NSConnectionDidDieNotification
object: [notif object]];
RELEASE (workspaceApp);
workspaceApp = nil;
NSRunAlertPanel(nil,
NSLocalizedString(@"The GWorkspace connection died.", @""),
NSLocalizedString(@"Ok", @""),
nil,
nil);
[self connectWorkspaceApp];
}
//
// Menu
//
- (void)newQuery:(id)sender
{
MDKWindow *window = [[MDKWindow alloc] initWithContentsOfFile: nil
windowRect: [self frameForNewWindow]
delegate: self];
[mdkwindows addObject: window];
RELEASE (window);
[window activate];
}
- (void)openQuery:(id)sender
{
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
int result;
[openPanel setTitle: NSLocalizedString(@"Open saved query", @"")];
[openPanel setAllowsMultipleSelection: NO];
[openPanel setCanChooseFiles: YES];
[openPanel setCanChooseDirectories: NO];
result = [openPanel runModalForDirectory: lastSaveDir
file: nil
types: [NSArray arrayWithObject: @"mdss"]];
if (result == NSOKButton) {
NSString *wpath = [openPanel filename];
MDKWindow *window = [self windowWithSavedPath: wpath];
if (window == nil) {
window = [[MDKWindow alloc] initWithContentsOfFile: wpath
windowRect: [self frameForNewWindow]
delegate: self];
if (window) {
[mdkwindows addObject: window];
RELEASE (window);
[NSApp activateIgnoringOtherApps: YES];
[window activate];
} else {
NSString *msg = NSLocalizedString(@"Invalid query description.", @"");
NSRunAlertPanel(nil,
[NSString stringWithFormat: @"%@: %@", wpath, msg],
NSLocalizedString(@"Ok", @""),
nil,
nil);
}
} else {
[NSApp activateIgnoringOtherApps: YES];
[window activate];
}
}
}
- (void)saveQuery:(id)sender
{
if (activeWindow) {
NSString *savePath = [activeWindow savePath];
if (savePath == nil) {
[self saveQueryAs: nil];
} else {
NSDictionary *info = [activeWindow statusInfo];
if ([info writeToFile: savePath atomically: YES]) {
[activeWindow setSaved: YES];
} else {
NSRunAlertPanel(nil,
NSLocalizedString(@"Error saving the query!", @""),
NSLocalizedString(@"Ok", @""),
nil,
nil);
}
}
}
}
- (void)saveQueryAs:(id)sender
{
if (activeWindow) {
NSSavePanel *savePanel = [NSSavePanel savePanel];
int result;
[savePanel setTitle: NSLocalizedString(@"Save query", @"")];
[savePanel setRequiredFileType: @"mdss"];
result = [savePanel runModalForDirectory: lastSaveDir file: @""];
if (result == NSOKButton) {
NSString *savepath = [savePanel filename];
[[activeWindow statusInfo] writeToFile: savepath atomically: YES];
[activeWindow setSavePath: savepath];
[activeWindow setSaved: YES];
ASSIGN (lastSaveDir, [savepath stringByDeletingLastPathComponent]);
}
}
}
- (void)closeMainWin:(id)sender
{
[[NSApp keyWindow] performClose: sender];
}
- (void)activateContextHelp:(id)sender
{
if ([NSHelpManager isContextHelpModeActive] == NO) {
[NSHelpManager setContextHelpModeActive: YES];
}
}
- (BOOL)validateMenuItem:(NSMenuItem *)item
{
SEL action = [item action];
if (sel_isEqual(action, @selector(saveQuery:))) {
return ((activeWindow != nil) && ([activeWindow isSaved] == NO));
} else if (sel_isEqual(action, @selector(saveQueryAs:))) {
return ((activeWindow != nil) && ([activeWindow savePath] != nil));
}
return YES;
}
//
// MDKWindow delegate
//
- (void)setActiveWindow:(MDKWindow *)window
{
[self connectWorkspaceApp];
if (workspaceApp) {
NSArray *selection = [window selectedPaths];
if ([selection count]) {
[workspaceApp showExternalSelection: selection];
} else {
[workspaceApp showExternalSelection: nil];
}
}
activeWindow = window;
}
- (void)window:(MDKWindow *)window
didChangeSelection:(NSArray *)selection
{
if (window == activeWindow) {
[self connectWorkspaceApp];
if (workspaceApp) {
NSArray *selection = [activeWindow selectedPaths];
if ([selection count]) {
[workspaceApp showExternalSelection: selection];
} else {
[workspaceApp showExternalSelection: nil];
}
}
}
}
- (void)mdkwindowWillClose:(MDKWindow *)window
{
if (activeWindow == window) {
[self connectWorkspaceApp];
if (workspaceApp) {
[workspaceApp showExternalSelection: nil];
}
activeWindow = nil;
}
[mdkwindows removeObject: window];
}
@end
@implementation StartAppWin
- (void)dealloc
{
TEST_RELEASE (win);
[super dealloc];
}
- (id)init
{
self = [super init];
if (self) {
if ([NSBundle loadNibNamed: @"StartAppWin" owner: self] == NO) {
NSLog(@"failed to load StartAppWin!");
DESTROY (self);
return self;
} else {
NSRect wframe = [win frame];
NSRect scrframe = [[NSScreen mainScreen] frame];
NSRect winrect = NSMakeRect((scrframe.size.width - wframe.size.width) / 2,
(scrframe.size.height - wframe.size.height) / 2,
wframe.size.width,
wframe.size.height);
[win setFrame: winrect display: NO];
[win setDelegate: self];
/* Internationalization */
[startLabel setStringValue: NSLocalizedString(@"starting:", @"")];
}
}
return self;
}
- (void)showWindowWithTitle:(NSString *)title
appName:(NSString *)appname
operation:(NSString *)operation
maxProgValue:(double)maxvalue
{
if (win) {
[win setTitle: title];
[startLabel setStringValue: operation];
[nameField setStringValue: appname];
[progInd setMinValue: 0.0];
[progInd setMaxValue: maxvalue];
[progInd setDoubleValue: 0.0];
if ([win isVisible] == NO) {
[win orderFrontRegardless];
}
}
}
- (void)updateProgressBy:(double)incr
{
[progInd incrementBy: incr];
}
- (NSWindow *)win
{
return win;
}
- (BOOL)windowShouldClose:(id)sender
{
return YES;
}
@end
id<NSMenuItem> addItemToMenu(NSMenu *menu, NSString *str,
NSString *comm, NSString *sel, NSString *key)
{
return [menu addItemWithTitle: NSLocalizedString(str, comm)
action: NSSelectorFromString(sel)
keyEquivalent: key];
}
|