xref: /AOO41X/main/apple_remote/RemoteControlContainer.m (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir/*****************************************************************************
2*cdf0e10cSrcweir * RemoteControlContainer.m
3*cdf0e10cSrcweir * RemoteControlWrapper
4*cdf0e10cSrcweir *
5*cdf0e10cSrcweir * Created by Martin Kahr on 11.03.06 under a MIT-style license.
6*cdf0e10cSrcweir * Copyright (c) 2006 martinkahr.com. All rights reserved.
7*cdf0e10cSrcweir *
8*cdf0e10cSrcweir * Code modified and adapted to OpenOffice.org
9*cdf0e10cSrcweir * by Eric Bachard on 11.08.2008 under the same License
10*cdf0e10cSrcweir *
11*cdf0e10cSrcweir * Permission is hereby granted, free of charge, to any person obtaining a
12*cdf0e10cSrcweir * copy of this software and associated documentation files (the "Software"),
13*cdf0e10cSrcweir * to deal in the Software without restriction, including without limitation
14*cdf0e10cSrcweir * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15*cdf0e10cSrcweir * and/or sell copies of the Software, and to permit persons to whom the
16*cdf0e10cSrcweir * Software is furnished to do so, subject to the following conditions:
17*cdf0e10cSrcweir *
18*cdf0e10cSrcweir * The above copyright notice and this permission notice shall be included
19*cdf0e10cSrcweir * in all copies or substantial portions of the Software.
20*cdf0e10cSrcweir *
21*cdf0e10cSrcweir * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22*cdf0e10cSrcweir * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23*cdf0e10cSrcweir * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24*cdf0e10cSrcweir * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25*cdf0e10cSrcweir * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26*cdf0e10cSrcweir * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27*cdf0e10cSrcweir * THE SOFTWARE.
28*cdf0e10cSrcweir *
29*cdf0e10cSrcweir *****************************************************************************/
30*cdf0e10cSrcweir
31*cdf0e10cSrcweir#import "RemoteControlContainer.h"
32*cdf0e10cSrcweir
33*cdf0e10cSrcweir@implementation RemoteControlContainer
34*cdf0e10cSrcweir
35*cdf0e10cSrcweir- (id) initWithDelegate: (id) _remoteControlDelegate {
36*cdf0e10cSrcweir	if ( (self = [super initWithDelegate:_remoteControlDelegate]) ) {
37*cdf0e10cSrcweir		remoteControls = [[NSMutableArray alloc] init];
38*cdf0e10cSrcweir#ifdef DEBUG
39*cdf0e10cSrcweir        NSLog(@"RemoteControlContainer initWithDelegate ok");
40*cdf0e10cSrcweir	}
41*cdf0e10cSrcweir    else {
42*cdf0e10cSrcweir        NSLog(@"RemoteControlContainer initWithDelegate failed");
43*cdf0e10cSrcweir#endif
44*cdf0e10cSrcweir    }
45*cdf0e10cSrcweir
46*cdf0e10cSrcweir	return self;
47*cdf0e10cSrcweir}
48*cdf0e10cSrcweir
49*cdf0e10cSrcweir- (void) dealloc {
50*cdf0e10cSrcweir	[self stopListening: self];
51*cdf0e10cSrcweir	[remoteControls release];
52*cdf0e10cSrcweir	[super dealloc];
53*cdf0e10cSrcweir}
54*cdf0e10cSrcweir
55*cdf0e10cSrcweir- (BOOL) instantiateAndAddRemoteControlDeviceWithClass: (Class) clazz {
56*cdf0e10cSrcweir    BOOL toReturn = NO;
57*cdf0e10cSrcweir    RemoteControl* remoteControl = [[clazz alloc] initWithDelegate: delegate];
58*cdf0e10cSrcweir    if (remoteControl) {
59*cdf0e10cSrcweir        [remoteControls addObject: remoteControl];
60*cdf0e10cSrcweir        [remoteControl addObserver: self forKeyPath:@"listeningToRemote" options:NSKeyValueObservingOptionNew context:nil];
61*cdf0e10cSrcweir        toReturn = YES;
62*cdf0e10cSrcweir    }
63*cdf0e10cSrcweir#ifdef DEBUG
64*cdf0e10cSrcweir    else {
65*cdf0e10cSrcweir        NSLog(@"RemoteControlContainer instantiateAndAddRemoteControlDeviceWithClass failed");
66*cdf0e10cSrcweir        toReturn = NO;
67*cdf0e10cSrcweir    }
68*cdf0e10cSrcweir#endif
69*cdf0e10cSrcweir    return toReturn;
70*cdf0e10cSrcweir}
71*cdf0e10cSrcweir
72*cdf0e10cSrcweir- (unsigned int) count {
73*cdf0e10cSrcweir	return [remoteControls count];
74*cdf0e10cSrcweir}
75*cdf0e10cSrcweir
76*cdf0e10cSrcweir- (void) reset {
77*cdf0e10cSrcweir	[self willChangeValueForKey:@"listeningToRemote"];
78*cdf0e10cSrcweir	[self didChangeValueForKey:@"listeningToRemote"];
79*cdf0e10cSrcweir#ifdef DEBUG
80*cdf0e10cSrcweir	// debug purpose
81*cdf0e10cSrcweir    NSLog(@"reset... (after listening to remote)");
82*cdf0e10cSrcweir#endif
83*cdf0e10cSrcweir}
84*cdf0e10cSrcweir
85*cdf0e10cSrcweir- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
86*cdf0e10cSrcweir	[self reset];
87*cdf0e10cSrcweir}
88*cdf0e10cSrcweir
89*cdf0e10cSrcweir- (void) setListeningToRemote: (BOOL) value {
90*cdf0e10cSrcweir	int i;
91*cdf0e10cSrcweir	for(i=0; i < [remoteControls count]; i++) {
92*cdf0e10cSrcweir		[[remoteControls objectAtIndex: i] setListeningToRemote: value];
93*cdf0e10cSrcweir	}
94*cdf0e10cSrcweir	if (value && value != [self isListeningToRemote]) [self performSelector:@selector(reset) withObject:nil afterDelay:0.01];
95*cdf0e10cSrcweir}
96*cdf0e10cSrcweir- (BOOL) isListeningToRemote {
97*cdf0e10cSrcweir	int i;
98*cdf0e10cSrcweir	for(i=0; i < [remoteControls count]; i++) {
99*cdf0e10cSrcweir		if ([[remoteControls objectAtIndex: i] isListeningToRemote]) {
100*cdf0e10cSrcweir			return YES;
101*cdf0e10cSrcweir		}
102*cdf0e10cSrcweir	}
103*cdf0e10cSrcweir	return NO;
104*cdf0e10cSrcweir}
105*cdf0e10cSrcweir
106*cdf0e10cSrcweir- (void) startListening: (id) sender {
107*cdf0e10cSrcweir#ifdef DEBUG
108*cdf0e10cSrcweir	NSLog(@"startListening to events... ");
109*cdf0e10cSrcweir#endif
110*cdf0e10cSrcweir	int i;
111*cdf0e10cSrcweir	for(i=0; i < [remoteControls count]; i++) {
112*cdf0e10cSrcweir		[[remoteControls objectAtIndex: i] startListening: sender];
113*cdf0e10cSrcweir	}
114*cdf0e10cSrcweir}
115*cdf0e10cSrcweir- (void) stopListening: (id) sender {
116*cdf0e10cSrcweir#ifdef DEBUG
117*cdf0e10cSrcweir	NSLog(@"stopListening to events... ");
118*cdf0e10cSrcweir#endif
119*cdf0e10cSrcweir	int i;
120*cdf0e10cSrcweir	for(i=0; i < [remoteControls count]; i++) {
121*cdf0e10cSrcweir		[[remoteControls objectAtIndex: i] stopListening: sender];
122*cdf0e10cSrcweir	}
123*cdf0e10cSrcweir}
124*cdf0e10cSrcweir
125*cdf0e10cSrcweir- (BOOL) isOpenInExclusiveMode {
126*cdf0e10cSrcweir	BOOL mode = YES;
127*cdf0e10cSrcweir	int i;
128*cdf0e10cSrcweir	for(i=0; i < [remoteControls count]; i++) {
129*cdf0e10cSrcweir		mode = mode && ([[remoteControls objectAtIndex: i] isOpenInExclusiveMode]);
130*cdf0e10cSrcweir	}
131*cdf0e10cSrcweir	return mode;
132*cdf0e10cSrcweir}
133*cdf0e10cSrcweir- (void) setOpenInExclusiveMode: (BOOL) value {
134*cdf0e10cSrcweir	int i;
135*cdf0e10cSrcweir	for(i=0; i < [remoteControls count]; i++) {
136*cdf0e10cSrcweir		[[remoteControls objectAtIndex: i] setOpenInExclusiveMode:value];
137*cdf0e10cSrcweir	}
138*cdf0e10cSrcweir}
139*cdf0e10cSrcweir
140*cdf0e10cSrcweir@end
141