2014-07-09 14:45:53 +04:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2.1 of the License, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This library 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 Lesser General Public License for more
|
|
|
|
* details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <AppKit/NSApplication.h>
|
|
|
|
#import <AppKit/NSWindow.h>
|
|
|
|
#import <objc/runtime.h>
|
|
|
|
#import "ContentManager.h"
|
|
|
|
#import "MenuManager.h"
|
|
|
|
#import "RequestManager.h"
|
|
|
|
#import "IconCache.h"
|
|
|
|
|
|
|
|
static ContentManager* sharedInstance = nil;
|
|
|
|
|
|
|
|
@implementation ContentManager
|
|
|
|
- init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
_fileNamesCache = [[NSMutableDictionary alloc] init];
|
|
|
|
_fileIconsEnabled = TRUE;
|
2014-08-27 21:05:26 +04:00
|
|
|
_hasChangedContent = TRUE;
|
2014-07-09 14:45:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[self removeAllIcons];
|
|
|
|
[_fileNamesCache release];
|
|
|
|
sharedInstance = nil;
|
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (ContentManager*)sharedInstance
|
|
|
|
{
|
|
|
|
@synchronized(self)
|
|
|
|
{
|
|
|
|
if (sharedInstance == nil)
|
|
|
|
{
|
|
|
|
sharedInstance = [[self alloc] init];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sharedInstance;
|
|
|
|
}
|
|
|
|
|
2014-09-12 17:06:51 +04:00
|
|
|
- (void)loadIconResourcePath:(NSString*)path
|
|
|
|
{
|
|
|
|
NSString *base = path;
|
|
|
|
|
2014-12-15 13:34:13 +03:00
|
|
|
_icnOk = [[IconCache sharedInstance] registerIcon:[base stringByAppendingPathComponent:@"ok.icns"]];
|
|
|
|
_icnSync = [[IconCache sharedInstance] registerIcon:[base stringByAppendingPathComponent:@"sync.icns"]];
|
|
|
|
_icnWarn = [[IconCache sharedInstance] registerIcon:[base stringByAppendingPathComponent:@"warning.icns"]];
|
|
|
|
_icnErr = [[IconCache sharedInstance] registerIcon:[base stringByAppendingPathComponent:@"error.icns"]];
|
|
|
|
_icnOkSwm = [[IconCache sharedInstance] registerIcon:[base stringByAppendingPathComponent:@"ok_swm.icns"]];
|
|
|
|
_icnSyncSwm = [[IconCache sharedInstance] registerIcon:[base stringByAppendingPathComponent:@"sync_swm.icns"]];
|
|
|
|
_icnWarnSwm = [[IconCache sharedInstance] registerIcon:[base stringByAppendingPathComponent:@"warning_swm.icns"]];
|
|
|
|
_icnErrSwm = [[IconCache sharedInstance] registerIcon:[base stringByAppendingPathComponent:@"error_swm.icns"]];
|
2014-09-12 17:06:51 +04:00
|
|
|
|
2015-02-17 20:15:25 +03:00
|
|
|
// NSLog(@"Icon ok identifier: %d from %@", [_icnOk intValue], [base stringByAppendingString:@"ok.icns"]);
|
2014-09-12 17:06:51 +04:00
|
|
|
}
|
|
|
|
|
2014-07-09 14:45:53 +04:00
|
|
|
- (void)enableFileIcons:(BOOL)enable
|
|
|
|
{
|
|
|
|
_fileIconsEnabled = enable;
|
|
|
|
|
|
|
|
[self repaintAllWindows];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setResultForPath:(NSString*)path result:(NSString*)result
|
|
|
|
{
|
2014-09-12 17:06:51 +04:00
|
|
|
if (_icnOk == nil) {
|
|
|
|
// no icon resource path registered yet
|
|
|
|
return;
|
|
|
|
}
|
2014-07-11 18:39:39 +04:00
|
|
|
|
|
|
|
NSNumber *res;
|
|
|
|
res = [NSNumber numberWithInt:0];
|
|
|
|
|
2014-07-09 14:45:53 +04:00
|
|
|
if( [result isEqualToString:@"OK"] ) {
|
2014-07-11 18:39:39 +04:00
|
|
|
res = _icnOk;
|
2014-07-11 13:31:04 +04:00
|
|
|
} else if( [result isEqualToString:@"SYNC"] || [result isEqualToString:@"NEW"] ) {
|
2014-07-11 18:39:39 +04:00
|
|
|
res = _icnSync;
|
2014-07-09 14:45:53 +04:00
|
|
|
} else if( [result isEqualToString:@"IGNORE"]) {
|
2014-07-11 18:39:39 +04:00
|
|
|
res = _icnWarn;
|
2014-07-09 14:45:53 +04:00
|
|
|
} else if( [result isEqualToString:@"ERROR"]) {
|
2014-07-11 18:39:39 +04:00
|
|
|
res = _icnErr;
|
|
|
|
} else if( [result isEqualToString:@"OK+SWM"] ) {
|
|
|
|
res = _icnOkSwm;
|
|
|
|
} else if( [result isEqualToString:@"SYNC+SWM"] || [result isEqualToString:@"NEW+SWM"] ) {
|
|
|
|
res = _icnSyncSwm;
|
|
|
|
} else if( [result isEqualToString:@"IGNORE+SWM"]) {
|
|
|
|
res = _icnWarnSwm;
|
|
|
|
} else if( [result isEqualToString:@"ERROR+SWM"]) {
|
|
|
|
res = _icnErrSwm;
|
2014-07-09 14:45:53 +04:00
|
|
|
}else if( [result isEqualToString:@"NOP"]) {
|
|
|
|
// Nothing.
|
|
|
|
} else {
|
2015-02-25 14:10:36 +03:00
|
|
|
NSLog(@"SyncState: Unknown status code %@", result);
|
2014-07-09 14:45:53 +04:00
|
|
|
}
|
2014-07-11 18:39:39 +04:00
|
|
|
|
2014-07-09 14:45:53 +04:00
|
|
|
NSString* normalizedPath = [path decomposedStringWithCanonicalMapping];
|
2014-08-27 21:05:26 +04:00
|
|
|
|
2014-08-28 12:21:37 +04:00
|
|
|
if (![_fileNamesCache objectForKey:normalizedPath] || ![[_fileNamesCache objectForKey:normalizedPath] isEqualTo:res]) {
|
2014-08-27 21:05:26 +04:00
|
|
|
[_fileNamesCache setObject:res forKey:normalizedPath];
|
2014-09-30 14:53:35 +04:00
|
|
|
//NSLog(@"SET value %d %@", [res intValue], normalizedPath);
|
2014-08-27 21:05:26 +04:00
|
|
|
_hasChangedContent = YES;
|
|
|
|
[self performSelector:@selector(repaintAllWindowsIfNeeded) withObject:0 afterDelay:1.0]; // 1 sec
|
|
|
|
}
|
2014-07-09 14:45:53 +04:00
|
|
|
}
|
|
|
|
|
2014-08-04 14:03:01 +04:00
|
|
|
- (NSNumber*)iconByPath:(NSString*)path isDirectory:(BOOL)isDir
|
2014-07-09 14:45:53 +04:00
|
|
|
{
|
2014-08-27 21:05:26 +04:00
|
|
|
//NSLog(@"%@ %@", NSStringFromSelector(_cmd), path);
|
2014-07-09 14:45:53 +04:00
|
|
|
if (!_fileIconsEnabled)
|
|
|
|
{
|
2015-02-25 14:10:36 +03:00
|
|
|
NSLog(@"SyncState: Icons are NOT ENABLED!");
|
2014-07-09 14:45:53 +04:00
|
|
|
// return nil;
|
|
|
|
}
|
|
|
|
|
2014-07-09 20:00:26 +04:00
|
|
|
if( path == nil ) {
|
|
|
|
NSNumber *res = [NSNumber numberWithInt:0];
|
|
|
|
return res;
|
|
|
|
}
|
2014-07-09 14:45:53 +04:00
|
|
|
NSString* normalizedPath = [path decomposedStringWithCanonicalMapping];
|
2014-09-12 13:46:15 +04:00
|
|
|
|
2014-09-23 12:22:40 +04:00
|
|
|
if (![[RequestManager sharedInstance] isRegisteredPath:normalizedPath isDirectory:isDir]) {
|
2014-09-12 13:46:15 +04:00
|
|
|
return [NSNumber numberWithInt:0];
|
|
|
|
}
|
2014-07-09 20:00:26 +04:00
|
|
|
|
2014-07-09 14:45:53 +04:00
|
|
|
NSNumber* result = [_fileNamesCache objectForKey:normalizedPath];
|
2014-09-30 14:53:35 +04:00
|
|
|
// NSLog(@"XXXXXXX Asking for icon for path %@ = %d",normalizedPath, [result intValue]);
|
2014-07-09 14:45:53 +04:00
|
|
|
|
2014-07-09 20:00:26 +04:00
|
|
|
if( result == nil ) {
|
2014-07-09 14:45:53 +04:00
|
|
|
// start the async call
|
2014-07-17 21:29:02 +04:00
|
|
|
NSNumber *askState = [[RequestManager sharedInstance] askForIcon:normalizedPath isDirectory:isDir];
|
|
|
|
[_fileNamesCache setObject:askState forKey:normalizedPath];
|
|
|
|
|
2014-07-09 20:00:26 +04:00
|
|
|
result = [NSNumber numberWithInt:0];
|
|
|
|
} else if( [result intValue] == -1 ) {
|
|
|
|
// the socket call is underways.
|
2014-07-09 14:45:53 +04:00
|
|
|
result = [NSNumber numberWithInt:0];
|
2014-07-09 20:00:26 +04:00
|
|
|
} else {
|
|
|
|
// there is a proper icon index
|
2014-07-09 14:45:53 +04:00
|
|
|
}
|
2014-07-11 13:31:04 +04:00
|
|
|
// NSLog(@"iconByPath return value %d", [result intValue]);
|
2014-07-17 21:29:02 +04:00
|
|
|
|
2014-07-09 14:45:53 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-07-11 13:31:04 +04:00
|
|
|
// called as a result of an UPDATE_VIEW message.
|
2014-12-08 19:54:32 +03:00
|
|
|
// it clears the entries from the hash to make it call again home to the desktop client.
|
2014-07-11 13:31:04 +04:00
|
|
|
- (void)clearFileNameCacheForPath:(NSString*)path
|
|
|
|
{
|
2015-02-04 20:42:31 +03:00
|
|
|
//NSLog(@"%@", NSStringFromSelector(_cmd));
|
2014-07-11 13:31:04 +04:00
|
|
|
NSMutableArray *keysToDelete = [NSMutableArray array];
|
|
|
|
|
2014-07-17 21:29:02 +04:00
|
|
|
if( path != nil ) {
|
|
|
|
for (id p in [_fileNamesCache keyEnumerator]) {
|
|
|
|
//do stuff with obj
|
|
|
|
if ( [p hasPrefix:path] ) {
|
|
|
|
[keysToDelete addObject:p];
|
|
|
|
}
|
2014-07-11 13:31:04 +04:00
|
|
|
}
|
2014-07-17 21:29:02 +04:00
|
|
|
} else {
|
|
|
|
// clear the entire fileNameCache
|
|
|
|
[_fileNamesCache release];
|
|
|
|
_fileNamesCache = [[NSMutableDictionary alloc] init];
|
|
|
|
return;
|
2014-07-11 13:31:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if( [keysToDelete count] > 0 ) {
|
2014-10-10 13:44:01 +04:00
|
|
|
NSLog( @"Entries to delete: %lu", (unsigned long)[keysToDelete count]);
|
2014-07-11 13:31:04 +04:00
|
|
|
[_fileNamesCache removeObjectsForKeys:keysToDelete];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-06 00:25:20 +04:00
|
|
|
- (void)reFetchFileNameCacheForPath:(NSString*)path
|
|
|
|
{
|
2014-09-30 14:53:35 +04:00
|
|
|
NSLog(@"%@", NSStringFromSelector(_cmd));
|
2014-09-06 00:25:20 +04:00
|
|
|
|
|
|
|
for (id p in [_fileNamesCache keyEnumerator]) {
|
|
|
|
if ( path && [p hasPrefix:path] ) {
|
2014-09-30 14:53:35 +04:00
|
|
|
[[RequestManager sharedInstance] askForIcon:p isDirectory:false]; // FIXME isDirectory parameter
|
|
|
|
//[_fileNamesCache setObject:askState forKey:p]; We don't do this since we want to keep the old icon meanwhile
|
|
|
|
//NSLog(@"%@ %@", NSStringFromSelector(_cmd), p);
|
2014-09-06 00:25:20 +04:00
|
|
|
}
|
|
|
|
}
|
2014-09-30 14:53:35 +04:00
|
|
|
|
|
|
|
// Ask for directory itself
|
|
|
|
if ([path hasSuffix:@"/"]) {
|
|
|
|
path = [path substringToIndex:path.length - 1];
|
|
|
|
}
|
|
|
|
[[RequestManager sharedInstance] askForIcon:path isDirectory:true];
|
|
|
|
//NSLog(@"%@ %@", NSStringFromSelector(_cmd), path);
|
2014-09-06 00:25:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-09 14:45:53 +04:00
|
|
|
- (void)removeAllIcons
|
|
|
|
{
|
|
|
|
[_fileNamesCache removeAllObjects];
|
|
|
|
|
|
|
|
[self repaintAllWindows];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeIcons:(NSArray*)paths
|
|
|
|
{
|
|
|
|
for (NSString* path in paths)
|
|
|
|
{
|
|
|
|
NSString* normalizedPath = [path decomposedStringWithCanonicalMapping];
|
|
|
|
|
|
|
|
[_fileNamesCache removeObjectForKey:normalizedPath];
|
|
|
|
}
|
|
|
|
|
|
|
|
[self repaintAllWindows];
|
|
|
|
}
|
|
|
|
|
2014-08-27 21:05:26 +04:00
|
|
|
- (void)repaintAllWindowsIfNeeded
|
|
|
|
{
|
|
|
|
if (!_hasChangedContent) {
|
2014-10-10 13:44:01 +04:00
|
|
|
//NSLog(@"%@ Repaint scheduled but not needed", NSStringFromSelector(_cmd));
|
2014-08-27 21:05:26 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_hasChangedContent = NO;
|
|
|
|
[self repaintAllWindows];
|
|
|
|
}
|
|
|
|
|
2014-07-09 14:45:53 +04:00
|
|
|
- (void)repaintAllWindows
|
|
|
|
{
|
2015-02-04 20:42:31 +03:00
|
|
|
//NSLog(@"%@", NSStringFromSelector(_cmd));
|
2014-07-09 14:45:53 +04:00
|
|
|
NSArray* windows = [[NSApplication sharedApplication] windows];
|
|
|
|
|
|
|
|
for (int i = 0; i < [windows count]; i++)
|
|
|
|
{
|
|
|
|
NSWindow* window = [windows objectAtIndex:i];
|
|
|
|
|
|
|
|
if (![window isVisible])
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
MenuManager* menuManager = [MenuManager sharedInstance];
|
|
|
|
RequestManager* requestManager = [RequestManager sharedInstance];
|
|
|
|
|
|
|
|
if ([[window className] isEqualToString:@"TBrowserWindow"])
|
|
|
|
{
|
|
|
|
NSObject* browserWindowController = [window browserWindowController];
|
|
|
|
|
|
|
|
BOOL repaintWindow = YES;
|
|
|
|
|
|
|
|
NSString* filterFolder = [requestManager filterFolder];
|
|
|
|
|
|
|
|
if (filterFolder)
|
|
|
|
{
|
|
|
|
repaintWindow = NO;
|
|
|
|
|
|
|
|
struct TFENodeVector* targetPath;
|
|
|
|
|
|
|
|
if ([browserWindowController respondsToSelector:@selector(targetPath)])
|
|
|
|
{
|
|
|
|
// 10.7 & 10.8
|
|
|
|
targetPath = [browserWindowController targetPath];
|
|
|
|
}
|
|
|
|
else if ([browserWindowController respondsToSelector:@selector(activeContainer)])
|
|
|
|
{
|
|
|
|
// 10.9
|
|
|
|
targetPath = [[browserWindowController activeContainer] targetPath];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-25 14:10:36 +03:00
|
|
|
NSLog(@"SyncState: refreshing icon badges failed");
|
2014-07-09 14:45:53 +04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSArray* folderPaths = [menuManager pathsForNodes:targetPath];
|
|
|
|
|
|
|
|
for (NSString* folderPath in folderPaths)
|
|
|
|
{
|
|
|
|
if ([folderPath hasPrefix:filterFolder] || [filterFolder hasPrefix:folderPath])
|
|
|
|
{
|
|
|
|
repaintWindow = YES;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (repaintWindow)
|
|
|
|
{
|
|
|
|
if ([browserWindowController respondsToSelector:@selector(browserViewController)])
|
|
|
|
{
|
|
|
|
// 10.7 & 10.8
|
|
|
|
NSObject* browserViewController = [browserWindowController browserViewController];
|
|
|
|
|
|
|
|
NSObject* browserView = [browserViewController browserView];
|
|
|
|
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{[browserView setNeedsDisplay:YES];});
|
|
|
|
}
|
|
|
|
else if ([browserWindowController respondsToSelector:@selector(activeBrowserViewController)])
|
|
|
|
{
|
|
|
|
// 10.9
|
|
|
|
NSObject* browserViewController = [browserWindowController activeBrowserViewController];
|
|
|
|
|
|
|
|
NSObject* browserView = [browserViewController browserView];
|
|
|
|
|
|
|
|
if ([browserView isKindOfClass:(id)objc_getClass("TListView")])
|
|
|
|
{
|
|
|
|
// List or Coverflow View
|
|
|
|
[self setNeedsDisplayForListView:browserView];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Icon or Column View
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{[browserView setNeedsDisplay:YES];});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-25 14:10:36 +03:00
|
|
|
NSLog(@"SyncState: refreshing icon badges failed");
|
2014-07-09 14:45:53 +04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setIcons:(NSDictionary*)iconDictionary filterByFolder:(NSString*)filterFolder
|
|
|
|
{
|
2014-08-27 21:05:26 +04:00
|
|
|
NSLog(@"%@", NSStringFromSelector(_cmd));
|
2014-07-09 14:45:53 +04:00
|
|
|
for (NSString* path in iconDictionary)
|
|
|
|
{
|
|
|
|
if (filterFolder && ![path hasPrefix:filterFolder])
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString* normalizedPath = [path decomposedStringWithCanonicalMapping];
|
|
|
|
NSNumber* iconId = [iconDictionary objectForKey:path];
|
|
|
|
|
|
|
|
if ([iconId intValue] == -1)
|
|
|
|
{
|
|
|
|
[_fileNamesCache removeObjectForKey:normalizedPath];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[_fileNamesCache setObject:iconId forKey:normalizedPath];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[self repaintAllWindows];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setNeedsDisplayForListView:(NSView*)view
|
|
|
|
{
|
|
|
|
NSArray* subviews = [view subviews];
|
|
|
|
|
|
|
|
for (int i = 0; i < [subviews count]; i++)
|
|
|
|
{
|
|
|
|
NSView* subview = [subviews objectAtIndex:i];
|
|
|
|
|
|
|
|
if ([subview isKindOfClass:(id)objc_getClass("TListRowView")])
|
|
|
|
{
|
|
|
|
[self setNeedsDisplayForListView:subview];
|
|
|
|
}
|
|
|
|
else if ([subview isKindOfClass:(id)objc_getClass("TListNameCellView")])
|
|
|
|
{
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{[subview setNeedsDisplay:YES];});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 19:54:32 +03:00
|
|
|
@end
|