RSS

(root)/iphone/tappity : 46 : tappity/source/TappityAppDelegate.m

To get this branch, use:
bzr branch /browse/iphone/tappity

« back to all changes in this revision

Viewing changes to tappity/source/TappityAppDelegate.m

Dömötör Gulyás
2009-10-27 01:03:19
Revision ID: dognotdog@gmail.com-20091027000319-1vrkixkgf9lkud6g
adds tappity project

Show diffs side-by-side

added added

removed removed

 
1
//
 
2
//  tappityAppDelegate.m
 
3
//  tappity
 
4
//
 
5
//  Created by döme on 25.10.2009.
 
6
//  Copyright __MyCompanyName__ 2009. All rights reserved.
 
7
//
 
8
 
 
9
#import "TappityAppDelegate.h"
 
10
#import "TappityViewController.h"
 
11
#import "BonjourBrowser.h"
 
12
#import "TappityClient.h"
 
13
#import "TappityView.h"
 
14
 
 
15
@implementation TappityAppDelegate
 
16
 
 
17
@synthesize window;
 
18
@synthesize viewController;
 
19
 
 
20
 
 
21
- (void)applicationDidFinishLaunching:(UIApplication *)application
 
22
{    
 
23
    
 
24
        tapClient = [[TappityClient alloc] init];
 
25
 
 
26
        browser = [[BonjourBrowser alloc] initForType: @"_tappity._tcp"
 
27
                                                                                 inDomain: @"local"
 
28
                                                                        customDomains: nil // we won't save any additional domains added by the user
 
29
                                                 showDisclosureIndicators: NO
 
30
                                                                 showCancelButton: YES];
 
31
 
 
32
        browser.delegate = self;
 
33
    
 
34
    // We want to let the user know that the services list is dynamic and always updating, even when there are no
 
35
    // services currently found.
 
36
        browser.searchingForServicesString = NSLocalizedString(@"Searching for web services", @"Searching for web services string");
 
37
 
 
38
        // Add the controller's view as a subview of the window
 
39
        [self.window addSubview: [browser view]];
 
40
 
 
41
//    [window addSubview:viewController.view];
 
42
    [window makeKeyAndVisible];
 
43
}
 
44
 
 
45
 
 
46
 
 
47
- (NSString *)copyStringFromTXTDict:(NSDictionary *)dict which:(NSString*)which {
 
48
        // Helper for getting information from the TXT data
 
49
        NSData* data = [dict objectForKey:which];
 
50
        return data ? [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease] : nil;
 
51
}
 
52
 
 
53
- (void) bonjourBrowser:(BonjourBrowser*) theBrowser didResolveInstance:(NSNetService*)service
 
54
{
 
55
        NSLog(@"found bonjour server");
 
56
        
 
57
        if (service)
 
58
        {
 
59
                [tapClient connectToService: service];
 
60
        }
 
61
 
 
62
        if (!tapView)
 
63
                tapView = [[TappityView alloc] initWithFrame: [theBrowser view].frame];
 
64
                
 
65
        tapView.tapClient = tapClient;
 
66
                
 
67
        [[theBrowser view] removeFromSuperview];
 
68
        
 
69
        [self.window addSubview: tapView];
 
70
        
 
71
        [[UIApplication sharedApplication] _addRecorder: tapClient];
 
72
 
 
73
        
 
74
        return;
 
75
 
 
76
        // Construct the URL including the port number
 
77
        // Also use the path, username and password fields that can be in the TXT record
 
78
        NSDictionary* dict = [[NSNetService dictionaryFromTXTRecordData:[service TXTRecordData]] retain];
 
79
        NSString *host = [service hostName];
 
80
        
 
81
        NSString* user = [[self copyStringFromTXTDict:dict which:@"u"] retain];
 
82
        NSString* pass = [[self copyStringFromTXTDict:dict which:@"p"] retain];
 
83
        
 
84
        NSString* portStr = [[NSString alloc] initWithString:@""];
 
85
        
 
86
        // Note that [NSNetService port:] returns an NSInteger in host byte order
 
87
        NSInteger port = [service port];
 
88
        if (port != 0 && port != 80)
 
89
                        portStr = [[NSString alloc] initWithFormat:@":%d",port];
 
90
        
 
91
        NSString* path = [[[self copyStringFromTXTDict:dict which:@"path"] retain] autorelease];
 
92
        if (!path || [path length]==0) {
 
93
                        path = [[[NSString alloc] initWithString:@"/"] autorelease];
 
94
        } else if (![[path substringToIndex:1] isEqual:@"/"]) {
 
95
                        path = [[[NSString alloc] initWithFormat:@"/%@",path] autorelease];
 
96
        }
 
97
        
 
98
        NSString* string = [[NSString alloc] initWithFormat:@"http://%@%@%@%@%@%@%@",
 
99
                                                                                                user?user:@"",
 
100
                                                                                                pass?@":":@"",
 
101
                                                                                                pass?pass:@"",
 
102
                                                                                                (user||pass)?@"@":@"",
 
103
                                                                                                host,
 
104
                                                                                                portStr,
 
105
                                                                                                path];
 
106
        
 
107
        NSURL *url = [[NSURL alloc] initWithString:string];
 
108
        [[UIApplication sharedApplication] openURL:url];
 
109
        
 
110
        [url release];
 
111
        [string release];
 
112
        [portStr release];
 
113
        [pass release];
 
114
        [user release];
 
115
        [dict release];
 
116
}
 
117
 
 
118
- (void)dealloc
 
119
{
 
120
    [viewController release];
 
121
        [browser release];
 
122
    [window release];
 
123
    [super dealloc];
 
124
}
 
125
 
 
126
 
 
127
@end

Loggerhead 1.17 is a web-based interface for Bazaar branches