CCCoreData.m 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  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 etag (BUG 2.10)
  49. [TableMetadata MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (etag == %@)", activeAccount, metadata.etag] 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 *)etag 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 (etag == %@)", activeAccount, etag] 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.etag offline:NO activeAccount:activeAccount];
  236. }];
  237. }
  238. */
  239. #pragma --------------------------------------------------------------------------------------------
  240. #pragma mark ===== Directory =====
  241. #pragma --------------------------------------------------------------------------------------------
  242. + (NSString *)addDirectory:(NSString *)serverUrl permissions:(NSString *)permissions activeAccount:(NSString *)activeAccount
  243. {
  244. NSString *directoryID;
  245. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  246. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount] inContext:context];
  247. if (record) {
  248. directoryID = record.directoryID;
  249. if (permissions) record.permissions = permissions;
  250. } else {
  251. TableDirectory *record = [TableDirectory MR_createEntityInContext:context];
  252. record.account = activeAccount;
  253. record.directoryID = [CCUtility createRandomString:16];
  254. directoryID = record.directoryID;
  255. if (permissions) record.permissions = permissions;
  256. record.serverUrl = serverUrl;
  257. }
  258. [context MR_saveToPersistentStoreAndWait];
  259. return directoryID;
  260. }
  261. + (void)updateDirectoryEtagServerUrl:(NSString *)serverUrl etag:(NSString *)etag activeAccount:(NSString *)activeAccount
  262. {
  263. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  264. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  265. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:localContext];
  266. if (record)
  267. record.rev = etag;
  268. }];
  269. }
  270. + (void)deleteDirectoryFromPredicate:(NSPredicate *)predicate
  271. {
  272. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  273. [TableDirectory MR_deleteAllMatchingPredicate:predicate inContext:localContext];
  274. }];
  275. }
  276. + (NSArray *)deleteDirectoryAndSubDirectory:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  277. {
  278. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  279. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  280. NSMutableArray *directoryIDs = [[NSMutableArray alloc] init];
  281. NSArray *tableDirectorys = [TableDirectory MR_findAllWithPredicate:predicate inContext:context];
  282. for(TableDirectory *recordDirectory in tableDirectorys) {
  283. NSLog(@"[LOG] %@", recordDirectory.serverUrl);
  284. if ([recordDirectory.serverUrl hasPrefix:serverUrl]) {
  285. // List directoryIDs removed
  286. [directoryIDs addObject:recordDirectory.directoryID];
  287. // remove all TableMetadata
  288. NSLog(@"[LOG] %@", recordDirectory.directoryID);
  289. // remove directory in Metadata come cazzo si fa a saperlo
  290. //[TableMetadata MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", activeAccount, recordDirectory.directoryID] inContext:context];
  291. /*
  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.etag);
  303. [self deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (etag == %@)", activeAccount, recordMetadata.etag]];
  304. [recordMetadata MR_deleteEntityInContext:context];
  305. }
  306. [recordDirectory MR_deleteEntityInContext:context];
  307. */
  308. }
  309. }
  310. [context MR_saveToPersistentStoreAndWait];
  311. return directoryIDs;
  312. }
  313. + (void)renameDirectory:(NSString *)serverUrl serverUrlTo:(NSString *)serverUrlTo activeAccount:(NSString *)activeAccount
  314. {
  315. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  316. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  317. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:localContext];
  318. if (record)
  319. record.serverUrl = serverUrlTo;
  320. }];
  321. }
  322. + (void)setDateReadDirectoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount
  323. {
  324. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  325. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", directoryID, activeAccount];
  326. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  327. if (record) {
  328. record.dateReadDirectory = [NSDate date];
  329. [context MR_saveToPersistentStoreAndWait];
  330. }
  331. }
  332. + (TableDirectory *)getTableDirectoryWithPreficate:(NSPredicate *)predicate
  333. {
  334. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  335. return [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  336. }
  337. + (NSDate *)getDateReadDirectoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount
  338. {
  339. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  340. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", directoryID, activeAccount];
  341. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  342. if (record) return record.dateReadDirectory;
  343. else return nil;
  344. }
  345. + (void)setDirectoryRev:(NSString *)rev serverUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  346. {
  347. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  348. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  349. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  350. if (record) {
  351. record.rev = rev;
  352. [context MR_saveToPersistentStoreAndWait];
  353. }
  354. }
  355. + (NSString *)getDirectoryRevFromServerUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  356. {
  357. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  358. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  359. if (record) return record.rev;
  360. else return nil;
  361. }
  362. + (NSArray *)getDirectoryIDsFromBeginsWithServerUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  363. {
  364. NSString *serverUrlBeginWith = serverUrl;
  365. if (![serverUrl hasSuffix:@"/"])
  366. serverUrlBeginWith = [serverUrl stringByAppendingString:@"/"];
  367. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"((serverUrl == %@) OR (serverUrl BEGINSWITH %@)) AND (account == %@)", serverUrl, serverUrlBeginWith, activeAccount];
  368. return [TableDirectory MR_findAllWithPredicate:predicate];
  369. }
  370. + (NSString *)getDirectoryIDFromServerUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  371. {
  372. if (serverUrl == nil) return nil;
  373. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  374. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  375. if (record) return record.directoryID;
  376. else {
  377. return [self addDirectory:serverUrl permissions:nil activeAccount:activeAccount];
  378. }
  379. return nil;
  380. }
  381. + (NSString *)getServerUrlFromDirectoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount
  382. {
  383. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", directoryID, activeAccount];
  384. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  385. if (record) return record.serverUrl;
  386. else return nil;
  387. }
  388. + (void)clearDateReadAccount:(NSString *)activeAccount serverUrl:(NSString *)serverUrl directoryID:(NSString *)directoryID
  389. {
  390. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  391. NSPredicate *predicate;
  392. if ([serverUrl length] > 0)
  393. predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  394. if ([directoryID length] > 0)
  395. predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", directoryID, activeAccount];
  396. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:localContext];
  397. if (record) {
  398. record.dateReadDirectory = NULL;
  399. record.rev = @"";
  400. }
  401. }];
  402. }
  403. + (void)clearAllDateReadDirectory
  404. {
  405. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  406. NSArray *records = [TableDirectory MR_findAllInContext:localContext];
  407. for (TableDirectory *record in records) {
  408. record.dateReadDirectory = NULL;
  409. record.rev = @"";
  410. }
  411. }];
  412. }
  413. + (BOOL)isDirectoryOutOfDate:(int)numAddDay directoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount
  414. {
  415. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", activeAccount, directoryID];
  416. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  417. if (record == nil || record.dateReadDirectory == nil) {
  418. return YES;
  419. }
  420. NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
  421. [dateComponents setWeekday:numAddDay];
  422. NSCalendar *calendar = [NSCalendar currentCalendar];
  423. NSDate *datePlus = [calendar dateByAddingComponents:dateComponents toDate:record.dateReadDirectory options:0];
  424. NSDate *now = [NSDate date];
  425. // usa la Cache se richiesto e se la data è entro X giorni dall'ultima volta che l'hai letta.
  426. if ([now compare:datePlus] == NSOrderedDescending) {
  427. return YES;
  428. }
  429. return NO;
  430. }
  431. #pragma --------------------------------------------------------------------------------------------
  432. #pragma mark ===== Offline Directory =====
  433. #pragma --------------------------------------------------------------------------------------------
  434. + (void)removeOfflineDirectoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount
  435. {
  436. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  437. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@) AND (offline == 1)", directoryID, activeAccount];
  438. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:localContext];
  439. if (record)
  440. record.offline = [NSNumber numberWithBool:FALSE];
  441. }];
  442. }
  443. + (NSArray *)getOfflineDirectoryActiveAccount:(NSString *)activeAccount
  444. {
  445. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (offline == 1)", activeAccount];
  446. NSArray *recordsTable = [TableDirectory MR_findAllWithPredicate:predicate];
  447. // Order by serverUrl
  448. NSArray *sortedRecordsTable = [recordsTable sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  449. TableDirectory *record1 = obj1, *record2 = obj2;
  450. return [record1.serverUrl compare:record2.serverUrl];
  451. }];
  452. return sortedRecordsTable;
  453. }
  454. + (void)setOfflineDirectoryServerUrl:(NSString *)serverUrl offline:(BOOL)offline activeAccount:(NSString *)activeAccount
  455. {
  456. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  457. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  458. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:localContext];
  459. if (record)
  460. record.offline = [NSNumber numberWithBool:offline];
  461. }];
  462. }
  463. + (BOOL)isOfflineDirectoryServerUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  464. {
  465. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (offline == 1) AND (account == %@)", serverUrl, activeAccount];
  466. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  467. if (record) return YES;
  468. else return NO;
  469. }
  470. #pragma --------------------------------------------------------------------------------------------
  471. #pragma mark ===== Directory Lock =====
  472. #pragma --------------------------------------------------------------------------------------------
  473. + (BOOL)setDirectoryLock:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  474. {
  475. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  476. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", [self getDirectoryIDFromServerUrl:serverUrl activeAccount:activeAccount], activeAccount];
  477. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  478. if (record) {
  479. record.lock = [NSNumber numberWithBool:YES];
  480. [context MR_saveToPersistentStoreAndWait];
  481. return YES;
  482. }
  483. else return NO;
  484. }
  485. + (BOOL)setDirectoryUnLock:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  486. {
  487. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  488. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", [self getDirectoryIDFromServerUrl:serverUrl activeAccount:activeAccount], activeAccount];
  489. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  490. if (record) {
  491. record.lock = [NSNumber numberWithBool:NO];
  492. [context MR_saveToPersistentStoreAndWait];
  493. return YES;
  494. }
  495. else return NO;
  496. }
  497. + (void)setAllDirectoryUnLockForAccount:(NSString *)activeAccount
  498. {
  499. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  500. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  501. NSArray *records = [TableDirectory MR_findAllWithPredicate:predicate inContext:localContext];
  502. for(TableDirectory *record in records)
  503. record.lock = [NSNumber numberWithBool:NO];
  504. }];
  505. }
  506. + (BOOL)isDirectoryLock:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  507. {
  508. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (lock == 1) AND (account == %@)", [self getDirectoryIDFromServerUrl:serverUrl activeAccount:activeAccount], activeAccount];
  509. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  510. if (record) return YES;
  511. else return NO;
  512. }
  513. + (BOOL)isBlockZone:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  514. {
  515. /*
  516. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (directory == 1)", activeAccount];
  517. NSArray *records = [TableMetadata MR_findAllWithPredicate:predicate];
  518. if ([records count] > 0) {
  519. NSArray *pathComponents = [serverUrl pathComponents];
  520. for (NSString *fileName in pathComponents) {
  521. for(TableMetadata *record in records){
  522. NSString *fileNameEntity = [CCUtility trasformedFileNamePlistInCrypto:record.fileName];
  523. NSString *directoryID = record.directoryID;
  524. NSString *serverUrlEntity = [self getServerUrlFromDirectoryID:directoryID activeAccount:activeAccount];
  525. if([fileName isEqualToString:fileNameEntity]) {
  526. NSString *lockServerUrl = [CCUtility stringAppendServerUrl:serverUrlEntity addFileName:fileNameEntity];
  527. BOOL risultato = [self isDirectoryLock:lockServerUrl activeAccount:activeAccount];
  528. if (risultato) return YES;
  529. }
  530. }
  531. }
  532. }
  533. */
  534. return NO;
  535. }
  536. #pragma --------------------------------------------------------------------------------------------
  537. #pragma mark ===== LocalFile =====
  538. #pragma --------------------------------------------------------------------------------------------
  539. + (void)addLocalFile:(tableMetadata *)metadata activeAccount:(NSString *)activeAccount
  540. {
  541. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  542. BOOL offline = NO;
  543. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (etag == %@)", activeAccount, metadata.etag];
  544. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  545. if (record) {
  546. offline = [[record valueForKey:@"offline"] boolValue];
  547. [record MR_deleteEntityInContext:localContext];
  548. }
  549. record = [TableLocalFile MR_createEntityInContext:localContext];
  550. record.account = activeAccount;
  551. record.date = metadata.date;
  552. record.etag = metadata.etag;
  553. record.exifDate = [NSDate date];
  554. record.exifLatitude = @"-1";
  555. record.exifLongitude = @"-1";
  556. record.offline = [NSNumber numberWithBool:offline];
  557. record.fileName = metadata.fileName;
  558. record.fileNamePrint = metadata.fileNamePrint;
  559. record.rev = metadata.rev;
  560. record.size = [NSNumber numberWithLong:metadata.size];
  561. }];
  562. }
  563. + (void)deleteLocalFileWithPredicate:(NSPredicate *)predicate
  564. {
  565. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  566. [TableLocalFile MR_deleteAllMatchingPredicate:predicate inContext:localContext];
  567. }];
  568. }
  569. + (void)renameLocalFileWithEtag:(NSString *)etag fileNameTo:(NSString *)fileNameTo fileNamePrintTo:(NSString *)fileNamePrintTo activeAccount:(NSString *)activeAccount
  570. {
  571. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  572. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(etag == %@) AND (account == %@)", etag, activeAccount];
  573. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  574. if (record) {
  575. if (fileNameTo)record.fileName = fileNameTo;
  576. if (fileNamePrintTo)record.fileNamePrint = fileNamePrintTo;
  577. }
  578. }];
  579. }
  580. + (void)updateLocalFileModel:(tableMetadata *)metadata activeAccount:(NSString *)activeAccount
  581. {
  582. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  583. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileName == %@)", activeAccount, metadata.fileName];
  584. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  585. if (record) {
  586. record.etag = metadata.etag;
  587. record.date = metadata.date;
  588. record.fileNamePrint = metadata.fileNamePrint;
  589. } else {
  590. [self addLocalFile:metadata activeAccount:activeAccount];
  591. }
  592. }];
  593. }
  594. + (TableLocalFile *)getLocalFileWithEtag:(NSString *)etag activeAccount:(NSString *)activeAccount
  595. {
  596. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(etag == %@) AND (account == %@)", etag, activeAccount];
  597. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate];
  598. if (record) {
  599. return record;
  600. } else return nil;
  601. }
  602. + (NSMutableArray *)getTableLocalFileWithPredicate:(NSPredicate *)predicate controlZombie:(BOOL)controlZombie activeAccount:(NSString *)activeAccount directoryUser:(NSString *)directoryUser
  603. {
  604. NSMutableArray *ritorno = [[NSMutableArray alloc] init];
  605. NSArray *records = [TableLocalFile MR_findAllWithPredicate:predicate];
  606. if ([records count] > 0) {
  607. // verifichiamo esistano tutti i file altrimenti rimuoviamo il record
  608. for(TableLocalFile *record in records){
  609. if (controlZombie) {
  610. NSString *etag = record.etag;
  611. NSString *FilePathEtag = [NSString stringWithFormat:@"%@/%@", directoryUser, etag];
  612. NSString *FilePathFileName = [NSString stringWithFormat:@"%@/%@", directoryUser, record.fileName];
  613. if (![[NSFileManager defaultManager] fileExistsAtPath:FilePathEtag] && ![[NSFileManager defaultManager] fileExistsAtPath:FilePathFileName] && controlZombie) {
  614. // non esiste nè il file etag e nemmeno il plist, eliminiamolo.
  615. [self deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"(etag == %@) AND (account == %@)", etag, activeAccount]];
  616. } else [ritorno addObject:record];
  617. } else [ritorno addObject:record];
  618. }
  619. } else return nil;
  620. if ([ritorno count] == 0) return nil;
  621. else return ritorno;
  622. }
  623. + (NSArray *)getTableLocalFileWithPredicate:(NSPredicate *)predicate
  624. {
  625. return [TableLocalFile MR_findAllWithPredicate:predicate];
  626. }
  627. #pragma --------------------------------------------------------------------------------------------
  628. #pragma mark ===== Offline LocalFile =====
  629. #pragma --------------------------------------------------------------------------------------------
  630. + (void)setOfflineLocalEtag:(NSString *)etag offline:(BOOL)offline activeAccount:(NSString *)activeAccount
  631. {
  632. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  633. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(etag == %@) AND (account == %@)", etag, activeAccount];
  634. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  635. if (record)
  636. record.offline = [NSNumber numberWithBool:offline];
  637. }];
  638. }
  639. + (BOOL)isOfflineLocalEtag:(NSString *)etag activeAccount:(NSString *)activeAccount
  640. {
  641. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(etag == %@) AND (offline == 1) AND (account == %@)", etag, activeAccount];
  642. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate];
  643. if (record) return YES;
  644. else return NO;
  645. }
  646. + (NSArray *)getOfflineLocalFileActiveAccount:(NSString *)activeAccount directoryUser:(NSString *)directoryUser
  647. {
  648. NSMutableArray *metadatas = [NSMutableArray new];
  649. NSArray *files = [self getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (offline == 1)", activeAccount] controlZombie:YES activeAccount:activeAccount directoryUser:directoryUser];
  650. for (TableLocalFile *file in files) {
  651. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(etag == %@) AND (account == %@)", file.etag, activeAccount];
  652. //tableMetadata *metadata = [self getMetadataWithPreficate:predicate context:nil];
  653. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPreficate:predicate];
  654. if (metadata) {
  655. // verify if is not on directory offline
  656. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (offline == 1) AND (account == %@)", metadata.directoryID, activeAccount];
  657. TableDirectory *directory = [TableDirectory MR_findFirstWithPredicate:predicate];
  658. if (!directory)
  659. [metadatas addObject:metadata];
  660. }
  661. }
  662. return metadatas;
  663. }
  664. #pragma --------------------------------------------------------------------------------------------
  665. #pragma mark ===== GeoInformation =====
  666. #pragma --------------------------------------------------------------------------------------------
  667. + (NSArray *)getGeoInformationLocalFromEtag:(NSString *)etag activeAccount:(NSString *)activeAccount
  668. {
  669. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(etag == %@) AND (account == %@)", etag, activeAccount];
  670. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate];
  671. if (record) return [[NSArray alloc] initWithObjects:record.exifDate, record.exifLatitude, record.exifLongitude, nil];
  672. else return nil;
  673. }
  674. + (void)setGeoInformationLocalFromEtag:(NSString *)etag exifDate:(NSDate *)exifDate exifLatitude:(NSString *)exifLatitude exifLongitude:(NSString *)exifLongitude activeAccount:(NSString *)activeAccount
  675. {
  676. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  677. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (etag == %@)", activeAccount, etag];
  678. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  679. if (record) {
  680. record.exifDate = exifDate;
  681. record.exifLatitude = exifLatitude;
  682. record.exifLongitude = exifLongitude;
  683. }
  684. }];
  685. }
  686. + (void)setGeoInformationLocalNull
  687. {
  688. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  689. NSArray *records = [TableLocalFile MR_findAllInContext:localContext];
  690. for (TableLocalFile *record in records) {
  691. if ([record.exifLatitude doubleValue] != 0 || [record.exifLongitude doubleValue] != 0) {
  692. record.exifLatitude = @"9999";
  693. record.exifLongitude = @"9999";
  694. }
  695. }
  696. }];
  697. }
  698. #pragma --------------------------------------------------------------------------------------------
  699. #pragma mark ===== Certificates =====
  700. #pragma --------------------------------------------------------------------------------------------
  701. + (NSMutableArray *)getAllCertificatesLocationOldDB
  702. {
  703. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  704. NSMutableArray *output = [NSMutableArray new];
  705. NSArray *records = [TableCertificates MR_findAllInContext:context];
  706. for (TableCertificates *record in records) {
  707. if (record.certificateLocation && record.certificateLocation.length > 0)
  708. [output addObject:record.certificateLocation];
  709. }
  710. return output;
  711. }
  712. #pragma --------------------------------------------------------------------------------------------
  713. #pragma mark ===== Share =====
  714. #pragma --------------------------------------------------------------------------------------------
  715. /*
  716. + (void)setShareLink:(NSString *)share fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl sharesLink:(NSMutableDictionary *)sharesLink activeAccount:(NSString *)activeAccount
  717. {
  718. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  719. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileName == %@) AND (serverUrl == %@)", activeAccount, fileName, serverUrl];
  720. TableShare *record = [TableShare MR_findFirstWithPredicate:predicate inContext:context];
  721. if (record) {
  722. record.shareLink = share;
  723. } else {
  724. TableShare *record = [TableShare MR_createEntityInContext:context];
  725. record.account = activeAccount;
  726. record.fileName = fileName;
  727. record.serverUrl = serverUrl;
  728. record.shareLink = share;
  729. }
  730. [context MR_saveToPersistentStoreAndWait];
  731. if (share && serverUrl && fileName)
  732. [sharesLink setObject:share forKey:[serverUrl stringByAppendingString:fileName]];
  733. }
  734. + (void)setShareUserAndGroup:(NSString *)share fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup activeAccount:(NSString *)activeAccount
  735. {
  736. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  737. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileName == %@) AND (serverUrl == %@)", activeAccount, fileName, serverUrl];
  738. TableShare *record = [TableShare MR_findFirstWithPredicate:predicate inContext:context];
  739. if (record) {
  740. record.shareUserAndGroup = share;
  741. } else {
  742. TableShare *record = [TableShare MR_createEntityInContext:context];
  743. record.account = activeAccount;
  744. record.fileName = fileName;
  745. record.serverUrl = serverUrl;
  746. record.shareUserAndGroup = share;
  747. }
  748. [context MR_saveToPersistentStoreAndWait];
  749. [sharesUserAndGroup setObject:share forKey:[serverUrl stringByAppendingString:fileName]];
  750. }
  751. + (void)unShare:(NSString *)share fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl sharesLink:(NSMutableDictionary *)sharesLink sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup activeAccount:(NSString *)activeAccount
  752. {
  753. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  754. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND ((shareLink CONTAINS %@) OR (shareUserAndGroup CONTAINS %@))", activeAccount, share, share];
  755. TableShare *record = [TableShare MR_findFirstWithPredicate:predicate inContext:context];
  756. if (record) {
  757. if ([record.shareLink containsString:share]) record.shareLink = @"";
  758. if ([record.shareUserAndGroup containsString:share]) {
  759. NSMutableArray *shares = [[NSMutableArray alloc] initWithArray:[record.shareUserAndGroup componentsSeparatedByString:@","]];
  760. [shares removeObject:share];
  761. record.shareUserAndGroup = [shares componentsJoinedByString:@","];
  762. }
  763. if ([record.shareLink length] == 0 && [record.shareUserAndGroup length] == 0)
  764. [record MR_deleteEntityInContext:context];
  765. [context MR_saveToPersistentStoreAndWait];
  766. if ([record.shareLink length] > 0) [sharesLink setObject:record.shareLink forKey:[serverUrl stringByAppendingString:fileName]];
  767. else [sharesLink removeObjectForKey:[serverUrl stringByAppendingString:fileName]];
  768. if ([record.shareUserAndGroup length] > 0) [sharesUserAndGroup setObject:record.shareUserAndGroup forKey:[serverUrl stringByAppendingString:fileName]];
  769. else [sharesUserAndGroup removeObjectForKey:[serverUrl stringByAppendingString:fileName]];
  770. }
  771. }
  772. + (void)removeAllShareActiveAccount:(NSString *)activeAccount sharesLink:(NSMutableDictionary *)sharesLink sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup
  773. {
  774. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  775. [TableShare MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"account == %@", activeAccount] inContext:context];
  776. [context MR_saveToPersistentStoreAndWait];
  777. [sharesLink removeAllObjects];
  778. [sharesUserAndGroup removeAllObjects];
  779. }
  780. + (void)updateShare:(NSDictionary *)items sharesLink:(NSMutableDictionary *)sharesLink sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup activeAccount:(NSString *)activeAccount activeUrl:(NSString *)activeUrl
  781. {
  782. // rimuovi tutte le condivisioni
  783. [self removeAllShareActiveAccount:activeAccount sharesLink:sharesLink sharesUserAndGroup:sharesUserAndGroup];
  784. NSMutableArray *itemsLink = [[NSMutableArray alloc] init];
  785. NSMutableArray *itemsUsersAndGroups = [[NSMutableArray alloc] init];
  786. for (NSString *idRemoteShared in items) {
  787. OCSharedDto *item = [items objectForKey:idRemoteShared];
  788. if (item.shareType == shareTypeLink) [itemsLink addObject:item];
  789. if ([[item shareWith] length] > 0 && (item.shareType == shareTypeUser || item.shareType == shareTypeGroup || item.shareType == shareTypeRemote)) [itemsUsersAndGroups addObject:item];
  790. }
  791. // Link
  792. for (OCSharedDto *item in itemsLink) {
  793. NSString *fullPath = [[CCUtility getHomeServerUrlActiveUrl:activeUrl] stringByAppendingString:item.path];
  794. NSString *fileName = [fullPath lastPathComponent];
  795. NSString *serverUrl = [fullPath substringToIndex:([fullPath length]-[fileName length]-1)];
  796. if ([serverUrl hasSuffix:@"/"]) serverUrl = [serverUrl substringToIndex:[serverUrl length] - 1];
  797. if ([@(item.idRemoteShared) stringValue])
  798. [self setShareLink:[@(item.idRemoteShared) stringValue] fileName:fileName serverUrl:serverUrl sharesLink:sharesLink activeAccount:activeAccount];
  799. }
  800. // Condivisioni
  801. NSMutableDictionary *paths = [[NSMutableDictionary alloc] init];
  802. // Creazione dizionario
  803. for (OCSharedDto *item in itemsUsersAndGroups) {
  804. if ([paths objectForKey:item.path]) {
  805. NSMutableArray *share = [paths objectForKey:item.path];
  806. [share addObject:[@(item.idRemoteShared) stringValue]];
  807. [paths setObject:share forKey:item.path];
  808. } else {
  809. NSMutableArray *share = [[NSMutableArray alloc] initWithObjects:[@(item.idRemoteShared) stringValue], nil];
  810. [paths setObject:share forKey:item.path];
  811. }
  812. }
  813. // Scrittura su DB
  814. for (NSString *path in paths) {
  815. NSArray *items = [paths objectForKey:path];
  816. NSString *share = [items componentsJoinedByString:@","];
  817. NSLog(@"[LOG] share %@", share);
  818. NSString *fullPath = [[CCUtility getHomeServerUrlActiveUrl:activeUrl] stringByAppendingString:path];
  819. NSString *fileName = [fullPath lastPathComponent];
  820. NSString *serverUrl = [fullPath substringToIndex:([fullPath length]-[fileName length]-1)];
  821. if ([serverUrl hasSuffix:@"/"]) serverUrl = [serverUrl substringToIndex:[serverUrl length] - 1];
  822. if (share)
  823. [self setShareUserAndGroup:share fileName:fileName serverUrl:serverUrl sharesUserAndGroup:sharesUserAndGroup activeAccount:activeAccount];
  824. }
  825. }
  826. + (void)populateSharesVariableFromDBActiveAccount:(NSString *)activeAccount sharesLink:(NSMutableDictionary *)sharesLink sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup
  827. {
  828. [sharesLink removeAllObjects];
  829. [sharesUserAndGroup removeAllObjects];
  830. NSArray *records = [TableShare MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", activeAccount]];
  831. for (TableShare *record in records) {
  832. if ([record.shareLink length] > 0 && record.serverUrl && record.fileName)
  833. [sharesLink setObject:record.shareLink forKey:[record.serverUrl stringByAppendingString:record.fileName]];
  834. if ([record.shareUserAndGroup length] > 0 && record.serverUrl && record.fileName)
  835. [sharesUserAndGroup setObject:record.shareUserAndGroup forKey:[record.serverUrl stringByAppendingString:record.fileName]];
  836. }
  837. return;
  838. }
  839. */
  840. #pragma --------------------------------------------------------------------------------------------
  841. #pragma mark ===== Offline =====
  842. #pragma --------------------------------------------------------------------------------------------
  843. + (NSArray *)getHomeOfflineActiveAccount:(NSString *)activeAccount directoryUser:(NSString *)directoryUser fieldOrder:(NSString *)fieldOrder ascending:(BOOL)ascending
  844. {
  845. NSMutableArray *tableMetadatas = [NSMutableArray new];
  846. NSArray *directoriesOffline = [self getOfflineDirectoryActiveAccount:activeAccount];
  847. NSString *father = @"";
  848. NSSortDescriptor *descriptor;
  849. // Add directory
  850. for (TableDirectory *directory in directoriesOffline) {
  851. if (![directory.serverUrl containsString:father]) {
  852. father = directory.serverUrl;
  853. NSString *upDir = [CCUtility deletingLastPathComponentFromServerUrl:father];
  854. NSString *directoryID = [self getDirectoryIDFromServerUrl:upDir activeAccount:activeAccount];
  855. NSString *fileName = [father lastPathComponent];
  856. if (upDir && directoryID && fileName) {
  857. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@) AND (directory == 1) AND (fileNameData == %@)", directoryID, activeAccount, fileName];
  858. //TableMetadata *tableMetadata = [self getTableMetadataWithPreficate:predicate];
  859. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPreficate:predicate];
  860. if (metadata)
  861. [tableMetadatas addObject:metadata];
  862. }
  863. }
  864. }
  865. // Add files
  866. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (offline == 1)", activeAccount];
  867. NSArray *localFiles = [CCCoreData getTableLocalFileWithPredicate:predicate];
  868. for (TableLocalFile *localFile in localFiles) {
  869. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (etag == %@)", activeAccount, localFile.etag];
  870. //TableMetadata *tableMetadata = [self getTableMetadataWithPreficate:predicate];
  871. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataWithPreficate:predicate];
  872. if (metadata)
  873. [tableMetadatas addObject:metadata];
  874. }
  875. // Order
  876. if ([fieldOrder isEqualToString:@"fileName"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"fileNamePrint" ascending:ascending selector:@selector(localizedCaseInsensitiveCompare:)];
  877. else if ([fieldOrder isEqualToString:@"fileDate"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:ascending selector:nil];
  878. else if ([fieldOrder isEqualToString:@"sessionTaskIdentifier"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"sessionTaskIdentifier" ascending:ascending selector:nil];
  879. else descriptor = [[NSSortDescriptor alloc] initWithKey:fieldOrder ascending:ascending selector:@selector(localizedCaseInsensitiveCompare:)];
  880. return [tableMetadatas sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];//[NSArray arrayWithArray:tableMetadatas];
  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.etag fileNameDecrypted:metadata.etag 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.etag directoryUser:directoryUser fileNameTo:metadata.etag 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.etag] error:nil];
  916. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.ico", directoryUser, metadata.etag] 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. [self deleteDirectoryAndSubDirectory:dirForDelete activeAccount:activeAccount];
  922. }
  923. [self deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"(etag == %@) AND (account == %@)", metadata.etag, activeAccount]];
  924. [[NCManageDatabase sharedInstance] deleteMetadata:[NSPredicate predicateWithFormat:@"(etag == %@) AND (account == %@)", metadata.etag, 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