1 |
/* GWLib.m |
2 |
* |
3 |
* Copyright (C) 2003 Free Software Foundation, Inc. |
4 |
* |
5 |
* Author: Enrico Sersale <enrico@imago.ro> |
6 |
* Date: August 2001 |
7 |
* |
8 |
* This file is part of the GNUstep GWorkspace application |
9 |
* |
10 |
* This program is free software; you can redistribute it and/or modify |
11 |
* it under the terms of the GNU General Public License as published by |
12 |
* the Free Software Foundation; either version 2 of the License, or |
13 |
* (at your option) any later version. |
14 |
* |
15 |
* This program is distributed in the hope that it will be useful, |
16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
* GNU General Public License for more details. |
19 |
* |
20 |
* You should have received a copy of the GNU General Public License |
21 |
* along with this program; if not, write to the Free Software |
22 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23 |
*/ |
24 |
|
25 |
|
26 |
#include <Foundation/Foundation.h> |
27 |
#include <AppKit/AppKit.h> |
28 |
#include "GWLib.h" |
29 |
#include "GWProtocol.h" |
30 |
#include "GNUstep.h" |
31 |
#ifndef GNUSTEP |
32 |
#include "OSXCompatibility.h" |
33 |
#endif |
34 |
|
35 |
static id gwapp = nil; |
36 |
static NSString *gwName = @"GWorkspace"; |
37 |
|
38 |
#define CHECKGW \ |
39 |
if (gwapp == nil) \ |
40 |
gwapp = (id <GWProtocol>)[[GWLib class] gworkspaceApplication]; \ |
41 |
if (gwapp == nil) return |
42 |
|
43 |
#define CHECKGW_RET(x) \ |
44 |
if (gwapp == nil) \ |
45 |
gwapp = (id <GWProtocol>)[[GWLib class] gworkspaceApplication]; \ |
46 |
if (gwapp == nil) return x |
47 |
|
48 |
@implementation GWLib |
49 |
|
50 |
+ (id)gworkspaceApplication |
51 |
{ |
52 |
if (gwapp == nil) { |
53 |
NSString *host; |
54 |
NSString *port; |
55 |
NSDate *when = nil; |
56 |
BOOL done = NO; |
57 |
|
58 |
while (done == NO) { |
59 |
host = [[NSUserDefaults standardUserDefaults] stringForKey: @"NSHost"]; |
60 |
|
61 |
if (host == nil) { |
62 |
host = @""; |
63 |
} else { |
64 |
NSHost *h = [NSHost hostWithName: host]; |
65 |
|
66 |
if ([h isEqual: [NSHost currentHost]]) { |
67 |
host = @""; |
68 |
} |
69 |
} |
70 |
|
71 |
port = gwName; |
72 |
|
73 |
NS_DURING |
74 |
{ |
75 |
gwapp = (id <GWProtocol>)[NSConnection rootProxyForConnectionWithRegisteredName: port host: host]; |
76 |
RETAIN (gwapp); |
77 |
} |
78 |
NS_HANDLER |
79 |
{ |
80 |
gwapp = nil; |
81 |
} |
82 |
NS_ENDHANDLER |
83 |
|
84 |
if (gwapp) { |
85 |
done = YES; |
86 |
} |
87 |
|
88 |
if (gwapp == nil) { |
89 |
[[NSWorkspace sharedWorkspace] launchApplication: gwName]; |
90 |
|
91 |
if (when == nil) { |
92 |
when = [[NSDate alloc] init]; |
93 |
done = NO; |
94 |
} else if ([when timeIntervalSinceNow] > 5.0) { |
95 |
int result; |
96 |
|
97 |
DESTROY (when); |
98 |
result = NSRunAlertPanel(gwName, |
99 |
@"Application seems to have hung", |
100 |
@"Continue", @"Terminate", @"Wait"); |
101 |
|
102 |
if (result == NSAlertDefaultReturn) { |
103 |
done = YES; |
104 |
} else if (result == NSAlertOtherReturn) { |
105 |
done = NO; |
106 |
} else { |
107 |
done = YES; |
108 |
} |
109 |
} |
110 |
|
111 |
if (done == NO) { |
112 |
NSDate *limit = [[NSDate alloc] initWithTimeIntervalSinceNow: 0.5]; |
113 |
[[NSRunLoop currentRunLoop] runUntilDate: limit]; |
114 |
RELEASE(limit); |
115 |
} |
116 |
} |
117 |
} |
118 |
|
119 |
TEST_RELEASE (when); |
120 |
} |
121 |
|
122 |
return gwapp; |
123 |
} |
124 |
|
125 |
+ (BOOL)selectFile:(NSString *)fullPath |
126 |
inFileViewerRootedAtPath:(NSString *)rootFullpath |
127 |
{ |
128 |
CHECKGW_RET(NO); |
129 |
return [gwapp selectFile: fullPath inFileViewerRootedAtPath: rootFullpath]; |
130 |
return NO; |
131 |
} |
132 |
|
133 |
+ (oneway void)rootViewerSelectFiles:(NSArray *)paths |
134 |
{ |
135 |
CHECKGW; |
136 |
[gwapp rootViewerSelectFiles: paths]; |
137 |
} |
138 |
|
139 |
+ (oneway void)openSelectedPaths:(NSArray *)paths |
140 |
{ |
141 |
CHECKGW; |
142 |
[gwapp openSelectedPaths: paths]; |
143 |
} |
144 |
|
145 |
+ (oneway void)addWatcherForPath:(NSString *)path |
146 |
{ |
147 |
CHECKGW; |
148 |
[gwapp addWatcherForPath: path]; |
149 |
} |
150 |
|
151 |
+ (oneway void)removeWatcherForPath:(NSString *)path |
152 |
{ |
153 |
CHECKGW; |
154 |
[gwapp removeWatcherForPath: path]; |
155 |
} |
156 |
|
157 |
+ (BOOL)isPakageAtPath:(NSString *)path |
158 |
{ |
159 |
CHECKGW_RET(NO); |
160 |
return [gwapp isPakageAtPath: path]; |
161 |
return NO; |
162 |
} |
163 |
|
164 |
+ (oneway void)performFileOperationWithDictionary:(NSDictionary *)dict |
165 |
{ |
166 |
CHECKGW; |
167 |
[gwapp performFileOperationWithDictionary: dict]; |
168 |
} |
169 |
|
170 |
+ (oneway void)performServiceWithName:(NSString *)sname |
171 |
pasteboard:(NSPasteboard *)pboard |
172 |
{ |
173 |
NSPerformService(sname, pboard); |
174 |
} |
175 |
|
176 |
+ (NSString *)trashPath |
177 |
{ |
178 |
CHECKGW_RET(nil); |
179 |
return [gwapp trashPath]; |
180 |
return nil; |
181 |
} |
182 |
|
183 |
@end |