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    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*/,
107                            nil];
108#ifdef DEBUG
109    // Debug purpose: returns all the existing dictionary keys.
110    NSString *s;
111    NSEnumerator *e = [userInfo keyEnumerator];
112    while ( (s = [e nextObject]) ) {
113        NSLog(@"key = %@ ",s);
114    }
115    NSEnumerator *f = [userInfo objectEnumerator ];
116    while ( (s = [f nextObject]) ) {
117        NSLog(@"value = %@ ",s);
118    }
119    NSLog(@"sendDistributedNotification ...");
120#endif
121
122	[[NSDistributedNotificationCenter defaultCenter] postNotificationName:notificationName
123																   object:nil
124																 userInfo:userInfo
125													   deliverImmediately:YES];
126    }
127}
128
129+ (void) sendFinishedNotifcationForAppIdentifier: (NSString*) identifier {
130    [self sendDistributedNotification:FINISHED_USING_REMOTE_CONTROL_NOTIFICATION targetBundleIdentifier:identifier];
131#ifdef DEBUG
132    NSLog(@"sendFinishedNotifcationForAppIdentifier ...");
133#endif
134}
135+ (void) sendRequestForRemoteControlNotification {
136    [self sendDistributedNotification:REQUEST_FOR_REMOTE_CONTROL_NOTIFCATION targetBundleIdentifier:nil];
137#ifdef DEBUG
138    NSLog(@"sendRequestForRemoteControlNotification ...");
139#endif
140}
141
142+ (const char*) remoteControlDeviceName {
143	return NULL;
144}
145
146@end
147