2009-08-05 08:59:46 +00:00
|
|
|
/*
|
2015-01-03 21:30:57 +00:00
|
|
|
Copyright 2015 Hardcoded Software (http://www.hardcoded.net)
|
2009-08-05 08:59:46 +00:00
|
|
|
|
2015-01-03 21:33:16 +00:00
|
|
|
This software is licensed under the "GPLv3" 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
|
2015-01-03 21:33:16 +00:00
|
|
|
http://www.gnu.org/licenses/gpl-3.0.html
|
2009-08-05 08:59:46 +00:00
|
|
|
*/
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
#import "DirectoryPanel.h"
|
2012-07-23 21:46:01 +00:00
|
|
|
#import "DirectoryPanel_UI.h"
|
2009-06-01 09:55:11 +00:00
|
|
|
#import "Dialogs.h"
|
|
|
|
#import "Utils.h"
|
|
|
|
#import "AppDelegate.h"
|
|
|
|
|
2010-02-05 16:05:00 +00:00
|
|
|
@implementation DirectoryPanel
|
2012-07-23 21:46:01 +00:00
|
|
|
|
2016-06-03 01:31:12 +00:00
|
|
|
@synthesize scanTypePopup;
|
2012-07-23 21:46:01 +00:00
|
|
|
@synthesize addButtonPopUp;
|
|
|
|
@synthesize loadRecentButtonPopUp;
|
|
|
|
@synthesize outlineView;
|
|
|
|
@synthesize removeButton;
|
|
|
|
@synthesize loadResultsButton;
|
|
|
|
|
2011-01-15 11:08:10 +00:00
|
|
|
- (id)initWithParentApp:(AppDelegateBase *)aParentApp
|
2009-06-01 09:55:11 +00:00
|
|
|
{
|
2012-07-23 21:46:01 +00:00
|
|
|
self = [super initWithWindow:nil];
|
|
|
|
[self setWindow:createDirectoryPanel_UI(self)];
|
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]];
|
2016-06-03 01:31:12 +00:00
|
|
|
[[self scanTypePopup] addItemsWithTitles:[[aParentApp model] getScanOptions]];
|
|
|
|
[[self scanTypePopup] bind:@"selectedIndex" toObject:[NSUserDefaultsController sharedUserDefaultsController] withKeyPath:@"values.scanType" options:nil];
|
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];
|
2012-08-01 20:34:12 +00:00
|
|
|
NSMenuItem *mi = [m addItemWithTitle:NSLocalizedString(@"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 */
|
|
|
|
|
2012-07-23 21:46:01 +00:00
|
|
|
- (void)askForDirectory
|
2009-06-01 09:55:11 +00:00
|
|
|
{
|
|
|
|
NSOpenPanel *op = [NSOpenPanel openPanel];
|
|
|
|
[op setCanChooseFiles:YES];
|
|
|
|
[op setCanChooseDirectories:YES];
|
2010-09-25 14:12:20 +00:00
|
|
|
[op setAllowsMultipleSelection:YES];
|
2012-08-01 20:34:12 +00:00
|
|
|
[op setTitle:NSLocalizedString(@"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) {
|
2013-11-10 17:41:10 +00:00
|
|
|
for (NSURL *directoryURL in [op URLs]) {
|
|
|
|
[self addDirectory:[directoryURL path]];
|
2010-09-25 14:12:20 +00:00
|
|
|
}
|
2009-06-01 09:55:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-23 21:46:01 +00:00
|
|
|
- (void)popupAddDirectoryMenu:(id)sender
|
2009-06-01 09:55:11 +00:00
|
|
|
{
|
2011-01-14 14:07:11 +00:00
|
|
|
if ((!_alwaysShowPopUp) && ([[_recentDirectories filepaths] count] == 0)) {
|
2012-07-23 21:46:01 +00:00
|
|
|
[self askForDirectory];
|
2009-06-01 09:55:11 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2012-07-23 21:46:01 +00:00
|
|
|
- (void)popupLoadRecentMenu:(id)sender
|
2011-01-15 11:08:10 +00:00
|
|
|
{
|
|
|
|
if ([[[_app recentResults] filepaths] count] > 0) {
|
|
|
|
NSMenu *m = [loadRecentButtonPopUp menu];
|
|
|
|
while ([m numberOfItems] > 0) {
|
|
|
|
[m removeItemAtIndex:0];
|
|
|
|
}
|
2012-08-01 20:34:12 +00:00
|
|
|
NSMenuItem *mi = [m addItemWithTitle:NSLocalizedString(@"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 {
|
2012-07-27 22:30:34 +00:00
|
|
|
[_app loadResults];
|
2011-01-15 11:08:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-23 21:46:01 +00:00
|
|
|
- (void)removeSelectedDirectory
|
2009-06-01 09:55:11 +00:00
|
|
|
{
|
|
|
|
[[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];
|
|
|
|
}
|
|
|
|
|
2016-06-05 01:18:14 +00:00
|
|
|
- (void)startDuplicateScan
|
|
|
|
{
|
|
|
|
if ([model resultsAreModified]) {
|
|
|
|
if ([Dialogs askYesNo:NSLocalizedString(@"You have unsaved results, do you really want to continue?", @"")] == NSAlertSecondButtonReturn) // NO
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
[_app setScanOptions];
|
|
|
|
[model doScan];
|
|
|
|
}
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
/* Public */
|
|
|
|
- (void)addDirectory:(NSString *)directory
|
|
|
|
{
|
2012-07-31 19:33:44 +00:00
|
|
|
[model addDirectory:directory];
|
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
|
|
|
}
|
|
|
|
|
2013-11-24 15:12:47 +00:00
|
|
|
- (void)markAll
|
|
|
|
{
|
|
|
|
/* markAll isn't very descriptive of what we do, but since we re-use the Mark All button from
|
|
|
|
the result window, we don't have much choice.
|
|
|
|
*/
|
|
|
|
[outline selectAll];
|
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2013-11-24 15:12:47 +00:00
|
|
|
- (BOOL)validateMenuItem:(NSMenuItem *)item
|
|
|
|
{
|
|
|
|
if ([item action] == @selector(markAll)) {
|
|
|
|
[item setTitle:NSLocalizedString(@"Select All", @"")];
|
|
|
|
}
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
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
|