5 // Created by Charles Jolley on Tue Jun 15 2004.
6 // Copyright (c) 2004, Nisus Software, Inc.
7 // All rights reserved.
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
19 // Neither the name of the Nisus Software, Inc. nor the names of its
20 // contributors may be used to endorse or promote products derived from this
21 // software without specific prior written permission.
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #import "LinkBackServer.h"
39 NSString* MakeLinkBackServerName(NSString* bundleIdentifier, NSString* name)
41 return [bundleIdentifier stringByAppendingFormat: @":%@",name] ;
44 NSMutableDictionary* LinkBackServers = nil ;
46 @implementation LinkBackServer
50 static BOOL inited = NO ;
56 if (!LinkBackServers) LinkBackServers = [[NSMutableDictionary alloc] init];
59 + (LinkBackServer*)LinkBackServerWithName:(NSString*)aName
61 return [LinkBackServers objectForKey: aName] ;
64 + (BOOL)publishServerWithName:(NSString*)aName delegate:(id<LinkBackServerDelegate>)del
66 LinkBackServer* serv = [[LinkBackServer alloc] initWithName: aName delegate: del] ;
67 BOOL ret = [serv publish] ; // retains if successful
72 BOOL LinkBackServerIsSupported(NSString* name, id supportedServers)
76 NSString* curServer = supportedServers ;
78 // NOTE: supportedServers may be nil, an NSArray, or NSString.
79 if (supportedServers) {
80 if ([supportedServers isKindOfClass: [NSArray class]]) {
81 idx = [supportedServers count] ;
82 while((NO==ret) && (--idx >= 0)) {
83 curServer = [supportedServers objectAtIndex: idx] ;
84 ret = [curServer isEqualToString: name] ;
86 } else ret = [curServer isEqualToString: name] ;
92 NSString* FindLinkBackServer(NSString* bundleIdentifier, NSString* serverName, NSString* dir, int level)
96 NSFileManager* fm = [NSFileManager defaultManager] ;
97 NSArray* contents = [fm directoryContentsAtPath: dir] ;
100 NSLog(@"searching for %@ in folder: %@", serverName, dir) ;
105 NSString* cbundleIdentifier ;
106 id supportedServers ;
108 // resolve any symlinks, expand tildes.
109 dir = [dir stringByStandardizingPath] ;
111 // find all .app bundles in the directory and test them.
112 idx = (contents) ? [contents count] : 0 ;
113 while((nil==ret) && (--idx >= 0)) {
114 cpath = [contents objectAtIndex: idx] ;
116 if ([[cpath pathExtension] isEqualToString: @"app"]) {
117 cpath = [dir stringByAppendingPathComponent: cpath] ;
118 cbundle = [NSBundle bundleWithPath: cpath] ;
119 cbundleIdentifier = [cbundle bundleIdentifier] ;
121 if ([cbundleIdentifier isEqualToString: bundleIdentifier]) {
122 supportedServers = [[cbundle infoDictionary] objectForKey: @"LinkBackServer"] ;
123 ret= (LinkBackServerIsSupported(serverName, supportedServers)) ? cpath : nil ;
128 // if the app was not found, descend into non-app dirs. only descend 4 levels to avoid taking forever.
129 if ((nil==ret) && (level<4)) {
130 idx = (contents) ? [contents count] : 0 ;
131 while((nil==ret) && (--idx >= 0)) {
134 cpath = [contents objectAtIndex: idx] ;
135 [fm fileExistsAtPath: cpath isDirectory: &isdir] ;
136 if (isdir && (![[cpath pathExtension] isEqualToString: @"app"])) {
137 cpath = [dir stringByAppendingPathComponent: cpath] ;
138 ret = FindLinkBackServer(bundleIdentifier, serverName, cpath, level+1) ;
146 void LinkBackRunAppNotFoundPanel(NSString* appName, NSURL* url)
151 NSBundle* b = [NSBundle bundleForClass: [LinkBack class]] ;
157 title = NSLocalizedStringFromTableInBundle(@"_AppNotFoundTitle", @"Localized", b, @"app not found title") ;
158 ok = NSLocalizedStringFromTableInBundle(@"_OK", @"Localized", b, @"ok") ;
160 msg = (url) ? NSLocalizedStringFromTableInBundle(@"_AppNotFoundMessageWithURL", @"Localized", b, @"app not found msg") : NSLocalizedStringFromTableInBundle(@"_AppNotFoundMessageNoURL", @"Localized", b, @"app not found msg") ;
162 urlstr = (url) ? NSLocalizedStringFromTableInBundle(@"_GetApplication", @"Localized", b, @"Get application") : nil ;
164 title = [NSString stringWithFormat: title, appName] ;
166 result = NSRunCriticalAlertPanel(title, msg, ok, urlstr, nil) ;
167 if (NSAlertAlternateReturn == result) {
168 [[NSWorkspace sharedWorkspace] openURL: url] ;
172 + (LinkBackServer*)LinkBackServerWithName:(NSString*)aName inApplication:(NSString*)bundleIdentifier launchIfNeeded:(BOOL)flag fallbackURL:(NSURL*)url appName:(NSString*)appName ;
175 NSString* serverName = MakeLinkBackServerName(bundleIdentifier, aName) ;
177 NSTimeInterval tryMark ;
180 ret = [NSConnection rootProxyForConnectionWithRegisteredName: serverName host: nil] ;
182 // if launchIfNeeded, and the connection was not available, try to launch.
183 if((!ret) && (flag)) {
187 // first, try to find the app with the bundle identifier
188 appPath = [[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier: bundleIdentifier] ;
189 linkBackServers = [[[NSBundle bundleWithPath: appPath] infoDictionary] objectForKey: @"LinkBackServer"] ;
190 appPath = (LinkBackServerIsSupported(aName, linkBackServers)) ? appPath : nil ;
192 // if the found app is not supported, we will need to search for the app ourselves.
193 if (nil==appPath) appPath = FindLinkBackServer(bundleIdentifier, aName, @"/Applications",0);
195 if (nil==appPath) appPath = FindLinkBackServer(bundleIdentifier, aName, @"~/Applications",0);
197 if (nil==appPath) appPath = FindLinkBackServer(bundleIdentifier, aName, @"/Network/Applications",0);
199 // if app path has been found, launch the app.
201 [[NSWorkspace sharedWorkspace] launchApplication: appName] ;
203 LinkBackRunAppNotFoundPanel(appName, url) ;
208 // if needed, try to connect.
209 // retry connection for a while if we did not succeed at first. This gives the app time to launch.
210 if (connect && (nil==ret)) {
211 tryMark = [NSDate timeIntervalSinceReferenceDate] ;
213 ret = [NSConnection rootProxyForConnectionWithRegisteredName: serverName host: nil] ;
214 } while ((!ret) && (([NSDate timeIntervalSinceReferenceDate]-tryMark)<10)) ;
218 // setup protocol and return
219 if (ret) [ret setProtocolForProxy: @protocol(LinkBackServer)] ;
223 - (id)initWithName:(NSString*)aName delegate:(id<LinkBackServerDelegate>)aDel
225 if (self = [super init]) {
226 name = [aName copy] ;
236 if (listener) [self retract] ;
243 NSString* serverName = MakeLinkBackServerName([[NSBundle mainBundle] bundleIdentifier], name) ;
246 // create listener and connect
247 NSPort* port = [NSPort port] ;
248 listener = [NSConnection connectionWithReceivePort: port sendPort:port] ;
249 [listener setRootObject: self] ;
250 ret = [listener registerName: serverName] ;
252 // if successful, retain connection and add self to list of servers.
255 [LinkBackServers setObject: self forKey: name] ;
256 } else listener = nil ; // listener will dealloc on its own.
264 [listener invalidate] ;
269 [LinkBackServers removeObjectForKey: name] ;
272 - (LinkBack*)initiateLinkBackFromClient:(LinkBack*)clientLinkBack
274 LinkBack* ret = [[LinkBack alloc] initServerWithClient: clientLinkBack delegate: delegate] ;
276 // NOTE: we do not release because LinkBack will release itself when it the link closes. (caj)