2009-08-05 08:59:46 +00:00
|
|
|
/*
|
2011-04-12 08:04:01 +00:00
|
|
|
Copyright 2011 Hardcoded Software (http://www.hardcoded.net)
|
2009-08-05 08:59:46 +00:00
|
|
|
|
2010-09-30 10:17:41 +00:00
|
|
|
This software is licensed under the "BSD" License as described in the "LICENSE" file,
|
2009-08-05 08:59:46 +00:00
|
|
|
which should be included with this package. The terms are also available at
|
2010-09-30 10:17:41 +00:00
|
|
|
http://www.hardcoded.net/licenses/bsd_license
|
2009-08-05 08:59:46 +00:00
|
|
|
*/
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
#import "DirectoryPanel.h"
|
|
|
|
#import "Dialogs.h"
|
|
|
|
#import "Utils.h"
|
|
|
|
#import "AppDelegate.h"
|
2011-01-18 14:35:14 +00:00
|
|
|
#import "Consts.h"
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2010-02-05 16:05:00 +00:00
|
|
|
@implementation DirectoryPanel
|
2011-01-15 11:08:10 +00:00
|
|
|
- (id)initWithParentApp:(AppDelegateBase *)aParentApp
|
2009-06-01 09:55:11 +00:00
|
|
|
{
|
2009-10-25 10:42:00 +00:00
|
|
|
self = [super initWithWindowNibName:@"DirectoryPanel"];
|
2009-06-01 09:55:11 +00:00
|
|
|
[self window];
|
2011-01-15 11:08:10 +00:00
|
|
|
_app = aParentApp;
|
|
|
|
_py = [_app py];
|
2011-04-22 09:37:53 +00:00
|
|
|
[[self window] setTitle:[_py appName]];
|
2011-01-14 14:07:11 +00:00
|
|
|
_alwaysShowPopUp = NO;
|
|
|
|
[self fillPopUpMenu];
|
2011-01-13 15:20:03 +00:00
|
|
|
_recentDirectories = [[HSRecentFiles alloc] initWithName:@"recentDirectories" menu:[addButtonPopUp menu]];
|
|
|
|
[_recentDirectories setDelegate:self];
|
2010-02-07 14:26:50 +00:00
|
|
|
outline = [[DirectoryOutline alloc] initWithPyParent:_py view:outlineView];
|
2009-06-01 09:55:11 +00:00
|
|
|
[self refreshRemoveButtonText];
|
2010-02-07 14:26:50 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(directorySelectionChanged:)
|
|
|
|
name:NSOutlineViewSelectionDidChangeNotification object:outlineView];
|
2011-01-27 09:27:17 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(outlineAddedFolders:)
|
|
|
|
name:DGAddedFoldersNotification object:outline];
|
2009-06-01 09:55:11 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2010-02-07 14:26:50 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[outline release];
|
2011-01-13 15:20:03 +00:00
|
|
|
[_recentDirectories release];
|
2010-02-07 14:26:50 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2011-01-14 14:07:11 +00:00
|
|
|
/* Virtual */
|
|
|
|
|
|
|
|
- (void)fillPopUpMenu
|
|
|
|
{
|
|
|
|
NSMenu *m = [addButtonPopUp menu];
|
2011-01-18 14:35:14 +00:00
|
|
|
NSMenuItem *mi = [m addItemWithTitle:TR(@"Add New Folder...") action:@selector(askForDirectory:) keyEquivalent:@""];
|
2011-01-14 14:07:11 +00:00
|
|
|
[mi setTarget:self];
|
|
|
|
[m addItem:[NSMenuItem separatorItem]];
|
|
|
|
}
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
/* Actions */
|
|
|
|
|
|
|
|
- (IBAction)askForDirectory:(id)sender
|
|
|
|
{
|
|
|
|
NSOpenPanel *op = [NSOpenPanel openPanel];
|
|
|
|
[op setCanChooseFiles:YES];
|
|
|
|
[op setCanChooseDirectories:YES];
|
2010-09-25 14:12:20 +00:00
|
|
|
[op setAllowsMultipleSelection:YES];
|
2011-11-04 17:10:11 +00:00
|
|
|
[op setTitle:TR(@"Select a folder to add to the scanning list")];
|
2009-06-01 09:55:11 +00:00
|
|
|
[op setDelegate:self];
|
2010-09-25 14:12:20 +00:00
|
|
|
if ([op runModal] == NSOKButton) {
|
|
|
|
for (NSString *directory in [op filenames]) {
|
|
|
|
[self addDirectory:directory];
|
|
|
|
}
|
2009-06-01 09:55:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)popupAddDirectoryMenu:(id)sender
|
|
|
|
{
|
2011-01-14 14:07:11 +00:00
|
|
|
if ((!_alwaysShowPopUp) && ([[_recentDirectories filepaths] count] == 0)) {
|
2009-06-01 09:55:11 +00:00
|
|
|
[self askForDirectory:sender];
|
|
|
|
}
|
2011-01-13 15:20:03 +00:00
|
|
|
else {
|
|
|
|
[addButtonPopUp selectItem:nil];
|
|
|
|
[[addButtonPopUp cell] performClickWithFrame:[sender frame] inView:[sender superview]];
|
|
|
|
}
|
2009-06-01 09:55:11 +00:00
|
|
|
}
|
|
|
|
|
2011-01-15 11:08:10 +00:00
|
|
|
- (IBAction)popupLoadRecentMenu:(id)sender
|
|
|
|
{
|
|
|
|
if ([[[_app recentResults] filepaths] count] > 0) {
|
|
|
|
NSMenu *m = [loadRecentButtonPopUp menu];
|
|
|
|
while ([m numberOfItems] > 0) {
|
|
|
|
[m removeItemAtIndex:0];
|
|
|
|
}
|
2011-01-18 14:35:14 +00:00
|
|
|
NSMenuItem *mi = [m addItemWithTitle:TR(@"Load from file...") action:@selector(loadResults:) keyEquivalent:@""];
|
2011-01-15 11:08:10 +00:00
|
|
|
[mi setTarget:_app];
|
|
|
|
[m addItem:[NSMenuItem separatorItem]];
|
|
|
|
[[_app recentResults] fillMenu:m];
|
|
|
|
[loadRecentButtonPopUp selectItem:nil];
|
|
|
|
[[loadRecentButtonPopUp cell] performClickWithFrame:[sender frame] inView:[sender superview]];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
[_app loadResults:nil];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
- (IBAction)removeSelectedDirectory:(id)sender
|
|
|
|
{
|
|
|
|
[[self window] makeKeyAndOrderFront:nil];
|
2011-11-28 19:52:48 +00:00
|
|
|
[[outline py] removeSelectedDirectory];
|
|
|
|
[outlineView setNeedsDisplay:YES];
|
2009-06-01 09:55:11 +00:00
|
|
|
[self refreshRemoveButtonText];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Public */
|
|
|
|
- (void)addDirectory:(NSString *)directory
|
|
|
|
{
|
2010-01-15 10:19:24 +00:00
|
|
|
NSInteger r = [[_py addDirectory:directory] intValue];
|
2010-02-07 14:26:50 +00:00
|
|
|
if (r) {
|
2010-09-25 14:12:20 +00:00
|
|
|
NSString *m = @"";
|
|
|
|
if (r == 1) {
|
2011-11-04 17:10:11 +00:00
|
|
|
m = TR(@"'%@' already is in the list.");
|
2010-09-25 14:12:20 +00:00
|
|
|
}
|
|
|
|
else if (r == 2) {
|
2011-11-04 17:10:11 +00:00
|
|
|
m = TR(@"'%@' does not exist.");
|
2009-06-01 09:55:11 +00:00
|
|
|
}
|
2010-09-25 14:12:20 +00:00
|
|
|
[Dialogs showMessage:[NSString stringWithFormat:m,directory]];
|
2009-06-01 09:55:11 +00:00
|
|
|
}
|
2011-01-13 15:20:03 +00:00
|
|
|
[_recentDirectories addFile:directory];
|
2009-06-01 09:55:11 +00:00
|
|
|
[[self window] makeKeyAndOrderFront:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)refreshRemoveButtonText
|
|
|
|
{
|
2010-02-07 14:26:50 +00:00
|
|
|
if ([outlineView selectedRow] < 0) {
|
2009-06-01 09:55:11 +00:00
|
|
|
[removeButton setEnabled:NO];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
[removeButton setEnabled:YES];
|
2010-02-07 14:26:50 +00:00
|
|
|
NSInteger state = [outline intProperty:@"state" valueAtPath:[outline selectedIndexPath]];
|
2011-01-14 14:51:19 +00:00
|
|
|
NSString *imgName = state == 2 ? @"NSGoLeftTemplate" : @"NSRemoveTemplate";
|
|
|
|
[removeButton setImage:[NSImage imageNamed:imgName]];
|
2009-06-01 09:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Delegate */
|
|
|
|
- (BOOL)panel:(id)sender shouldShowFilename:(NSString *)path
|
|
|
|
{
|
|
|
|
BOOL isdir;
|
|
|
|
[[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isdir];
|
|
|
|
return isdir;
|
|
|
|
}
|
|
|
|
|
2011-01-13 15:20:03 +00:00
|
|
|
- (void)recentFileClicked:(NSString *)path
|
|
|
|
{
|
|
|
|
[self addDirectory:path];
|
|
|
|
}
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
/* Notifications */
|
|
|
|
|
|
|
|
- (void)directorySelectionChanged:(NSNotification *)aNotification
|
|
|
|
{
|
|
|
|
[self refreshRemoveButtonText];
|
|
|
|
}
|
|
|
|
|
2011-01-27 09:27:17 +00:00
|
|
|
- (void)outlineAddedFolders:(NSNotification *)aNotification
|
|
|
|
{
|
|
|
|
NSArray *foldernames = [[aNotification userInfo] objectForKey:@"foldernames"];
|
|
|
|
for (NSString *foldername in foldernames) {
|
|
|
|
[_recentDirectories addFile:foldername];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
@end
|