CCCoreData.m 86 KB

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