5 // Created by Mike Mayo on 1/17/11.
6 // Copyright 2011 __MyCompanyName__. All rights reserved.
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"
18 @implementation ErrorAlerter
20 @synthesize failedRequest, logEntryModalViewController, viewController;
22 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
23 if (buttonIndex == 1) {
25 if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
26 self.logEntryModalViewController.modalPresentationStyle = UIModalPresentationFormSheet;
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];
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];
47 if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
48 [app.rootViewController.popoverController dismissPopoverAnimated:YES];
53 - (void)alert:(NSString *)message request:(OpenStackRequest *)request viewController:(UIViewController *)aViewController
54 addAccountSettingsButton:(BOOL)addAccountSettingsButton {
55 self.failedRequest = request;
56 self.viewController = aViewController;
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];
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];
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];
74 if (addAccountSettingsButton && ((request.responseStatusCode == 401) || (request.responseStatusCode == 403))) {
75 [alert addButtonWithTitle:@"Account Settings"];
82 - (void)alert:(NSString *)message request:(OpenStackRequest *)request viewController:(UIViewController *)aViewController {
83 [self alert:message request:request viewController:aViewController addAccountSettingsButton:NO];
87 [failedRequest release];
88 [logEntryModalViewController release];
89 [viewController release];