f89c3294 |
1 | /* $Id$ */ |
6cb61a05 |
2 | /* |
3 | * Copyright (c) 2002 Ben Harris |
4 | * All rights reserved. |
5 | * |
6 | * Permission is hereby granted, free of charge, to any person |
7 | * obtaining a copy of this software and associated documentation |
8 | * files (the "Software"), to deal in the Software without |
9 | * restriction, including without limitation the rights to use, |
10 | * copy, modify, merge, publish, distribute, sublicense, and/or |
11 | * sell copies of the Software, and to permit persons to whom the |
12 | * Software is furnished to do so, subject to the following |
13 | * conditions: |
14 | * |
15 | * The above copyright notice and this permission notice shall be |
16 | * included in all copies or substantial portions of the Software. |
17 | * |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR |
22 | * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
23 | * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25 | * SOFTWARE. |
26 | */ |
27 | |
28 | /* |
29 | * macdlg.c - settings dialogue box for Mac OS. |
30 | */ |
31 | |
32 | #include <MacTypes.h> |
d70e3b83 |
33 | #include <AEDataModel.h> |
34 | #include <AppleEvents.h> |
9ef3c1ca |
35 | #include <Navigation.h> |
b537dd42 |
36 | #include <Resources.h> |
2b8dca9e |
37 | #include <StandardFile.h> |
6cb61a05 |
38 | #include <Windows.h> |
39 | |
98f59a35 |
40 | #include <assert.h> |
6cb61a05 |
41 | #include <string.h> |
42 | |
43 | #include "putty.h" |
8a7e67ec |
44 | #include "dialog.h" |
6cb61a05 |
45 | #include "mac.h" |
46 | #include "macresid.h" |
2b8dca9e |
47 | #include "storage.h" |
6cb61a05 |
48 | |
8a7e67ec |
49 | static void mac_closedlg(WindowPtr); |
f73f2f31 |
50 | static void mac_enddlg(WindowPtr, int); |
f854b482 |
51 | |
6cb61a05 |
52 | void mac_newsession(void) |
53 | { |
54 | Session *s; |
f854b482 |
55 | WinInfo *wi; |
8a7e67ec |
56 | static struct sesslist sesslist; |
6cb61a05 |
57 | |
34773273 |
58 | s = snew(Session); |
6cb61a05 |
59 | memset(s, 0, sizeof(*s)); |
60 | do_defaults(NULL, &s->cfg); |
98f59a35 |
61 | s->hasfile = FALSE; |
6cb61a05 |
62 | |
8a7e67ec |
63 | if (HAVE_COLOR_QD()) |
64 | s->settings_window = GetNewCWindow(wSettings, NULL, (WindowPtr)-1); |
65 | else |
66 | s->settings_window = GetNewWindow(wSettings, NULL, (WindowPtr)-1); |
67 | |
68 | get_sesslist(&sesslist, TRUE); |
69 | s->ctrlbox = ctrl_new_box(); |
f89c3294 |
70 | setup_config_box(s->ctrlbox, &sesslist, FALSE, 0, 0); |
8a7e67ec |
71 | |
72 | s->settings_ctrls.data = &s->cfg; |
f73f2f31 |
73 | s->settings_ctrls.end = &mac_enddlg; |
8a7e67ec |
74 | macctrl_layoutbox(s->ctrlbox, s->settings_window, &s->settings_ctrls); |
6cb61a05 |
75 | |
34773273 |
76 | wi = snew(WinInfo); |
f854b482 |
77 | memset(wi, 0, sizeof(*wi)); |
78 | wi->s = s; |
5cec2fd9 |
79 | wi->mcs = &s->settings_ctrls; |
f854b482 |
80 | wi->wtype = wSettings; |
8a7e67ec |
81 | wi->update = &macctrl_update; |
82 | wi->click = &macctrl_click; |
1f1ec0e5 |
83 | wi->key = &macctrl_key; |
8a7e67ec |
84 | wi->activate = &macctrl_activate; |
85 | wi->adjustmenus = &macctrl_adjustmenus; |
ee10bc56 |
86 | wi->close = &mac_closedlg; |
f854b482 |
87 | SetWRefCon(s->settings_window, (long)wi); |
6cb61a05 |
88 | ShowWindow(s->settings_window); |
89 | } |
90 | |
ee10bc56 |
91 | static void mac_closedlg(WindowPtr window) |
92 | { |
93 | Session *s = mac_windowsession(window); |
94 | |
95 | macctrl_close(window); |
96 | DisposeWindow(window); |
97 | if (s->window == NULL) |
98 | sfree(s); |
99 | } |
100 | |
f73f2f31 |
101 | static void mac_enddlg(WindowPtr window, int value) |
102 | { |
103 | Session *s = mac_windowsession(window); |
104 | |
105 | if (value == 0) |
106 | mac_closedlg(window); |
107 | else { |
108 | mac_startsession(s); |
109 | mac_closedlg(window); |
110 | } |
111 | } |
ee10bc56 |
112 | |
5211281f |
113 | void mac_dupsession(void) |
114 | { |
347bfcd7 |
115 | Session *s1 = mac_windowsession(FrontWindow()); |
5211281f |
116 | Session *s2; |
117 | |
34773273 |
118 | s2 = snew(Session); |
5211281f |
119 | memset(s2, 0, sizeof(*s2)); |
120 | s2->cfg = s1->cfg; |
121 | s2->hasfile = s1->hasfile; |
122 | s2->savefile = s1->savefile; |
123 | |
124 | mac_startsession(s2); |
125 | } |
126 | |
d70e3b83 |
127 | static OSErr mac_opensessionfrom(FSSpec *fss) |
128 | { |
129 | FInfo fi; |
2b8dca9e |
130 | Session *s; |
2b8dca9e |
131 | void *sesshandle; |
d70e3b83 |
132 | OSErr err; |
2b8dca9e |
133 | |
34773273 |
134 | s = snew(Session); |
2b8dca9e |
135 | memset(s, 0, sizeof(*s)); |
136 | |
d70e3b83 |
137 | err = FSpGetFInfo(fss, &fi); |
138 | if (err != noErr) return err; |
139 | if (fi.fdFlags & kIsStationery) |
98f59a35 |
140 | s->hasfile = FALSE; |
141 | else { |
142 | s->hasfile = TRUE; |
d70e3b83 |
143 | s->savefile = *fss; |
98f59a35 |
144 | } |
2b8dca9e |
145 | |
d70e3b83 |
146 | sesshandle = open_settings_r_fsp(fss); |
147 | if (sesshandle == NULL) { |
148 | /* XXX need a way to pass up an error number */ |
149 | err = -9999; |
150 | goto fail; |
151 | } |
152 | load_open_settings(sesshandle, TRUE, &s->cfg); |
153 | close_settings_r(sesshandle); |
154 | |
2b8dca9e |
155 | mac_startsession(s); |
d70e3b83 |
156 | return noErr; |
2b8dca9e |
157 | |
158 | fail: |
159 | sfree(s); |
d70e3b83 |
160 | return err; |
161 | } |
162 | |
9ef3c1ca |
163 | static OSErr mac_openlist(AEDesc docs) |
164 | { |
165 | OSErr err; |
166 | long ndocs, i; |
167 | FSSpec fss; |
168 | AEKeyword keywd; |
169 | DescType type; |
170 | Size size; |
171 | |
172 | err = AECountItems(&docs, &ndocs); |
173 | if (err != noErr) return err; |
174 | |
175 | for (i = 0; i < ndocs; i++) { |
176 | err = AEGetNthPtr(&docs, i + 1, typeFSS, |
177 | &keywd, &type, &fss, sizeof(fss), &size); |
178 | if (err != noErr) return err;; |
179 | err = mac_opensessionfrom(&fss); |
180 | if (err != noErr) return err; |
181 | } |
182 | return noErr; |
183 | } |
184 | |
cef180ab |
185 | void mac_opensession(void) |
186 | { |
9ef3c1ca |
187 | |
188 | if (mac_gestalts.navsvers > 0) { |
189 | NavReplyRecord navr; |
190 | NavDialogOptions navopts; |
191 | NavTypeListHandle navtypes; |
192 | AEDesc defaultloc = { 'null', NULL }; |
193 | AEDesc *navdefault = NULL; |
194 | short vol; |
195 | long dirid; |
196 | FSSpec fss; |
197 | |
198 | if (NavGetDefaultDialogOptions(&navopts) != noErr) return; |
199 | /* XXX should we create sessions dir? */ |
200 | if (get_session_dir(FALSE, &vol, &dirid) == noErr && |
201 | FSMakeFSSpec(vol, dirid, NULL, &fss) == noErr && |
202 | AECreateDesc(typeFSS, &fss, sizeof(fss), &defaultloc) == noErr) |
203 | navdefault = &defaultloc; |
204 | /* Can't meaningfully preview a saved session yet */ |
205 | navopts.dialogOptionFlags &= ~kNavAllowPreviews; |
206 | navtypes = (NavTypeListHandle)GetResource('open', open_pTTY); |
207 | if (NavGetFile(navdefault, &navr, &navopts, NULL, NULL, NULL, navtypes, |
208 | NULL) == noErr && navr.validRecord) |
209 | mac_openlist(navr.selection); |
210 | NavDisposeReply(&navr); |
211 | if (navtypes != NULL) |
212 | ReleaseResource((Handle)navtypes); |
213 | } |
cef180ab |
214 | #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */ |
9ef3c1ca |
215 | else { |
216 | StandardFileReply sfr; |
217 | static const OSType sftypes[] = { 'Sess', 0, 0, 0 }; |
d70e3b83 |
218 | |
9ef3c1ca |
219 | StandardGetFile(NULL, 1, sftypes, &sfr); |
220 | if (!sfr.sfGood) return; |
d70e3b83 |
221 | |
9ef3c1ca |
222 | mac_opensessionfrom(&sfr.sfFile); |
223 | /* XXX handle error */ |
224 | } |
cef180ab |
225 | #endif |
2b8dca9e |
226 | } |
227 | |
b537dd42 |
228 | void mac_savesession(void) |
229 | { |
347bfcd7 |
230 | Session *s = mac_windowsession(FrontWindow()); |
98f59a35 |
231 | void *sesshandle; |
b537dd42 |
232 | |
98f59a35 |
233 | assert(s->hasfile); |
234 | sesshandle = open_settings_w_fsp(&s->savefile); |
235 | if (sesshandle == NULL) return; /* XXX report error */ |
236 | save_open_settings(sesshandle, TRUE, &s->cfg); |
237 | close_settings_w(sesshandle); |
b537dd42 |
238 | } |
239 | |
240 | void mac_savesessionas(void) |
241 | { |
cef180ab |
242 | #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */ |
347bfcd7 |
243 | Session *s = mac_windowsession(FrontWindow()); |
b537dd42 |
244 | StandardFileReply sfr; |
245 | void *sesshandle; |
246 | |
98f59a35 |
247 | StandardPutFile("\pSave session as:", |
248 | s->hasfile ? s->savefile.name : "\puntitled", &sfr); |
b537dd42 |
249 | if (!sfr.sfGood) return; |
250 | |
251 | if (!sfr.sfReplacing) { |
252 | FSpCreateResFile(&sfr.sfFile, PUTTY_CREATOR, SESS_TYPE, sfr.sfScript); |
253 | if (ResError() != noErr) return; /* XXX report error */ |
254 | } |
255 | sesshandle = open_settings_w_fsp(&sfr.sfFile); |
256 | if (sesshandle == NULL) return; /* XXX report error */ |
257 | save_open_settings(sesshandle, TRUE, &s->cfg); |
258 | close_settings_w(sesshandle); |
98f59a35 |
259 | s->hasfile = TRUE; |
260 | s->savefile = sfr.sfFile; |
cef180ab |
261 | #endif |
b537dd42 |
262 | } |
263 | |
d70e3b83 |
264 | pascal OSErr mac_aevt_oapp(const AppleEvent *req, AppleEvent *reply, |
265 | long refcon) |
266 | { |
267 | DescType type; |
268 | Size size; |
269 | |
270 | if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard, |
271 | &type, NULL, 0, &size) == noErr) |
272 | return errAEParamMissed; |
273 | |
274 | /* XXX we should do something here. */ |
275 | return noErr; |
276 | } |
277 | |
278 | pascal OSErr mac_aevt_odoc(const AppleEvent *req, AppleEvent *reply, |
279 | long refcon) |
280 | { |
281 | DescType type; |
d70e3b83 |
282 | Size size; |
283 | AEDescList docs = { typeNull, NULL }; |
284 | OSErr err; |
d70e3b83 |
285 | |
286 | err = AEGetParamDesc(req, keyDirectObject, typeAEList, &docs); |
287 | if (err != noErr) goto out; |
288 | |
289 | if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard, |
290 | &type, NULL, 0, &size) == noErr) { |
291 | err = errAEParamMissed; |
292 | goto out; |
293 | } |
294 | |
9ef3c1ca |
295 | err = mac_openlist(docs); |
d70e3b83 |
296 | |
297 | out: |
298 | AEDisposeDesc(&docs); |
299 | return err; |
300 | } |
301 | |
302 | pascal OSErr mac_aevt_pdoc(const AppleEvent *req, AppleEvent *reply, |
303 | long refcon) |
304 | { |
305 | DescType type; |
306 | Size size; |
307 | |
308 | if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard, |
309 | &type, NULL, 0, &size) == noErr) |
310 | return errAEParamMissed; |
311 | |
312 | /* We can't meaningfully do anything here. */ |
313 | return errAEEventNotHandled; |
314 | } |
315 | |
6cb61a05 |
316 | /* |
317 | * Local Variables: |
318 | * c-file-style: "simon" |
319 | * End: |
320 | */ |