// // SharingAccountsViewController.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 "SharingAccountsViewController.h" #import "UIViewController+Conveniences.h" #import "ContainersViewController.h" #import "PithosImageViewController.h" #import "OpenStackRequest.h" #import "OpenStackAccount.h" #import "AccountManager.h" #import "ActivityIndicatorView.h" #import "SBJSON.h" #import "AccountHomeViewController.h" #import "APICallback.h" @implementation SharingAccountsViewController @synthesize tableView, account, sharingAccounts, accountHomeViewController; #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @"Sharing Accounts"; [self addHomeButton]; sharingAccounts = [[NSMutableArray alloc] init]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) { PithosImageViewController *vc = [[PithosImageViewController alloc] initWithNibName:@"PithosImageViewController" bundle:nil]; [self presentPrimaryViewController:vc]; [vc release]; } [self refreshButtonPressed:nil]; } - (void)viewDidUnload { [super viewDidUnload]; account.sharingAccount = nil; } #pragma mark - Memory management - (void)dealloc { [tableView release]; [account release]; [sharingAccounts release]; [super dealloc]; } #pragma mark - Button Handlers - (IBAction)homeButtonPressed:(id)sender { [self.navigationController popToViewController:accountHomeViewController animated:YES]; } - (IBAction)refreshButtonPressed:(id)sender { __block ActivityIndicatorView *activityIndicatorView = [ActivityIndicatorView activityIndicatorViewWithText:@"Loading..." andAddToView:self.view]; [[self.account.manager getSharingAccounts] success:^(OpenStackRequest *request) { SBJSON *parser = [[SBJSON alloc] init]; NSArray *jsonObjects = [parser objectWithString:[request responseString]]; [sharingAccounts removeAllObjects]; for (int i = 0; i < [jsonObjects count]; i++) { NSDictionary *dict = [jsonObjects objectAtIndex:i]; [sharingAccounts addObject:[dict objectForKey:@"name"]]; } [parser release]; contentsLoaded = YES; [activityIndicatorView removeFromSuperview]; [self.tableView reloadData]; } failure:^(OpenStackRequest *request) { contentsLoaded = NO; [activityIndicatorView removeFromSuperview]; if (request.responseStatusCode != 0) { [self alert:@"There was a problem loading sharing accounts." request:request addAccountSettingsButton:YES]; } }]; } #pragma mark - UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if ([sharingAccounts count] == 0) { self.tableView.allowsSelection = NO; self.tableView.scrollEnabled = NO; } else { self.tableView.allowsSelection = YES; self.tableView.scrollEnabled = YES; } return MAX(contentsLoaded ? 1 : 0, [sharingAccounts count]); } - (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if ([sharingAccounts count] == 0) { return aTableView.frame.size.height; } else { return aTableView.rowHeight; } } - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if ([sharingAccounts count] == 0) { return [self tableView:tableView emptyCellWithImage:[UIImage imageNamed:@"OthersSharedLarge.png"] title:@"No sharing accounts" subtitle:@""]; } else { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } cell.textLabel.text = [sharingAccounts objectAtIndex:indexPath.row]; cell.imageView.image = [UIImage imageNamed:@"myShared.png"]; return cell; } } #pragma mark - UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if ([sharingAccounts count] == 0) return; ContainersViewController *vc = [[ContainersViewController alloc] initWithNibName:@"ContainersViewController" bundle:nil]; account.shared = NO; account.sharingAccount = [sharingAccounts objectAtIndex:indexPath.row]; vc.account = account; vc.accountHomeViewController = accountHomeViewController; [self.navigationController pushViewController:vc animated:YES]; [vc release]; [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; } @end