Handle local/global connections

This commit is contained in:
Yann Diorcet 2012-11-20 11:30:21 +01:00
parent 16c0c38c68
commit a87e5e58e5
9 changed files with 57 additions and 24 deletions

View file

@ -40,6 +40,7 @@ typedef enum _BuschJaegerConfigurationRequestType{
BuschJaegerConfigurationRequestType_Global
} BuschJaegerConfigurationRequestType;
@property (readonly) NSData *homeAP;
@property (readonly) NSMutableSet *history;
@property (readonly) NSMutableSet *users;
@property (readonly) NSMutableSet *outdoorStations;
@ -53,14 +54,15 @@ typedef enum _BuschJaegerConfigurationRequestType{
- (BOOL)saveFile:(NSString*)file;
- (BOOL)parseQRCode:(NSString*)data delegate:(id<BuschJaegerConfigurationDelegate>)delegate;
- (BOOL)loadHistory:(BuschJaegerConfigurationRequestType)type delegate:(id<BuschJaegerConfigurationDelegate>)delegate;
- (BOOL)removeHistory:(BuschJaegerConfigurationRequestType)type history:(History*)history delegate:(id<BuschJaegerConfigurationDelegate>)delegate;
- (BOOL)loadHistory:(id<BuschJaegerConfigurationDelegate>)delegate;
- (BOOL)removeHistory:(History*)history delegate:(id<BuschJaegerConfigurationDelegate>)delegate;
- (User*)getCurrentUser;
- (NSString*)getImageUrl:(BuschJaegerConfigurationRequestType)type image:(NSString *)image;
- (NSString*)getImageUrl:(NSString *)image;
- (NSString*)getGateway:(BuschJaegerConfigurationRequestType)type;
- (BuschJaegerConfigurationRequestType)getCurrentRequestType;
- (NSString*)getGateway;
- (BOOL)canHandleAuthChallenge : (NSURLConnection*)connection : (NSURLProtectionSpace*)protectionSpace;

View file

@ -22,8 +22,11 @@
#import "Utils.h"
#import "NSURLConnection+SynchronousDelegate.h"
static NSString *const CONFIGURATION_HOME_AP_KEY = @"CONFIGURATION_HOME_AP_KEY";
@implementation BuschJaegerConfiguration
@synthesize homeAP;
@synthesize outdoorStations;
@synthesize users;
@synthesize network;
@ -98,11 +101,13 @@
levelPushButton = [[LevelPushButton alloc] init];
certificate = NULL;
[self reloadCertificates];
homeAP = [[[NSUserDefaults standardUserDefaults] dataForKey:CONFIGURATION_HOME_AP_KEY] retain];
}
return self;
}
- (void)dealloc {
[homeAP release];
[outdoorStations release];
[users release];
[history release];
@ -287,7 +292,6 @@
}
- (void)loadCertificates {
if(certificate != NULL) {
CFRelease(certificate);
certificate = NULL;
@ -306,15 +310,18 @@
}
- (void)reset {
[homeAP release];
[history removeAllObjects];
[outdoorStations removeAllObjects];
[users removeAllObjects];
if(network != nil) {
[network release];
network = nil;
}
network = [[Network alloc] init];
if(levelPushButton != nil) {
[levelPushButton release];
levelPushButton = nil;
}
levelPushButton = [[LevelPushButton alloc] init];
}
@ -380,6 +387,8 @@
NSHTTPURLResponse *urlResponse = (NSHTTPURLResponse*) response;
if(urlResponse.statusCode == 200) {
if([self parseConfig:[NSString stringWithUTF8String:[data bytes]] delegate:delegate]) {
homeAP = [[LinphoneManager getWifiData] retain];
[[NSUserDefaults standardUserDefaults] setObject:homeAP forKey:CONFIGURATION_HOME_AP_KEY];
[[NSUserDefaults standardUserDefaults] setObject:userString forKey:@"username_preference"];
[[NSUserDefaults standardUserDefaults] setObject:network.domain forKey:@"domain_preference"];
[[NSUserDefaults standardUserDefaults] setObject:passwordString forKey:@"password_preference"];
@ -404,12 +413,10 @@
return FALSE;
}
- (BOOL)loadHistory:(BuschJaegerConfigurationRequestType)type delegate:(id<BuschJaegerConfigurationDelegate>)delegate {
- (BOOL)loadHistory:(id<BuschJaegerConfigurationDelegate>)delegate {
[history removeAllObjects];
NSString *domain = (type == BuschJaegerConfigurationRequestType_Local)? network.localHistory: network.globalHistory;
domain = [self addUserNameAndPasswordToUrl:domain];
NSString* url = [NSString stringWithFormat:@"%@", domain];
NSString *url = ([self getCurrentRequestType] == BuschJaegerConfigurationRequestType_Local)? network.localHistory: network.globalHistory;
url = [self addUserNameAndPasswordToUrl:url];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:5];
if(request != nil) {
//[NSURLConnection connectionWithRequest:request delegate:self];
@ -531,8 +538,8 @@
}
- (BOOL)removeHistory:(BuschJaegerConfigurationRequestType)type history:(History*)ahistory delegate:(id<BuschJaegerConfigurationDelegate>)delegate {
NSString *url = [NSString stringWithFormat:@"%@/adduser.cgi?type=delhistory&id=%d", [self getGateway:type], ahistory.ID];
- (BOOL)removeHistory:(History*)ahistory delegate:(id<BuschJaegerConfigurationDelegate>)delegate {
NSString *url = [NSString stringWithFormat:@"%@/adduser.cgi?type=delhistory&id=%d", [self getGateway], ahistory.ID];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:5];
if(request != nil) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, (unsigned long)NULL), ^(void) {
@ -585,9 +592,16 @@
return [NSString stringWithFormat:@"%@%@:%@@%@", proto, username, password, domain];
}
- (NSString*)getGateway:(BuschJaegerConfigurationRequestType)type {
- (BuschJaegerConfigurationRequestType)getCurrentRequestType {
if([[LinphoneManager getWifiData] isEqualToData:homeAP]) {
return BuschJaegerConfigurationRequestType_Local;
}
return BuschJaegerConfigurationRequestType_Global;
}
- (NSString*)getGateway {
NSString *gateway = nil;
NSString *urlString = (type == BuschJaegerConfigurationRequestType_Local)? network.localHistory: network.globalHistory;
NSString *urlString = ([self getCurrentRequestType] == BuschJaegerConfigurationRequestType_Local)? network.localHistory: network.globalHistory;
NSURL *url = [NSURL URLWithString:urlString];
NSRange range = [urlString rangeOfString:[url relativePath]];
@ -600,8 +614,9 @@
}
- (NSString*)getImageUrl:(BuschJaegerConfigurationRequestType)type image:(NSString *)image {
return [NSString stringWithFormat:@"%@/%@", [self getGateway:type], image];
- (NSString*)getImageUrl:(NSString *)image {
NSString *url = [self getGateway];
return [NSString stringWithFormat:@"%@/%@", url, image];
}
- (User*)getCurrentUser {

View file

@ -160,7 +160,7 @@
}
- (IBAction)onDeleteClick:(id)sender {
[[LinphoneManager instance].configuration removeHistory:BuschJaegerConfigurationRequestType_Local history:history delegate:self];
[[LinphoneManager instance].configuration removeHistory:history delegate:self];
}
- (IBAction)doDetailsSwipe:(UISwipeGestureRecognizer *)sender {
@ -168,12 +168,12 @@
if([history.images count]) {
currentIndex = (currentIndex - 1);
if(currentIndex < 0) currentIndex = [history.images count] - 1;
[imageView loadImage:[[LinphoneManager instance].configuration getImageUrl:BuschJaegerConfigurationRequestType_Local image:[history.images objectAtIndex:currentIndex]]];
[imageView loadImage:[[LinphoneManager instance].configuration getImageUrl:[history.images objectAtIndex:currentIndex]]];
}
} else if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
if([history.images count]) {
currentIndex = (currentIndex + 1) % [history.images count];
[imageView loadImage:[[LinphoneManager instance].configuration getImageUrl:BuschJaegerConfigurationRequestType_Local image:[history.images objectAtIndex:currentIndex]]];
[imageView loadImage:[[LinphoneManager instance].configuration getImageUrl:[history.images objectAtIndex:currentIndex]]];
}
}
}
@ -222,7 +222,7 @@
currentIndex = [indexPath row];
[fullscreenView setHidden:FALSE];
[imageView setImage:nil];
[imageView loadImage:[[LinphoneManager instance].configuration getImageUrl:BuschJaegerConfigurationRequestType_Local image:[history.images objectAtIndex:currentIndex]]];
[imageView loadImage:[[LinphoneManager instance].configuration getImageUrl:[history.images objectAtIndex:currentIndex]]];
}
#pragma mark - BuschJaegerConfigurationDelegate Functions

View file

@ -96,7 +96,7 @@
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if(editingStyle == UITableViewCellEditingStyleDelete) {
History *ahistory = [history objectAtIndex:[indexPath row]];
[[LinphoneManager instance].configuration removeHistory:BuschJaegerConfigurationRequestType_Local history:ahistory delegate:self];
[[LinphoneManager instance].configuration removeHistory:ahistory delegate:self];
[tableView beginUpdates];
[history removeObjectAtIndex:[indexPath row]];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

View file

@ -70,7 +70,7 @@
- (void)reload {
[self view]; // Force view load
if([[LinphoneManager instance].configuration loadHistory:BuschJaegerConfigurationRequestType_Local delegate:self]) {
if([[LinphoneManager instance].configuration loadHistory:self]) {
[waitView setHidden:FALSE];
} else {
[waitView setHidden:TRUE];

View file

@ -154,6 +154,8 @@ typedef struct _LinphoneManagerSounds {
- (void)lpConfigSetBool:(BOOL)value forKey:(NSString*)key forSection:(NSString*)section;
- (BOOL)lpConfigBoolForKey:(NSString*)key forSection:(NSString*)section;
+ (NSData*)getWifiData;
/* MODIFICATION: Add NSUSerdefault settings */
- (BOOL)reconfigureLinphone;
/**/

View file

@ -26,6 +26,7 @@
#import <AudioToolbox/AudioToolbox.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <CoreTelephony/CTCallCenter.h>
#import <SystemConfiguration/CaptiveNetwork.h>
#import "LinphoneManager.h"
@ -1591,6 +1592,18 @@ static void audioRouteChangeListenerCallback (
/**/
+ (NSData*)getWifiData {
NSData *data = nil;
CFDictionaryRef dict = CNCopyCurrentNetworkInfo((CFStringRef)@"en0");
if(dict) {
[LinphoneLogger log:LinphoneLoggerDebug format:@"AP Wifi: %@", dict];
data = [NSData dataWithData:(NSData*) CFDictionaryGetValue(dict, @"SSIDDATA")];
CFRelease(dict);
}
return data;
}
#pragma mark - LPConfig Functions
- (void)lpConfigSetString:(NSString*)value forKey:(NSString*)key {
@ -1649,6 +1662,7 @@ static void audioRouteChangeListenerCallback (
return [self lpConfigIntForKey:key forSection:section] == 1;
}
#pragma GSM management
- (void)setupGSMInteraction {

View file

@ -90,7 +90,7 @@
if([history.images count] > 0) {
NSString *image = [history.images objectAtIndex:0];
[iconImage setImage:nil];
[iconImage loadImage:[[LinphoneManager instance].configuration getImageUrl:BuschJaegerConfigurationRequestType_Local image:image]];
[iconImage loadImage:[[LinphoneManager instance].configuration getImageUrl:image]];
}
}

View file

@ -64,7 +64,7 @@
- (void)update {
if(image) {
[imageView setImage:nil];
[imageView loadImage:[[LinphoneManager instance].configuration getImageUrl:BuschJaegerConfigurationRequestType_Local image:image]];
[imageView loadImage:[[LinphoneManager instance].configuration getImageUrl:image]];
}
}