CCSynchronize.m 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. //
  2. // CCSynchronize.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 19/10/16.
  6. // Copyright (c) 2016 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "CCSynchronize.h"
  24. #import "AppDelegate.h"
  25. #import "CCCoreData.h"
  26. #import "CCMain.h"
  27. #ifdef CUSTOM_BUILD
  28. #import "CustomSwift.h"
  29. #else
  30. #import "Nextcloud-Swift.h"
  31. #endif
  32. @interface CCSynchronize () <CCActionsListingFavoritesDelegate>
  33. {
  34. // local
  35. }
  36. @end
  37. @implementation CCSynchronize
  38. + (CCSynchronize *)sharedSynchronize {
  39. static CCSynchronize *sharedSynchronize;
  40. @synchronized(self)
  41. {
  42. if (!sharedSynchronize) {
  43. sharedSynchronize = [CCSynchronize new];
  44. sharedSynchronize.foldersInSynchronized = [NSMutableOrderedSet new];
  45. }
  46. return sharedSynchronize;
  47. }
  48. }
  49. #pragma --------------------------------------------------------------------------------------------
  50. #pragma mark ===== Read Listing Favorites =====
  51. #pragma --------------------------------------------------------------------------------------------
  52. - (void)readListingFavorites
  53. {
  54. // test
  55. if (app.activeAccount.length == 0)
  56. return;
  57. // verify is offline procedure is in progress selectorDownloadSynchronize
  58. if ([[app verifyExistsInQueuesDownloadSelector:selectorDownloadSynchronize] count] > 0)
  59. return;
  60. [[CCActions sharedInstance] listingFavorites:@"" delegate:self];
  61. }
  62. - (void)addFavoriteFolder:(NSString *)serverUrl
  63. {
  64. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:serverUrl activeAccount:app.activeAccount];
  65. NSString *selector;
  66. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  67. metadataNet.action = actionReadFolder;
  68. metadataNet.directoryID = directoryID;
  69. metadataNet.priority = NSOperationQueuePriorityNormal;
  70. if ([CCUtility getFavoriteOffline])
  71. selector = selectorReadFolderWithDownload;
  72. else
  73. selector = selectorReadFolder;
  74. metadataNet.selector = selector;
  75. metadataNet.serverUrl = serverUrl;
  76. [app addNetworkingOperationQueue:app.netQueue delegate:self metadataNet:metadataNet];
  77. }
  78. - (void)listingFavoritesSuccess:(CCMetadataNet *)metadataNet metadatas:(NSArray *)metadatas
  79. {
  80. // verify active user
  81. TableAccount *record = [CCCoreData getActiveAccount];
  82. if (![record.account isEqualToString:metadataNet.account])
  83. return;
  84. NSString *father = @"";
  85. NSMutableArray *filesID = [NSMutableArray new];
  86. for (CCMetadata *metadata in metadatas) {
  87. // type of file
  88. NSInteger typeFilename = [CCUtility getTypeFileName:metadata.fileName];
  89. // do not insert cryptated favorite file
  90. if (typeFilename == k_metadataTypeFilenameCrypto || typeFilename == k_metadataTypeFilenamePlist)
  91. continue;
  92. // Delete Record NOT in session
  93. [CCCoreData deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@) AND (fileID = %@) AND ((session == NULL) OR (session == ''))", app.activeAccount, metadata.directoryID, metadata.fileID]];
  94. // end test, insert in CoreData
  95. [CCCoreData addMetadata:metadata activeAccount:app.activeAccount activeUrl:app.activeUrl context:nil];
  96. // insert for test NOT favorite
  97. [filesID addObject:metadata.fileID];
  98. // ---- Synchronized ----
  99. // Get ServerUrl
  100. NSString* serverUrl = [CCCoreData getServerUrlFromDirectoryID:metadata.directoryID activeAccount:app.activeAccount];
  101. serverUrl = [CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileNameData];
  102. if (![serverUrl containsString:father]) {
  103. if (metadata.directory) {
  104. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:serverUrl activeAccount:app.activeAccount];
  105. NSString *selector;
  106. if ([CCUtility getFavoriteOffline])
  107. selector = selectorReadFolderWithDownload;
  108. else
  109. selector = selectorReadFolder;
  110. [self readFolderServerUrl:serverUrl directoryID:directoryID selector:selector];
  111. } else {
  112. if ([CCUtility getFavoriteOffline])
  113. [self readFile:metadata withDownload:YES];
  114. else
  115. [self readFile:metadata withDownload:NO];
  116. }
  117. father = serverUrl;
  118. }
  119. }
  120. // Verify remove favorite
  121. NSArray *allRecordFavorite = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (favorite == 1)", app.activeAccount] context:nil];
  122. for (TableMetadata *tableMetadata in allRecordFavorite)
  123. if (![filesID containsObject:tableMetadata.fileID])
  124. [CCCoreData setMetadataFavoriteFileID:tableMetadata.fileID favorite:NO activeAccount:app.activeAccount context:nil];
  125. [[NSNotificationCenter defaultCenter] postNotificationName:@"clearDateReadDataSource" object:nil];
  126. }
  127. - (void)listingFavoritesFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  128. {
  129. }
  130. #pragma --------------------------------------------------------------------------------------------
  131. #pragma mark ===== Read Offline =====
  132. #pragma --------------------------------------------------------------------------------------------
  133. - (void)readOffline
  134. {
  135. // test
  136. if (app.activeAccount.length == 0)
  137. return;
  138. // verify is offline procedure is in progress selectorDownloadSynchronize
  139. if ([[app verifyExistsInQueuesDownloadSelector:selectorDownloadSynchronize] count] > 0)
  140. return;
  141. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  142. NSString *father = @"";
  143. NSArray *directories = [CCCoreData getOfflineDirectoryActiveAccount:app.activeAccount];
  144. for (TableDirectory *directory in directories) {
  145. if (![directory.serverUrl containsString:father]) {
  146. father = directory.serverUrl;
  147. [self readFolderServerUrl:directory.serverUrl directoryID:directory.directoryID selector:selectorReadFolder];
  148. }
  149. }
  150. NSArray *metadatas = [CCCoreData getOfflineLocalFileActiveAccount:app.activeAccount directoryUser:app.directoryUser];
  151. for (CCMetadata *metadata in metadatas) {
  152. [self readFile:metadata withDownload:YES];
  153. }
  154. });
  155. }
  156. //
  157. // Add Folder offline
  158. //
  159. - (void)addOfflineFolder:(NSString *)serverUrl
  160. {
  161. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:serverUrl activeAccount:app.activeAccount];
  162. // Set offline directory
  163. [CCCoreData setOfflineDirectoryServerUrl:serverUrl offline:YES activeAccount:app.activeAccount];
  164. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  165. metadataNet.action = actionReadFolder;
  166. metadataNet.directoryID = directoryID;
  167. metadataNet.priority = NSOperationQueuePriorityNormal;
  168. metadataNet.selector = selectorReadFolder;
  169. metadataNet.serverUrl = serverUrl;
  170. [app addNetworkingOperationQueue:app.netQueue delegate:self metadataNet:metadataNet];
  171. }
  172. #pragma --------------------------------------------------------------------------------------------
  173. #pragma mark ===== Read Folder =====
  174. #pragma --------------------------------------------------------------------------------------------
  175. // MULTI THREAD
  176. - (void)readFolderServerUrl:(NSString *)serverUrl directoryID:(NSString *)directoryID selector:(NSString *)selector
  177. {
  178. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  179. metadataNet.action = actionReadFolder;
  180. metadataNet.directoryID = directoryID;
  181. metadataNet.priority = NSOperationQueuePriorityNormal;
  182. metadataNet.selector = selector;
  183. metadataNet.serverUrl = serverUrl;
  184. dispatch_async(dispatch_get_main_queue(), ^{
  185. [app addNetworkingOperationQueue:app.netQueue delegate:self metadataNet:metadataNet];
  186. });
  187. NSLog(@"[LOG] %@ directory : %@", selector, serverUrl);
  188. }
  189. - (void)readFolderFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  190. {
  191. // verify active user
  192. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  193. // Folder not present, remove it
  194. if (errorCode == 404 && [recordAccount.account isEqualToString:metadataNet.account])
  195. [CCCoreData deleteDirectoryAndSubDirectory:metadataNet.serverUrl activeAccount:app.activeAccount];
  196. }
  197. // MULTI THREAD
  198. - (void)readFolderSuccess:(CCMetadataNet *)metadataNet permissions:(NSString *)permissions etag:(NSString *)etag metadatas:(NSArray *)metadatas
  199. {
  200. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  201. __block NSMutableArray *metadatasForVerifyChange = [NSMutableArray new];
  202. if ([recordAccount.account isEqualToString:metadataNet.account] == NO)
  203. return;
  204. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  205. NSArray *recordsInSessions = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@) AND (session != NULL) AND (session != '')", app.activeAccount, metadataNet.directoryID] context:nil];
  206. // ----- Test : (DELETE) -----
  207. NSMutableArray *metadatasNotPresents = [[NSMutableArray alloc] init];
  208. NSArray *tableMetadatas = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@) AND ((session == NULL) OR (session == ''))", app.activeAccount, metadataNet.directoryID] context:nil];
  209. for (TableMetadata *tableMetadata in tableMetadatas) {
  210. // reject cryptated
  211. if (tableMetadata.cryptated)
  212. continue;
  213. BOOL fileIDFound = NO;
  214. for (CCMetadata *metadata in metadatas) {
  215. if ([tableMetadata.fileID isEqualToString:metadata.fileID]) {
  216. fileIDFound = YES;
  217. break;
  218. }
  219. }
  220. if (!fileIDFound)
  221. [metadatasNotPresents addObject:[CCCoreData insertEntityInMetadata:tableMetadata]];
  222. }
  223. dispatch_async(dispatch_get_main_queue(), ^{
  224. // delete metadata not present
  225. for (CCMetadata *metadata in metadatasNotPresents) {
  226. [CCCoreData deleteFile:metadata serverUrl:metadataNet.serverUrl directoryUser:app.directoryUser activeAccount:app.activeAccount];
  227. }
  228. if ([metadatasNotPresents count] > 0)
  229. [app.activeMain reloadDatasource:metadataNet.serverUrl fileID:nil selector:nil];
  230. });
  231. // ----- Test : (MODIFY) -----
  232. for (CCMetadata *metadata in metadatas) {
  233. // reject cryptated
  234. if (metadata.cryptated)
  235. continue;
  236. // dir recursive
  237. if (metadata.directory) {
  238. NSString *serverUrl = [CCUtility stringAppendServerUrl:metadataNet.serverUrl addFileName:metadata.fileNameData];
  239. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:serverUrl activeAccount:app.activeAccount];
  240. // Verify if do not exists this Metadata
  241. if (![CCCoreData getTableMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", metadataNet.account, metadata.fileID]]) {
  242. dispatch_async(dispatch_get_main_queue(), ^{
  243. [CCCoreData addMetadata:metadata activeAccount:app.activeAccount activeUrl:app.activeUrl context:nil];
  244. });
  245. }
  246. // Load if different etag
  247. TableDirectory *tableDirectory = [CCCoreData getTableDirectoryWithPreficate:[NSPredicate predicateWithFormat:@"(account == %@) AND (serverUrl == %@)", metadataNet.account, serverUrl]];
  248. if (![tableDirectory.rev isEqualToString:metadata.rev]) {
  249. [self readFolderServerUrl:serverUrl directoryID:directoryID selector:metadataNet.selector];
  250. [CCCoreData updateDirectoryEtagServerUrl:serverUrl etag:metadata.rev activeAccount:metadataNet.account];
  251. }
  252. } else {
  253. if ([metadataNet.selector isEqualToString:selectorReadFolderWithDownload]) {
  254. // It's in session
  255. BOOL recordInSession = NO;
  256. for (TableMetadata *record in recordsInSessions) {
  257. if ([record.fileID isEqualToString:metadata.fileID]) {
  258. recordInSession = YES;
  259. break;
  260. }
  261. }
  262. if (recordInSession)
  263. continue;
  264. // Ohhhh INSERT
  265. [metadatasForVerifyChange addObject:metadata];
  266. }
  267. if ([metadataNet.selector isEqualToString:selectorReadFolder]) {
  268. // Verify if do not exists this Metadata
  269. if (![CCCoreData getTableMetadataWithPreficate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", metadataNet.account, metadata.fileID]]) {
  270. dispatch_async(dispatch_get_main_queue(), ^{
  271. [CCCoreData addMetadata:metadata activeAccount:metadataNet.account activeUrl:metadataNet.serverUrl context:nil];
  272. });
  273. }
  274. }
  275. }
  276. }
  277. if ([metadatasForVerifyChange count] > 0)
  278. [self verifyChangeMedatas:metadatasForVerifyChange serverUrl:metadataNet.serverUrl account:metadataNet.account withDownload:YES];
  279. });
  280. }
  281. #pragma --------------------------------------------------------------------------------------------
  282. #pragma mark ===== Read File =====
  283. #pragma --------------------------------------------------------------------------------------------
  284. - (void)readFile:(CCMetadata *)metadata withDownload:(BOOL)withDownload
  285. {
  286. NSString *serverUrl = [CCCoreData getServerUrlFromDirectoryID:metadata.directoryID activeAccount:app.activeAccount];
  287. if (serverUrl == nil) return;
  288. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  289. metadataNet.action = actionReadFile;
  290. metadataNet.fileID = metadata.fileID;
  291. metadataNet.fileName = metadata.fileName;
  292. metadataNet.fileNamePrint = metadata.fileNamePrint;
  293. metadataNet.options = [NSNumber numberWithBool:withDownload] ;
  294. metadataNet.priority = NSOperationQueuePriorityLow;
  295. metadataNet.selector = selectorReadFile;
  296. metadataNet.serverUrl = serverUrl;
  297. [app addNetworkingOperationQueue:app.netQueue delegate:self metadataNet:metadataNet];
  298. }
  299. - (void)readFileFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  300. {
  301. // verify active user
  302. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  303. // File not present, remove it
  304. if (errorCode == 404 && [recordAccount.account isEqualToString:metadataNet.account]) {
  305. [CCCoreData deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", metadataNet.account, metadataNet.fileID]];
  306. [CCCoreData deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", metadataNet.account, metadataNet.fileID]];
  307. }
  308. }
  309. - (void)readFileSuccess:(CCMetadataNet *)metadataNet metadata:(CCMetadata *)metadata
  310. {
  311. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
  312. BOOL withDownload = [metadataNet.options boolValue];
  313. [self verifyChangeMedatas:[[NSArray alloc] initWithObjects:metadata, nil] serverUrl:metadataNet.serverUrl account:app.activeAccount withDownload:withDownload];
  314. });
  315. }
  316. #pragma --------------------------------------------------------------------------------------------
  317. #pragma mark ===== Verify Metadatas =====
  318. #pragma --------------------------------------------------------------------------------------------
  319. // MULTI THREAD
  320. - (void)verifyChangeMedatas:(NSArray *)allRecordMetadatas serverUrl:(NSString *)serverUrl account:(NSString *)account withDownload:(BOOL)withDownload
  321. {
  322. NSMutableArray *metadatas = [[NSMutableArray alloc] init];
  323. for (CCMetadata *metadata in allRecordMetadatas) {
  324. BOOL changeRev = NO;
  325. // change account
  326. if ([metadata.account isEqualToString:account] == NO)
  327. return;
  328. // no dir
  329. if (metadata.directory)
  330. continue;
  331. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", app.activeAccount, metadata.fileID]];
  332. if (withDownload) {
  333. if (![record.rev isEqualToString:metadata.rev])
  334. changeRev = YES;
  335. } else {
  336. if (record && ![record.rev isEqualToString:metadata.rev]) // it must be in TableRecord
  337. changeRev = YES;
  338. }
  339. if (changeRev) {
  340. if ([metadata.type isEqualToString: k_metadataType_file]) {
  341. // remove file and ico
  342. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID] error:nil];
  343. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID] error:nil];
  344. }
  345. if ([metadata.type isEqualToString: k_metadataType_template]) {
  346. // remove model
  347. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileName] error:nil];
  348. }
  349. [metadatas addObject:metadata];
  350. }
  351. }
  352. dispatch_async(dispatch_get_main_queue(), ^{
  353. if ([metadatas count])
  354. [self SynchronizeMetadatas:metadatas serverUrl:serverUrl withDownload:withDownload];
  355. });
  356. }
  357. // MAIN THREAD
  358. - (void)SynchronizeMetadatas:(NSArray *)metadatas serverUrl:(NSString *)serverUrl withDownload:(BOOL)withDownload
  359. {
  360. // HUD
  361. if ([metadatas count] > 50 && withDownload) {
  362. if (!_hud) _hud = [[CCHud alloc] initWithView:[[[UIApplication sharedApplication] delegate] window]];
  363. [_hud visibleIndeterminateHud];
  364. }
  365. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  366. NSString *oldDirectoryID, *serverUrl;
  367. for (CCMetadata *metadata in metadatas) {
  368. NSString *selector, *selectorPost;
  369. BOOL downloadData, downloadPlist;
  370. // it's a offline ?
  371. BOOL isOffline = [CCCoreData isOfflineLocalFileID:metadata.fileID activeAccount:app.activeAccount];
  372. if (isOffline)
  373. selectorPost = selectorAddOffline;
  374. if ([metadata.type isEqualToString: k_metadataType_file]) {
  375. downloadData = YES;
  376. selector = selectorDownloadSynchronize;
  377. }
  378. if ([metadata.type isEqualToString: k_metadataType_template]) {
  379. downloadPlist = YES;
  380. selector = selectorLoadPlist;
  381. }
  382. // Clear date for dorce refresh view
  383. if (![oldDirectoryID isEqualToString:metadata.directoryID]) {
  384. serverUrl = [CCCoreData getServerUrlFromDirectoryID:metadata.directoryID activeAccount:app.activeAccount];
  385. oldDirectoryID = metadata.directoryID;
  386. [CCCoreData clearDateReadAccount:app.activeAccount serverUrl:serverUrl directoryID:nil];
  387. }
  388. [CCCoreData addMetadata:metadata activeAccount:app.activeAccount activeUrl:serverUrl context:nil];
  389. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  390. metadataNet.action = actionDownloadFile;
  391. metadataNet.metadata = metadata;
  392. metadataNet.downloadData = downloadData;
  393. metadataNet.downloadPlist = downloadPlist;
  394. metadataNet.selector = selector;
  395. metadataNet.selectorPost = selectorPost;
  396. metadataNet.serverUrl = serverUrl;
  397. metadataNet.session = k_download_session;
  398. metadataNet.taskStatus = k_taskStatusResume;
  399. [app addNetworkingOperationQueue:app.netQueueDownload delegate:app.activeMain metadataNet:metadataNet];
  400. }
  401. [[CCSynchronize sharedSynchronize] synchronizeFolderAnimationDirectory:[[NSArray alloc] initWithObjects:serverUrl, nil] setGraphicsFolder:YES];
  402. [app.activeMain reloadDatasource:serverUrl fileID:nil selector:nil];
  403. [_hud hideHud];
  404. });
  405. }
  406. #pragma --------------------------------------------------------------------------------------------
  407. #pragma mark ===== Synchronize Folder Animation =====
  408. #pragma --------------------------------------------------------------------------------------------
  409. - (BOOL)synchronizeFolderAnimationDirectory:(NSArray *)directory setGraphicsFolder:(BOOL)setGraphicsFolder
  410. {
  411. BOOL animation = NO;
  412. BOOL isAtLeastOneInAnimation = NO;
  413. NSMutableOrderedSet *serversUrlInDownload = [NSMutableOrderedSet new];
  414. // test
  415. if ([directory count] == 0 && [self.foldersInSynchronized count] == 0)
  416. return isAtLeastOneInAnimation;
  417. if (directory)
  418. [self.foldersInSynchronized addObjectsFromArray:directory];
  419. else
  420. directory = [[NSArray alloc] initWithArray:self.foldersInSynchronized.array];
  421. // Active in download
  422. NSMutableArray *metadatasNet = [app verifyExistsInQueuesDownloadSelector:selectorDownloadSynchronize];
  423. for (CCMetadataNet *metadataNet in metadatasNet)
  424. [serversUrlInDownload addObject:metadataNet.serverUrl];
  425. // Animation ON/OFF
  426. for (NSString *serverUrl in directory) {
  427. animation = [serversUrlInDownload containsObject:serverUrl];
  428. if (animation)
  429. isAtLeastOneInAnimation = YES;
  430. else
  431. [self.foldersInSynchronized removeObject:serverUrl];
  432. if (setGraphicsFolder) {
  433. NSString *serverUrlOffline = [CCUtility deletingLastPathComponentFromServerUrl:serverUrl];
  434. CCMain *viewController = [app.listMainVC objectForKey:serverUrlOffline];
  435. if (viewController)
  436. [viewController synchronizeFolderGraphicsServerUrl:serverUrl animation:animation];
  437. }
  438. }
  439. return isAtLeastOneInAnimation;
  440. }
  441. @end