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