CCOfflineFileFolder.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. // Graphics Animation Offline Folders
  88. //
  89. // User return BOOL animation for 1 directory only
  90. //
  91. - (BOOL)offlineFolderAnimationDirectory:(NSArray *)directory callViewController:(BOOL)callViewController
  92. {
  93. BOOL animation = NO;
  94. NSMutableOrderedSet *serversUrlInDownload = [[NSMutableOrderedSet alloc] init];
  95. NSMutableArray *metadatasNet = [app verifyExistsInQueuesDownloadSelector:selectorDownloadOffline];
  96. for (CCMetadataNet *metadataNet in metadatasNet)
  97. [serversUrlInDownload addObject:metadataNet.serverUrl];
  98. /* Animation ON/OFF */
  99. for (NSString *serverUrl in directory) {
  100. animation = [serversUrlInDownload containsObject:serverUrl];
  101. if (callViewController) {
  102. NSString *serverUrlOffline = [CCUtility deletingLastPathComponentFromServerUrl:serverUrl];
  103. CCMain *viewController = [app.listMainVC objectForKey:serverUrlOffline];
  104. if (viewController)
  105. [viewController offlineFolderGraphicsServerUrl:serverUrl animation:animation];
  106. }
  107. }
  108. return animation;
  109. }
  110. - (void)readFolderFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  111. {
  112. // verify active user
  113. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  114. // Folder not present, remove it
  115. if (errorCode == 404 && [recordAccount.account isEqualToString:metadataNet.account])
  116. [CCCoreData deleteDirectoryAndSubDirectory:metadataNet.serverUrl activeAccount:app.activeAccount];
  117. }
  118. // MULTI THREAD
  119. - (void)readFolderSuccess:(CCMetadataNet *)metadataNet permissions:(NSString *)permissions rev:(NSString *)rev metadatas:(NSArray *)metadatas
  120. {
  121. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  122. __block NSMutableArray *metadatasForOfflineFolder = [[NSMutableArray alloc] init];
  123. if ([recordAccount.account isEqualToString:metadataNet.account] == NO && [metadataNet.selector isEqualToString:selectorOfflineFolder])
  124. return;
  125. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  126. NSArray *recordsInSessions = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@) AND (session != NULL) AND (session != '')", app.activeAccount, metadataNet.directoryID] context:nil];
  127. // ----- Test metadata not present (DELETE) -----
  128. NSMutableArray *metadatasNotPresents = [[NSMutableArray alloc] init];
  129. NSArray *metadatasInDB = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", app.activeAccount, metadataNet.directoryID] context:nil];
  130. for (CCMetadata *metadataDB in metadatasInDB) {
  131. BOOL fileIDFound = NO;
  132. for (CCMetadata *metadata in metadatas) {
  133. if ([metadataDB.fileID isEqualToString:metadata.fileID]) {
  134. fileIDFound = YES;
  135. break;
  136. }
  137. }
  138. if (!fileIDFound)
  139. [metadatasNotPresents addObject:metadataDB];
  140. }
  141. dispatch_async(dispatch_get_main_queue(), ^{
  142. // delete metadata not present
  143. for (CCMetadata *metadata in metadatasNotPresents)
  144. [CCCoreData deleteFile:metadata serverUrl:metadataNet.serverUrl directoryUser:app.directoryUser typeCloud:app.typeCloud activeAccount:app.activeAccount];
  145. [app.activeMain getDataSourceWithReloadTableView:metadataNet.directoryID fileID:nil selector:nil];
  146. });
  147. // ----- Search metadata for test change metadata (MODIFY) -----
  148. for (CCMetadata *metadata in metadatas) {
  149. // dir recursive
  150. if (metadata.directory) {
  151. dispatch_async(dispatch_get_main_queue(), ^{
  152. NSString *dir = [CCUtility stringAppendServerUrl:metadataNet.serverUrl addServerUrl:metadata.fileNameData];
  153. [CCCoreData addMetadata:metadata activeAccount:app.activeAccount activeUrl:app.activeUrl typeCloud:app.typeCloud context:nil];
  154. [[CCOfflineFileFolder sharedOfflineFileFolder] addOfflineFolder:dir];
  155. });
  156. } else {
  157. NSInteger typeFilename = [CCUtility getTypeFileName:metadata.fileName];
  158. // reject crypto
  159. if (typeFilename == metadataTypeFilenameCrypto) continue;
  160. // Verify if the plist is complited
  161. if (typeFilename == metadataTypeFilenamePlist) {
  162. BOOL isCryptoComplete = NO;
  163. NSString *fileNameCrypto = [CCUtility trasformedFileNamePlistInCrypto:metadata.fileName];
  164. for (CCMetadata *completeMetadata in metadatas) {
  165. if (completeMetadata.cryptated == NO) continue;
  166. else if ([completeMetadata.fileName isEqualToString:fileNameCrypto]) {
  167. isCryptoComplete = YES;
  168. break;
  169. }
  170. }
  171. if (isCryptoComplete == NO) continue;
  172. }
  173. // Error password
  174. if (metadata.errorPasscode)
  175. continue;
  176. // Plist not download
  177. if (metadata.cryptated && [metadata.title length] == 0)
  178. continue;
  179. // It's in session
  180. BOOL recordInSession = NO;
  181. for (TableMetadata *record in recordsInSessions) {
  182. if ([record.fileID isEqualToString:metadata.fileID]) {
  183. recordInSession = YES;
  184. break;
  185. }
  186. }
  187. if (recordInSession)
  188. continue;
  189. // Ohhhh INSERT
  190. [metadatasForOfflineFolder addObject:metadata];
  191. }
  192. }
  193. if ([metadatasForOfflineFolder count] > 0)
  194. [self verifyChangeMedatas:metadatasForOfflineFolder serverUrl:metadataNet.serverUrl directoryID:metadataNet.directoryID account:metadataNet.account offline:YES];
  195. });
  196. }
  197. #pragma --------------------------------------------------------------------------------------------
  198. #pragma mark ===== Read File Offline =====
  199. #pragma --------------------------------------------------------------------------------------------
  200. - (void)readFileOffline
  201. {
  202. if (app.activeAccount == nil || [CCUtility getHomeServerUrlActiveUrl:app.activeUrl typeCloud:app.typeCloud] == nil)
  203. return;
  204. NSArray *metadatas = [CCCoreData getOfflineLocalFileActiveAccount:app.activeAccount directoryUser:app.directoryUser];
  205. for (CCMetadata *metadata in metadatas) {
  206. NSString *serverUrl = [CCCoreData getServerUrlFromDirectoryID:metadata.directoryID activeAccount:app.activeAccount];
  207. if (serverUrl == nil) continue;
  208. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  209. metadataNet.action = actionReadFile;
  210. metadataNet.fileID = metadata.fileID;
  211. metadataNet.fileName = metadata.fileName;
  212. metadataNet.fileNamePrint = metadata.fileNamePrint;
  213. metadataNet.serverUrl = serverUrl;
  214. metadataNet.selector = selectorReadFileOffline;
  215. metadataNet.priority = NSOperationQueuePriorityVeryLow;
  216. [app addNetworkingOperationQueue:app.netQueue delegate:self metadataNet:metadataNet];
  217. }
  218. }
  219. - (void)readFileFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  220. {
  221. // verify active user
  222. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  223. // File not present, remove it
  224. if (errorCode == 404 && [recordAccount.account isEqualToString:metadataNet.account]) {
  225. [CCCoreData deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", metadataNet.account, metadataNet.fileID]];
  226. [CCCoreData deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", metadataNet.account, metadataNet.fileID]];
  227. }
  228. }
  229. - (void)readFileSuccess:(CCMetadataNet *)metadataNet metadata:(CCMetadata *)metadata
  230. {
  231. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
  232. [self verifyChangeMedatas:[[NSArray alloc] initWithObjects:metadata, nil] serverUrl:metadataNet.serverUrl directoryID:metadataNet.directoryID account:app.activeAccount offline:NO];
  233. });
  234. }
  235. #pragma --------------------------------------------------------------------------------------------
  236. #pragma mark ===== Verify Metadatas =====
  237. #pragma --------------------------------------------------------------------------------------------
  238. // MULTI THREAD
  239. - (void)verifyChangeMedatas:(NSArray *)allRecordMetadatas serverUrl:(NSString *)serverUrl directoryID:(NSString *)directoryID account:(NSString *)account offline:(BOOL)offline
  240. {
  241. NSMutableArray *metadatas = [[NSMutableArray alloc] init];
  242. for (CCMetadata *metadata in allRecordMetadatas) {
  243. BOOL changeRev = NO;
  244. // change account
  245. if ([metadata.account isEqualToString:account] == NO)
  246. return;
  247. // no dir
  248. if (metadata.directory)
  249. continue;
  250. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", app.activeAccount, metadata.fileID]];
  251. if (offline) {
  252. // add flag offline
  253. dispatch_async(dispatch_get_main_queue(), ^{
  254. [CCCoreData addOffline:metadata.fileID activeAccount:app.activeAccount];
  255. });
  256. if (![record.rev isEqualToString:metadata.rev ])
  257. changeRev = YES;
  258. } else {
  259. if (record && ![record.rev isEqualToString:metadata.rev ])
  260. changeRev = YES;
  261. }
  262. if (changeRev) {
  263. if ([metadata.type isEqualToString:metadataType_file]) {
  264. // remove file and ico
  265. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID] error:nil];
  266. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID] error:nil];
  267. }
  268. if ([metadata.type isEqualToString:metadataType_model]) {
  269. // remove model
  270. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileName] error:nil];
  271. }
  272. [metadatas addObject:metadata];
  273. }
  274. }
  275. dispatch_async(dispatch_get_main_queue(), ^{
  276. if ([metadatas count])
  277. [self offlineMetadatas:metadatas serverUrl:serverUrl directoryID:directoryID offline:offline];
  278. });
  279. }
  280. // MAIN THREAD
  281. - (void)offlineMetadatas:(NSArray *)metadatas serverUrl:(NSString *)serverUrl directoryID:(NSString *)directoryID offline:(BOOL)offline
  282. {
  283. // HUD
  284. if ([metadatas count] > 50 && offline) {
  285. if (!_hud) _hud = [[CCHud alloc] initWithView:[[[UIApplication sharedApplication] delegate] window]];
  286. [_hud visibleIndeterminateHud];
  287. }
  288. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  289. for (CCMetadata *metadata in metadatas) {
  290. NSString *selector, *selectorPost;
  291. BOOL downloadData, downloadPlist;
  292. // it's a offline ?
  293. BOOL isOffline = [CCCoreData isOffline:metadata.fileID activeAccount:app.activeAccount];
  294. if (isOffline)
  295. selectorPost = selectorAddOffline;
  296. if ([metadata.type isEqualToString:metadataType_file]) {
  297. downloadData = YES;
  298. selector = selectorDownloadOffline;
  299. }
  300. if ([metadata.type isEqualToString:metadataType_model]) {
  301. downloadPlist = YES;
  302. selector = selectorLoadPlist;
  303. }
  304. [CCCoreData addMetadata:metadata activeAccount:app.activeAccount activeUrl:serverUrl typeCloud:app.typeCloud context:nil];
  305. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  306. metadataNet.action = actionDownloadFile;
  307. metadataNet.metadata = metadata;
  308. metadataNet.downloadData = downloadData;
  309. metadataNet.downloadPlist = downloadPlist;
  310. metadataNet.selector = selector;
  311. metadataNet.selectorPost = selectorPost;
  312. metadataNet.serverUrl = serverUrl;
  313. metadataNet.session = download_session;
  314. metadataNet.taskStatus = taskStatusResume;
  315. [app addNetworkingOperationQueue:app.netQueueDownload delegate:app.activeMain metadataNet:metadataNet];
  316. }
  317. [[CCOfflineFileFolder sharedOfflineFileFolder] offlineFolderAnimationDirectory:[[NSArray alloc] initWithObjects:serverUrl, nil] callViewController:YES];
  318. [app.activeMain getDataSourceWithReloadTableView:directoryID fileID:nil selector:nil];
  319. [_hud hideHud];
  320. });
  321. }
  322. @end