CCCoreData.m 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236
  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. 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. record.permissions = permissions;
  644. record.serverUrl = serverUrl;
  645. }
  646. [context MR_saveToPersistentStoreAndWait];
  647. return directoryID;
  648. }
  649. + (void)deleteDirectoryFromPredicate:(NSPredicate *)predicate
  650. {
  651. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  652. [TableDirectory MR_deleteAllMatchingPredicate:predicate inContext:localContext];
  653. }];
  654. }
  655. + (NSArray *)deleteDirectoryAndSubDirectory:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  656. {
  657. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  658. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  659. NSMutableArray *directoryIDs = [[NSMutableArray alloc] init];
  660. NSArray *tableDirectorys = [TableDirectory MR_findAllWithPredicate:predicate inContext:context];
  661. for(TableDirectory *recordDirectory in tableDirectorys) {
  662. NSLog(@"[LOG] %@", recordDirectory.serverUrl);
  663. if ([recordDirectory.serverUrl hasPrefix:serverUrl]) {
  664. // List directoryIDs removed
  665. [directoryIDs addObject:recordDirectory.directoryID];
  666. // remove all TableMetadata
  667. NSLog(@"[LOG] %@", recordDirectory.directoryID);
  668. // remove directory in Metadata come cazzo si fa a saperlo
  669. //[TableMetadata MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", activeAccount, recordDirectory.directoryID] inContext:context];
  670. NSArray *tableMetadatas = [TableMetadata MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", activeAccount, recordDirectory.directoryID] inContext:context];
  671. for(TableMetadata *recordMetadata in tableMetadatas) {
  672. // remove if in session
  673. if ([recordMetadata.session length] >0) {
  674. if (recordMetadata.sessionTaskIdentifier >= 0)
  675. [[CCNetworking sharedNetworking] settingSession:recordMetadata.session sessionTaskIdentifier:[recordMetadata.sessionTaskIdentifier integerValue] taskStatus: k_taskStatusCancel];
  676. if (recordMetadata.sessionTaskIdentifierPlist >= 0)
  677. [[CCNetworking sharedNetworking] settingSession:recordMetadata.session sessionTaskIdentifier:[recordMetadata.sessionTaskIdentifierPlist integerValue] taskStatus: k_taskStatusCancel];
  678. }
  679. // remove file local
  680. NSLog(@"[LOG] %@", recordMetadata.fileID);
  681. [self deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", activeAccount, recordMetadata.fileID]];
  682. [recordMetadata MR_deleteEntityInContext:context];
  683. }
  684. [recordDirectory MR_deleteEntityInContext:context];
  685. }
  686. }
  687. [context MR_saveToPersistentStoreAndWait];
  688. return directoryIDs;
  689. }
  690. + (void)renameDirectory:(NSString *)serverUrl serverUrlTo:(NSString *)serverUrlTo activeAccount:(NSString *)activeAccount
  691. {
  692. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  693. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  694. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:localContext];
  695. if (record)
  696. record.serverUrl = serverUrlTo;
  697. }];
  698. }
  699. + (void)setDateReadDirectoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount
  700. {
  701. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  702. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", directoryID, activeAccount];
  703. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  704. if (record) {
  705. record.dateReadDirectory = [NSDate date];
  706. [context MR_saveToPersistentStoreAndWait];
  707. }
  708. }
  709. + (NSDate *)getDateReadDirectoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount
  710. {
  711. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  712. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", directoryID, activeAccount];
  713. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  714. if (record) return record.dateReadDirectory;
  715. else return nil;
  716. }
  717. + (void)setDirectoryRev:(NSString *)rev serverUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  718. {
  719. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  720. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  721. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  722. if (record) {
  723. record.rev = rev;
  724. [context MR_saveToPersistentStoreAndWait];
  725. }
  726. }
  727. + (NSString *)getDirectoryRevFromServerUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  728. {
  729. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  730. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  731. if (record) return record.rev;
  732. else return nil;
  733. }
  734. + (NSArray *)getDirectoryIDsFromBeginsWithServerUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  735. {
  736. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl BEGINSWITH %@) AND (account == %@)", serverUrl, activeAccount];
  737. return [TableDirectory MR_findAllWithPredicate:predicate];
  738. }
  739. + (NSString *)getDirectoryIDFromServerUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  740. {
  741. if (serverUrl == nil) return nil;
  742. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  743. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  744. if (record) return record.directoryID;
  745. else {
  746. return [self addDirectory:serverUrl permissions:nil activeAccount:activeAccount];
  747. }
  748. return nil;
  749. }
  750. + (NSString *)getServerUrlFromDirectoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount
  751. {
  752. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", directoryID, activeAccount];
  753. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  754. if (record) return record.serverUrl;
  755. else return nil;
  756. }
  757. + (void)clearDateReadDirectory:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  758. {
  759. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  760. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  761. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:localContext];
  762. if (record)
  763. record.dateReadDirectory = NULL;
  764. }];
  765. }
  766. + (void)clearAllDateReadDirectory
  767. {
  768. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  769. NSArray *records = [TableDirectory MR_findAllInContext:localContext];
  770. for (TableDirectory *record in records)
  771. record.dateReadDirectory = NULL;
  772. }];
  773. }
  774. + (BOOL)isDirectoryOutOfDate:(int)numAddDay directoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount
  775. {
  776. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", activeAccount, directoryID];
  777. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  778. if (record == nil || record.dateReadDirectory == nil) {
  779. return YES;
  780. }
  781. NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
  782. [dateComponents setWeekday:numAddDay];
  783. NSCalendar *calendar = [NSCalendar currentCalendar];
  784. NSDate *datePlus = [calendar dateByAddingComponents:dateComponents toDate:record.dateReadDirectory options:0];
  785. NSDate *now = [NSDate date];
  786. // usa la Cache se richiesto e se la data è entro X giorni dall'ultima volta che l'hai letta.
  787. if ([now compare:datePlus] == NSOrderedDescending) {
  788. return YES;
  789. }
  790. return NO;
  791. }
  792. #pragma --------------------------------------------------------------------------------------------
  793. #pragma mark ===== Offline Directory =====
  794. #pragma --------------------------------------------------------------------------------------------
  795. + (void)removeOfflineDirectoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount
  796. {
  797. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  798. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@) AND (offline == 1)", directoryID, activeAccount];
  799. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:localContext];
  800. if (record)
  801. record.offline = [NSNumber numberWithBool:FALSE];
  802. }];
  803. }
  804. + (NSArray *)getOfflineDirectoryActiveAccount:(NSString *)activeAccount
  805. {
  806. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (offline == 1)", activeAccount];
  807. NSArray *recordsTable = [TableDirectory MR_findAllWithPredicate:predicate];
  808. // Order by serverUrl
  809. NSArray *sortedRecordsTable = [recordsTable sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  810. TableDirectory *record1 = obj1, *record2 = obj2;
  811. return [record1.serverUrl compare:record2.serverUrl];
  812. }];
  813. return sortedRecordsTable;
  814. }
  815. + (void)setOfflineDirectoryServerUrl:(NSString *)serverUrl offline:(BOOL)offline activeAccount:(NSString *)activeAccount
  816. {
  817. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  818. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  819. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:localContext];
  820. if (record)
  821. record.offline = [NSNumber numberWithBool:offline];
  822. }];
  823. }
  824. + (BOOL)isOfflineDirectoryServerUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  825. {
  826. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (offline == 1) AND (account == %@)", serverUrl, activeAccount];
  827. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  828. if (record) return YES;
  829. else return NO;
  830. }
  831. #pragma --------------------------------------------------------------------------------------------
  832. #pragma mark ===== Directory Lock =====
  833. #pragma --------------------------------------------------------------------------------------------
  834. + (BOOL)setDirectoryLock:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  835. {
  836. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  837. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", [self getDirectoryIDFromServerUrl:serverUrl activeAccount:activeAccount], activeAccount];
  838. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  839. if (record) {
  840. record.lock = [NSNumber numberWithBool:YES];
  841. [context MR_saveToPersistentStoreAndWait];
  842. return YES;
  843. }
  844. else return NO;
  845. }
  846. + (BOOL)setDirectoryUnLock:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  847. {
  848. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  849. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", [self getDirectoryIDFromServerUrl:serverUrl activeAccount:activeAccount], activeAccount];
  850. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  851. if (record) {
  852. record.lock = [NSNumber numberWithBool:NO];
  853. [context MR_saveToPersistentStoreAndWait];
  854. return YES;
  855. }
  856. else return NO;
  857. }
  858. + (void)setAllDirectoryUnLockForAccount:(NSString *)activeAccount
  859. {
  860. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  861. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  862. NSArray *records = [TableDirectory MR_findAllWithPredicate:predicate inContext:localContext];
  863. for(TableDirectory *record in records)
  864. record.lock = [NSNumber numberWithBool:NO];
  865. }];
  866. }
  867. + (BOOL)isDirectoryLock:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  868. {
  869. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (lock == 1) AND (account == %@)", [self getDirectoryIDFromServerUrl:serverUrl activeAccount:activeAccount], activeAccount];
  870. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  871. if (record) return YES;
  872. else return NO;
  873. }
  874. + (BOOL)isBlockZone:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  875. {
  876. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (directory == 1)", activeAccount];
  877. NSArray *records = [TableMetadata MR_findAllWithPredicate:predicate];
  878. if ([records count] > 0) {
  879. NSArray *pathComponents = [serverUrl pathComponents];
  880. for (NSString *fileName in pathComponents) {
  881. for(TableMetadata *record in records){
  882. NSString *fileNameEntity = [CCUtility trasformedFileNamePlistInCrypto:record.fileName];
  883. NSString *directoryID = record.directoryID;
  884. NSString *serverUrlEntity = [self getServerUrlFromDirectoryID:directoryID activeAccount:activeAccount];
  885. if([fileName isEqualToString:fileNameEntity]) {
  886. NSString *lockServerUrl = [CCUtility stringAppendServerUrl:serverUrlEntity addFileName:fileNameEntity];
  887. BOOL risultato = [self isDirectoryLock:lockServerUrl activeAccount:activeAccount];
  888. if (risultato) return YES;
  889. }
  890. }
  891. }
  892. }
  893. return NO;
  894. }
  895. #pragma --------------------------------------------------------------------------------------------
  896. #pragma mark ===== LocalFile =====
  897. #pragma --------------------------------------------------------------------------------------------
  898. + (void)addLocalFile:(CCMetadata *)metadata activeAccount:(NSString *)activeAccount
  899. {
  900. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  901. BOOL offline = NO;
  902. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", activeAccount, metadata.fileID];
  903. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  904. if (record) {
  905. offline = [[record valueForKey:@"offline"] boolValue];
  906. [record MR_deleteEntityInContext:localContext];
  907. }
  908. record = [TableLocalFile MR_createEntityInContext:localContext];
  909. record.account = activeAccount;
  910. record.date = metadata.date;
  911. record.fileID = metadata.fileID;
  912. record.exifDate = [NSDate date];
  913. record.exifLatitude = @"-1";
  914. record.exifLongitude = @"-1";
  915. record.offline = [NSNumber numberWithBool:offline];
  916. record.fileName = metadata.fileName;
  917. record.fileNamePrint = metadata.fileNamePrint;
  918. record.rev = metadata.rev;
  919. record.size = [NSNumber numberWithLong:metadata.size];
  920. }];
  921. }
  922. + (void)deleteLocalFileWithPredicate:(NSPredicate *)predicate
  923. {
  924. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  925. [TableLocalFile MR_deleteAllMatchingPredicate:predicate inContext:localContext];
  926. }];
  927. }
  928. + (void)renameLocalFileWithFileID:(NSString *)fileID fileNameTo:(NSString *)fileNameTo fileNamePrintTo:(NSString *)fileNamePrintTo activeAccount:(NSString *)activeAccount
  929. {
  930. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  931. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, activeAccount];
  932. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  933. if (record) {
  934. if (fileNameTo)record.fileName = fileNameTo;
  935. if (fileNamePrintTo)record.fileNamePrint = fileNamePrintTo;
  936. }
  937. }];
  938. }
  939. + (void)updateLocalFileModel:(CCMetadata *)metadata activeAccount:(NSString *)activeAccount
  940. {
  941. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  942. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileName == %@)", activeAccount, metadata.fileName];
  943. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  944. if (record) {
  945. record.fileID = metadata.fileID;
  946. record.date = metadata.date;
  947. record.fileNamePrint = metadata.fileNamePrint;
  948. } else {
  949. [self addLocalFile:metadata activeAccount:activeAccount];
  950. }
  951. }];
  952. }
  953. + (TableLocalFile *)getLocalFileWithFileID:(NSString *)fileID activeAccount:(NSString *)activeAccount
  954. {
  955. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, activeAccount];
  956. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate];
  957. if (record) {
  958. return record;
  959. } else return nil;
  960. }
  961. + (NSMutableArray *)getTableLocalFileWithPredicate:(NSPredicate *)predicate controlZombie:(BOOL)controlZombie activeAccount:(NSString *)activeAccount directoryUser:(NSString *)directoryUser
  962. {
  963. NSMutableArray *ritorno = [[NSMutableArray alloc] init];
  964. NSArray *records = [TableLocalFile MR_findAllWithPredicate:predicate];
  965. if ([records count] > 0) {
  966. // verifichiamo esistano tutti i file altrimenti rimuoviamo il record
  967. for(TableLocalFile *record in records){
  968. if (controlZombie) {
  969. NSString *fileID = record.fileID;
  970. NSString *FilePathFileID = [NSString stringWithFormat:@"%@/%@", directoryUser, fileID];
  971. NSString *FilePathFileName = [NSString stringWithFormat:@"%@/%@", directoryUser, record.fileName];
  972. if (![[NSFileManager defaultManager] fileExistsAtPath:FilePathFileID] && ![[NSFileManager defaultManager] fileExistsAtPath:FilePathFileName] && controlZombie) {
  973. // non esiste nè il file fileID e nemmeno il plist, eliminiamolo.
  974. [self deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, activeAccount]];
  975. } else [ritorno addObject:record];
  976. } else [ritorno addObject:record];
  977. }
  978. } else return nil;
  979. if ([ritorno count] == 0) return nil;
  980. else return ritorno;
  981. }
  982. + (NSArray *)getTableLocalFileWithPredicate:(NSPredicate *)predicate
  983. {
  984. return [TableLocalFile MR_findAllWithPredicate:predicate];
  985. }
  986. #pragma --------------------------------------------------------------------------------------------
  987. #pragma mark ===== Offline LocalFile =====
  988. #pragma --------------------------------------------------------------------------------------------
  989. + (void)setOfflineLocalFileID:(NSString *)fileID offline:(BOOL)offline activeAccount:(NSString *)activeAccount
  990. {
  991. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  992. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, activeAccount];
  993. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  994. if (record)
  995. record.offline = [NSNumber numberWithBool:offline];
  996. }];
  997. }
  998. + (BOOL)isOfflineLocalFileID:(NSString *)fileID activeAccount:(NSString *)activeAccount
  999. {
  1000. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (offline == 1) AND (account == %@)", fileID, activeAccount];
  1001. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate];
  1002. if (record) return YES;
  1003. else return NO;
  1004. }
  1005. + (NSArray *)getOfflineLocalFileActiveAccount:(NSString *)activeAccount directoryUser:(NSString *)directoryUser
  1006. {
  1007. NSMutableArray *metadatas = [NSMutableArray new];
  1008. NSArray *files = [self getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (offline == 1)", activeAccount] controlZombie:YES activeAccount:activeAccount directoryUser:directoryUser];
  1009. for (TableLocalFile *file in files) {
  1010. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", file.fileID, activeAccount];
  1011. CCMetadata *metadata = [self getMetadataWithPreficate:predicate context:nil];
  1012. if (metadata) {
  1013. // verify if is not on directory offline
  1014. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (offline == 1) AND (account == %@)", metadata.directoryID, activeAccount];
  1015. TableDirectory *directory = [TableDirectory MR_findFirstWithPredicate:predicate];
  1016. if (!directory)
  1017. [metadatas addObject:metadata];
  1018. }
  1019. }
  1020. return metadatas;
  1021. }
  1022. #pragma --------------------------------------------------------------------------------------------
  1023. #pragma mark ===== GeoInformation =====
  1024. #pragma --------------------------------------------------------------------------------------------
  1025. + (NSArray *)getGeoInformationLocalFromFileID:(NSString *)fileID activeAccount:(NSString *)activeAccount
  1026. {
  1027. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, activeAccount];
  1028. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate];
  1029. if (record) return [[NSArray alloc] initWithObjects:record.exifDate, record.exifLatitude, record.exifLongitude, nil];
  1030. else return nil;
  1031. }
  1032. + (void)setGeoInformationLocalFromFileID:(NSString *)fileID exifDate:(NSDate *)exifDate exifLatitude:(NSString *)exifLatitude exifLongitude:(NSString *)exifLongitude activeAccount:(NSString *)activeAccount
  1033. {
  1034. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  1035. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", activeAccount, fileID];
  1036. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  1037. if (record) {
  1038. record.exifDate = exifDate;
  1039. record.exifLatitude = exifLatitude;
  1040. record.exifLongitude = exifLongitude;
  1041. }
  1042. }];
  1043. }
  1044. + (void)setGeoInformationLocalNull
  1045. {
  1046. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  1047. NSArray *records = [TableLocalFile MR_findAllInContext:localContext];
  1048. for (TableLocalFile *record in records) {
  1049. if ([record.exifLatitude doubleValue] != 0 || [record.exifLongitude doubleValue] != 0) {
  1050. record.exifLatitude = @"9999";
  1051. record.exifLongitude = @"9999";
  1052. }
  1053. }
  1054. }];
  1055. }
  1056. #pragma --------------------------------------------------------------------------------------------
  1057. #pragma mark ===== Automatic Upload =====
  1058. #pragma --------------------------------------------------------------------------------------------
  1059. + (void)addTableAutomaticUpload:(CCMetadataNet *)metadataNet account:(NSString *)account context:(NSManagedObjectContext *)context
  1060. {
  1061. if (context == nil)
  1062. context = [NSManagedObjectContext MR_context];
  1063. // Delete record if exists
  1064. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileName == %@) AND (serverUrl == %@) AND (selector == %@)", account, metadataNet.fileName, metadataNet.serverUrl, metadataNet.selector];
  1065. [TableAutomaticUpload MR_deleteAllMatchingPredicate:predicate inContext:context];
  1066. TableAutomaticUpload *record = [TableAutomaticUpload MR_createEntityInContext:context];
  1067. record.account = account;
  1068. record.identifier = metadataNet.identifier;
  1069. record.date = [NSDate date];
  1070. record.fileName = metadataNet.fileName;
  1071. record.selector = metadataNet.selector;
  1072. record.selectorPost = metadataNet.selectorPost;
  1073. record.serverUrl = metadataNet.serverUrl;
  1074. record.session = metadataNet.session;
  1075. record.priority = [NSNumber numberWithLong:metadataNet.priority];
  1076. [context MR_saveToPersistentStoreAndWait];
  1077. }
  1078. + (CCMetadataNet *)getTableAutomaticUploadForAccount:(NSString *)account selector:(NSString *)selector context:(NSManagedObjectContext *)context
  1079. {
  1080. if (context == nil)
  1081. context = [NSManagedObjectContext MR_context];
  1082. TableAutomaticUpload *record = [TableAutomaticUpload MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (selector == %@)", account, selector] inContext:context];
  1083. if (record) {
  1084. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] init];
  1085. metadataNet.action = actionUploadAsset; // Default
  1086. metadataNet.identifier = record.identifier;
  1087. metadataNet.fileName = record.fileName;
  1088. metadataNet.priority = [record.priority longValue];
  1089. metadataNet.selector = record.selector;
  1090. metadataNet.selectorPost = record.selectorPost;
  1091. metadataNet.serverUrl = record.serverUrl;
  1092. metadataNet.session = record.session;
  1093. metadataNet.taskStatus = k_taskStatusResume; // Default
  1094. [record MR_deleteEntityInContext:context]; // Remove record
  1095. [context MR_saveToPersistentStoreAndWait];
  1096. return metadataNet;
  1097. }
  1098. return nil;
  1099. }
  1100. + (NSUInteger)countTableAutomaticUploadForAccount:(NSString *)account selector:(NSString *)selector
  1101. {
  1102. if (selector)
  1103. return [TableAutomaticUpload MR_countOfEntitiesWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (selector == %@)", account, selector]];
  1104. else
  1105. return [TableAutomaticUpload MR_countOfEntitiesWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", account]];
  1106. }
  1107. #pragma --------------------------------------------------------------------------------------------
  1108. #pragma mark ===== GPS =====
  1109. #pragma --------------------------------------------------------------------------------------------
  1110. + (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
  1111. {
  1112. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1113. TableGPS *record = [TableGPS MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(latitude == %@) AND (longitude == %@)", latitude, longitude] inContext:context];
  1114. if (!record) {
  1115. record = [TableGPS MR_createEntityInContext:context];
  1116. }
  1117. record.latitude = latitude;
  1118. record.longitude = longitude;
  1119. if (location) record.location = location;
  1120. if (placemarkAdministrativeArea) record.placemarkAdministrativeArea = placemarkAdministrativeArea;
  1121. if (placemarkCountry) record.placemarkCountry = placemarkCountry;
  1122. if (placemarkLocality) record.placemarkLocality = placemarkLocality;
  1123. if (placemarkPostalCode) record.placemarkPostalCode = placemarkPostalCode;
  1124. if (placemarkThoroughfare) record.placemarkThoroughfare = placemarkThoroughfare;
  1125. [context MR_saveToPersistentStoreAndWait];
  1126. }
  1127. + (NSString *)getLocationFromGeoLatitude:(NSString *)latitude longitude:(NSString *)longitude
  1128. {
  1129. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1130. TableGPS *record = [TableGPS MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(latitude == %@) AND (longitude == %@)", latitude, longitude] inContext:context];
  1131. if (record) return record.location;
  1132. else return nil;
  1133. }
  1134. #pragma --------------------------------------------------------------------------------------------
  1135. #pragma mark ===== Share =====
  1136. #pragma --------------------------------------------------------------------------------------------
  1137. + (void)setShareLink:(NSString *)share fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl sharesLink:(NSMutableDictionary *)sharesLink activeAccount:(NSString *)activeAccount
  1138. {
  1139. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1140. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileName == %@) AND (serverUrl == %@)", activeAccount, fileName, serverUrl];
  1141. TableShare *record = [TableShare MR_findFirstWithPredicate:predicate inContext:context];
  1142. if (record) {
  1143. record.shareLink = share;
  1144. } else {
  1145. TableShare *record = [TableShare MR_createEntityInContext:context];
  1146. record.account = activeAccount;
  1147. record.fileName = fileName;
  1148. record.serverUrl = serverUrl;
  1149. record.shareLink = share;
  1150. }
  1151. [context MR_saveToPersistentStoreAndWait];
  1152. if (share && serverUrl && fileName)
  1153. [sharesLink setObject:share forKey:[serverUrl stringByAppendingString:fileName]];
  1154. }
  1155. + (void)setShareUserAndGroup:(NSString *)share fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup 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.shareUserAndGroup = share;
  1162. } else {
  1163. TableShare *record = [TableShare MR_createEntityInContext:context];
  1164. record.account = activeAccount;
  1165. record.fileName = fileName;
  1166. record.serverUrl = serverUrl;
  1167. record.shareUserAndGroup = share;
  1168. }
  1169. [context MR_saveToPersistentStoreAndWait];
  1170. [sharesUserAndGroup setObject:share forKey:[serverUrl stringByAppendingString:fileName]];
  1171. }
  1172. + (void)unShare:(NSString *)share fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl sharesLink:(NSMutableDictionary *)sharesLink sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup activeAccount:(NSString *)activeAccount
  1173. {
  1174. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1175. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND ((shareLink CONTAINS %@) OR (shareUserAndGroup CONTAINS %@))", activeAccount, share, share];
  1176. TableShare *record = [TableShare MR_findFirstWithPredicate:predicate inContext:context];
  1177. if (record) {
  1178. if ([record.shareLink containsString:share]) record.shareLink = @"";
  1179. if ([record.shareUserAndGroup containsString:share]) {
  1180. NSMutableArray *shares = [[NSMutableArray alloc] initWithArray:[record.shareUserAndGroup componentsSeparatedByString:@","]];
  1181. [shares removeObject:share];
  1182. record.shareUserAndGroup = [shares componentsJoinedByString:@","];
  1183. }
  1184. if ([record.shareLink length] == 0 && [record.shareUserAndGroup length] == 0)
  1185. [record MR_deleteEntityInContext:context];
  1186. [context MR_saveToPersistentStoreAndWait];
  1187. if ([record.shareLink length] > 0) [sharesLink setObject:record.shareLink forKey:[serverUrl stringByAppendingString:fileName]];
  1188. else [sharesLink removeObjectForKey:[serverUrl stringByAppendingString:fileName]];
  1189. if ([record.shareUserAndGroup length] > 0) [sharesUserAndGroup setObject:record.shareUserAndGroup forKey:[serverUrl stringByAppendingString:fileName]];
  1190. else [sharesUserAndGroup removeObjectForKey:[serverUrl stringByAppendingString:fileName]];
  1191. }
  1192. }
  1193. + (void)removeAllShareActiveAccount:(NSString *)activeAccount sharesLink:(NSMutableDictionary *)sharesLink sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup
  1194. {
  1195. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1196. [TableShare MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"account == %@", activeAccount] inContext:context];
  1197. [context MR_saveToPersistentStoreAndWait];
  1198. [sharesLink removeAllObjects];
  1199. [sharesUserAndGroup removeAllObjects];
  1200. }
  1201. + (void)updateShare:(NSDictionary *)items sharesLink:(NSMutableDictionary *)sharesLink sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup activeAccount:(NSString *)activeAccount activeUrl:(NSString *)activeUrl
  1202. {
  1203. // rimuovi tutte le condivisioni
  1204. [self removeAllShareActiveAccount:activeAccount sharesLink:sharesLink sharesUserAndGroup:sharesUserAndGroup];
  1205. NSMutableArray *itemsLink = [[NSMutableArray alloc] init];
  1206. NSMutableArray *itemsUsersAndGroups = [[NSMutableArray alloc] init];
  1207. for (NSString *idRemoteShared in items) {
  1208. OCSharedDto *item = [items objectForKey:idRemoteShared];
  1209. if (item.shareType == shareTypeLink) [itemsLink addObject:item];
  1210. if ([[item shareWith] length] > 0 && (item.shareType == shareTypeUser || item.shareType == shareTypeGroup || item.shareType == shareTypeRemote)) [itemsUsersAndGroups addObject:item];
  1211. }
  1212. // Link
  1213. for (OCSharedDto *item in itemsLink) {
  1214. NSString *fullPath = [[CCUtility getHomeServerUrlActiveUrl:activeUrl] stringByAppendingString:item.path];
  1215. NSString *fileName = [fullPath lastPathComponent];
  1216. NSString *serverUrl = [fullPath substringToIndex:([fullPath length]-[fileName length]-1)];
  1217. if ([serverUrl hasSuffix:@"/"]) serverUrl = [serverUrl substringToIndex:[serverUrl length] - 1];
  1218. if ([@(item.idRemoteShared) stringValue])
  1219. [self setShareLink:[@(item.idRemoteShared) stringValue] fileName:fileName serverUrl:serverUrl sharesLink:sharesLink activeAccount:activeAccount];
  1220. }
  1221. // Condivisioni
  1222. NSMutableDictionary *paths = [[NSMutableDictionary alloc] init];
  1223. // Creazione dizionario
  1224. for (OCSharedDto *item in itemsUsersAndGroups) {
  1225. if ([paths objectForKey:item.path]) {
  1226. NSMutableArray *share = [paths objectForKey:item.path];
  1227. [share addObject:[@(item.idRemoteShared) stringValue]];
  1228. [paths setObject:share forKey:item.path];
  1229. } else {
  1230. NSMutableArray *share = [[NSMutableArray alloc] initWithObjects:[@(item.idRemoteShared) stringValue], nil];
  1231. [paths setObject:share forKey:item.path];
  1232. }
  1233. }
  1234. // Scrittura su DB
  1235. for (NSString *path in paths) {
  1236. NSArray *items = [paths objectForKey:path];
  1237. NSString *share = [items componentsJoinedByString:@","];
  1238. NSLog(@"[LOG] share %@", share);
  1239. NSString *fullPath = [[CCUtility getHomeServerUrlActiveUrl:activeUrl] stringByAppendingString:path];
  1240. NSString *fileName = [fullPath lastPathComponent];
  1241. NSString *serverUrl = [fullPath substringToIndex:([fullPath length]-[fileName length]-1)];
  1242. if ([serverUrl hasSuffix:@"/"]) serverUrl = [serverUrl substringToIndex:[serverUrl length] - 1];
  1243. if (share)
  1244. [self setShareUserAndGroup:share fileName:fileName serverUrl:serverUrl sharesUserAndGroup:sharesUserAndGroup activeAccount:activeAccount];
  1245. }
  1246. }
  1247. + (void)populateSharesVariableFromDBActiveAccount:(NSString *)activeAccount sharesLink:(NSMutableDictionary *)sharesLink sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup
  1248. {
  1249. [sharesLink removeAllObjects];
  1250. [sharesUserAndGroup removeAllObjects];
  1251. NSArray *records = [TableShare MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", activeAccount]];
  1252. for (TableShare *record in records) {
  1253. if ([record.shareLink length] > 0 && record.serverUrl && record.fileName)
  1254. [sharesLink setObject:record.shareLink forKey:[record.serverUrl stringByAppendingString:record.fileName]];
  1255. if ([record.shareUserAndGroup length] > 0 && record.serverUrl && record.fileName)
  1256. [sharesUserAndGroup setObject:record.shareUserAndGroup forKey:[record.serverUrl stringByAppendingString:record.fileName]];
  1257. }
  1258. return;
  1259. }
  1260. #pragma --------------------------------------------------------------------------------------------
  1261. #pragma mark ===== Offline =====
  1262. #pragma --------------------------------------------------------------------------------------------
  1263. + (NSArray *)getHomeOfflineActiveAccount:(NSString *)activeAccount directoryUser:(NSString *)directoryUser fieldOrder:(NSString *)fieldOrder ascending:(BOOL)ascending
  1264. {
  1265. NSMutableArray *tableMetadatas = [NSMutableArray new];
  1266. NSArray *directoriesOffline = [self getOfflineDirectoryActiveAccount:activeAccount];
  1267. NSString *father = @"";
  1268. NSSortDescriptor *descriptor;
  1269. // Add directory
  1270. for (TableDirectory *directory in directoriesOffline) {
  1271. if (![directory.serverUrl containsString:father]) {
  1272. father = directory.serverUrl;
  1273. NSString *upDir = [CCUtility deletingLastPathComponentFromServerUrl:father];
  1274. NSString *directoryID = [self getDirectoryIDFromServerUrl:upDir activeAccount:activeAccount];
  1275. NSString *fileName = [father lastPathComponent];
  1276. if (upDir && directoryID && fileName) {
  1277. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@) AND (directory == 1) AND (fileNameData == %@)", directoryID, activeAccount, fileName];
  1278. TableMetadata *tableMetadata = [self getTableMetadataWithPreficate:predicate];
  1279. if (tableMetadata)
  1280. [tableMetadatas addObject:tableMetadata];
  1281. }
  1282. }
  1283. }
  1284. // Add files
  1285. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (offline == 1)", activeAccount];
  1286. NSArray *localFiles = [CCCoreData getTableLocalFileWithPredicate:predicate];
  1287. for (TableLocalFile *localFile in localFiles) {
  1288. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", activeAccount, localFile.fileID];
  1289. TableMetadata *tableMetadata = [self getTableMetadataWithPreficate:predicate];
  1290. if (tableMetadata)
  1291. [tableMetadatas addObject:tableMetadata];
  1292. }
  1293. // Order
  1294. if ([fieldOrder isEqualToString:@"fileName"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"fileNamePrint" ascending:ascending selector:@selector(localizedCaseInsensitiveCompare:)];
  1295. else if ([fieldOrder isEqualToString:@"fileDate"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:ascending selector:nil];
  1296. else if ([fieldOrder isEqualToString:@"sessionTaskIdentifier"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"sessionTaskIdentifier" ascending:ascending selector:nil];
  1297. else descriptor = [[NSSortDescriptor alloc] initWithKey:fieldOrder ascending:ascending selector:@selector(localizedCaseInsensitiveCompare:)];
  1298. return [tableMetadatas sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];//[NSArray arrayWithArray:tableMetadatas];
  1299. }
  1300. #pragma --------------------------------------------------------------------------------------------
  1301. #pragma mark ===== Activity =====
  1302. #pragma --------------------------------------------------------------------------------------------
  1303. + (void)addActivity:(OCActivity *)activity account:(NSString *)account
  1304. {
  1305. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  1306. [TableActivity MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (idActivity == %d)", account, activity.idActivity] inContext:localContext];
  1307. TableActivity *record = [TableActivity MR_createEntityInContext:localContext];
  1308. record.account = account;
  1309. record.idActivity = [NSNumber numberWithInteger:activity.idActivity];
  1310. record.date = activity.date;
  1311. record.file = activity.file;
  1312. record.link = activity.link;
  1313. record.message = activity.message;
  1314. record.subject = activity.subject;
  1315. }];
  1316. }
  1317. + (NSArray *)getAllTableActivityWithPredicate:(NSPredicate *)predicate
  1318. {
  1319. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1320. NSArray *records = [TableActivity MR_findAllWithPredicate:predicate inContext:context];
  1321. if ([records count] == 0) return nil;
  1322. NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO selector:nil];
  1323. return [records sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
  1324. }
  1325. + (NSInteger)getLastIDActivityActiveAccount:(NSString *)activeAccount
  1326. {
  1327. NSNumber *lastID = [TableActivity MR_aggregateOperation:@"max:" onAttribute:@"idActivity" withPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", activeAccount]];
  1328. return [lastID integerValue];
  1329. }
  1330. #pragma --------------------------------------------------------------------------------------------
  1331. #pragma mark ===== File System =====
  1332. #pragma --------------------------------------------------------------------------------------------
  1333. + (BOOL)downloadFile:(CCMetadata *)metadata directoryUser:(NSString *)directoryUser activeAccount:(NSString *)activeAccount
  1334. {
  1335. CCCrypto *crypto = [[CCCrypto alloc] init];
  1336. // ----------------------------------------- FILESYSTEM ------------------------------------------
  1337. // if encrypted, rewrite
  1338. if (metadata.cryptated == YES)
  1339. if ([crypto decrypt:metadata.fileID fileNameDecrypted:metadata.fileID fileNamePrint:metadata.fileNamePrint password:[crypto getKeyPasscode:metadata.uuid] directoryUser:directoryUser] == 0) return NO;
  1340. // ------------------------------------------ COREDATA -------------------------------------------
  1341. // add/update Table Local File
  1342. [self addLocalFile:metadata activeAccount:activeAccount];
  1343. // EXIF
  1344. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image])
  1345. [CCExifGeo setExifLocalTableFileID:metadata directoryUser:directoryUser activeAccount:activeAccount];
  1346. // Icon
  1347. [CCGraphics createNewImageFrom:metadata.fileID directoryUser:directoryUser fileNameTo:metadata.fileID fileNamePrint:metadata.fileNamePrint size:@"m" imageForUpload:NO typeFile:metadata.typeFile writePreview:YES optimizedFileName:[CCUtility getOptimizedPhoto]];
  1348. return YES;
  1349. }
  1350. + (void)downloadFilePlist:(CCMetadata *)metadata activeAccount:(NSString *)activeAccount activeUrl:(NSString *)activeUrl directoryUser:(NSString *)directoryUser
  1351. {
  1352. // inseriamo le info nel plist
  1353. [CCUtility insertInformationPlist:metadata directoryUser:directoryUser];
  1354. // aggiorniamo il CCMetadata
  1355. [self updateMetadata:metadata predicate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", metadata.fileID, activeAccount] activeAccount:activeAccount activeUrl:activeUrl context:nil];
  1356. // se è un template aggiorniamo anche nel FileSystem
  1357. if ([metadata.type isEqualToString: k_metadataType_template]){
  1358. [self updateLocalFileModel:metadata activeAccount:activeAccount];
  1359. }
  1360. }
  1361. + (void)deleteFile:(CCMetadata *)metadata serverUrl:(NSString *)serverUrl directoryUser:(NSString *)directoryUser activeAccount:(NSString *)activeAccount
  1362. {
  1363. // ----------------------------------------- FILESYSTEM ------------------------------------------
  1364. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", directoryUser, metadata.fileID] error:nil];
  1365. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.ico", directoryUser, metadata.fileID] error:nil];
  1366. // ------------------------------------------ COREDATA -------------------------------------------
  1367. [self deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", metadata.fileID, activeAccount]];
  1368. [self deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", metadata.fileID, activeAccount]];
  1369. // se è una directory cancelliamo tutto quello che è della directory
  1370. if (metadata.directory && serverUrl) {
  1371. NSString *dirForDelete = [CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileNameData];
  1372. [self deleteDirectoryAndSubDirectory:dirForDelete activeAccount:activeAccount];
  1373. }
  1374. }
  1375. #pragma --------------------------------------------------------------------------------------------
  1376. #pragma mark ===== Metadata <> Entity =====
  1377. #pragma --------------------------------------------------------------------------------------------
  1378. + (void)insertMetadataInEntity:(CCMetadata *)metadata recordMetadata:(TableMetadata *)recordMetadata activeAccount:(NSString *)activeAccount activeUrl:(NSString *)activeUrl
  1379. {
  1380. if ([activeAccount length]) recordMetadata.account = activeAccount;
  1381. recordMetadata.cryptated = [NSNumber numberWithBool:metadata.cryptated];
  1382. if (metadata.date) recordMetadata.date = metadata.date;
  1383. recordMetadata.directory = [NSNumber numberWithBool:metadata.directory];
  1384. recordMetadata.errorPasscode = [NSNumber numberWithBool:metadata.errorPasscode];
  1385. recordMetadata.favorite = [NSNumber numberWithBool:metadata.favorite];
  1386. if ([metadata.fileID length]) recordMetadata.fileID = metadata.fileID;
  1387. if ([metadata.directoryID length]) recordMetadata.directoryID = metadata.directoryID;
  1388. if ([metadata.fileName length]) recordMetadata.fileName = metadata.fileName;
  1389. if ([metadata.fileName length]) recordMetadata.fileNameData = [CCUtility trasformedFileNamePlistInCrypto:metadata.fileName];
  1390. if ([metadata.fileNamePrint length]) recordMetadata.fileNamePrint = metadata.fileNamePrint;
  1391. if ([metadata.localIdentifier length]) recordMetadata.localIdentifier = metadata.localIdentifier;
  1392. if ([metadata.model length]) recordMetadata.model = metadata.model;
  1393. if ([metadata.nameCurrentDevice length]) recordMetadata.nameCurrentDevice = metadata.nameCurrentDevice;
  1394. if ([metadata.permissions length]) recordMetadata.permissions = metadata.permissions;
  1395. if ([metadata.protocol length]) recordMetadata.protocol = metadata.protocol;
  1396. if ([metadata.rev length]) recordMetadata.rev = metadata.rev;
  1397. if (metadata.session) recordMetadata.session = metadata.session;
  1398. else metadata.session = @"";
  1399. recordMetadata.sessionError = metadata.sessionError;
  1400. recordMetadata.sessionID = metadata.sessionID;
  1401. recordMetadata.sessionSelector = metadata.sessionSelector;
  1402. recordMetadata.sessionSelectorPost = metadata.sessionSelectorPost;
  1403. recordMetadata.sessionTaskIdentifier = [NSNumber numberWithInt:metadata.sessionTaskIdentifier];
  1404. recordMetadata.sessionTaskIdentifierPlist = [NSNumber numberWithInt:metadata.sessionTaskIdentifierPlist];
  1405. if (metadata.size) recordMetadata.size = [NSNumber numberWithLong:metadata.size];
  1406. if ([metadata.title length]) recordMetadata.title = metadata.title;
  1407. recordMetadata.thumbnailExists = [NSNumber numberWithBool:metadata.thumbnailExists];
  1408. if ([metadata.type length]) recordMetadata.type = metadata.type;
  1409. if ([metadata.uuid length]) recordMetadata.uuid = metadata.uuid;
  1410. // inseriamo il typeFile e icona di default.
  1411. [CCUtility insertTypeFileIconName:metadata directory:[self getServerUrlFromDirectoryID:metadata.directoryID activeAccount:activeAccount] cameraFolderName:[self getCameraUploadFolderNameActiveAccount:activeAccount] cameraFolderPath:[self getCameraUploadFolderPathActiveAccount:activeAccount activeUrl:activeUrl]];
  1412. recordMetadata.typeFile = metadata.typeFile;
  1413. recordMetadata.iconName = metadata.iconName;
  1414. }
  1415. + (CCMetadata *)insertEntityInMetadata:(TableMetadata *)recordMetadata
  1416. {
  1417. CCMetadata *metadata = [[CCMetadata alloc] init];
  1418. metadata.account = recordMetadata.account;
  1419. metadata.cryptated = [recordMetadata.cryptated boolValue];
  1420. metadata.date = recordMetadata.date;
  1421. metadata.directory = [recordMetadata.directory boolValue];
  1422. metadata.errorPasscode = [recordMetadata.errorPasscode boolValue];
  1423. metadata.favorite = [recordMetadata.favorite boolValue];
  1424. metadata.fileID = recordMetadata.fileID;
  1425. metadata.directoryID = recordMetadata.directoryID;
  1426. metadata.fileName = recordMetadata.fileName;
  1427. metadata.fileNameData = recordMetadata.fileNameData;
  1428. metadata.fileNamePrint = recordMetadata.fileNamePrint;
  1429. metadata.iconName = recordMetadata.iconName;
  1430. metadata.localIdentifier = recordMetadata.localIdentifier;
  1431. metadata.model = recordMetadata.model;
  1432. metadata.nameCurrentDevice = recordMetadata.nameCurrentDevice;
  1433. metadata.permissions = recordMetadata.permissions;
  1434. metadata.protocol = recordMetadata.protocol;
  1435. metadata.rev = recordMetadata.rev;
  1436. metadata.session = recordMetadata.session;
  1437. metadata.sessionError = recordMetadata.sessionError;
  1438. metadata.sessionID = recordMetadata.sessionID;
  1439. metadata.sessionSelector = recordMetadata.sessionSelector;
  1440. metadata.sessionSelectorPost = recordMetadata.sessionSelectorPost;
  1441. metadata.sessionTaskIdentifier = [recordMetadata.sessionTaskIdentifier intValue];
  1442. metadata.sessionTaskIdentifierPlist = [recordMetadata.sessionTaskIdentifierPlist intValue];
  1443. metadata.size = [recordMetadata.size longValue];
  1444. metadata.thumbnailExists = [recordMetadata.thumbnailExists boolValue];
  1445. metadata.title = recordMetadata.title;
  1446. metadata.type = recordMetadata.type;
  1447. metadata.typeFile = recordMetadata.typeFile;
  1448. metadata.uuid = recordMetadata.uuid;
  1449. return metadata;
  1450. }
  1451. #pragma --------------------------------------------------------------------------------------------
  1452. #pragma mark ===== Routine for migrate =====
  1453. #pragma --------------------------------------------------------------------------------------------
  1454. + (void)localTableCopyFavoriteToOffline
  1455. {
  1456. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  1457. NSArray *records = [TableLocalFile MR_findAllInContext:localContext];
  1458. for (TableLocalFile *record in records) {
  1459. if ([record.favorite isEqualToNumber:[NSNumber numberWithInt:1]]) {
  1460. record.favorite = [NSNumber numberWithInteger:0];
  1461. record.offline = [NSNumber numberWithInteger:1];
  1462. }
  1463. }
  1464. }];
  1465. }
  1466. + (void)directoryTableCopySynchronizedToOffline
  1467. {
  1468. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  1469. NSArray *records = [TableDirectory MR_findAllInContext:localContext];
  1470. for (TableDirectory *record in records) {
  1471. if ([record.synchronized isEqualToNumber:[NSNumber numberWithInt:1]]) {
  1472. record.synchronized = [NSNumber numberWithInteger:0];
  1473. record.offline = [NSNumber numberWithInteger:1];
  1474. }
  1475. }
  1476. }];
  1477. }
  1478. #pragma --------------------------------------------------------------------------------------------
  1479. #pragma mark ===== Utility Database =====
  1480. #pragma --------------------------------------------------------------------------------------------
  1481. + (void)moveCoreDataToGroup
  1482. {
  1483. NSString *applicationName = [[[NSBundle mainBundle] infoDictionary] valueForKey:(NSString *)kCFBundleNameKey];
  1484. NSURL *dirGroup = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:k_capabilitiesGroups];
  1485. NSString *dirToPath = [[dirGroup URLByAppendingPathComponent:appDatabase] path];
  1486. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
  1487. NSString *dirFromPath = [[paths lastObject] stringByAppendingPathComponent:applicationName];
  1488. NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirFromPath error:nil];
  1489. NSError *error;
  1490. for(NSString *filename in files)
  1491. [[NSFileManager defaultManager] moveItemAtPath:[dirFromPath stringByAppendingPathComponent:filename] toPath:[dirToPath stringByAppendingPathComponent:filename] error:&error];
  1492. }
  1493. + (void)moveAllUserToGroup
  1494. {
  1495. NSArray *records = [TableAccount MR_findAll];
  1496. for (TableAccount *record in records) {
  1497. NSString *dirFromPath = [CCUtility getOLDDirectoryActiveUser:record.user activeUrl:record.url];
  1498. NSString *dirToPath = [CCUtility getDirectoryActiveUser:record.user activeUrl:record.url];
  1499. NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirFromPath error:nil];
  1500. NSError *error;
  1501. for(NSString *filename in files)
  1502. [[NSFileManager defaultManager] moveItemAtPath:[dirFromPath stringByAppendingPathComponent:filename] toPath:[dirToPath stringByAppendingPathComponent:filename] error:&error];
  1503. }
  1504. }
  1505. + (void)verifyVersionCoreData:(UIViewController *)vc
  1506. {
  1507. NSString *applicationName = [[[NSBundle mainBundle] infoDictionary] valueForKey:(NSString *)kCFBundleNameKey];
  1508. // Get the path for our model (in this case it's named 'cache')
  1509. NSURL *url = [[NSBundle mainBundle] URLForResource:@"cryptocloud" withExtension:@"momd"];
  1510. NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[[NSManagedObjectModel alloc] initWithContentsOfURL:url]]; /* get a coordinator */
  1511. NSString *sourceStoreType = nil;/* type for the source store, or nil if not known */ ;
  1512. NSString *path = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) firstObject];
  1513. // Figure out the full path where our store is located
  1514. path = [path stringByAppendingFormat:@"/%@/cryptocloud", applicationName];
  1515. NSURL *sourceStoreURL = [NSURL fileURLWithPath:path]; /* URL for the source store */ ;
  1516. NSError *error = nil;
  1517. NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:sourceStoreType URL:sourceStoreURL error:&error];
  1518. if (sourceMetadata == nil) {
  1519. NSLog(@"[LOG] Error checking migration validity");
  1520. } else {
  1521. if (![[psc managedObjectModel] isConfiguration:nil compatibleWithStoreMetadata:sourceMetadata]) {
  1522. UIAlertController * alert= [UIAlertController alertControllerWithTitle:nil message:NSLocalizedString(@"_required_new_database_", nil) preferredStyle:UIAlertControllerStyleAlert];
  1523. UIAlertAction* ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault
  1524. handler:^(UIAlertAction * action) {
  1525. [alert dismissViewControllerAnimated:YES completion:nil];
  1526. }];
  1527. [alert addAction:ok];
  1528. [vc presentViewController:alert animated:YES completion:nil];
  1529. // Delete CoreData store
  1530. NSFileManager *manager = [NSFileManager defaultManager];
  1531. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
  1532. NSString *directory = [[paths lastObject] stringByAppendingPathComponent:applicationName];
  1533. NSArray *files = [manager contentsOfDirectoryAtPath:directory error:nil];
  1534. NSError *error;
  1535. for(NSString *filename in files) {
  1536. [manager removeItemAtPath:[directory stringByAppendingPathComponent:filename] error:&error];
  1537. }
  1538. }
  1539. }
  1540. }
  1541. + (void)flushTableActivityAccount:(NSString *)account
  1542. {
  1543. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1544. if (account) {
  1545. [TableActivity MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", account] inContext:context];
  1546. } else {
  1547. [TableActivity MR_truncateAllInContext:context];
  1548. }
  1549. [context MR_saveToPersistentStoreAndWait];
  1550. }
  1551. + (void)flushTableAutomaticUploadAccount:(NSString *)account selector:(NSString *)selector
  1552. {
  1553. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1554. if (account && selector)
  1555. [TableAutomaticUpload MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (selector == %@)", account, selector] inContext:context];
  1556. else if (account && !selector )
  1557. [TableAutomaticUpload MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", account] inContext:context];
  1558. else
  1559. [TableAutomaticUpload MR_truncateAllInContext:context];
  1560. [context MR_saveToPersistentStoreAndWait];
  1561. }
  1562. + (void)flushTableDirectoryAccount:(NSString *)account
  1563. {
  1564. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1565. if (account) {
  1566. [TableDirectory MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", account] inContext:context];
  1567. } else {
  1568. [TableDirectory MR_truncateAllInContext:context];
  1569. }
  1570. [context MR_saveToPersistentStoreAndWait];
  1571. }
  1572. + (void)flushTableLocalFileAccount:(NSString *)account
  1573. {
  1574. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1575. if (account) {
  1576. [TableLocalFile MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", account] inContext:context];
  1577. } else {
  1578. [TableLocalFile MR_truncateAllInContext:context];
  1579. }
  1580. [context MR_saveToPersistentStoreAndWait];
  1581. }
  1582. + (void)flushTableMetadataAccount:(NSString *)account
  1583. {
  1584. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1585. if (account) {
  1586. [TableMetadata MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", account] inContext:context];
  1587. } else {
  1588. [TableMetadata MR_truncateAllInContext:context];
  1589. }
  1590. [context MR_saveToPersistentStoreAndWait];
  1591. }
  1592. + (void)flushTableGPS
  1593. {
  1594. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1595. [TableGPS MR_truncateAllInContext:context];
  1596. [context MR_saveToPersistentStoreAndWait];
  1597. }
  1598. + (void)flushAllDatabase
  1599. {
  1600. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1601. [TableAccount MR_truncateAllInContext:context];
  1602. [TableActivity MR_truncateAllInContext:context];
  1603. [TableAutomaticUpload MR_truncateAllInContext:context];
  1604. [TableCertificates MR_truncateAllInContext:context];
  1605. [TableDirectory MR_truncateAllInContext:context];
  1606. [TableGPS MR_truncateAllInContext:context];
  1607. [TableLocalFile MR_truncateAllInContext:context];
  1608. [TableMetadata MR_truncateAllInContext:context];
  1609. [TableShare MR_truncateAllInContext:context];
  1610. [context MR_saveToPersistentStoreAndWait];
  1611. }
  1612. @end