ContentManager.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /**
  2. * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. #import <AppKit/NSApplication.h>
  15. #import <AppKit/NSWindow.h>
  16. #import <objc/runtime.h>
  17. #import "ContentManager.h"
  18. #import "MenuManager.h"
  19. #import "RequestManager.h"
  20. #import "IconCache.h"
  21. static ContentManager* sharedInstance = nil;
  22. @implementation ContentManager
  23. - init
  24. {
  25. self = [super init];
  26. if (self)
  27. {
  28. _fileNamesCache = [[NSMutableDictionary alloc] init];
  29. _fileIconsEnabled = TRUE;
  30. _hasChangedContent = TRUE;
  31. }
  32. return self;
  33. }
  34. - (void)dealloc
  35. {
  36. [self removeAllIcons];
  37. [_fileNamesCache release];
  38. sharedInstance = nil;
  39. [super dealloc];
  40. }
  41. + (ContentManager*)sharedInstance
  42. {
  43. @synchronized(self)
  44. {
  45. if (sharedInstance == nil)
  46. {
  47. sharedInstance = [[self alloc] init];
  48. }
  49. }
  50. return sharedInstance;
  51. }
  52. - (void)loadIconResourcePath:(NSString*)path
  53. {
  54. NSString *base = path;
  55. _icnOk = [[IconCache sharedInstance] registerIcon:[base stringByAppendingString:@"ok.icns"]];
  56. _icnSync = [[IconCache sharedInstance] registerIcon:[base stringByAppendingString:@"sync.icns"]];
  57. _icnWarn = [[IconCache sharedInstance] registerIcon:[base stringByAppendingString:@"warning.icns"]];
  58. _icnErr = [[IconCache sharedInstance] registerIcon:[base stringByAppendingString:@"error.icns"]];
  59. _icnOkSwm = [[IconCache sharedInstance] registerIcon:[base stringByAppendingString:@"ok_swm.icns"]];
  60. _icnSyncSwm = [[IconCache sharedInstance] registerIcon:[base stringByAppendingString:@"sync_swm.icns"]];
  61. _icnWarnSwm = [[IconCache sharedInstance] registerIcon:[base stringByAppendingString:@"warning_swm.icns"]];
  62. _icnErrSwm = [[IconCache sharedInstance] registerIcon:[base stringByAppendingString:@"error_swm.icns"]];
  63. NSLog(@"Icon ok identifier: %d from %@", [_icnOk intValue], [base stringByAppendingString:@"ok.icns"]);
  64. }
  65. - (void)enableFileIcons:(BOOL)enable
  66. {
  67. _fileIconsEnabled = enable;
  68. [self repaintAllWindows];
  69. }
  70. - (void)setResultForPath:(NSString*)path result:(NSString*)result
  71. {
  72. if (_icnOk == nil) {
  73. // no icon resource path registered yet
  74. return;
  75. }
  76. NSNumber *res;
  77. res = [NSNumber numberWithInt:0];
  78. if( [result isEqualToString:@"OK"] ) {
  79. res = _icnOk;
  80. } else if( [result isEqualToString:@"SYNC"] || [result isEqualToString:@"NEW"] ) {
  81. res = _icnSync;
  82. } else if( [result isEqualToString:@"IGNORE"]) {
  83. res = _icnWarn;
  84. } else if( [result isEqualToString:@"ERROR"]) {
  85. res = _icnErr;
  86. } else if( [result isEqualToString:@"OK+SWM"] ) {
  87. res = _icnOkSwm;
  88. } else if( [result isEqualToString:@"SYNC+SWM"] || [result isEqualToString:@"NEW+SWM"] ) {
  89. res = _icnSyncSwm;
  90. } else if( [result isEqualToString:@"IGNORE+SWM"]) {
  91. res = _icnWarnSwm;
  92. } else if( [result isEqualToString:@"ERROR+SWM"]) {
  93. res = _icnErrSwm;
  94. }else if( [result isEqualToString:@"NOP"]) {
  95. // Nothing.
  96. } else {
  97. NSLog(@"Unknown status code %@", result);
  98. }
  99. NSString* normalizedPath = [path decomposedStringWithCanonicalMapping];
  100. if (![_fileNamesCache objectForKey:normalizedPath] || ![[_fileNamesCache objectForKey:normalizedPath] isEqualTo:res]) {
  101. [_fileNamesCache setObject:res forKey:normalizedPath];
  102. //NSLog(@"SET value %d %@", [res intValue], normalizedPath);
  103. _hasChangedContent = YES;
  104. [self performSelector:@selector(repaintAllWindowsIfNeeded) withObject:0 afterDelay:1.0]; // 1 sec
  105. }
  106. }
  107. - (NSNumber*)iconByPath:(NSString*)path isDirectory:(BOOL)isDir
  108. {
  109. //NSLog(@"%@ %@", NSStringFromSelector(_cmd), path);
  110. if (!_fileIconsEnabled)
  111. {
  112. NSLog(@"Icons are NOT ENABLED!");
  113. // return nil;
  114. }
  115. if( path == nil ) {
  116. NSNumber *res = [NSNumber numberWithInt:0];
  117. return res;
  118. }
  119. NSString* normalizedPath = [path decomposedStringWithCanonicalMapping];
  120. if (![[RequestManager sharedInstance] isRegisteredPath:normalizedPath isDirectory:isDir]) {
  121. return [NSNumber numberWithInt:0];
  122. }
  123. NSNumber* result = [_fileNamesCache objectForKey:normalizedPath];
  124. // NSLog(@"XXXXXXX Asking for icon for path %@ = %d",normalizedPath, [result intValue]);
  125. if( result == nil ) {
  126. // start the async call
  127. NSNumber *askState = [[RequestManager sharedInstance] askForIcon:normalizedPath isDirectory:isDir];
  128. [_fileNamesCache setObject:askState forKey:normalizedPath];
  129. result = [NSNumber numberWithInt:0];
  130. } else if( [result intValue] == -1 ) {
  131. // the socket call is underways.
  132. result = [NSNumber numberWithInt:0];
  133. } else {
  134. // there is a proper icon index
  135. }
  136. // NSLog(@"iconByPath return value %d", [result intValue]);
  137. return result;
  138. }
  139. // called as a result of an UPDATE_VIEW message.
  140. // it clears the entries from the hash to make it call again home to the desktop client.
  141. - (void)clearFileNameCacheForPath:(NSString*)path
  142. {
  143. NSLog(@"%@", NSStringFromSelector(_cmd));
  144. NSMutableArray *keysToDelete = [NSMutableArray array];
  145. if( path != nil ) {
  146. for (id p in [_fileNamesCache keyEnumerator]) {
  147. //do stuff with obj
  148. if ( [p hasPrefix:path] ) {
  149. [keysToDelete addObject:p];
  150. }
  151. }
  152. } else {
  153. // clear the entire fileNameCache
  154. [_fileNamesCache release];
  155. _fileNamesCache = [[NSMutableDictionary alloc] init];
  156. return;
  157. }
  158. if( [keysToDelete count] > 0 ) {
  159. NSLog( @"Entries to delete: %lu", (unsigned long)[keysToDelete count]);
  160. [_fileNamesCache removeObjectsForKeys:keysToDelete];
  161. }
  162. }
  163. - (void)reFetchFileNameCacheForPath:(NSString*)path
  164. {
  165. NSLog(@"%@", NSStringFromSelector(_cmd));
  166. for (id p in [_fileNamesCache keyEnumerator]) {
  167. if ( path && [p hasPrefix:path] ) {
  168. [[RequestManager sharedInstance] askForIcon:p isDirectory:false]; // FIXME isDirectory parameter
  169. //[_fileNamesCache setObject:askState forKey:p]; We don't do this since we want to keep the old icon meanwhile
  170. //NSLog(@"%@ %@", NSStringFromSelector(_cmd), p);
  171. }
  172. }
  173. // Ask for directory itself
  174. if ([path hasSuffix:@"/"]) {
  175. path = [path substringToIndex:path.length - 1];
  176. }
  177. [[RequestManager sharedInstance] askForIcon:path isDirectory:true];
  178. //NSLog(@"%@ %@", NSStringFromSelector(_cmd), path);
  179. }
  180. - (void)removeAllIcons
  181. {
  182. [_fileNamesCache removeAllObjects];
  183. [self repaintAllWindows];
  184. }
  185. - (void)removeIcons:(NSArray*)paths
  186. {
  187. for (NSString* path in paths)
  188. {
  189. NSString* normalizedPath = [path decomposedStringWithCanonicalMapping];
  190. [_fileNamesCache removeObjectForKey:normalizedPath];
  191. }
  192. [self repaintAllWindows];
  193. }
  194. - (void)repaintAllWindowsIfNeeded
  195. {
  196. if (!_hasChangedContent) {
  197. //NSLog(@"%@ Repaint scheduled but not needed", NSStringFromSelector(_cmd));
  198. return;
  199. }
  200. _hasChangedContent = NO;
  201. [self repaintAllWindows];
  202. }
  203. - (void)repaintAllWindows
  204. {
  205. NSLog(@"%@", NSStringFromSelector(_cmd));
  206. NSArray* windows = [[NSApplication sharedApplication] windows];
  207. for (int i = 0; i < [windows count]; i++)
  208. {
  209. NSWindow* window = [windows objectAtIndex:i];
  210. if (![window isVisible])
  211. {
  212. continue;
  213. }
  214. MenuManager* menuManager = [MenuManager sharedInstance];
  215. RequestManager* requestManager = [RequestManager sharedInstance];
  216. if ([[window className] isEqualToString:@"TBrowserWindow"])
  217. {
  218. NSObject* browserWindowController = [window browserWindowController];
  219. BOOL repaintWindow = YES;
  220. NSString* filterFolder = [requestManager filterFolder];
  221. if (filterFolder)
  222. {
  223. repaintWindow = NO;
  224. struct TFENodeVector* targetPath;
  225. if ([browserWindowController respondsToSelector:@selector(targetPath)])
  226. {
  227. // 10.7 & 10.8
  228. targetPath = [browserWindowController targetPath];
  229. }
  230. else if ([browserWindowController respondsToSelector:@selector(activeContainer)])
  231. {
  232. // 10.9
  233. targetPath = [[browserWindowController activeContainer] targetPath];
  234. }
  235. else
  236. {
  237. NSLog(@"OwnCloudFinder: refreshing icon badges failed");
  238. return;
  239. }
  240. NSArray* folderPaths = [menuManager pathsForNodes:targetPath];
  241. for (NSString* folderPath in folderPaths)
  242. {
  243. if ([folderPath hasPrefix:filterFolder] || [filterFolder hasPrefix:folderPath])
  244. {
  245. repaintWindow = YES;
  246. break;
  247. }
  248. }
  249. }
  250. if (repaintWindow)
  251. {
  252. if ([browserWindowController respondsToSelector:@selector(browserViewController)])
  253. {
  254. // 10.7 & 10.8
  255. NSObject* browserViewController = [browserWindowController browserViewController];
  256. NSObject* browserView = [browserViewController browserView];
  257. dispatch_async(dispatch_get_main_queue(), ^{[browserView setNeedsDisplay:YES];});
  258. }
  259. else if ([browserWindowController respondsToSelector:@selector(activeBrowserViewController)])
  260. {
  261. // 10.9
  262. NSObject* browserViewController = [browserWindowController activeBrowserViewController];
  263. NSObject* browserView = [browserViewController browserView];
  264. if ([browserView isKindOfClass:(id)objc_getClass("TListView")])
  265. {
  266. // List or Coverflow View
  267. [self setNeedsDisplayForListView:browserView];
  268. }
  269. else
  270. {
  271. // Icon or Column View
  272. dispatch_async(dispatch_get_main_queue(), ^{[browserView setNeedsDisplay:YES];});
  273. }
  274. }
  275. else
  276. {
  277. NSLog(@"OwnCloudFinder: refreshing icon badges failed");
  278. return;
  279. }
  280. }
  281. }
  282. }
  283. }
  284. - (void)setIcons:(NSDictionary*)iconDictionary filterByFolder:(NSString*)filterFolder
  285. {
  286. NSLog(@"%@", NSStringFromSelector(_cmd));
  287. for (NSString* path in iconDictionary)
  288. {
  289. if (filterFolder && ![path hasPrefix:filterFolder])
  290. {
  291. continue;
  292. }
  293. NSString* normalizedPath = [path decomposedStringWithCanonicalMapping];
  294. NSNumber* iconId = [iconDictionary objectForKey:path];
  295. if ([iconId intValue] == -1)
  296. {
  297. [_fileNamesCache removeObjectForKey:normalizedPath];
  298. }
  299. else
  300. {
  301. [_fileNamesCache setObject:iconId forKey:normalizedPath];
  302. }
  303. }
  304. [self repaintAllWindows];
  305. }
  306. - (void)setNeedsDisplayForListView:(NSView*)view
  307. {
  308. NSArray* subviews = [view subviews];
  309. for (int i = 0; i < [subviews count]; i++)
  310. {
  311. NSView* subview = [subviews objectAtIndex:i];
  312. if ([subview isKindOfClass:(id)objc_getClass("TListRowView")])
  313. {
  314. [self setNeedsDisplayForListView:subview];
  315. }
  316. else if ([subview isKindOfClass:(id)objc_getClass("TListNameCellView")])
  317. {
  318. dispatch_async(dispatch_get_main_queue(), ^{[subview setNeedsDisplay:YES];});
  319. }
  320. }
  321. }
  322. @end