+- (void)openFile {
+ OpenStackAppDelegate *app = [[UIApplication sharedApplication] delegate];
+ NSString *filePath = [app cacheFilePathForHash:object.hash];
+ self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
+ self.documentInteractionController.delegate = self;
+ self.documentInteractionController.name = object.name;
+
+ CGRect frameToPresentMenuFrom;
+ if ([[UIDevice currentDevice].systemVersion hasPrefix:@"6"]) {
+ frameToPresentMenuFrom = CGRectMake(0.0,
+ self.tableView.contentOffset.y,
+ self.view.frame.size.width,
+ 1.0);
+ } else {
+ UITableViewCell *openFileCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:kActions]];
+ frameToPresentMenuFrom = CGRectMake(openFileCell.frame.origin.x,
+ openFileCell.frame.origin.y - self.tableView.contentOffset.y,
+ openFileCell.frame.size.width,
+ openFileCell.frame.size.height);
+ }
+ if (![self.documentInteractionController presentOptionsMenuFromRect:frameToPresentMenuFrom inView:self.view animated:YES]) {
+ if ([self.object isPlayableMedia]) {
+ MediaViewController *vc = [[MediaViewController alloc] initWithNibName:@"MediaViewController" bundle:nil];
+ vc.container = self.container;
+ vc.object = self.object;
+ [self.navigationController pushViewController:vc animated:YES];
+ [vc release];
+ } else {
+ [self alert:@"Error" message:@"This file could not be opened."];
+ }
+ }
+}
+
+- (void)mailFile {
+ if (![MFMailComposeViewController canSendMail]) {
+ [self alert:@"Cannot send mail" message:@"Your device has not been configured for sending mail"];
+ } else {
+ OpenStackAppDelegate *app = [[UIApplication sharedApplication] delegate];
+ NSString *filePath = [app cacheFilePathForHash:object.hash];
+ NSData *data = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:filePath]];
+
+ MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init];
+ vc.mailComposeDelegate = self;
+ [vc setSubject:object.name];
+ [vc addAttachmentData:data mimeType:object.contentType fileName:object.name];
+ [vc setMessageBody:@"" isHTML:NO];
+ if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
+ vc.modalPresentationStyle = UIModalPresentationPageSheet;
+ [self presentModalViewController:vc animated:YES];
+ [vc release];
+ }
+}
+
+- (void)updatePermissionsUserCatalog {
+ NSMutableArray *UUIDs = [NSMutableArray arrayWithCapacity:permissions.count];
+ for (NSString *user in permissions) {
+ NSRange rangeOfColumn = [user rangeOfString:@":"];
+ NSString *UUID = (rangeOfColumn.location == NSNotFound) ? user : [user substringToIndex:rangeOfColumn.location];
+ if (![UUID isEqualToString:@"*"]) {
+ [UUIDs addObject:UUID];
+ }
+ }
+ if (UUIDs.count) {
+ __block ActivityIndicatorView *activityIndicatorView = [ActivityIndicatorView activityIndicatorViewWithText:@"Loading permissions..."
+ andAddToView:self.view];
+ [[self.account.manager userCatalogForDisplaynames:nil UUIDs:UUIDs]
+ success:^(OpenStackRequest *request) {
+ [activityIndicatorView stopAnimatingAndRemoveFromSuperview];
+ [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:permissionsSection] withRowAnimation:UITableViewRowAnimationNone];
+ }
+ failure:^(OpenStackRequest *request) {
+ [activityIndicatorView stopAnimatingAndRemoveFromSuperview];
+ if (request.responseStatusCode != 404) {
+ // Don't show alert on 404, since it can be a pre-UUID server.
+ [self alert:@"Failed to translate sharing UUIDs." request:request];
+ }
+ }];
+ }
+}
+
+- (void)delete {
+ if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
+ folderViewController.deletedObject = object;
+ [self.navigationController popViewControllerAnimated:YES];
+ } else {
+ [folderViewController deleteAnimatedObject:object];
+ [folderViewController setDetailViewController];
+ }
+}
+
+#pragma mark - Actions
+
+- (void)reloadMetadataSection {
+ __block ActivityIndicatorView *activityIndicatorView = [ActivityIndicatorView activityIndicatorViewWithText:@"Loading metadata..."
+ andAddToView:self.view];
+ [[self.account.manager getObjectInfo:container object:object version:versionID]
+ success:^(OpenStackRequest *request) {
+ [activityIndicatorView stopAnimatingAndRemoveFromSuperview];
+ object.metadata = [NSMutableDictionary dictionary];
+ for (NSString *header in request.responseHeaders) {
+ NSString *metadataKey;
+ NSString *metadataValue;
+ if ([header rangeOfString:@"X-Object-Meta-"].location != NSNotFound) {
+ metadataKey = [NSString decodeFromPercentEscape:[header substringFromIndex:14]];
+ metadataValue = [NSString decodeFromPercentEscape:[request.responseHeaders objectForKey:header]];
+ [object.metadata setObject:metadataValue forKey:metadataKey];
+ }
+ }
+ [self.tableView reloadData];
+ [self updatePermissionsUserCatalog];
+ }
+ failure:^(OpenStackRequest *request) {
+ [activityIndicatorView stopAnimatingAndRemoveFromSuperview];
+ [self alert:@"There was a problem retrieving the object's metadata." request:request];
+ }];
+}
+
+- (void)downloadFileForAction:(StorageObjectAction)action {
+ if (fileDownloading)
+ return;
+ fileDownloading = YES;
+ [downloadProgressView setProgress:0.0 animated:NO];
+ [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:kActions] withRowAnimation:UITableViewRowAnimationNone];
+ [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:kActions]
+ atScrollPosition:UITableViewScrollPositionMiddle
+ animated:YES];
+ NSMutableDictionary *requestUserInfo = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:action] forKey:@"action"];
+ [[self.account.manager getObject:self.container object:self.object downloadProgressDelegate:self requestUserInfo:requestUserInfo version:versionID]
+ success:^(OpenStackRequest *request) {
+ if (request.isCancelled) {
+ fileDownloaded = NO;
+ fileDownloading = NO;
+ [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:kActions] withRowAnimation:UITableViewRowAnimationNone];
+ } else {
+ fileDownloaded = YES;
+ fileDownloading = NO;
+ if ([self.navigationController.visibleViewController isEqual:self]) {
+ if ([[request.userInfo objectForKey:@"action"] integerValue] == StorageObjectActionMailFile) {
+ [self mailFile];
+ } else {
+ [self openFile];
+ }
+ }
+ [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:kActions] withRowAnimation:UITableViewRowAnimationNone];
+ }
+ }
+ failure:^(OpenStackRequest *request) {
+ fileDownloaded = NO;
+ fileDownloading = NO;
+ [self alert:@"File failed to download." request:request];
+ [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:kActions] withRowAnimation:UITableViewRowAnimationNone];
+ }];
+}
+
+#pragma mark - UITableViewDataSource