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( @"Apple 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( @"Apple RemoteControl setListeningToRemote ok");
68#endif
69}
70- (BOOL) isListeningToRemote {
71	return NO;
72}
73
74- (void) startListening: (id) sender {
75#ifdef DEBUG
76            NSLog( @"Apple RemoteControl startListening ok");
77#endif
78}
79- (void) stopListening: (id) sender {
80#ifdef DEBUG
81            NSLog( @"Apple RemoteControl 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( @"Apple RemoteControl: 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    NSLog( @"Apple Remote: sendDistributedNotification ...");
109    // Debug purpose: returns all the existing dictionary keys.
110    NSEnumerator* itKey = [userInfo keyEnumerator];
111    NSEnumerator* itVal = [userInfo objectEnumerator];
112    for(;;) {
113        NSString* sKey = [itKey nextObject];
114        NSString* sVal = [itVal nextObject];
115        if( !sKey && !sVal)
116            break;
117        if( !sKey) sKey = @"nil";
118	if( !sVal) sVal = @"nil";
119        NSLog( @"\tARdict[\"%@\"] = \"%@\"",sKey,sVal);
120    }
121#endif
122
123	[[NSDistributedNotificationCenter defaultCenter] postNotificationName:notificationName
124																   object:nil
125																 userInfo:userInfo
126													   deliverImmediately:YES];
127}
128
129+ (void) sendFinishedNotifcationForAppIdentifier: (NSString*) identifier {
130    [self sendDistributedNotification:FINISHED_USING_REMOTE_CONTROL_NOTIFICATION targetBundleIdentifier:identifier];
131#ifdef DEBUG
132    NSLog( @"Apple RemoteControl: sendFinishedNotifcationForAppIdentifier ...");
133#endif
134}
135+ (void) sendRequestForRemoteControlNotification {
136    [self sendDistributedNotification:REQUEST_FOR_REMOTE_CONTROL_NOTIFCATION targetBundleIdentifier:nil];
137#ifdef DEBUG
138    NSLog( @"Apple RemoteControl: sendRequestForRemoteControlNotification ...");
139#endif
140}
141
142+ (const char*) remoteControlDeviceName {
143	return NULL;
144}
145
146@end
147