CCCoreData.m 86 KB

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