CCCoreData.m 101 KB

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