Fix compile errors
[pithos-ios] / Classes / ObjectVersionsViewController.m
1 //
2 //  ObjectVersionsViewController.m
3 //  pithos-ios
4 //
5 // Copyright 2011 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
50 @implementation ObjectVersionsViewController
51
52 @synthesize tableView, versions, account, container, object;
53
54 #pragma mark - View lifecycle
55
56 - (void)viewDidLoad {
57     [super viewDidLoad];
58     self.navigationItem.title = @"Versions";
59     self.versions = [NSMutableArray array];
60     versionsLoaded = NO;
61     NSString *activityMessage = @"Loading..";
62     [activityIndicatorView release];
63     activityIndicatorView = [[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:activityMessage] text:activityMessage];
64     [activityIndicatorView addToView:self.view];
65
66     [[self.account.manager  getObjectVersionsList:container object:object]
67      success:^(OpenStackRequest *request) {
68          SBJSON *parser = [[SBJSON alloc] init];
69          NSArray *versionsInJson = [[parser objectWithString:[request responseString]] objectForKey:@"versions"];
70          for (NSArray *versionInfo in versionsInJson) {
71              [versions addObject:[NSDictionary dictionaryWithObjectsAndKeys:[versionInfo objectAtIndex:0], @"versionID",
72                                  [NSDate dateWithTimeIntervalSince1970:[[versionInfo objectAtIndex:1] doubleValue]], @"versionDate",
73                                  nil]]; 
74          }
75          [parser release];
76          [activityIndicatorView removeFromSuperview];
77          versionsLoaded = YES;
78          [self.tableView reloadData];
79      }
80      failure:^(OpenStackRequest *request) {
81          [activityIndicatorView removeFromSuperview];
82          [self alert:@"Couldn't get versions from server" request:request];
83      }];    
84 }
85
86 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
87     return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
88 }
89
90 #pragma mark - memory management
91
92 - (void)dealloc {
93     [account release];
94     [container release];
95     [object release];
96     [versions release];
97     [super dealloc];
98 }
99
100
101 #pragma mark - Table view data source
102
103 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
104     return 1;
105 }
106
107 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
108     if (!versionsLoaded)
109         return 0;
110     else
111         return MAX(1, versions.count);
112 }
113
114 - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
115     static NSString *CellIdentifier = @"Cell";
116     
117     UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
118     if (cell == nil) {
119         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
120     }
121     cell.userInteractionEnabled = YES;
122     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
123     cell.selectionStyle = UITableViewCellSelectionStyleBlue;
124     if (versions.count == 0 || !versions) {
125         cell.textLabel.text = @"No previous versions available";
126         cell.userInteractionEnabled = NO;
127         cell.accessoryType = UITableViewCellAccessoryNone;
128         cell.selectionStyle = UITableViewCellSelectionStyleNone;
129     } else {
130         NSDictionary *versionDetails = [versions objectAtIndex:indexPath.row];
131         cell.textLabel.text = [[versionDetails objectForKey:@"versionID"] description];
132         NSString *dateString = [NSDateFormatter localizedStringFromDate:[versionDetails objectForKey:@"versionDate"]
133                                                               dateStyle:NSDateFormatterMediumStyle
134                                                               timeStyle:NSDateFormatterMediumStyle];
135         cell.detailTextLabel.text = dateString;
136     }
137     
138     return cell;
139 }
140
141 #pragma mark - Table view delegate
142
143 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
144     NSString *activityMessage = @"Loading..";
145     [activityIndicatorView release];
146     activityIndicatorView = [[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:activityMessage] text:activityMessage];
147     [activityIndicatorView addToView:self.view];
148     
149     NSDictionary *versionDetails = [versions objectAtIndex:indexPath.row];
150     NSString *versionID = [[versionDetails objectForKey:@"versionID"] description];
151     
152     [[self.account.manager getObjectInfo:container object:object version:versionID]
153      success:^(OpenStackRequest *request) {
154          [activityIndicatorView removeFromSuperview];
155          StorageObject *versionedObject = [[[StorageObject alloc] init] autorelease];
156          versionedObject.name = object.name;
157          versionedObject.fullPath = object.fullPath;
158          [versionedObject setPropertiesfromResponseHeaders:request.responseHeaders];
159          StorageObjectViewController *vc = [[StorageObjectViewController alloc] initWithNibName:@"StorageObjectViewController" bundle:nil];
160          vc.objectIsReadOnly = YES;
161          vc.account = account;
162          vc.container = container;
163          vc.object = versionedObject;
164          vc.versionID = versionID;
165          [self.navigationController pushViewController:vc animated:YES];
166          [vc release];
167          [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
168      } 
169      failure:^(OpenStackRequest *request) {
170          [activityIndicatorView removeFromSuperview];
171          [self alert:[NSString stringWithFormat:@"Failed to get file info for version: %@", versionID] request:request];
172      }];
173 }
174
175 @end