CCCoreData.m 94 KB

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