PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/SimpleFTPSample/ListController.m

https://gitlab.com/praveenvelanati/ios-demo
Objective C | 618 lines | 381 code | 122 blank | 115 comment | 103 complexity | aa3db742dedeb2c1f3c9b2ed359b0e07 MD5 | raw file
  1. /*
  2. File: ListController.m
  3. Contains: Manages the List tab.
  4. Written by: DTS
  5. Copyright: Copyright (c) 2010 Apple Inc. All Rights Reserved.
  6. Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
  7. ("Apple") in consideration of your agreement to the following
  8. terms, and your use, installation, modification or
  9. redistribution of this Apple software constitutes acceptance of
  10. these terms. If you do not agree with these terms, please do
  11. not use, install, modify or redistribute this Apple software.
  12. In consideration of your agreement to abide by the following
  13. terms, and subject to these terms, Apple grants you a personal,
  14. non-exclusive license, under Apple's copyrights in this
  15. original Apple software (the "Apple Software"), to use,
  16. reproduce, modify and redistribute the Apple Software, with or
  17. without modifications, in source and/or binary forms; provided
  18. that if you redistribute the Apple Software in its entirety and
  19. without modifications, you must retain this notice and the
  20. following text and disclaimers in all such redistributions of
  21. the Apple Software. Neither the name, trademarks, service marks
  22. or logos of Apple Inc. may be used to endorse or promote
  23. products derived from the Apple Software without specific prior
  24. written permission from Apple. Except as expressly stated in
  25. this notice, no other rights or licenses, express or implied,
  26. are granted by Apple herein, including but not limited to any
  27. patent rights that may be infringed by your derivative works or
  28. by other works in which the Apple Software may be incorporated.
  29. The Apple Software is provided by Apple on an "AS IS" basis.
  30. APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
  31. WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
  32. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING
  33. THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  34. COMBINATION WITH YOUR PRODUCTS.
  35. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT,
  36. INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  37. TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  38. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY
  39. OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  40. OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY
  41. OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR
  42. OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF
  43. SUCH DAMAGE.
  44. */
  45. #import "ListController.h"
  46. #import "AppDelegate.h"
  47. #include <sys/socket.h>
  48. #include <sys/dirent.h>
  49. #include <CFNetwork/CFNetwork.h>
  50. #pragma mark * ListController
  51. @interface ListController ()
  52. // Properties that don't need to be seen by the outside world.
  53. @property (nonatomic, readonly) BOOL isReceiving;
  54. @property (nonatomic, retain) NSInputStream * networkStream;
  55. @property (nonatomic, retain) NSMutableData * listData;
  56. @property (nonatomic, retain) NSMutableArray * listEntries;
  57. @property (nonatomic, copy) NSString * status;
  58. - (void)_updateStatus:(NSString *)statusString;
  59. @end
  60. @implementation ListController
  61. #pragma mark * Status management
  62. // These methods are used by the core transfer code to update the UI.
  63. - (void)_receiveDidStart
  64. {
  65. // Clear the current image so that we get a nice visual cue if the receive fails.
  66. [self.listEntries removeAllObjects];
  67. [self.tableView reloadData];
  68. [self _updateStatus:@"Receiving"];
  69. self.listOrCancelButton.title = @"Cancel";
  70. [self.activityIndicator startAnimating];
  71. [[AppDelegate sharedAppDelegate] didStartNetworking];
  72. }
  73. - (void)_updateStatus:(NSString *)statusString
  74. {
  75. assert(statusString != nil);
  76. self.status = statusString;
  77. [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
  78. }
  79. - (void)_addListEntries:(NSArray *)newEntries
  80. {
  81. assert(self.listEntries != nil);
  82. [self.listEntries addObjectsFromArray:newEntries];
  83. [self.tableView reloadData];
  84. }
  85. - (void)_receiveDidStopWithStatus:(NSString *)statusString
  86. {
  87. if (statusString == nil) {
  88. statusString = @"List succeeded";
  89. }
  90. [self _updateStatus:statusString];
  91. self.listOrCancelButton.title = @"List";
  92. [self.activityIndicator stopAnimating];
  93. [[AppDelegate sharedAppDelegate] didStopNetworking];
  94. }
  95. #pragma mark * Core transfer code
  96. // This is the code that actually does the networking.
  97. @synthesize networkStream = _networkStream;
  98. @synthesize listData = _listData;
  99. @synthesize listEntries = _listEntries;
  100. - (BOOL)isReceiving
  101. {
  102. return (self.networkStream != nil);
  103. }
  104. - (void)_startReceive
  105. // Starts a connection to download the current URL.
  106. {
  107. BOOL success;
  108. NSURL * url;
  109. CFReadStreamRef ftpStream;
  110. assert(self.networkStream == nil); // don't tap receive twice in a row!
  111. // First get and check the URL.
  112. url = [[AppDelegate sharedAppDelegate] smartURLForString:self.urlText.text];
  113. success = (url != nil);
  114. // If the URL is bogus, let the user know. Otherwise kick off the connection.
  115. if ( ! success) {
  116. [self _updateStatus:@"Invalid URL"];
  117. } else {
  118. // Create the mutable data into which we will receive the listing.
  119. self.listData = [NSMutableData data];
  120. assert(self.listData != nil);
  121. // Open a CFFTPStream for the URL.
  122. ftpStream = CFReadStreamCreateWithFTPURL(NULL, (CFURLRef) url);
  123. assert(ftpStream != NULL);
  124. self.networkStream = (NSInputStream *) ftpStream;
  125. self.networkStream.delegate = self;
  126. [self.networkStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  127. [self.networkStream open];
  128. // Have to release ftpStream to balance out the create. self.networkStream
  129. // has retained this for our persistent use.
  130. CFRelease(ftpStream);
  131. // Tell the UI we're receiving.
  132. [self _receiveDidStart];
  133. }
  134. }
  135. - (void)_stopReceiveWithStatus:(NSString *)statusString
  136. // Shuts down the connection and displays the result (statusString == nil)
  137. // or the error status (otherwise).
  138. {
  139. if (self.networkStream != nil) {
  140. [self.networkStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  141. self.networkStream.delegate = nil;
  142. [self.networkStream close];
  143. self.networkStream = nil;
  144. }
  145. [self _receiveDidStopWithStatus:statusString];
  146. self.listData = nil;
  147. }
  148. - (NSDictionary *)_entryByReencodingNameInEntry:(NSDictionary *)entry encoding:(NSStringEncoding)newEncoding
  149. // CFFTPCreateParsedResourceListing always interprets the file name as MacRoman,
  150. // which is clearly bogus <rdar://problem/7420589>. This code attempts to fix
  151. // that by converting the Unicode name back to MacRoman (to get the original bytes;
  152. // this works because there's a lossless round trip between MacRoman and Unicode)
  153. // and then reconverting those bytes to Unicode using the encoding provided.
  154. {
  155. NSDictionary * result;
  156. NSString * name;
  157. NSData * nameData;
  158. NSString * newName;
  159. newName = nil;
  160. // Try to get the name, convert it back to MacRoman, and then reconvert it
  161. // with the preferred encoding.
  162. name = [entry objectForKey:(id) kCFFTPResourceName];
  163. if (name != nil) {
  164. assert([name isKindOfClass:[NSString class]]);
  165. nameData = [name dataUsingEncoding:NSMacOSRomanStringEncoding];
  166. if (nameData != nil) {
  167. newName = [[[NSString alloc] initWithData:nameData encoding:newEncoding] autorelease];
  168. }
  169. }
  170. // If the above failed, just return the entry unmodified. If it succeeded,
  171. // make a copy of the entry and replace the name with the new name that we
  172. // calculated.
  173. if (newName == nil) {
  174. assert(NO); // in the debug builds, if this fails, we should investigate why
  175. result = (NSDictionary *) entry;
  176. } else {
  177. NSMutableDictionary * newEntry;
  178. newEntry = [[entry mutableCopy] autorelease];
  179. assert(newEntry != nil);
  180. [newEntry setObject:newName forKey:(id) kCFFTPResourceName];
  181. result = newEntry;
  182. }
  183. return result;
  184. }
  185. - (void)_parseListData
  186. {
  187. NSMutableArray * newEntries;
  188. NSUInteger offset;
  189. // We accumulate the new entries into an array to avoid a) adding items to the
  190. // table one-by-one, and b) repeatedly shuffling the listData buffer around.
  191. newEntries = [NSMutableArray array];
  192. assert(newEntries != nil);
  193. offset = 0;
  194. do {
  195. CFIndex bytesConsumed;
  196. CFDictionaryRef thisEntry;
  197. thisEntry = NULL;
  198. assert(offset <= self.listData.length);
  199. bytesConsumed = CFFTPCreateParsedResourceListing(NULL, &((const uint8_t *) self.listData.bytes)[offset], self.listData.length - offset, &thisEntry);
  200. if (bytesConsumed > 0) {
  201. // It is possible for CFFTPCreateParsedResourceListing to return a
  202. // positive number but not create a parse dictionary. For example,
  203. // if the end of the listing text contains stuff that can't be parsed,
  204. // CFFTPCreateParsedResourceListing returns a positive number (to tell
  205. // the caller that it has consumed the data), but doesn't create a parse
  206. // dictionary (because it couldn't make sense of the data). So, it's
  207. // important that we check for NULL.
  208. if (thisEntry != NULL) {
  209. NSDictionary * entryToAdd;
  210. // Try to interpret the name as UTF-8, which makes things work properly
  211. // with many UNIX-like systems, including the Mac OS X built-in FTP
  212. // server. If you have some idea what type of text your target system
  213. // is going to return, you could tweak this encoding. For example,
  214. // if you know that the target system is running Windows, then
  215. // NSWindowsCP1252StringEncoding would be a good choice here.
  216. //
  217. // Alternatively you could let the user choose the encoding up
  218. // front, or reencode the listing after they've seen it and decided
  219. // it's wrong.
  220. //
  221. // Ain't FTP a wonderful protocol!
  222. entryToAdd = [self _entryByReencodingNameInEntry:(NSDictionary *) thisEntry encoding:NSUTF8StringEncoding];
  223. [newEntries addObject:entryToAdd];
  224. }
  225. // We consume the bytes regardless of whether we get an entry.
  226. offset += bytesConsumed;
  227. }
  228. if (thisEntry != NULL) {
  229. CFRelease(thisEntry);
  230. }
  231. if (bytesConsumed == 0) {
  232. // We haven't yet got enough data to parse an entry. Wait for more data
  233. // to arrive.
  234. break;
  235. } else if (bytesConsumed < 0) {
  236. // We totally failed to parse the listing. Fail.
  237. [self _stopReceiveWithStatus:@"Listing parse failed"];
  238. break;
  239. }
  240. } while (YES);
  241. if (newEntries.count != 0) {
  242. [self _addListEntries:newEntries];
  243. }
  244. if (offset != 0) {
  245. [self.listData replaceBytesInRange:NSMakeRange(0, offset) withBytes:NULL length:0];
  246. }
  247. }
  248. - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
  249. // An NSStream delegate callback that's called when events happen on our
  250. // network stream.
  251. {
  252. #pragma unused(aStream)
  253. assert(aStream == self.networkStream);
  254. switch (eventCode) {
  255. case NSStreamEventOpenCompleted: {
  256. [self _updateStatus:@"Opened connection"];
  257. } break;
  258. case NSStreamEventHasBytesAvailable: {
  259. NSInteger bytesRead;
  260. uint8_t buffer[32768];
  261. [self _updateStatus:@"Receiving"];
  262. // Pull some data off the network.
  263. bytesRead = [self.networkStream read:buffer maxLength:sizeof(buffer)];
  264. if (bytesRead == -1) {
  265. [self _stopReceiveWithStatus:@"Network read error"];
  266. } else if (bytesRead == 0) {
  267. [self _stopReceiveWithStatus:nil];
  268. } else {
  269. assert(self.listData != nil);
  270. // Append the data to our listing buffer.
  271. [self.listData appendBytes:buffer length:bytesRead];
  272. // Check the listing buffer for any complete entries and update
  273. // the UI if we find any.
  274. [self _parseListData];
  275. }
  276. } break;
  277. case NSStreamEventHasSpaceAvailable: {
  278. assert(NO); // should never happen for the output stream
  279. } break;
  280. case NSStreamEventErrorOccurred: {
  281. [self _stopReceiveWithStatus:@"Stream open error"];
  282. } break;
  283. case NSStreamEventEndEncountered: {
  284. // ignore
  285. } break;
  286. default: {
  287. assert(NO);
  288. } break;
  289. }
  290. }
  291. #pragma mark * UI actions
  292. - (IBAction)listOrCancelAction:(id)sender
  293. {
  294. #pragma unused(sender)
  295. if (self.isReceiving) {
  296. [self _stopReceiveWithStatus:@"Cancelled"];
  297. } else {
  298. [self _startReceive];
  299. }
  300. }
  301. - (void)textFieldDidEndEditing:(UITextField *)textField
  302. // A delegate method called by the URL text field when the editing is complete.
  303. // We save the current value of the field in our settings.
  304. {
  305. #pragma unused(textField)
  306. NSString * newValue;
  307. NSString * oldValue;
  308. assert(textField == self.urlText);
  309. newValue = self.urlText.text;
  310. oldValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"ListURLText"];
  311. // Save the URL text if it's changed.
  312. assert(newValue != nil); // what is UITextField thinking!?!
  313. assert(oldValue != nil); // because we registered a default
  314. if ( ! [newValue isEqual:oldValue] ) {
  315. [[NSUserDefaults standardUserDefaults] setObject:newValue forKey:@"ListURLText"];
  316. }
  317. }
  318. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  319. // A delegate method called by the URL text field when the user taps the Return
  320. // key. We just dismiss the keyboard.
  321. {
  322. #pragma unused(textField)
  323. assert(textField == self.urlText);
  324. [self.urlText resignFirstResponder];
  325. return NO;
  326. }
  327. #pragma mark * Table view data source and delegate
  328. - (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section
  329. {
  330. #pragma unused(tv)
  331. #pragma unused(section)
  332. assert(tv == self.tableView);
  333. assert(section == 0);
  334. return self.listEntries.count + 1;
  335. }
  336. - (NSString *)_stringForNumber:(double)num asUnits:(NSString *)units
  337. {
  338. NSString * result;
  339. double fractional;
  340. double integral;
  341. fractional = modf(num, &integral);
  342. if ( (fractional < 0.1) || (fractional > 0.9) ) {
  343. result = [NSString stringWithFormat:@"%.0f %@", round(num), units];
  344. } else {
  345. result = [NSString stringWithFormat:@"%.1f %@", num, units];
  346. }
  347. return result;
  348. }
  349. - (NSString *)_stringForFileSize:(unsigned long long)fileSizeExact
  350. {
  351. double fileSize;
  352. NSString * result;
  353. fileSize = (double) fileSizeExact;
  354. if (fileSizeExact == 1) {
  355. result = @"1 byte";
  356. } else if (fileSizeExact < 1024) {
  357. result = [NSString stringWithFormat:@"%llu bytes", fileSizeExact];
  358. } else if (fileSize < (1024.0 * 1024.0 * 0.1)) {
  359. result = [self _stringForNumber:fileSize / 1024.0 asUnits:@"KB"];
  360. } else if (fileSize < (1024.0 * 1024.0 * 1024.0 * 0.1)) {
  361. result = [self _stringForNumber:fileSize / (1024.0 * 1024.0) asUnits:@"MB"];
  362. } else {
  363. result = [self _stringForNumber:fileSize / (1024.0 * 1024.0 * 1024.0) asUnits:@"MB"];
  364. }
  365. return result;
  366. }
  367. static NSDateFormatter * sDateFormatter;
  368. - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
  369. {
  370. UITableViewCell * cell;
  371. NSDictionary * listEntry;
  372. NSNumber * typeNum;
  373. int type;
  374. NSNumber * sizeNum;
  375. NSString * sizeStr;
  376. NSNumber * modeNum;
  377. char modeCStr[12];
  378. NSDate * date;
  379. NSString * dateStr;
  380. #pragma unused(tv)
  381. assert(tv == self.tableView);
  382. assert(indexPath != nil);
  383. assert(indexPath.section == 0);
  384. assert(indexPath.row < (self.listEntries.count + 1));
  385. if (indexPath.row == 0) {
  386. cell = [self.tableView dequeueReusableCellWithIdentifier:@"StatusCell"];
  387. if (cell == nil) {
  388. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"StatusCell"] autorelease];
  389. }
  390. assert(cell != nil);
  391. cell.textLabel.text = self.status;
  392. cell.textLabel.font = [UIFont systemFontOfSize:17.0f];
  393. cell.textLabel.textAlignment = UITextAlignmentCenter;
  394. } else {
  395. cell = [self.tableView dequeueReusableCellWithIdentifier:@"ListCell"];
  396. if (cell == nil) {
  397. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ListCell"] autorelease];
  398. }
  399. assert(cell != nil);
  400. listEntry = [self.listEntries objectAtIndex:indexPath.row - 1];
  401. assert([listEntry isKindOfClass:[NSDictionary class]]);
  402. // The first line of the cell is the item name.
  403. cell.textLabel.text = [listEntry objectForKey:(id) kCFFTPResourceName];
  404. // Use the second line of the cell to show various attributes.
  405. typeNum = [listEntry objectForKey:(id) kCFFTPResourceType];
  406. if (typeNum != nil) {
  407. assert([typeNum isKindOfClass:[NSNumber class]]);
  408. type = [typeNum intValue];
  409. } else {
  410. type = 0;
  411. }
  412. modeNum = [listEntry objectForKey:(id) kCFFTPResourceMode];
  413. if (modeNum != nil) {
  414. assert([modeNum isKindOfClass:[NSNumber class]]);
  415. strmode([modeNum intValue] + DTTOIF(type), modeCStr);
  416. } else {
  417. strlcat(modeCStr, "???????????", sizeof(modeCStr));
  418. }
  419. sizeNum = [listEntry objectForKey:(id) kCFFTPResourceSize];
  420. if (sizeNum != nil) {
  421. if (type == DT_REG) {
  422. assert([sizeNum isKindOfClass:[NSNumber class]]);
  423. sizeStr = [self _stringForFileSize:[sizeNum unsignedLongLongValue]];
  424. } else {
  425. sizeStr = @"-";
  426. }
  427. } else {
  428. sizeStr = @"?";
  429. }
  430. date = [listEntry objectForKey:(id) kCFFTPResourceModDate];
  431. if (date != nil) {
  432. if (sDateFormatter == nil) {
  433. sDateFormatter = [[NSDateFormatter alloc] init];
  434. assert(sDateFormatter != nil);
  435. sDateFormatter.dateStyle = NSDateFormatterShortStyle;
  436. sDateFormatter.timeStyle = NSDateFormatterShortStyle;
  437. }
  438. dateStr = [sDateFormatter stringFromDate:date];
  439. } else {
  440. dateStr = @"";
  441. }
  442. cell.detailTextLabel.text = [NSString stringWithFormat:@"%s %@ %@", modeCStr, sizeStr, dateStr];
  443. }
  444. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  445. return cell;
  446. }
  447. #pragma mark * View controller boilerplate
  448. @synthesize urlText = _urlText;
  449. @synthesize activityIndicator = _activityIndicator;
  450. @synthesize tableView = _tableView;
  451. @synthesize listOrCancelButton = _listOrCancelButton;
  452. @synthesize status = _status;
  453. - (void)viewDidLoad
  454. {
  455. [super viewDidLoad];
  456. assert(self.urlText != nil);
  457. assert(self.activityIndicator != nil);
  458. assert(self.tableView != nil);
  459. assert(self.listOrCancelButton != nil);
  460. self.listOrCancelButton.possibleTitles = [NSSet setWithObjects:@"List", @"Cancel", nil];
  461. if (self.listEntries == nil) {
  462. self.listEntries = [NSMutableArray array];
  463. assert(self.listEntries != nil);
  464. }
  465. self.urlText.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"ListURLText"];
  466. self.activityIndicator.hidden = YES;
  467. [self _updateStatus:@"Tap a picture to start listing"];
  468. }
  469. - (void)viewDidUnload
  470. {
  471. [super viewDidUnload];
  472. self.urlText = nil;
  473. self.activityIndicator = nil;
  474. self.tableView = nil;
  475. self.listOrCancelButton = nil;
  476. }
  477. - (void)dealloc
  478. {
  479. [self _stopReceiveWithStatus:@"Stopped"];
  480. [self->_listEntries release];
  481. [self->_status release];
  482. [self->_urlText release];
  483. [self->_activityIndicator release];
  484. [self->_tableView release];
  485. [self->_listOrCancelButton release];
  486. [super dealloc];
  487. }
  488. @end