root / Classes / SharingAccountsViewController.m @ c2940e36
History | View | Annotate | Download (6.8 kB)
1 |
// |
---|---|
2 |
// SharingAccountsViewController.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 "SharingAccountsViewController.h" |
39 |
#import "UIViewController+Conveniences.h" |
40 |
#import "ContainersViewController.h" |
41 |
#import "PithosImageViewController.h" |
42 |
#import "OpenStackRequest.h" |
43 |
#import "OpenStackAccount.h" |
44 |
#import "AccountManager.h" |
45 |
#import "ActivityIndicatorView.h" |
46 |
#import "AccountHomeViewController.h" |
47 |
#import "APICallback.h" |
48 |
|
49 |
@implementation SharingAccountsViewController |
50 |
|
51 |
@synthesize tableView, account, sharingAccounts, accountHomeViewController; |
52 |
|
53 |
#pragma mark - View lifecycle |
54 |
|
55 |
- (void)viewDidLoad { |
56 |
[super viewDidLoad]; |
57 |
self.navigationItem.title = @"Sharing Accounts"; |
58 |
[self addHomeButton]; |
59 |
sharingAccounts = [[NSMutableArray alloc] init]; |
60 |
} |
61 |
|
62 |
- (void)viewWillAppear:(BOOL)animated { |
63 |
[super viewWillAppear:animated]; |
64 |
|
65 |
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) { |
66 |
PithosImageViewController *vc = [[PithosImageViewController alloc] initWithNibName:@"PithosImageViewController" bundle:nil]; |
67 |
[self presentPrimaryViewController:vc]; |
68 |
[vc release]; |
69 |
} |
70 |
|
71 |
[self refreshButtonPressed:nil]; |
72 |
} |
73 |
|
74 |
- (void)viewDidUnload { |
75 |
[super viewDidUnload]; |
76 |
account.sharingAccount = nil; |
77 |
} |
78 |
|
79 |
#pragma mark - Memory management |
80 |
|
81 |
- (void)dealloc { |
82 |
[tableView release]; |
83 |
[account release]; |
84 |
[sharingAccounts release]; |
85 |
[super dealloc]; |
86 |
} |
87 |
|
88 |
#pragma mark - Button Handlers |
89 |
|
90 |
- (IBAction)homeButtonPressed:(id)sender { |
91 |
[self.navigationController popToViewController:accountHomeViewController animated:YES]; |
92 |
} |
93 |
|
94 |
- (IBAction)refreshButtonPressed:(id)sender { |
95 |
__block ActivityIndicatorView *activityIndicatorView = [ActivityIndicatorView activityIndicatorViewWithText:@"Loading..." |
96 |
andAddToView:self.view]; |
97 |
[[self.account.manager getSharingAccounts] |
98 |
success:^(OpenStackRequest *request) { |
99 |
[sharingAccounts removeAllObjects]; |
100 |
for (NSDictionary *dict in [request sharingAccounts]) { |
101 |
[sharingAccounts addObject:[dict objectForKey:@"name"]]; |
102 |
} |
103 |
[[self.account.manager userCatalogForDisplaynames:nil |
104 |
UUIDs:sharingAccounts] |
105 |
success:^(OpenStackRequest *request) { |
106 |
contentsLoaded = YES; |
107 |
[activityIndicatorView stopAnimatingAndRemoveFromSuperview]; |
108 |
[self.tableView reloadData]; |
109 |
} |
110 |
failure:^(OpenStackRequest *request) { |
111 |
contentsLoaded = YES; |
112 |
[activityIndicatorView stopAnimatingAndRemoveFromSuperview]; |
113 |
[self.tableView reloadData]; |
114 |
}]; |
115 |
} |
116 |
failure:^(OpenStackRequest *request) { |
117 |
contentsLoaded = NO; |
118 |
[activityIndicatorView stopAnimatingAndRemoveFromSuperview]; |
119 |
if (request.responseStatusCode != 0) { |
120 |
[self alert:@"There was a problem loading sharing accounts." request:request addAccountSettingsButton:YES]; |
121 |
} |
122 |
}]; |
123 |
} |
124 |
|
125 |
#pragma mark - UITableViewDataSource |
126 |
|
127 |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
128 |
if ([sharingAccounts count] == 0) { |
129 |
self.tableView.allowsSelection = NO; |
130 |
self.tableView.scrollEnabled = NO; |
131 |
} else { |
132 |
self.tableView.allowsSelection = YES; |
133 |
self.tableView.scrollEnabled = YES; |
134 |
} |
135 |
return MAX(contentsLoaded ? 1 : 0, [sharingAccounts count]); |
136 |
} |
137 |
|
138 |
- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
139 |
if ([sharingAccounts count] == 0) { |
140 |
return aTableView.frame.size.height; |
141 |
} else { |
142 |
return aTableView.rowHeight; |
143 |
} |
144 |
} |
145 |
|
146 |
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
147 |
if ([sharingAccounts count] == 0) { |
148 |
return [self tableView:tableView emptyCellWithImage:[UIImage imageNamed:@"OthersSharedLarge.png"] |
149 |
title:@"No sharing accounts" |
150 |
subtitle:@""]; |
151 |
} else { |
152 |
static NSString *CellIdentifier = @"Cell"; |
153 |
|
154 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
155 |
if (cell == nil) { |
156 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; |
157 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
158 |
} |
159 |
cell.textLabel.text = [account displaynameForUUID:[sharingAccounts objectAtIndex:indexPath.row] safe:YES]; |
160 |
cell.imageView.image = [UIImage imageNamed:@"myShared.png"]; |
161 |
|
162 |
return cell; |
163 |
} |
164 |
} |
165 |
|
166 |
#pragma mark - UITableViewDelegate |
167 |
|
168 |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
169 |
if ([sharingAccounts count] == 0) |
170 |
return; |
171 |
|
172 |
ContainersViewController *vc = [[ContainersViewController alloc] initWithNibName:@"ContainersViewController" bundle:nil]; |
173 |
account.shared = NO; |
174 |
account.sharingAccount = [sharingAccounts objectAtIndex:indexPath.row]; |
175 |
vc.account = account; |
176 |
vc.accountHomeViewController = accountHomeViewController; |
177 |
[self.navigationController pushViewController:vc animated:YES]; |
178 |
[vc release]; |
179 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
180 |
} |
181 |
|
182 |
@end |