CCCoreData.m 87 KB

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