CCCoreData.m 101 KB

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