CCCoreData.m 97 KB

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