CCOfflineFileFolder.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. //
  2. // CCOfflineFileFolder.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 "CCOfflineFileFolder.h"
  24. #import "AppDelegate.h"
  25. #import "CCCoreData.h"
  26. #import "CCMain.h"
  27. @interface CCOfflineFileFolder ()
  28. {
  29. // local
  30. }
  31. @end
  32. @implementation CCOfflineFileFolder
  33. + (CCOfflineFileFolder *)sharedOfflineFileFolder{
  34. static CCOfflineFileFolder *sharedOfflineFileFolder;
  35. @synchronized(self)
  36. {
  37. if (!sharedOfflineFileFolder) {
  38. sharedOfflineFileFolder = [[CCOfflineFileFolder alloc] init];
  39. }
  40. return sharedOfflineFileFolder;
  41. }
  42. }
  43. #pragma --------------------------------------------------------------------------------------------
  44. #pragma mark ===== Read Folder Offline =====
  45. #pragma --------------------------------------------------------------------------------------------
  46. - (void)readFolderOffline
  47. {
  48. if ([app.activeAccount length] == 0)
  49. return;
  50. // verify is offline procedure is in progress selectorDownloadOffline
  51. if ([[app verifyExistsInQueuesDownloadSelector:selectorDownloadOffline] count] > 0)
  52. return;
  53. NSString *father = @"";
  54. NSArray *directories = [CCCoreData getOfflineDirectoryActiveAccount:app.activeAccount];
  55. for (TableDirectory *directory in directories) {
  56. if (![directory.serverUrl containsString:father]) {
  57. father = directory.serverUrl;
  58. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  59. metadataNet.action = actionReadFolder;
  60. metadataNet.date = [NSDate date];
  61. metadataNet.directoryID = directory.directoryID;
  62. metadataNet.priority = NSOperationQueuePriorityVeryLow;
  63. metadataNet.selector = selectorOfflineFolder;
  64. metadataNet.serverUrl = directory.serverUrl;
  65. [app addNetworkingOperationQueue:app.netQueue delegate:self metadataNet:metadataNet];
  66. NSLog(@"[LOG] Read offline directory : %@", directory.serverUrl);
  67. }
  68. }
  69. }
  70. //
  71. // Add Folder offline
  72. //
  73. - (void)addOfflineFolder:(NSString *)serverUrl
  74. {
  75. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:serverUrl activeAccount:app.activeAccount];
  76. // Set offline directory
  77. [CCCoreData setOfflineDirectory:serverUrl offline:YES activeAccount:app.activeAccount];
  78. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  79. metadataNet.action = actionReadFolder;
  80. metadataNet.directoryID = directoryID;
  81. metadataNet.priority = NSOperationQueuePriorityVeryHigh;
  82. metadataNet.selector = selectorReadFolder;
  83. metadataNet.serverUrl = serverUrl;
  84. [app addNetworkingOperationQueue:app.netQueue delegate:self metadataNet:metadataNet];
  85. NSLog(@"[LOG] Read offline directory : %@", serverUrl);
  86. }
  87. #pragma --------------------------------------------------------------------------------------------
  88. #pragma mark ===== Read Folder offline =====
  89. #pragma --------------------------------------------------------------------------------------------
  90. - (void)readFolderFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  91. {
  92. // verify active user
  93. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  94. // Folder not present, remove it
  95. if (errorCode == 404 && [recordAccount.account isEqualToString:metadataNet.account])
  96. [CCCoreData deleteDirectoryAndSubDirectory:metadataNet.serverUrl activeAccount:app.activeAccount];
  97. }
  98. // MULTI THREAD
  99. - (void)readFolderSuccess:(CCMetadataNet *)metadataNet permissions:(NSString *)permissions rev:(NSString *)rev metadatas:(NSArray *)metadatas
  100. {
  101. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  102. __block NSMutableArray *metadatasForOfflineFolder = [[NSMutableArray alloc] init];
  103. if ([recordAccount.account isEqualToString:metadataNet.account] == NO && [metadataNet.selector isEqualToString:selectorOfflineFolder])
  104. return;
  105. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  106. NSArray *recordsInSessions = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@) AND (session != NULL) AND (session != '')", app.activeAccount, metadataNet.directoryID] context:nil];
  107. // ----- Test metadata not present (DELETE) -----
  108. NSMutableArray *metadatasNotPresents = [[NSMutableArray alloc] init];
  109. NSArray *metadatasInDB = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", app.activeAccount, metadataNet.directoryID] context:nil];
  110. for (CCMetadata *metadataDB in metadatasInDB) {
  111. BOOL fileIDFound = NO;
  112. for (CCMetadata *metadata in metadatas) {
  113. if ([metadataDB.fileID isEqualToString:metadata.fileID]) {
  114. fileIDFound = YES;
  115. break;
  116. }
  117. }
  118. if (!fileIDFound)
  119. [metadatasNotPresents addObject:metadataDB];
  120. }
  121. dispatch_async(dispatch_get_main_queue(), ^{
  122. // delete metadata not present
  123. for (CCMetadata *metadata in metadatasNotPresents)
  124. [CCCoreData deleteFile:metadata serverUrl:metadataNet.serverUrl directoryUser:app.directoryUser typeCloud:app.typeCloud activeAccount:app.activeAccount];
  125. [app.activeMain getDataSourceWithReloadTableView:metadataNet.directoryID fileID:nil selector:nil];
  126. });
  127. // ----- Search metadata for test change metadata (MODIFY) -----
  128. for (CCMetadata *metadata in metadatas) {
  129. // dir recursive
  130. if (metadata.directory) {
  131. dispatch_async(dispatch_get_main_queue(), ^{
  132. NSString *dir = [CCUtility stringAppendServerUrl:metadataNet.serverUrl addServerUrl:metadata.fileNameData];
  133. [CCCoreData addMetadata:metadata activeAccount:app.activeAccount activeUrl:app.activeUrl typeCloud:app.typeCloud context:nil];
  134. [[CCOfflineFileFolder sharedOfflineFileFolder] addOfflineFolder:dir];
  135. });
  136. } else {
  137. NSInteger typeFilename = [CCUtility getTypeFileName:metadata.fileName];
  138. // reject crypto
  139. if (typeFilename == metadataTypeFilenameCrypto) continue;
  140. // Verify if the plist is complited
  141. if (typeFilename == metadataTypeFilenamePlist) {
  142. BOOL isCryptoComplete = NO;
  143. NSString *fileNameCrypto = [CCUtility trasformedFileNamePlistInCrypto:metadata.fileName];
  144. for (CCMetadata *completeMetadata in metadatas) {
  145. if (completeMetadata.cryptated == NO) continue;
  146. else if ([completeMetadata.fileName isEqualToString:fileNameCrypto]) {
  147. isCryptoComplete = YES;
  148. break;
  149. }
  150. }
  151. if (isCryptoComplete == NO) continue;
  152. }
  153. // Error password
  154. if (metadata.errorPasscode)
  155. continue;
  156. // Plist not download
  157. if (metadata.cryptated && [metadata.title length] == 0)
  158. continue;
  159. // It's in session
  160. BOOL recordInSession = NO;
  161. for (TableMetadata *record in recordsInSessions) {
  162. if ([record.fileID isEqualToString:metadata.fileID]) {
  163. recordInSession = YES;
  164. break;
  165. }
  166. }
  167. if (recordInSession)
  168. continue;
  169. // Ohhhh INSERT
  170. [metadatasForOfflineFolder addObject:metadata];
  171. }
  172. }
  173. if ([metadatasForOfflineFolder count] > 0)
  174. [self verifyChangeMedatas:metadatasForOfflineFolder serverUrl:metadataNet.serverUrl directoryID:metadataNet.directoryID account:metadataNet.account offline:YES];
  175. });
  176. }
  177. // MULTI THREAD
  178. - (void)verifyChangeMedatas:(NSArray *)allRecordMetadatas serverUrl:(NSString *)serverUrl directoryID:(NSString *)directoryID account:(NSString *)account offline:(BOOL)offline
  179. {
  180. NSMutableArray *metadatas = [[NSMutableArray alloc] init];
  181. for (CCMetadata *metadata in allRecordMetadatas) {
  182. BOOL changeRev = NO;
  183. // change account
  184. if ([metadata.account isEqualToString:account] == NO)
  185. return;
  186. // no dir
  187. if (metadata.directory)
  188. continue;
  189. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", app.activeAccount, metadata.fileID]];
  190. if (offline) {
  191. // add flag offline
  192. dispatch_async(dispatch_get_main_queue(), ^{
  193. [CCCoreData addOffline:metadata.fileID activeAccount:app.activeAccount];
  194. });
  195. if (![record.rev isEqualToString:metadata.rev ])
  196. changeRev = YES;
  197. } else {
  198. if (record && ![record.rev isEqualToString:metadata.rev ])
  199. changeRev = YES;
  200. }
  201. if (changeRev) {
  202. if ([metadata.type isEqualToString:metadataType_file]) {
  203. // remove file and ico
  204. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID] error:nil];
  205. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID] error:nil];
  206. }
  207. if ([metadata.type isEqualToString:metadataType_model]) {
  208. // remove model
  209. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileName] error:nil];
  210. }
  211. [metadatas addObject:metadata];
  212. }
  213. }
  214. dispatch_async(dispatch_get_main_queue(), ^{
  215. if ([metadatas count])
  216. [self offlineMetadatas:metadatas serverUrl:serverUrl directoryID:directoryID offline:offline];
  217. });
  218. }
  219. // MAIN THREAD
  220. - (void)offlineMetadatas:(NSArray *)metadatas serverUrl:(NSString *)serverUrl directoryID:(NSString *)directoryID offline:(BOOL)offline
  221. {
  222. // HUD
  223. if ([metadatas count] > 50 && offline) {
  224. if (!_hud) _hud = [[CCHud alloc] initWithView:[[[UIApplication sharedApplication] delegate] window]];
  225. [_hud visibleIndeterminateHud];
  226. }
  227. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  228. for (CCMetadata *metadata in metadatas) {
  229. NSString *selector, *selectorPost;
  230. BOOL downloadData, downloadPlist;
  231. // it's a offline ?
  232. BOOL isOffline = [CCCoreData isOffline:metadata.fileID activeAccount:app.activeAccount];
  233. if (isOffline)
  234. selectorPost = selectorAddOffline;
  235. if ([metadata.type isEqualToString:metadataType_file]) {
  236. downloadData = YES;
  237. selector = selectorDownloadOffline;
  238. }
  239. if ([metadata.type isEqualToString:metadataType_model]) {
  240. downloadPlist = YES;
  241. selector = selectorLoadPlist;
  242. }
  243. [CCCoreData addMetadata:metadata activeAccount:app.activeAccount activeUrl:serverUrl typeCloud:app.typeCloud context:nil];
  244. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  245. metadataNet.action = actionDownloadFile;
  246. metadataNet.metadata = metadata;
  247. metadataNet.downloadData = downloadData;
  248. metadataNet.downloadPlist = downloadPlist;
  249. metadataNet.selector = selector;
  250. metadataNet.selectorPost = selectorPost;
  251. metadataNet.serverUrl = serverUrl;
  252. metadataNet.session = download_session;
  253. metadataNet.taskStatus = taskStatusResume;
  254. [app addNetworkingOperationQueue:app.netQueueDownload delegate:app.activeMain metadataNet:metadataNet];
  255. }
  256. [[CCOfflineFileFolder sharedOfflineFileFolder] offlineFolderAnimationDirectory:[[NSArray alloc] initWithObjects:serverUrl, nil] callViewController:YES];
  257. [app.activeMain getDataSourceWithReloadTableView:directoryID fileID:nil selector:nil];
  258. [_hud hideHud];
  259. });
  260. }
  261. // Graphics Animation Offline Folders
  262. //
  263. // User return BOOL animation for 1 directory only
  264. //
  265. - (BOOL)offlineFolderAnimationDirectory:(NSArray *)directory callViewController:(BOOL)callViewController
  266. {
  267. BOOL animation = NO;
  268. NSMutableOrderedSet *serversUrlInDownload = [[NSMutableOrderedSet alloc] init];
  269. NSMutableArray *metadatasNet = [app verifyExistsInQueuesDownloadSelector:selectorDownloadOffline];
  270. for (CCMetadataNet *metadataNet in metadatasNet)
  271. [serversUrlInDownload addObject:metadataNet.serverUrl];
  272. /* Animation ON/OFF */
  273. for (NSString *serverUrl in directory) {
  274. animation = [serversUrlInDownload containsObject:serverUrl];
  275. if (callViewController) {
  276. NSString *serverUrlOffline = [CCUtility deletingLastPathComponentFromServerUrl:serverUrl];
  277. CCMain *viewController = [app.listMainVC objectForKey:serverUrlOffline];
  278. if (viewController)
  279. [viewController offlineFolderGraphicsServerUrl:serverUrl animation:animation];
  280. }
  281. }
  282. return animation;
  283. }
  284. @end