CCCoreData.m 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  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. /*
  842. + (NSArray *)getHomeOfflineActiveAccount:(NSString *)activeAccount directoryUser:(NSString *)directoryUser fieldOrder:(NSString *)fieldOrder ascending:(BOOL)ascending
  843. {
  844. NSMutableArray *tableMetadatas = [NSMutableArray new];
  845. NSArray *directoriesOffline = [self getOfflineDirectoryActiveAccount:activeAccount];
  846. NSString *father = @"";
  847. NSSortDescriptor *descriptor;
  848. // Add directory
  849. for (TableDirectory *directory in directoriesOffline) {
  850. if (![directory.serverUrl containsString:father]) {
  851. father = directory.serverUrl;
  852. NSString *upDir = [CCUtility deletingLastPathComponentFromServerUrl:father];
  853. NSString *directoryID = [self getDirectoryIDFromServerUrl:upDir activeAccount:activeAccount];
  854. NSString *fileName = [father lastPathComponent];
  855. if (upDir && directoryID && fileName) {
  856. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@) AND (directory == 1) AND (fileNameData == %@)", directoryID, activeAccount, fileName];
  857. //TableMetadata *tableMetadata = [self getTableMetadataWithPreficate:predicate];
  858. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPreficate:predicate];
  859. if (metadata)
  860. [tableMetadatas addObject:metadata];
  861. }
  862. }
  863. }
  864. // Add files
  865. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (offline == 1)", activeAccount];
  866. NSArray *localFiles = [CCCoreData getTableLocalFileWithPredicate:predicate];
  867. for (TableLocalFile *localFile in localFiles) {
  868. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", activeAccount, localFile.fileID];
  869. //TableMetadata *tableMetadata = [self getTableMetadataWithPreficate:predicate];
  870. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPreficate:predicate];
  871. if (metadata)
  872. [tableMetadatas addObject:metadata];
  873. }
  874. // Order
  875. if ([fieldOrder isEqualToString:@"fileName"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"fileNamePrint" ascending:ascending selector:@selector(localizedCaseInsensitiveCompare:)];
  876. else if ([fieldOrder isEqualToString:@"fileDate"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:ascending selector:nil];
  877. else if ([fieldOrder isEqualToString:@"sessionTaskIdentifier"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"sessionTaskIdentifier" ascending:ascending selector:nil];
  878. else descriptor = [[NSSortDescriptor alloc] initWithKey:fieldOrder ascending:ascending selector:@selector(localizedCaseInsensitiveCompare:)];
  879. return [tableMetadatas sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];//[NSArray arrayWithArray:tableMetadatas];
  880. }
  881. */
  882. #pragma --------------------------------------------------------------------------------------------
  883. #pragma mark ===== File System =====
  884. #pragma --------------------------------------------------------------------------------------------
  885. + (BOOL)downloadFile:(tableMetadata *)metadata directoryUser:(NSString *)directoryUser activeAccount:(NSString *)activeAccount
  886. {
  887. // ----------------------------------------- FILESYSTEM ------------------------------------------
  888. // if encrypted, rewrite
  889. if (metadata.cryptated == YES)
  890. if ([[CCCrypto sharedManager] decrypt:metadata.fileID fileNameDecrypted:metadata.fileID fileNamePrint:metadata.fileNamePrint password:[[CCCrypto sharedManager] getKeyPasscode:metadata.uuid] directoryUser:directoryUser] == 0) return NO;
  891. // ------------------------------------------ COREDATA -------------------------------------------
  892. // add/update Table Local File
  893. [self addLocalFile:metadata activeAccount:activeAccount];
  894. // EXIF
  895. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image])
  896. [CCExifGeo setExifLocalTableEtag:metadata directoryUser:directoryUser activeAccount:activeAccount];
  897. // Icon
  898. [CCGraphics createNewImageFrom:metadata.fileID directoryUser:directoryUser fileNameTo:metadata.fileID fileNamePrint:metadata.fileNamePrint size:@"m" imageForUpload:NO typeFile:metadata.typeFile writePreview:YES optimizedFileName:[CCUtility getOptimizedPhoto]];
  899. return YES;
  900. }
  901. + (void)downloadFilePlist:(tableMetadata *)metadata activeAccount:(NSString *)activeAccount activeUrl:(NSString *)activeUrl directoryUser:(NSString *)directoryUser
  902. {
  903. metadata = [[NCManageDatabase sharedInstance] copyTableMetadata:metadata];
  904. [CCUtility insertInformationPlist:metadata directoryUser:directoryUser];
  905. [[NCManageDatabase sharedInstance] updateMetadata:metadata activeUrl:activeUrl];
  906. // se è un template aggiorniamo anche nel FileSystem
  907. if ([metadata.type isEqualToString: k_metadataType_template]){
  908. [self updateLocalFileModel:metadata activeAccount:activeAccount];
  909. }
  910. }
  911. + (void)deleteFile:(tableMetadata *)metadata serverUrl:(NSString *)serverUrl directoryUser:(NSString *)directoryUser activeAccount:(NSString *)activeAccount
  912. {
  913. if (!metadata) return;
  914. // ----------------------------------------- FILESYSTEM ------------------------------------------
  915. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", directoryUser, metadata.fileID] error:nil];
  916. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.ico", directoryUser, metadata.fileID] error:nil];
  917. // ------------------------------------------ DATABASE -------------------------------------------
  918. // se è una directory cancelliamo tutto quello che è della directory
  919. if (metadata.directory && serverUrl) {
  920. NSString *dirForDelete = [CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileNameData];
  921. [[NCManageDatabase sharedInstance] deleteDirectoryAndSubDirectoryWithServerUrl:dirForDelete];
  922. }
  923. [self deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", metadata.fileID, activeAccount]];
  924. [[NCManageDatabase sharedInstance] deleteMetadata:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", metadata.fileID, activeAccount]];
  925. }
  926. #pragma --------------------------------------------------------------------------------------------
  927. #pragma mark ===== Utility Database =====
  928. #pragma --------------------------------------------------------------------------------------------
  929. + (void)moveCoreDataToGroup
  930. {
  931. NSString *applicationName = [[[NSBundle mainBundle] infoDictionary] valueForKey:(NSString *)kCFBundleNameKey];
  932. NSURL *dirGroup = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:[NCBrandOptions sharedInstance].capabilitiesGroups];
  933. NSString *dirToPath = [[dirGroup URLByAppendingPathComponent:appDatabase] path];
  934. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
  935. NSString *dirFromPath = [[paths lastObject] stringByAppendingPathComponent:applicationName];
  936. NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirFromPath error:nil];
  937. NSError *error;
  938. for(NSString *filename in files)
  939. [[NSFileManager defaultManager] moveItemAtPath:[dirFromPath stringByAppendingPathComponent:filename] toPath:[dirToPath stringByAppendingPathComponent:filename] error:&error];
  940. }
  941. + (void)moveAllUserToGroup
  942. {
  943. NSArray *records = [TableAccount MR_findAll];
  944. for (TableAccount *record in records) {
  945. NSString *dirFromPath = [CCUtility getOLDDirectoryActiveUser:record.user activeUrl:record.url];
  946. NSString *dirToPath = [CCUtility getDirectoryActiveUser:record.user activeUrl:record.url];
  947. NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirFromPath error:nil];
  948. NSError *error;
  949. for(NSString *filename in files)
  950. [[NSFileManager defaultManager] moveItemAtPath:[dirFromPath stringByAppendingPathComponent:filename] toPath:[dirToPath stringByAppendingPathComponent:filename] error:&error];
  951. }
  952. }
  953. + (void)verifyVersionCoreData:(UIViewController *)vc
  954. {
  955. NSString *applicationName = [[[NSBundle mainBundle] infoDictionary] valueForKey:(NSString *)kCFBundleNameKey];
  956. // Get the path for our model (in this case it's named 'cache')
  957. NSURL *url = [[NSBundle mainBundle] URLForResource:@"cryptocloud" withExtension:@"momd"];
  958. NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[[NSManagedObjectModel alloc] initWithContentsOfURL:url]]; /* get a coordinator */
  959. NSString *sourceStoreType = nil;/* type for the source store, or nil if not known */ ;
  960. NSString *path = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) firstObject];
  961. // Figure out the full path where our store is located
  962. path = [path stringByAppendingFormat:@"/%@/cryptocloud", applicationName];
  963. NSURL *sourceStoreURL = [NSURL fileURLWithPath:path]; /* URL for the source store */ ;
  964. NSError *error = nil;
  965. NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:sourceStoreType URL:sourceStoreURL error:&error];
  966. if (sourceMetadata == nil) {
  967. NSLog(@"[LOG] Error checking migration validity");
  968. } else {
  969. if (![[psc managedObjectModel] isConfiguration:nil compatibleWithStoreMetadata:sourceMetadata]) {
  970. UIAlertController * alert= [UIAlertController alertControllerWithTitle:nil message:NSLocalizedString(@"_required_new_database_", nil) preferredStyle:UIAlertControllerStyleAlert];
  971. UIAlertAction* ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault
  972. handler:^(UIAlertAction * action) {
  973. [alert dismissViewControllerAnimated:YES completion:nil];
  974. }];
  975. [alert addAction:ok];
  976. [vc presentViewController:alert animated:YES completion:nil];
  977. // Delete CoreData store
  978. NSFileManager *manager = [NSFileManager defaultManager];
  979. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
  980. NSString *directory = [[paths lastObject] stringByAppendingPathComponent:applicationName];
  981. NSArray *files = [manager contentsOfDirectoryAtPath:directory error:nil];
  982. NSError *error;
  983. for(NSString *filename in files) {
  984. [manager removeItemAtPath:[directory stringByAppendingPathComponent:filename] error:&error];
  985. }
  986. }
  987. }
  988. }
  989. #pragma --------------------------------------------------------------------------------------------
  990. #pragma mark ===== Flush Database =====
  991. #pragma --------------------------------------------------------------------------------------------
  992. + (void)flushTableDirectoryAccount:(NSString *)account
  993. {
  994. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  995. if (account) {
  996. [TableDirectory MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", account] inContext:context];
  997. } else {
  998. [TableDirectory MR_truncateAllInContext:context];
  999. }
  1000. [context MR_saveToPersistentStoreAndWait];
  1001. }
  1002. + (void)flushTableLocalFileAccount:(NSString *)account
  1003. {
  1004. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1005. if (account) {
  1006. [TableLocalFile MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", account] inContext:context];
  1007. } else {
  1008. [TableLocalFile MR_truncateAllInContext:context];
  1009. }
  1010. [context MR_saveToPersistentStoreAndWait];
  1011. }
  1012. + (void)flushAllDatabase
  1013. {
  1014. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1015. [TableAccount MR_truncateAllInContext:context];
  1016. [TableDirectory MR_truncateAllInContext:context];
  1017. [TableLocalFile MR_truncateAllInContext:context];
  1018. [context MR_saveToPersistentStoreAndWait];
  1019. }
  1020. @end