CCOfflineFolder.m 14 KB

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