| 1 | /* |
| 2 | * PushrNetUtil.m |
| 3 | * -------------- |
| 4 | * |
| 5 | * Author: Chris Lee <clee@mg8.org> |
| 6 | * License: GPL v2 <http://www.opensource.org/licenses/gpl-license.php> |
| 7 | */ |
| 8 | |
| 9 | #import <Foundation/Foundation.h> |
| 10 | #import <UIKit/UIKit.h> |
| 11 | #import "PushrNetUtil.h" |
| 12 | |
| 13 | #include <sys/types.h> |
| 14 | #include <sys/socket.h> |
| 15 | #include <ifaddrs.h> |
| 16 | |
| 17 | @implementation PushrNetUtil |
| 18 | |
| 19 | - (id)initWithPushr: (MobilePushr *)pushr |
| 20 | { |
| 21 | if (![super init]) { |
| 22 | return nil; |
| 23 | } |
| 24 | |
| 25 | _pushr = [pushr retain]; |
| 26 | _activeInterfaceNames = [[NSMutableArray alloc] initWithCapacity: 0]; |
| 27 | |
| 28 | struct ifaddrs *first_ifaddr, *current_ifaddr; |
| 29 | getifaddrs(&first_ifaddr); |
| 30 | current_ifaddr = first_ifaddr; |
| 31 | while (current_ifaddr != NULL) { |
| 32 | if (current_ifaddr->ifa_addr->sa_family == 0x02) |
| 33 | [_activeInterfaceNames addObject: [NSString stringWithFormat: @"%s", current_ifaddr->ifa_name]]; |
| 34 | current_ifaddr = current_ifaddr->ifa_next; |
| 35 | } |
| 36 | freeifaddrs(first_ifaddr); |
| 37 | |
| 38 | return self; |
| 39 | } |
| 40 | |
| 41 | - (void)dealloc |
| 42 | { |
| 43 | [_activeInterfaceNames release]; |
| 44 | [_pushr release]; |
| 45 | [super dealloc]; |
| 46 | } |
| 47 | |
| 48 | - (void)warnUserAboutSlowEDGE |
| 49 | { |
| 50 | UIAlertSheet *alertSheet = [[UIAlertSheet alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 240.0f)]; |
| 51 | [alertSheet setTitle: @"This might take a while"]; |
| 52 | [alertSheet setBodyText: @"You don't seem to have an active WiFi connection, and pushing photos over EDGE is really slow. Still want to push over EDGE?"]; |
| 53 | [alertSheet addButtonWithTitle: @"Push over EDGE"]; |
| 54 | [alertSheet addButtonWithTitle: @"Try again later"]; |
| 55 | [alertSheet setDelegate: self]; |
| 56 | [alertSheet setRunsModal: YES]; |
| 57 | [alertSheet popupAlertAnimated: YES]; |
| 58 | } |
| 59 | |
| 60 | - (void)drownWithoutNetwork |
| 61 | { |
| 62 | UIAlertSheet *alertSheet = [[UIAlertSheet alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 240.0f)]; |
| 63 | [alertSheet setTitle: @"No network available"]; |
| 64 | [alertSheet setBodyText: @"Pushr doesn't work if it can't talk to Flickr, and right now, no network connections are active."]; |
| 65 | [alertSheet addButtonWithTitle: @"Try again later"]; |
| 66 | [alertSheet setDelegate: _pushr]; |
| 67 | [alertSheet setRunsModal: YES]; |
| 68 | [alertSheet popupAlertAnimated: YES]; |
| 69 | } |
| 70 | |
| 71 | - (void) alertSheet: (UIAlertSheet *)sheet buttonClicked: (int)button |
| 72 | { |
| 73 | [sheet dismiss]; |
| 74 | [sheet release]; |
| 75 | |
| 76 | switch (button) { |
| 77 | case 1: { |
| 78 | NSLog(@"Told the user EDGE was slow, but they want to push anyway..."); |
| 79 | break; |
| 80 | } |
| 81 | default: { |
| 82 | [_pushr terminate]; |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | - (NSArray *)activeInterfaceNames |
| 89 | { |
| 90 | return [NSArray arrayWithArray: _activeInterfaceNames]; |
| 91 | } |
| 92 | |
| 93 | - (BOOL)hasWiFi |
| 94 | { |
| 95 | NSArray *activeInterfaces = [self activeInterfaceNames]; |
| 96 | return [activeInterfaces containsObject: @"en0"] || [activeInterfaces containsObject: @"en1"]; |
| 97 | } |
| 98 | |
| 99 | - (BOOL)hasEDGE |
| 100 | { |
| 101 | NSArray *activeInterfaces = [self activeInterfaceNames]; |
| 102 | return [activeInterfaces containsObject: @"ip0"] || [activeInterfaces containsObject: @"ip1"]; |
| 103 | } |
| 104 | |
| 105 | @end |