sfree the memory dupprintf allocated
[sgt/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);
66dba8af 116 sfree(str);
f8d7de79 117 SetWTitle(s->settings_window, mactitle);
6cb61a05 118 ShowWindow(s->settings_window);
119}
120
ee10bc56 121static void mac_closedlg(WindowPtr window)
122{
123 Session *s = mac_windowsession(window);
124
125 macctrl_close(window);
126 DisposeWindow(window);
127 if (s->window == NULL)
128 sfree(s);
129}
130
71581f96 131static void mac_enddlg_config(WindowPtr window, int value)
f73f2f31 132{
133 Session *s = mac_windowsession(window);
134
135 if (value == 0)
136 mac_closedlg(window);
137 else {
138 mac_startsession(s);
139 mac_closedlg(window);
140 }
141}
ee10bc56 142
71581f96 143static void mac_enddlg_reconfig(WindowPtr window, int value)
144{
145 Session *s = mac_windowsession(window);
146
147 if (value == 0)
148 mac_closedlg(window);
149 else {
150 mac_closedlg(window);
151 }
152}
153
5211281f 154void mac_dupsession(void)
155{
347bfcd7 156 Session *s1 = mac_windowsession(FrontWindow());
5211281f 157 Session *s2;
158
34773273 159 s2 = snew(Session);
5211281f 160 memset(s2, 0, sizeof(*s2));
161 s2->cfg = s1->cfg;
162 s2->hasfile = s1->hasfile;
163 s2->savefile = s1->savefile;
164
165 mac_startsession(s2);
166}
167
d70e3b83 168static OSErr mac_opensessionfrom(FSSpec *fss)
169{
170 FInfo fi;
2b8dca9e 171 Session *s;
2b8dca9e 172 void *sesshandle;
d70e3b83 173 OSErr err;
2b8dca9e 174
34773273 175 s = snew(Session);
2b8dca9e 176 memset(s, 0, sizeof(*s));
177
d70e3b83 178 err = FSpGetFInfo(fss, &fi);
179 if (err != noErr) return err;
180 if (fi.fdFlags & kIsStationery)
98f59a35 181 s->hasfile = FALSE;
182 else {
183 s->hasfile = TRUE;
d70e3b83 184 s->savefile = *fss;
98f59a35 185 }
2b8dca9e 186
d70e3b83 187 sesshandle = open_settings_r_fsp(fss);
188 if (sesshandle == NULL) {
189 /* XXX need a way to pass up an error number */
190 err = -9999;
191 goto fail;
192 }
193 load_open_settings(sesshandle, TRUE, &s->cfg);
194 close_settings_r(sesshandle);
195
2b8dca9e 196 mac_startsession(s);
d70e3b83 197 return noErr;
2b8dca9e 198
199 fail:
200 sfree(s);
d70e3b83 201 return err;
202}
203
9ef3c1ca 204static OSErr mac_openlist(AEDesc docs)
205{
206 OSErr err;
207 long ndocs, i;
208 FSSpec fss;
209 AEKeyword keywd;
210 DescType type;
211 Size size;
212
213 err = AECountItems(&docs, &ndocs);
214 if (err != noErr) return err;
215
216 for (i = 0; i < ndocs; i++) {
217 err = AEGetNthPtr(&docs, i + 1, typeFSS,
218 &keywd, &type, &fss, sizeof(fss), &size);
219 if (err != noErr) return err;;
220 err = mac_opensessionfrom(&fss);
221 if (err != noErr) return err;
222 }
223 return noErr;
224}
225
cef180ab 226void mac_opensession(void)
227{
9ef3c1ca 228
229 if (mac_gestalts.navsvers > 0) {
230 NavReplyRecord navr;
231 NavDialogOptions navopts;
232 NavTypeListHandle navtypes;
233 AEDesc defaultloc = { 'null', NULL };
234 AEDesc *navdefault = NULL;
235 short vol;
236 long dirid;
237 FSSpec fss;
238
239 if (NavGetDefaultDialogOptions(&navopts) != noErr) return;
240 /* XXX should we create sessions dir? */
241 if (get_session_dir(FALSE, &vol, &dirid) == noErr &&
242 FSMakeFSSpec(vol, dirid, NULL, &fss) == noErr &&
243 AECreateDesc(typeFSS, &fss, sizeof(fss), &defaultloc) == noErr)
244 navdefault = &defaultloc;
245 /* Can't meaningfully preview a saved session yet */
246 navopts.dialogOptionFlags &= ~kNavAllowPreviews;
247 navtypes = (NavTypeListHandle)GetResource('open', open_pTTY);
248 if (NavGetFile(navdefault, &navr, &navopts, NULL, NULL, NULL, navtypes,
249 NULL) == noErr && navr.validRecord)
250 mac_openlist(navr.selection);
251 NavDisposeReply(&navr);
252 if (navtypes != NULL)
253 ReleaseResource((Handle)navtypes);
254 }
cef180ab 255#if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
9ef3c1ca 256 else {
257 StandardFileReply sfr;
258 static const OSType sftypes[] = { 'Sess', 0, 0, 0 };
d70e3b83 259
9ef3c1ca 260 StandardGetFile(NULL, 1, sftypes, &sfr);
261 if (!sfr.sfGood) return;
d70e3b83 262
9ef3c1ca 263 mac_opensessionfrom(&sfr.sfFile);
264 /* XXX handle error */
265 }
cef180ab 266#endif
2b8dca9e 267}
268
b537dd42 269void mac_savesession(void)
270{
347bfcd7 271 Session *s = mac_windowsession(FrontWindow());
98f59a35 272 void *sesshandle;
b537dd42 273
98f59a35 274 assert(s->hasfile);
275 sesshandle = open_settings_w_fsp(&s->savefile);
276 if (sesshandle == NULL) return; /* XXX report error */
277 save_open_settings(sesshandle, TRUE, &s->cfg);
278 close_settings_w(sesshandle);
b537dd42 279}
280
281void mac_savesessionas(void)
282{
cef180ab 283#if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
347bfcd7 284 Session *s = mac_windowsession(FrontWindow());
b537dd42 285 StandardFileReply sfr;
286 void *sesshandle;
287
98f59a35 288 StandardPutFile("\pSave session as:",
289 s->hasfile ? s->savefile.name : "\puntitled", &sfr);
b537dd42 290 if (!sfr.sfGood) return;
291
292 if (!sfr.sfReplacing) {
293 FSpCreateResFile(&sfr.sfFile, PUTTY_CREATOR, SESS_TYPE, sfr.sfScript);
294 if (ResError() != noErr) return; /* XXX report error */
295 }
296 sesshandle = open_settings_w_fsp(&sfr.sfFile);
297 if (sesshandle == NULL) return; /* XXX report error */
298 save_open_settings(sesshandle, TRUE, &s->cfg);
299 close_settings_w(sesshandle);
98f59a35 300 s->hasfile = TRUE;
301 s->savefile = sfr.sfFile;
cef180ab 302#endif
b537dd42 303}
304
d70e3b83 305pascal OSErr mac_aevt_oapp(const AppleEvent *req, AppleEvent *reply,
306 long refcon)
307{
308 DescType type;
309 Size size;
310
311 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
312 &type, NULL, 0, &size) == noErr)
313 return errAEParamMissed;
314
315 /* XXX we should do something here. */
316 return noErr;
317}
318
319pascal OSErr mac_aevt_odoc(const AppleEvent *req, AppleEvent *reply,
320 long refcon)
321{
322 DescType type;
d70e3b83 323 Size size;
324 AEDescList docs = { typeNull, NULL };
325 OSErr err;
d70e3b83 326
327 err = AEGetParamDesc(req, keyDirectObject, typeAEList, &docs);
328 if (err != noErr) goto out;
329
330 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
331 &type, NULL, 0, &size) == noErr) {
332 err = errAEParamMissed;
333 goto out;
334 }
335
9ef3c1ca 336 err = mac_openlist(docs);
d70e3b83 337
338 out:
339 AEDisposeDesc(&docs);
340 return err;
341}
342
343pascal OSErr mac_aevt_pdoc(const AppleEvent *req, AppleEvent *reply,
344 long refcon)
345{
346 DescType type;
347 Size size;
348
349 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
350 &type, NULL, 0, &size) == noErr)
351 return errAEParamMissed;
352
353 /* We can't meaningfully do anything here. */
354 return errAEEventNotHandled;
355}
356
6cb61a05 357/*
358 * Local Variables:
359 * c-file-style: "simon"
360 * End:
361 */