CCCoreData.m 87 KB

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