1/***************************************************************************** 2 * RemoteControl.m 3 * RemoteControlWrapper 4 * 5 * Created by Martin Kahr on 11.03.06 under a MIT-style license. 6 * Copyright (c) 2006 martinkahr.com. All rights reserved. 7 * 8 * Code modified and adapted to OpenOffice.org 9 * by Eric Bachard on 11.08.2008 under the same License 10 * 11 * Permission is hereby granted, free of charge, to any person obtaining a 12 * copy of this software and associated documentation files (the "Software"), 13 * to deal in the Software without restriction, including without limitation 14 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 15 * and/or sell copies of the Software, and to permit persons to whom the 16 * Software is furnished to do so, subject to the following conditions: 17 * 18 * The above copyright notice and this permission notice shall be included 19 * in all copies or substantial portions of the Software. 20 * 21 * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 27 * THE SOFTWARE. 28 * 29 *****************************************************************************/ 30 31#import "RemoteControl.h" 32 33// notifaction names that are being used to signal that an application wants to 34// have access to the remote control device or if the application has finished 35// using the remote control device 36NSString* REQUEST_FOR_REMOTE_CONTROL_NOTIFCATION = @"mac.remotecontrols.RequestForRemoteControl"; 37NSString* FINISHED_USING_REMOTE_CONTROL_NOTIFICATION = @"mac.remotecontrols.FinishedUsingRemoteControl"; 38 39// keys used in user objects for distributed notifications 40NSString* kRemoteControlDeviceName = @"RemoteControlDeviceName"; 41NSString* kApplicationIdentifier = @"CFBundleIdentifier"; 42// bundle identifier of the application that should get access to the remote control 43// this key is being used in the FINISHED notification only 44NSString* kTargetApplicationIdentifier = @"TargetBundleIdentifier"; 45 46 47@implementation RemoteControl 48 49// returns nil if the remote control device is not available 50- (id) initWithDelegate: (id) _remoteControlDelegate { 51 if ( (self = [super init]) ) { 52 delegate = [_remoteControlDelegate retain]; 53#ifdef DEBUG 54 NSLog(@"RemoteControl initWithDelegate ok"); 55#endif 56 } 57 return self; 58} 59 60- (void) dealloc { 61 [delegate release]; 62 [super dealloc]; 63} 64 65- (void) setListeningToRemote: (BOOL) value { 66#ifdef DEBUG 67 NSLog(@"setListeningToRemote ok"); 68#endif 69} 70- (BOOL) isListeningToRemote { 71 return NO; 72} 73 74- (void) startListening: (id) sender { 75#ifdef DEBUG 76 NSLog(@"startListening ok"); 77#endif 78} 79- (void) stopListening: (id) sender { 80#ifdef DEBUG 81 NSLog(@"stopListening ok"); 82#endif 83} 84 85- (BOOL) isOpenInExclusiveMode { 86 return YES; 87} 88- (void) setOpenInExclusiveMode: (BOOL) value { 89} 90 91- (BOOL) sendsEventForButtonIdentifier: (RemoteControlEventIdentifier) identifier { 92#ifdef DEBUG 93 NSLog(@"sending event for button identifier \n"); 94#endif 95 return YES; 96} 97 98+ (void) sendDistributedNotification: (NSString*) notificationName targetBundleIdentifier: (NSString*) targetIdentifier 99{ 100 NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys: [NSString stringWithCString:[self remoteControlDeviceName] encoding:NSASCIIStringEncoding], 101 kRemoteControlDeviceName /* key = RemoteControlDeviceName -> OK */, 102 [[NSBundle mainBundle] bundleIdentifier] /* value = org.openoffice.script -> OK */, 103 kApplicationIdentifier/* key = CFBundleIdentifier -> OK */, 104 targetIdentifier /*value = AppleIRController -> OK */, 105 kTargetApplicationIdentifier /*targetBundleIdentifier -> does not appear, since the peer is nil*/, 106 nil]; 107#ifdef DEBUG 108 // Debug purpose: returns all the existing dictionary keys. 109 NSString *s; 110 NSEnumerator *e = [userInfo keyEnumerator]; 111 while ( (s = [e nextObject]) ) { 112 NSLog(@"key = %@ ",s); 113 } 114 NSEnumerator *f = [userInfo objectEnumerator ]; 115 while ( (s = [f nextObject]) ) { 116 NSLog(@"value = %@ ",s); 117 } 118 NSLog(@"sendDistributedNotification ..."); 119#endif 120 121 [[NSDistributedNotificationCenter defaultCenter] postNotificationName:notificationName 122 object:nil 123 userInfo:userInfo 124 deliverImmediately:YES]; 125} 126 127+ (void) sendFinishedNotifcationForAppIdentifier: (NSString*) identifier { 128 [self sendDistributedNotification:FINISHED_USING_REMOTE_CONTROL_NOTIFICATION targetBundleIdentifier:identifier]; 129#ifdef DEBUG 130 NSLog(@"sendFinishedNotifcationForAppIdentifier ..."); 131#endif 132} 133+ (void) sendRequestForRemoteControlNotification { 134 [self sendDistributedNotification:REQUEST_FOR_REMOTE_CONTROL_NOTIFCATION targetBundleIdentifier:nil]; 135#ifdef DEBUG 136 NSLog(@"sendRequestForRemoteControlNotification ..."); 137#endif 138} 139 140+ (const char*) remoteControlDeviceName { 141 return NULL; 142} 143 144@end 145