CCCoreData.m 101 KB

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