#import "OCMimeType.h"
#import "RSTextFieldCell.h"
#import "NSObject+Conveniences.h"
+#import "APICallback.h"
+#import "ComputeModel.h"
#define kName 0
#define kContentType 1
-
@implementation UploadGenericFileViewController
@synthesize account, container, folder, folderViewController, data, format, contentType, contentTypeEditable;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
- return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
+ return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
-#pragma mark -
-#pragma mark View lifecycle
+#pragma mark - View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated {
- if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
+ if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(98.0, 13.0, 400.0, 24.0)];
} else {
nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(79.0, 13.0, 222.0, 24.0)];
nameTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
nameTextField.placeholder = [NSString stringWithFormat:@"ios_upload_%.0f", [[NSDate date] timeIntervalSince1970]];
- if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
+ if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
formatLabel = [[UILabel alloc] initWithFrame:CGRectMake(41.0, 15.5, 458.0, 18.0)];
} else {
formatLabel = [[UILabel alloc] initWithFrame:CGRectMake(21.0, 15.5, 280.0, 18.0)];
// move the text field to make room for the numbers label
CGSize size = [formatLabel.text sizeWithFont:formatLabel.font constrainedToSize:CGSizeMake(280.0, 900.0f)];
- if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
+ if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
nameTextField.frame = CGRectMake(98.0, 13.0, 400.0 - size.width, 24.0);
} else {
nameTextField.frame = CGRectMake(79.0, 13.0, 222.0 - size.width, 24.0);
}
- if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
+ if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
contentTypeTextField = [[UITextField alloc] initWithFrame:CGRectMake(79.0, 13.0, 400.0, 24.0)];
} else {
contentTypeTextField = [[UITextField alloc] initWithFrame:CGRectMake(79.0, 13.0, 222.0, 24.0)];
}
}
-#pragma mark -
-#pragma mark Table view data source
+#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
}
-#pragma mark -
-#pragma mark Save Button
+#pragma mark - Save Button
- (void)saveButtonPressed:(id)sender {
- activityIndicatorView = [[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:@"Uploading..." withProgress:YES] text:@"Uploading..." withProgress:YES];
- [activityIndicatorView addToView:self.view];
-
- StorageObject *object = [[StorageObject alloc] init];
-
+ [nameTextField resignFirstResponder];
+ NSString *objectName;
if (nameTextField.text && ![nameTextField.text isEqualToString:@""]) {
- object.name = [NSString stringWithFormat:@"%@%@", nameTextField.text, format];
+ objectName = [NSString stringWithFormat:@"%@%@", nameTextField.text, format];
} else {
- object.name = [NSString stringWithFormat:@"%@%@", nameTextField.placeholder, format];
+ objectName = [NSString stringWithFormat:@"%@%@", nameTextField.placeholder, format];
+ }
+
+ BOOL objectNameHasTrailingSlash = [objectName hasSuffix:@"/"];
+ NSUInteger indexOfFirstSlash = [objectName rangeOfString:@"/"].location;
+
+ if ((!objectNameHasTrailingSlash && indexOfFirstSlash != NSNotFound) ||
+ (objectNameHasTrailingSlash && indexOfFirstSlash < objectName.length - 1)) {
+ [self alert:@"Invalid folder name" message:@"'/' characters are only allowed at the end of folder names"];
+ return;
}
+
+ if ([folderViewController.folder.objects objectForKey:objectName] && !allowOverwrite) {
+ NSString *alertMessage = [NSString stringWithFormat:@"An object with path '%@' in the container '%@' already exists, do you want to replace it?",objectName, folderViewController.container.name];
+ NSString *alertTitle = @"Apply changes";
+ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertTitle
+ message:alertMessage
+ delegate:self
+ cancelButtonTitle:@"Cancel"
+ otherButtonTitles:@"OK", nil];
+ [alert show];
+ [alert release];
+ return;
+ }
+
+ StorageObject *object = [[StorageObject alloc] init];
+ object.name = objectName;
object.fullPath = [NSString stringWithFormat:@"%@/%@", [folder fullPath], object.name];
object.fullPath = [object.fullPath substringFromIndex:1];
object.contentType = contentTypeTextField.text;
-
if (!contentType) {
object.contentType = @"application/octet-stream";
}
-
object.data = data;
object.bytes = [data length];
- [self.account.manager writeObject:self.container object:object downloadProgressDelegate:activityIndicatorView.progressView];
-
- successObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"writeObjectSucceeded" object:object queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification* notification)
- {
- [activityIndicatorView removeFromSuperviewAndRelease];
- object.data = nil;
- object.sharing = folder.sharing;
- [folder.objects setObject:object forKey:object.name];
- [folderViewController.tableView reloadData];
- [self dismissModalViewControllerAnimated:YES];
- [[NSNotificationCenter defaultCenter] removeObserver:successObserver];
- }];
-
- failureObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"writeObjectFailed" object:object queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification* notification)
- {
- [activityIndicatorView removeFromSuperviewAndRelease];
- [self alert:@"There was a problem uploading the file." request:[notification.userInfo objectForKey:@"request"]];
- [[NSNotificationCenter defaultCenter] removeObserver:failureObserver];
- }];
+ __block ActivityIndicatorView *activityIndicatorView = [ActivityIndicatorView activityIndicatorViewWithText:@"Uploading..."
+ withProgress:YES
+ andAddToView:self.view];
+ [[self.account.manager writeObject:self.container object:object downloadProgressDelegate:activityIndicatorView.progressView]
+ success:^(OpenStackRequest *request) {
+ [activityIndicatorView removeFromSuperview];
+ object.data = nil;
+ object.sharing = folder.sharing;
+ object.lastModified = [ComputeModel dateFromRFC1123String:[request.responseHeaders objectForKey:@"Date"]];
+
+ BOOL currentFolderIsRoot = NO;
+ if ([folderViewController.folder isEqual:container.rootFolder]) {
+ currentFolderIsRoot = YES;
+ }
+ [folder addObject:object];
+ folderViewController.folder = folderViewController.folder;
+ if (currentFolderIsRoot) {
+ container.count += 1;
+ container.rootFolder = folder;
+ [self.account.containers setObject:container forKey:container.name];
+ }
+ if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
+ [folderViewController setDetailViewController];
+ allowOverwrite = NO;
+ [self dismissModalViewControllerAnimated:YES];
+ }
+ failure:^(OpenStackRequest *request) {
+ allowOverwrite = NO;
+ [activityIndicatorView removeFromSuperview];
+ [self alert:@"There was a problem uploading the file." request:request];
+ }];
}
-#pragma mark -
-#pragma mark Text Field Delegate
+#pragma mark - Alertview delegate
+
+- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
+ if (buttonIndex == 1) {
+ allowOverwrite = YES;
+ [self saveButtonPressed:nil];
+ }
+}
+
+
+#pragma mark - Text Field Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
-#pragma mark -
-#pragma mark Memory management
+#pragma mark - Memory management
- (void)dealloc {
[account release];
[super dealloc];
}
-
@end