CCSynchronize.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. #import "Nextcloud-Swift.h"
  28. @interface CCSynchronize () <CCActionsListingFavoritesDelegate>
  29. {
  30. // local
  31. }
  32. @end
  33. @implementation CCSynchronize
  34. + (CCSynchronize *)sharedSynchronize{
  35. static CCSynchronize *sharedSynchronize;
  36. @synchronized(self)
  37. {
  38. if (!sharedSynchronize) {
  39. sharedSynchronize = [[CCSynchronize alloc] init];
  40. }
  41. return sharedSynchronize;
  42. }
  43. }
  44. #pragma --------------------------------------------------------------------------------------------
  45. #pragma mark ===== Read Listing Favorites =====
  46. #pragma --------------------------------------------------------------------------------------------
  47. - (void)readListingFavorites
  48. {
  49. // test
  50. if (app.activeAccount.length == 0)
  51. return;
  52. // verify is offline procedure is in progress selectorDownloadFavorites
  53. if ([[app verifyExistsInQueuesDownloadSelector:selectorDownloadFavorites] count] > 0)
  54. return;
  55. [[CCActions sharedInstance] listingFavorites:@"" delegate:self];
  56. }
  57. - (void)addFavoriteFolder:(NSString *)serverUrl
  58. {
  59. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:serverUrl activeAccount:app.activeAccount];
  60. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  61. metadataNet.action = actionReadFolder;
  62. metadataNet.directoryID = directoryID;
  63. metadataNet.priority = NSOperationQueuePriorityVeryHigh;
  64. metadataNet.selector = selectorReadFolder;
  65. metadataNet.serverUrl = serverUrl;
  66. [app addNetworkingOperationQueue:app.netQueue delegate:self metadataNet:metadataNet];
  67. }
  68. - (void)listingFavoritesSuccess:(CCMetadataNet *)metadataNet metadatas:(NSArray *)metadatas
  69. {
  70. // verify active user
  71. TableAccount *record = [CCCoreData getActiveAccount];
  72. if (![record.account isEqualToString:metadataNet.account])
  73. return;
  74. NSString *father = @"";
  75. for (CCMetadata *metadata in metadatas) {
  76. // Delete Record NOT in session
  77. [CCCoreData deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@) AND (fileID = %@) AND ((session == NULL) OR (session == ''))", app.activeAccount, metadata.directoryID, metadata.fileID]];
  78. // type of file
  79. NSInteger typeFilename = [CCUtility getTypeFileName:metadata.fileName];
  80. // if crypto do not insert
  81. if (typeFilename == k_metadataTypeFilenameCrypto) continue;
  82. // verify if the record encrypted has plist + crypto
  83. if (typeFilename == k_metadataTypeFilenamePlist && metadata.directory == NO) {
  84. BOOL isCryptoComplete = NO;
  85. NSString *fileNameCrypto = [CCUtility trasformedFileNamePlistInCrypto:metadata.fileName];
  86. for (CCMetadata *completeMetadata in metadatas) {
  87. if (completeMetadata.cryptated == NO) continue;
  88. else if ([completeMetadata.fileName isEqualToString:fileNameCrypto]) {
  89. isCryptoComplete = YES;
  90. break;
  91. }
  92. }
  93. if (isCryptoComplete == NO) continue;
  94. }
  95. // end test, insert in CoreData
  96. [CCCoreData addMetadata:metadata activeAccount:app.activeAccount activeUrl:app.activeUrl context:nil];
  97. // ---- Synchronized ----
  98. // Get ServerUrl
  99. NSString* serverUrl = [CCCoreData getServerUrlFromDirectoryID:metadata.directoryID activeAccount:app.activeAccount];
  100. serverUrl = [CCUtility stringAppendServerUrl:serverUrl addServerUrl:metadata.fileNameData];
  101. if (![serverUrl containsString:father]) {
  102. if (metadata.directory) {
  103. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:serverUrl activeAccount:app.activeAccount];
  104. [self readFolderServerUrl:serverUrl directoryID:directoryID];
  105. } else {
  106. [self readFile:metadata];
  107. }
  108. father = serverUrl;
  109. }
  110. }
  111. }
  112. - (void)listingFavoritesFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  113. {
  114. }
  115. #pragma --------------------------------------------------------------------------------------------
  116. #pragma mark ===== Read Offline =====
  117. #pragma --------------------------------------------------------------------------------------------
  118. - (void)readOffline
  119. {
  120. // test
  121. if (app.activeAccount.length == 0)
  122. return;
  123. // verify is offline procedure is in progress selectorDownloadOffline
  124. if ([[app verifyExistsInQueuesDownloadSelector:selectorDownloadOffline] count] > 0)
  125. return;
  126. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  127. NSString *father = @"";
  128. NSArray *directories = [CCCoreData getOfflineDirectoryActiveAccount:app.activeAccount];
  129. for (TableDirectory *directory in directories) {
  130. if (![directory.serverUrl containsString:father]) {
  131. father = directory.serverUrl;
  132. [self readFolderServerUrl:directory.serverUrl directoryID:directory.directoryID];
  133. }
  134. }
  135. NSArray *metadatas = [CCCoreData getOfflineLocalFileActiveAccount:app.activeAccount directoryUser:app.directoryUser];
  136. for (CCMetadata *metadata in metadatas) {
  137. [self readFile:metadata];
  138. }
  139. });
  140. }
  141. //
  142. // Add Folder offline
  143. //
  144. - (void)addOfflineFolder:(NSString *)serverUrl
  145. {
  146. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:serverUrl activeAccount:app.activeAccount];
  147. // Set offline directory
  148. [CCCoreData setOfflineDirectoryServerUrl:serverUrl offline:YES activeAccount:app.activeAccount];
  149. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  150. metadataNet.action = actionReadFolder;
  151. metadataNet.directoryID = directoryID;
  152. metadataNet.priority = NSOperationQueuePriorityVeryHigh;
  153. metadataNet.selector = selectorReadFolder;
  154. metadataNet.serverUrl = serverUrl;
  155. [app addNetworkingOperationQueue:app.netQueue delegate:self metadataNet:metadataNet];
  156. }
  157. // Graphics Animation Offline Folders
  158. //
  159. //
  160. - (BOOL)offlineFolderAnimationDirectory:(NSArray *)directory setGraphicsFolder:(BOOL)setGraphicsFolder
  161. {
  162. BOOL animation = NO;
  163. BOOL isAtLeastOneInAnimation = NO;
  164. NSMutableOrderedSet *serversUrlInDownload = [[NSMutableOrderedSet alloc] init];
  165. // Active for download
  166. NSMutableArray *metadatasNet = [app verifyExistsInQueuesDownloadSelector:selectorDownloadOffline];
  167. for (CCMetadataNet *metadataNet in metadatasNet)
  168. [serversUrlInDownload addObject:metadataNet.serverUrl];
  169. // Animation ON/OFF
  170. for (NSString *serverUrl in directory) {
  171. animation = [serversUrlInDownload containsObject:serverUrl];
  172. if (animation)
  173. isAtLeastOneInAnimation = YES;
  174. if (setGraphicsFolder) {
  175. NSString *serverUrlOffline = [CCUtility deletingLastPathComponentFromServerUrl:serverUrl];
  176. CCMain *viewController = [app.listMainVC objectForKey:serverUrlOffline];
  177. if (viewController)
  178. [viewController offlineFolderGraphicsServerUrl:serverUrl animation:animation];
  179. }
  180. }
  181. return isAtLeastOneInAnimation;
  182. }
  183. #pragma --------------------------------------------------------------------------------------------
  184. #pragma mark ===== Read Folder =====
  185. #pragma --------------------------------------------------------------------------------------------
  186. // MULTI THREAD
  187. - (void)readFolderServerUrl:(NSString *)serverUrl directoryID:(NSString *)directoryID
  188. {
  189. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  190. metadataNet.action = actionReadFolder;
  191. metadataNet.directoryID = directoryID;
  192. metadataNet.priority = NSOperationQueuePriorityVeryLow;
  193. metadataNet.selector = selectorReadFolder;
  194. metadataNet.serverUrl = serverUrl;
  195. [app addNetworkingOperationQueue:app.netQueue delegate:self metadataNet:metadataNet];
  196. NSLog(@"[LOG] Read offline directory : %@", serverUrl);
  197. }
  198. - (void)readFolderFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  199. {
  200. // verify active user
  201. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  202. // Folder not present, remove it
  203. if (errorCode == 404 && [recordAccount.account isEqualToString:metadataNet.account])
  204. [CCCoreData deleteDirectoryAndSubDirectory:metadataNet.serverUrl activeAccount:app.activeAccount];
  205. }
  206. // MULTI THREAD
  207. - (void)readFolderSuccess:(CCMetadataNet *)metadataNet permissions:(NSString *)permissions metadatas:(NSArray *)metadatas
  208. {
  209. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  210. __block NSMutableArray *metadatasForOfflineFolder = [[NSMutableArray alloc] init];
  211. if ([recordAccount.account isEqualToString:metadataNet.account] == NO)
  212. return;
  213. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  214. NSArray *recordsInSessions = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@) AND (session != NULL) AND (session != '')", app.activeAccount, metadataNet.directoryID] context:nil];
  215. // ----- Test : (DELETE) -----
  216. NSMutableArray *metadatasNotPresents = [[NSMutableArray alloc] init];
  217. NSArray *tableMetadatasInDB = [CCCoreData getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@) AND ((session == NULL) OR (session == ''))", app.activeAccount, metadataNet.directoryID] context:nil];
  218. for (TableMetadata *tableMetadataDB in tableMetadatasInDB) {
  219. BOOL fileIDFound = NO;
  220. for (CCMetadata *metadata in metadatas) {
  221. if ([tableMetadataDB.fileID isEqualToString:metadata.fileID]) {
  222. fileIDFound = YES;
  223. break;
  224. }
  225. }
  226. if (!fileIDFound)
  227. [metadatasNotPresents addObject:[CCCoreData insertEntityInMetadata:tableMetadataDB]];
  228. }
  229. dispatch_async(dispatch_get_main_queue(), ^{
  230. // delete metadata not present
  231. for (CCMetadata *metadata in metadatasNotPresents) {
  232. [CCCoreData deleteFile:metadata serverUrl:metadataNet.serverUrl directoryUser:app.directoryUser activeAccount:app.activeAccount];
  233. }
  234. if ([metadatasNotPresents count] > 0)
  235. [app.activeMain reloadDatasource:metadataNet.serverUrl fileID:nil selector:nil];
  236. });
  237. // ----- Test : (MODIFY) -----
  238. for (CCMetadata *metadata in metadatas) {
  239. // reject cryptated
  240. if (metadata.cryptated)
  241. continue;
  242. // dir recursive
  243. if (metadata.directory) {
  244. dispatch_async(dispatch_get_main_queue(), ^{
  245. NSString *serverUrl = [CCUtility stringAppendServerUrl:metadataNet.serverUrl addServerUrl:metadata.fileNameData];
  246. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:serverUrl activeAccount:app.activeAccount];
  247. [CCCoreData addMetadata:metadata activeAccount:app.activeAccount activeUrl:app.activeUrl context:nil];
  248. [self readFolderServerUrl:serverUrl directoryID:directoryID];
  249. });
  250. } else {
  251. // It's in session
  252. BOOL recordInSession = NO;
  253. for (TableMetadata *record in recordsInSessions) {
  254. if ([record.fileID isEqualToString:metadata.fileID]) {
  255. recordInSession = YES;
  256. break;
  257. }
  258. }
  259. if (recordInSession)
  260. continue;
  261. // Ohhhh INSERT
  262. [metadatasForOfflineFolder addObject:metadata];
  263. }
  264. }
  265. if ([metadatasForOfflineFolder count] > 0)
  266. [self verifyChangeMedatas:metadatasForOfflineFolder serverUrl:metadataNet.serverUrl account:metadataNet.account offline:YES];
  267. });
  268. }
  269. #pragma --------------------------------------------------------------------------------------------
  270. #pragma mark ===== Read File =====
  271. #pragma --------------------------------------------------------------------------------------------
  272. - (void)readFile:(CCMetadata *)metadata
  273. {
  274. NSString *serverUrl = [CCCoreData getServerUrlFromDirectoryID:metadata.directoryID activeAccount:app.activeAccount];
  275. if (serverUrl == nil) return;
  276. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  277. metadataNet.action = actionReadFile;
  278. metadataNet.fileID = metadata.fileID;
  279. metadataNet.fileName = metadata.fileName;
  280. metadataNet.fileNamePrint = metadata.fileNamePrint;
  281. metadataNet.serverUrl = serverUrl;
  282. metadataNet.selector = selectorReadFileOffline;
  283. metadataNet.priority = NSOperationQueuePriorityVeryLow;
  284. [app addNetworkingOperationQueue:app.netQueue delegate:self metadataNet:metadataNet];
  285. }
  286. - (void)readFileFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  287. {
  288. // verify active user
  289. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  290. // File not present, remove it
  291. if (errorCode == 404 && [recordAccount.account isEqualToString:metadataNet.account]) {
  292. [CCCoreData deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", metadataNet.account, metadataNet.fileID]];
  293. [CCCoreData deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", metadataNet.account, metadataNet.fileID]];
  294. }
  295. }
  296. - (void)readFileSuccess:(CCMetadataNet *)metadataNet metadata:(CCMetadata *)metadata
  297. {
  298. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
  299. [self verifyChangeMedatas:[[NSArray alloc] initWithObjects:metadata, nil] serverUrl:metadataNet.serverUrl account:app.activeAccount offline:NO];
  300. });
  301. }
  302. #pragma --------------------------------------------------------------------------------------------
  303. #pragma mark ===== Verify Metadatas =====
  304. #pragma --------------------------------------------------------------------------------------------
  305. // MULTI THREAD
  306. - (void)verifyChangeMedatas:(NSArray *)allRecordMetadatas serverUrl:(NSString *)serverUrl account:(NSString *)account offline:(BOOL)offline
  307. {
  308. NSMutableArray *metadatas = [[NSMutableArray alloc] init];
  309. for (CCMetadata *metadata in allRecordMetadatas) {
  310. BOOL changeRev = NO;
  311. // change account
  312. if ([metadata.account isEqualToString:account] == NO)
  313. return;
  314. // no dir
  315. if (metadata.directory)
  316. continue;
  317. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", app.activeAccount, metadata.fileID]];
  318. if (offline) {
  319. if (![record.rev isEqualToString:metadata.rev])
  320. changeRev = YES;
  321. } else {
  322. if (record && ![record.rev isEqualToString:metadata.rev])
  323. changeRev = YES;
  324. }
  325. if (changeRev) {
  326. if ([metadata.type isEqualToString: k_metadataType_file]) {
  327. // remove file and ico
  328. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID] error:nil];
  329. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID] error:nil];
  330. }
  331. if ([metadata.type isEqualToString: k_metadataType_template]) {
  332. // remove model
  333. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileName] error:nil];
  334. }
  335. [metadatas addObject:metadata];
  336. }
  337. }
  338. dispatch_async(dispatch_get_main_queue(), ^{
  339. if ([metadatas count])
  340. [self SynchronizeMetadatas:metadatas serverUrl:serverUrl offline:offline];
  341. });
  342. }
  343. // MAIN THREAD
  344. - (void)SynchronizeMetadatas:(NSArray *)metadatas serverUrl:(NSString *)serverUrl offline:(BOOL)offline
  345. {
  346. // HUD
  347. if ([metadatas count] > 50 && offline) {
  348. if (!_hud) _hud = [[CCHud alloc] initWithView:[[[UIApplication sharedApplication] delegate] window]];
  349. [_hud visibleIndeterminateHud];
  350. }
  351. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  352. for (CCMetadata *metadata in metadatas) {
  353. NSString *selector, *selectorPost;
  354. BOOL downloadData, downloadPlist;
  355. // it's a offline ?
  356. BOOL isOffline = [CCCoreData isOfflineLocalFileID:metadata.fileID activeAccount:app.activeAccount];
  357. if (isOffline)
  358. selectorPost = selectorAddOffline;
  359. if ([metadata.type isEqualToString: k_metadataType_file]) {
  360. downloadData = YES;
  361. selector = selectorDownloadOffline;
  362. }
  363. if ([metadata.type isEqualToString: k_metadataType_template]) {
  364. downloadPlist = YES;
  365. selector = selectorLoadPlist;
  366. }
  367. [CCCoreData addMetadata:metadata activeAccount:app.activeAccount activeUrl:serverUrl context:nil];
  368. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  369. metadataNet.action = actionDownloadFile;
  370. metadataNet.metadata = metadata;
  371. metadataNet.downloadData = downloadData;
  372. metadataNet.downloadPlist = downloadPlist;
  373. metadataNet.selector = selector;
  374. metadataNet.selectorPost = selectorPost;
  375. metadataNet.serverUrl = serverUrl;
  376. metadataNet.session = k_download_session;
  377. metadataNet.taskStatus = k_taskStatusResume;
  378. [app addNetworkingOperationQueue:app.netQueueDownload delegate:app.activeMain metadataNet:metadataNet];
  379. }
  380. [[CCSynchronize sharedSynchronize] offlineFolderAnimationDirectory:[[NSArray alloc] initWithObjects:serverUrl, nil] setGraphicsFolder:YES];
  381. [app.activeMain reloadDatasource:serverUrl fileID:nil selector:nil];
  382. [_hud hideHud];
  383. });
  384. }
  385. @end