Use service catalog and update version
[pithos-ios] / Classes / ErrorAlerter.m
1 //
2 //  ErrorAlerter.m
3 //  OpenStack
4 //
5 //  Created by Mike Mayo on 1/17/11.
6 //  Copyright 2011 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "ErrorAlerter.h"
10 #import "LogEntryModalViewController.h"
11 #import "OpenStackRequest.h"
12 #import "UIViewController+Conveniences.h"
13 #import "APILogEntry.h"
14 #import "OpenStackAppDelegate.h"
15 #import "RootViewController.h"
16 #import "AccountHomeViewController.h"
17
18 @implementation ErrorAlerter
19
20 @synthesize failedRequest, logEntryModalViewController, viewController;
21
22 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
23     if (buttonIndex == 1) {
24         // "Details" button
25         if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
26             self.logEntryModalViewController.modalPresentationStyle = UIModalPresentationFormSheet;
27         }
28         // XXX If the viewController is removed from the windows hierarchy, for a late request e.g.,
29         // this will do nothing and result in a warning.
30         [self.viewController presentModalViewController:self.logEntryModalViewController animated:YES];
31     } else if (buttonIndex == 2) {
32         // "Account Settings" button
33         OpenStackAppDelegate *app = [[UIApplication sharedApplication] delegate];
34         if (app.rootViewController.navigationController.viewControllers.count == 1) {
35             // Push the AccountHomeViewController in the stack and then push the AccountSettingsViewController.
36             [app.rootViewController presentAccountHomeViewControllerForAccount:self.failedRequest.account
37                                                                       animated:([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)];
38             AccountHomeViewController *ahvc = [app.rootViewController.navigationController.viewControllers objectAtIndex:1];
39             [ahvc presentAccountSettingsViewControllerAnimated:YES];
40         } else {
41             // Already in the stack, pop to the AccountHomeViewController and then push the AccountSettingsViewController.
42             AccountHomeViewController *ahvc = [app.rootViewController.navigationController.viewControllers objectAtIndex:1];
43             [self.viewController.navigationController popToViewController:ahvc
44                                                                  animated:([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)];
45             [ahvc presentAccountSettingsViewControllerAnimated:YES];
46         }
47         if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
48             [app.rootViewController.popoverController dismissPopoverAnimated:YES];
49         }
50     }
51 }
52
53 - (void)alert:(NSString *)message request:(OpenStackRequest *)request viewController:(UIViewController *)aViewController
54                  addAccountSettingsButton:(BOOL)addAccountSettingsButton {
55     self.failedRequest = request;
56     self.viewController = aViewController;
57
58     NSString *title = @"Error";
59     if (request.responseStatusCode == 0) {
60         title = @"Connection Error";
61         message = [NSString stringWithFormat:@"Please check your connection or API URL and try again. Error: %@", request.error];
62     }
63
64     self.logEntryModalViewController = [[[LogEntryModalViewController alloc] initWithNibName:@"LogEntryModalViewController" bundle:nil] autorelease];
65     self.logEntryModalViewController.logEntry = [[[APILogEntry alloc] initWithRequest:request] autorelease];
66     self.logEntryModalViewController.requestDescription = [logEntryModalViewController.logEntry requestDescription];
67     self.logEntryModalViewController.responseDescription = [logEntryModalViewController.logEntry responseDescription];
68     self.logEntryModalViewController.requestMethod = [logEntryModalViewController.logEntry requestMethod];
69     self.logEntryModalViewController.url = [[logEntryModalViewController.logEntry url] description];
70     
71     // present an alert with a Details button to show the API log entry
72     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Details", nil];
73     
74     if (addAccountSettingsButton && ((request.responseStatusCode == 401) || (request.responseStatusCode == 403))) {
75         [alert addButtonWithTitle:@"Account Settings"];
76     }
77     
78     [alert show];
79     [alert release];        
80 }
81
82 - (void)alert:(NSString *)message request:(OpenStackRequest *)request viewController:(UIViewController *)aViewController {
83     [self alert:message request:request viewController:aViewController addAccountSettingsButton:NO];
84 }
85
86 - (void)dealloc {
87     [failedRequest release];
88     [logEntryModalViewController release];
89     [viewController release];
90     [super dealloc];
91 }
92
93
94 @end