CCCoreData.m 88 KB

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