CCCoreData.m 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  1. //
  2. // CCCoreData.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 02/02/16.
  6. // Copyright (c) 2017 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 "CCCoreData.h"
  24. #import "CCNetworking.h"
  25. #import "NCBridgeSwift.h"
  26. @implementation CCCoreData
  27. #pragma --------------------------------------------------------------------------------------------
  28. #pragma mark ===== Account =====
  29. #pragma --------------------------------------------------------------------------------------------
  30. + (NSArray *)getAllAccount
  31. {
  32. NSMutableArray *accounts = [NSMutableArray new];
  33. NSArray *records;
  34. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  35. records = [TableAccount MR_findAllInContext:context];
  36. for (TableAccount *tableAccount in records)
  37. [accounts addObject:tableAccount];
  38. return accounts;
  39. }
  40. #pragma --------------------------------------------------------------------------------------------
  41. #pragma mark ===== Metadata =====
  42. #pragma --------------------------------------------------------------------------------------------
  43. /*
  44. + (void)addMetadata:(tableMetadata *)metadata activeAccount:(NSString *)activeAccount activeUrl:(NSString *)activeUrl context:(NSManagedObjectContext *)context
  45. {
  46. if (context == nil)
  47. context = [NSManagedObjectContext MR_context];
  48. // remove all fileID (BUG 2.10)
  49. [TableMetadata MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", activeAccount, metadata.fileID] inContext:context];
  50. [context MR_saveToPersistentStoreAndWait];
  51. // remove record if exists
  52. [TableMetadata MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileName == %@) AND (directoryID == %@)", activeAccount, metadata.fileName, metadata.directoryID] inContext:context];
  53. [context MR_saveToPersistentStoreAndWait];
  54. // create new record Metadata
  55. TableMetadata *record = [TableMetadata MR_createEntityInContext:context];
  56. // set default value
  57. metadata.sessionTaskIdentifier = k_taskIdentifierDone;
  58. metadata.sessionTaskIdentifierPlist = k_taskIdentifierDone;
  59. // Insert metdata -> entity
  60. [self insertMetadataInEntity:metadata recordMetadata:record activeAccount:activeAccount activeUrl:activeUrl];
  61. // Aggiorniamo la data nella directory (ottimizzazione v 2.10)
  62. [self setDateReadDirectoryID:metadata.directoryID activeAccount:activeAccount];
  63. [context MR_saveToPersistentStoreAndWait];
  64. }
  65. + (void)deleteMetadataWithPredicate:(NSPredicate *)predicate
  66. {
  67. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  68. NSString *directoryID;
  69. NSArray *records = [TableMetadata MR_findAllWithPredicate:predicate inContext:localContext];
  70. for(TableMetadata *record in records) {
  71. // Aggiorniamo la data nella directory (ottimizzazione v 2.10)
  72. if ([directoryID isEqualToString:record.directoryID] == NO)
  73. [self setDateReadDirectoryID:record.directoryID activeAccount:record.account];
  74. directoryID = record.directoryID;
  75. [record MR_deleteEntityInContext:localContext];
  76. }
  77. }];
  78. }
  79. + (void)moveMetadata:(NSString *)fileName directoryID:(NSString *)directoryID directoryIDTo:(NSString *)directoryIDTo activeAccount:(NSString *)activeAccount
  80. {
  81. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  82. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileName == %@) AND (directoryID == %@)", activeAccount, fileName, directoryID];
  83. TableMetadata *record = [TableMetadata MR_findFirstWithPredicate:predicate inContext:localContext];
  84. if (record) {
  85. record.directoryID = directoryIDTo;
  86. // Aggiorniamo la data nella directory (ottimizzazione v 2.10)
  87. [self setDateReadDirectoryID:directoryID activeAccount:activeAccount];
  88. [self setDateReadDirectoryID:directoryIDTo activeAccount:activeAccount];
  89. }
  90. }];
  91. }
  92. + (void)updateMetadata:(tableMetadata *)metadata predicate:(NSPredicate *)predicate activeAccount:(NSString *)activeAccount activeUrl:(NSString *)activeUrl context:(NSManagedObjectContext *)context
  93. {
  94. TableMetadata *record;
  95. if (context == nil)
  96. context = [NSManagedObjectContext MR_defaultContext];
  97. record = [TableMetadata MR_findFirstWithPredicate:predicate inContext:context];
  98. if (record) {
  99. [self insertMetadataInEntity:metadata recordMetadata:record activeAccount:activeAccount activeUrl:activeUrl];
  100. // Aggiorniamo la data nella directory (ottimizzazione v 2.10)
  101. [self setDateReadDirectoryID:metadata.directoryID activeAccount:activeAccount];
  102. [context MR_saveToPersistentStoreAndWait];
  103. }
  104. }
  105. + (void)setMetadataSession:(NSString *)session sessionError:(NSString *)sessionError sessionSelector:(NSString *)sessionSelector sessionSelectorPost:(NSString *)sessionSelectorPost sessionTaskIdentifier:(NSInteger)sessionTaskIdentifier sessionTaskIdentifierPlist:(NSInteger)sessionTaskIdentifierPlist predicate:(NSPredicate *)predicate context:(NSManagedObjectContext *)context
  106. {
  107. if (context == nil)
  108. context = [NSManagedObjectContext MR_defaultContext];
  109. NSArray *records = [TableMetadata MR_findAllWithPredicate:predicate inContext:context];
  110. NSMutableSet *directoryIDs = [[NSMutableSet alloc] init];
  111. NSString *directoryID;
  112. for(TableMetadata *record in records) {
  113. if (session) record.session = session;
  114. if (sessionError) record.sessionError = sessionError;
  115. if (sessionSelector) record.sessionSelector = sessionSelector;
  116. if (sessionSelectorPost) record.sessionSelectorPost = sessionSelectorPost;
  117. if (sessionTaskIdentifier != k_taskIdentifierNULL) record.sessionTaskIdentifier = [NSNumber numberWithInteger:sessionTaskIdentifier];
  118. if (sessionTaskIdentifierPlist != k_taskIdentifierNULL) record.sessionTaskIdentifierPlist = [NSNumber numberWithInteger:sessionTaskIdentifierPlist];
  119. [directoryIDs addObject:record.directoryID];
  120. // Aggiorniamo la data nella directory (ottimizzazione v 2.10)
  121. if ([directoryID isEqualToString:record.directoryID] == NO)
  122. [self setDateReadDirectoryID:record.directoryID activeAccount:record.account];
  123. directoryID = record.directoryID;
  124. }
  125. [context MR_saveToPersistentStoreAndWait];
  126. }
  127. + (void)setMetadataFavoriteFileID:(NSString *)fileID favorite:(BOOL)favorite activeAccount:(NSString *)activeAccount context:(NSManagedObjectContext *)context
  128. {
  129. if (context == nil)
  130. context = [NSManagedObjectContext MR_defaultContext];
  131. TableMetadata *tableMetadata = [TableMetadata MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", activeAccount, fileID] inContext:context];
  132. if (tableMetadata) {
  133. tableMetadata.favorite = [NSNumber numberWithBool:favorite];
  134. // Aggiorniamo la data nella directory (ottimizzazione v 2.10)
  135. [self setDateReadDirectoryID:tableMetadata.directoryID activeAccount:activeAccount];
  136. [context MR_saveToPersistentStoreAndWait];
  137. }
  138. }
  139. + (tableMetadata *)getMetadataWithPreficate:(NSPredicate *)predicate context:(NSManagedObjectContext *)context
  140. {
  141. if (context == nil)
  142. context = [NSManagedObjectContext MR_defaultContext];
  143. TableMetadata *record;
  144. record = [TableMetadata MR_findFirstWithPredicate:predicate inContext:context];
  145. if (record) {
  146. return [self insertEntityInMetadata:record];
  147. } else return nil;
  148. }
  149. + (TableMetadata *)getTableMetadataWithPreficate:(NSPredicate *)predicate
  150. {
  151. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  152. return [TableMetadata MR_findFirstWithPredicate:predicate inContext:context];
  153. }
  154. + (NSArray *)getTableMetadataWithPredicate:(NSPredicate *)predicate context:(NSManagedObjectContext *)context
  155. {
  156. if (context == nil)
  157. context = [NSManagedObjectContext MR_defaultContext];
  158. return [TableMetadata MR_findAllWithPredicate:predicate inContext:context];
  159. }
  160. + (NSArray *)getTableMetadataWithPredicate:(NSPredicate *)predicate fieldOrder:(NSString *)fieldOrder ascending:(BOOL)ascending
  161. {
  162. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  163. NSArray *records = [[NSArray alloc] init];
  164. NSSortDescriptor *descriptor;
  165. records = [TableMetadata MR_findAllWithPredicate:predicate inContext:context];
  166. if ([records count] == 0) return nil;
  167. if ([fieldOrder isEqualToString:@"fileName"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"fileNamePrint" ascending:ascending selector:@selector(localizedCaseInsensitiveCompare:)];
  168. else if ([fieldOrder isEqualToString:@"fileDate"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:ascending selector:nil];
  169. else if ([fieldOrder isEqualToString:@"sessionTaskIdentifier"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"sessionTaskIdentifier" ascending:ascending selector:nil];
  170. else descriptor = [[NSSortDescriptor alloc] initWithKey:fieldOrder ascending:ascending selector:@selector(localizedCaseInsensitiveCompare:)];
  171. return [records sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
  172. }
  173. + (tableMetadata *)getMetadataAtIndex:(NSPredicate *)predicate fieldOrder:(NSString *)fieldOrder ascending:(BOOL)ascending objectAtIndex:(NSUInteger)index
  174. {
  175. NSArray *records = [self getTableMetadataWithPredicate:predicate fieldOrder:fieldOrder ascending:ascending];
  176. TableMetadata *record = [records objectAtIndex:index];
  177. return [self insertEntityInMetadata:record];
  178. }
  179. + (tableMetadata *)getMetadataFromFileName:(NSString *)fileName directoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount context:(NSManagedObjectContext *)context
  180. {
  181. if (fileName == nil || directoryID == nil || activeAccount == nil)
  182. return nil;
  183. if (context == nil)
  184. context = [NSManagedObjectContext MR_defaultContext];
  185. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@) AND ((fileName == %@) OR (fileNameData == %@))", activeAccount, directoryID, fileName, fileName];
  186. TableMetadata *record = [TableMetadata MR_findFirstWithPredicate:predicate inContext:context];
  187. if (record) return [self insertEntityInMetadata:record];
  188. else return nil;
  189. }
  190. + (NSArray *)getTableMetadataDownloadAccount:(NSString *)activeAccount
  191. {
  192. return [self getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND ((session == %@) || (session == %@)) AND ((sessionTaskIdentifier != %i) OR (sessionTaskIdentifierPlist != %i))", activeAccount, k_download_session, k_download_session_foreground, k_taskIdentifierDone, k_taskIdentifierDone] context:nil];
  193. }
  194. + (NSArray *)getTableMetadataDownloadWWanAccount:(NSString *)activeAccount
  195. {
  196. return [self getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (session == %@) AND ((sessionTaskIdentifier != %i) OR (sessionTaskIdentifierPlist != %i))", activeAccount, k_download_session_wwan, k_taskIdentifierDone, k_taskIdentifierDone] context:nil];
  197. }
  198. + (NSArray *)getTableMetadataUploadAccount:(NSString *)activeAccount
  199. {
  200. return [self getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND ((session == %@) || (session == %@)) AND ((sessionTaskIdentifier != %i) OR (sessionTaskIdentifierPlist != %i))", activeAccount, k_upload_session, k_upload_session_foreground, k_taskIdentifierDone, k_taskIdentifierDone] context:nil];
  201. }
  202. + (NSArray *)getTableMetadataUploadWWanAccount:(NSString *)activeAccount
  203. {
  204. return [self getTableMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (session == %@) AND ((sessionTaskIdentifier != %i) OR (sessionTaskIdentifierPlist != %i))", activeAccount, k_upload_session_wwan, k_taskIdentifierDone, k_taskIdentifierDone] context:nil];
  205. }
  206. + (NSArray *)getRecordsTableMetadataPhotosCameraUpload:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  207. {
  208. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  209. NSMutableArray *recordsPhotosCameraUpload = [[NSMutableArray alloc] init];
  210. NSArray *tableDirectoryes = [self getDirectoryIDsFromBeginsWithServerUrl:serverUrl activeAccount:activeAccount];
  211. for (TableDirectory *record in tableDirectoryes) {
  212. NSArray *records = [TableMetadata MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@) AND ((session == NULL) OR (session == '')) AND (type == 'file') AND ((typeFile == %@) OR (typeFile == %@))", activeAccount, record.directoryID, k_metadataTypeFile_image, k_metadataTypeFile_video] inContext:context];
  213. if ([records count] > 0)
  214. [recordsPhotosCameraUpload addObjectsFromArray:records];
  215. }
  216. // test
  217. if ([recordsPhotosCameraUpload count] == 0) return nil;
  218. // Order
  219. NSString *fieldOrder = [CCUtility getOrderSettings];
  220. BOOL ascending = [CCUtility getAscendingSettings];
  221. NSSortDescriptor *descriptor;
  222. if ([fieldOrder isEqualToString:@"fileName"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"fileNamePrint" ascending:ascending selector:@selector(localizedCaseInsensitiveCompare:)];
  223. else if ([fieldOrder isEqualToString:@"fileDate"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:ascending selector:nil];
  224. else if ([fieldOrder isEqualToString:@"sessionTaskIdentifier"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"sessionTaskIdentifier" ascending:ascending selector:nil];
  225. else descriptor = [[NSSortDescriptor alloc] initWithKey:fieldOrder ascending:ascending selector:@selector(localizedCaseInsensitiveCompare:)];
  226. return [recordsPhotosCameraUpload sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
  227. }
  228. + (void)removeOfflineAllFileFromServerUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  229. {
  230. NSString *directoryID = [self getDirectoryIDFromServerUrl:serverUrl activeAccount:activeAccount];
  231. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  232. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", activeAccount, directoryID];
  233. NSArray *records = [TableMetadata MR_findAllWithPredicate:predicate];
  234. for (TableMetadata *record in records)
  235. [self setOfflineLocalFileID:record.fileID offline:NO activeAccount:activeAccount];
  236. }];
  237. }
  238. */
  239. #pragma --------------------------------------------------------------------------------------------
  240. #pragma mark ===== Directory =====
  241. #pragma --------------------------------------------------------------------------------------------
  242. /*
  243. + (NSString *)addDirectory:(NSString *)serverUrl permissions:(NSString *)permissions activeAccount:(NSString *)activeAccount
  244. {
  245. NSString *directoryID;
  246. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  247. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount] inContext:context];
  248. if (record) {
  249. directoryID = record.directoryID;
  250. if (permissions) record.permissions = permissions;
  251. } else {
  252. TableDirectory *record = [TableDirectory MR_createEntityInContext:context];
  253. record.account = activeAccount;
  254. record.directoryID = [CCUtility createRandomString:16];
  255. directoryID = record.directoryID;
  256. if (permissions) record.permissions = permissions;
  257. record.serverUrl = serverUrl;
  258. }
  259. [context MR_saveToPersistentStoreAndWait];
  260. return directoryID;
  261. }
  262. + (void)updateDirectoryEtagServerUrl:(NSString *)serverUrl fileID:(NSString *)fileID activeAccount:(NSString *)activeAccount
  263. {
  264. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  265. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  266. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:localContext];
  267. if (record)
  268. record.rev = fileID;
  269. }];
  270. }
  271. + (void)deleteDirectoryFromPredicate:(NSPredicate *)predicate
  272. {
  273. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  274. [TableDirectory MR_deleteAllMatchingPredicate:predicate inContext:localContext];
  275. }];
  276. }
  277. + (NSArray *)deleteDirectoryAndSubDirectory:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  278. {
  279. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  280. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  281. NSMutableArray *directoryIDs = [[NSMutableArray alloc] init];
  282. NSArray *tableDirectorys = [TableDirectory MR_findAllWithPredicate:predicate inContext:context];
  283. for(TableDirectory *recordDirectory in tableDirectorys) {
  284. NSLog(@"[LOG] %@", recordDirectory.serverUrl);
  285. if ([recordDirectory.serverUrl hasPrefix:serverUrl]) {
  286. // List directoryIDs removed
  287. [directoryIDs addObject:recordDirectory.directoryID];
  288. // remove all TableMetadata
  289. NSLog(@"[LOG] %@", recordDirectory.directoryID);
  290. // remove directory in Metadata come cazzo si fa a saperlo
  291. //[TableMetadata MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", activeAccount, recordDirectory.directoryID] inContext:context];
  292. NSArray *tableMetadatas = [TableMetadata MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", activeAccount, recordDirectory.directoryID] inContext:context];
  293. for(TableMetadata *recordMetadata in tableMetadatas) {
  294. // remove if in session
  295. if ([recordMetadata.session length] >0) {
  296. if (recordMetadata.sessionTaskIdentifier >= 0)
  297. [[CCNetworking sharedNetworking] settingSession:recordMetadata.session sessionTaskIdentifier:[recordMetadata.sessionTaskIdentifier integerValue] taskStatus: k_taskStatusCancel];
  298. if (recordMetadata.sessionTaskIdentifierPlist >= 0)
  299. [[CCNetworking sharedNetworking] settingSession:recordMetadata.session sessionTaskIdentifier:[recordMetadata.sessionTaskIdentifierPlist integerValue] taskStatus: k_taskStatusCancel];
  300. }
  301. // remove file local
  302. NSLog(@"[LOG] %@", recordMetadata.fileID);
  303. [self deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", activeAccount, recordMetadata.fileID]];
  304. [recordMetadata MR_deleteEntityInContext:context];
  305. }
  306. [recordDirectory MR_deleteEntityInContext:context];
  307. }
  308. }
  309. [context MR_saveToPersistentStoreAndWait];
  310. return directoryIDs;
  311. }
  312. + (void)renameDirectory:(NSString *)serverUrl serverUrlTo:(NSString *)serverUrlTo activeAccount:(NSString *)activeAccount
  313. {
  314. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  315. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  316. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:localContext];
  317. if (record)
  318. record.serverUrl = serverUrlTo;
  319. }];
  320. }
  321. + (void)setDateReadDirectoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount
  322. {
  323. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  324. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", directoryID, activeAccount];
  325. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  326. if (record) {
  327. record.dateReadDirectory = [NSDate date];
  328. [context MR_saveToPersistentStoreAndWait];
  329. }
  330. }
  331. + (TableDirectory *)getTableDirectoryWithPreficate:(NSPredicate *)predicate
  332. {
  333. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  334. return [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  335. }
  336. + (NSDate *)getDateReadDirectoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount
  337. {
  338. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  339. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", directoryID, activeAccount];
  340. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  341. if (record) return record.dateReadDirectory;
  342. else return nil;
  343. }
  344. + (void)setDirectoryRev:(NSString *)rev serverUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  345. {
  346. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  347. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  348. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  349. if (record) {
  350. record.rev = rev;
  351. [context MR_saveToPersistentStoreAndWait];
  352. }
  353. }
  354. + (NSString *)getDirectoryRevFromServerUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  355. {
  356. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  357. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  358. if (record) return record.rev;
  359. else return nil;
  360. }
  361. + (NSArray *)getDirectoryIDsFromBeginsWithServerUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  362. {
  363. NSString *serverUrlBeginWith = serverUrl;
  364. if (![serverUrl hasSuffix:@"/"])
  365. serverUrlBeginWith = [serverUrl stringByAppendingString:@"/"];
  366. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"((serverUrl == %@) OR (serverUrl BEGINSWITH %@)) AND (account == %@)", serverUrl, serverUrlBeginWith, activeAccount];
  367. return [TableDirectory MR_findAllWithPredicate:predicate];
  368. }
  369. + (NSString *)getDirectoryIDFromServerUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  370. {
  371. if (serverUrl == nil) return nil;
  372. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  373. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  374. if (record) return record.directoryID;
  375. else {
  376. return [self addDirectory:serverUrl permissions:nil activeAccount:activeAccount];
  377. }
  378. return nil;
  379. }
  380. + (NSString *)getServerUrlFromDirectoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount
  381. {
  382. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", directoryID, activeAccount];
  383. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  384. if (record) return record.serverUrl;
  385. else return nil;
  386. }
  387. + (void)clearDateReadAccount:(NSString *)activeAccount serverUrl:(NSString *)serverUrl directoryID:(NSString *)directoryID
  388. {
  389. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  390. NSPredicate *predicate;
  391. if ([serverUrl length] > 0)
  392. predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  393. if ([directoryID length] > 0)
  394. predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", directoryID, activeAccount];
  395. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:localContext];
  396. if (record) {
  397. record.dateReadDirectory = NULL;
  398. record.rev = @"";
  399. }
  400. }];
  401. }
  402. + (void)clearAllDateReadDirectory
  403. {
  404. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  405. NSArray *records = [TableDirectory MR_findAllInContext:localContext];
  406. for (TableDirectory *record in records) {
  407. record.dateReadDirectory = NULL;
  408. record.rev = @"";
  409. }
  410. }];
  411. }
  412. + (BOOL)isDirectoryOutOfDate:(int)numAddDay directoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount
  413. {
  414. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", activeAccount, directoryID];
  415. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  416. if (record == nil || record.dateReadDirectory == nil) {
  417. return YES;
  418. }
  419. NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
  420. [dateComponents setWeekday:numAddDay];
  421. NSCalendar *calendar = [NSCalendar currentCalendar];
  422. NSDate *datePlus = [calendar dateByAddingComponents:dateComponents toDate:record.dateReadDirectory options:0];
  423. NSDate *now = [NSDate date];
  424. // usa la Cache se richiesto e se la data è entro X giorni dall'ultima volta che l'hai letta.
  425. if ([now compare:datePlus] == NSOrderedDescending) {
  426. return YES;
  427. }
  428. return NO;
  429. }
  430. #pragma --------------------------------------------------------------------------------------------
  431. #pragma mark ===== Offline Directory =====
  432. #pragma --------------------------------------------------------------------------------------------
  433. + (void)removeOfflineDirectoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount
  434. {
  435. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  436. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@) AND (offline == 1)", directoryID, activeAccount];
  437. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:localContext];
  438. if (record)
  439. record.offline = [NSNumber numberWithBool:FALSE];
  440. }];
  441. }
  442. + (NSArray *)getOfflineDirectoryActiveAccount:(NSString *)activeAccount
  443. {
  444. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (offline == 1)", activeAccount];
  445. NSArray *recordsTable = [TableDirectory MR_findAllWithPredicate:predicate];
  446. // Order by serverUrl
  447. NSArray *sortedRecordsTable = [recordsTable sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  448. TableDirectory *record1 = obj1, *record2 = obj2;
  449. return [record1.serverUrl compare:record2.serverUrl];
  450. }];
  451. return sortedRecordsTable;
  452. }
  453. + (void)setOfflineDirectoryServerUrl:(NSString *)serverUrl offline:(BOOL)offline activeAccount:(NSString *)activeAccount
  454. {
  455. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  456. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  457. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:localContext];
  458. if (record)
  459. record.offline = [NSNumber numberWithBool:offline];
  460. }];
  461. }
  462. + (BOOL)isOfflineDirectoryServerUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  463. {
  464. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (offline == 1) AND (account == %@)", serverUrl, activeAccount];
  465. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  466. if (record) return YES;
  467. else return NO;
  468. }
  469. #pragma --------------------------------------------------------------------------------------------
  470. #pragma mark ===== Directory Lock =====
  471. #pragma --------------------------------------------------------------------------------------------
  472. + (BOOL)setDirectoryLock:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  473. {
  474. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  475. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", [self getDirectoryIDFromServerUrl:serverUrl activeAccount:activeAccount], activeAccount];
  476. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  477. if (record) {
  478. record.lock = [NSNumber numberWithBool:YES];
  479. [context MR_saveToPersistentStoreAndWait];
  480. return YES;
  481. }
  482. else return NO;
  483. }
  484. + (BOOL)setDirectoryUnLock:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  485. {
  486. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  487. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", [self getDirectoryIDFromServerUrl:serverUrl activeAccount:activeAccount], activeAccount];
  488. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  489. if (record) {
  490. record.lock = [NSNumber numberWithBool:NO];
  491. [context MR_saveToPersistentStoreAndWait];
  492. return YES;
  493. }
  494. else return NO;
  495. }
  496. + (void)setAllDirectoryUnLockForAccount:(NSString *)activeAccount
  497. {
  498. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  499. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  500. NSArray *records = [TableDirectory MR_findAllWithPredicate:predicate inContext:localContext];
  501. for(TableDirectory *record in records)
  502. record.lock = [NSNumber numberWithBool:NO];
  503. }];
  504. }
  505. + (BOOL)isDirectoryLock:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  506. {
  507. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (lock == 1) AND (account == %@)", [self getDirectoryIDFromServerUrl:serverUrl activeAccount:activeAccount], activeAccount];
  508. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  509. if (record) return YES;
  510. else return NO;
  511. }
  512. + (BOOL)isBlockZone:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  513. {
  514. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (directory == 1)", activeAccount];
  515. NSArray *records = [TableMetadata MR_findAllWithPredicate:predicate];
  516. if ([records count] > 0) {
  517. NSArray *pathComponents = [serverUrl pathComponents];
  518. for (NSString *fileName in pathComponents) {
  519. for(TableMetadata *record in records){
  520. NSString *fileNameEntity = [CCUtility trasformedFileNamePlistInCrypto:record.fileName];
  521. NSString *directoryID = record.directoryID;
  522. NSString *serverUrlEntity = [self getServerUrlFromDirectoryID:directoryID activeAccount:activeAccount];
  523. if([fileName isEqualToString:fileNameEntity]) {
  524. NSString *lockServerUrl = [CCUtility stringAppendServerUrl:serverUrlEntity addFileName:fileNameEntity];
  525. BOOL risultato = [self isDirectoryLock:lockServerUrl activeAccount:activeAccount];
  526. if (risultato) return YES;
  527. }
  528. }
  529. }
  530. }
  531. return NO;
  532. }
  533. */
  534. #pragma --------------------------------------------------------------------------------------------
  535. #pragma mark ===== LocalFile =====
  536. #pragma --------------------------------------------------------------------------------------------
  537. + (void)addLocalFile:(tableMetadata *)metadata activeAccount:(NSString *)activeAccount
  538. {
  539. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  540. BOOL offline = NO;
  541. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", activeAccount, metadata.fileID];
  542. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  543. if (record) {
  544. offline = [[record valueForKey:@"offline"] boolValue];
  545. [record MR_deleteEntityInContext:localContext];
  546. }
  547. record = [TableLocalFile MR_createEntityInContext:localContext];
  548. record.account = activeAccount;
  549. record.date = metadata.date;
  550. record.fileID = metadata.fileID;
  551. record.exifDate = [NSDate date];
  552. record.exifLatitude = @"-1";
  553. record.exifLongitude = @"-1";
  554. record.offline = [NSNumber numberWithBool:offline];
  555. record.fileName = metadata.fileName;
  556. record.fileNamePrint = metadata.fileNamePrint;
  557. record.rev = metadata.rev;
  558. record.size = [NSNumber numberWithLong:metadata.size];
  559. }];
  560. }
  561. + (void)deleteLocalFileWithPredicate:(NSPredicate *)predicate
  562. {
  563. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  564. [TableLocalFile MR_deleteAllMatchingPredicate:predicate inContext:localContext];
  565. }];
  566. }
  567. + (void)renameLocalFileWithEtag:(NSString *)fileID fileNameTo:(NSString *)fileNameTo fileNamePrintTo:(NSString *)fileNamePrintTo activeAccount:(NSString *)activeAccount
  568. {
  569. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  570. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, activeAccount];
  571. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  572. if (record) {
  573. if (fileNameTo)record.fileName = fileNameTo;
  574. if (fileNamePrintTo)record.fileNamePrint = fileNamePrintTo;
  575. }
  576. }];
  577. }
  578. + (void)updateLocalFileModel:(tableMetadata *)metadata activeAccount:(NSString *)activeAccount
  579. {
  580. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  581. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileName == %@)", activeAccount, metadata.fileName];
  582. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  583. if (record) {
  584. record.fileID = metadata.fileID;
  585. record.date = metadata.date;
  586. record.fileNamePrint = metadata.fileNamePrint;
  587. } else {
  588. [self addLocalFile:metadata activeAccount:activeAccount];
  589. }
  590. }];
  591. }
  592. + (TableLocalFile *)getLocalFileWithEtag:(NSString *)fileID activeAccount:(NSString *)activeAccount
  593. {
  594. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, activeAccount];
  595. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate];
  596. if (record) {
  597. return record;
  598. } else return nil;
  599. }
  600. + (NSMutableArray *)getTableLocalFileWithPredicate:(NSPredicate *)predicate controlZombie:(BOOL)controlZombie activeAccount:(NSString *)activeAccount directoryUser:(NSString *)directoryUser
  601. {
  602. NSMutableArray *ritorno = [[NSMutableArray alloc] init];
  603. NSArray *records = [TableLocalFile MR_findAllWithPredicate:predicate];
  604. if ([records count] > 0) {
  605. // verifichiamo esistano tutti i file altrimenti rimuoviamo il record
  606. for(TableLocalFile *record in records){
  607. if (controlZombie) {
  608. NSString *fileID = record.fileID;
  609. NSString *FilePathEtag = [NSString stringWithFormat:@"%@/%@", directoryUser, fileID];
  610. NSString *FilePathFileName = [NSString stringWithFormat:@"%@/%@", directoryUser, record.fileName];
  611. if (![[NSFileManager defaultManager] fileExistsAtPath:FilePathEtag] && ![[NSFileManager defaultManager] fileExistsAtPath:FilePathFileName] && controlZombie) {
  612. // non esiste nè il file fileID e nemmeno il plist, eliminiamolo.
  613. [self deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, activeAccount]];
  614. } else [ritorno addObject:record];
  615. } else [ritorno addObject:record];
  616. }
  617. } else return nil;
  618. if ([ritorno count] == 0) return nil;
  619. else return ritorno;
  620. }
  621. + (NSArray *)getTableLocalFileWithPredicate:(NSPredicate *)predicate
  622. {
  623. return [TableLocalFile MR_findAllWithPredicate:predicate];
  624. }
  625. #pragma --------------------------------------------------------------------------------------------
  626. #pragma mark ===== Offline LocalFile =====
  627. #pragma --------------------------------------------------------------------------------------------
  628. + (void)setOfflineLocalEtag:(NSString *)fileID offline:(BOOL)offline activeAccount:(NSString *)activeAccount
  629. {
  630. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  631. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, activeAccount];
  632. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  633. if (record)
  634. record.offline = [NSNumber numberWithBool:offline];
  635. }];
  636. }
  637. + (BOOL)isOfflineLocalEtag:(NSString *)fileID activeAccount:(NSString *)activeAccount
  638. {
  639. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (offline == 1) AND (account == %@)", fileID, activeAccount];
  640. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate];
  641. if (record) return YES;
  642. else return NO;
  643. }
  644. + (NSArray *)getOfflineLocalFileActiveAccount:(NSString *)activeAccount directoryUser:(NSString *)directoryUser
  645. {
  646. NSMutableArray *metadatas = [NSMutableArray new];
  647. NSArray *files = [self getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (offline == 1)", activeAccount] controlZombie:YES activeAccount:activeAccount directoryUser:directoryUser];
  648. for (TableLocalFile *file in files) {
  649. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", file.fileID, activeAccount];
  650. //tableMetadata *metadata = [self getMetadataWithPreficate:predicate context:nil];
  651. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPreficate:predicate];
  652. if (metadata) {
  653. // verify if is not on directory offline
  654. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (offline == 1) AND (account == %@)", metadata.directoryID, activeAccount];
  655. TableDirectory *directory = [TableDirectory MR_findFirstWithPredicate:predicate];
  656. if (!directory)
  657. [metadatas addObject:metadata];
  658. }
  659. }
  660. return metadatas;
  661. }
  662. #pragma --------------------------------------------------------------------------------------------
  663. #pragma mark ===== GeoInformation =====
  664. #pragma --------------------------------------------------------------------------------------------
  665. + (NSArray *)getGeoInformationLocalFromEtag:(NSString *)fileID activeAccount:(NSString *)activeAccount
  666. {
  667. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, activeAccount];
  668. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate];
  669. if (record) return [[NSArray alloc] initWithObjects:record.exifDate, record.exifLatitude, record.exifLongitude, nil];
  670. else return nil;
  671. }
  672. + (void)setGeoInformationLocalFromEtag:(NSString *)fileID exifDate:(NSDate *)exifDate exifLatitude:(NSString *)exifLatitude exifLongitude:(NSString *)exifLongitude activeAccount:(NSString *)activeAccount
  673. {
  674. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  675. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", activeAccount, fileID];
  676. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  677. if (record) {
  678. record.exifDate = exifDate;
  679. record.exifLatitude = exifLatitude;
  680. record.exifLongitude = exifLongitude;
  681. }
  682. }];
  683. }
  684. + (void)setGeoInformationLocalNull
  685. {
  686. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  687. NSArray *records = [TableLocalFile MR_findAllInContext:localContext];
  688. for (TableLocalFile *record in records) {
  689. if ([record.exifLatitude doubleValue] != 0 || [record.exifLongitude doubleValue] != 0) {
  690. record.exifLatitude = @"9999";
  691. record.exifLongitude = @"9999";
  692. }
  693. }
  694. }];
  695. }
  696. #pragma --------------------------------------------------------------------------------------------
  697. #pragma mark ===== Certificates =====
  698. #pragma --------------------------------------------------------------------------------------------
  699. + (NSMutableArray *)getAllCertificatesLocationOldDB
  700. {
  701. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  702. NSMutableArray *output = [NSMutableArray new];
  703. NSArray *records = [TableCertificates MR_findAllInContext:context];
  704. for (TableCertificates *record in records) {
  705. if (record.certificateLocation && record.certificateLocation.length > 0)
  706. [output addObject:record.certificateLocation];
  707. }
  708. return output;
  709. }
  710. #pragma --------------------------------------------------------------------------------------------
  711. #pragma mark ===== Share =====
  712. #pragma --------------------------------------------------------------------------------------------
  713. /*
  714. + (void)setShareLink:(NSString *)share fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl sharesLink:(NSMutableDictionary *)sharesLink activeAccount:(NSString *)activeAccount
  715. {
  716. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  717. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileName == %@) AND (serverUrl == %@)", activeAccount, fileName, serverUrl];
  718. TableShare *record = [TableShare MR_findFirstWithPredicate:predicate inContext:context];
  719. if (record) {
  720. record.shareLink = share;
  721. } else {
  722. TableShare *record = [TableShare MR_createEntityInContext:context];
  723. record.account = activeAccount;
  724. record.fileName = fileName;
  725. record.serverUrl = serverUrl;
  726. record.shareLink = share;
  727. }
  728. [context MR_saveToPersistentStoreAndWait];
  729. if (share && serverUrl && fileName)
  730. [sharesLink setObject:share forKey:[serverUrl stringByAppendingString:fileName]];
  731. }
  732. + (void)setShareUserAndGroup:(NSString *)share fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup activeAccount:(NSString *)activeAccount
  733. {
  734. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  735. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileName == %@) AND (serverUrl == %@)", activeAccount, fileName, serverUrl];
  736. TableShare *record = [TableShare MR_findFirstWithPredicate:predicate inContext:context];
  737. if (record) {
  738. record.shareUserAndGroup = share;
  739. } else {
  740. TableShare *record = [TableShare MR_createEntityInContext:context];
  741. record.account = activeAccount;
  742. record.fileName = fileName;
  743. record.serverUrl = serverUrl;
  744. record.shareUserAndGroup = share;
  745. }
  746. [context MR_saveToPersistentStoreAndWait];
  747. [sharesUserAndGroup setObject:share forKey:[serverUrl stringByAppendingString:fileName]];
  748. }
  749. + (void)unShare:(NSString *)share fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl sharesLink:(NSMutableDictionary *)sharesLink sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup activeAccount:(NSString *)activeAccount
  750. {
  751. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  752. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND ((shareLink CONTAINS %@) OR (shareUserAndGroup CONTAINS %@))", activeAccount, share, share];
  753. TableShare *record = [TableShare MR_findFirstWithPredicate:predicate inContext:context];
  754. if (record) {
  755. if ([record.shareLink containsString:share]) record.shareLink = @"";
  756. if ([record.shareUserAndGroup containsString:share]) {
  757. NSMutableArray *shares = [[NSMutableArray alloc] initWithArray:[record.shareUserAndGroup componentsSeparatedByString:@","]];
  758. [shares removeObject:share];
  759. record.shareUserAndGroup = [shares componentsJoinedByString:@","];
  760. }
  761. if ([record.shareLink length] == 0 && [record.shareUserAndGroup length] == 0)
  762. [record MR_deleteEntityInContext:context];
  763. [context MR_saveToPersistentStoreAndWait];
  764. if ([record.shareLink length] > 0) [sharesLink setObject:record.shareLink forKey:[serverUrl stringByAppendingString:fileName]];
  765. else [sharesLink removeObjectForKey:[serverUrl stringByAppendingString:fileName]];
  766. if ([record.shareUserAndGroup length] > 0) [sharesUserAndGroup setObject:record.shareUserAndGroup forKey:[serverUrl stringByAppendingString:fileName]];
  767. else [sharesUserAndGroup removeObjectForKey:[serverUrl stringByAppendingString:fileName]];
  768. }
  769. }
  770. + (void)removeAllShareActiveAccount:(NSString *)activeAccount sharesLink:(NSMutableDictionary *)sharesLink sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup
  771. {
  772. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  773. [TableShare MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"account == %@", activeAccount] inContext:context];
  774. [context MR_saveToPersistentStoreAndWait];
  775. [sharesLink removeAllObjects];
  776. [sharesUserAndGroup removeAllObjects];
  777. }
  778. + (void)updateShare:(NSDictionary *)items sharesLink:(NSMutableDictionary *)sharesLink sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup activeAccount:(NSString *)activeAccount activeUrl:(NSString *)activeUrl
  779. {
  780. // rimuovi tutte le condivisioni
  781. [self removeAllShareActiveAccount:activeAccount sharesLink:sharesLink sharesUserAndGroup:sharesUserAndGroup];
  782. NSMutableArray *itemsLink = [[NSMutableArray alloc] init];
  783. NSMutableArray *itemsUsersAndGroups = [[NSMutableArray alloc] init];
  784. for (NSString *idRemoteShared in items) {
  785. OCSharedDto *item = [items objectForKey:idRemoteShared];
  786. if (item.shareType == shareTypeLink) [itemsLink addObject:item];
  787. if ([[item shareWith] length] > 0 && (item.shareType == shareTypeUser || item.shareType == shareTypeGroup || item.shareType == shareTypeRemote)) [itemsUsersAndGroups addObject:item];
  788. }
  789. // Link
  790. for (OCSharedDto *item in itemsLink) {
  791. NSString *fullPath = [[CCUtility getHomeServerUrlActiveUrl:activeUrl] stringByAppendingString:item.path];
  792. NSString *fileName = [fullPath lastPathComponent];
  793. NSString *serverUrl = [fullPath substringToIndex:([fullPath length]-[fileName length]-1)];
  794. if ([serverUrl hasSuffix:@"/"]) serverUrl = [serverUrl substringToIndex:[serverUrl length] - 1];
  795. if ([@(item.idRemoteShared) stringValue])
  796. [self setShareLink:[@(item.idRemoteShared) stringValue] fileName:fileName serverUrl:serverUrl sharesLink:sharesLink activeAccount:activeAccount];
  797. }
  798. // Condivisioni
  799. NSMutableDictionary *paths = [[NSMutableDictionary alloc] init];
  800. // Creazione dizionario
  801. for (OCSharedDto *item in itemsUsersAndGroups) {
  802. if ([paths objectForKey:item.path]) {
  803. NSMutableArray *share = [paths objectForKey:item.path];
  804. [share addObject:[@(item.idRemoteShared) stringValue]];
  805. [paths setObject:share forKey:item.path];
  806. } else {
  807. NSMutableArray *share = [[NSMutableArray alloc] initWithObjects:[@(item.idRemoteShared) stringValue], nil];
  808. [paths setObject:share forKey:item.path];
  809. }
  810. }
  811. // Scrittura su DB
  812. for (NSString *path in paths) {
  813. NSArray *items = [paths objectForKey:path];
  814. NSString *share = [items componentsJoinedByString:@","];
  815. NSLog(@"[LOG] share %@", share);
  816. NSString *fullPath = [[CCUtility getHomeServerUrlActiveUrl:activeUrl] stringByAppendingString:path];
  817. NSString *fileName = [fullPath lastPathComponent];
  818. NSString *serverUrl = [fullPath substringToIndex:([fullPath length]-[fileName length]-1)];
  819. if ([serverUrl hasSuffix:@"/"]) serverUrl = [serverUrl substringToIndex:[serverUrl length] - 1];
  820. if (share)
  821. [self setShareUserAndGroup:share fileName:fileName serverUrl:serverUrl sharesUserAndGroup:sharesUserAndGroup activeAccount:activeAccount];
  822. }
  823. }
  824. + (void)populateSharesVariableFromDBActiveAccount:(NSString *)activeAccount sharesLink:(NSMutableDictionary *)sharesLink sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup
  825. {
  826. [sharesLink removeAllObjects];
  827. [sharesUserAndGroup removeAllObjects];
  828. NSArray *records = [TableShare MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", activeAccount]];
  829. for (TableShare *record in records) {
  830. if ([record.shareLink length] > 0 && record.serverUrl && record.fileName)
  831. [sharesLink setObject:record.shareLink forKey:[record.serverUrl stringByAppendingString:record.fileName]];
  832. if ([record.shareUserAndGroup length] > 0 && record.serverUrl && record.fileName)
  833. [sharesUserAndGroup setObject:record.shareUserAndGroup forKey:[record.serverUrl stringByAppendingString:record.fileName]];
  834. }
  835. return;
  836. }
  837. */
  838. #pragma --------------------------------------------------------------------------------------------
  839. #pragma mark ===== Offline =====
  840. #pragma --------------------------------------------------------------------------------------------
  841. + (NSArray *)getHomeOfflineActiveAccount:(NSString *)activeAccount directoryUser:(NSString *)directoryUser fieldOrder:(NSString *)fieldOrder ascending:(BOOL)ascending
  842. {
  843. NSMutableArray *tableMetadatas = [NSMutableArray new];
  844. NSArray *directoriesOffline = [self getOfflineDirectoryActiveAccount:activeAccount];
  845. NSString *father = @"";
  846. NSSortDescriptor *descriptor;
  847. // Add directory
  848. for (TableDirectory *directory in directoriesOffline) {
  849. if (![directory.serverUrl containsString:father]) {
  850. father = directory.serverUrl;
  851. NSString *upDir = [CCUtility deletingLastPathComponentFromServerUrl:father];
  852. NSString *directoryID = [self getDirectoryIDFromServerUrl:upDir activeAccount:activeAccount];
  853. NSString *fileName = [father lastPathComponent];
  854. if (upDir && directoryID && fileName) {
  855. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@) AND (directory == 1) AND (fileNameData == %@)", directoryID, activeAccount, fileName];
  856. //TableMetadata *tableMetadata = [self getTableMetadataWithPreficate:predicate];
  857. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPreficate:predicate];
  858. if (metadata)
  859. [tableMetadatas addObject:metadata];
  860. }
  861. }
  862. }
  863. // Add files
  864. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (offline == 1)", activeAccount];
  865. NSArray *localFiles = [CCCoreData getTableLocalFileWithPredicate:predicate];
  866. for (TableLocalFile *localFile in localFiles) {
  867. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", activeAccount, localFile.fileID];
  868. //TableMetadata *tableMetadata = [self getTableMetadataWithPreficate:predicate];
  869. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPreficate:predicate];
  870. if (metadata)
  871. [tableMetadatas addObject:metadata];
  872. }
  873. // Order
  874. if ([fieldOrder isEqualToString:@"fileName"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"fileNamePrint" ascending:ascending selector:@selector(localizedCaseInsensitiveCompare:)];
  875. else if ([fieldOrder isEqualToString:@"fileDate"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:ascending selector:nil];
  876. else if ([fieldOrder isEqualToString:@"sessionTaskIdentifier"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"sessionTaskIdentifier" ascending:ascending selector:nil];
  877. else descriptor = [[NSSortDescriptor alloc] initWithKey:fieldOrder ascending:ascending selector:@selector(localizedCaseInsensitiveCompare:)];
  878. return [tableMetadatas sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];//[NSArray arrayWithArray:tableMetadatas];
  879. }
  880. #pragma --------------------------------------------------------------------------------------------
  881. #pragma mark ===== File System =====
  882. #pragma --------------------------------------------------------------------------------------------
  883. + (BOOL)downloadFile:(tableMetadata *)metadata directoryUser:(NSString *)directoryUser activeAccount:(NSString *)activeAccount
  884. {
  885. // ----------------------------------------- FILESYSTEM ------------------------------------------
  886. // if encrypted, rewrite
  887. if (metadata.cryptated == YES)
  888. if ([[CCCrypto sharedManager] decrypt:metadata.fileID fileNameDecrypted:metadata.fileID fileNamePrint:metadata.fileNamePrint password:[[CCCrypto sharedManager] getKeyPasscode:metadata.uuid] directoryUser:directoryUser] == 0) return NO;
  889. // ------------------------------------------ COREDATA -------------------------------------------
  890. // add/update Table Local File
  891. [self addLocalFile:metadata activeAccount:activeAccount];
  892. // EXIF
  893. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image])
  894. [CCExifGeo setExifLocalTableEtag:metadata directoryUser:directoryUser activeAccount:activeAccount];
  895. // Icon
  896. [CCGraphics createNewImageFrom:metadata.fileID directoryUser:directoryUser fileNameTo:metadata.fileID fileNamePrint:metadata.fileNamePrint size:@"m" imageForUpload:NO typeFile:metadata.typeFile writePreview:YES optimizedFileName:[CCUtility getOptimizedPhoto]];
  897. return YES;
  898. }
  899. + (void)downloadFilePlist:(tableMetadata *)metadata activeAccount:(NSString *)activeAccount activeUrl:(NSString *)activeUrl directoryUser:(NSString *)directoryUser
  900. {
  901. metadata = [[NCManageDatabase sharedInstance] copyTableMetadata:metadata];
  902. [CCUtility insertInformationPlist:metadata directoryUser:directoryUser];
  903. [[NCManageDatabase sharedInstance] updateMetadata:metadata activeUrl:activeUrl];
  904. // se è un template aggiorniamo anche nel FileSystem
  905. if ([metadata.type isEqualToString: k_metadataType_template]){
  906. [self updateLocalFileModel:metadata activeAccount:activeAccount];
  907. }
  908. }
  909. + (void)deleteFile:(tableMetadata *)metadata serverUrl:(NSString *)serverUrl directoryUser:(NSString *)directoryUser activeAccount:(NSString *)activeAccount
  910. {
  911. if (!metadata) return;
  912. // ----------------------------------------- FILESYSTEM ------------------------------------------
  913. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", directoryUser, metadata.fileID] error:nil];
  914. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.ico", directoryUser, metadata.fileID] error:nil];
  915. // ------------------------------------------ DATABASE -------------------------------------------
  916. // se è una directory cancelliamo tutto quello che è della directory
  917. if (metadata.directory && serverUrl) {
  918. NSString *dirForDelete = [CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileNameData];
  919. [self deleteDirectoryAndSubDirectory:dirForDelete activeAccount:activeAccount];
  920. }
  921. [self deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", metadata.fileID, activeAccount]];
  922. [[NCManageDatabase sharedInstance] deleteMetadata:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", metadata.fileID, activeAccount]];
  923. }
  924. #pragma --------------------------------------------------------------------------------------------
  925. #pragma mark ===== Utility Database =====
  926. #pragma --------------------------------------------------------------------------------------------
  927. + (void)moveCoreDataToGroup
  928. {
  929. NSString *applicationName = [[[NSBundle mainBundle] infoDictionary] valueForKey:(NSString *)kCFBundleNameKey];
  930. NSURL *dirGroup = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:[NCBrandOptions sharedInstance].capabilitiesGroups];
  931. NSString *dirToPath = [[dirGroup URLByAppendingPathComponent:appDatabase] path];
  932. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
  933. NSString *dirFromPath = [[paths lastObject] stringByAppendingPathComponent:applicationName];
  934. NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirFromPath error:nil];
  935. NSError *error;
  936. for(NSString *filename in files)
  937. [[NSFileManager defaultManager] moveItemAtPath:[dirFromPath stringByAppendingPathComponent:filename] toPath:[dirToPath stringByAppendingPathComponent:filename] error:&error];
  938. }
  939. + (void)moveAllUserToGroup
  940. {
  941. NSArray *records = [TableAccount MR_findAll];
  942. for (TableAccount *record in records) {
  943. NSString *dirFromPath = [CCUtility getOLDDirectoryActiveUser:record.user activeUrl:record.url];
  944. NSString *dirToPath = [CCUtility getDirectoryActiveUser:record.user activeUrl:record.url];
  945. NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirFromPath error:nil];
  946. NSError *error;
  947. for(NSString *filename in files)
  948. [[NSFileManager defaultManager] moveItemAtPath:[dirFromPath stringByAppendingPathComponent:filename] toPath:[dirToPath stringByAppendingPathComponent:filename] error:&error];
  949. }
  950. }
  951. + (void)verifyVersionCoreData:(UIViewController *)vc
  952. {
  953. NSString *applicationName = [[[NSBundle mainBundle] infoDictionary] valueForKey:(NSString *)kCFBundleNameKey];
  954. // Get the path for our model (in this case it's named 'cache')
  955. NSURL *url = [[NSBundle mainBundle] URLForResource:@"cryptocloud" withExtension:@"momd"];
  956. NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[[NSManagedObjectModel alloc] initWithContentsOfURL:url]]; /* get a coordinator */
  957. NSString *sourceStoreType = nil;/* type for the source store, or nil if not known */ ;
  958. NSString *path = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) firstObject];
  959. // Figure out the full path where our store is located
  960. path = [path stringByAppendingFormat:@"/%@/cryptocloud", applicationName];
  961. NSURL *sourceStoreURL = [NSURL fileURLWithPath:path]; /* URL for the source store */ ;
  962. NSError *error = nil;
  963. NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:sourceStoreType URL:sourceStoreURL error:&error];
  964. if (sourceMetadata == nil) {
  965. NSLog(@"[LOG] Error checking migration validity");
  966. } else {
  967. if (![[psc managedObjectModel] isConfiguration:nil compatibleWithStoreMetadata:sourceMetadata]) {
  968. UIAlertController * alert= [UIAlertController alertControllerWithTitle:nil message:NSLocalizedString(@"_required_new_database_", nil) preferredStyle:UIAlertControllerStyleAlert];
  969. UIAlertAction* ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault
  970. handler:^(UIAlertAction * action) {
  971. [alert dismissViewControllerAnimated:YES completion:nil];
  972. }];
  973. [alert addAction:ok];
  974. [vc presentViewController:alert animated:YES completion:nil];
  975. // Delete CoreData store
  976. NSFileManager *manager = [NSFileManager defaultManager];
  977. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
  978. NSString *directory = [[paths lastObject] stringByAppendingPathComponent:applicationName];
  979. NSArray *files = [manager contentsOfDirectoryAtPath:directory error:nil];
  980. NSError *error;
  981. for(NSString *filename in files) {
  982. [manager removeItemAtPath:[directory stringByAppendingPathComponent:filename] error:&error];
  983. }
  984. }
  985. }
  986. }
  987. #pragma --------------------------------------------------------------------------------------------
  988. #pragma mark ===== Flush Database =====
  989. #pragma --------------------------------------------------------------------------------------------
  990. + (void)flushTableDirectoryAccount:(NSString *)account
  991. {
  992. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  993. if (account) {
  994. [TableDirectory MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", account] inContext:context];
  995. } else {
  996. [TableDirectory MR_truncateAllInContext:context];
  997. }
  998. [context MR_saveToPersistentStoreAndWait];
  999. }
  1000. + (void)flushTableLocalFileAccount:(NSString *)account
  1001. {
  1002. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1003. if (account) {
  1004. [TableLocalFile MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", account] inContext:context];
  1005. } else {
  1006. [TableLocalFile MR_truncateAllInContext:context];
  1007. }
  1008. [context MR_saveToPersistentStoreAndWait];
  1009. }
  1010. + (void)flushAllDatabase
  1011. {
  1012. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1013. [TableAccount MR_truncateAllInContext:context];
  1014. [TableDirectory MR_truncateAllInContext:context];
  1015. [TableLocalFile MR_truncateAllInContext:context];
  1016. [context MR_saveToPersistentStoreAndWait];
  1017. }
  1018. @end