CCCoreData.m 87 KB

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