// // ObjectVersionsViewController.m // pithos-ios // // Copyright 2011 GRNET S.A. All rights reserved. // // Redistribution and use in source and binary forms, with or // without modification, are permitted provided that the following // conditions are met: // // 1. Redistributions of source code must retain the above // copyright notice, this list of conditions and the following // disclaimer. // // 2. Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following // disclaimer in the documentation and/or other materials // provided with the distribution. // // THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. // // The views and conclusions contained in the software and // documentation are those of the authors and should not be // interpreted as representing official policies, either expressed // or implied, of GRNET S.A. #import "ObjectVersionsViewController.h" #import "StorageObjectViewController.h" #import "OpenStackAccount.h" #import "OpenStackRequest.h" #import "AccountManager.h" #import "APICallback.h" #import "JSON.h" #import "ActivityIndicatorView.h" #import "UIViewController+Conveniences.h" #import "StorageObject.h" @implementation ObjectVersionsViewController @synthesize versions, account, container, object; #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @"Versions"; self.versions = [NSMutableArray array]; versionsLoaded = NO; __block ActivityIndicatorView *activityIndicatorView = [ActivityIndicatorView activityIndicatorViewWithText:@"Loading..." andAddToView:self.view]; [[self.account.manager getObjectVersionsList:container object:object] success:^(OpenStackRequest *request) { SBJSON *parser = [[SBJSON alloc] init]; NSArray *versionsInJson = [[parser objectWithString:[request responseString]] objectForKey:@"versions"]; for (NSArray *versionInfo in versionsInJson) { [versions addObject:[NSDictionary dictionaryWithObjectsAndKeys:[versionInfo objectAtIndex:0], @"versionID", [NSDate dateWithTimeIntervalSince1970:[[versionInfo objectAtIndex:1] doubleValue]], @"versionDate", nil]]; } [parser release]; [activityIndicatorView removeFromSuperview]; versionsLoaded = YES; [self.tableView reloadData]; } failure:^(OpenStackRequest *request) { [activityIndicatorView removeFromSuperview]; [self alert:@"Couldn't get versions from server." request:request]; }]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait); } #pragma mark - Memory management - (void)dealloc { [account release]; [container release]; [object release]; [versions release]; [super dealloc]; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (!versionsLoaded) return 0; else return MAX(1, versions.count); } - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; } cell.userInteractionEnabled = YES; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.selectionStyle = UITableViewCellSelectionStyleBlue; if (versions.count == 0 || !versions) { cell.textLabel.text = @"No previous versions available"; cell.userInteractionEnabled = NO; cell.accessoryType = UITableViewCellAccessoryNone; cell.selectionStyle = UITableViewCellSelectionStyleNone; } else { NSDictionary *versionDetails = [versions objectAtIndex:indexPath.row]; cell.textLabel.text = [[versionDetails objectForKey:@"versionID"] description]; NSString *dateString = [NSDateFormatter localizedStringFromDate:[versionDetails objectForKey:@"versionDate"] dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterMediumStyle]; cell.detailTextLabel.text = dateString; } return cell; } #pragma mark - Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { __block ActivityIndicatorView *activityIndicatorView = [ActivityIndicatorView activityIndicatorViewWithText:@"Loading..." andAddToView:self.view]; NSDictionary *versionDetails = [versions objectAtIndex:indexPath.row]; NSString *versionID = [[versionDetails objectForKey:@"versionID"] description]; [[self.account.manager getObjectInfo:container object:object version:versionID] success:^(OpenStackRequest *request) { [activityIndicatorView removeFromSuperview]; StorageObject *versionedObject = [[[StorageObject alloc] init] autorelease]; versionedObject.name = object.name; versionedObject.fullPath = object.fullPath; [versionedObject setPropertiesfromResponseHeaders:request.responseHeaders]; StorageObjectViewController *vc = [[StorageObjectViewController alloc] initWithNibName:@"StorageObjectViewController" bundle:nil]; vc.objectIsReadOnly = YES; vc.account = account; vc.container = container; vc.object = versionedObject; vc.versionID = versionID; [self.navigationController pushViewController:vc animated:YES]; [vc release]; [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; } failure:^(OpenStackRequest *request) { [activityIndicatorView removeFromSuperview]; [self alert:[NSString stringWithFormat:@"Failed to get file info for version: %@", versionID] request:request]; }]; } @end