CCCoreData.m 87 KB

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