CCCoreData.m 97 KB

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