CCCoreData.m 101 KB

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