1 /*****************************************************************************
5 * Created by Martin Kahr on 11.03.06 under a MIT-style license.
6 * Copyright (c) 2006 martinkahr.com. All rights reserved.
8 * Code modified and adapted to OpenOffice.org
9 * by Eric Bachard on 11.08.2008 under the same License
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:
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
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
29 *****************************************************************************/
31 #import "RemoteControl.h"
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
36 NSString* REQUEST_FOR_REMOTE_CONTROL_NOTIFCATION = @"mac.remotecontrols.RequestForRemoteControl";
37 NSString* FINISHED_USING_REMOTE_CONTROL_NOTIFICATION = @"mac.remotecontrols.FinishedUsingRemoteControl";
39 // keys used in user objects for distributed notifications
40 NSString* kRemoteControlDeviceName = @"RemoteControlDeviceName";
41 NSString* 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
44 NSString* kTargetApplicationIdentifier = @"TargetBundleIdentifier";
47 @implementation RemoteControl
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];
54 NSLog(@"RemoteControl initWithDelegate ok");
65 - (void) setListeningToRemote: (BOOL) value {
67 NSLog(@"setListeningToRemote ok");
70 - (BOOL) isListeningToRemote {
74 - (void) startListening: (id) sender {
76 NSLog(@"startListening ok");
79 - (void) stopListening: (id) sender {
81 NSLog(@"stopListening ok");
85 - (BOOL) isOpenInExclusiveMode {
88 - (void) setOpenInExclusiveMode: (BOOL) value {
91 - (BOOL) sendsEventForButtonIdentifier: (RemoteControlEventIdentifier) identifier {
93 NSLog(@"sending event for button identifier \n");
98 + (void) sendDistributedNotification: (NSString*) notificationName targetBundleIdentifier: (NSString*) targetIdentifier
100 if ( (self = [super init]) ) {
101 NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys: [NSString stringWithCString:[self remoteControlDeviceName] encoding:NSASCIIStringEncoding],
102 kRemoteControlDeviceName /* key = RemoteControlDeviceName -> OK */,
103 [[NSBundle mainBundle] bundleIdentifier] /* value = org.openoffice.script -> OK */,
104 kApplicationIdentifier/* key = CFBundleIdentifier -> OK */,
105 targetIdentifier /*value = AppleIRController -> OK */,
106 kTargetApplicationIdentifier /*targetBundleIdentifier -> does not appear, since the peer is nil*/,
109 // Debug purpose: returns all the existing dictionary keys.
111 NSEnumerator *e = [userInfo keyEnumerator];
112 while ( (s = [e nextObject]) ) {
113 NSLog(@"key = %@ ",s);
115 NSEnumerator *f = [userInfo objectEnumerator ];
116 while ( (s = [f nextObject]) ) {
117 NSLog(@"value = %@ ",s);
119 NSLog(@"sendDistributedNotification ...");
122 [[NSDistributedNotificationCenter defaultCenter] postNotificationName:notificationName
125 deliverImmediately:YES];
129 + (void) sendFinishedNotifcationForAppIdentifier: (NSString*) identifier {
130 [self sendDistributedNotification:FINISHED_USING_REMOTE_CONTROL_NOTIFICATION targetBundleIdentifier:identifier];
132 NSLog(@"sendFinishedNotifcationForAppIdentifier ...");
135 + (void) sendRequestForRemoteControlNotification {
136 [self sendDistributedNotification:REQUEST_FOR_REMOTE_CONTROL_NOTIFCATION targetBundleIdentifier:nil];
138 NSLog(@"sendRequestForRemoteControlNotification ...");
142 + (const char*) remoteControlDeviceName {