CCCoreData.m 86 KB

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