In get_sesslist(), when freeing, set freed members to NULL on general
[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;
8154b602 80 s->session_closed = FALSE;
71581f96 81 }
6cb61a05 82
d36f85ac 83 /* Copy the configuration somewhere else in case this is a *
84 * reconfiguration and the user cancels the operation */
85
86 s->temp_cfg = s->cfg;
87
8a7e67ec 88 if (HAVE_COLOR_QD())
89 s->settings_window = GetNewCWindow(wSettings, NULL, (WindowPtr)-1);
90 else
91 s->settings_window = GetNewWindow(wSettings, NULL, (WindowPtr)-1);
92
93 get_sesslist(&sesslist, TRUE);
94 s->ctrlbox = ctrl_new_box();
71581f96 95 setup_config_box(s->ctrlbox, &sesslist, midsession, 0, 0);
8a7e67ec 96
d36f85ac 97 s->settings_ctrls.data = &s->temp_cfg;
71581f96 98 if (midsession)
99 s->settings_ctrls.end = &mac_enddlg_reconfig;
100 else
101 s->settings_ctrls.end = &mac_enddlg_config;
102
8a7e67ec 103 macctrl_layoutbox(s->ctrlbox, s->settings_window, &s->settings_ctrls);
6cb61a05 104
34773273 105 wi = snew(WinInfo);
f854b482 106 memset(wi, 0, sizeof(*wi));
107 wi->s = s;
5cec2fd9 108 wi->mcs = &s->settings_ctrls;
f854b482 109 wi->wtype = wSettings;
8a7e67ec 110 wi->update = &macctrl_update;
111 wi->click = &macctrl_click;
1f1ec0e5 112 wi->key = &macctrl_key;
8a7e67ec 113 wi->activate = &macctrl_activate;
114 wi->adjustmenus = &macctrl_adjustmenus;
ee10bc56 115 wi->close = &mac_closedlg;
f854b482 116 SetWRefCon(s->settings_window, (long)wi);
71581f96 117 if (midsession)
118 str = dupprintf("%s Reconfiguration", appname);
119 else
120 str = dupprintf("%s Configuration", appname);
121 c2pstrcpy(mactitle, str);
66dba8af 122 sfree(str);
f8d7de79 123 SetWTitle(s->settings_window, mactitle);
6cb61a05 124 ShowWindow(s->settings_window);
125}
126
ee10bc56 127static void mac_closedlg(WindowPtr window)
128{
129 Session *s = mac_windowsession(window);
130
131 macctrl_close(window);
132 DisposeWindow(window);
133 if (s->window == NULL)
134 sfree(s);
135}
136
71581f96 137static void mac_enddlg_config(WindowPtr window, int value)
f73f2f31 138{
139 Session *s = mac_windowsession(window);
140
141 if (value == 0)
142 mac_closedlg(window);
143 else {
d36f85ac 144 s->cfg = s->temp_cfg;
f73f2f31 145 mac_startsession(s);
146 mac_closedlg(window);
147 }
148}
ee10bc56 149
71581f96 150static void mac_enddlg_reconfig(WindowPtr window, int value)
151{
152 Session *s = mac_windowsession(window);
153
154 if (value == 0)
155 mac_closedlg(window);
156 else {
d36f85ac 157 Config prev_cfg = s->cfg;
158 s->cfg = s->temp_cfg;
71581f96 159 mac_closedlg(window);
d36f85ac 160
161 /* Pass new config data to the logging module */
162 log_reconfig(s->logctx, &s->cfg);
163
164 /*
165 * Flush the line discipline's edit buffer in the
166 * case where local editing has just been disabled.
167 */
168 if (s->ldisc)
169 ldisc_send(s->ldisc, NULL, 0, 0);
170
171 /* Change the palette */
172 palette_reset(s);
173
0d737aa4 174 /* Reinitialise line codepage */
175 init_ucs(s);
176
d36f85ac 177 /* Pass new config data to the terminal */
178 term_reconfig(s->term, &s->cfg);
179
180 /* Pass new config data to the back end */
181 if (s->back)
182 s->back->reconfig(s->backhandle, &s->cfg);
183
184 /* Screen size changed ? */
185 if (s->cfg.height != prev_cfg.height ||
186 s->cfg.width != prev_cfg.width ||
187 s->cfg.savelines != prev_cfg.savelines) {
d36f85ac 188 request_resize(s, s->cfg.width, s->cfg.height);
189 }
190
191 /* Set the window title */
192 if (s->cfg.wintitle[0])
193 set_title(s, s->cfg.wintitle);
194
fb783caf 195 /* Scroll bar */
196 if (s->cfg.scrollbar != prev_cfg.scrollbar)
197 request_resize(s, s->cfg.width, s->cfg.height);
198
199 /* TODO: zoom, font */
71581f96 200 }
201}
202
5211281f 203void mac_dupsession(void)
204{
347bfcd7 205 Session *s1 = mac_windowsession(FrontWindow());
5211281f 206 Session *s2;
207
34773273 208 s2 = snew(Session);
5211281f 209 memset(s2, 0, sizeof(*s2));
210 s2->cfg = s1->cfg;
211 s2->hasfile = s1->hasfile;
212 s2->savefile = s1->savefile;
213
214 mac_startsession(s2);
215}
216
d70e3b83 217static OSErr mac_opensessionfrom(FSSpec *fss)
218{
219 FInfo fi;
2b8dca9e 220 Session *s;
2b8dca9e 221 void *sesshandle;
d70e3b83 222 OSErr err;
2b8dca9e 223
34773273 224 s = snew(Session);
2b8dca9e 225 memset(s, 0, sizeof(*s));
226
d70e3b83 227 err = FSpGetFInfo(fss, &fi);
228 if (err != noErr) return err;
229 if (fi.fdFlags & kIsStationery)
98f59a35 230 s->hasfile = FALSE;
231 else {
232 s->hasfile = TRUE;
d70e3b83 233 s->savefile = *fss;
98f59a35 234 }
2b8dca9e 235
d70e3b83 236 sesshandle = open_settings_r_fsp(fss);
237 if (sesshandle == NULL) {
238 /* XXX need a way to pass up an error number */
239 err = -9999;
240 goto fail;
241 }
242 load_open_settings(sesshandle, TRUE, &s->cfg);
243 close_settings_r(sesshandle);
244
2b8dca9e 245 mac_startsession(s);
d70e3b83 246 return noErr;
2b8dca9e 247
248 fail:
249 sfree(s);
d70e3b83 250 return err;
251}
252
9ef3c1ca 253static OSErr mac_openlist(AEDesc docs)
254{
255 OSErr err;
256 long ndocs, i;
257 FSSpec fss;
258 AEKeyword keywd;
259 DescType type;
260 Size size;
261
262 err = AECountItems(&docs, &ndocs);
263 if (err != noErr) return err;
264
265 for (i = 0; i < ndocs; i++) {
266 err = AEGetNthPtr(&docs, i + 1, typeFSS,
267 &keywd, &type, &fss, sizeof(fss), &size);
268 if (err != noErr) return err;;
269 err = mac_opensessionfrom(&fss);
270 if (err != noErr) return err;
271 }
272 return noErr;
273}
274
cef180ab 275void mac_opensession(void)
276{
9ef3c1ca 277
278 if (mac_gestalts.navsvers > 0) {
279 NavReplyRecord navr;
280 NavDialogOptions navopts;
281 NavTypeListHandle navtypes;
282 AEDesc defaultloc = { 'null', NULL };
283 AEDesc *navdefault = NULL;
284 short vol;
285 long dirid;
286 FSSpec fss;
287
288 if (NavGetDefaultDialogOptions(&navopts) != noErr) return;
289 /* XXX should we create sessions dir? */
290 if (get_session_dir(FALSE, &vol, &dirid) == noErr &&
291 FSMakeFSSpec(vol, dirid, NULL, &fss) == noErr &&
292 AECreateDesc(typeFSS, &fss, sizeof(fss), &defaultloc) == noErr)
293 navdefault = &defaultloc;
294 /* Can't meaningfully preview a saved session yet */
295 navopts.dialogOptionFlags &= ~kNavAllowPreviews;
296 navtypes = (NavTypeListHandle)GetResource('open', open_pTTY);
297 if (NavGetFile(navdefault, &navr, &navopts, NULL, NULL, NULL, navtypes,
298 NULL) == noErr && navr.validRecord)
299 mac_openlist(navr.selection);
300 NavDisposeReply(&navr);
301 if (navtypes != NULL)
302 ReleaseResource((Handle)navtypes);
303 }
cef180ab 304#if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
9ef3c1ca 305 else {
306 StandardFileReply sfr;
307 static const OSType sftypes[] = { 'Sess', 0, 0, 0 };
d70e3b83 308
9ef3c1ca 309 StandardGetFile(NULL, 1, sftypes, &sfr);
310 if (!sfr.sfGood) return;
d70e3b83 311
9ef3c1ca 312 mac_opensessionfrom(&sfr.sfFile);
313 /* XXX handle error */
314 }
cef180ab 315#endif
2b8dca9e 316}
317
b537dd42 318void mac_savesession(void)
319{
347bfcd7 320 Session *s = mac_windowsession(FrontWindow());
98f59a35 321 void *sesshandle;
b537dd42 322
98f59a35 323 assert(s->hasfile);
324 sesshandle = open_settings_w_fsp(&s->savefile);
325 if (sesshandle == NULL) return; /* XXX report error */
326 save_open_settings(sesshandle, TRUE, &s->cfg);
327 close_settings_w(sesshandle);
b537dd42 328}
329
330void mac_savesessionas(void)
331{
cef180ab 332#if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
347bfcd7 333 Session *s = mac_windowsession(FrontWindow());
b537dd42 334 StandardFileReply sfr;
335 void *sesshandle;
336
98f59a35 337 StandardPutFile("\pSave session as:",
338 s->hasfile ? s->savefile.name : "\puntitled", &sfr);
b537dd42 339 if (!sfr.sfGood) return;
340
341 if (!sfr.sfReplacing) {
342 FSpCreateResFile(&sfr.sfFile, PUTTY_CREATOR, SESS_TYPE, sfr.sfScript);
343 if (ResError() != noErr) return; /* XXX report error */
344 }
345 sesshandle = open_settings_w_fsp(&sfr.sfFile);
346 if (sesshandle == NULL) return; /* XXX report error */
347 save_open_settings(sesshandle, TRUE, &s->cfg);
348 close_settings_w(sesshandle);
98f59a35 349 s->hasfile = TRUE;
350 s->savefile = sfr.sfFile;
cef180ab 351#endif
b537dd42 352}
353
d70e3b83 354pascal OSErr mac_aevt_oapp(const AppleEvent *req, AppleEvent *reply,
355 long refcon)
356{
357 DescType type;
358 Size size;
359
360 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
361 &type, NULL, 0, &size) == noErr)
362 return errAEParamMissed;
363
364 /* XXX we should do something here. */
365 return noErr;
366}
367
368pascal OSErr mac_aevt_odoc(const AppleEvent *req, AppleEvent *reply,
369 long refcon)
370{
371 DescType type;
d70e3b83 372 Size size;
373 AEDescList docs = { typeNull, NULL };
374 OSErr err;
d70e3b83 375
376 err = AEGetParamDesc(req, keyDirectObject, typeAEList, &docs);
377 if (err != noErr) goto out;
378
379 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
380 &type, NULL, 0, &size) == noErr) {
381 err = errAEParamMissed;
382 goto out;
383 }
384
9ef3c1ca 385 err = mac_openlist(docs);
d70e3b83 386
387 out:
388 AEDisposeDesc(&docs);
389 return err;
390}
391
392pascal OSErr mac_aevt_pdoc(const AppleEvent *req, AppleEvent *reply,
393 long refcon)
394{
395 DescType type;
396 Size size;
397
398 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
399 &type, NULL, 0, &size) == noErr)
400 return errAEParamMissed;
401
402 /* We can't meaningfully do anything here. */
403 return errAEEventNotHandled;
404}
405
6cb61a05 406/*
407 * Local Variables:
408 * c-file-style: "simon"
409 * End:
410 */