CCCoreData.m 88 KB

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