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. NSString *serverUrlBeginWith = serverUrl;
  760. if (![serverUrl hasSuffix:@"/"])
  761. serverUrlBeginWith = [serverUrl stringByAppendingString:@"/"];
  762. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"((serverUrl == %@) OR (serverUrl BEGINSWITH %@)) AND (account == %@)", serverUrl, serverUrlBeginWith, activeAccount];
  763. return [TableDirectory MR_findAllWithPredicate:predicate];
  764. }
  765. + (NSString *)getDirectoryIDFromServerUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  766. {
  767. if (serverUrl == nil) return nil;
  768. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  769. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  770. if (record) return record.directoryID;
  771. else {
  772. return [self addDirectory:serverUrl permissions:nil activeAccount:activeAccount];
  773. }
  774. return nil;
  775. }
  776. + (NSString *)getServerUrlFromDirectoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount
  777. {
  778. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", directoryID, activeAccount];
  779. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  780. if (record) return record.serverUrl;
  781. else return nil;
  782. }
  783. + (void)clearDateReadAccount:(NSString *)activeAccount serverUrl:(NSString *)serverUrl directoryID:(NSString *)directoryID
  784. {
  785. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  786. NSPredicate *predicate;
  787. if ([serverUrl length] > 0)
  788. predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  789. if ([directoryID length] > 0)
  790. predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", directoryID, activeAccount];
  791. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:localContext];
  792. if (record) {
  793. record.dateReadDirectory = NULL;
  794. record.rev = @"";
  795. }
  796. }];
  797. }
  798. + (void)clearAllDateReadDirectory
  799. {
  800. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  801. NSArray *records = [TableDirectory MR_findAllInContext:localContext];
  802. for (TableDirectory *record in records) {
  803. record.dateReadDirectory = NULL;
  804. record.rev = @"";
  805. }
  806. }];
  807. }
  808. + (BOOL)isDirectoryOutOfDate:(int)numAddDay directoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount
  809. {
  810. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (directoryID == %@)", activeAccount, directoryID];
  811. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  812. if (record == nil || record.dateReadDirectory == nil) {
  813. return YES;
  814. }
  815. NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
  816. [dateComponents setWeekday:numAddDay];
  817. NSCalendar *calendar = [NSCalendar currentCalendar];
  818. NSDate *datePlus = [calendar dateByAddingComponents:dateComponents toDate:record.dateReadDirectory options:0];
  819. NSDate *now = [NSDate date];
  820. // usa la Cache se richiesto e se la data è entro X giorni dall'ultima volta che l'hai letta.
  821. if ([now compare:datePlus] == NSOrderedDescending) {
  822. return YES;
  823. }
  824. return NO;
  825. }
  826. #pragma --------------------------------------------------------------------------------------------
  827. #pragma mark ===== Offline Directory =====
  828. #pragma --------------------------------------------------------------------------------------------
  829. + (void)removeOfflineDirectoryID:(NSString *)directoryID activeAccount:(NSString *)activeAccount
  830. {
  831. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  832. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@) AND (offline == 1)", directoryID, activeAccount];
  833. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:localContext];
  834. if (record)
  835. record.offline = [NSNumber numberWithBool:FALSE];
  836. }];
  837. }
  838. + (NSArray *)getOfflineDirectoryActiveAccount:(NSString *)activeAccount
  839. {
  840. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (offline == 1)", activeAccount];
  841. NSArray *recordsTable = [TableDirectory MR_findAllWithPredicate:predicate];
  842. // Order by serverUrl
  843. NSArray *sortedRecordsTable = [recordsTable sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  844. TableDirectory *record1 = obj1, *record2 = obj2;
  845. return [record1.serverUrl compare:record2.serverUrl];
  846. }];
  847. return sortedRecordsTable;
  848. }
  849. + (void)setOfflineDirectoryServerUrl:(NSString *)serverUrl offline:(BOOL)offline activeAccount:(NSString *)activeAccount
  850. {
  851. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  852. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (account == %@)", serverUrl, activeAccount];
  853. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:localContext];
  854. if (record)
  855. record.offline = [NSNumber numberWithBool:offline];
  856. }];
  857. }
  858. + (BOOL)isOfflineDirectoryServerUrl:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  859. {
  860. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(serverUrl == %@) AND (offline == 1) AND (account == %@)", serverUrl, activeAccount];
  861. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  862. if (record) return YES;
  863. else return NO;
  864. }
  865. #pragma --------------------------------------------------------------------------------------------
  866. #pragma mark ===== Directory Lock =====
  867. #pragma --------------------------------------------------------------------------------------------
  868. + (BOOL)setDirectoryLock:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  869. {
  870. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  871. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", [self getDirectoryIDFromServerUrl:serverUrl activeAccount:activeAccount], activeAccount];
  872. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  873. if (record) {
  874. record.lock = [NSNumber numberWithBool:YES];
  875. [context MR_saveToPersistentStoreAndWait];
  876. return YES;
  877. }
  878. else return NO;
  879. }
  880. + (BOOL)setDirectoryUnLock:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  881. {
  882. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  883. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@)", [self getDirectoryIDFromServerUrl:serverUrl activeAccount:activeAccount], activeAccount];
  884. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate inContext:context];
  885. if (record) {
  886. record.lock = [NSNumber numberWithBool:NO];
  887. [context MR_saveToPersistentStoreAndWait];
  888. return YES;
  889. }
  890. else return NO;
  891. }
  892. + (void)setAllDirectoryUnLockForAccount:(NSString *)activeAccount
  893. {
  894. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  895. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", activeAccount];
  896. NSArray *records = [TableDirectory MR_findAllWithPredicate:predicate inContext:localContext];
  897. for(TableDirectory *record in records)
  898. record.lock = [NSNumber numberWithBool:NO];
  899. }];
  900. }
  901. + (BOOL)isDirectoryLock:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  902. {
  903. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (lock == 1) AND (account == %@)", [self getDirectoryIDFromServerUrl:serverUrl activeAccount:activeAccount], activeAccount];
  904. TableDirectory *record = [TableDirectory MR_findFirstWithPredicate:predicate];
  905. if (record) return YES;
  906. else return NO;
  907. }
  908. + (BOOL)isBlockZone:(NSString *)serverUrl activeAccount:(NSString *)activeAccount
  909. {
  910. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (directory == 1)", activeAccount];
  911. NSArray *records = [TableMetadata MR_findAllWithPredicate:predicate];
  912. if ([records count] > 0) {
  913. NSArray *pathComponents = [serverUrl pathComponents];
  914. for (NSString *fileName in pathComponents) {
  915. for(TableMetadata *record in records){
  916. NSString *fileNameEntity = [CCUtility trasformedFileNamePlistInCrypto:record.fileName];
  917. NSString *directoryID = record.directoryID;
  918. NSString *serverUrlEntity = [self getServerUrlFromDirectoryID:directoryID activeAccount:activeAccount];
  919. if([fileName isEqualToString:fileNameEntity]) {
  920. NSString *lockServerUrl = [CCUtility stringAppendServerUrl:serverUrlEntity addFileName:fileNameEntity];
  921. BOOL risultato = [self isDirectoryLock:lockServerUrl activeAccount:activeAccount];
  922. if (risultato) return YES;
  923. }
  924. }
  925. }
  926. }
  927. return NO;
  928. }
  929. #pragma --------------------------------------------------------------------------------------------
  930. #pragma mark ===== LocalFile =====
  931. #pragma --------------------------------------------------------------------------------------------
  932. + (void)addLocalFile:(CCMetadata *)metadata activeAccount:(NSString *)activeAccount
  933. {
  934. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  935. BOOL offline = NO;
  936. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", activeAccount, metadata.fileID];
  937. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  938. if (record) {
  939. offline = [[record valueForKey:@"offline"] boolValue];
  940. [record MR_deleteEntityInContext:localContext];
  941. }
  942. record = [TableLocalFile MR_createEntityInContext:localContext];
  943. record.account = activeAccount;
  944. record.date = metadata.date;
  945. record.fileID = metadata.fileID;
  946. record.exifDate = [NSDate date];
  947. record.exifLatitude = @"-1";
  948. record.exifLongitude = @"-1";
  949. record.offline = [NSNumber numberWithBool:offline];
  950. record.fileName = metadata.fileName;
  951. record.fileNamePrint = metadata.fileNamePrint;
  952. record.rev = metadata.rev;
  953. record.size = [NSNumber numberWithLong:metadata.size];
  954. }];
  955. }
  956. + (void)deleteLocalFileWithPredicate:(NSPredicate *)predicate
  957. {
  958. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  959. [TableLocalFile MR_deleteAllMatchingPredicate:predicate inContext:localContext];
  960. }];
  961. }
  962. + (void)renameLocalFileWithFileID:(NSString *)fileID fileNameTo:(NSString *)fileNameTo fileNamePrintTo:(NSString *)fileNamePrintTo activeAccount:(NSString *)activeAccount
  963. {
  964. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  965. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, activeAccount];
  966. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  967. if (record) {
  968. if (fileNameTo)record.fileName = fileNameTo;
  969. if (fileNamePrintTo)record.fileNamePrint = fileNamePrintTo;
  970. }
  971. }];
  972. }
  973. + (void)updateLocalFileModel:(CCMetadata *)metadata activeAccount:(NSString *)activeAccount
  974. {
  975. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  976. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileName == %@)", activeAccount, metadata.fileName];
  977. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  978. if (record) {
  979. record.fileID = metadata.fileID;
  980. record.date = metadata.date;
  981. record.fileNamePrint = metadata.fileNamePrint;
  982. } else {
  983. [self addLocalFile:metadata activeAccount:activeAccount];
  984. }
  985. }];
  986. }
  987. + (TableLocalFile *)getLocalFileWithFileID:(NSString *)fileID activeAccount:(NSString *)activeAccount
  988. {
  989. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, activeAccount];
  990. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate];
  991. if (record) {
  992. return record;
  993. } else return nil;
  994. }
  995. + (NSMutableArray *)getTableLocalFileWithPredicate:(NSPredicate *)predicate controlZombie:(BOOL)controlZombie activeAccount:(NSString *)activeAccount directoryUser:(NSString *)directoryUser
  996. {
  997. NSMutableArray *ritorno = [[NSMutableArray alloc] init];
  998. NSArray *records = [TableLocalFile MR_findAllWithPredicate:predicate];
  999. if ([records count] > 0) {
  1000. // verifichiamo esistano tutti i file altrimenti rimuoviamo il record
  1001. for(TableLocalFile *record in records){
  1002. if (controlZombie) {
  1003. NSString *fileID = record.fileID;
  1004. NSString *FilePathFileID = [NSString stringWithFormat:@"%@/%@", directoryUser, fileID];
  1005. NSString *FilePathFileName = [NSString stringWithFormat:@"%@/%@", directoryUser, record.fileName];
  1006. if (![[NSFileManager defaultManager] fileExistsAtPath:FilePathFileID] && ![[NSFileManager defaultManager] fileExistsAtPath:FilePathFileName] && controlZombie) {
  1007. // non esiste nè il file fileID e nemmeno il plist, eliminiamolo.
  1008. [self deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, activeAccount]];
  1009. } else [ritorno addObject:record];
  1010. } else [ritorno addObject:record];
  1011. }
  1012. } else return nil;
  1013. if ([ritorno count] == 0) return nil;
  1014. else return ritorno;
  1015. }
  1016. + (NSArray *)getTableLocalFileWithPredicate:(NSPredicate *)predicate
  1017. {
  1018. return [TableLocalFile MR_findAllWithPredicate:predicate];
  1019. }
  1020. #pragma --------------------------------------------------------------------------------------------
  1021. #pragma mark ===== Offline LocalFile =====
  1022. #pragma --------------------------------------------------------------------------------------------
  1023. + (void)setOfflineLocalFileID:(NSString *)fileID offline:(BOOL)offline activeAccount:(NSString *)activeAccount
  1024. {
  1025. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  1026. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, activeAccount];
  1027. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  1028. if (record)
  1029. record.offline = [NSNumber numberWithBool:offline];
  1030. }];
  1031. }
  1032. + (BOOL)isOfflineLocalFileID:(NSString *)fileID activeAccount:(NSString *)activeAccount
  1033. {
  1034. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (offline == 1) AND (account == %@)", fileID, activeAccount];
  1035. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate];
  1036. if (record) return YES;
  1037. else return NO;
  1038. }
  1039. + (NSArray *)getOfflineLocalFileActiveAccount:(NSString *)activeAccount directoryUser:(NSString *)directoryUser
  1040. {
  1041. NSMutableArray *metadatas = [NSMutableArray new];
  1042. NSArray *files = [self getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (offline == 1)", activeAccount] controlZombie:YES activeAccount:activeAccount directoryUser:directoryUser];
  1043. for (TableLocalFile *file in files) {
  1044. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", file.fileID, activeAccount];
  1045. CCMetadata *metadata = [self getMetadataWithPreficate:predicate context:nil];
  1046. if (metadata) {
  1047. // verify if is not on directory offline
  1048. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (offline == 1) AND (account == %@)", metadata.directoryID, activeAccount];
  1049. TableDirectory *directory = [TableDirectory MR_findFirstWithPredicate:predicate];
  1050. if (!directory)
  1051. [metadatas addObject:metadata];
  1052. }
  1053. }
  1054. return metadatas;
  1055. }
  1056. #pragma --------------------------------------------------------------------------------------------
  1057. #pragma mark ===== GeoInformation =====
  1058. #pragma --------------------------------------------------------------------------------------------
  1059. + (NSArray *)getGeoInformationLocalFromFileID:(NSString *)fileID activeAccount:(NSString *)activeAccount
  1060. {
  1061. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, activeAccount];
  1062. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate];
  1063. if (record) return [[NSArray alloc] initWithObjects:record.exifDate, record.exifLatitude, record.exifLongitude, nil];
  1064. else return nil;
  1065. }
  1066. + (void)setGeoInformationLocalFromFileID:(NSString *)fileID exifDate:(NSDate *)exifDate exifLatitude:(NSString *)exifLatitude exifLongitude:(NSString *)exifLongitude activeAccount:(NSString *)activeAccount
  1067. {
  1068. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  1069. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", activeAccount, fileID];
  1070. TableLocalFile *record = [TableLocalFile MR_findFirstWithPredicate:predicate inContext:localContext];
  1071. if (record) {
  1072. record.exifDate = exifDate;
  1073. record.exifLatitude = exifLatitude;
  1074. record.exifLongitude = exifLongitude;
  1075. }
  1076. }];
  1077. }
  1078. + (void)setGeoInformationLocalNull
  1079. {
  1080. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  1081. NSArray *records = [TableLocalFile MR_findAllInContext:localContext];
  1082. for (TableLocalFile *record in records) {
  1083. if ([record.exifLatitude doubleValue] != 0 || [record.exifLongitude doubleValue] != 0) {
  1084. record.exifLatitude = @"9999";
  1085. record.exifLongitude = @"9999";
  1086. }
  1087. }
  1088. }];
  1089. }
  1090. #pragma --------------------------------------------------------------------------------------------
  1091. #pragma mark ===== Automatic Upload =====
  1092. #pragma --------------------------------------------------------------------------------------------
  1093. + (BOOL)addTableAutomaticUpload:(CCMetadataNet *)metadataNet account:(NSString *)account
  1094. {
  1095. NSManagedObjectContext *context = [NSManagedObjectContext MR_context];
  1096. TableAutomaticUpload *record = nil;
  1097. // Record exists ?
  1098. record = [TableAutomaticUpload MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (assetLocalIdentifier == %@)", account, metadataNet.assetLocalIdentifier] inContext:context];
  1099. if (record)
  1100. return NO;
  1101. record = [TableAutomaticUpload MR_createEntityInContext:context];
  1102. record.account = account;
  1103. record.assetLocalIdentifier = metadataNet.assetLocalIdentifier;
  1104. record.lock = [NSNumber numberWithBool:NO];
  1105. record.date = [NSDate date];
  1106. record.fileName = metadataNet.fileName;
  1107. record.selector = metadataNet.selector;
  1108. record.selectorPost = metadataNet.selectorPost;
  1109. record.serverUrl = metadataNet.serverUrl;
  1110. record.session = metadataNet.session;
  1111. record.priority = [NSNumber numberWithLong:metadataNet.priority];
  1112. [context MR_saveToPersistentStoreAndWait];
  1113. return YES;
  1114. }
  1115. + (CCMetadataNet *)getTableAutomaticUploadForAccount:(NSString *)account selector:(NSString *)selector
  1116. {
  1117. NSManagedObjectContext *context = [NSManagedObjectContext MR_context];
  1118. TableAutomaticUpload *record = [TableAutomaticUpload MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (selector == %@) AND (lock == 0)", account, selector] inContext:context];
  1119. if (record) {
  1120. CCMetadataNet *metadataNet = [CCMetadataNet new];
  1121. metadataNet.action = actionUploadAsset; // Default
  1122. metadataNet.assetLocalIdentifier = record.assetLocalIdentifier;
  1123. metadataNet.fileName = record.fileName;
  1124. metadataNet.priority = [record.priority longValue];
  1125. metadataNet.selector = record.selector;
  1126. metadataNet.selectorPost = record.selectorPost;
  1127. metadataNet.serverUrl = record.serverUrl;
  1128. metadataNet.session = record.session;
  1129. metadataNet.taskStatus = k_taskStatusResume; // Default
  1130. // LOCK
  1131. record.lock = [NSNumber numberWithBool:YES];
  1132. [context MR_saveToPersistentStoreAndWait];
  1133. return metadataNet;
  1134. }
  1135. return nil;
  1136. }
  1137. + (NSArray *)getAllLockTableAutomaticUploadForAccount:(NSString *)account
  1138. {
  1139. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (lock == 1)", account];
  1140. return [TableAutomaticUpload MR_findAllWithPredicate:predicate];
  1141. }
  1142. + (void)unlockTableAutomaticUploadForAccount:(NSString *)account assetLocalIdentifier:(NSString *)assetLocalIdentifier
  1143. {
  1144. NSManagedObjectContext *context = [NSManagedObjectContext MR_context];
  1145. TableAutomaticUpload *record = [TableAutomaticUpload MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (assetLocalIdentifier == %@)", account, assetLocalIdentifier] inContext:context];
  1146. if (record) {
  1147. // UN-LOCK
  1148. record.lock = [NSNumber numberWithBool:NO];
  1149. [context MR_saveToPersistentStoreAndWait];
  1150. }
  1151. }
  1152. + (void)deleteTableAutomaticUploadForAccount:(NSString *)account assetLocalIdentifier:(NSString *)assetLocalIdentifier
  1153. {
  1154. NSManagedObjectContext *context = [NSManagedObjectContext MR_context];
  1155. TableAutomaticUpload *record = [TableAutomaticUpload MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (assetLocalIdentifier == %@)", account, assetLocalIdentifier] inContext:context];
  1156. if (record) {
  1157. [record MR_deleteEntityInContext:context];
  1158. [context MR_saveToPersistentStoreAndWait];
  1159. }
  1160. }
  1161. + (NSUInteger)countTableAutomaticUploadForAccount:(NSString *)account selector:(NSString *)selector
  1162. {
  1163. if (selector)
  1164. return [TableAutomaticUpload MR_countOfEntitiesWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (selector == %@)", account, selector]];
  1165. else
  1166. return [TableAutomaticUpload MR_countOfEntitiesWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", account]];
  1167. }
  1168. #pragma --------------------------------------------------------------------------------------------
  1169. #pragma mark ===== GPS =====
  1170. #pragma --------------------------------------------------------------------------------------------
  1171. + (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
  1172. {
  1173. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1174. TableGPS *record = [TableGPS MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(latitude == %@) AND (longitude == %@)", latitude, longitude] inContext:context];
  1175. if (!record) {
  1176. record = [TableGPS MR_createEntityInContext:context];
  1177. }
  1178. record.latitude = latitude;
  1179. record.longitude = longitude;
  1180. if (location) record.location = location;
  1181. if (placemarkAdministrativeArea) record.placemarkAdministrativeArea = placemarkAdministrativeArea;
  1182. if (placemarkCountry) record.placemarkCountry = placemarkCountry;
  1183. if (placemarkLocality) record.placemarkLocality = placemarkLocality;
  1184. if (placemarkPostalCode) record.placemarkPostalCode = placemarkPostalCode;
  1185. if (placemarkThoroughfare) record.placemarkThoroughfare = placemarkThoroughfare;
  1186. [context MR_saveToPersistentStoreAndWait];
  1187. }
  1188. + (NSString *)getLocationFromGeoLatitude:(NSString *)latitude longitude:(NSString *)longitude
  1189. {
  1190. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1191. TableGPS *record = [TableGPS MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"(latitude == %@) AND (longitude == %@)", latitude, longitude] inContext:context];
  1192. if (record) return record.location;
  1193. else return nil;
  1194. }
  1195. #pragma --------------------------------------------------------------------------------------------
  1196. #pragma mark ===== Share =====
  1197. #pragma --------------------------------------------------------------------------------------------
  1198. + (void)setShareLink:(NSString *)share fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl sharesLink:(NSMutableDictionary *)sharesLink activeAccount:(NSString *)activeAccount
  1199. {
  1200. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1201. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileName == %@) AND (serverUrl == %@)", activeAccount, fileName, serverUrl];
  1202. TableShare *record = [TableShare MR_findFirstWithPredicate:predicate inContext:context];
  1203. if (record) {
  1204. record.shareLink = share;
  1205. } else {
  1206. TableShare *record = [TableShare MR_createEntityInContext:context];
  1207. record.account = activeAccount;
  1208. record.fileName = fileName;
  1209. record.serverUrl = serverUrl;
  1210. record.shareLink = share;
  1211. }
  1212. [context MR_saveToPersistentStoreAndWait];
  1213. if (share && serverUrl && fileName)
  1214. [sharesLink setObject:share forKey:[serverUrl stringByAppendingString:fileName]];
  1215. }
  1216. + (void)setShareUserAndGroup:(NSString *)share fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup activeAccount:(NSString *)activeAccount
  1217. {
  1218. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1219. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileName == %@) AND (serverUrl == %@)", activeAccount, fileName, serverUrl];
  1220. TableShare *record = [TableShare MR_findFirstWithPredicate:predicate inContext:context];
  1221. if (record) {
  1222. record.shareUserAndGroup = share;
  1223. } else {
  1224. TableShare *record = [TableShare MR_createEntityInContext:context];
  1225. record.account = activeAccount;
  1226. record.fileName = fileName;
  1227. record.serverUrl = serverUrl;
  1228. record.shareUserAndGroup = share;
  1229. }
  1230. [context MR_saveToPersistentStoreAndWait];
  1231. [sharesUserAndGroup setObject:share forKey:[serverUrl stringByAppendingString:fileName]];
  1232. }
  1233. + (void)unShare:(NSString *)share fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl sharesLink:(NSMutableDictionary *)sharesLink sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup activeAccount:(NSString *)activeAccount
  1234. {
  1235. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1236. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND ((shareLink CONTAINS %@) OR (shareUserAndGroup CONTAINS %@))", activeAccount, share, share];
  1237. TableShare *record = [TableShare MR_findFirstWithPredicate:predicate inContext:context];
  1238. if (record) {
  1239. if ([record.shareLink containsString:share]) record.shareLink = @"";
  1240. if ([record.shareUserAndGroup containsString:share]) {
  1241. NSMutableArray *shares = [[NSMutableArray alloc] initWithArray:[record.shareUserAndGroup componentsSeparatedByString:@","]];
  1242. [shares removeObject:share];
  1243. record.shareUserAndGroup = [shares componentsJoinedByString:@","];
  1244. }
  1245. if ([record.shareLink length] == 0 && [record.shareUserAndGroup length] == 0)
  1246. [record MR_deleteEntityInContext:context];
  1247. [context MR_saveToPersistentStoreAndWait];
  1248. if ([record.shareLink length] > 0) [sharesLink setObject:record.shareLink forKey:[serverUrl stringByAppendingString:fileName]];
  1249. else [sharesLink removeObjectForKey:[serverUrl stringByAppendingString:fileName]];
  1250. if ([record.shareUserAndGroup length] > 0) [sharesUserAndGroup setObject:record.shareUserAndGroup forKey:[serverUrl stringByAppendingString:fileName]];
  1251. else [sharesUserAndGroup removeObjectForKey:[serverUrl stringByAppendingString:fileName]];
  1252. }
  1253. }
  1254. + (void)removeAllShareActiveAccount:(NSString *)activeAccount sharesLink:(NSMutableDictionary *)sharesLink sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup
  1255. {
  1256. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1257. [TableShare MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"account == %@", activeAccount] inContext:context];
  1258. [context MR_saveToPersistentStoreAndWait];
  1259. [sharesLink removeAllObjects];
  1260. [sharesUserAndGroup removeAllObjects];
  1261. }
  1262. + (void)updateShare:(NSDictionary *)items sharesLink:(NSMutableDictionary *)sharesLink sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup activeAccount:(NSString *)activeAccount activeUrl:(NSString *)activeUrl
  1263. {
  1264. // rimuovi tutte le condivisioni
  1265. [self removeAllShareActiveAccount:activeAccount sharesLink:sharesLink sharesUserAndGroup:sharesUserAndGroup];
  1266. NSMutableArray *itemsLink = [[NSMutableArray alloc] init];
  1267. NSMutableArray *itemsUsersAndGroups = [[NSMutableArray alloc] init];
  1268. for (NSString *idRemoteShared in items) {
  1269. OCSharedDto *item = [items objectForKey:idRemoteShared];
  1270. if (item.shareType == shareTypeLink) [itemsLink addObject:item];
  1271. if ([[item shareWith] length] > 0 && (item.shareType == shareTypeUser || item.shareType == shareTypeGroup || item.shareType == shareTypeRemote)) [itemsUsersAndGroups addObject:item];
  1272. }
  1273. // Link
  1274. for (OCSharedDto *item in itemsLink) {
  1275. NSString *fullPath = [[CCUtility getHomeServerUrlActiveUrl:activeUrl] stringByAppendingString:item.path];
  1276. NSString *fileName = [fullPath lastPathComponent];
  1277. NSString *serverUrl = [fullPath substringToIndex:([fullPath length]-[fileName length]-1)];
  1278. if ([serverUrl hasSuffix:@"/"]) serverUrl = [serverUrl substringToIndex:[serverUrl length] - 1];
  1279. if ([@(item.idRemoteShared) stringValue])
  1280. [self setShareLink:[@(item.idRemoteShared) stringValue] fileName:fileName serverUrl:serverUrl sharesLink:sharesLink activeAccount:activeAccount];
  1281. }
  1282. // Condivisioni
  1283. NSMutableDictionary *paths = [[NSMutableDictionary alloc] init];
  1284. // Creazione dizionario
  1285. for (OCSharedDto *item in itemsUsersAndGroups) {
  1286. if ([paths objectForKey:item.path]) {
  1287. NSMutableArray *share = [paths objectForKey:item.path];
  1288. [share addObject:[@(item.idRemoteShared) stringValue]];
  1289. [paths setObject:share forKey:item.path];
  1290. } else {
  1291. NSMutableArray *share = [[NSMutableArray alloc] initWithObjects:[@(item.idRemoteShared) stringValue], nil];
  1292. [paths setObject:share forKey:item.path];
  1293. }
  1294. }
  1295. // Scrittura su DB
  1296. for (NSString *path in paths) {
  1297. NSArray *items = [paths objectForKey:path];
  1298. NSString *share = [items componentsJoinedByString:@","];
  1299. NSLog(@"[LOG] share %@", share);
  1300. NSString *fullPath = [[CCUtility getHomeServerUrlActiveUrl:activeUrl] stringByAppendingString:path];
  1301. NSString *fileName = [fullPath lastPathComponent];
  1302. NSString *serverUrl = [fullPath substringToIndex:([fullPath length]-[fileName length]-1)];
  1303. if ([serverUrl hasSuffix:@"/"]) serverUrl = [serverUrl substringToIndex:[serverUrl length] - 1];
  1304. if (share)
  1305. [self setShareUserAndGroup:share fileName:fileName serverUrl:serverUrl sharesUserAndGroup:sharesUserAndGroup activeAccount:activeAccount];
  1306. }
  1307. }
  1308. + (void)populateSharesVariableFromDBActiveAccount:(NSString *)activeAccount sharesLink:(NSMutableDictionary *)sharesLink sharesUserAndGroup:(NSMutableDictionary *)sharesUserAndGroup
  1309. {
  1310. [sharesLink removeAllObjects];
  1311. [sharesUserAndGroup removeAllObjects];
  1312. NSArray *records = [TableShare MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", activeAccount]];
  1313. for (TableShare *record in records) {
  1314. if ([record.shareLink length] > 0 && record.serverUrl && record.fileName)
  1315. [sharesLink setObject:record.shareLink forKey:[record.serverUrl stringByAppendingString:record.fileName]];
  1316. if ([record.shareUserAndGroup length] > 0 && record.serverUrl && record.fileName)
  1317. [sharesUserAndGroup setObject:record.shareUserAndGroup forKey:[record.serverUrl stringByAppendingString:record.fileName]];
  1318. }
  1319. return;
  1320. }
  1321. #pragma --------------------------------------------------------------------------------------------
  1322. #pragma mark ===== Offline =====
  1323. #pragma --------------------------------------------------------------------------------------------
  1324. + (NSArray *)getHomeOfflineActiveAccount:(NSString *)activeAccount directoryUser:(NSString *)directoryUser fieldOrder:(NSString *)fieldOrder ascending:(BOOL)ascending
  1325. {
  1326. NSMutableArray *tableMetadatas = [NSMutableArray new];
  1327. NSArray *directoriesOffline = [self getOfflineDirectoryActiveAccount:activeAccount];
  1328. NSString *father = @"";
  1329. NSSortDescriptor *descriptor;
  1330. // Add directory
  1331. for (TableDirectory *directory in directoriesOffline) {
  1332. if (![directory.serverUrl containsString:father]) {
  1333. father = directory.serverUrl;
  1334. NSString *upDir = [CCUtility deletingLastPathComponentFromServerUrl:father];
  1335. NSString *directoryID = [self getDirectoryIDFromServerUrl:upDir activeAccount:activeAccount];
  1336. NSString *fileName = [father lastPathComponent];
  1337. if (upDir && directoryID && fileName) {
  1338. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(directoryID == %@) AND (account == %@) AND (directory == 1) AND (fileNameData == %@)", directoryID, activeAccount, fileName];
  1339. TableMetadata *tableMetadata = [self getTableMetadataWithPreficate:predicate];
  1340. if (tableMetadata)
  1341. [tableMetadatas addObject:tableMetadata];
  1342. }
  1343. }
  1344. }
  1345. // Add files
  1346. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (offline == 1)", activeAccount];
  1347. NSArray *localFiles = [CCCoreData getTableLocalFileWithPredicate:predicate];
  1348. for (TableLocalFile *localFile in localFiles) {
  1349. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@) AND (fileID == %@)", activeAccount, localFile.fileID];
  1350. TableMetadata *tableMetadata = [self getTableMetadataWithPreficate:predicate];
  1351. if (tableMetadata)
  1352. [tableMetadatas addObject:tableMetadata];
  1353. }
  1354. // Order
  1355. if ([fieldOrder isEqualToString:@"fileName"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"fileNamePrint" ascending:ascending selector:@selector(localizedCaseInsensitiveCompare:)];
  1356. else if ([fieldOrder isEqualToString:@"fileDate"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:ascending selector:nil];
  1357. else if ([fieldOrder isEqualToString:@"sessionTaskIdentifier"]) descriptor = [[NSSortDescriptor alloc] initWithKey:@"sessionTaskIdentifier" ascending:ascending selector:nil];
  1358. else descriptor = [[NSSortDescriptor alloc] initWithKey:fieldOrder ascending:ascending selector:@selector(localizedCaseInsensitiveCompare:)];
  1359. return [tableMetadatas sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];//[NSArray arrayWithArray:tableMetadatas];
  1360. }
  1361. #pragma --------------------------------------------------------------------------------------------
  1362. #pragma mark ===== Activity =====
  1363. #pragma --------------------------------------------------------------------------------------------
  1364. + (void)addActivityServer:(OCActivity *)activity account:(NSString *)account
  1365. {
  1366. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  1367. if (activity.idActivity != 0)
  1368. [TableActivity MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@) AND (idActivity == %d)", account, activity.idActivity] inContext:localContext];
  1369. TableActivity *record = [TableActivity MR_createEntityInContext:localContext];
  1370. record.account = account;
  1371. record.action = @"Activity";
  1372. record.date = activity.date;
  1373. record.file = activity.file;
  1374. record.fileID = @"";
  1375. record.idActivity = [NSNumber numberWithInteger:activity.idActivity];
  1376. record.link = activity.link;
  1377. record.note = activity.subject;
  1378. record.selector = @"";
  1379. record.type = k_activityTypeInfo;
  1380. record.verbose = [NSNumber numberWithInteger:k_activityVerboseDefault];
  1381. }];
  1382. }
  1383. + (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
  1384. {
  1385. note = [note stringByReplacingOccurrencesOfString:[activeUrl stringByAppendingString:webDAV] withString:@""];
  1386. note = [note stringByReplacingOccurrencesOfString:[k_domain_session_queue stringByAppendingString:@"."] withString:@""];
  1387. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  1388. TableActivity *record = [TableActivity MR_createEntityInContext:localContext];
  1389. if (!account) record.account = @"";
  1390. else record.account = account;
  1391. record.action = action;
  1392. record.date = [NSDate date];
  1393. record.file = file;
  1394. record.fileID = fileID;
  1395. record.idActivity = 0;
  1396. record.link = @"";
  1397. record.note = note;
  1398. record.selector = selector;
  1399. record.type = type;
  1400. record.verbose = [NSNumber numberWithInteger:verbose];
  1401. }];
  1402. }
  1403. + (NSArray *)getAllTableActivityWithPredicate:(NSPredicate *)predicate
  1404. {
  1405. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1406. NSArray *records = [TableActivity MR_findAllWithPredicate:predicate inContext:context];
  1407. if ([records count] == 0) return nil;
  1408. NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO selector:nil];
  1409. return [records sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
  1410. }
  1411. #pragma --------------------------------------------------------------------------------------------
  1412. #pragma mark ===== External Sites =====
  1413. #pragma --------------------------------------------------------------------------------------------
  1414. + (void)addExternalSites:(OCExternalSites *)externalSites account:(NSString *)account
  1415. {
  1416. [MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
  1417. TableExternalSites *record = [TableExternalSites MR_createEntityInContext:localContext];
  1418. record.account = account;
  1419. record.idExternalSite = [NSNumber numberWithInteger:externalSites.idExternalSite];
  1420. record.icon = externalSites.icon;
  1421. record.lang = externalSites.lang;
  1422. record.name = externalSites.name;
  1423. record.url = externalSites.url;
  1424. record.type = externalSites.type;
  1425. }];
  1426. }
  1427. + (void)deleteAllExternalSitesForAccount:(NSString *)account
  1428. {
  1429. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1430. [TableExternalSites MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"(account == %@)", account] inContext:context];
  1431. [context MR_saveToPersistentStoreAndWait];
  1432. }
  1433. + (NSArray *)getAllTableExternalSitesWithPredicate:(NSPredicate *)predicate
  1434. {
  1435. NSManagedObjectContext *context = [NSManagedObjectContext MR_defaultContext];
  1436. NSArray *records = [TableExternalSites MR_findAllWithPredicate:predicate inContext:context];
  1437. if ([records count] == 0) return nil;
  1438. NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"idExternalSite" ascending:YES selector:nil];
  1439. return [records sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
  1440. }
  1441. #pragma --------------------------------------------------------------------------------------------
  1442. #pragma mark ===== File System =====
  1443. #pragma --------------------------------------------------------------------------------------------
  1444. + (BOOL)downloadFile:(CCMetadata *)metadata directoryUser:(NSString *)directoryUser activeAccount:(NSString *)activeAccount
  1445. {
  1446. // ----------------------------------------- FILESYSTEM ------------------------------------------
  1447. // if encrypted, rewrite
  1448. if (metadata.cryptated == YES)
  1449. if ([[CCCrypto sharedManager] decrypt:metadata.fileID fileNameDecrypted:metadata.fileID fileNamePrint:metadata.fileNamePrint password:[[CCCrypto sharedManager] getKeyPasscode:metadata.uuid] directoryUser:directoryUser] == 0) return NO;
  1450. // ------------------------------------------ COREDATA -------------------------------------------
  1451. // add/update Table Local File
  1452. [self addLocalFile:metadata activeAccount:activeAccount];
  1453. // EXIF
  1454. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image])
  1455. [CCExifGeo setExifLocalTableFileID:metadata directoryUser:directoryUser activeAccount:activeAccount];
  1456. // Icon
  1457. [CCGraphics createNewImageFrom:metadata.fileID directoryUser:directoryUser fileNameTo:metadata.fileID fileNamePrint:metadata.fileNamePrint size:@"m" imageForUpload:NO typeFile:metadata.typeFile writePreview:YES optimizedFileName:[CCUtility getOptimizedPhoto]];
  1458. return YES;
  1459. }
  1460. + (void)downloadFilePlist:(CCMetadata *)metadata activeAccount:(NSString *)activeAccount activeUrl:(NSString *)activeUrl directoryUser:(NSString *)directoryUser
  1461. {
  1462. // inseriamo le info nel plist
  1463. [CCUtility insertInformationPlist:metadata directoryUser:directoryUser];
  1464. // aggiorniamo il CCMetadata
  1465. [self updateMetadata:metadata predicate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", metadata.fileID, activeAccount] activeAccount:activeAccount activeUrl:activeUrl context:nil];
  1466. // se è un template aggiorniamo anche nel FileSystem
  1467. if ([metadata.type isEqualToString: k_metadataType_template]){
  1468. [self updateLocalFileModel:metadata activeAccount:activeAccount];
  1469. }
  1470. }
  1471. + (void)deleteFile:(CCMetadata *)metadata serverUrl:(NSString *)serverUrl directoryUser:(NSString *)directoryUser activeAccount:(NSString *)activeAccount
  1472. {
  1473. // ----------------------------------------- FILESYSTEM ------------------------------------------
  1474. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", directoryUser, metadata.fileID] error:nil];
  1475. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.ico", directoryUser, metadata.fileID] error:nil];
  1476. // ------------------------------------------ COREDATA -------------------------------------------
  1477. [self deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", metadata.fileID, activeAccount]];
  1478. [self deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", metadata.fileID, activeAccount]];
  1479. // se è una directory cancelliamo tutto quello che è della directory
  1480. if (metadata.directory && serverUrl) {
  1481. NSString *dirForDelete = [CCUtility stringAppendServerUrl:serverUrl addFileName:metadata.fileNameData];
  1482. [self deleteDirectoryAndSubDirectory:dirForDelete activeAccount:activeAccount];
  1483. }
  1484. }
  1485. #pragma --------------------------------------------------------------------------------------------
  1486. #pragma mark ===== Metadata <> Entity =====
  1487. #pragma --------------------------------------------------------------------------------------------
  1488. + (void)insertMetadataInEntity:(CCMetadata *)metadata recordMetadata:(TableMetadata *)recordMetadata activeAccount:(NSString *)activeAccount activeUrl:(NSString *)activeUrl
  1489. {
  1490. if ([activeAccount length]) recordMetadata.account = activeAccount;
  1491. recordMetadata.cryptated = [NSNumber numberWithBool:metadata.cryptated];
  1492. if (metadata.date) recordMetadata.date = metadata.date;
  1493. recordMetadata.directory = [NSNumber numberWithBool:metadata.directory];
  1494. recordMetadata.errorPasscode = [NSNumber numberWithBool:metadata.errorPasscode];
  1495. recordMetadata.favorite = [NSNumber numberWithBool:metadata.favorite];
  1496. if ([metadata.fileID length]) recordMetadata.fileID = metadata.fileID;
  1497. if ([metadata.directoryID length]) recordMetadata.directoryID = metadata.directoryID;
  1498. if ([metadata.fileName length]) recordMetadata.fileName = metadata.fileName;
  1499. if ([metadata.fileName length]) recordMetadata.fileNameData = [CCUtility trasformedFileNamePlistInCrypto:metadata.fileName];
  1500. if ([metadata.fileNamePrint length]) recordMetadata.fileNamePrint = metadata.fileNamePrint;
  1501. if ([metadata.assetLocalIdentifier length]) recordMetadata.assetLocalIdentifier = metadata.assetLocalIdentifier;
  1502. if ([metadata.model length]) recordMetadata.model = metadata.model;
  1503. if ([metadata.nameCurrentDevice length]) recordMetadata.nameCurrentDevice = metadata.nameCurrentDevice;
  1504. if ([metadata.permissions length]) recordMetadata.permissions = metadata.permissions;
  1505. if ([metadata.protocol length]) recordMetadata.protocol = metadata.protocol;
  1506. if ([metadata.rev length]) recordMetadata.rev = metadata.rev;
  1507. if (metadata.session) recordMetadata.session = metadata.session;
  1508. else metadata.session = @"";
  1509. recordMetadata.sessionError = metadata.sessionError;
  1510. recordMetadata.sessionID = metadata.sessionID;
  1511. recordMetadata.sessionSelector = metadata.sessionSelector;
  1512. recordMetadata.sessionSelectorPost = metadata.sessionSelectorPost;
  1513. recordMetadata.sessionTaskIdentifier = [NSNumber numberWithInt:metadata.sessionTaskIdentifier];
  1514. recordMetadata.sessionTaskIdentifierPlist = [NSNumber numberWithInt:metadata.sessionTaskIdentifierPlist];
  1515. if (metadata.size) recordMetadata.size = [NSNumber numberWithLong:metadata.size];
  1516. if ([metadata.title length]) recordMetadata.title = metadata.title;
  1517. recordMetadata.thumbnailExists = [NSNumber numberWithBool:metadata.thumbnailExists];
  1518. if ([metadata.type length]) recordMetadata.type = metadata.type;
  1519. if ([metadata.uuid length]) recordMetadata.uuid = metadata.uuid;
  1520. // inseriamo il typeFile e icona di default.
  1521. [CCUtility insertTypeFileIconName:metadata directory:[self getServerUrlFromDirectoryID:metadata.directoryID activeAccount:activeAccount] cameraFolderName:[self getCameraUploadFolderNameActiveAccount:activeAccount] cameraFolderPath:[self getCameraUploadFolderPathActiveAccount:activeAccount activeUrl:activeUrl]];
  1522. recordMetadata.typeFile = metadata.typeFile;
  1523. recordMetadata.iconName = metadata.iconName;
  1524. }
  1525. + (CCMetadata *)insertEntityInMetadata:(TableMetadata *)recordMetadata
  1526. {
  1527. CCMetadata *metadata = [[CCMetadata alloc] init];
  1528. metadata.account = recordMetadata.account;
  1529. metadata.cryptated = [recordMetadata.cryptated boolValue];
  1530. metadata.date = recordMetadata.date;
  1531. metadata.directory = [recordMetadata.directory boolValue];
  1532. metadata.errorPasscode = [recordMetadata.errorPasscode boolValue];
  1533. metadata.favorite = [recordMetadata.favorite boolValue];
  1534. metadata.fileID = recordMetadata.fileID;
  1535. metadata.directoryID = recordMetadata.directoryID;
  1536. metadata.fileName = recordMetadata.fileName;
  1537. metadata.fileNameData = recordMetadata.fileNameData;
  1538. metadata.fileNamePrint = recordMetadata.fileNamePrint;
  1539. metadata.iconName = recordMetadata.iconName;
  1540. metadata.assetLocalIdentifier = recordMetadata.assetLocalIdentifier;
  1541. metadata.model = recordMetadata.model;
  1542. metadata.nameCurrentDevice = recordMetadata.nameCurrentDevice;
  1543. metadata.permissions = recordMetadata.permissions;
  1544. metadata.protocol = recordMetadata.protocol;
  1545. metadata.rev = recordMetadata.rev;
  1546. metadata.session = recordMetadata.session;
  1547. metadata.sessionError = recordMetadata.sessionError;
  1548. metadata.sessionID = recordMetadata.sessionID;
  1549. metadata.sessionSelector = recordMetadata.sessionSelector;
  1550. metadata.sessionSelectorPost = recordMetadata.sessionSelectorPost;
  1551. metadata.sessionTaskIdentifier = [recordMetadata.sessionTaskIdentifier intValue];
  1552. metadata.sessionTaskIdentifierPlist = [recordMetadata.sessionTaskIdentifierPlist intValue];
  1553. metadata.size = [recordMetadata.size longValue];
  1554. metadata.thumbnailExists = [recordMetadata.thumbnailExists boolValue];
  1555. metadata.title = recordMetadata.title;
  1556. metadata.type = recordMetadata.type;
  1557. metadata.typeFile = recordMetadata.typeFile;
  1558. metadata.uuid = recordMetadata.uuid;
  1559. return metadata;
  1560. }
  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