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