CCCoreData.m 85 KB

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