2009-08-05 08:59:46 +00:00
|
|
|
/*
|
2012-03-15 18:28:40 +00:00
|
|
|
Copyright 2012 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;
|
2012-01-13 19:43:43 +00:00
|
|
|
model = [_app model];
|
|
|
|
[[self window] setTitle:[model 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];
|
2012-01-13 20:25:34 +00:00
|
|
|
outline = [[DirectoryOutline alloc] initWithPyRef:[model directoryTree] outlineView:outlineView];
|
2009-06-01 09:55:11 +00:00
|
|
|
[self refreshRemoveButtonText];
|
2012-03-20 19:43:47 +00:00
|
|
|
[self adjustUIToLocalization];
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2012-03-20 19:43:47 +00:00
|
|
|
/* Private */
|
2011-01-14 14:07:11 +00:00
|
|
|
|
|
|
|
- (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]];
|
|
|
|
}
|
|
|
|
|
2012-03-20 19:43:47 +00:00
|
|
|
- (void)adjustUIToLocalization
|
|
|
|
{
|
|
|
|
NSString *lang = [[NSBundle preferredLocalizationsFromArray:[[NSBundle mainBundle] localizations]] objectAtIndex:0];
|
|
|
|
NSInteger loadResultsWidthDelta = 0;
|
|
|
|
if ([lang isEqual:@"ru"]) {
|
|
|
|
loadResultsWidthDelta = 50;
|
|
|
|
}
|
|
|
|
else if ([lang isEqual:@"uk"]) {
|
|
|
|
loadResultsWidthDelta = 70;
|
|
|
|
}
|
|
|
|
else if ([lang isEqual:@"hy"]) {
|
|
|
|
loadResultsWidthDelta = 30;
|
|
|
|
}
|
|
|
|
if (loadResultsWidthDelta) {
|
|
|
|
NSRect r = [loadResultsButton frame];
|
|
|
|
r.size.width += loadResultsWidthDelta;
|
|
|
|
r.origin.x -= loadResultsWidthDelta;
|
|
|
|
[loadResultsButton setFrame:r];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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];
|
2012-01-15 23:07:32 +00:00
|
|
|
[[outline model] removeSelectedDirectory];
|
2009-06-01 09:55:11 +00:00
|
|
|
[self refreshRemoveButtonText];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Public */
|
|
|
|
- (void)addDirectory:(NSString *)directory
|
|
|
|
{
|
2012-01-13 19:43:43 +00:00
|
|
|
NSInteger r = [model addDirectory:directory];
|
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];
|
2011-11-28 20:25:18 +00:00
|
|
|
NSIndexPath *path = [outline selectedIndexPath];
|
2011-11-28 20:42:13 +00:00
|
|
|
if (path != nil) {
|
|
|
|
NSInteger state = [outline intProperty:@"state" valueAtPath:path];
|
|
|
|
BOOL shouldDisplayArrow = ([path length] > 1) && (state == 2);
|
|
|
|
NSString *imgName = shouldDisplayArrow ? @"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
|