CCCoreData.m 103 KB

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