Change default authURL
[pithos-ios] / Classes / ObjectVersionsViewController.m
1 //
2 //  ObjectVersionsViewController.m
3 //  pithos-ios
4 //
5 // Copyright 2011-2012 GRNET S.A. All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or
8 // without modification, are permitted provided that the following
9 // conditions are met:
10 // 
11 //   1. Redistributions of source code must retain the above
12 //      copyright notice, this list of conditions and the following
13 //      disclaimer.
14 // 
15 //   2. Redistributions in binary form must reproduce the above
16 //      copyright notice, this list of conditions and the following
17 //      disclaimer in the documentation and/or other materials
18 //      provided with the distribution.
19 // 
20 // THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
21 // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
24 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 // POSSIBILITY OF SUCH DAMAGE.
32 // 
33 // The views and conclusions contained in the software and
34 // documentation are those of the authors and should not be
35 // interpreted as representing official policies, either expressed
36 // or implied, of GRNET S.A.
37
38 #import "ObjectVersionsViewController.h"
39 #import "StorageObjectViewController.h"
40 #import "OpenStackAccount.h"
41 #import "OpenStackRequest.h"
42 #import "AccountManager.h"
43 #import "APICallback.h"
44 #import "JSON.h"
45 #import "ActivityIndicatorView.h"
46 #import "UIViewController+Conveniences.h"
47 #import "StorageObject.h"
48
49 @implementation ObjectVersionsViewController
50
51 @synthesize versions, account, container, object;
52
53 #pragma mark - View lifecycle
54
55 - (void)viewDidLoad {
56     [super viewDidLoad];
57     self.navigationItem.title = @"Versions";
58     self.versions = [NSMutableArray array];
59     versionsLoaded = NO;
60
61     __block ActivityIndicatorView *activityIndicatorView = [ActivityIndicatorView activityIndicatorViewWithText:@"Loading..."
62                                                                                                    andAddToView:self.view];
63     [[self.account.manager getObjectVersionsList:container object:object]
64      success:^(OpenStackRequest *request) {
65          [versions addObjectsFromArray:[request versions]];
66          [activityIndicatorView stopAnimatingAndRemoveFromSuperview];
67          versionsLoaded = YES;
68          [self.tableView reloadData];
69      }
70      failure:^(OpenStackRequest *request) {
71          [activityIndicatorView stopAnimatingAndRemoveFromSuperview];
72          [self alert:@"Couldn't get versions from server." request:request];
73      }];    
74 }
75
76 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
77     return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
78 }
79
80 #pragma mark - Memory management
81
82 - (void)dealloc {
83     [account release];
84     [container release];
85     [object release];
86     [versions release];
87     [super dealloc];
88 }
89
90 #pragma mark - Table view data source
91
92 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
93     return 1;
94 }
95
96 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
97     if (!versionsLoaded)
98         return 0;
99     else
100         return MAX(1, versions.count);
101 }
102
103 - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
104     static NSString *CellIdentifier = @"Cell";
105     
106     UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
107     if (cell == nil) {
108         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
109     }
110     cell.userInteractionEnabled = YES;
111     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
112     cell.selectionStyle = UITableViewCellSelectionStyleBlue;
113     if (versions.count == 0 || !versions) {
114         cell.textLabel.text = @"No previous versions available";
115         cell.userInteractionEnabled = NO;
116         cell.accessoryType = UITableViewCellAccessoryNone;
117         cell.selectionStyle = UITableViewCellSelectionStyleNone;
118     } else {
119         NSDictionary *versionDetails = [versions objectAtIndex:indexPath.row];
120         cell.textLabel.text = [[versionDetails objectForKey:@"versionID"] description];
121         NSString *dateString = [NSDateFormatter localizedStringFromDate:[versionDetails objectForKey:@"versionDate"]
122                                                               dateStyle:NSDateFormatterMediumStyle
123                                                               timeStyle:NSDateFormatterMediumStyle];
124         cell.detailTextLabel.text = dateString;
125     }
126     
127     return cell;
128 }
129
130 #pragma mark - Table view delegate
131
132 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
133     __block ActivityIndicatorView *activityIndicatorView = [ActivityIndicatorView activityIndicatorViewWithText:@"Loading..."
134                                                                                                    andAddToView:self.view];
135     NSDictionary *versionDetails = [versions objectAtIndex:indexPath.row];
136     NSString *versionID = [[versionDetails objectForKey:@"versionID"] description];
137     [[self.account.manager getObjectInfo:container object:object version:versionID]
138      success:^(OpenStackRequest *request) {
139          [activityIndicatorView stopAnimatingAndRemoveFromSuperview];
140          StorageObject *versionedObject = [[[StorageObject alloc] init] autorelease];
141          versionedObject.name = object.name;
142          versionedObject.fullPath = object.fullPath;
143          [versionedObject setPropertiesfromResponseHeaders:request.responseHeaders];
144          StorageObjectViewController *vc = [[StorageObjectViewController alloc] initWithNibName:@"StorageObjectViewController" bundle:nil];
145          vc.objectIsReadOnly = YES;
146          vc.account = account;
147          vc.container = container;
148          vc.object = versionedObject;
149          vc.versionID = versionID;
150          [self.navigationController pushViewController:vc animated:YES];
151          [vc release];
152          [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
153      } 
154      failure:^(OpenStackRequest *request) {
155          [activityIndicatorView stopAnimatingAndRemoveFromSuperview];
156          [self alert:[NSString stringWithFormat:@"Failed to get file info for version: %@", versionID] request:request];
157      }];
158 }
159
160 @end