2ec10625350b178b212daf4a3808c5a8d139a187
[u/mdw/putty] / mac / macdlg.c
1 /* $Id$ */
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>
33 #include <AEDataModel.h>
34 #include <AppleEvents.h>
35 #include <Navigation.h>
36 #include <Resources.h>
37 #include <StandardFile.h>
38 #include <TextUtils.h>
39 #include <Windows.h>
40
41 #include <assert.h>
42 #include <string.h>
43
44 #include "putty.h"
45 #include "dialog.h"
46 #include "mac.h"
47 #include "macresid.h"
48 #include "storage.h"
49
50 static void mac_config(int);
51 static void mac_closedlg(WindowPtr);
52 static void mac_enddlg_config(WindowPtr, int);
53 static void mac_enddlg_reconfig(WindowPtr, int);
54
55 void mac_newsession(void)
56 {
57 mac_config(FALSE);
58 }
59
60 void mac_reconfig(void)
61 {
62 mac_config(TRUE);
63 }
64
65 static void mac_config(int midsession)
66 {
67 Session *s;
68 WinInfo *wi;
69 static struct sesslist sesslist;
70 Str255 mactitle;
71 char *str;
72
73 if (midsession) {
74 s = mac_windowsession(FrontWindow());
75 } else {
76 s = snew(Session);
77 memset(s, 0, sizeof(*s));
78 do_defaults(NULL, &s->cfg);
79 s->hasfile = FALSE;
80 }
81
82 if (HAVE_COLOR_QD())
83 s->settings_window = GetNewCWindow(wSettings, NULL, (WindowPtr)-1);
84 else
85 s->settings_window = GetNewWindow(wSettings, NULL, (WindowPtr)-1);
86
87 get_sesslist(&sesslist, TRUE);
88 s->ctrlbox = ctrl_new_box();
89 setup_config_box(s->ctrlbox, &sesslist, midsession, 0, 0);
90
91 s->settings_ctrls.data = &s->cfg;
92 if (midsession)
93 s->settings_ctrls.end = &mac_enddlg_reconfig;
94 else
95 s->settings_ctrls.end = &mac_enddlg_config;
96
97 macctrl_layoutbox(s->ctrlbox, s->settings_window, &s->settings_ctrls);
98
99 wi = snew(WinInfo);
100 memset(wi, 0, sizeof(*wi));
101 wi->s = s;
102 wi->mcs = &s->settings_ctrls;
103 wi->wtype = wSettings;
104 wi->update = &macctrl_update;
105 wi->click = &macctrl_click;
106 wi->key = &macctrl_key;
107 wi->activate = &macctrl_activate;
108 wi->adjustmenus = &macctrl_adjustmenus;
109 wi->close = &mac_closedlg;
110 SetWRefCon(s->settings_window, (long)wi);
111 if (midsession)
112 str = dupprintf("%s Reconfiguration", appname);
113 else
114 str = dupprintf("%s Configuration", appname);
115 c2pstrcpy(mactitle, str);
116 SetWTitle(s->settings_window, mactitle);
117 ShowWindow(s->settings_window);
118 }
119
120 static void mac_closedlg(WindowPtr window)
121 {
122 Session *s = mac_windowsession(window);
123
124 macctrl_close(window);
125 DisposeWindow(window);
126 if (s->window == NULL)
127 sfree(s);
128 }
129
130 static void mac_enddlg_config(WindowPtr window, int value)
131 {
132 Session *s = mac_windowsession(window);
133
134 if (value == 0)
135 mac_closedlg(window);
136 else {
137 mac_startsession(s);
138 mac_closedlg(window);
139 }
140 }
141
142 static void mac_enddlg_reconfig(WindowPtr window, int value)
143 {
144 Session *s = mac_windowsession(window);
145
146 if (value == 0)
147 mac_closedlg(window);
148 else {
149 mac_closedlg(window);
150 }
151 }
152
153 void mac_dupsession(void)
154 {
155 Session *s1 = mac_windowsession(FrontWindow());
156 Session *s2;
157
158 s2 = snew(Session);
159 memset(s2, 0, sizeof(*s2));
160 s2->cfg = s1->cfg;
161 s2->hasfile = s1->hasfile;
162 s2->savefile = s1->savefile;
163
164 mac_startsession(s2);
165 }
166
167 static OSErr mac_opensessionfrom(FSSpec *fss)
168 {
169 FInfo fi;
170 Session *s;
171 void *sesshandle;
172 OSErr err;
173
174 s = snew(Session);
175 memset(s, 0, sizeof(*s));
176
177 err = FSpGetFInfo(fss, &fi);
178 if (err != noErr) return err;
179 if (fi.fdFlags & kIsStationery)
180 s->hasfile = FALSE;
181 else {
182 s->hasfile = TRUE;
183 s->savefile = *fss;
184 }
185
186 sesshandle = open_settings_r_fsp(fss);
187 if (sesshandle == NULL) {
188 /* XXX need a way to pass up an error number */
189 err = -9999;
190 goto fail;
191 }
192 load_open_settings(sesshandle, TRUE, &s->cfg);
193 close_settings_r(sesshandle);
194
195 mac_startsession(s);
196 return noErr;
197
198 fail:
199 sfree(s);
200 return err;
201 }
202
203 static OSErr mac_openlist(AEDesc docs)
204 {
205 OSErr err;
206 long ndocs, i;
207 FSSpec fss;
208 AEKeyword keywd;
209 DescType type;
210 Size size;
211
212 err = AECountItems(&docs, &ndocs);
213 if (err != noErr) return err;
214
215 for (i = 0; i < ndocs; i++) {
216 err = AEGetNthPtr(&docs, i + 1, typeFSS,
217 &keywd, &type, &fss, sizeof(fss), &size);
218 if (err != noErr) return err;;
219 err = mac_opensessionfrom(&fss);
220 if (err != noErr) return err;
221 }
222 return noErr;
223 }
224
225 void mac_opensession(void)
226 {
227
228 if (mac_gestalts.navsvers > 0) {
229 NavReplyRecord navr;
230 NavDialogOptions navopts;
231 NavTypeListHandle navtypes;
232 AEDesc defaultloc = { 'null', NULL };
233 AEDesc *navdefault = NULL;
234 short vol;
235 long dirid;
236 FSSpec fss;
237
238 if (NavGetDefaultDialogOptions(&navopts) != noErr) return;
239 /* XXX should we create sessions dir? */
240 if (get_session_dir(FALSE, &vol, &dirid) == noErr &&
241 FSMakeFSSpec(vol, dirid, NULL, &fss) == noErr &&
242 AECreateDesc(typeFSS, &fss, sizeof(fss), &defaultloc) == noErr)
243 navdefault = &defaultloc;
244 /* Can't meaningfully preview a saved session yet */
245 navopts.dialogOptionFlags &= ~kNavAllowPreviews;
246 navtypes = (NavTypeListHandle)GetResource('open', open_pTTY);
247 if (NavGetFile(navdefault, &navr, &navopts, NULL, NULL, NULL, navtypes,
248 NULL) == noErr && navr.validRecord)
249 mac_openlist(navr.selection);
250 NavDisposeReply(&navr);
251 if (navtypes != NULL)
252 ReleaseResource((Handle)navtypes);
253 }
254 #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
255 else {
256 StandardFileReply sfr;
257 static const OSType sftypes[] = { 'Sess', 0, 0, 0 };
258
259 StandardGetFile(NULL, 1, sftypes, &sfr);
260 if (!sfr.sfGood) return;
261
262 mac_opensessionfrom(&sfr.sfFile);
263 /* XXX handle error */
264 }
265 #endif
266 }
267
268 void mac_savesession(void)
269 {
270 Session *s = mac_windowsession(FrontWindow());
271 void *sesshandle;
272
273 assert(s->hasfile);
274 sesshandle = open_settings_w_fsp(&s->savefile);
275 if (sesshandle == NULL) return; /* XXX report error */
276 save_open_settings(sesshandle, TRUE, &s->cfg);
277 close_settings_w(sesshandle);
278 }
279
280 void mac_savesessionas(void)
281 {
282 #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
283 Session *s = mac_windowsession(FrontWindow());
284 StandardFileReply sfr;
285 void *sesshandle;
286
287 StandardPutFile("\pSave session as:",
288 s->hasfile ? s->savefile.name : "\puntitled", &sfr);
289 if (!sfr.sfGood) return;
290
291 if (!sfr.sfReplacing) {
292 FSpCreateResFile(&sfr.sfFile, PUTTY_CREATOR, SESS_TYPE, sfr.sfScript);
293 if (ResError() != noErr) return; /* XXX report error */
294 }
295 sesshandle = open_settings_w_fsp(&sfr.sfFile);
296 if (sesshandle == NULL) return; /* XXX report error */
297 save_open_settings(sesshandle, TRUE, &s->cfg);
298 close_settings_w(sesshandle);
299 s->hasfile = TRUE;
300 s->savefile = sfr.sfFile;
301 #endif
302 }
303
304 pascal OSErr mac_aevt_oapp(const AppleEvent *req, AppleEvent *reply,
305 long refcon)
306 {
307 DescType type;
308 Size size;
309
310 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
311 &type, NULL, 0, &size) == noErr)
312 return errAEParamMissed;
313
314 /* XXX we should do something here. */
315 return noErr;
316 }
317
318 pascal OSErr mac_aevt_odoc(const AppleEvent *req, AppleEvent *reply,
319 long refcon)
320 {
321 DescType type;
322 Size size;
323 AEDescList docs = { typeNull, NULL };
324 OSErr err;
325
326 err = AEGetParamDesc(req, keyDirectObject, typeAEList, &docs);
327 if (err != noErr) goto out;
328
329 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
330 &type, NULL, 0, &size) == noErr) {
331 err = errAEParamMissed;
332 goto out;
333 }
334
335 err = mac_openlist(docs);
336
337 out:
338 AEDisposeDesc(&docs);
339 return err;
340 }
341
342 pascal OSErr mac_aevt_pdoc(const AppleEvent *req, AppleEvent *reply,
343 long refcon)
344 {
345 DescType type;
346 Size size;
347
348 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
349 &type, NULL, 0, &size) == noErr)
350 return errAEParamMissed;
351
352 /* We can't meaningfully do anything here. */
353 return errAEEventNotHandled;
354 }
355
356 /*
357 * Local Variables:
358 * c-file-style: "simon"
359 * End:
360 */