CCCoreData.m 87 KB

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