summaryrefslogtreecommitdiff
path: root/ext/installfiles/mac/mac-ui-macgap1-wrapper/src/MacGap/Classes/Commands/Sound.m
blob: 9f4a44dbccd3cda509fdf269803ab27e14141d99 (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
#import "Sound.h"


@interface PlayDelegate : CallbackDelegate <NSSoundDelegate> {
}

@property (nonatomic, weak) Sound *sound;

- (id) initWithContext:(JSContextRef)aContext
           forCallback:(WebScriptObject*)aCallback
             withSound:(Sound*)aSound;
@end

@implementation PlayDelegate

@synthesize sound;

- (id) initWithContext:(JSContextRef)aContext
           forCallback:(WebScriptObject*)aCallback
             withSound:(Sound*)aSound
{
        self = [super initWithContext:aContext forCallback:aCallback];
        if (!self)
                return nil;
        sound = aSound;
        return self;
}

- (void)sound:(NSSound *)aSound didFinishPlaying:(BOOL)finishedPlaying {
        [self callWithParams:[aSound name], nil];
        [sound.pending removeObject:self];
}

@end

@implementation Sound

@synthesize pending;

- (id) initWithContext:(JSContextRef)aContext {
    self = [super initWithContext:aContext];
    if (!self) {
        return nil;
    }
    
    pending = [NSMutableSet new];
    return self;
}

- (void) playSound:(NSSound*)sound onComplete:(WebScriptObject*)callback {
    if (callback != (id)[WebUndefined undefined]) {
        PlayDelegate *d = [[PlayDelegate alloc] initWithContext:context
                                                    forCallback:callback
                                                      withSound:self];
        [pending addObject:d];
        [sound setDelegate:d];
    }
    [sound play];
}

- (void) play:(NSString*)file onComplete:(WebScriptObject*)callback {
	NSURL* fileUrl  = [NSURL fileURLWithPath:[[Utils sharedInstance] pathForResource:file]];
	DebugNSLog(@"Sound file:%@", [fileUrl description]);
	
	NSSound* sound = [[NSSound alloc] initWithContentsOfURL:fileUrl byReference:YES];
    [self playSound:sound onComplete:callback];
}

- (void) playSystem:(NSString*)name onComplete:(WebScriptObject*)callback {
    NSSound *systemSound = [NSSound soundNamed:name];
    [self playSound:systemSound onComplete:callback];
}

#pragma mark WebScripting Protocol

+ (BOOL) isSelectorExcludedFromWebScript:(SEL)selector {
    return [self webScriptNameForSelector:selector] == nil;
}

+ (BOOL) isKeyExcludedFromWebScript:(const char*)name {
	return YES;
}

+ (NSString*) webScriptNameForSelector:(SEL)selector {
    id	result = nil;

    if (selector == @selector(play:onComplete:)) {
            result = @"play";
    }
    else if (selector == @selector(playSystem:onComplete:)) {
            result = @"playSystem";
    }

    return result;
}

@end