CCCoreData.m 94 KB

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