-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXSSkype.m
152 lines (128 loc) · 4.23 KB
/
XSSkype.m
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// XSSkype.m
// SkypeToAddressBook
//
// Created by Xavi Aracil on 07/11/09.
// Copyright 2009 xaracSoft (Xavi Aracil Diaz). All rights reserved.
//
#import "XSSkype.h"
@interface XSSkype ()
- (void) applicationLaunched:(NSNotification *) notification;
- (void) internalConnect;
@end
@implementation XSSkype
@synthesize appName, delegate, connected;
- (void) applicationLaunched:(NSNotification *) notification
{
NSDictionary *userInfo = [notification userInfo];
NSString *applicationLaunched = [userInfo valueForKey:@"NSApplicationName"];
if ([@"Skype" isEqualToString:applicationLaunched]) {
[self internalConnect];
}
}
- (void) internalConnect
{
// setting delegate
[SkypeAPI setSkypeDelegate:self];
// connecting
[SkypeAPI connect];
}
- (void) connect
{
// load Skype
if ([SkypeAPI isSkypeRunning] == NO) {
NSNotificationCenter *notCenter = [[NSWorkspace sharedWorkspace] notificationCenter];
[notCenter addObserver:self
selector:@selector(applicationLaunched:)
name:NSWorkspaceDidLaunchApplicationNotification
object:nil]; // Register for all notifications
if (![[NSWorkspace sharedWorkspace] launchApplication:@"Skype"]){
if ([delegate respondsToSelector:@selector(skypeIsNotInstalled)]) {
[delegate performSelector:@selector(skypeIsNotInstalled)];
}
return;
};
} else {
[self internalConnect];
}
}
- (id) initWithAppName:(NSString *) name
{
self = [super init];
if (self != nil) {
connected = NO;
appName = name;
calls = [[NSMutableDictionary alloc] init];
lastCallId = 0;
}
return self;
}
- (void) dealloc
{
[calls release];
[delegate release];
[appName release];
[super dealloc];
}
-(void) sendCommand:(NSString *) command responder:(id<XSSkypeResponder>) responder {
if (connected == NO) return;
NSString *callId = [NSString stringWithFormat:@"#%d", lastCallId++];
NSString *skypeCommand = [NSString stringWithFormat:@"%@ %@", callId, command];
[calls setObject:responder forKey:callId];
[SkypeAPI sendSkypeCommand: skypeCommand];
}
# pragma mark Skype delegate methods
- (NSString*)clientApplicationName {
return appName;
}
// This is the main delegate method Skype uses to send information to your application.
// aNotificationString is a Skype API string as described in Skype API protocol documentation.
- (void)skypeNotificationReceived:(NSString*)aNotificationString {
NSLog(@"Message from skype: %@", aNotificationString);
// get identifier, if so
if ([aNotificationString characterAtIndex:0] == '#') {
NSScanner *theScanner = [NSScanner scannerWithString:aNotificationString];
NSCharacterSet *characterSet = [NSCharacterSet whitespaceCharacterSet];
NSString *callId = nil;
NSString *response = nil;
if ([theScanner isAtEnd] == NO) {
[theScanner scanUpToCharactersFromSet:characterSet intoString:&callId];
response = [[theScanner string] substringFromIndex:[theScanner scanLocation]];
}
id responder = [calls objectForKey:callId];
if ([responder conformsToProtocol:@protocol(XSSkypeResponder)]) {
[responder response:response];
}
/*
[SkypeAPI removeSkypeDelegate];
[SkypeAPI disconnect];
connected = NO;
*/
}
}
// This method is called after Skype API client application has called connect.
// aAttachResponseCode is 0 on failure and 1 on success.
- (void)skypeAttachResponse:(unsigned)aAttachResponseCode {
if (aAttachResponseCode) {
connected = YES;
if ([delegate respondsToSelector:@selector(skypeDidConnect)]) {
[delegate performSelector:@selector(skypeDidConnect)];
}
} else {
if ([delegate respondsToSelector:@selector(skypeDidFailConnect)]) {
[delegate performSelector:@selector(skypeDidFailConnect)];
}
}
}
// This method is called after Skype has been launched.
- (void)skypeBecameAvailable:(NSNotification*)aNotification {
if (connected == NO) {
[self connect];
}
}
// This method is called after Skype has quit.
- (void)skypeBecameUnavailable:(NSNotification*)aNotification {
NSLog(@"skypeBecameUnavailable");
connected = NO;
}
@end