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