Use AF_UNIX, not AF_LOCAL, since the former is in POSIX and the latter is not.
[u/mdw/putty] / mac / macdlg.c
CommitLineData
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>
f8d7de79 38#include <TextUtils.h>
6cb61a05 39#include <Windows.h>
40
98f59a35 41#include <assert.h>
6cb61a05 42#include <string.h>
43
44#include "putty.h"
8a7e67ec 45#include "dialog.h"
6cb61a05 46#include "mac.h"
47#include "macresid.h"
2b8dca9e 48#include "storage.h"
6cb61a05 49
71581f96 50static void mac_config(int);
8a7e67ec 51static void mac_closedlg(WindowPtr);
71581f96 52static void mac_enddlg_config(WindowPtr, int);
53static void mac_enddlg_reconfig(WindowPtr, int);
f854b482 54
6cb61a05 55void mac_newsession(void)
56{
71581f96 57 mac_config(FALSE);
58}
59
60void mac_reconfig(void)
61{
62 mac_config(TRUE);
63}
64
65static void mac_config(int midsession)
66{
6cb61a05 67 Session *s;
f854b482 68 WinInfo *wi;
8a7e67ec 69 static struct sesslist sesslist;
f8d7de79 70 Str255 mactitle;
71581f96 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 }
6cb61a05 81
8a7e67ec 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();
71581f96 89 setup_config_box(s->ctrlbox, &sesslist, midsession, 0, 0);
8a7e67ec 90
91 s->settings_ctrls.data = &s->cfg;
71581f96 92 if (midsession)
93 s->settings_ctrls.end = &mac_enddlg_reconfig;
94 else
95 s->settings_ctrls.end = &mac_enddlg_config;
96
8a7e67ec 97 macctrl_layoutbox(s->ctrlbox, s->settings_window, &s->settings_ctrls);
6cb61a05 98
34773273 99 wi = snew(WinInfo);
f854b482 100 memset(wi, 0, sizeof(*wi));
101 wi->s = s;
5cec2fd9 102 wi->mcs = &s->settings_ctrls;
f854b482 103 wi->wtype = wSettings;
8a7e67ec 104 wi->update = &macctrl_update;
105 wi->click = &macctrl_click;
1f1ec0e5 106 wi->key = &macctrl_key;
8a7e67ec 107 wi->activate = &macctrl_activate;
108 wi->adjustmenus = &macctrl_adjustmenus;
ee10bc56 109 wi->close = &mac_closedlg;
f854b482 110 SetWRefCon(s->settings_window, (long)wi);
71581f96 111 if (midsession)
112 str = dupprintf("%s Reconfiguration", appname);
113 else
114 str = dupprintf("%s Configuration", appname);
115 c2pstrcpy(mactitle, str);
f8d7de79 116 SetWTitle(s->settings_window, mactitle);
6cb61a05 117 ShowWindow(s->settings_window);
118}
119
ee10bc56 120static 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
71581f96 130static void mac_enddlg_config(WindowPtr window, int value)
f73f2f31 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}
ee10bc56 141
71581f96 142static 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
5211281f 153void mac_dupsession(void)
154{
347bfcd7 155 Session *s1 = mac_windowsession(FrontWindow());
5211281f 156 Session *s2;
157
34773273 158 s2 = snew(Session);
5211281f 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
d70e3b83 167static OSErr mac_opensessionfrom(FSSpec *fss)
168{
169 FInfo fi;
2b8dca9e 170 Session *s;
2b8dca9e 171 void *sesshandle;
d70e3b83 172 OSErr err;
2b8dca9e 173
34773273 174 s = snew(Session);
2b8dca9e 175 memset(s, 0, sizeof(*s));
176
d70e3b83 177 err = FSpGetFInfo(fss, &fi);
178 if (err != noErr) return err;
179 if (fi.fdFlags & kIsStationery)
98f59a35 180 s->hasfile = FALSE;
181 else {
182 s->hasfile = TRUE;
d70e3b83 183 s->savefile = *fss;
98f59a35 184 }
2b8dca9e 185
d70e3b83 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
2b8dca9e 195 mac_startsession(s);
d70e3b83 196 return noErr;
2b8dca9e 197
198 fail:
199 sfree(s);
d70e3b83 200 return err;
201}
202
9ef3c1ca 203static 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
cef180ab 225void mac_opensession(void)
226{
9ef3c1ca 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 }
cef180ab 254#if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
9ef3c1ca 255 else {
256 StandardFileReply sfr;
257 static const OSType sftypes[] = { 'Sess', 0, 0, 0 };
d70e3b83 258
9ef3c1ca 259 StandardGetFile(NULL, 1, sftypes, &sfr);
260 if (!sfr.sfGood) return;
d70e3b83 261
9ef3c1ca 262 mac_opensessionfrom(&sfr.sfFile);
263 /* XXX handle error */
264 }
cef180ab 265#endif
2b8dca9e 266}
267
b537dd42 268void mac_savesession(void)
269{
347bfcd7 270 Session *s = mac_windowsession(FrontWindow());
98f59a35 271 void *sesshandle;
b537dd42 272
98f59a35 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);
b537dd42 278}
279
280void mac_savesessionas(void)
281{
cef180ab 282#if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
347bfcd7 283 Session *s = mac_windowsession(FrontWindow());
b537dd42 284 StandardFileReply sfr;
285 void *sesshandle;
286
98f59a35 287 StandardPutFile("\pSave session as:",
288 s->hasfile ? s->savefile.name : "\puntitled", &sfr);
b537dd42 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);
98f59a35 299 s->hasfile = TRUE;
300 s->savefile = sfr.sfFile;
cef180ab 301#endif
b537dd42 302}
303
d70e3b83 304pascal 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
318pascal OSErr mac_aevt_odoc(const AppleEvent *req, AppleEvent *reply,
319 long refcon)
320{
321 DescType type;
d70e3b83 322 Size size;
323 AEDescList docs = { typeNull, NULL };
324 OSErr err;
d70e3b83 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
9ef3c1ca 335 err = mac_openlist(docs);
d70e3b83 336
337 out:
338 AEDisposeDesc(&docs);
339 return err;
340}
341
342pascal 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
6cb61a05 356/*
357 * Local Variables:
358 * c-file-style: "simon"
359 * End:
360 */