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