CCCoreData.m 95 KB

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