summaryrefslogtreecommitdiff
path: root/PushrNetUtil.m
blob: ffb0048b21969cb4d5e8dea9b117f7af2fa7deb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * PushrNetUtil.m
 * --------------
 *
 * Author: Chris Lee <clee@mg8.org>
 * License: GPL v2 <http://www.opensource.org/licenses/gpl-license.php>
 */

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "PushrNetUtil.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <ifaddrs.h>

@implementation PushrNetUtil

- (id)initWithPushr: (MobilePushr *)pushr
{
	if (![super init]) {
		return nil;
	}

	_pushr = [pushr retain];
	_activeInterfaceNames = [[NSMutableArray alloc] initWithCapacity: 0];

	struct ifaddrs *first_ifaddr, *current_ifaddr;
	getifaddrs(&first_ifaddr);
	current_ifaddr = first_ifaddr;
	while (current_ifaddr != NULL) {
		if (current_ifaddr->ifa_addr->sa_family == 0x02)
			[_activeInterfaceNames addObject: [NSString stringWithFormat: @"%s", current_ifaddr->ifa_name]];
		current_ifaddr = current_ifaddr->ifa_next;
	}
	freeifaddrs(first_ifaddr);
	
	return self;	
}

- (void)dealloc
{
	[_activeInterfaceNames release];
	[_pushr release];
	[super dealloc];
}

- (void)warnUserAboutSlowEDGE
{
	UIAlertSheet *alertSheet = [[UIAlertSheet alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 240.0f)];
	[alertSheet setTitle: @"This might take a while"];
	[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?"];
	[alertSheet addButtonWithTitle: @"Push over EDGE"];
	[alertSheet addButtonWithTitle: @"Try again later"];
	[alertSheet setDelegate: self];
	[alertSheet setRunsModal: YES];
	[alertSheet popupAlertAnimated: YES];
}

- (void)drownWithoutNetwork
{
	UIAlertSheet *alertSheet = [[UIAlertSheet alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 240.0f)];
	[alertSheet setTitle: @"No network available"];
	[alertSheet setBodyText: @"Pushr doesn't work if it can't talk to Flickr, and right now, no network connections are active."];
	[alertSheet addButtonWithTitle: @"Try again later"];
	[alertSheet setDelegate: _pushr];
	[alertSheet setRunsModal: YES];
	[alertSheet popupAlertAnimated: YES];
}

- (void) alertSheet: (UIAlertSheet *)sheet buttonClicked: (int)button
{
	[sheet dismiss];
	[sheet release];

	switch (button) {
		case 1: {
			NSLog(@"Told the user EDGE was slow, but they want to push anyway...");
			break;
		}
		default: {
			[_pushr terminate];
			break;
		}
	}
}

- (NSArray *)activeInterfaceNames
{
	return [NSArray arrayWithArray: _activeInterfaceNames];
}

- (BOOL)hasWiFi
{
	NSArray *activeInterfaces = [self activeInterfaceNames];
	return [activeInterfaces containsObject: @"en0"] || [activeInterfaces containsObject: @"en1"];
}

- (BOOL)hasEDGE
{
	NSArray *activeInterfaces = [self activeInterfaceNames];
	return [activeInterfaces containsObject: @"ip0"] || [activeInterfaces containsObject: @"ip1"];
}

@end