CCCoreData.m 97 KB

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