You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

398 lines
16 KiB

  1. //
  2. // FLEXFileBrowserTableViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 6/9/14.
  6. //
  7. //
  8. #import "FLEXFileBrowserTableViewController.h"
  9. #import "FLEXFileBrowserFileOperationController.h"
  10. #import "FLEXUtility.h"
  11. #import "FLEXWebViewController.h"
  12. #import "FLEXImagePreviewViewController.h"
  13. #import "FLEXTableListViewController.h"
  14. #import "FLEXObjectExplorerFactory.h"
  15. #import "FLEXObjectExplorerViewController.h"
  16. @interface FLEXFileBrowserTableViewCell : UITableViewCell
  17. @end
  18. @interface FLEXFileBrowserTableViewController () <FLEXFileBrowserFileOperationControllerDelegate, FLEXFileBrowserSearchOperationDelegate, UISearchResultsUpdating, UISearchControllerDelegate>
  19. @property (nonatomic, copy) NSString *path;
  20. @property (nonatomic, copy) NSArray<NSString *> *childPaths;
  21. @property (nonatomic, strong) NSArray<NSString *> *searchPaths;
  22. @property (nonatomic, strong) NSNumber *recursiveSize;
  23. @property (nonatomic, strong) NSNumber *searchPathsSize;
  24. @property (nonatomic, strong) UISearchController *searchController;
  25. @property (nonatomic) NSOperationQueue *operationQueue;
  26. @property (nonatomic, strong) UIDocumentInteractionController *documentController;
  27. @property (nonatomic, strong) id<FLEXFileBrowserFileOperationController> fileOperationController;
  28. @end
  29. @implementation FLEXFileBrowserTableViewController
  30. - (id)initWithStyle:(UITableViewStyle)style
  31. {
  32. return [self initWithPath:NSHomeDirectory()];
  33. }
  34. - (id)initWithPath:(NSString *)path
  35. {
  36. self = [super initWithStyle:UITableViewStyleGrouped];
  37. if (self) {
  38. self.path = path;
  39. self.title = [path lastPathComponent];
  40. self.operationQueue = [NSOperationQueue new];
  41. self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
  42. self.searchController.searchResultsUpdater = self;
  43. self.searchController.delegate = self;
  44. self.searchController.dimsBackgroundDuringPresentation = NO;
  45. self.tableView.tableHeaderView = self.searchController.searchBar;
  46. //computing path size
  47. FLEXFileBrowserTableViewController *__weak weakSelf = self;
  48. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  49. NSFileManager *fileManager = [NSFileManager defaultManager];
  50. NSDictionary<NSString *, id> *attributes = [fileManager attributesOfItemAtPath:path error:NULL];
  51. uint64_t totalSize = [attributes fileSize];
  52. for (NSString *fileName in [fileManager enumeratorAtPath:path]) {
  53. attributes = [fileManager attributesOfItemAtPath:[path stringByAppendingPathComponent:fileName] error:NULL];
  54. totalSize += [attributes fileSize];
  55. // Bail if the interested view controller has gone away.
  56. if (!weakSelf) {
  57. return;
  58. }
  59. }
  60. dispatch_async(dispatch_get_main_queue(), ^{
  61. FLEXFileBrowserTableViewController *__strong strongSelf = weakSelf;
  62. strongSelf.recursiveSize = @(totalSize);
  63. [strongSelf.tableView reloadData];
  64. });
  65. });
  66. [self reloadChildPaths];
  67. }
  68. return self;
  69. }
  70. #pragma mark - UIViewController
  71. - (void)viewDidLoad
  72. {
  73. [super viewDidLoad];
  74. }
  75. #pragma mark - Misc
  76. - (void)alert:(NSString *)title message:(NSString *)message {
  77. [[[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
  78. }
  79. #pragma mark - FLEXFileBrowserSearchOperationDelegate
  80. - (void)fileBrowserSearchOperationResult:(NSArray<NSString *> *)searchResult size:(uint64_t)size
  81. {
  82. self.searchPaths = searchResult;
  83. self.searchPathsSize = @(size);
  84. [self.tableView reloadData];
  85. }
  86. #pragma mark - UISearchResultsUpdating
  87. - (void)updateSearchResultsForSearchController:(UISearchController *)searchController
  88. {
  89. [self reloadDisplayedPaths];
  90. }
  91. #pragma mark - UISearchControllerDelegate
  92. - (void)willDismissSearchController:(UISearchController *)searchController
  93. {
  94. [self.operationQueue cancelAllOperations];
  95. [self reloadChildPaths];
  96. [self.tableView reloadData];
  97. }
  98. #pragma mark - Table view data source
  99. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  100. {
  101. return 1;
  102. }
  103. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  104. {
  105. return self.searchController.isActive ? [self.searchPaths count] : [self.childPaths count];
  106. }
  107. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  108. {
  109. BOOL isSearchActive = self.searchController.isActive;
  110. NSNumber *currentSize = isSearchActive ? self.searchPathsSize : self.recursiveSize;
  111. NSArray<NSString *> *currentPaths = isSearchActive ? self.searchPaths : self.childPaths;
  112. NSString *sizeString = nil;
  113. if (!currentSize) {
  114. sizeString = @"Computing size…";
  115. } else {
  116. sizeString = [NSByteCountFormatter stringFromByteCount:[currentSize longLongValue] countStyle:NSByteCountFormatterCountStyleFile];
  117. }
  118. return [NSString stringWithFormat:@"%lu files (%@)", (unsigned long)[currentPaths count], sizeString];
  119. }
  120. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  121. {
  122. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  123. NSDictionary<NSString *, id> *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:NULL];
  124. BOOL isDirectory = [[attributes fileType] isEqual:NSFileTypeDirectory];
  125. NSString *subtitle = nil;
  126. if (isDirectory) {
  127. NSUInteger count = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:fullPath error:NULL] count];
  128. subtitle = [NSString stringWithFormat:@"%lu file%@", (unsigned long)count, (count == 1 ? @"" : @"s")];
  129. } else {
  130. NSString *sizeString = [NSByteCountFormatter stringFromByteCount:[attributes fileSize] countStyle:NSByteCountFormatterCountStyleFile];
  131. subtitle = [NSString stringWithFormat:@"%@ - %@", sizeString, [attributes fileModificationDate]];
  132. }
  133. static NSString *textCellIdentifier = @"textCell";
  134. static NSString *imageCellIdentifier = @"imageCell";
  135. UITableViewCell *cell = nil;
  136. // Separate image and text only cells because otherwise the separator lines get out-of-whack on image cells reused with text only.
  137. BOOL showImagePreview = [FLEXUtility isImagePathExtension:[fullPath pathExtension]];
  138. NSString *cellIdentifier = showImagePreview ? imageCellIdentifier : textCellIdentifier;
  139. if (!cell) {
  140. cell = [[FLEXFileBrowserTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
  141. cell.textLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
  142. cell.detailTextLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
  143. cell.detailTextLabel.textColor = [UIColor grayColor];
  144. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  145. }
  146. NSString *cellTitle = [fullPath lastPathComponent];
  147. cell.textLabel.text = cellTitle;
  148. cell.detailTextLabel.text = subtitle;
  149. if (showImagePreview) {
  150. cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
  151. cell.imageView.image = [UIImage imageWithContentsOfFile:fullPath];
  152. }
  153. return cell;
  154. }
  155. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  156. {
  157. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  158. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  159. NSString *subpath = fullPath.lastPathComponent;
  160. NSString *pathExtension = subpath.pathExtension;
  161. BOOL isDirectory = NO;
  162. BOOL stillExists = [[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDirectory];
  163. if (!stillExists) {
  164. [self alert:@"File Not Found" message:@"The file at the specified path no longer exists."];
  165. [self reloadDisplayedPaths];
  166. return;
  167. }
  168. UIViewController *drillInViewController = nil;
  169. if (isDirectory) {
  170. drillInViewController = [[[self class] alloc] initWithPath:fullPath];
  171. } else if ([FLEXUtility isImagePathExtension:pathExtension]) {
  172. UIImage *image = [UIImage imageWithContentsOfFile:fullPath];
  173. drillInViewController = [[FLEXImagePreviewViewController alloc] initWithImage:image];
  174. } else {
  175. NSData *fileData = [NSData dataWithContentsOfFile:fullPath];
  176. if (!fileData.length) {
  177. [self alert:@"Empty File" message:@"No data returned from the file."];
  178. return;
  179. }
  180. // Special case keyed archives, json, and plists to get more readable data.
  181. NSString *prettyString = nil;
  182. if ([pathExtension isEqualToString:@"json"]) {
  183. prettyString = [FLEXUtility prettyJSONStringFromData:fileData];
  184. } else {
  185. @try {
  186. // Try to decode an archived object regardless of file extension
  187. id object = [NSKeyedUnarchiver unarchiveObjectWithData:fileData];
  188. drillInViewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:object];
  189. } @catch (NSException *e) {
  190. // Try to decode a property list instead, also regardless of file extension
  191. prettyString = [[NSPropertyListSerialization propertyListWithData:fileData options:0 format:NULL error:NULL] description];
  192. }
  193. }
  194. if (prettyString.length) {
  195. drillInViewController = [[FLEXWebViewController alloc] initWithText:prettyString];
  196. } else if ([FLEXWebViewController supportsPathExtension:pathExtension]) {
  197. drillInViewController = [[FLEXWebViewController alloc] initWithURL:[NSURL fileURLWithPath:fullPath]];
  198. } else if ([FLEXTableListViewController supportsExtension:pathExtension]) {
  199. drillInViewController = [[FLEXTableListViewController alloc] initWithPath:fullPath];
  200. }
  201. else if (!drillInViewController) {
  202. NSString *fileString = [NSString stringWithUTF8String:fileData.bytes];
  203. if (fileString.length) {
  204. drillInViewController = [[FLEXWebViewController alloc] initWithText:fileString];
  205. }
  206. }
  207. }
  208. if (drillInViewController) {
  209. drillInViewController.title = subpath.lastPathComponent;
  210. [self.navigationController pushViewController:drillInViewController animated:YES];
  211. } else {
  212. // Share the file otherwise
  213. [self openFileController:fullPath];
  214. }
  215. }
  216. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
  217. {
  218. UIMenuItem *renameMenuItem = [[UIMenuItem alloc] initWithTitle:@"Rename" action:@selector(fileBrowserRename:)];
  219. UIMenuItem *deleteMenuItem = [[UIMenuItem alloc] initWithTitle:@"Delete" action:@selector(fileBrowserDelete:)];
  220. NSMutableArray *menus = [NSMutableArray arrayWithObjects:renameMenuItem, deleteMenuItem, nil];
  221. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  222. NSError *error = nil;
  223. NSDictionary *attributes = [NSFileManager.defaultManager attributesOfItemAtPath:fullPath error:&error];
  224. if (error == nil && [attributes fileType] != NSFileTypeDirectory) {
  225. UIMenuItem *shareMenuItem = [[UIMenuItem alloc] initWithTitle:@"Share" action:@selector(fileBrowserShare:)];
  226. [menus addObject:shareMenuItem];
  227. }
  228. [UIMenuController sharedMenuController].menuItems = menus;
  229. return YES;
  230. }
  231. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  232. {
  233. return action == @selector(fileBrowserDelete:) || action == @selector(fileBrowserRename:) || action == @selector(fileBrowserShare:);
  234. }
  235. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  236. {
  237. // Empty, but has to exist for the menu to show
  238. // The table view only calls this method for actions in the UIResponderStandardEditActions informal protocol.
  239. // Since our actions are outside of that protocol, we need to manually handle the action forwarding from the cells.
  240. }
  241. #pragma mark - FLEXFileBrowserFileOperationControllerDelegate
  242. - (void)fileOperationControllerDidDismiss:(id<FLEXFileBrowserFileOperationController>)controller
  243. {
  244. [self reloadDisplayedPaths];
  245. }
  246. - (void)openFileController:(NSString *)fullPath
  247. {
  248. UIDocumentInteractionController *controller = [UIDocumentInteractionController new];
  249. controller.URL = [[NSURL alloc] initFileURLWithPath:fullPath];
  250. [controller presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
  251. self.documentController = controller;
  252. }
  253. - (void)fileBrowserRename:(UITableViewCell *)sender
  254. {
  255. NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  256. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  257. self.fileOperationController = [[FLEXFileBrowserFileRenameOperationController alloc] initWithPath:fullPath];
  258. self.fileOperationController.delegate = self;
  259. [self.fileOperationController show];
  260. }
  261. - (void)fileBrowserDelete:(UITableViewCell *)sender
  262. {
  263. NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  264. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  265. self.fileOperationController = [[FLEXFileBrowserFileDeleteOperationController alloc] initWithPath:fullPath];
  266. self.fileOperationController.delegate = self;
  267. [self.fileOperationController show];
  268. }
  269. - (void)fileBrowserShare:(UITableViewCell *)sender
  270. {
  271. NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
  272. NSString *fullPath = [self filePathAtIndexPath:indexPath];
  273. UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[fullPath] applicationActivities:nil];
  274. [self presentViewController:activityViewController animated:true completion:nil];
  275. }
  276. - (void)reloadDisplayedPaths
  277. {
  278. if (self.searchController.isActive) {
  279. [self reloadSearchPaths];
  280. } else {
  281. [self reloadChildPaths];
  282. }
  283. [self.tableView reloadData];
  284. }
  285. - (void)reloadChildPaths
  286. {
  287. NSMutableArray<NSString *> *childPaths = [NSMutableArray array];
  288. NSArray<NSString *> *subpaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.path error:NULL];
  289. for (NSString *subpath in subpaths) {
  290. [childPaths addObject:[self.path stringByAppendingPathComponent:subpath]];
  291. }
  292. self.childPaths = childPaths;
  293. }
  294. - (void)reloadSearchPaths
  295. {
  296. self.searchPaths = nil;
  297. self.searchPathsSize = nil;
  298. //clear pre search request and start a new one
  299. [self.operationQueue cancelAllOperations];
  300. FLEXFileBrowserSearchOperation *newOperation = [[FLEXFileBrowserSearchOperation alloc] initWithPath:self.path searchString:self.searchController.searchBar.text];
  301. newOperation.delegate = self;
  302. [self.operationQueue addOperation:newOperation];
  303. }
  304. - (NSString *)filePathAtIndexPath:(NSIndexPath *)indexPath
  305. {
  306. return self.searchController.isActive ? self.searchPaths[indexPath.row] : self.childPaths[indexPath.row];
  307. }
  308. @end
  309. @implementation FLEXFileBrowserTableViewCell
  310. - (void)fileBrowserRename:(UIMenuController *)sender
  311. {
  312. id target = [self.nextResponder targetForAction:_cmd withSender:sender];
  313. [[UIApplication sharedApplication] sendAction:_cmd to:target from:self forEvent:nil];
  314. }
  315. - (void)fileBrowserDelete:(UIMenuController *)sender
  316. {
  317. id target = [self.nextResponder targetForAction:_cmd withSender:sender];
  318. [[UIApplication sharedApplication] sendAction:_cmd to:target from:self forEvent:nil];
  319. }
  320. - (void)fileBrowserShare:(UIMenuController *)sender
  321. {
  322. id target = [self.nextResponder targetForAction:_cmd withSender:sender];
  323. [[UIApplication sharedApplication] sendAction:_cmd to:target from:self forEvent:nil];
  324. }
  325. @end