CCCoreData.m 86 KB

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