Expanded open file functionality to use available apps.
[pithos-ios] / Classes / OpenStackViewController.m
1 //
2 //  OpenStackViewController.m
3 //  OpenStack
4 //
5 //  Created by Mike Mayo on 10/21/10.
6 //  The OpenStack project is provided under the Apache 2.0 license.
7 //
8
9 #import "OpenStackViewController.h"
10 #import "LogEntryModalViewController.h"
11 #import "OpenStackRequest.h"
12 #import "OpenStackAccount.h"
13 #import "UIViewController+Conveniences.h"
14 #import "APILogEntry.h"
15 #import "AnimatedProgressView.h"
16
17
18 @implementation OpenStackViewController
19
20 @synthesize toolbar, selectedIndexPath;
21
22 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
23     return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
24 }
25
26 - (void)viewDidLoad {
27     if (self.navigationController.navigationBar) {
28         if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad) {
29             self.toolbar.tintColor = self.navigationController.navigationBar.tintColor;
30             self.toolbar.translucent = self.navigationController.navigationBar.translucent;
31             self.toolbar.opaque = self.navigationController.navigationBar.opaque;
32             self.toolbar.barStyle = self.navigationController.navigationBar.barStyle;
33         } else {
34             self.toolbar.translucent = NO;
35             self.toolbar.opaque = NO;            
36             self.toolbar.tintColor = [UIColor blackColor];
37         }
38     }
39     
40     toolbarProgressView = [[AnimatedProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
41     toolbarProgressView.frame = CGRectMake(0.0, 24.0, 185.0, 10.0);
42 }
43
44 - (void)showToolbarInfoMessage:(NSString *)text {    
45     if (toolbarInfoMessageVisible) {
46         toolbarLabel.text = text;
47         
48     }
49     else {
50         UIFont *font = [UIFont boldSystemFontOfSize:12.0];
51         CGSize stringSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(226.0, 20.0f) lineBreakMode:UILineBreakModeTailTruncation];
52
53         toolbarLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 12.0, stringSize.width, 20.0)];
54         toolbarLabel.textColor = [UIColor whiteColor];
55         toolbarLabel.textAlignment = UITextAlignmentLeft;
56         toolbarLabel.font = font;
57         toolbarLabel.backgroundColor = [UIColor clearColor];
58         toolbarLabel.shadowOffset = CGSizeMake(0, -1.0);
59         toolbarLabel.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.4];
60         toolbarLabel.text = text;
61     
62         NSMutableArray *items;
63     
64         toolbarLabelItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarLabel];
65         items = [NSMutableArray arrayWithArray:toolbar.items];
66         [items insertObject:toolbarLabelItem atIndex:2];
67     
68         toolbar.items = [NSArray arrayWithArray:items];
69     }
70     
71     toolbarInfoMessageVisible = YES;
72 }
73
74 - (void)showToolbarActivityMessage:(NSString *)text progress:(BOOL)hasProgress {
75     UIFont *font = [UIFont boldSystemFontOfSize:12.0];
76     CGSize stringSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(226.0, 20.0f) lineBreakMode:UILineBreakModeTailTruncation];
77     if (toolbarActivityMessageVisible) {
78         //[self hideToolbarActivityMessage];
79         [toolbarLabel setFrame:CGRectMake(10.0, 12.0, stringSize.width, 20.0)];
80         toolbarLabel.text = text;        
81     } else {
82         toolbarLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 12.0, stringSize.width, 20.0)];
83         toolbarLabel.textColor = [UIColor whiteColor];
84         toolbarLabel.textAlignment = UITextAlignmentLeft;
85         toolbarLabel.font = font;
86         toolbarLabel.backgroundColor = [UIColor clearColor];
87         toolbarLabel.shadowOffset = CGSizeMake(0, -1.0);
88         toolbarLabel.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.4];
89         toolbarLabel.text = text;
90         
91         NSMutableArray *items;
92         
93         if (hasProgress) {
94             
95             //toolbarLabel.frame = CGRectMake(0.0, 2.0, stringSize.width, 20.0);
96             toolbarLabel.frame = CGRectMake(0.0, 2.0, 185.0, 20.0);
97             toolbarLabel.textAlignment = UITextAlignmentCenter;
98             
99             UIView *labelWithProgress = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 185.0, 40.0)];
100             
101             [labelWithProgress addSubview:toolbarLabel];
102             [labelWithProgress addSubview:toolbarProgressView];
103             
104             toolbarLabelItem = [[UIBarButtonItem alloc] initWithCustomView:labelWithProgress];
105             //toolbarLabelItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarProgressView];
106             
107             [toolbarProgressView setProgress:0.40 animated:YES];
108             
109             items = [NSMutableArray arrayWithArray:toolbar.items];
110             
111             [items insertObject:toolbarLabelItem atIndex:1];
112             
113             [labelWithProgress release];
114         } else {
115             toolbarActivityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
116             toolbarActivityIndicatorView.frame = CGRectMake(10.0, 12.0, 20.0, 20.0);
117             [toolbarActivityIndicatorView startAnimating];
118             
119             toolbarActivityIndicatorItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarActivityIndicatorView];
120             toolbarLabelItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarLabel];
121             
122             items = [NSMutableArray arrayWithArray:toolbar.items];
123             
124             if ([items count] > 2) {
125                 [items insertObject:toolbarActivityIndicatorItem atIndex:2];
126                 [items insertObject:toolbarLabelItem atIndex:3];
127             } else {
128                 [items insertObject:toolbarActivityIndicatorItem atIndex:1];
129                 [items insertObject:toolbarLabelItem atIndex:2];
130             }
131         }
132         
133         toolbar.items = [NSArray arrayWithArray:items];
134         
135         toolbarActivityMessageVisible = YES;
136     }    
137 }
138
139 - (void)showToolbarActivityMessage:(NSString *)text {
140     [self showToolbarActivityMessage:text progress:NO];
141 }
142
143 - (void)hideToolbarInfoMessage {
144     
145     if (toolbarInfoMessageVisible) {
146         NSMutableArray *items = [NSMutableArray arrayWithArray:toolbar.items];
147         [items removeObject:toolbarLabelItem];
148         toolbar.items = [NSArray arrayWithArray:items];    
149     
150         [toolbarLabelItem release];
151         
152         [toolbarLabel removeFromSuperview];
153         [toolbarLabel release];
154         
155         toolbarInfoMessageVisible = NO;
156     }
157     
158 }
159
160
161 - (void)hideToolbarActivityMessage {
162
163     if (toolbarActivityMessageVisible) {
164         NSMutableArray *items = [NSMutableArray arrayWithArray:toolbar.items];
165         [items removeObject:toolbarActivityIndicatorItem];
166         [items removeObject:toolbarLabelItem];
167         toolbar.items = [NSArray arrayWithArray:items];    
168
169         [toolbarActivityIndicatorItem release];
170         [toolbarLabelItem release];
171         
172         [toolbarLabel removeFromSuperview];
173         [toolbarLabel release];
174         
175         [toolbarActivityIndicatorView removeFromSuperview];
176         [toolbarActivityIndicatorView release];
177         toolbarActivityMessageVisible = NO;
178     }
179 }
180
181 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
182     selectedIndexPath = indexPath;
183 }
184
185 - (void)viewWillAppear:(BOOL)animated {
186     [super viewWillAppear:animated];
187 //    if (selectedIndexPath && [self respondsToSelector:@selector(tableView)]) {
188 //        UITableView *tv = [self performSelector:@selector(tableView)];
189 //        [tv deselectRowAtIndexPath:selectedIndexPath animated:YES];        
190 //    }
191 }
192
193 - (void)dealloc {
194     if (toolbarActivityMessageVisible) {
195         [self hideToolbarActivityMessage];
196     }
197     if (toolbarInfoMessageVisible) {
198         [self hideToolbarInfoMessage];
199     }
200     [toolbarProgressView release];
201     [selectedIndexPath release];
202     [super dealloc];
203 }
204
205 @end