CCCoreData.m 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230
  1. //
  2. // CCCoreData.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 02/02/16.
  6. // Copyright (c) 2014 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. @implementation CCCoreData
  26. #pragma --------------------------------------------------------------------------------------------
  27. #pragma mark ===== Account =====
  28. #pragma --------------------------------------------------------------------------------------------
  29. + (void)addAccount:(NSString *)account url:(NSString *)url user:(NSString *)user password:(NSString *)password
  30. {
  31. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  32. TableAccount *record = [TableAccount MR_createEntityInContext:context];
  33. record.account = account;
  34. record.active = [NSNumber numberWithBool:NO];
  35. record.cameraUpload = [NSNumber numberWithBool:NO];
  36. record.cameraUploadPhoto = [NSNumber numberWithBool:NO];
  37. record.cameraUploadVideo = [NSNumber numberWithBool:NO];
  38. record.cameraUploadCryptatedPhoto = [NSNumber numberWithBool:NO];
  39. record.cameraUploadCryptatedVideo = [NSNumber numberWithBool:NO];
  40. record.cameraUploadWWAnPhoto = [NSNumber numberWithBool:NO];
  41. record.cameraUploadWWAnVideo = [NSNumber numberWithBool:NO];
  42. record.optimization = [NSDate date];
  43. record.password = password;
  44. record.url = url;
  45. record.user = user;
  46. [context MR_saveToPersistentStoreAndWait];
  47. }
  48. + (void)updateAccount:(NSString *)account withPassword:(NSString *)password
  49. {
  50. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  51. TableAccount *record = [TableAccount MR_findFirstByAttribute:@"account" withValue:account inContext:context];
  52. record.password = password;
  53. [context MR_saveToPersistentStoreAndWait];
  54. }
  55. + (void)deleteAccount:(NSString *)account
  56. {
  57. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  58. TableAccount *record = [TableAccount MR_findFirstByAttribute:@"account" withValue:account inContext:context];
  59. [record MR_deleteEntityInContext:context];
  60. [context MR_saveToPersistentStoreAndWait];
  61. }
  62. + (TableAccount *)setActiveAccount:(NSString *)account
  63. {
  64. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  65. TableAccount *recordAccount = nil;
  66. NSArray *records = [TableAccount MR_findAllInContext:context];
  67. for (TableAccount *record in records) {
  68. if ([record.account isEqualToString:account]) {
  69. record.active = [NSNumber numberWithBool:YES];
  70. recordAccount = record;
  71. } else {
  72. record.active = [NSNumber numberWithBool:NO];
  73. }
  74. }
  75. [context MR_saveToPersistentStoreAndWait];
  76. return [self getActiveAccount];
  77. }
  78. + (NSArray *)getAllAccount
  79. {
  80. NSMutableArray *accounts = [NSMutableArray new];
  81. NSArray *records;
  82. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  83. records = [TableAccount MR_findAllInContext:context];
  84. for (TableAccount *tableAccount in records)
  85. [accounts addObject:tableAccount.account];
  86. return accounts;
  87. }
  88. + (TableAccount *)getTableAccountFromAccount:(NSString *)account
  89. {
  90. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  91. return [TableAccount MR_findFirstByAttribute:@"account" withValue:account inContext:context];
  92. }
  93. + (NSArray *)getAllTableAccount
  94. {
  95. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  96. NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"account" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
  97. NSArray *records;
  98. records = [TableAccount MR_findAllInContext:context];
  99. records = [NSMutableArray arrayWithArray:[records sortedArrayUsingDescriptors:[[NSArray alloc] initWithObjects:descriptor, nil]]];
  100. return records;
  101. }
  102. + (TableAccount *)getActiveAccount
  103. {
  104. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  105. TableAccount *record = [TableAccount MR_findFirstByAttribute:@"active" withValue:[NSNumber numberWithBool:YES] inContext:context];
  106. if (record) return record;
  107. else return nil;
  108. }
  109. + (NSString *)getCameraUploadFolderNameActiveAccount:(NSString *)activeAccount
  110. {
  111. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  112. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate];
  113. if (record) {
  114. if ([record.cameraUploadFolderName length] > 0 ) return record.cameraUploadFolderName;
  115. else return k_folderDefaultCameraUpload;
  116. } else return @"";
  117. }
  118. + (NSString *)getCameraUploadFolderPathActiveAccount:(NSString *)activeAccount activeUrl:(NSString *)activeUrl
  119. {
  120. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  121. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate];
  122. if (record) {
  123. if ([record.cameraUploadFolderPath length] > 0 ) return record.cameraUploadFolderPath;
  124. else return [CCUtility getHomeServerUrlActiveUrl:activeUrl];
  125. } else return @"";
  126. }
  127. + (NSString *)getCameraUploadFolderNamePathActiveAccount:(NSString *)activeAccount activeUrl:(NSString *)activeUrl
  128. {
  129. NSString *cameraFolderName = [self getCameraUploadFolderNameActiveAccount:activeAccount];
  130. NSString *cameraFolderPath = [self getCameraUploadFolderPathActiveAccount:activeAccount activeUrl:activeUrl];
  131. NSString *folderPhotos = [CCUtility stringAppendServerUrl:cameraFolderPath addFileName:cameraFolderName];
  132. return folderPhotos;
  133. }
  134. + (BOOL)getCameraUploadActiveAccount:(NSString *)activeAccount
  135. {
  136. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  137. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate];
  138. if (record) return [record.cameraUpload boolValue];
  139. else return NO;
  140. }
  141. + (BOOL)getCameraUploadBackgroundActiveAccount:(NSString *)activeAccount
  142. {
  143. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  144. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate];
  145. if (record) return [record.cameraUploadBackground boolValue];
  146. else return NO;
  147. }
  148. + (BOOL)getCameraUploadCreateSubfolderActiveAccount:(NSString *)activeAccount
  149. {
  150. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  151. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate];
  152. if (record) return [record.cameraUploadCreateSubfolder boolValue];
  153. else return NO;
  154. }
  155. + (BOOL)getCameraUploadFullPhotosActiveAccount:(NSString *)activeAccount
  156. {
  157. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  158. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate];
  159. if (record) return [record.cameraUploadFull boolValue];
  160. else return NO;
  161. }
  162. + (BOOL)getCameraUploadPhotoActiveAccount:(NSString *)activeAccount
  163. {
  164. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  165. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate];
  166. if (record) return [record.cameraUploadPhoto boolValue];
  167. else return NO;
  168. }
  169. + (BOOL)getCameraUploadVideoActiveAccount:(NSString *)activeAccount
  170. {
  171. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  172. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate];
  173. if (record) return [record.cameraUploadVideo boolValue];
  174. else return NO;
  175. }
  176. + (NSDate *)getCameraUploadDatePhotoActiveAccount:(NSString *)activeAccount
  177. {
  178. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  179. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate];
  180. if (record) return record.cameraUploadDatePhoto;
  181. else return nil;
  182. }
  183. + (NSDate *)getCameraUploadDateVideoActiveAccount:(NSString *)activeAccount
  184. {
  185. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  186. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate];
  187. if (record) return record.cameraUploadDateVideo;
  188. else return nil;
  189. }
  190. + (BOOL)getCameraUploadCryptatedPhotoActiveAccount:(NSString *)activeAccount
  191. {
  192. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  193. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate];
  194. if (record) return [record.cameraUploadCryptatedPhoto boolValue];
  195. else return NO;
  196. }
  197. + (BOOL)getCameraUploadCryptatedVideoActiveAccount:(NSString *)activeAccount
  198. {
  199. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  200. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate];
  201. if (record) return [record.cameraUploadCryptatedVideo boolValue];
  202. else return NO;
  203. }
  204. + (BOOL)getCameraUploadWWanPhotoActiveAccount:(NSString *)activeAccount
  205. {
  206. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  207. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate];
  208. if (record) return [record.cameraUploadWWAnPhoto boolValue];
  209. else return NO;
  210. }
  211. + (BOOL)getCameraUploadWWanVideoActiveAccount:(NSString *)activeAccount
  212. {
  213. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  214. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate];
  215. if (record) return [record.cameraUploadWWAnVideo boolValue];
  216. else return NO;
  217. }
  218. + (float)getServerVersionActiveAccount:(NSString *)activeAccount
  219. {
  220. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  221. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate];
  222. if (record) {
  223. NSInteger versionMajor = [record.versionMajor integerValue];
  224. NSInteger versionMinor = [record.versionMinor integerValue];
  225. return versionMajor + versionMinor/100;
  226. } else
  227. return 0.0;
  228. }
  229. + (void)setCameraUpload:(BOOL)state activeAccount:(NSString *)activeAccount
  230. {
  231. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  232. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  233. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate inContext:localContext];
  234. if (record)
  235. record.cameraUpload = [NSNumber numberWithBool:state];
  236. }];
  237. }
  238. + (void)setCameraUploadBackground:(BOOL)state activeAccount:(NSString *)activeAccount
  239. {
  240. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  241. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  242. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate inContext:localContext];
  243. if (record)
  244. record.cameraUploadBackground = [NSNumber numberWithBool:state];
  245. }];
  246. }
  247. + (void)setCameraUploadCreateSubfolderActiveAccount:(BOOL)state activeAccount:(NSString *)activeAccount
  248. {
  249. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  250. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  251. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate inContext:localContext];
  252. if (record)
  253. record.cameraUploadCreateSubfolder = [NSNumber numberWithBool:state];
  254. }];
  255. }
  256. + (void)setCameraUploadFullPhotosActiveAccount:(BOOL)state activeAccount:(NSString *)activeAccount
  257. {
  258. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  259. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  260. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate inContext:localContext];
  261. if (record)
  262. record.cameraUploadFull = [NSNumber numberWithBool:state];
  263. }];
  264. }
  265. + (void)setCameraUploadPhoto:(BOOL)state activeAccount:(NSString *)activeAccount
  266. {
  267. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  268. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  269. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate inContext:localContext];
  270. if (record)
  271. record.cameraUploadPhoto = [NSNumber numberWithBool:state];
  272. }];
  273. }
  274. + (void)setCameraUploadVideo:(BOOL)video activeAccount:(NSString *)activeAccount
  275. {
  276. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  277. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  278. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate inContext:localContext];
  279. if (record)
  280. record.cameraUploadVideo = [NSNumber numberWithBool:video];
  281. }];
  282. }
  283. + (void)setCameraUploadDatePhoto:(NSDate *)date
  284. {
  285. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  286. NSArray *records = [TableAccount MR_findAllInContext:context];
  287. for (TableAccount *record in records)
  288. record.cameraUploadDatePhoto = date;
  289. [context MR_saveToPersistentStoreAndWait];
  290. }
  291. + (void)setCameraUploadDateVideo:(NSDate *)date
  292. {
  293. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  294. NSArray *records = [TableAccount MR_findAllInContext:context];
  295. for (TableAccount *record in records)
  296. record.cameraUploadDateVideo = date;
  297. [context MR_saveToPersistentStoreAndWait];
  298. }
  299. + (void)setCameraUploadDateAssetType:(PHAssetMediaType)assetMediaType assetDate:(NSDate *)assetDate activeAccount:(NSString *)activeAccount
  300. {
  301. if (assetMediaType == PHAssetMediaTypeImage && [assetDate compare:[self getCameraUploadDatePhotoActiveAccount:activeAccount]] == NSOrderedDescending && assetDate) {
  302. [self setCameraUploadDatePhoto:assetDate];
  303. }
  304. if (assetMediaType == PHAssetMediaTypeVideo && [assetDate compare:[self getCameraUploadDateVideoActiveAccount:activeAccount]] == NSOrderedDescending && assetDate) {
  305. [self setCameraUploadDateVideo:assetDate];
  306. }
  307. }
  308. + (void)setCameraUploadCryptatedPhoto:(BOOL)cryptated activeAccount:(NSString *)activeAccount
  309. {
  310. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  311. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  312. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate inContext:localContext];
  313. if (record)
  314. record.cameraUploadCryptatedPhoto = [NSNumber numberWithBool:cryptated];
  315. }];
  316. }
  317. + (void)setCameraUploadCryptatedVideo:(BOOL)cryptated activeAccount:(NSString *)activeAccount
  318. {
  319. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  320. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  321. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate inContext:localContext];
  322. if (record)
  323. record.cameraUploadCryptatedVideo = [NSNumber numberWithBool:cryptated];
  324. }];
  325. }
  326. + (void)setCameraUploadWWanPhoto:(BOOL)wWan activeAccount:(NSString *)activeAccount
  327. {
  328. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  329. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  330. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate inContext:localContext];
  331. if (record)
  332. record.cameraUploadWWAnPhoto = [NSNumber numberWithBool:wWan];
  333. }];
  334. }
  335. + (void)setCameraUploadWWanVideo:(BOOL)wWan activeAccount:(NSString *)activeAccount
  336. {
  337. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  338. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  339. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate inContext:localContext];
  340. if (record)
  341. record.cameraUploadWWAnVideo = [NSNumber numberWithBool:wWan];
  342. }];
  343. }
  344. + (void)setCameraUploadFolderName:(NSString *)fileName activeAccount:(NSString *)activeAccount
  345. {
  346. if (fileName == nil)
  347. fileName = [self getCameraUploadFolderNameActiveAccount:activeAccount];
  348. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  349. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  350. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate inContext:localContext];
  351. if (record)
  352. record.cameraUploadFolderName = fileName;
  353. }];
  354. }
  355. + (void)setCameraUploadFolderPath:(NSString *)pathName activeUrl:(NSString *)activeUrl activeAccount:(NSString *)activeAccount
  356. {
  357. if (pathName == nil)
  358. pathName = [self getCameraUploadFolderPathActiveAccount:activeAccount activeUrl:activeUrl];
  359. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  360. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  361. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate inContext:localContext];
  362. if (record)
  363. record.cameraUploadFolderPath = pathName;
  364. }];
  365. }
  366. + (void)setCameraUploadSaveAlbum:(BOOL)saveAlbum activeAccount:(NSString *)activeAccount
  367. {
  368. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  369. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  370. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate inContext:localContext];
  371. if (record)
  372. record.cameraUploadSaveAlbum = [NSNumber numberWithBool:saveAlbum];
  373. }];
  374. }
  375. + (void)setUserProfileActiveAccount:(NSString *)activeAccount userProfile:(OCUserProfile *)userProfile
  376. {
  377. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  378. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  379. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate inContext:localContext];
  380. if (record) {
  381. record.enabled = [NSNumber numberWithBool:userProfile.enabled];
  382. record.address = userProfile.address;
  383. record.displayName = userProfile.displayName;
  384. record.email = userProfile.email;
  385. record.phone = userProfile.phone;
  386. record.twitter = userProfile.twitter;
  387. record.webpage = userProfile.webpage;
  388. record.quota = [NSNumber numberWithDouble:userProfile.quota];
  389. record.quotaFree = [NSNumber numberWithDouble:userProfile.quotaFree];
  390. record.quotaRelative = [NSNumber numberWithDouble:userProfile.quotaRelative];
  391. record.quotaTotal = [NSNumber numberWithDouble:userProfile.quotaTotal];
  392. record.quotaUsed = [NSNumber numberWithDouble:userProfile.quotaUsed];
  393. }
  394. }];
  395. }
  396. + (void)setServerVersionActiveAccount:(NSString *)activeAccount versionMajor:(NSInteger)versionMajor versionMinor:(NSInteger)versionMinor versionMicro:(NSInteger)versionMicro
  397. {
  398. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  399. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  400. TableAccount *record = [TableAccount MR_findFirstWithPredicate:predicate inContext:localContext];
  401. if (record) {
  402. record.versionMajor = [NSNumber numberWithInteger:versionMajor];
  403. record.versionMinor = [NSNumber numberWithInteger:versionMinor];
  404. record.versionMicro = [NSNumber numberWithInteger:versionMicro];
  405. }
  406. }];
  407. }
  408. #pragma --------------------------------------------------------------------------------------------
  409. #pragma mark ===== Certificates =====
  410. #pragma --------------------------------------------------------------------------------------------
  411. + (void)addCertificate:(NSString *)certificateLocation
  412. {
  413. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  414. TableCertificates *record = [TableCertificates MR_createEntityInContext:context];
  415. record.certificateLocation = certificateLocation;
  416. [context MR_saveToPersistentStoreAndWait];
  417. }
  418. + (NSMutableArray *)getAllCertificatesLocation
  419. {
  420. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  421. NSString *localCertificatesFolder = [CCUtility getDirectoryCerificates];
  422. NSMutableArray *output = [NSMutableArray new];
  423. NSArray *records = [TableCertificates MR_findAllInContext:context];
  424. for (TableCertificates *record in records) {
  425. NSString *certificatePath = [NSString stringWithFormat:@"%@%@", localCertificatesFolder, record.certificateLocation];
  426. [output addObject:certificatePath];
  427. }
  428. return output;
  429. }
  430. #pragma --------------------------------------------------------------------------------------------
  431. #pragma mark ===== Metadata =====
  432. #pragma --------------------------------------------------------------------------------------------
  433. + (void)addMetadata:(CCMetadata *)metadata activeAccount:(NSString *)activeAccount activeUrl:(NSString *)activeUrl context:(NSManagedObjectContext *)context
  434. {
  435. if (context == nil)
  436. context = [NSManagedObjectContext MR_context];
  437. // remove all fileID (BUG 2.10)
  438. [TableMetadata MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", activeAccount, metadata.fileID] inContext:context];
  439. [context MR_saveToPersistentStoreAndWait];
  440. // remove record if exists
  441. [TableMetadata MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileName == %@) AND (directoryID == %@)", activeAccount, metadata.fileName, metadata.directoryID] inContext:context];
  442. [context MR_saveToPersistentStoreAndWait];
  443. // create new record Metadata
  444. TableMetadata *record = [TableMetadata MR_createEntityInContext:context];
  445. // set default value
  446. metadata.sessionTaskIdentifier = k_taskIdentifierDone;
  447. metadata.sessionTaskIdentifierPlist = k_taskIdentifierDone;
  448. // Insert metdata -> entity
  449. [self insertMetadataInEntity:metadata recordMetadata:record activeAccount:activeAccount activeUrl:activeUrl];
  450. // Aggiorniamo la data nella directory (ottimizzazione v 2.10)
  451. [self setDateReadDirectoryID:metadata.directoryID activeAccount:activeAccount];
  452. [context MR_saveToPersistentStoreAndWait];
  453. }
  454. + (void)deleteMetadataWithPredicate:(NSPredicate *)predicate
  455. {
  456. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  457. NSString *directoryID;
  458. NSArray *records = [TableMetadata MR_findAllWithPredicate:predicate inContext:localContext];
  459. for(TableMetadata *record in records) {
  460. // Aggiorniamo la data nella directory (ottimizzazione v 2.10)
  461. if ([directoryID isEqualToString:record.directoryID] == NO)
  462. [self setDateReadDirectoryID:record.directoryID activeAccount:record.account];
  463. directoryID = record.directoryID;
  464. [record MR_deleteEntityInContext:localContext];
  465. }
  466. }];
  467. }
  468. + (void)moveMetadata:(NSString *)fileName directoryID:(NSString *)directoryID directoryIDTo:(NSString *)directoryIDTo activeAccount:(NSString *)activeAccount
  469. {
  470. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  471. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileName == %@) AND (directoryID == %@)", activeAccount, fileName, directoryID];
  472. TableMetadata *record = [TableMetadata MR_findFirstWithPredicate:predicate inContext:localContext];
  473. if (record) {
  474. record.directoryID = directoryIDTo;
  475. // Aggiorniamo la data nella directory (ottimizzazione v 2.10)
  476. [self setDateReadDirectoryID:directoryID activeAccount:activeAccount];
  477. [self setDateReadDirectoryID:directoryIDTo activeAccount:activeAccount];
  478. }
  479. }];
  480. }
  481. + (void)updateMetadata:(CCMetadata *)metadata predicate:(NSPredicate *)predicate activeAccount:(NSString *)activeAccount activeUrl:(NSString *)activeUrl context:(NSManagedObjectContext *)context
  482. {
  483. TableMetadata *record;
  484. if (context == nil)
  485. context = [NSManagedObjectContext MR_defaultContext];
  486. record = [TableMetadata MR_findFirstWithPredicate:predicate inContext:context];
  487. if (record) {
  488. [self insertMetadataInEntity:metadata recordMetadata:record activeAccount:activeAccount activeUrl:activeUrl];
  489. // Aggiorniamo la data nella directory (ottimizzazione v 2.10)
  490. [self setDateReadDirectoryID:metadata.directoryID activeAccount:activeAccount];
  491. [context MR_saveToPersistentStoreAndWait];
  492. }
  493. }
  494. + (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
  495. {
  496. if (context == nil)
  497. context = [NSManagedObjectContext MR_defaultContext];
  498. NSArray *records = [TableMetadata MR_findAllWithPredicate:predicate inContext:context];
  499. NSMutableSet *directoryIDs = [[NSMutableSet alloc] init];
  500. NSString *directoryID;
  501. for(TableMetadata *record in records) {
  502. if (session) record.session = session;
  503. if (sessionError) record.sessionError = sessionError;
  504. if (sessionSelector) record.sessionSelector = sessionSelector;
  505. if (sessionSelectorPost) record.sessionSelectorPost = sessionSelectorPost;
  506. if (sessionTaskIdentifier != k_taskIdentifierNULL) record.sessionTaskIdentifier = [NSNumber numberWithInteger:sessionTaskIdentifier];
  507. if (sessionTaskIdentifierPlist != k_taskIdentifierNULL) record.sessionTaskIdentifierPlist = [NSNumber numberWithInteger:sessionTaskIdentifierPlist];
  508. [directoryIDs addObject:record.directoryID];
  509. // Aggiorniamo la data nella directory (ottimizzazione v 2.10)
  510. if ([directoryID isEqualToString:record.directoryID] == NO)
  511. [self setDateReadDirectoryID:record.directoryID activeAccount:record.account];
  512. directoryID = record.directoryID;
  513. }
  514. [context MR_saveToPersistentStoreAndWait];
  515. }
  516. + (void)setMetadataFavoriteFileID:(NSString *)fileID favorite:(BOOL)favorite activeAccount:(NSString *)activeAccount context:(NSManagedObjectContext *)context
  517. {
  518. if (context == nil)
  519. context = [NSManagedObjectContext MR_defaultContext];
  520. TableMetadata *tableMetadata = [TableMetadata MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", activeAccount, fileID] inContext:context];
  521. if (tableMetadata) {
  522. tableMetadata.favorite = [NSNumber numberWithBool:favorite];
  523. // Aggiorniamo la data nella directory (ottimizzazione v 2.10)
  524. [self setDateReadDirectoryID:tableMetadata.directoryID activeAccount:activeAccount];
  525. [context MR_saveToPersistentStoreAndWait];
  526. }
  527. }
  528. + (CCMetadata *)getMetadataWithPreficate:(NSPredicate *)predicate context:(NSManagedObjectContext *)context
  529. {
  530. if (context == nil)
  531. context = [NSManagedObjectContext MR_defaultContext];
  532. TableMetadata *record;
  533. record = [TableMetadata MR_findFirstWithPredicate:predicate inContext:context];
  534. if (record) {
  535. return [self insertEntityInMetadata:record];
  536. } else return nil;
  537. }
  538. + (TableMetadata *)getTableMetadataWithPreficate:(NSPredicate *)predicate
  539. {
  540. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  541. return [TableMetadata MR_findFirstWithPredicate:predicate inContext:context];
  542. }
  543. + (NSArray *)getTableMetadataWithPredicate:(NSPredicate *)predicate context:(NSManagedObjectContext *)context
  544. {
  545. if (context == nil)
  546. context = [NSManagedObjectContext MR_defaultContext];
  547. return [TableMetadata MR_findAllWithPredicate:predicate inContext:context];
  548. }
  549. + (NSArray *)getTableMetadataWithPredicate:(NSPredicate *)predicate fieldOrder:(NSString *)fieldOrder ascending:(BOOL)ascending
  550. {
  551. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  552. NSArray *records = [[NSArray alloc] init];
  553. NSSortDescriptor *descriptor;
  554. records = [TableMetadata MR_findAllWithPredicate:predicate inContext:context];
  555. if ([records count] == 0) return nil;
  556. if ([fieldOrder isEqualToString:@"fileName"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"fileNamePrint" ascending:ascending selector:@selector(localizedCaseInsensitiveCompare:)];
  557. else if ([fieldOrder isEqualToString:@"fileDate"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:ascending selector:nil];
  558. else if ([fieldOrder isEqualToString:@"sessionTaskIdentifier"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"sessionTaskIdentifier" ascending:ascending selector:nil];
  559. else descriptor = [[NSSortDescriptor alloc] initWithKey:fieldOrder ascending:ascending selector:@selector(localizedCaseInsensitiveCompare:)];
  560. return [records sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
  561. }
  562. + (CCMetadata *)getMetadataAtIndex:(NSPredicate *)predicate fieldOrder:(NSString *)fieldOrder ascending:(BOOL)ascending objectAtIndex:(NSUInteger)index
  563. {
  564. NSArray *records = [self getTableMetadataWithPredicate:predicate fieldOrder:fieldOrder ascending:ascending];
  565. TableMetadata *record = [records objectAtIndex:index];
  566. return [self insertEntityInMetadata:record];
  567. }
  568. + (CCMetadata *)getMetadataFromFileName:(NSString *)fileName directoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount context:(NSManagedObjectContext *)context
  569. {
  570. if (fileName == nil || directoryID == nil || activeAccount == nil)
  571. return nil;
  572. if (context == nil)
  573. context = [NSManagedObjectContext MR_defaultContext];
  574. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@) AND ((fileName == %@) OR (fileNameData == %@))", activeAccount, directoryID, fileName, fileName];
  575. TableMetadata *record = [TableMetadata MR_findFirstWithPredicate:predicate inContext:context];
  576. if (record) return [self insertEntityInMetadata:record];
  577. else return nil;
  578. }
  579. + (NSArray *)getTableMetadataDownloadAccount:(NSString *)activeAccount
  580. {
  581. 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];
  582. }
  583. + (NSArray *)getTableMetadataDownloadWWanAccount:(NSString *)activeAccount
  584. {
  585. 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];
  586. }
  587. + (NSArray *)getTableMetadataUploadAccount:(NSString *)activeAccount
  588. {
  589. 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];
  590. }
  591. + (NSArray *)getTableMetadataUploadWWanAccount:(NSString *)activeAccount
  592. {
  593. 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];
  594. }
  595. + (NSArray *)getRecordsTableMetadataPhotosCameraUpload:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  596. {
  597. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  598. NSMutableArray *recordsPhotosCameraUpload = [[NSMutableArray alloc] init];
  599. NSArray *tableDirectoryes = [self getDirectoryIDsFromBeginsWithServerUrl:serverUrl activeAccount:activeAccount];
  600. for (TableDirectory *record in tableDirectoryes) {
  601. 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];
  602. if ([records count] > 0)
  603. [recordsPhotosCameraUpload addObjectsFromArray:records];
  604. }
  605. // test
  606. if ([recordsPhotosCameraUpload count] == 0) return nil;
  607. // Order
  608. NSString *fieldOrder = [CCUtility getOrderSettings];
  609. BOOL ascending = [CCUtility getAscendingSettings];
  610. NSSortDescriptor *descriptor;
  611. if ([fieldOrder isEqualToString:@"fileName"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"fileNamePrint" ascending:ascending selector:@selector(localizedCaseInsensitiveCompare:)];
  612. else if ([fieldOrder isEqualToString:@"fileDate"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:ascending selector:nil];
  613. else if ([fieldOrder isEqualToString:@"sessionTaskIdentifier"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"sessionTaskIdentifier" ascending:ascending selector:nil];
  614. else descriptor = [[NSSortDescriptor alloc] initWithKey:fieldOrder ascending:ascending selector:@selector(localizedCaseInsensitiveCompare:)];
  615. return [recordsPhotosCameraUpload sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
  616. }
  617. + (void)removeOfflineAllFileFromServerUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  618. {
  619. NSString *directoryID = [self getDirectoryIDFromServerUrl:serverUrl activeAccount:activeAccount];
  620. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  621. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", activeAccount, directoryID];
  622. NSArray *records = [TableMetadata MR_findAllWithPredicate:predicate];
  623. for (TableMetadata *record in records)
  624. [self setOfflineLocalFileID:record.fileID offline:NO activeAccount:activeAccount];
  625. }];
  626. }
  627. #pragma --------------------------------------------------------------------------------------------
  628. #pragma mark ===== Directory =====
  629. #pragma --------------------------------------------------------------------------------------------
  630. + (NSString *)addDirectory:(NSString *)serverUrl permissions:(NSString *)permissions activeAccount:(NSString *)activeAccount
  631. {
  632. NSString *directoryID;
  633. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  634. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount] inContext:context];
  635. if (record) {
  636. directoryID = record.directoryID;
  637. if (permissions) record.permissions = permissions;
  638. } else {
  639. TableDirectory *record = [TableDirectory MR_createEntityInContext:context];
  640. record.account = activeAccount;
  641. record.directoryID = [CCUtility createID];
  642. directoryID = record.directoryID;
  643. if (permissions) record.permissions = permissions;
  644. record.serverUrl = serverUrl;
  645. }
  646. [context MR_saveToPersistentStoreAndWait];
  647. return directoryID;
  648. }
  649. + (void)updateDirectoryEtagServerUrl:(NSString *)serverUrl etag:(NSString *)etag activeAccount:(NSString *)activeAccount
  650. {
  651. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  652. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  653. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:localContext];
  654. if (record)
  655. record.rev = etag;
  656. }];
  657. }
  658. + (void)deleteDirectoryFromPredicate:(NSPredicate *)predicate
  659. {
  660. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  661. [TableDirectory MR_deleteAllMatchingPredicate:predicate inContext:localContext];
  662. }];
  663. }
  664. + (NSArray *)deleteDirectoryAndSubDirectory:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  665. {
  666. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  667. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  668. NSMutableArray *directoryIDs = [[NSMutableArray alloc] init];
  669. NSArray *tableDirectorys = [TableDirectory MR_findAllWithPredicate:predicate inContext:context];
  670. for(TableDirectory *recordDirectory in tableDirectorys) {
  671. NSLog(@"[LOG] %@", recordDirectory.serverUrl);
  672. if ([recordDirectory.serverUrl hasPrefix:serverUrl]) {
  673. // List directoryIDs removed
  674. [directoryIDs addObject:recordDirectory.directoryID];
  675. // remove all TableMetadata
  676. NSLog(@"[LOG] %@", recordDirectory.directoryID);
  677. // remove directory in Metadata come cazzo si fa a saperlo
  678. //[TableMetadata MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", activeAccount, recordDirectory.directoryID] inContext:context];
  679. NSArray *tableMetadatas = [TableMetadata MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", activeAccount, recordDirectory.directoryID] inContext:context];
  680. for(TableMetadata *recordMetadata in tableMetadatas) {
  681. // remove if in session
  682. if ([recordMetadata.session length] >0) {
  683. if (recordMetadata.sessionTaskIdentifier >= 0)
  684. [[CCNetworking sharedNetworking] settingSession:recordMetadata.session sessionTaskIdentifier:[recordMetadata.sessionTaskIdentifier integerValue] taskStatus: k_taskStatusCancel];
  685. if (recordMetadata.sessionTaskIdentifierPlist >= 0)
  686. [[CCNetworking sharedNetworking] settingSession:recordMetadata.session sessionTaskIdentifier:[recordMetadata.sessionTaskIdentifierPlist integerValue] taskStatus: k_taskStatusCancel];
  687. }
  688. // remove file local
  689. NSLog(@"[LOG] %@", recordMetadata.fileID);
  690. [self deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", activeAccount, recordMetadata.fileID]];
  691. [recordMetadata MR_deleteEntityInContext:context];
  692. }
  693. [recordDirectory MR_deleteEntityInContext:context];
  694. }
  695. }
  696. [context MR_saveToPersistentStoreAndWait];
  697. return directoryIDs;
  698. }
  699. + (void)renameDirectory:(NSString *)serverUrl serverUrlTo:(NSString *)serverUrlTo activeAccount:(NSString *)activeAccount
  700. {
  701. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  702. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  703. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:localContext];
  704. if (record)
  705. record.serverUrl = serverUrlTo;
  706. }];
  707. }
  708. + (void)setDateReadDirectoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount
  709. {
  710. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  711. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", directoryID, activeAccount];
  712. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  713. if (record) {
  714. record.dateReadDirectory = [NSDate date];
  715. [context MR_saveToPersistentStoreAndWait];
  716. }
  717. }
  718. + (TableDirectory *)getTableDirectoryWithPreficate:(NSPredicate *)predicate
  719. {
  720. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  721. return [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  722. }
  723. + (NSDate *)getDateReadDirectoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount
  724. {
  725. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  726. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", directoryID, activeAccount];
  727. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  728. if (record) return record.dateReadDirectory;
  729. else return nil;
  730. }
  731. + (void)setDirectoryRev:(NSString *)rev serverUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  732. {
  733. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  734. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  735. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  736. if (record) {
  737. record.rev = rev;
  738. [context MR_saveToPersistentStoreAndWait];
  739. }
  740. }
  741. + (NSString *)getDirectoryRevFromServerUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  742. {
  743. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  744. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  745. if (record) return record.rev;
  746. else return nil;
  747. }
  748. + (NSArray *)getDirectoryIDsFromBeginsWithServerUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  749. {
  750. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl BEGINSWITH %@) AND (account == %@)", serverUrl, activeAccount];
  751. return [TableDirectory MR_findAllWithPredicate:predicate];
  752. }
  753. + (NSString *)getDirectoryIDFromServerUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  754. {
  755. if (serverUrl == nil) return nil;
  756. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  757. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  758. if (record) return record.directoryID;
  759. else {
  760. return [self addDirectory:serverUrl permissions:nil activeAccount:activeAccount];
  761. }
  762. return nil;
  763. }
  764. + (NSString *)getServerUrlFromDirectoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount
  765. {
  766. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", directoryID, activeAccount];
  767. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  768. if (record) return record.serverUrl;
  769. else return nil;
  770. }
  771. + (void)clearDateReadDirectory:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  772. {
  773. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  774. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  775. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:localContext];
  776. if (record) {
  777. record.dateReadDirectory = NULL;
  778. record.rev = @"";
  779. }
  780. }];
  781. }
  782. + (void)clearAllDateReadDirectory
  783. {
  784. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  785. NSArray *records = [TableDirectory MR_findAllInContext:localContext];
  786. for (TableDirectory *record in records) {
  787. record.dateReadDirectory = NULL;
  788. record.rev = @"";
  789. }
  790. }];
  791. }
  792. + (BOOL)isDirectoryOutOfDate:(int)numAddDay directoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount
  793. {
  794. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", activeAccount, directoryID];
  795. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  796. if (record == nil || record.dateReadDirectory == nil) {
  797. return YES;
  798. }
  799. NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
  800. [dateComponents setWeekday:numAddDay];
  801. NSCalendar *calendar = [NSCalendar currentCalendar];
  802. NSDate *datePlus = [calendar dateByAddingComponents:dateComponents toDate:record.dateReadDirectory options:0];
  803. NSDate *now = [NSDate date];
  804. // usa la Cache se richiesto e se la data è entro X giorni dall'ultima volta che l'hai letta.
  805. if ([now compare:datePlus] == NSOrderedDescending) {
  806. return YES;
  807. }
  808. return NO;
  809. }
  810. #pragma --------------------------------------------------------------------------------------------
  811. #pragma mark ===== Offline Directory =====
  812. #pragma --------------------------------------------------------------------------------------------
  813. + (void)removeOfflineDirectoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount
  814. {
  815. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  816. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@) AND (offline == 1)", directoryID, activeAccount];
  817. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:localContext];
  818. if (record)
  819. record.offline = [NSNumber numberWithBool:FALSE];
  820. }];
  821. }
  822. + (NSArray *)getOfflineDirectoryActiveAccount:(NSString *)activeAccount
  823. {
  824. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (offline == 1)", activeAccount];
  825. NSArray *recordsTable = [TableDirectory MR_findAllWithPredicate:predicate];
  826. // Order by serverUrl
  827. NSArray *sortedRecordsTable = [recordsTable sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  828. TableDirectory *record1 = obj1, *record2 = obj2;
  829. return [record1.serverUrl compare:record2.serverUrl];
  830. }];
  831. return sortedRecordsTable;
  832. }
  833. + (void)setOfflineDirectoryServerUrl:(NSString *)serverUrl offline:(BOOL)offline activeAccount:(NSString *)activeAccount
  834. {
  835. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  836. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  837. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:localContext];
  838. if (record)
  839. record.offline = [NSNumber numberWithBool:offline];
  840. }];
  841. }
  842. + (BOOL)isOfflineDirectoryServerUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  843. {
  844. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (offline == 1) AND (account == %@)", serverUrl, activeAccount];
  845. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  846. if (record) return YES;
  847. else return NO;
  848. }
  849. #pragma --------------------------------------------------------------------------------------------
  850. #pragma mark ===== Directory Lock =====
  851. #pragma --------------------------------------------------------------------------------------------
  852. + (BOOL)setDirectoryLock:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  853. {
  854. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  855. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", [self getDirectoryIDFromServerUrl:serverUrl activeAccount:activeAccount], activeAccount];
  856. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  857. if (record) {
  858. record.lock = [NSNumber numberWithBool:YES];
  859. [context MR_saveToPersistentStoreAndWait];
  860. return YES;
  861. }
  862. else return NO;
  863. }
  864. + (BOOL)setDirectoryUnLock:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  865. {
  866. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  867. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", [self getDirectoryIDFromServerUrl:serverUrl activeAccount:activeAccount], activeAccount];
  868. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  869. if (record) {
  870. record.lock = [NSNumber numberWithBool:NO];
  871. [context MR_saveToPersistentStoreAndWait];
  872. return YES;
  873. }
  874. else return NO;
  875. }
  876. + (void)setAllDirectoryUnLockForAccount:(NSString *)activeAccount
  877. {
  878. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  879. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  880. NSArray *records = [TableDirectory MR_findAllWithPredicate:predicate inContext:localContext];
  881. for(TableDirectory *record in records)
  882. record.lock = [NSNumber numberWithBool:NO];
  883. }];
  884. }
  885. + (BOOL)isDirectoryLock:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  886. {
  887. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (lock == 1) AND (account == %@)", [self getDirectoryIDFromServerUrl:serverUrl activeAccount:activeAccount], activeAccount];
  888. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  889. if (record) return YES;
  890. else return NO;
  891. }
  892. + (BOOL)isBlockZone:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  893. {
  894. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (directory == 1)", activeAccount];
  895. NSArray *records = [TableMetadata MR_findAllWithPredicate:predicate];
  896. if ([records count] > 0) {
  897. NSArray *pathComponents = [serverUrl pathComponents];
  898. for (NSString *fileName in pathComponents) {
  899. for(TableMetadata *record in records){
  900. NSString *fileNameEntity = [CCUtility trasformedFileNamePlistInCrypto:record.fileName];
  901. NSString *directoryID = record.directoryID;
  902. NSString *serverUrlEntity = [self getServerUrlFromDirectoryID:directoryID activeAccount:activeAccount];
  903. if([fileName isEqualToString:fileNameEntity]) {
  904. NSString *lockServerUrl = [CCUtility stringAppendServerUrl:serverUrlEntity addFileName:fileNameEntity];
  905. BOOL risultato = [self isDirectoryLock:lockServerUrl activeAccount:activeAccount];
  906. if (risultato) return YES;
  907. }
  908. }
  909. }
  910. }
  911. return NO;
  912. }
  913. #pragma --------------------------------------------------------------------------------------------
  914. #pragma mark ===== LocalFile =====
  915. #pragma --------------------------------------------------------------------------------------------
  916. + (void)addLocalFile:(CCMetadata *)metadata activeAccount:(NSString *)activeAccount
  917. {
  918. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  919. BOOL offline = NO;
  920. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", activeAccount, metadata.fileID];
  921. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  922. if (record) {
  923. offline = [[record valueForKey:@"offline"] boolValue];
  924. [record MR_deleteEntityInContext:localContext];
  925. }
  926. record = [TableLocalFile MR_createEntityInContext:localContext];
  927. record.account = activeAccount;
  928. record.date = metadata.date;
  929. record.fileID = metadata.fileID;
  930. record.exifDate = [NSDate date];
  931. record.exifLatitude = @"-1";
  932. record.exifLongitude = @"-1";
  933. record.offline = [NSNumber numberWithBool:offline];
  934. record.fileName = metadata.fileName;
  935. record.fileNamePrint = metadata.fileNamePrint;
  936. record.rev = metadata.rev;
  937. record.size = [NSNumber numberWithLong:metadata.size];
  938. }];
  939. }
  940. + (void)deleteLocalFileWithPredicate:(NSPredicate *)predicate
  941. {
  942. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  943. [TableLocalFile MR_deleteAllMatchingPredicate:predicate inContext:localContext];
  944. }];
  945. }
  946. + (void)renameLocalFileWithFileID:(NSString *)fileID fileNameTo:(NSString *)fileNameTo fileNamePrintTo:(NSString *)fileNamePrintTo activeAccount:(NSString *)activeAccount
  947. {
  948. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  949. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, activeAccount];
  950. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  951. if (record) {
  952. if (fileNameTo)record.fileName = fileNameTo;
  953. if (fileNamePrintTo)record.fileNamePrint = fileNamePrintTo;
  954. }
  955. }];
  956. }
  957. + (void)updateLocalFileModel:(CCMetadata *)metadata activeAccount:(NSString *)activeAccount
  958. {
  959. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  960. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileName == %@)", activeAccount, metadata.fileName];
  961. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  962. if (record) {
  963. record.fileID = metadata.fileID;
  964. record.date = metadata.date;
  965. record.fileNamePrint = metadata.fileNamePrint;
  966. } else {
  967. [self addLocalFile:metadata activeAccount:activeAccount];
  968. }
  969. }];
  970. }
  971. + (TableLocalFile *)getLocalFileWithFileID:(NSString *)fileID activeAccount:(NSString *)activeAccount
  972. {
  973. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, activeAccount];
  974. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate];
  975. if (record) {
  976. return record;
  977. } else return nil;
  978. }
  979. + (NSMutableArray *)getTableLocalFileWithPredicate:(NSPredicate *)predicate controlZombie:(BOOL)controlZombie activeAccount:(NSString *)activeAccount directoryUser:(NSString *)directoryUser
  980. {
  981. NSMutableArray *ritorno = [[NSMutableArray alloc] init];
  982. NSArray *records = [TableLocalFile MR_findAllWithPredicate:predicate];
  983. if ([records count] > 0) {
  984. // verifichiamo esistano tutti i file altrimenti rimuoviamo il record
  985. for(TableLocalFile *record in records){
  986. if (controlZombie) {
  987. NSString *fileID = record.fileID;
  988. NSString *FilePathFileID = [NSString stringWithFormat:@"%@/%@", directoryUser, fileID];
  989. NSString *FilePathFileName = [NSString stringWithFormat:@"%@/%@", directoryUser, record.fileName];
  990. if (![[NSFileManager defaultManager] fileExistsAtPath:FilePathFileID] && ![[NSFileManager defaultManager] fileExistsAtPath:FilePathFileName] && controlZombie) {
  991. // non esiste nè il file fileID e nemmeno il plist, eliminiamolo.
  992. [self deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, activeAccount]];
  993. } else [ritorno addObject:record];
  994. } else [ritorno addObject:record];
  995. }
  996. } else return nil;
  997. if ([ritorno count] == 0) return nil;
  998. else return ritorno;
  999. }
  1000. + (NSArray *)getTableLocalFileWithPredicate:(NSPredicate *)predicate
  1001. {
  1002. return [TableLocalFile MR_findAllWithPredicate:predicate];
  1003. }
  1004. #pragma --------------------------------------------------------------------------------------------
  1005. #pragma mark ===== Offline LocalFile =====
  1006. #pragma --------------------------------------------------------------------------------------------
  1007. + (void)setOfflineLocalFileID:(NSString *)fileID offline:(BOOL)offline activeAccount:(NSString *)activeAccount
  1008. {
  1009. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  1010. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, activeAccount];
  1011. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  1012. if (record)
  1013. record.offline = [NSNumber numberWithBool:offline];
  1014. }];
  1015. }
  1016. + (BOOL)isOfflineLocalFileID:(NSString *)fileID activeAccount:(NSString *)activeAccount
  1017. {
  1018. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (offline == 1) AND (account == %@)", fileID, activeAccount];
  1019. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate];
  1020. if (record) return YES;
  1021. else return NO;
  1022. }
  1023. + (NSArray *)getOfflineLocalFileActiveAccount:(NSString *)activeAccount directoryUser:(NSString *)directoryUser
  1024. {
  1025. NSMutableArray *metadatas = [NSMutableArray new];
  1026. NSArray *files = [self getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (offline == 1)", activeAccount] controlZombie:YES activeAccount:activeAccount directoryUser:directoryUser];
  1027. for (TableLocalFile *file in files) {
  1028. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", file.fileID, activeAccount];
  1029. CCMetadata *metadata = [self getMetadataWithPreficate:predicate context:nil];
  1030. if (metadata) {
  1031. // verify if is not on directory offline
  1032. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (offline == 1) AND (account == %@)", metadata.directoryID, activeAccount];
  1033. TableDirectory *directory = [TableDirectory MR_findFirstWithPredicate:predicate];
  1034. if (!directory)
  1035. [metadatas addObject:metadata];
  1036. }
  1037. }
  1038. return metadatas;
  1039. }
  1040. #pragma --------------------------------------------------------------------------------------------
  1041. #pragma mark ===== GeoInformation =====
  1042. #pragma --------------------------------------------------------------------------------------------
  1043. + (NSArray *)getGeoInformationLocalFromFileID:(NSString *)fileID activeAccount:(NSString *)activeAccount
  1044. {
  1045. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, activeAccount];
  1046. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate];
  1047. if (record) return [[NSArray alloc] initWithObjects:record.exifDate, record.exifLatitude, record.exifLongitude, nil];
  1048. else return nil;
  1049. }
  1050. + (void)setGeoInformationLocalFromFileID:(NSString *)fileID exifDate:(NSDate *)exifDate exifLatitude:(NSString *)exifLatitude exifLongitude:(NSString *)exifLongitude activeAccount:(NSString *)activeAccount
  1051. {
  1052. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  1053. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", activeAccount, fileID];
  1054. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  1055. if (record) {
  1056. record.exifDate = exifDate;
  1057. record.exifLatitude = exifLatitude;
  1058. record.exifLongitude = exifLongitude;
  1059. }
  1060. }];
  1061. }
  1062. + (void)setGeoInformationLocalNull
  1063. {
  1064. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  1065. NSArray *records = [TableLocalFile MR_findAllInContext:localContext];
  1066. for (TableLocalFile *record in records) {
  1067. if ([record.exifLatitude doubleValue] != 0 || [record.exifLongitude doubleValue] != 0) {
  1068. record.exifLatitude = @"9999";
  1069. record.exifLongitude = @"9999";
  1070. }
  1071. }
  1072. }];
  1073. }
  1074. #pragma --------------------------------------------------------------------------------------------
  1075. #pragma mark ===== Automatic Upload =====
  1076. #pragma --------------------------------------------------------------------------------------------
  1077. + (void)addTableAutomaticUpload:(CCMetadataNet *)metadataNet account:(NSString *)account context:(NSManagedObjectContext *)context
  1078. {
  1079. if (context == nil)
  1080. context = [NSManagedObjectContext MR_context];
  1081. // Delete record if exists
  1082. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileName == %@) AND (serverUrl == %@) AND (selector == %@)", account, metadataNet.fileName, metadataNet.serverUrl, metadataNet.selector];
  1083. [TableAutomaticUpload MR_deleteAllMatchingPredicate:predicate inContext:context];
  1084. TableAutomaticUpload *record = [TableAutomaticUpload MR_createEntityInContext:context];
  1085. record.account = account;
  1086. record.identifier = metadataNet.identifier;
  1087. record.date = [NSDate date];
  1088. record.fileName = metadataNet.fileName;
  1089. record.selector = metadataNet.selector;
  1090. record.selectorPost = metadataNet.selectorPost;
  1091. record.serverUrl = metadataNet.serverUrl;
  1092. record.session = metadataNet.session;
  1093. record.priority = [NSNumber numberWithLong:metadataNet.priority];
  1094. [context MR_saveToPersistentStoreAndWait];
  1095. }
  1096. + (CCMetadataNet *)getTableAutomaticUploadForAccount:(NSString *)account selector:(NSString *)selector context:(NSManagedObjectContext *)context
  1097. {
  1098. if (context == nil)
  1099. context = [NSManagedObjectContext MR_context];
  1100. TableAutomaticUpload *record = [TableAutomaticUpload MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (selector == %@)", account, selector] inContext:context];
  1101. if (record) {
  1102. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] init];
  1103. metadataNet.action = actionUploadAsset; // Default
  1104. metadataNet.identifier = record.identifier;
  1105. metadataNet.fileName = record.fileName;
  1106. metadataNet.priority = [record.priority longValue];
  1107. metadataNet.selector = record.selector;
  1108. metadataNet.selectorPost = record.selectorPost;
  1109. metadataNet.serverUrl = record.serverUrl;
  1110. metadataNet.session = record.session;
  1111. metadataNet.taskStatus = k_taskStatusResume; // Default
  1112. [record MR_deleteEntityInContext:context]; // Remove record
  1113. [context MR_saveToPersistentStoreAndWait];
  1114. return metadataNet;
  1115. }
  1116. return nil;
  1117. }
  1118. + (NSUInteger)countTableAutomaticUploadForAccount:(NSString *)account selector:(NSString *)selector
  1119. {
  1120. if (selector)
  1121. return [TableAutomaticUpload MR_countOfEntitiesWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (selector == %@)", account, selector]];
  1122. else
  1123. return [TableAutomaticUpload MR_countOfEntitiesWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", account]];
  1124. }
  1125. #pragma --------------------------------------------------------------------------------------------
  1126. #pragma mark ===== GPS =====
  1127. #pragma --------------------------------------------------------------------------------------------
  1128. + (void)setGeocoderLocation:(NSString *)location placemarkAdministrativeArea:(NSString *)placemarkAdministrativeArea placemarkCountry:(NSString *)placemarkCountry placemarkLocality:(NSString *)placemarkLocality placemarkPostalCode:(NSString *)placemarkPostalCode placemarkThoroughfare:(NSString *)placemarkThoroughfare latitude:(NSString *)latitude longitude:(NSString *)longitude
  1129. {
  1130. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1131. TableGPS *record = [TableGPS MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(latitude == %@) AND (longitude == %@)", latitude, longitude] inContext:context];
  1132. if (!record) {
  1133. record = [TableGPS MR_createEntityInContext:context];
  1134. }
  1135. record.latitude = latitude;
  1136. record.longitude = longitude;
  1137. if (location) record.location = location;
  1138. if (placemarkAdministrativeArea) record.placemarkAdministrativeArea = placemarkAdministrativeArea;
  1139. if (placemarkCountry) record.placemarkCountry = placemarkCountry;
  1140. if (placemarkLocality) record.placemarkLocality = placemarkLocality;
  1141. if (placemarkPostalCode) record.placemarkPostalCode = placemarkPostalCode;
  1142. if (placemarkThoroughfare) record.placemarkThoroughfare = placemarkThoroughfare;
  1143. [context MR_saveToPersistentStoreAndWait];
  1144. }
  1145. + (NSString *)getLocationFromGeoLatitude:(NSString *)latitude longitude:(NSString *)longitude
  1146. {
  1147. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1148. TableGPS *record = [TableGPS MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(latitude == %@) AND (longitude == %@)", latitude, longitude] inContext:context];
  1149. if (record) return record.location;
  1150. else return nil;
  1151. }
  1152. #pragma --------------------------------------------------------------------------------------------
  1153. #pragma mark ===== Share =====
  1154. #pragma --------------------------------------------------------------------------------------------
  1155. + (void)setShareLink:(NSString *)share fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl sharesLink:(NSMutableDictionary *)sharesLink activeAccount:(NSString *)activeAccount
  1156. {
  1157. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1158. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileName == %@) AND (serverUrl == %@)", activeAccount, fileName, serverUrl];
  1159. TableShare *record = [TableShare MR_findFirstWithPredicate:predicate inContext:context];
  1160. if (record) {
  1161. record.shareLink = share;
  1162. } else {
  1163. TableShare *record = [TableShare MR_createEntityInContext:context];
  1164. record.account = activeAccount;
  1165. record.fileName = fileName;
  1166. record.serverUrl = serverUrl;
  1167. record.shareLink = share;
  1168. }
  1169. [context MR_saveToPersistentStoreAndWait];
  1170. if (share && serverUrl && fileName)
  1171. [sharesLink setObject:share forKey:[serverUrl stringByAppendingString:fileName]];
  1172. }
  1173. + (void)setShareUserAndGroup:(NSString *)share fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup activeAccount:(NSString *)activeAccount
  1174. {
  1175. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1176. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileName == %@) AND (serverUrl == %@)", activeAccount, fileName, serverUrl];
  1177. TableShare *record = [TableShare MR_findFirstWithPredicate:predicate inContext:context];
  1178. if (record) {
  1179. record.shareUserAndGroup = share;
  1180. } else {
  1181. TableShare *record = [TableShare MR_createEntityInContext:context];
  1182. record.account = activeAccount;
  1183. record.fileName = fileName;
  1184. record.serverUrl = serverUrl;
  1185. record.shareUserAndGroup = share;
  1186. }
  1187. [context MR_saveToPersistentStoreAndWait];
  1188. [sharesUserAndGroup setObject:share forKey:[serverUrl stringByAppendingString:fileName]];
  1189. }
  1190. + (void)unShare:(NSString *)share fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl sharesLink:(NSMutableDictionary *)sharesLink sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup activeAccount:(NSString *)activeAccount
  1191. {
  1192. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1193. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND ((shareLink CONTAINS %@) OR (shareUserAndGroup CONTAINS %@))", activeAccount, share, share];
  1194. TableShare *record = [TableShare MR_findFirstWithPredicate:predicate inContext:context];
  1195. if (record) {
  1196. if ([record.shareLink containsString:share]) record.shareLink = @"";
  1197. if ([record.shareUserAndGroup containsString:share]) {
  1198. NSMutableArray *shares = [[NSMutableArray alloc] initWithArray:[record.shareUserAndGroup componentsSeparatedByString:@","]];
  1199. [shares removeObject:share];
  1200. record.shareUserAndGroup = [shares componentsJoinedByString:@","];
  1201. }
  1202. if ([record.shareLink length] == 0 && [record.shareUserAndGroup length] == 0)
  1203. [record MR_deleteEntityInContext:context];
  1204. [context MR_saveToPersistentStoreAndWait];
  1205. if ([record.shareLink length] > 0) [sharesLink setObject:record.shareLink forKey:[serverUrl stringByAppendingString:fileName]];
  1206. else [sharesLink removeObjectForKey:[serverUrl stringByAppendingString:fileName]];
  1207. if ([record.shareUserAndGroup length] > 0) [sharesUserAndGroup setObject:record.shareUserAndGroup forKey:[serverUrl stringByAppendingString:fileName]];
  1208. else [sharesUserAndGroup removeObjectForKey:[serverUrl stringByAppendingString:fileName]];
  1209. }
  1210. }
  1211. + (void)removeAllShareActiveAccount:(NSString *)activeAccount sharesLink:(NSMutableDictionary *)sharesLink sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup
  1212. {
  1213. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1214. [TableShare MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"account == %@", activeAccount] inContext:context];
  1215. [context MR_saveToPersistentStoreAndWait];
  1216. [sharesLink removeAllObjects];
  1217. [sharesUserAndGroup removeAllObjects];
  1218. }
  1219. + (void)updateShare:(NSDictionary *)items sharesLink:(NSMutableDictionary *)sharesLink sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup activeAccount:(NSString *)activeAccount activeUrl:(NSString *)activeUrl
  1220. {
  1221. // rimuovi tutte le condivisioni
  1222. [self removeAllShareActiveAccount:activeAccount sharesLink:sharesLink sharesUserAndGroup:sharesUserAndGroup];
  1223. NSMutableArray *itemsLink = [[NSMutableArray alloc] init];
  1224. NSMutableArray *itemsUsersAndGroups = [[NSMutableArray alloc] init];
  1225. for (NSString *idRemoteShared in items) {
  1226. OCSharedDto *item = [items objectForKey:idRemoteShared];
  1227. if (item.shareType == shareTypeLink) [itemsLink addObject:item];
  1228. if ([[item shareWith] length] > 0 && (item.shareType == shareTypeUser || item.shareType == shareTypeGroup || item.shareType == shareTypeRemote)) [itemsUsersAndGroups addObject:item];
  1229. }
  1230. // Link
  1231. for (OCSharedDto *item in itemsLink) {
  1232. NSString *fullPath = [[CCUtility getHomeServerUrlActiveUrl:activeUrl] stringByAppendingString:item.path];
  1233. NSString *fileName = [fullPath lastPathComponent];
  1234. NSString *serverUrl = [fullPath substringToIndex:([fullPath length]-[fileName length]-1)];
  1235. if ([serverUrl hasSuffix:@"/"]) serverUrl = [serverUrl substringToIndex:[serverUrl length] - 1];
  1236. if ([@(item.idRemoteShared) stringValue])
  1237. [self setShareLink:[@(item.idRemoteShared) stringValue] fileName:fileName serverUrl:serverUrl sharesLink:sharesLink activeAccount:activeAccount];
  1238. }
  1239. // Condivisioni
  1240. NSMutableDictionary *paths = [[NSMutableDictionary alloc] init];
  1241. // Creazione dizionario
  1242. for (OCSharedDto *item in itemsUsersAndGroups) {
  1243. if ([paths objectForKey:item.path]) {
  1244. NSMutableArray *share = [paths objectForKey:item.path];
  1245. [share addObject:[@(item.idRemoteShared) stringValue]];
  1246. [paths setObject:share forKey:item.path];
  1247. } else {
  1248. NSMutableArray *share = [[NSMutableArray alloc] initWithObjects:[@(item.idRemoteShared) stringValue], nil];
  1249. [paths setObject:share forKey:item.path];
  1250. }
  1251. }
  1252. // Scrittura su DB
  1253. for (NSString *path in paths) {
  1254. NSArray *items = [paths objectForKey:path];
  1255. NSString *share = [items componentsJoinedByString:@","];
  1256. NSLog(@"[LOG] share %@", share);
  1257. NSString *fullPath = [[CCUtility getHomeServerUrlActiveUrl:activeUrl] stringByAppendingString:path];
  1258. NSString *fileName = [fullPath lastPathComponent];
  1259. NSString *serverUrl = [fullPath substringToIndex:([fullPath length]-[fileName length]-1)];
  1260. if ([serverUrl hasSuffix:@"/"]) serverUrl = [serverUrl substringToIndex:[serverUrl length] - 1];
  1261. if (share)
  1262. [self setShareUserAndGroup:share fileName:fileName serverUrl:serverUrl sharesUserAndGroup:sharesUserAndGroup activeAccount:activeAccount];
  1263. }
  1264. }
  1265. + (void)populateSharesVariableFromDBActiveAccount:(NSString *)activeAccount sharesLink:(NSMutableDictionary *)sharesLink sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup
  1266. {
  1267. [sharesLink removeAllObjects];
  1268. [sharesUserAndGroup removeAllObjects];
  1269. NSArray *records = [TableShare MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", activeAccount]];
  1270. for (TableShare *record in records) {
  1271. if ([record.shareLink length] > 0 && record.serverUrl && record.fileName)
  1272. [sharesLink setObject:record.shareLink forKey:[record.serverUrl stringByAppendingString:record.fileName]];
  1273. if ([record.shareUserAndGroup length] > 0 && record.serverUrl && record.fileName)
  1274. [sharesUserAndGroup setObject:record.shareUserAndGroup forKey:[record.serverUrl stringByAppendingString:record.fileName]];
  1275. }
  1276. return;
  1277. }
  1278. #pragma --------------------------------------------------------------------------------------------
  1279. #pragma mark ===== Offline =====
  1280. #pragma --------------------------------------------------------------------------------------------
  1281. + (NSArray *)getHomeOfflineActiveAccount:(NSString *)activeAccount directoryUser:(NSString *)directoryUser fieldOrder:(NSString *)fieldOrder ascending:(BOOL)ascending
  1282. {
  1283. NSMutableArray *tableMetadatas = [NSMutableArray new];
  1284. NSArray *directoriesOffline = [self getOfflineDirectoryActiveAccount:activeAccount];
  1285. NSString *father = @"";
  1286. NSSortDescriptor *descriptor;
  1287. // Add directory
  1288. for (TableDirectory *directory in directoriesOffline) {
  1289. if (![directory.serverUrl containsString:father]) {
  1290. father = directory.serverUrl;
  1291. NSString *upDir = [CCUtility deletingLastPathComponentFromServerUrl:father];
  1292. NSString *directoryID = [self getDirectoryIDFromServerUrl:upDir activeAccount:activeAccount];
  1293. NSString *fileName = [father lastPathComponent];
  1294. if (upDir && directoryID && fileName) {
  1295. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@) AND (directory == 1) AND (fileNameData == %@)", directoryID, activeAccount, fileName];
  1296. TableMetadata *tableMetadata = [self getTableMetadataWithPreficate:predicate];
  1297. if (tableMetadata)
  1298. [tableMetadatas addObject:tableMetadata];
  1299. }
  1300. }
  1301. }
  1302. // Add files
  1303. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (offline == 1)", activeAccount];
  1304. NSArray *localFiles = [CCCoreData getTableLocalFileWithPredicate:predicate];
  1305. for (TableLocalFile *localFile in localFiles) {
  1306. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", activeAccount, localFile.fileID];
  1307. TableMetadata *tableMetadata = [self getTableMetadataWithPreficate:predicate];
  1308. if (tableMetadata)
  1309. [tableMetadatas addObject:tableMetadata];
  1310. }
  1311. // Order
  1312. if ([fieldOrder isEqualToString:@"fileName"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"fileNamePrint" ascending:ascending selector:@selector(localizedCaseInsensitiveCompare:)];
  1313. else if ([fieldOrder isEqualToString:@"fileDate"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:ascending selector:nil];
  1314. else if ([fieldOrder isEqualToString:@"sessionTaskIdentifier"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"sessionTaskIdentifier" ascending:ascending selector:nil];
  1315. else descriptor = [[NSSortDescriptor alloc] initWithKey:fieldOrder ascending:ascending selector:@selector(localizedCaseInsensitiveCompare:)];
  1316. return [tableMetadatas sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];//[NSArray arrayWithArray:tableMetadatas];
  1317. }
  1318. #pragma --------------------------------------------------------------------------------------------
  1319. #pragma mark ===== Activity =====
  1320. #pragma --------------------------------------------------------------------------------------------
  1321. + (void)addActivity:(OCActivity *)activity account:(NSString *)account
  1322. {
  1323. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  1324. [TableActivity MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (idActivity == %d)", account, activity.idActivity] inContext:localContext];
  1325. TableActivity *record = [TableActivity MR_createEntityInContext:localContext];
  1326. record.account = account;
  1327. record.idActivity = [NSNumber numberWithInteger:activity.idActivity];
  1328. record.date = activity.date;
  1329. record.file = activity.file;
  1330. record.link = activity.link;
  1331. record.message = activity.message;
  1332. record.subject = activity.subject;
  1333. }];
  1334. }
  1335. + (NSArray *)getAllTableActivityWithPredicate:(NSPredicate *)predicate
  1336. {
  1337. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1338. NSArray *records = [TableActivity MR_findAllWithPredicate:predicate inContext:context];
  1339. if ([records count] == 0) return nil;
  1340. NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO selector:nil];
  1341. return [records sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
  1342. }
  1343. + (NSInteger)getLastIDActivityActiveAccount:(NSString *)activeAccount
  1344. {
  1345. NSNumber *lastID = [TableActivity MR_aggregateOperation:@"max:" onAttribute:@"idActivity" withPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", activeAccount]];
  1346. return [lastID integerValue];
  1347. }
  1348. #pragma --------------------------------------------------------------------------------------------
  1349. #pragma mark ===== File System =====
  1350. #pragma --------------------------------------------------------------------------------------------
  1351. + (BOOL)downloadFile:(CCMetadata *)metadata directoryUser:(NSString *)directoryUser activeAccount:(NSString *)activeAccount
  1352. {
  1353. CCCrypto *crypto = [[CCCrypto alloc] init];
  1354. // ----------------------------------------- FILESYSTEM ------------------------------------------
  1355. // if encrypted, rewrite
  1356. if (metadata.cryptated == YES)
  1357. if ([crypto decrypt:metadata.fileID fileNameDecrypted:metadata.fileID fileNamePrint:metadata.fileNamePrint password:[crypto getKeyPasscode:metadata.uuid] directoryUser:directoryUser] == 0) return NO;
  1358. // ------------------------------------------ COREDATA -------------------------------------------
  1359. // add/update Table Local File
  1360. [self addLocalFile:metadata activeAccount:activeAccount];
  1361. // EXIF
  1362. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image])
  1363. [CCExifGeo setExifLocalTableFileID:metadata directoryUser:directoryUser activeAccount:activeAccount];
  1364. // Icon
  1365. [CCGraphics createNewImageFrom:metadata.fileID directoryUser:directoryUser fileNameTo:metadata.fileID fileNamePrint:metadata.fileNamePrint size:@"m" imageForUpload:NO typeFile:metadata.typeFile writePreview:YES optimizedFileName:[CCUtility getOptimizedPhoto]];
  1366. return YES;
  1367. }
  1368. + (void)downloadFilePlist:(CCMetadata *)metadata activeAccount:(NSString *)activeAccount activeUrl:(NSString *)activeUrl directoryUser:(NSString *)directoryUser
  1369. {
  1370. // inseriamo le info nel plist
  1371. [CCUtility insertInformationPlist:metadata directoryUser:directoryUser];
  1372. // aggiorniamo il CCMetadata
  1373. [self updateMetadata:metadata predicate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", metadata.fileID, activeAccount] activeAccount:activeAccount activeUrl:activeUrl context:nil];
  1374. // se è un template aggiorniamo anche nel FileSystem
  1375. if ([metadata.type isEqualToString: k_metadataType_template]){
  1376. [self updateLocalFileModel:metadata activeAccount:activeAccount];
  1377. }
  1378. }
  1379. + (void)deleteFile:(CCMetadata *)metadata serverUrl:(NSString *)serverUrl directoryUser:(NSString *)directoryUser activeAccount:(NSString *)activeAccount
  1380. {
  1381. // ----------------------------------------- FILESYSTEM ------------------------------------------
  1382. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", directoryUser, metadata.fileID] error:nil];
  1383. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.ico", directoryUser, metadata.fileID] error:nil];
  1384. // ------------------------------------------ COREDATA -------------------------------------------
  1385. [self deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", metadata.fileID, activeAccount]];
  1386. [self deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", metadata.fileID, activeAccount]];
  1387. // se è una directory cancelliamo tutto quello che è della directory
  1388. if (metadata.directory && serverUrl) {
  1389. NSString *dirForDelete = [CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileNameData];
  1390. [self deleteDirectoryAndSubDirectory:dirForDelete activeAccount:activeAccount];
  1391. }
  1392. }
  1393. #pragma --------------------------------------------------------------------------------------------
  1394. #pragma mark ===== Metadata <> Entity =====
  1395. #pragma --------------------------------------------------------------------------------------------
  1396. + (void)insertMetadataInEntity:(CCMetadata *)metadata recordMetadata:(TableMetadata *)recordMetadata activeAccount:(NSString *)activeAccount activeUrl:(NSString *)activeUrl
  1397. {
  1398. if ([activeAccount length]) recordMetadata.account = activeAccount;
  1399. recordMetadata.cryptated = [NSNumber numberWithBool:metadata.cryptated];
  1400. if (metadata.date) recordMetadata.date = metadata.date;
  1401. recordMetadata.directory = [NSNumber numberWithBool:metadata.directory];
  1402. recordMetadata.errorPasscode = [NSNumber numberWithBool:metadata.errorPasscode];
  1403. recordMetadata.favorite = [NSNumber numberWithBool:metadata.favorite];
  1404. if ([metadata.fileID length]) recordMetadata.fileID = metadata.fileID;
  1405. if ([metadata.directoryID length]) recordMetadata.directoryID = metadata.directoryID;
  1406. if ([metadata.fileName length]) recordMetadata.fileName = metadata.fileName;
  1407. if ([metadata.fileName length]) recordMetadata.fileNameData = [CCUtility trasformedFileNamePlistInCrypto:metadata.fileName];
  1408. if ([metadata.fileNamePrint length]) recordMetadata.fileNamePrint = metadata.fileNamePrint;
  1409. if ([metadata.localIdentifier length]) recordMetadata.localIdentifier = metadata.localIdentifier;
  1410. if ([metadata.model length]) recordMetadata.model = metadata.model;
  1411. if ([metadata.nameCurrentDevice length]) recordMetadata.nameCurrentDevice = metadata.nameCurrentDevice;
  1412. if ([metadata.permissions length]) recordMetadata.permissions = metadata.permissions;
  1413. if ([metadata.protocol length]) recordMetadata.protocol = metadata.protocol;
  1414. if ([metadata.rev length]) recordMetadata.rev = metadata.rev;
  1415. if (metadata.session) recordMetadata.session = metadata.session;
  1416. else metadata.session = @"";
  1417. recordMetadata.sessionError = metadata.sessionError;
  1418. recordMetadata.sessionID = metadata.sessionID;
  1419. recordMetadata.sessionSelector = metadata.sessionSelector;
  1420. recordMetadata.sessionSelectorPost = metadata.sessionSelectorPost;
  1421. recordMetadata.sessionTaskIdentifier = [NSNumber numberWithInt:metadata.sessionTaskIdentifier];
  1422. recordMetadata.sessionTaskIdentifierPlist = [NSNumber numberWithInt:metadata.sessionTaskIdentifierPlist];
  1423. if (metadata.size) recordMetadata.size = [NSNumber numberWithLong:metadata.size];
  1424. if ([metadata.title length]) recordMetadata.title = metadata.title;
  1425. recordMetadata.thumbnailExists = [NSNumber numberWithBool:metadata.thumbnailExists];
  1426. if ([metadata.type length]) recordMetadata.type = metadata.type;
  1427. if ([metadata.uuid length]) recordMetadata.uuid = metadata.uuid;
  1428. // inseriamo il typeFile e icona di default.
  1429. [CCUtility insertTypeFileIconName:metadata directory:[self getServerUrlFromDirectoryID:metadata.directoryID activeAccount:activeAccount] cameraFolderName:[self getCameraUploadFolderNameActiveAccount:activeAccount] cameraFolderPath:[self getCameraUploadFolderPathActiveAccount:activeAccount activeUrl:activeUrl]];
  1430. recordMetadata.typeFile = metadata.typeFile;
  1431. recordMetadata.iconName = metadata.iconName;
  1432. }
  1433. + (CCMetadata *)insertEntityInMetadata:(TableMetadata *)recordMetadata
  1434. {
  1435. CCMetadata *metadata = [[CCMetadata alloc] init];
  1436. metadata.account = recordMetadata.account;
  1437. metadata.cryptated = [recordMetadata.cryptated boolValue];
  1438. metadata.date = recordMetadata.date;
  1439. metadata.directory = [recordMetadata.directory boolValue];
  1440. metadata.errorPasscode = [recordMetadata.errorPasscode boolValue];
  1441. metadata.favorite = [recordMetadata.favorite boolValue];
  1442. metadata.fileID = recordMetadata.fileID;
  1443. metadata.directoryID = recordMetadata.directoryID;
  1444. metadata.fileName = recordMetadata.fileName;
  1445. metadata.fileNameData = recordMetadata.fileNameData;
  1446. metadata.fileNamePrint = recordMetadata.fileNamePrint;
  1447. metadata.iconName = recordMetadata.iconName;
  1448. metadata.localIdentifier = recordMetadata.localIdentifier;
  1449. metadata.model = recordMetadata.model;
  1450. metadata.nameCurrentDevice = recordMetadata.nameCurrentDevice;
  1451. metadata.permissions = recordMetadata.permissions;
  1452. metadata.protocol = recordMetadata.protocol;
  1453. metadata.rev = recordMetadata.rev;
  1454. metadata.session = recordMetadata.session;
  1455. metadata.sessionError = recordMetadata.sessionError;
  1456. metadata.sessionID = recordMetadata.sessionID;
  1457. metadata.sessionSelector = recordMetadata.sessionSelector;
  1458. metadata.sessionSelectorPost = recordMetadata.sessionSelectorPost;
  1459. metadata.sessionTaskIdentifier = [recordMetadata.sessionTaskIdentifier intValue];
  1460. metadata.sessionTaskIdentifierPlist = [recordMetadata.sessionTaskIdentifierPlist intValue];
  1461. metadata.size = [recordMetadata.size longValue];
  1462. metadata.thumbnailExists = [recordMetadata.thumbnailExists boolValue];
  1463. metadata.title = recordMetadata.title;
  1464. metadata.type = recordMetadata.type;
  1465. metadata.typeFile = recordMetadata.typeFile;
  1466. metadata.uuid = recordMetadata.uuid;
  1467. return metadata;
  1468. }
  1469. #pragma --------------------------------------------------------------------------------------------
  1470. #pragma mark ===== Routine for migrate =====
  1471. #pragma --------------------------------------------------------------------------------------------
  1472. #pragma --------------------------------------------------------------------------------------------
  1473. #pragma mark ===== Utility Database =====
  1474. #pragma --------------------------------------------------------------------------------------------
  1475. + (void)moveCoreDataToGroup
  1476. {
  1477. NSString *applicationName = [[[NSBundle mainBundle] infoDictionary] valueForKey:(NSString *)kCFBundleNameKey];
  1478. NSURL *dirGroup = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:k_capabilitiesGroups];
  1479. NSString *dirToPath = [[dirGroup URLByAppendingPathComponent:appDatabase] path];
  1480. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
  1481. NSString *dirFromPath = [[paths lastObject] stringByAppendingPathComponent:applicationName];
  1482. NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirFromPath error:nil];
  1483. NSError *error;
  1484. for(NSString *filename in files)
  1485. [[NSFileManager defaultManager] moveItemAtPath:[dirFromPath stringByAppendingPathComponent:filename] toPath:[dirToPath stringByAppendingPathComponent:filename] error:&error];
  1486. }
  1487. + (void)moveAllUserToGroup
  1488. {
  1489. NSArray *records = [TableAccount MR_findAll];
  1490. for (TableAccount *record in records) {
  1491. NSString *dirFromPath = [CCUtility getOLDDirectoryActiveUser:record.user activeUrl:record.url];
  1492. NSString *dirToPath = [CCUtility getDirectoryActiveUser:record.user activeUrl:record.url];
  1493. NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirFromPath error:nil];
  1494. NSError *error;
  1495. for(NSString *filename in files)
  1496. [[NSFileManager defaultManager] moveItemAtPath:[dirFromPath stringByAppendingPathComponent:filename] toPath:[dirToPath stringByAppendingPathComponent:filename] error:&error];
  1497. }
  1498. }
  1499. + (void)verifyVersionCoreData:(UIViewController *)vc
  1500. {
  1501. NSString *applicationName = [[[NSBundle mainBundle] infoDictionary] valueForKey:(NSString *)kCFBundleNameKey];
  1502. // Get the path for our model (in this case it's named 'cache')
  1503. NSURL *url = [[NSBundle mainBundle] URLForResource:@"cryptocloud" withExtension:@"momd"];
  1504. NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[[NSManagedObjectModel alloc] initWithContentsOfURL:url]]; /* get a coordinator */
  1505. NSString *sourceStoreType = nil;/* type for the source store, or nil if not known */ ;
  1506. NSString *path = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) firstObject];
  1507. // Figure out the full path where our store is located
  1508. path = [path stringByAppendingFormat:@"/%@/cryptocloud", applicationName];
  1509. NSURL *sourceStoreURL = [NSURL fileURLWithPath:path]; /* URL for the source store */ ;
  1510. NSError *error = nil;
  1511. NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:sourceStoreType URL:sourceStoreURL error:&error];
  1512. if (sourceMetadata == nil) {
  1513. NSLog(@"[LOG] Error checking migration validity");
  1514. } else {
  1515. if (![[psc managedObjectModel] isConfiguration:nil compatibleWithStoreMetadata:sourceMetadata]) {
  1516. UIAlertController * alert= [UIAlertController alertControllerWithTitle:nil message:NSLocalizedString(@"_required_new_database_", nil) preferredStyle:UIAlertControllerStyleAlert];
  1517. UIAlertAction* ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault
  1518. handler:^(UIAlertAction * action) {
  1519. [alert dismissViewControllerAnimated:YES completion:nil];
  1520. }];
  1521. [alert addAction:ok];
  1522. [vc presentViewController:alert animated:YES completion:nil];
  1523. // Delete CoreData store
  1524. NSFileManager *manager = [NSFileManager defaultManager];
  1525. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
  1526. NSString *directory = [[paths lastObject] stringByAppendingPathComponent:applicationName];
  1527. NSArray *files = [manager contentsOfDirectoryAtPath:directory error:nil];
  1528. NSError *error;
  1529. for(NSString *filename in files) {
  1530. [manager removeItemAtPath:[directory stringByAppendingPathComponent:filename] error:&error];
  1531. }
  1532. }
  1533. }
  1534. }
  1535. + (void)flushTableActivityAccount:(NSString *)account
  1536. {
  1537. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1538. if (account) {
  1539. [TableActivity MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", account] inContext:context];
  1540. } else {
  1541. [TableActivity MR_truncateAllInContext:context];
  1542. }
  1543. [context MR_saveToPersistentStoreAndWait];
  1544. }
  1545. + (void)flushTableAutomaticUploadAccount:(NSString *)account selector:(NSString *)selector
  1546. {
  1547. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1548. if (account && selector)
  1549. [TableAutomaticUpload MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (selector == %@)", account, selector] inContext:context];
  1550. else if (account && !selector )
  1551. [TableAutomaticUpload MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", account] inContext:context];
  1552. else
  1553. [TableAutomaticUpload MR_truncateAllInContext:context];
  1554. [context MR_saveToPersistentStoreAndWait];
  1555. }
  1556. + (void)flushTableDirectoryAccount:(NSString *)account
  1557. {
  1558. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1559. if (account) {
  1560. [TableDirectory MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", account] inContext:context];
  1561. } else {
  1562. [TableDirectory MR_truncateAllInContext:context];
  1563. }
  1564. [context MR_saveToPersistentStoreAndWait];
  1565. }
  1566. + (void)flushTableLocalFileAccount:(NSString *)account
  1567. {
  1568. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1569. if (account) {
  1570. [TableLocalFile MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", account] inContext:context];
  1571. } else {
  1572. [TableLocalFile MR_truncateAllInContext:context];
  1573. }
  1574. [context MR_saveToPersistentStoreAndWait];
  1575. }
  1576. + (void)flushTableMetadataAccount:(NSString *)account
  1577. {
  1578. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1579. if (account) {
  1580. [TableMetadata MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", account] inContext:context];
  1581. } else {
  1582. [TableMetadata MR_truncateAllInContext:context];
  1583. }
  1584. [context MR_saveToPersistentStoreAndWait];
  1585. }
  1586. + (void)flushTableGPS
  1587. {
  1588. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1589. [TableGPS MR_truncateAllInContext:context];
  1590. [context MR_saveToPersistentStoreAndWait];
  1591. }
  1592. + (void)flushAllDatabase
  1593. {
  1594. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1595. [TableAccount MR_truncateAllInContext:context];
  1596. [TableActivity MR_truncateAllInContext:context];
  1597. [TableAutomaticUpload MR_truncateAllInContext:context];
  1598. [TableCertificates MR_truncateAllInContext:context];
  1599. [TableDirectory MR_truncateAllInContext:context];
  1600. [TableGPS MR_truncateAllInContext:context];
  1601. [TableLocalFile MR_truncateAllInContext:context];
  1602. [TableMetadata MR_truncateAllInContext:context];
  1603. [TableShare MR_truncateAllInContext:context];
  1604. [context MR_saveToPersistentStoreAndWait];
  1605. }
  1606. @end