| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- /**
- * 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;
- _hasChangedContent = TRUE;
- }
- 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;
- }
- - (void)loadIconResourcePath:(NSString*)path
- {
- NSString *base = path;
- _icnOk = [[IconCache sharedInstance] registerIcon:[base stringByAppendingString:@"ok.icns"]];
- _icnSync = [[IconCache sharedInstance] registerIcon:[base stringByAppendingString:@"sync.icns"]];
- _icnWarn = [[IconCache sharedInstance] registerIcon:[base stringByAppendingString:@"warning.icns"]];
- _icnErr = [[IconCache sharedInstance] registerIcon:[base stringByAppendingString:@"error.icns"]];
- _icnOkSwm = [[IconCache sharedInstance] registerIcon:[base stringByAppendingString:@"ok_swm.icns"]];
- _icnSyncSwm = [[IconCache sharedInstance] registerIcon:[base stringByAppendingString:@"sync_swm.icns"]];
- _icnWarnSwm = [[IconCache sharedInstance] registerIcon:[base stringByAppendingString:@"warning_swm.icns"]];
- _icnErrSwm = [[IconCache sharedInstance] registerIcon:[base stringByAppendingString:@"error_swm.icns"]];
- NSLog(@"Icon ok identifier: %d from %@", [_icnOk intValue], [base stringByAppendingString:@"ok.icns"]);
- }
- - (void)enableFileIcons:(BOOL)enable
- {
- _fileIconsEnabled = enable;
- [self repaintAllWindows];
- }
- - (void)setResultForPath:(NSString*)path result:(NSString*)result
- {
- if (_icnOk == nil) {
- // no icon resource path registered yet
- return;
- }
- NSNumber *res;
- res = [NSNumber numberWithInt:0];
- if( [result isEqualToString:@"OK"] ) {
- res = _icnOk;
- } else if( [result isEqualToString:@"SYNC"] || [result isEqualToString:@"NEW"] ) {
- res = _icnSync;
- } else if( [result isEqualToString:@"IGNORE"]) {
- res = _icnWarn;
- } else if( [result isEqualToString:@"ERROR"]) {
- 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;
- }else if( [result isEqualToString:@"NOP"]) {
- // Nothing.
- } else {
- NSLog(@"Unknown status code %@", result);
- }
-
- NSString* normalizedPath = [path decomposedStringWithCanonicalMapping];
- if (![_fileNamesCache objectForKey:normalizedPath] || ![[_fileNamesCache objectForKey:normalizedPath] isEqualTo:res]) {
- [_fileNamesCache setObject:res forKey:normalizedPath];
- //NSLog(@"SET value %d %@", [res intValue], normalizedPath);
- _hasChangedContent = YES;
- [self performSelector:@selector(repaintAllWindowsIfNeeded) withObject:0 afterDelay:1.0]; // 1 sec
- }
- }
- - (NSNumber*)iconByPath:(NSString*)path isDirectory:(BOOL)isDir
- {
- //NSLog(@"%@ %@", NSStringFromSelector(_cmd), path);
- if (!_fileIconsEnabled)
- {
- NSLog(@"Icons are NOT ENABLED!");
- // return nil;
- }
- if( path == nil ) {
- NSNumber *res = [NSNumber numberWithInt:0];
- return res;
- }
- NSString* normalizedPath = [path decomposedStringWithCanonicalMapping];
- if (![[RequestManager sharedInstance] isRegisteredPath:normalizedPath isDirectory:isDir]) {
- return [NSNumber numberWithInt:0];
- }
-
- NSNumber* result = [_fileNamesCache objectForKey:normalizedPath];
- // NSLog(@"XXXXXXX Asking for icon for path %@ = %d",normalizedPath, [result intValue]);
-
- if( result == nil ) {
- // start the async call
- NSNumber *askState = [[RequestManager sharedInstance] askForIcon:normalizedPath isDirectory:isDir];
- [_fileNamesCache setObject:askState forKey:normalizedPath];
- result = [NSNumber numberWithInt:0];
- } else if( [result intValue] == -1 ) {
- // the socket call is underways.
- result = [NSNumber numberWithInt:0];
- } else {
- // there is a proper icon index
- }
- // NSLog(@"iconByPath return value %d", [result intValue]);
- return result;
- }
- // called as a result of an UPDATE_VIEW message.
- // it clears the entries from the hash to make it call again home to the desktop client.
- - (void)clearFileNameCacheForPath:(NSString*)path
- {
- NSLog(@"%@", NSStringFromSelector(_cmd));
- NSMutableArray *keysToDelete = [NSMutableArray array];
-
- if( path != nil ) {
- for (id p in [_fileNamesCache keyEnumerator]) {
- //do stuff with obj
- if ( [p hasPrefix:path] ) {
- [keysToDelete addObject:p];
- }
- }
- } else {
- // clear the entire fileNameCache
- [_fileNamesCache release];
- _fileNamesCache = [[NSMutableDictionary alloc] init];
- return;
- }
-
- if( [keysToDelete count] > 0 ) {
- NSLog( @"Entries to delete: %lu", (unsigned long)[keysToDelete count]);
- [_fileNamesCache removeObjectsForKeys:keysToDelete];
- }
- }
- - (void)reFetchFileNameCacheForPath:(NSString*)path
- {
- NSLog(@"%@", NSStringFromSelector(_cmd));
- for (id p in [_fileNamesCache keyEnumerator]) {
- if ( path && [p hasPrefix:path] ) {
- [[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);
- }
- }
- // Ask for directory itself
- if ([path hasSuffix:@"/"]) {
- path = [path substringToIndex:path.length - 1];
- }
- [[RequestManager sharedInstance] askForIcon:path isDirectory:true];
- //NSLog(@"%@ %@", NSStringFromSelector(_cmd), path);
- }
- - (void)removeAllIcons
- {
- [_fileNamesCache removeAllObjects];
- [self repaintAllWindows];
- }
- - (void)removeIcons:(NSArray*)paths
- {
- for (NSString* path in paths)
- {
- NSString* normalizedPath = [path decomposedStringWithCanonicalMapping];
- [_fileNamesCache removeObjectForKey:normalizedPath];
- }
- [self repaintAllWindows];
- }
- - (void)repaintAllWindowsIfNeeded
- {
- if (!_hasChangedContent) {
- //NSLog(@"%@ Repaint scheduled but not needed", NSStringFromSelector(_cmd));
- return;
- }
- _hasChangedContent = NO;
- [self repaintAllWindows];
- }
- - (void)repaintAllWindows
- {
- NSLog(@"%@", NSStringFromSelector(_cmd));
- 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
- {
- NSLog(@"OwnCloudFinder: refreshing icon badges failed");
- 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
- {
- NSLog(@"OwnCloudFinder: refreshing icon badges failed");
- return;
- }
- }
- }
- }
- }
- - (void)setIcons:(NSDictionary*)iconDictionary filterByFolder:(NSString*)filterFolder
- {
- NSLog(@"%@", NSStringFromSelector(_cmd));
- 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];});
- }
- }
- }
- @end
|