OCCommunication.m 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964
  1. //
  2. // OCCommunication.m
  3. // Owncloud iOs Client
  4. //
  5. // Copyright (C) 2016, ownCloud GmbH. ( http://www.owncloud.org/ )
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. //
  24. // Add : getNotificationServer & setNotificationServer
  25. // Add : getUserProfileServer
  26. // Add : Support for Favorite
  27. // Add : getActivityServer
  28. //
  29. // Author Marino Faggiana <m.faggiana@twsweb.it>
  30. // Copyright (c) 2017 TWS. All rights reserved.
  31. //
  32. #import "OCCommunication.h"
  33. #import "OCHTTPRequestOperation.h"
  34. #import "UtilsFramework.h"
  35. #import "OCXMLParser.h"
  36. #import "OCXMLSharedParser.h"
  37. #import "OCXMLServerErrorsParser.h"
  38. #import "OCXMLListParser.h"
  39. #import "NSString+Encode.h"
  40. #import "OCFrameworkConstants.h"
  41. #import "OCWebDAVClient.h"
  42. #import "OCXMLShareByLinkParser.h"
  43. #import "OCErrorMsg.h"
  44. #import "AFURLSessionManager.h"
  45. #import "OCShareUser.h"
  46. #import "OCActivity.h"
  47. #import "OCExternalSites.h"
  48. #import "OCCapabilities.h"
  49. #import "OCNotifications.h"
  50. #import "OCNotificationsAction.h"
  51. #import "OCRichObjectStrings.h"
  52. #import "OCUserProfile.h"
  53. @interface OCCommunication ()
  54. @property (nonatomic, strong) NSString *currentServerVersion;
  55. @end
  56. @implementation OCCommunication
  57. -(id) init {
  58. self = [super init];
  59. if (self) {
  60. //Init the Donwload queue array
  61. self.downloadTaskNetworkQueueArray = [NSMutableArray new];
  62. //Credentials not set yet
  63. self.kindOfCredential = credentialNotSet;
  64. [self setSecurityPolicyManagers:[self createSecurityPolicy]];
  65. self.isCookiesAvailable = YES;
  66. self.isForbiddenCharactersAvailable = NO;
  67. #ifdef UNIT_TEST
  68. self.uploadSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  69. self.downloadSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  70. self.networkSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  71. self.networkSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  72. #else
  73. //Network Upload queue for NSURLSession (iOS 7)
  74. NSURLSessionConfiguration *uploadConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:k_session_name];
  75. uploadConfiguration.HTTPShouldUsePipelining = YES;
  76. uploadConfiguration.HTTPMaximumConnectionsPerHost = 1;
  77. uploadConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  78. self.uploadSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:uploadConfiguration];
  79. [self.uploadSessionManager.operationQueue setMaxConcurrentOperationCount:1];
  80. //Network Download queue for NSURLSession (iOS 7)
  81. NSURLSessionConfiguration *downConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:k_download_session_name];
  82. downConfiguration.HTTPShouldUsePipelining = YES;
  83. downConfiguration.HTTPMaximumConnectionsPerHost = 1;
  84. downConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  85. self.downloadSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:downConfiguration];
  86. [self.downloadSessionManager.operationQueue setMaxConcurrentOperationCount:1];
  87. //Network Download queue for NSURLSession (iOS 7)
  88. NSURLSessionConfiguration *networkConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
  89. networkConfiguration.HTTPShouldUsePipelining = YES;
  90. networkConfiguration.HTTPMaximumConnectionsPerHost = 1;
  91. networkConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  92. self.networkSessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:networkConfiguration];
  93. [self.networkSessionManager.operationQueue setMaxConcurrentOperationCount:1];
  94. self.networkSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  95. #endif
  96. }
  97. return self;
  98. }
  99. -(id) initWithUploadSessionManager:(AFURLSessionManager *) uploadSessionManager {
  100. self = [super init];
  101. if (self) {
  102. //Init the Donwload queue array
  103. self.downloadTaskNetworkQueueArray = [NSMutableArray new];
  104. self.isCookiesAvailable = YES;
  105. self.isForbiddenCharactersAvailable = NO;
  106. //Credentials not set yet
  107. self.kindOfCredential = credentialNotSet;
  108. [self setSecurityPolicyManagers:[self createSecurityPolicy]];
  109. self.uploadSessionManager = uploadSessionManager;
  110. }
  111. return self;
  112. }
  113. -(id) initWithUploadSessionManager:(AFURLSessionManager *) uploadSessionManager andDownloadSessionManager:(AFURLSessionManager *) downloadSessionManager andNetworkSessionManager:(AFURLSessionManager *) networkSessionManager {
  114. self = [super init];
  115. if (self) {
  116. //Init the Donwload queue array
  117. self.downloadTaskNetworkQueueArray = [NSMutableArray new];
  118. //Credentials not set yet
  119. self.kindOfCredential = credentialNotSet;
  120. [self setSecurityPolicyManagers:[self createSecurityPolicy]];
  121. self.uploadSessionManager = uploadSessionManager;
  122. self.downloadSessionManager = downloadSessionManager;
  123. self.networkSessionManager = networkSessionManager;
  124. }
  125. return self;
  126. }
  127. - (AFSecurityPolicy *) createSecurityPolicy {
  128. return [AFSecurityPolicy defaultPolicy];
  129. }
  130. - (void)setSecurityPolicyManagers:(AFSecurityPolicy *)securityPolicy {
  131. self.securityPolicy = securityPolicy;
  132. self.uploadSessionManager.securityPolicy = securityPolicy;
  133. self.downloadSessionManager.securityPolicy = securityPolicy;
  134. }
  135. #pragma mark - Setting Credentials
  136. - (void) setCredentialsWithUser:(NSString*) user andPassword:(NSString*) password {
  137. self.kindOfCredential = credentialNormal;
  138. self.user = user;
  139. self.password = password;
  140. }
  141. - (void) setCredentialsWithCookie:(NSString*) cookie {
  142. self.kindOfCredential = credentialCookie;
  143. self.password = cookie;
  144. }
  145. - (void) setCredentialsOauthWithToken:(NSString*) token {
  146. self.kindOfCredential = credentialOauth;
  147. self.password = token;
  148. }
  149. ///-----------------------------------
  150. /// @name getRequestWithCredentials
  151. ///-----------------------------------
  152. /**
  153. * Method to return the request with the right credential
  154. *
  155. * @param OCWebDAVClient like a dinamic typed
  156. *
  157. * @return OCWebDAVClient like a dinamic typed
  158. *
  159. */
  160. - (id) getRequestWithCredentials:(id) request {
  161. if ([request isKindOfClass:[NSMutableURLRequest class]]) {
  162. NSMutableURLRequest *myRequest = (NSMutableURLRequest *)request;
  163. switch (self.kindOfCredential) {
  164. case credentialNotSet:
  165. //Without credentials
  166. break;
  167. case credentialNormal:
  168. {
  169. NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", self.user, self.password];
  170. [myRequest addValue:[NSString stringWithFormat:@"Basic %@", [UtilsFramework AFBase64EncodedStringFromString:basicAuthCredentials]] forHTTPHeaderField:@"Authorization"];
  171. break;
  172. }
  173. case credentialCookie:
  174. NSLog(@"Cookie: %@", self.password);
  175. [myRequest addValue:self.password forHTTPHeaderField:@"Cookie"];
  176. break;
  177. case credentialOauth:
  178. [myRequest addValue:[NSString stringWithFormat:@"Bearer %@", self.password] forHTTPHeaderField:@"Authorization"];
  179. break;
  180. default:
  181. break;
  182. }
  183. if (self.userAgent) {
  184. [myRequest addValue:self.userAgent forHTTPHeaderField:@"User-Agent"];
  185. }
  186. return myRequest;
  187. } else if([request isKindOfClass:[OCWebDAVClient class]]) {
  188. OCWebDAVClient *myRequest = (OCWebDAVClient *)request;
  189. switch (self.kindOfCredential) {
  190. case credentialNotSet:
  191. //Without credentials
  192. break;
  193. case credentialNormal:
  194. [myRequest setAuthorizationHeaderWithUsername:self.user password:self.password];
  195. break;
  196. case credentialCookie:
  197. [myRequest setAuthorizationHeaderWithCookie:self.password];
  198. break;
  199. case credentialOauth:
  200. [myRequest setAuthorizationHeaderWithToken:[NSString stringWithFormat:@"Bearer %@", self.password]];
  201. break;
  202. default:
  203. break;
  204. }
  205. if (self.userAgent) {
  206. [myRequest setUserAgent:self.userAgent];
  207. }
  208. return request;
  209. } else {
  210. NSLog(@"We do not know witch kind of object is");
  211. return request;
  212. }
  213. }
  214. #pragma mark - WebDav network Operations
  215. ///-----------------------------------
  216. /// @name Check Server
  217. ///-----------------------------------
  218. - (void) checkServer: (NSString *) path
  219. onCommunication:(OCCommunication *)sharedOCCommunication
  220. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest
  221. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  222. OCWebDAVClient *request = [OCWebDAVClient new];
  223. request = [self getRequestWithCredentials:request];
  224. if (self.userAgent) {
  225. [request setUserAgent:self.userAgent];
  226. }
  227. path = [path encodeString:NSUTF8StringEncoding];
  228. [request checkServer:path onCommunication:sharedOCCommunication
  229. success:^(NSHTTPURLResponse *response, id responseObject) {
  230. if (successRequest) {
  231. successRequest(response, request.redirectedServer);
  232. }
  233. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  234. failureRequest(response, error, request.redirectedServer);
  235. }];
  236. }
  237. ///-----------------------------------
  238. /// @name Create a folder
  239. ///-----------------------------------
  240. - (void) createFolder: (NSString *) path
  241. onCommunication:(OCCommunication *)sharedOCCommunication withForbiddenCharactersSupported:(BOOL)isFCSupported
  242. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest
  243. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest
  244. errorBeforeRequest:(void(^)(NSError *error)) errorBeforeRequest {
  245. if ([UtilsFramework isForbiddenCharactersInFileName:[UtilsFramework getFileNameOrFolderByPath:path] withForbiddenCharactersSupported:isFCSupported]) {
  246. NSError *error = [UtilsFramework getErrorByCodeId:OCErrorForbidenCharacters];
  247. errorBeforeRequest(error);
  248. } else {
  249. OCWebDAVClient *request = [OCWebDAVClient new];
  250. request = [self getRequestWithCredentials:request];
  251. path = [path encodeString:NSUTF8StringEncoding];
  252. [request makeCollection:path onCommunication:sharedOCCommunication
  253. success:^(NSHTTPURLResponse *response, id responseObject) {
  254. if (successRequest) {
  255. successRequest(response, request.redirectedServer);
  256. }
  257. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  258. OCXMLServerErrorsParser *serverErrorParser = [OCXMLServerErrorsParser new];
  259. [serverErrorParser startToParseWithData:responseData withCompleteBlock:^(NSError *err) {
  260. if (err) {
  261. failureRequest(response, err, request.redirectedServer);
  262. }else{
  263. failureRequest(response, error, request.redirectedServer);
  264. }
  265. }];
  266. }];
  267. }
  268. }
  269. ///-----------------------------------
  270. /// @name Move a file or a folder
  271. ///-----------------------------------
  272. - (void) moveFileOrFolder:(NSString *)sourcePath
  273. toDestiny:(NSString *)destinyPath
  274. onCommunication:(OCCommunication *)sharedOCCommunication withForbiddenCharactersSupported:(BOOL)isFCSupported
  275. successRequest:(void (^)(NSHTTPURLResponse *response, NSString *redirectServer))successRequest
  276. failureRequest:(void (^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer))failureRequest
  277. errorBeforeRequest:(void (^)(NSError *error))errorBeforeRequest {
  278. if ([UtilsFramework isTheSameFileOrFolderByNewURLString:destinyPath andOriginURLString:sourcePath]) {
  279. //We check that we are not trying to move the file to the same place
  280. NSError *error = [UtilsFramework getErrorByCodeId:OCErrorMovingTheDestinyAndOriginAreTheSame];
  281. errorBeforeRequest(error);
  282. } else if ([UtilsFramework isAFolderUnderItByNewURLString:destinyPath andOriginURLString:sourcePath]) {
  283. //We check we are not trying to move a folder inside himself
  284. NSError *error = [UtilsFramework getErrorByCodeId:OCErrorMovingFolderInsideHimself];
  285. errorBeforeRequest(error);
  286. } else if ([UtilsFramework isForbiddenCharactersInFileName:[UtilsFramework getFileNameOrFolderByPath:destinyPath] withForbiddenCharactersSupported:isFCSupported]) {
  287. //We check that we are making a move not a rename to prevent special characters problems
  288. NSError *error = [UtilsFramework getErrorByCodeId:OCErrorMovingDestinyNameHaveForbiddenCharacters];
  289. errorBeforeRequest(error);
  290. } else {
  291. sourcePath = [sourcePath encodeString:NSUTF8StringEncoding];
  292. destinyPath = [destinyPath encodeString:NSUTF8StringEncoding];
  293. OCWebDAVClient *request = [OCWebDAVClient new];
  294. request = [self getRequestWithCredentials:request];
  295. [request movePath:sourcePath toPath:destinyPath onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  296. if (successRequest) {
  297. successRequest(response, request.redirectedServer);
  298. }
  299. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  300. OCXMLServerErrorsParser *serverErrorParser = [OCXMLServerErrorsParser new];
  301. [serverErrorParser startToParseWithData:responseData withCompleteBlock:^(NSError *err) {
  302. if (err) {
  303. failureRequest(response, err, request.redirectedServer);
  304. }else{
  305. failureRequest(response, error, request.redirectedServer);
  306. }
  307. }];
  308. }];
  309. }
  310. }
  311. ///-----------------------------------
  312. /// @name Delete a file or a folder
  313. ///-----------------------------------
  314. - (void) deleteFileOrFolder:(NSString *)path
  315. onCommunication:(OCCommunication *)sharedOCCommunication
  316. successRequest:(void (^)(NSHTTPURLResponse *response, NSString *redirectedServer))successRequest
  317. failureRquest:(void (^)(NSHTTPURLResponse *resposne, NSError *error, NSString *redirectedServer))failureRequest {
  318. path = [path encodeString:NSUTF8StringEncoding];
  319. OCWebDAVClient *request = [OCWebDAVClient new];
  320. request = [self getRequestWithCredentials:request];
  321. [request deletePath:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  322. if (successRequest) {
  323. successRequest(response, request.redirectedServer);
  324. }
  325. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  326. failureRequest(response, error, request.redirectedServer);
  327. }];
  328. }
  329. ///-----------------------------------
  330. /// @name Read folder
  331. ///-----------------------------------
  332. - (void) readFolder: (NSString *) path depth:(NSString *)depth withUserSessionToken:(NSString *)token
  333. onCommunication:(OCCommunication *)sharedOCCommunication
  334. successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token)) successRequest
  335. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer)) failureRequest{
  336. if (!token){
  337. token = @"no token";
  338. }
  339. path = [path encodeString:NSUTF8StringEncoding];
  340. OCWebDAVClient *request = [OCWebDAVClient new];
  341. request = [self getRequestWithCredentials:request];
  342. [request listPath:path depth:depth onCommunication:sharedOCCommunication withUserSessionToken:token success:^(NSHTTPURLResponse *response, id responseObject, NSString *token) {
  343. if (successRequest) {
  344. NSData *responseData = (NSData*) responseObject;
  345. // NSString* newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  346. // NSLog(@"newStrReadFolder: %@", newStr);
  347. OCXMLParser *parser = [[OCXMLParser alloc]init];
  348. [parser initParserWithData:responseData];
  349. NSMutableArray *directoryList = [parser.directoryList mutableCopy];
  350. //Return success
  351. successRequest(response, directoryList, request.redirectedServer, token);
  352. }
  353. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error, NSString *token) {
  354. NSLog(@"Failure");
  355. failureRequest(response, error, token, request.redirectedServer);
  356. }];
  357. }
  358. ///-----------------------------------
  359. /// @name Download File Session
  360. ///-----------------------------------
  361. - (NSURLSessionDownloadTask *) downloadFileSession:(NSString *)remotePath toDestiny:(NSString *)localPath defaultPriority:(BOOL)defaultPriority onCommunication:(OCCommunication *)sharedOCCommunication progress:(void(^)(NSProgress *progress))downloadProgress successRequest:(void(^)(NSURLResponse *response, NSURL *filePath)) successRequest failureRequest:(void(^)(NSURLResponse *response, NSError *error)) failureRequest {
  362. OCWebDAVClient *request = [OCWebDAVClient new];
  363. request = [self getRequestWithCredentials:request];
  364. remotePath = [remotePath encodeString:NSUTF8StringEncoding];
  365. NSURLSessionDownloadTask *downloadTask = [request downloadWithSessionPath:remotePath toPath:localPath defaultPriority:defaultPriority onCommunication:sharedOCCommunication progress:^(NSProgress *progress) {
  366. downloadProgress(progress);
  367. } success:^(NSURLResponse *response, NSURL *filePath) {
  368. [UtilsFramework addCookiesToStorageFromResponse:(NSURLResponse *) response andPath:[NSURL URLWithString:remotePath]];
  369. successRequest(response,filePath);
  370. } failure:^(NSURLResponse *response, NSError *error) {
  371. [UtilsFramework addCookiesToStorageFromResponse:(NSURLResponse *) response andPath:[NSURL URLWithString:remotePath]];
  372. failureRequest(response,error);
  373. }];
  374. return downloadTask;
  375. }
  376. ///-----------------------------------
  377. /// @name Set Download Task Complete Block
  378. ///-----------------------------------
  379. - (void)setDownloadTaskComleteBlock: (NSURL * (^)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, NSURL *location))block{
  380. [self.downloadSessionManager setDownloadTaskDidFinishDownloadingBlock:block];
  381. }
  382. ///-----------------------------------
  383. /// @name Set Download Task Did Get Body Data Block
  384. ///-----------------------------------
  385. - (void) setDownloadTaskDidGetBodyDataBlock: (void(^)(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite)) block{
  386. [self.downloadSessionManager setDownloadTaskDidWriteDataBlock:^(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
  387. block(session,downloadTask,bytesWritten,totalBytesWritten,totalBytesExpectedToWrite);
  388. }];
  389. }
  390. ///-----------------------------------
  391. /// @name Upload File Session
  392. ///-----------------------------------
  393. - (NSURLSessionUploadTask *) uploadFileSession:(NSString *) localPath toDestiny:(NSString *) remotePath onCommunication:(OCCommunication *)sharedOCCommunication progress:(void(^)(NSProgress *progress))uploadProgress successRequest:(void(^)(NSURLResponse *response, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSURLResponse *response, NSString *redirectedServer, NSError *error)) failureRequest failureBeforeRequest:(void(^)(NSError *error)) failureBeforeRequest {
  394. OCWebDAVClient *request = [OCWebDAVClient new];
  395. request = [self getRequestWithCredentials:request];
  396. remotePath = [remotePath encodeString:NSUTF8StringEncoding];
  397. NSURLSessionUploadTask *uploadTask = [request putWithSessionLocalPath:localPath atRemotePath:remotePath onCommunication:sharedOCCommunication progress:^(NSProgress *progress) {
  398. uploadProgress(progress);
  399. } success:^(NSURLResponse *response, id responseObjec){
  400. [UtilsFramework addCookiesToStorageFromResponse:(NSURLResponse *) response andPath:[NSURL URLWithString:remotePath]];
  401. //TODO: The second parameter is the redirected server
  402. successRequest(response, @"");
  403. } failure:^(NSURLResponse *response, id responseObject, NSError *error) {
  404. [UtilsFramework addCookiesToStorageFromResponse:(NSURLResponse *) response andPath:[NSURL URLWithString:remotePath]];
  405. //TODO: The second parameter is the redirected server
  406. NSData *responseData = (NSData*) responseObject;
  407. OCXMLServerErrorsParser *serverErrorParser = [OCXMLServerErrorsParser new];
  408. [serverErrorParser startToParseWithData:responseData withCompleteBlock:^(NSError *err) {
  409. if (err) {
  410. failureRequest(response, @"", err);
  411. }else{
  412. failureRequest(response, @"", error);
  413. }
  414. }];
  415. } failureBeforeRequest:^(NSError *error) {
  416. failureBeforeRequest(error);
  417. }];
  418. return uploadTask;
  419. }
  420. ///-----------------------------------
  421. /// @name Set Task Did Complete Block
  422. ///-----------------------------------
  423. - (void) setTaskDidCompleteBlock: (void(^)(NSURLSession *session, NSURLSessionTask *task, NSError *error)) block{
  424. [self.uploadSessionManager setTaskDidCompleteBlock:^(NSURLSession *session, NSURLSessionTask *task, NSError *error) {
  425. block(session, task, error);
  426. }];
  427. }
  428. ///-----------------------------------
  429. /// @name Set Task Did Send Body Data Block
  430. ///-----------------------------------
  431. - (void) setTaskDidSendBodyDataBlock: (void(^)(NSURLSession *session, NSURLSessionTask *task, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend)) block{
  432. [self.uploadSessionManager setTaskDidSendBodyDataBlock:^(NSURLSession *session, NSURLSessionTask *task, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
  433. block(session, task, bytesSent, totalBytesSent, totalBytesExpectedToSend);
  434. }];
  435. }
  436. ///-----------------------------------
  437. /// @name Read File
  438. ///-----------------------------------
  439. - (void) readFile: (NSString *) path
  440. onCommunication:(OCCommunication *)sharedOCCommunication
  441. successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer)) successRequest
  442. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  443. path = [path encodeString:NSUTF8StringEncoding];
  444. OCWebDAVClient *request = [OCWebDAVClient new];
  445. request = [self getRequestWithCredentials:request];
  446. [request propertiesOfPath:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  447. if (successRequest) {
  448. NSData *responseData = (NSData*) responseObject;
  449. // NSString* newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  450. // NSLog(@"newStrReadFile: %@", newStr);
  451. OCXMLParser *parser = [[OCXMLParser alloc]init];
  452. [parser initParserWithData:responseData];
  453. NSMutableArray *directoryList = [parser.directoryList mutableCopy];
  454. //Return success
  455. successRequest(response, directoryList, request.redirectedServer);
  456. }
  457. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  458. failureRequest(response, error, request.redirectedServer);
  459. }];
  460. }
  461. ///-----------------------------------
  462. /// @name search
  463. ///-----------------------------------
  464. - (void)search:(NSString *)path folder:(NSString *)folder fileName:(NSString *)fileName depth:(NSString *)depth dateLastModified:(NSString *)dateLastModified withUserSessionToken:(NSString *)token onCommunication:(OCCommunication *)sharedOCCommunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer)) failureRequest{
  465. if (!token){
  466. token = @"no token";
  467. }
  468. path = [path encodeString:NSUTF8StringEncoding];
  469. OCWebDAVClient *request = [OCWebDAVClient new];
  470. request = [self getRequestWithCredentials:request];
  471. [request search:path folder:folder fileName:fileName depth:depth dateLastModified:dateLastModified user:_user onCommunication:sharedOCCommunication withUserSessionToken:token success:^(NSHTTPURLResponse *response, id responseObject, NSString *token) {
  472. if (successRequest) {
  473. NSData *responseData = (NSData*) responseObject;
  474. OCXMLListParser *parser = [OCXMLListParser new];
  475. [parser initParserWithData:responseData];
  476. NSMutableArray *searchList = [parser.searchList mutableCopy];
  477. //Return success
  478. successRequest(response, searchList, request.redirectedServer, token);
  479. }
  480. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error, NSString *token) {
  481. failureRequest(response, error, token, request.redirectedServer);
  482. }];
  483. }
  484. ///-----------------------------------
  485. /// @name Setting favorite
  486. ///-----------------------------------
  487. - (void)settingFavoriteServer:(NSString *)path andFileOrFolderPath:(NSString *)filePath favorite:(BOOL)favorite withUserSessionToken:(NSString *)token onCommunication:(OCCommunication *)sharedOCCommunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer, NSString *token)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer)) failureRequest {
  488. if (!token){
  489. token = @"no token";
  490. }
  491. path = [NSString stringWithFormat:@"%@/files/%@/%@", path, _user, filePath];
  492. path = [path encodeString:NSUTF8StringEncoding];
  493. OCWebDAVClient *request = [OCWebDAVClient new];
  494. request = [self getRequestWithCredentials:request];
  495. [request settingFavorite:path favorite:favorite onCommunication:sharedOCCommunication withUserSessionToken:token success:^(NSHTTPURLResponse *response, id responseObject, NSString *token) {
  496. if (successRequest) {
  497. //Return success
  498. successRequest(response, request.redirectedServer, token);
  499. }
  500. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error, NSString *token) {
  501. NSLog(@"Failure");
  502. failureRequest(response, error, token, request.redirectedServer);
  503. }];
  504. }
  505. ///-----------------------------------
  506. /// @name Listing favorites
  507. ///-----------------------------------
  508. - (void)listingFavorites:(NSString *)path folder:(NSString *)folder withUserSessionToken:(NSString *)token onCommunication:(OCCommunication *)sharedOCCommunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer)) failureRequest{
  509. if (!token){
  510. token = @"no token";
  511. }
  512. path = [path encodeString:NSUTF8StringEncoding];
  513. OCWebDAVClient *request = [OCWebDAVClient new];
  514. request = [self getRequestWithCredentials:request];
  515. [request listingFavorites:path folder:folder user:_user onCommunication:sharedOCCommunication withUserSessionToken:token success:^(NSHTTPURLResponse *response, id responseObject, NSString *token) {
  516. if (successRequest) {
  517. NSData *responseData = (NSData*) responseObject;
  518. OCXMLListParser *parser = [OCXMLListParser new];
  519. [parser initParserWithData:responseData];
  520. NSMutableArray *searchList = [parser.searchList mutableCopy];
  521. //Return success
  522. successRequest(response, searchList, request.redirectedServer, token);
  523. }
  524. } failure:^(NSHTTPURLResponse *response, id responseData, NSError *error, NSString *token) {
  525. failureRequest(response, error, token, request.redirectedServer);
  526. }];
  527. }
  528. #pragma mark - OC API Calls
  529. - (NSString *) getCurrentServerVersion {
  530. return self.currentServerVersion;
  531. }
  532. - (void) getServerVersionWithPath:(NSString*) path onCommunication:(OCCommunication *)sharedOCCommunication
  533. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *serverVersion, NSString *redirectedServer)) success
  534. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failure{
  535. OCWebDAVClient *request = [OCWebDAVClient new];
  536. if (self.userAgent) {
  537. [request setUserAgent:self.userAgent];
  538. }
  539. [request getStatusOfTheServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  540. NSData *data = (NSData*) responseObject;
  541. NSString *versionString = [NSString new];
  542. NSError* error=nil;
  543. if (data) {
  544. NSMutableDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &error];
  545. if(error) {
  546. NSLog(@"Error parsing JSON: %@", error);
  547. } else {
  548. //Obtain the server version from the version field
  549. versionString = [jsonArray valueForKey:@"version"];
  550. self.currentServerVersion = versionString;
  551. }
  552. } else {
  553. NSLog(@"Error parsing JSON: data is null");
  554. }
  555. success(response, versionString, request.redirectedServer);
  556. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  557. failure(response, error, request.redirectedServer);
  558. }];
  559. }
  560. ///-----------------------------------
  561. /// @name Get UserName by cookie
  562. ///-----------------------------------
  563. - (void) getUserNameByCookie:(NSString *) cookieString ofServerPath:(NSString *)path onCommunication:
  564. (OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *response, NSData *responseData, NSString *redirectedServer))success
  565. failure:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer))failure{
  566. OCWebDAVClient *request = [OCWebDAVClient new];
  567. request = [self getRequestWithCredentials:request];
  568. [request requestUserNameOfServer: path byCookie:cookieString onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  569. success(response, responseObject, request.redirectedServer);
  570. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  571. failure(response, error, request.redirectedServer);
  572. }];
  573. }
  574. - (void) getFeaturesSupportedByServer:(NSString*) path onCommunication:(OCCommunication *)sharedOCCommunication
  575. successRequest:(void(^)(NSHTTPURLResponse *response, BOOL hasShareSupport, BOOL hasShareeSupport, BOOL hasCookiesSupport, BOOL hasForbiddenCharactersSupport, BOOL hasCapabilitiesSupport, BOOL hasFedSharesOptionShareSupport, NSString *redirectedServer)) success
  576. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failure{
  577. OCWebDAVClient *request = [OCWebDAVClient new];
  578. if (self.userAgent) {
  579. [request setUserAgent:self.userAgent];
  580. }
  581. [request getStatusOfTheServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  582. if (responseObject) {
  583. NSError* error = nil;
  584. NSMutableDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData: (NSData*) responseObject options: NSJSONReadingMutableContainers error: &error];
  585. if(error) {
  586. // NSLog(@"Error parsing JSON: %@", error);
  587. failure(response, error, request.redirectedServer);
  588. }else{
  589. self.currentServerVersion = [jsonArray valueForKey:@"version"];
  590. BOOL hasShareSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_shared];
  591. BOOL hasShareeSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_sharee_api];
  592. BOOL hasCookiesSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_cookies];
  593. BOOL hasForbiddenCharactersSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_forbidden_characters];
  594. BOOL hasCapabilitiesSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_capabilities];
  595. BOOL hasFedSharesOptionShareSupport = [UtilsFramework isServerVersion:self.currentServerVersion higherThanLimitVersion:k_version_support_share_option_fed_share];
  596. success(response, hasShareSupport, hasShareeSupport, hasCookiesSupport, hasForbiddenCharactersSupport, hasCapabilitiesSupport, hasFedSharesOptionShareSupport, request.redirectedServer);
  597. }
  598. } else {
  599. // NSLog(@"Error parsing JSON: data is null");
  600. failure(response, nil, request.redirectedServer);
  601. }
  602. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  603. failure(response, error, request.redirectedServer);
  604. }];
  605. }
  606. - (void) readSharedByServer: (NSString *) path
  607. onCommunication:(OCCommunication *)sharedOCCommunication
  608. successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *listOfShared, NSString *redirectedServer)) successRequest
  609. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  610. path = [path encodeString:NSUTF8StringEncoding];
  611. path = [path stringByAppendingString:k_url_acces_shared_api];
  612. OCWebDAVClient *request = [OCWebDAVClient new];
  613. request = [self getRequestWithCredentials:request];
  614. [request listSharedByServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  615. if (successRequest) {
  616. NSData *responseData = (NSData*) responseObject;
  617. OCXMLSharedParser *parser = [[OCXMLSharedParser alloc]init];
  618. //NSLog(@"response: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
  619. [parser initParserWithData:responseData];
  620. NSMutableArray *sharedList = [parser.shareList mutableCopy];
  621. //Return success
  622. successRequest(response, sharedList, request.redirectedServer);
  623. }
  624. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  625. failureRequest(response, error, request.redirectedServer);
  626. }];
  627. }
  628. - (void) readSharedByServer: (NSString *) serverPath andPath: (NSString *) path
  629. onCommunication:(OCCommunication *)sharedOCCommunication
  630. successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *listOfShared, NSString *redirectedServer)) successRequest
  631. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  632. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  633. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  634. path = [path encodeString:NSUTF8StringEncoding];
  635. OCWebDAVClient *request = [OCWebDAVClient new];
  636. request = [self getRequestWithCredentials:request];
  637. [request listSharedByServer:serverPath andPath:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  638. if (successRequest) {
  639. NSData *responseData = (NSData*) responseObject;
  640. OCXMLSharedParser *parser = [[OCXMLSharedParser alloc]init];
  641. // NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  642. // NSLog(@"responseDataReadSharedByServer:andPath: %@", str);
  643. // NSLog(@"pathFolders: %@", path);
  644. // NSLog(@"serverPath: %@", serverPath);
  645. [parser initParserWithData:responseData];
  646. NSMutableArray *sharedList = [parser.shareList mutableCopy];
  647. //Return success
  648. successRequest(response, sharedList, request.redirectedServer);
  649. }
  650. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  651. failureRequest(response, error, request.redirectedServer);
  652. }];
  653. }
  654. - (void) shareFileOrFolderByServer: (NSString *) serverPath andFileOrFolderPath: (NSString *) filePath andPassword:(NSString *)password
  655. onCommunication:(OCCommunication *)sharedOCCommunication
  656. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer)) successRequest
  657. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  658. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  659. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  660. OCWebDAVClient *request = [OCWebDAVClient new];
  661. request = [self getRequestWithCredentials:request];
  662. [request shareByLinkFileOrFolderByServer:serverPath andPath:filePath andPassword:password onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  663. NSData *responseData = (NSData*) responseObject;
  664. OCXMLShareByLinkParser *parser = [[OCXMLShareByLinkParser alloc]init];
  665. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  666. [parser initParserWithData:responseData];
  667. switch (parser.statusCode) {
  668. case kOCSharedAPISuccessful:
  669. {
  670. NSString *url = parser.url;
  671. NSString *token = parser.token;
  672. if (url != nil) {
  673. successRequest(response, url, request.redirectedServer);
  674. }else if (token != nil){
  675. //We remove the \n and the empty spaces " "
  676. token = [token stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  677. token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
  678. successRequest(response, token, request.redirectedServer);
  679. }else{
  680. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  681. failureRequest(response, error, request.redirectedServer);
  682. }
  683. break;
  684. }
  685. default:
  686. {
  687. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  688. failureRequest(response, error, request.redirectedServer);
  689. }
  690. }
  691. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  692. failureRequest(response, error, request.redirectedServer);
  693. }];
  694. }
  695. - (void) shareFileOrFolderByServer: (NSString *) serverPath andFileOrFolderPath: (NSString *) filePath
  696. onCommunication:(OCCommunication *)sharedOCCommunication
  697. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *shareLink, NSString *redirectedServer)) successRequest
  698. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  699. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  700. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  701. OCWebDAVClient *request = [OCWebDAVClient new];
  702. request = [self getRequestWithCredentials:request];
  703. [request shareByLinkFileOrFolderByServer:serverPath andPath:filePath onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  704. NSData *responseData = (NSData*) responseObject;
  705. OCXMLShareByLinkParser *parser = [[OCXMLShareByLinkParser alloc]init];
  706. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  707. [parser initParserWithData:responseData];
  708. switch (parser.statusCode) {
  709. case kOCSharedAPISuccessful:
  710. {
  711. NSString *url = parser.url;
  712. NSString *token = parser.token;
  713. if (url != nil) {
  714. successRequest(response, url, request.redirectedServer);
  715. }else if (token != nil){
  716. //We remove the \n and the empty spaces " "
  717. token = [token stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  718. token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
  719. successRequest(response, token, request.redirectedServer);
  720. }else{
  721. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  722. failureRequest(response, error, request.redirectedServer);
  723. }
  724. break;
  725. }
  726. default:
  727. {
  728. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  729. failureRequest(response, error, request.redirectedServer);
  730. }
  731. }
  732. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  733. failureRequest(response, error, request.redirectedServer);
  734. }];
  735. }
  736. - (void)shareWith:(NSString *)userOrGroup shareeType:(NSInteger)shareeType inServer:(NSString *) serverPath andFileOrFolderPath:(NSString *) filePath andPermissions:(NSInteger) permissions onCommunication:(OCCommunication *)sharedOCCommunication
  737. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer))successRequest
  738. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer))failureRequest{
  739. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  740. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  741. userOrGroup = [userOrGroup encodeString:NSUTF8StringEncoding];
  742. OCWebDAVClient *request = [OCWebDAVClient new];
  743. request = [self getRequestWithCredentials:request];
  744. [request shareWith:userOrGroup shareeType:shareeType inServer:serverPath andPath:filePath andPermissions:permissions onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  745. NSData *responseData = (NSData*) responseObject;
  746. OCXMLShareByLinkParser *parser = [[OCXMLShareByLinkParser alloc]init];
  747. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  748. [parser initParserWithData:responseData];
  749. switch (parser.statusCode) {
  750. case kOCSharedAPISuccessful:
  751. {
  752. successRequest(response, request.redirectedServer);
  753. break;
  754. }
  755. default:
  756. {
  757. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  758. failureRequest(response, error, request.redirectedServer);
  759. }
  760. }
  761. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  762. failureRequest(response, error, request.redirectedServer);
  763. }];
  764. }
  765. - (void) unShareFileOrFolderByServer: (NSString *) path andIdRemoteShared: (NSInteger) idRemoteShared
  766. onCommunication:(OCCommunication *)sharedOCCommunication
  767. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest
  768. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest{
  769. path = [path encodeString:NSUTF8StringEncoding];
  770. path = [path stringByAppendingString:k_url_acces_shared_api];
  771. path = [path stringByAppendingString:[NSString stringWithFormat:@"/%ld",(long)idRemoteShared]];
  772. OCWebDAVClient *request = [OCWebDAVClient new];
  773. request = [self getRequestWithCredentials:request];
  774. [request unShareFileOrFolderByServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  775. if (successRequest) {
  776. //Return success
  777. successRequest(response, request.redirectedServer);
  778. }
  779. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  780. failureRequest(response, error, request.redirectedServer);
  781. }];
  782. }
  783. - (void) isShareFileOrFolderByServer: (NSString *) path andIdRemoteShared: (NSInteger) idRemoteShared
  784. onCommunication:(OCCommunication *)sharedOCCommunication
  785. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer, BOOL isShared, id shareDto)) successRequest
  786. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  787. path = [path encodeString:NSUTF8StringEncoding];
  788. path = [path stringByAppendingString:k_url_acces_shared_api];
  789. path = [path stringByAppendingString:[NSString stringWithFormat:@"/%ld",(long)idRemoteShared]];
  790. OCWebDAVClient *request = [OCWebDAVClient new];
  791. request = [self getRequestWithCredentials:request];
  792. [request isShareFileOrFolderByServer:path onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  793. if (successRequest) {
  794. NSData *responseData = (NSData*) responseObject;
  795. OCXMLSharedParser *parser = [[OCXMLSharedParser alloc]init];
  796. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  797. [parser initParserWithData:responseData];
  798. BOOL isShared = NO;
  799. OCSharedDto *shareDto = nil;
  800. if (parser.shareList) {
  801. NSMutableArray *sharedList = [parser.shareList mutableCopy];
  802. if ([sharedList count] > 0) {
  803. isShared = YES;
  804. shareDto = [sharedList objectAtIndex:0];
  805. }
  806. }
  807. //Return success
  808. successRequest(response, request.redirectedServer, isShared, shareDto);
  809. }
  810. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  811. failureRequest(response, error, request.redirectedServer);
  812. }];
  813. }
  814. - (void) updateShare:(NSInteger)shareId ofServerPath:(NSString *)serverPath withPasswordProtect:(NSString*)password andExpirationTime:(NSString*)expirationTime andPermissions:(NSInteger)permissions
  815. onCommunication:(OCCommunication *)sharedOCCommunication
  816. successRequest:(void(^)(NSHTTPURLResponse *response, NSString *redirectedServer)) successRequest
  817. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest{
  818. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  819. serverPath = [serverPath stringByAppendingString:k_url_acces_shared_api];
  820. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"/%ld",(long)shareId]];
  821. OCWebDAVClient *request = [OCWebDAVClient new];
  822. request = [self getRequestWithCredentials:request];
  823. [request updateShareItem:shareId ofServerPath:serverPath withPasswordProtect:password andExpirationTime:expirationTime andPermissions:permissions onCommunication:sharedOCCommunication success:^(NSHTTPURLResponse *response, id responseObject) {
  824. NSData *responseData = (NSData*) responseObject;
  825. OCXMLShareByLinkParser *parser = [[OCXMLShareByLinkParser alloc]init];
  826. // NSLog(@"response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
  827. [parser initParserWithData:responseData];
  828. switch (parser.statusCode) {
  829. case kOCSharedAPISuccessful:
  830. {
  831. successRequest(response, request.redirectedServer);
  832. break;
  833. }
  834. default:
  835. {
  836. NSError *error = [UtilsFramework getErrorWithCode:parser.statusCode andCustomMessageFromTheServer:parser.message];
  837. failureRequest(response, error, request.redirectedServer);
  838. }
  839. }
  840. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  841. failureRequest(response, error, request.redirectedServer);
  842. }];
  843. }
  844. - (void) searchUsersAndGroupsWith:(NSString *)searchString forPage:(NSInteger)page with:(NSInteger)resultsPerPage ofServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *itemList, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest{
  845. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  846. serverPath = [serverPath stringByAppendingString:k_url_access_sharee_api];
  847. searchString = [searchString encodeString:NSUTF8StringEncoding];
  848. OCWebDAVClient *request = [OCWebDAVClient new];
  849. request = [self getRequestWithCredentials:request];
  850. [request searchUsersAndGroupsWith:searchString forPage:page with:resultsPerPage ofServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  851. NSData *responseData = (NSData*) responseObject;
  852. NSMutableArray *itemList = [NSMutableArray new];
  853. //Parse
  854. NSError *error;
  855. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  856. if (error == nil) {
  857. NSDictionary *ocsDict = [jsongParsed valueForKey:@"ocs"];
  858. NSDictionary *metaDict = [ocsDict valueForKey:@"meta"];
  859. NSInteger statusCode = [[metaDict valueForKey:@"statuscode"] integerValue];
  860. if (statusCode == kOCShareeAPISuccessful || statusCode == kOCSharedAPISuccessful) {
  861. NSDictionary *dataDict = [ocsDict valueForKey:@"data"];
  862. NSArray *exactDict = [dataDict valueForKey:@"exact"];
  863. NSArray *usersFounded = [dataDict valueForKey:@"users"];
  864. NSArray *groupsFounded = [dataDict valueForKey:@"groups"];
  865. NSArray *usersRemote = [dataDict valueForKey:@"remotes"];
  866. NSArray *usersExact = [exactDict valueForKey:@"users"];
  867. NSArray *groupsExact = [exactDict valueForKey:@"groups"];
  868. NSArray *remotesExact = [exactDict valueForKey:@"remotes"];
  869. [self addUserItemOfType:shareTypeUser fromArray:usersFounded ToList:itemList];
  870. [self addUserItemOfType:shareTypeUser fromArray:usersExact ToList:itemList];
  871. [self addUserItemOfType:shareTypeRemote fromArray:usersRemote ToList:itemList];
  872. [self addUserItemOfType:shareTypeRemote fromArray:remotesExact ToList:itemList];
  873. [self addGroupItemFromArray:groupsFounded ToList:itemList];
  874. [self addGroupItemFromArray:groupsExact ToList:itemList];
  875. }else{
  876. NSString *message = (NSString*)[metaDict objectForKey:@"message"];
  877. if ([message isKindOfClass:[NSNull class]]) {
  878. message = @"";
  879. }
  880. NSError *error = [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message];
  881. failureRequest(response, error, request.redirectedServer);
  882. }
  883. //Return success
  884. successRequest(response, itemList, request.redirectedServer);
  885. }
  886. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  887. failureRequest(response, error, request.redirectedServer);
  888. }];
  889. }
  890. - (void) getCapabilitiesOfServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, OCCapabilities *capabilities, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest{
  891. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  892. serverPath = [serverPath stringByAppendingString:k_url_capabilities];
  893. OCWebDAVClient *request = [OCWebDAVClient new];
  894. request = [self getRequestWithCredentials:request];
  895. [request getCapabilitiesOfServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  896. NSData *responseData = (NSData*) responseObject;
  897. //Parse
  898. NSError *error;
  899. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  900. NSLog(@"dic: %@",jsongParsed);
  901. OCCapabilities *capabilities = [OCCapabilities new];
  902. if (jsongParsed.allKeys > 0) {
  903. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  904. NSDictionary *data = [ocs valueForKey:@"data"];
  905. NSDictionary *version = [data valueForKey:@"version"];
  906. if (ocs.count > 0 && data.count > 0 && version.count > 0) {
  907. //VERSION
  908. NSNumber *versionMajorNumber = (NSNumber*) [version valueForKey:@"major"];
  909. NSNumber *versionMinorNumber = (NSNumber*) [version valueForKey:@"minor"];
  910. NSNumber *versionMicroNumber = (NSNumber*) [version valueForKey:@"micro"];
  911. capabilities.versionMajor = versionMajorNumber.integerValue;
  912. capabilities.versionMinor = versionMinorNumber.integerValue;
  913. capabilities.versionMicro = versionMicroNumber.integerValue;
  914. capabilities.versionString = (NSString*)[version valueForKey:@"string"];
  915. capabilities.versionEdition = (NSString*)[version valueForKey:@"edition"];
  916. NSDictionary *capabilitiesDict = [data valueForKey:@"capabilities"];
  917. NSDictionary *core = [capabilitiesDict valueForKey:@"core"];
  918. //CORE
  919. NSNumber *corePollIntervalNumber = (NSNumber*)[core valueForKey:@"pollinterval"];
  920. capabilities.corePollInterval = corePollIntervalNumber.integerValue;
  921. NSDictionary *fileSharing = [capabilitiesDict valueForKey:@"files_sharing"];
  922. //FILE SHARING
  923. NSNumber *fileSharingAPIEnabledNumber = (NSNumber*)[fileSharing valueForKey:@"api_enabled"];
  924. NSNumber *filesSharingReSharingEnabledNumber = (NSNumber*)[fileSharing valueForKey:@"resharing"];
  925. capabilities.isFilesSharingAPIEnabled = fileSharingAPIEnabledNumber.boolValue;
  926. capabilities.isFilesSharingReSharingEnabled = filesSharingReSharingEnabledNumber.boolValue;
  927. NSDictionary *fileSharingPublic = [fileSharing valueForKey:@"public"];
  928. NSNumber *filesSharingShareLinkEnabledNumber = (NSNumber*)[fileSharingPublic valueForKey:@"enabled"];
  929. NSNumber *filesSharingAllowPublicUploadsEnabledNumber = (NSNumber*)[fileSharingPublic valueForKey:@"upload"];
  930. NSNumber *filesSharingAllowUserSendMailNotificationAboutShareLinkEnabledNumber = (NSNumber*)[fileSharingPublic valueForKey:@"send_mail"];
  931. capabilities.isFilesSharingShareLinkEnabled = filesSharingShareLinkEnabledNumber.boolValue;
  932. capabilities.isFilesSharingAllowPublicUploadsEnabled = filesSharingAllowPublicUploadsEnabledNumber.boolValue;
  933. capabilities.isFilesSharingAllowUserSendMailNotificationAboutShareLinkEnabled = filesSharingAllowUserSendMailNotificationAboutShareLinkEnabledNumber.boolValue;
  934. NSDictionary *fileSharingPublicExpireDate = [fileSharingPublic valueForKey:@"expire_date"];
  935. NSNumber *filesSharingExpireDateByDefaultEnabledNumber = (NSNumber*)[fileSharingPublicExpireDate valueForKey:@"enabled"];
  936. NSNumber *filesSharingExpireDateEnforceEnabledNumber = (NSNumber*)[fileSharingPublicExpireDate valueForKey:@"enforced"];
  937. NSNumber *filesSharingExpireDateDaysNumber = (NSNumber*)[fileSharingPublicExpireDate valueForKey:@"days"];
  938. capabilities.isFilesSharingExpireDateByDefaultEnabled = filesSharingExpireDateByDefaultEnabledNumber.boolValue;
  939. capabilities.isFilesSharingExpireDateEnforceEnabled = filesSharingExpireDateEnforceEnabledNumber.boolValue;
  940. capabilities.filesSharingExpireDateDaysNumber = filesSharingExpireDateDaysNumber.integerValue;
  941. NSDictionary *fileSharingPublicPassword = [fileSharingPublic valueForKey:@"password"];
  942. NSNumber *filesSharingPasswordEnforcedEnabledNumber = (NSNumber*)[fileSharingPublicPassword valueForKey:@"enforced"];
  943. capabilities.isFilesSharingPasswordEnforcedEnabled = filesSharingPasswordEnforcedEnabledNumber.boolValue;;
  944. NSDictionary *fileSharingUser = [fileSharing valueForKey:@"user"];
  945. NSNumber *filesSharingAllowUserSendMailNotificationAboutOtherUsersEnabledNumber = (NSNumber*)[fileSharingUser valueForKey:@"send_mail"];
  946. capabilities.isFilesSharingAllowUserSendMailNotificationAboutOtherUsersEnabled = filesSharingAllowUserSendMailNotificationAboutOtherUsersEnabledNumber.boolValue;
  947. //FEDERATION
  948. NSDictionary *fileSharingFederation = [fileSharing valueForKey:@"federation"];
  949. NSNumber *filesSharingAllowUserSendSharesToOtherServersEnabledNumber = (NSNumber*)[fileSharingFederation valueForKey:@"outgoing"];
  950. NSNumber *filesSharingAllowUserReceiveSharesToOtherServersEnabledNumber = (NSNumber*)[fileSharingFederation valueForKey:@"incoming"];
  951. capabilities.isFilesSharingAllowUserSendSharesToOtherServersEnabled = filesSharingAllowUserSendSharesToOtherServersEnabledNumber.boolValue;
  952. capabilities.isFilesSharingAllowUserReceiveSharesToOtherServersEnabled = filesSharingAllowUserReceiveSharesToOtherServersEnabledNumber.boolValue;
  953. // EXTERNAL SITES
  954. NSDictionary *externalSitesDic = [capabilitiesDict valueForKey:@"external"];
  955. if (externalSitesDic) {
  956. NSArray *externalSitesArray = [externalSitesDic valueForKey:@"v1"];
  957. if (externalSitesArray)
  958. if ([[externalSitesArray objectAtIndex:0] isEqualToString:@"sites"])
  959. capabilities.isExternalSitesServerEnabled = YES;
  960. }
  961. //FILES
  962. NSDictionary *files = [capabilitiesDict valueForKey:@"files"];
  963. NSNumber *fileBigFileChunkingEnabledNumber = (NSNumber*)[files valueForKey:@"bigfilechunking"];
  964. NSNumber *fileUndeleteEnabledNumber = (NSNumber*)[files valueForKey:@"undelete"];
  965. NSNumber *fileVersioningEnabledNumber = (NSNumber*)[files valueForKey:@"versioning"];
  966. capabilities.isFileBigFileChunkingEnabled = fileBigFileChunkingEnabledNumber.boolValue;
  967. capabilities.isFileUndeleteEnabled = fileUndeleteEnabledNumber.boolValue;
  968. capabilities.isFileVersioningEnabled = fileVersioningEnabledNumber.boolValue;
  969. //THEMING
  970. NSDictionary *theming = [capabilitiesDict valueForKey:@"theming"];
  971. if ([theming count] > 0) {
  972. if ([theming valueForKey:@"background"] && ![[theming valueForKey:@"background"] isEqual:[NSNull null]])
  973. capabilities.themingBackground = [theming valueForKey:@"background"];
  974. if ([theming valueForKey:@"color"] && ![[theming valueForKey:@"color"] isEqual:[NSNull null]])
  975. capabilities.themingColor = [theming valueForKey:@"color"];
  976. if ([theming valueForKey:@"logo"] && ![[theming valueForKey:@"logo"] isEqual:[NSNull null]])
  977. capabilities.themingLogo = [theming valueForKey:@"logo"];
  978. if ([theming valueForKey:@"name"] && ![[theming valueForKey:@"name"] isEqual:[NSNull null]])
  979. capabilities.themingName = [theming valueForKey:@"name"];
  980. if ([theming valueForKey:@"slogan"] && ![[theming valueForKey:@"slogan"] isEqual:[NSNull null]])
  981. capabilities.themingSlogan = [theming valueForKey:@"slogan"];
  982. if ([theming valueForKey:@"url"] && ![[theming valueForKey:@"url"] isEqual:[NSNull null]])
  983. capabilities.themingUrl = [theming valueForKey:@"url"];
  984. }
  985. }
  986. successRequest(response, capabilities, request.redirectedServer);
  987. } else {
  988. failureRequest(response, error, request.redirectedServer);
  989. }
  990. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  991. failureRequest(response, error, request.redirectedServer);
  992. }];
  993. }
  994. #pragma mark - Remote thumbnails
  995. - (NSURLSessionTask *) getRemoteThumbnailByServer:(NSString*)serverPath ofFilePath:(NSString *)filePath withWidth:(NSInteger)fileWidth andHeight:(NSInteger)fileHeight onCommunication:(OCCommunication *)sharedOCComunication
  996. successRequest:(void(^)(NSHTTPURLResponse *response, NSData *thumbnail, NSString *redirectedServer)) successRequest
  997. failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  998. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  999. serverPath = [serverPath stringByAppendingString:k_url_thumbnails];
  1000. filePath = [filePath encodeString:NSUTF8StringEncoding];
  1001. OCWebDAVClient *request = [OCWebDAVClient new];
  1002. request = [self getRequestWithCredentials:request];
  1003. OCHTTPRequestOperation *operation = [request getRemoteThumbnailByServer:serverPath ofFilePath:filePath withWidth:fileWidth andHeight:fileHeight onCommunication:sharedOCComunication
  1004. success:^(NSHTTPURLResponse *response, id responseObject) {
  1005. NSData *responseData = (NSData*) responseObject;
  1006. successRequest(response, responseData, request.redirectedServer);
  1007. } failure:^(NSHTTPURLResponse *response, id _Nullable responseObject, NSError * _Nonnull error) {
  1008. failureRequest(response, error, request.redirectedServer);
  1009. }];
  1010. [operation resume];
  1011. return operation;
  1012. }
  1013. #pragma mark - Notification Server
  1014. - (void)getNotificationServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *listOfNotifications, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1015. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1016. serverPath = [serverPath stringByAppendingString:k_url_acces_remote_notification_api];
  1017. OCWebDAVClient *request = [OCWebDAVClient new];
  1018. request = [self getRequestWithCredentials:request];
  1019. [request getNotificationServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1020. NSData *responseData = (NSData*) responseObject;
  1021. //Parse
  1022. NSError *error;
  1023. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1024. NSLog(@"[LOG] Notifications : %@",jsongParsed);
  1025. NSMutableArray *listOfNotifications = [NSMutableArray new];
  1026. if (jsongParsed.allKeys > 0) {
  1027. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1028. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1029. NSDictionary *datas = [ocs valueForKey:@"data"];
  1030. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1031. if (statusCode == kOCNotificationAPINoContent || statusCode == kOCNotificationAPISuccessful) {
  1032. for (NSDictionary *data in datas) {
  1033. OCNotifications *notification = [OCNotifications new];
  1034. if ([data valueForKey:@"notification_id"] && ![[data valueForKey:@"notification_id"] isEqual:[NSNull null]])
  1035. notification.idNotification = [[data valueForKey:@"notification_id"] integerValue];
  1036. if ([data valueForKey:@"app"] && ![[data valueForKey:@"app"] isEqual:[NSNull null]])
  1037. notification.application = [data valueForKey:@"app"];
  1038. if ([data valueForKey:@"user"] && ![[data valueForKey:@"user"] isEqual:[NSNull null]])
  1039. notification.user = [data valueForKey:@"user"];
  1040. if ([data valueForKey:@"datetime"] && ![[data valueForKey:@"datetime"] isEqual:[NSNull null]]) {
  1041. NSString *dateString = [data valueForKey:@"datetime"];
  1042. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  1043. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  1044. [dateFormatter setLocale:enUSPOSIXLocale];
  1045. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
  1046. notification.date = [dateFormatter dateFromString:dateString];
  1047. }
  1048. if ([data valueForKey:@"object_type"] && ![[data valueForKey:@"object_type"] isEqual:[NSNull null]])
  1049. notification.typeObject = [data valueForKey:@"object_type"];
  1050. if ([data valueForKey:@"object_id"] && ![[data valueForKey:@"object_id"] isEqual:[NSNull null]])
  1051. notification.idObject = [data valueForKey:@"object_id"];
  1052. if ([data valueForKey:@"subject"] && ![[data valueForKey:@"subject"] isEqual:[NSNull null]])
  1053. notification.subject = [data valueForKey:@"subject"];
  1054. if ([data valueForKey:@"subjectRich"] && ![[data valueForKey:@"subjectRich"] isEqual:[NSNull null]])
  1055. notification.subjectRich = [data valueForKey:@"subjectRich"];
  1056. if ([data valueForKey:@"subjectRichParameters"] && ![[data valueForKey:@"subjectRichParameters"] isEqual:[NSNull null]])
  1057. notification.subjectRichParameters = [data valueForKey:@"subjectRichParameters"];
  1058. if ([data valueForKey:@"message"] && ![[data valueForKey:@"message"] isEqual:[NSNull null]])
  1059. notification.message = [data valueForKey:@"message"];
  1060. if ([data valueForKey:@"messageRich"] && ![[data valueForKey:@"messageRich"] isEqual:[NSNull null]])
  1061. notification.messageRich = [data valueForKey:@"messageRich"];
  1062. if ([data valueForKey:@"messageRichParameters"] && ![[data valueForKey:@"messageRichParameters"] isEqual:[NSNull null]])
  1063. notification.messageRichParameters = [data valueForKey:@"messageRichParameters"];
  1064. if ([data valueForKey:@"link"] && ![[data valueForKey:@"link"] isEqual:[NSNull null]])
  1065. notification.link = [data valueForKey:@"link"];
  1066. if ([data valueForKey:@"icon"] && ![[data valueForKey:@"icon"] isEqual:[NSNull null]])
  1067. notification.icon = [data valueForKey:@"icon"];
  1068. /* ACTION */
  1069. NSMutableArray *actionsArr = [NSMutableArray new];
  1070. NSDictionary *actions = [data valueForKey:@"actions"];
  1071. for (NSDictionary *action in actions) {
  1072. OCNotificationsAction *notificationAction = [OCNotificationsAction new];
  1073. if ([action valueForKey:@"label"] && ![[action valueForKey:@"label"] isEqual:[NSNull null]])
  1074. notificationAction.label = [action valueForKey:@"label"];
  1075. if ([action valueForKey:@"link"] && ![[action valueForKey:@"link"] isEqual:[NSNull null]])
  1076. notificationAction.link = [action valueForKey:@"link"];
  1077. if ([action valueForKey:@"primary"] && ![[action valueForKey:@"primary"] isEqual:[NSNull null]])
  1078. notificationAction.primary = [[action valueForKey:@"primary"] boolValue];
  1079. if ([action valueForKey:@"type"] && ![[action valueForKey:@"type"] isEqual:[NSNull null]])
  1080. notificationAction.type = [action valueForKey:@"type"];
  1081. [actionsArr addObject:notificationAction];
  1082. }
  1083. notification.actions = [[NSArray alloc] initWithArray:actionsArr];
  1084. [listOfNotifications addObject:notification];
  1085. }
  1086. } else {
  1087. NSString *message = (NSString*)[meta objectForKey:@"message"];
  1088. if ([message isKindOfClass:[NSNull class]]) {
  1089. message = @"";
  1090. }
  1091. NSError *error = [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message];
  1092. failureRequest(response, error, request.redirectedServer);
  1093. }
  1094. }
  1095. //Return success
  1096. successRequest(response, listOfNotifications, request.redirectedServer);
  1097. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1098. failureRequest(response, error, request.redirectedServer);
  1099. }];
  1100. }
  1101. - (void)setNotificationServer:(NSString*)serverPath type:(NSString *)type onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void (^)(NSHTTPURLResponse *, NSString *))successRequest failureRequest:(void (^)(NSHTTPURLResponse *, NSError *, NSString *))failureRequest {
  1102. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1103. OCWebDAVClient *request = [OCWebDAVClient new];
  1104. request = [self getRequestWithCredentials:request];
  1105. __weak OCWebDAVClient *wrequest = request;
  1106. [request setNotificationServer:serverPath type:type onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1107. successRequest(response, wrequest.redirectedServer);
  1108. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1109. failureRequest(response, error, wrequest.redirectedServer);
  1110. }];
  1111. }
  1112. - (void)subscribingNextcloudServerPush:(NSString *)serverPath pushTokenHash:(NSString *)pushTokenHash devicePublicKey:(NSString *)devicePublicKey proxyServerPath:(NSString *)proxyServerPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSString *publicKey, NSString *deviceIdentifier, NSString *signature, NSString *redirectedServer)) successRequest failureRequest:(void (^)(NSHTTPURLResponse *, NSError *, NSString *))failureRequest {
  1113. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1114. serverPath = [serverPath stringByAppendingString:k_url_acces_remote_subscribing_nextcloud_server_api];
  1115. OCWebDAVClient *request = [OCWebDAVClient new];
  1116. request = [self getRequestWithCredentials:request];
  1117. [request subscribingNextcloudServerPush:serverPath authorizationToken:_password pushTokenHash:pushTokenHash devicePublicKey:devicePublicKey proxyServerPath:proxyServerPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1118. NSData *responseData = (NSData*) responseObject;
  1119. //Parse
  1120. NSError *error;
  1121. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1122. NSLog(@"[LOG] Subscribing at the Nextcloud server : %@",jsongParsed);
  1123. NSString *publicKey = [jsongParsed objectForKey:@"publicKey"];
  1124. NSString *deviceIdentifier = [jsongParsed objectForKey:@"deviceIdentifier"];
  1125. NSString *signature = [jsongParsed objectForKey:@"signature"];
  1126. successRequest(response, publicKey, deviceIdentifier, signature, request.redirectedServer);
  1127. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1128. failureRequest(response, error, request.redirectedServer);
  1129. }];
  1130. }
  1131. - (void)subscribingPushProxy:(NSString *)serverPath pushToken:(NSString *)pushToken deviceIdentifier:(NSString *)deviceIdentifier deviceIdentifierSignature:(NSString *)deviceIdentifierSignature userPublicKey:(NSString *)userPublicKey onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void (^)(NSHTTPURLResponse *, NSString *redirectedServer))successRequest failureRequest:(void (^)(NSHTTPURLResponse *, NSError *, NSString *))failureRequest {
  1132. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1133. serverPath = [serverPath stringByAppendingString:@"/devices"];
  1134. OCWebDAVClient *request = [OCWebDAVClient new];
  1135. request = [self getRequestWithCredentials:request];
  1136. [request subscribingPushProxy:serverPath authorizationToken:_password pushToken:pushToken deviceIdentifier:deviceIdentifier deviceIdentifierSignature:deviceIdentifierSignature userPublicKey:userPublicKey onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1137. if (successRequest) {
  1138. //Return success
  1139. successRequest(response, request.redirectedServer);
  1140. }
  1141. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1142. failureRequest(response, error, request.redirectedServer);
  1143. }];
  1144. }
  1145. #pragma mark - Activity
  1146. - (void) getActivityServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *listOfActivity, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1147. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1148. serverPath = [serverPath stringByAppendingString:k_url_acces_remote_activity_api];
  1149. OCWebDAVClient *request = [OCWebDAVClient new];
  1150. request = [self getRequestWithCredentials:request];
  1151. [request getActivityServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1152. NSData *responseData = (NSData*) responseObject;
  1153. //Parse
  1154. NSError *error;
  1155. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1156. NSLog(@"[LOG] Activity : %@",jsongParsed);
  1157. NSMutableArray *listOfActivity = [NSMutableArray new];
  1158. if (jsongParsed.allKeys > 0) {
  1159. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1160. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1161. NSDictionary *datas = [ocs valueForKey:@"data"];
  1162. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1163. if (statusCode == kOCNotificationAPINoContent || statusCode == kOCNotificationAPISuccessful) {
  1164. for (NSDictionary *data in datas) {
  1165. OCActivity *activity = [OCActivity new];
  1166. if ([data valueForKey:@"id"] && ![[data valueForKey:@"id"] isEqual:[NSNull null]])
  1167. activity.idActivity = [[data valueForKey:@"id"] integerValue];
  1168. if ([data valueForKey:@"date"] && ![[data valueForKey:@"date"] isEqual:[NSNull null]]) {
  1169. NSString *dateString = [data valueForKey:@"date"];
  1170. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  1171. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  1172. [dateFormatter setLocale:enUSPOSIXLocale];
  1173. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
  1174. activity.date = [dateFormatter dateFromString:dateString];
  1175. }
  1176. if ([data valueForKey:@"file"] && ![[data valueForKey:@"file"] isEqual:[NSNull null]])
  1177. activity.file = [data valueForKey:@"file"];
  1178. if ([data valueForKey:@"link"] && ![[data valueForKey:@"link"] isEqual:[NSNull null]])
  1179. activity.link = [data valueForKey:@"link"];
  1180. if ([data valueForKey:@"message"] && ![[data valueForKey:@"message"] isEqual:[NSNull null]])
  1181. activity.message = [data valueForKey:@"message"];
  1182. if ([data valueForKey:@"subject"] && ![[data valueForKey:@"subject"] isEqual:[NSNull null]])
  1183. activity.subject = [data valueForKey:@"subject"];
  1184. [listOfActivity addObject:activity];
  1185. }
  1186. } else {
  1187. NSString *message = (NSString*)[meta objectForKey:@"message"];
  1188. if ([message isKindOfClass:[NSNull class]]) {
  1189. message = @"";
  1190. }
  1191. NSError *error = [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message];
  1192. failureRequest(response, error, request.redirectedServer);
  1193. }
  1194. }
  1195. //Return success
  1196. successRequest(response, listOfActivity, request.redirectedServer);
  1197. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1198. failureRequest(response, error, request.redirectedServer);
  1199. }];
  1200. }
  1201. #pragma mark - External sites
  1202. - (void) getExternalSitesServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1203. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1204. serverPath = [serverPath stringByAppendingString:k_url_acces_external_sites_api];
  1205. OCWebDAVClient *request = [OCWebDAVClient new];
  1206. request = [self getRequestWithCredentials:request];
  1207. [request getExternalSitesServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1208. NSData *responseData = (NSData*) responseObject;
  1209. //Parse
  1210. NSError *error;
  1211. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1212. NSLog(@"[LOG] External Sites : %@",jsongParsed);
  1213. NSMutableArray *listOfExternalSites = [NSMutableArray new];
  1214. if (jsongParsed.allKeys > 0) {
  1215. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1216. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1217. NSDictionary *datas = [ocs valueForKey:@"data"];
  1218. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1219. if (statusCode == kOCNotificationAPINoContent || statusCode == kOCNotificationAPISuccessful) {
  1220. for (NSDictionary *data in datas) {
  1221. OCExternalSites *externalSites = [OCExternalSites new];
  1222. externalSites.idExternalSite = [[data valueForKey:@"id"] integerValue];
  1223. if ([data valueForKey:@"icon"] && ![[data valueForKey:@"icon"] isEqual:[NSNull null]])
  1224. externalSites.icon = [data valueForKey:@"icon"];
  1225. if ([data valueForKey:@"lang"] && ![[data valueForKey:@"lang"] isEqual:[NSNull null]])
  1226. externalSites.lang = [data valueForKey:@"lang"];
  1227. if ([data valueForKey:@"name"] && ![[data valueForKey:@"name"] isEqual:[NSNull null]])
  1228. externalSites.name = [data valueForKey:@"name"];
  1229. if ([data valueForKey:@"url"] && ![[data valueForKey:@"url"] isEqual:[NSNull null]])
  1230. externalSites.url = [data valueForKey:@"url"];
  1231. if ([data valueForKey:@"type"] && ![[data valueForKey:@"type"] isEqual:[NSNull null]])
  1232. externalSites.type = [data valueForKey:@"type"];
  1233. [listOfExternalSites addObject:externalSites];
  1234. }
  1235. } else {
  1236. NSString *message = (NSString*)[meta objectForKey:@"message"];
  1237. if ([message isKindOfClass:[NSNull class]]) {
  1238. message = @"";
  1239. }
  1240. NSError *error = [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message];
  1241. failureRequest(response, error, request.redirectedServer);
  1242. }
  1243. }
  1244. //Return success
  1245. successRequest(response, listOfExternalSites, request.redirectedServer);
  1246. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1247. failureRequest(response, error, request.redirectedServer);
  1248. }];
  1249. }
  1250. #pragma mark - User Profile
  1251. - (void) getUserProfileServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCComunication successRequest:(void(^)(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer)) successRequest failureRequest:(void(^)(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer)) failureRequest {
  1252. serverPath = [serverPath stringByAppendingString:k_url_acces_remote_userprofile_api];
  1253. serverPath = [serverPath encodeString:NSUTF8StringEncoding];
  1254. OCWebDAVClient *request = [OCWebDAVClient new];
  1255. request = [self getRequestWithCredentials:request];
  1256. [request getUserProfileServer:serverPath onCommunication:sharedOCComunication success:^(NSHTTPURLResponse *response, id responseObject) {
  1257. NSData *responseData = (NSData*) responseObject;
  1258. //Parse
  1259. NSError *error;
  1260. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
  1261. NSLog(@"[LOG] User Profile : %@",jsongParsed);
  1262. OCUserProfile *userProfile = [OCUserProfile new];
  1263. if (jsongParsed.allKeys > 0) {
  1264. NSDictionary *ocs = [jsongParsed valueForKey:@"ocs"];
  1265. NSDictionary *meta = [ocs valueForKey:@"meta"];
  1266. NSDictionary *data = [ocs valueForKey:@"data"];
  1267. NSInteger statusCode = [[meta valueForKey:@"statuscode"] integerValue];
  1268. if (statusCode == kOCUserProfileAPISuccessful) {
  1269. if ([data valueForKey:@"address"] && ![[data valueForKey:@"address"] isKindOfClass:[NSNull class]])
  1270. userProfile.address = [data valueForKey:@"address"];
  1271. if ([data valueForKey:@"displayname"] && ![[data valueForKey:@"displayname"] isKindOfClass:[NSNull class]])
  1272. userProfile.displayName = [data valueForKey:@"displayname"];
  1273. if ([data valueForKey:@"email"] && ![[data valueForKey:@"email"] isKindOfClass:[NSNull class]])
  1274. userProfile.email = [data valueForKey:@"email"];
  1275. if ([data valueForKey:@"enabled"] && ![[data valueForKey:@"enabled"] isKindOfClass:[NSNull class]])
  1276. userProfile.enabled = [[data valueForKey:@"enabled"] boolValue];
  1277. if ([data valueForKey:@"id"] && ![[data valueForKey:@"id"] isKindOfClass:[NSNull class]])
  1278. userProfile.id = [data valueForKey:@"id"];
  1279. if ([data valueForKey:@"phone"] && ![[data valueForKey:@"phone"] isKindOfClass:[NSNull class]])
  1280. userProfile.phone = [data valueForKey:@"phone"];
  1281. if ([data valueForKey:@"twitter"] && ![[data valueForKey:@"twitter"] isKindOfClass:[NSNull class]])
  1282. userProfile.twitter = [data valueForKey:@"twitter"];
  1283. if ([data valueForKey:@"webpage"] && ![[data valueForKey:@"webpage"] isKindOfClass:[NSNull class]])
  1284. userProfile.webpage = [data valueForKey:@"webpage"];
  1285. /* QUOTA */
  1286. NSDictionary *quota = [data valueForKey:@"quota"];
  1287. if ([quota count] > 0) {
  1288. if ([quota valueForKey:@"free"] && ![[quota valueForKey:@"free"] isKindOfClass:[NSNull class]])
  1289. userProfile.quotaFree = [[quota valueForKey:@"free"] doubleValue];
  1290. if ([quota valueForKey:@"quota"] && ![[quota valueForKey:@"quota"] isKindOfClass:[NSNull class]])
  1291. userProfile.quota = [[quota valueForKey:@"quota"] doubleValue];
  1292. if ([quota valueForKey:@"relative"] && ![[quota valueForKey:@"relative"] isKindOfClass:[NSNull class]])
  1293. userProfile.quotaRelative = [[quota valueForKey:@"relative"] doubleValue];
  1294. if ([quota valueForKey:@"total"] && ![[quota valueForKey:@"total"] isKindOfClass:[NSNull class]])
  1295. userProfile.quotaTotal = [[quota valueForKey:@"total"] doubleValue];
  1296. if ([quota valueForKey:@"used"] && ![[quota valueForKey:@"used"] isKindOfClass:[NSNull class]])
  1297. userProfile.quotaUsed = [[quota valueForKey:@"used"] doubleValue];
  1298. }
  1299. } else {
  1300. NSString *message = (NSString*)[meta objectForKey:@"message"];
  1301. if ([message isKindOfClass:[NSNull class]]) {
  1302. message = @"";
  1303. }
  1304. NSError *error = [UtilsFramework getErrorWithCode:statusCode andCustomMessageFromTheServer:message];
  1305. failureRequest(response, error, request.redirectedServer);
  1306. }
  1307. }
  1308. //Return success
  1309. successRequest(response, userProfile, request.redirectedServer);
  1310. } failure:^(NSHTTPURLResponse *response, NSData *responseData, NSError *error) {
  1311. failureRequest(response, error, request.redirectedServer);
  1312. }];
  1313. }
  1314. #pragma mark - Clear Cache
  1315. - (void)eraseURLCache
  1316. {
  1317. [[NSURLCache sharedURLCache] setMemoryCapacity:0];
  1318. [[NSURLCache sharedURLCache] setDiskCapacity:0];
  1319. }
  1320. #pragma mark - Utils
  1321. - (void) addUserItemOfType:(NSInteger) shareeType fromArray:(NSArray*) usersArray ToList: (NSMutableArray *) itemList
  1322. {
  1323. for (NSDictionary *userFound in usersArray) {
  1324. OCShareUser *user = [OCShareUser new];
  1325. if ([[userFound valueForKey:@"label"] isKindOfClass:[NSNumber class]]) {
  1326. NSNumber *number = [userFound valueForKey:@"label"];
  1327. user.displayName = [NSString stringWithFormat:@"%ld", number.longValue];
  1328. }else{
  1329. user.displayName = [userFound valueForKey:@"label"];
  1330. }
  1331. NSDictionary *userValues = [userFound valueForKey:@"value"];
  1332. if ([[userValues valueForKey:@"shareWith"] isKindOfClass:[NSNumber class]]) {
  1333. NSNumber *number = [userValues valueForKey:@"shareWith"];
  1334. user.name = [NSString stringWithFormat:@"%ld", number.longValue];
  1335. }else{
  1336. user.name = [userValues valueForKey:@"shareWith"];
  1337. }
  1338. user.shareeType = shareeType;
  1339. user.server = [userValues valueForKey:@"server"];
  1340. [itemList addObject:user];
  1341. }
  1342. }
  1343. - (void) addGroupItemFromArray:(NSArray*) groupsArray ToList: (NSMutableArray *) itemList
  1344. {
  1345. for (NSDictionary *groupFound in groupsArray) {
  1346. OCShareUser *group = [OCShareUser new];
  1347. NSDictionary *groupValues = [groupFound valueForKey:@"value"];
  1348. if ([[groupValues valueForKey:@"shareWith"] isKindOfClass:[NSNumber class]]) {
  1349. NSNumber *number = [groupValues valueForKey:@"shareWith"];
  1350. group.name = [NSString stringWithFormat:@"%ld", number.longValue];
  1351. }else{
  1352. group.name = [groupValues valueForKey:@"shareWith"];
  1353. }
  1354. group.shareeType = shareTypeGroup;
  1355. [itemList addObject:group];
  1356. }
  1357. }
  1358. @end