Expanded open file functionality to use available apps.
[pithos-ios] / Classes / LoadBalancersViewController.m
1 //
2 //  LoadBalancersViewController.m
3 //  OpenStack
4 //
5 //  Created by Michael Mayo on 2/18/11.
6 //  Copyright 2011 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "LoadBalancersViewController.h"
10 #import "OpenStackAccount.h"
11 #import "AccountManager.h"
12 #import "LoadBalancer.h"
13 #import "NSObject+Conveniences.h"
14 #import "UIViewController+Conveniences.h"
15 #import "LoadBalancerViewController.h"
16 #import "AddLoadBalancerViewController.h"
17 #import "APICallback.h"
18 #import "UIViewController+Conveniences.h"
19 #import "LoadBalancerProtocol.h"
20 #import "OpenStackAppDelegate.h"
21 #import "RootViewController.h"
22
23
24 @implementation LoadBalancersViewController
25
26 @synthesize account, tableView;
27
28 #pragma mark -
29 #pragma mark View lifecycle
30
31 - (void)viewDidLoad {
32     [super viewDidLoad];
33     self.navigationItem.title = @"Load Balancers";
34     [self addAddButton];
35     
36     algorithmNames = [[NSDictionary alloc] initWithObjectsAndKeys:
37                       @"Random",@"RANDOM", 
38                       @"Round Robin", @"ROUND_ROBIN", 
39                       @"Weighted Round Robin", @"WEIGHTED_ROUND_ROBIN", 
40                       @"Least Connections", @"LEAST_CONNECTIONS", 
41                       @"Weighted Least Connections", @"WEIGHTED_LEAST_CONNECTIONS", 
42                       nil];
43 }
44
45 - (void)viewWillAppear:(BOOL)animated {
46     [super viewWillAppear:animated];
47     [self.tableView reloadData];
48 }
49
50 - (void)viewDidAppear:(BOOL)animated {
51     [super viewDidAppear:animated];
52     if (!lbsLoaded) {
53         [self refreshButtonPressed:nil];
54     }
55 }
56
57 /*
58 // Override to allow orientations other than the default portrait orientation.
59 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
60     // Return YES for supported orientations.
61     return (interfaceOrientation == UIInterfaceOrientationPortrait);
62 }
63 */
64
65 #pragma mark -
66 #pragma mark Table view data source
67
68 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
69     return 1;
70 }
71
72
73 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
74     return [self.account.sortedLoadBalancers count];
75 }
76
77 // Customize the appearance of table view cells.
78 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
79     
80     static NSString *CellIdentifier = @"Cell";
81     
82     UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
83     if (cell == nil) {
84         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
85     }
86     
87     LoadBalancer *loadBalancer = [self.account.sortedLoadBalancers objectAtIndex:indexPath.row];
88     cell.textLabel.text = loadBalancer.name;
89     cell.detailTextLabel.text = [NSString stringWithFormat:@"%@:%i - %@", loadBalancer.protocol.name, loadBalancer.protocol.port, [algorithmNames objectForKey:loadBalancer.algorithm]];
90 //    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@:%i - %@", loadBalancer.status, loadBalancer.protocol.port, [algorithmNames objectForKey:loadBalancer.algorithm]];
91     cell.imageView.image = [UIImage imageNamed:@"load-balancers-icon.png"];
92     
93     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
94         cell.accessoryType = UITableViewCellAccessoryNone;
95     } else {
96         cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
97     }
98     return cell;
99 }
100
101
102 /*
103 // Override to support conditional editing of the table view.
104 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
105     // Return NO if you do not want the specified item to be editable.
106     return YES;
107 }
108 */
109
110 #pragma mark - Table view delegate
111
112 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
113     LoadBalancer *loadBalancer = [self.account.sortedLoadBalancers objectAtIndex:indexPath.row];
114     LoadBalancerViewController *vc = [[LoadBalancerViewController alloc] initWithLoadBalancer:loadBalancer];
115     vc.account = self.account;
116     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
117         [self presentPrimaryViewController:vc];
118 //        if (loaded) {
119             OpenStackAppDelegate *app = [[UIApplication sharedApplication] delegate];
120             if (app.rootViewController.popoverController != nil) {
121                 [app.rootViewController.popoverController dismissPopoverAnimated:YES];
122             }
123 //        }
124     } else {
125         [self.navigationController pushViewController:vc animated:YES];
126     }
127     
128     [vc release];
129 }
130
131 #pragma - Button Handlers
132
133 - (void)addButtonPressed:(id)sender {
134     lbsLoaded = NO; // refresh the list when we come back
135     AddLoadBalancerViewController *vc = [[AddLoadBalancerViewController alloc] initWithAccount:self.account];
136     [self presentModalViewControllerWithNavigation:vc];
137     [vc release];
138 }
139
140 - (IBAction)refreshButtonPressed:(id)sender {
141     
142     lbsLoaded = YES;
143     [self showToolbarActivityMessage:@"Refreshing load balancers..."];
144     __block NSInteger refreshCount = 0;
145     
146     for (NSString *endpoint in [self.account loadBalancerURLs]) {
147         [[self.account.manager getLoadBalancers:endpoint] success:^(OpenStackRequest *request) {
148             refreshCount++;
149             if (refreshCount == [[self.account loadBalancerURLs] count]) {
150                 [self hideToolbarActivityMessage];
151             }
152             [self.tableView reloadData];
153         } failure:^(OpenStackRequest *request) {
154             refreshCount++;
155             if (refreshCount == [[self.account loadBalancerURLs] count]) {
156                 [self hideToolbarActivityMessage];
157             }
158         }];
159     }
160     
161 }
162
163 #pragma mark -
164 #pragma mark Memory management
165
166 - (void)viewDidUnload {
167     self.tableView = nil;
168     self.toolbar = nil;
169     [super viewDidUnload];
170 }
171
172 - (void)dealloc {
173     [account release];
174     [tableView release];
175     [algorithmNames release];
176     [super dealloc];
177 }
178
179
180 @end
181