sfree the memory dupprintf allocated
[sgt/putty] / mac / macdlg.c
... / ...
CommitLineData
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
50static void mac_config(int);
51static void mac_closedlg(WindowPtr);
52static void mac_enddlg_config(WindowPtr, int);
53static void mac_enddlg_reconfig(WindowPtr, int);
54
55void mac_newsession(void)
56{
57 mac_config(FALSE);
58}
59
60void mac_reconfig(void)
61{
62 mac_config(TRUE);
63}
64
65static 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 sfree(str);
117 SetWTitle(s->settings_window, mactitle);
118 ShowWindow(s->settings_window);
119}
120
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
131static void mac_enddlg_config(WindowPtr window, int value)
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}
142
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
154void mac_dupsession(void)
155{
156 Session *s1 = mac_windowsession(FrontWindow());
157 Session *s2;
158
159 s2 = snew(Session);
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
168static OSErr mac_opensessionfrom(FSSpec *fss)
169{
170 FInfo fi;
171 Session *s;
172 void *sesshandle;
173 OSErr err;
174
175 s = snew(Session);
176 memset(s, 0, sizeof(*s));
177
178 err = FSpGetFInfo(fss, &fi);
179 if (err != noErr) return err;
180 if (fi.fdFlags & kIsStationery)
181 s->hasfile = FALSE;
182 else {
183 s->hasfile = TRUE;
184 s->savefile = *fss;
185 }
186
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
196 mac_startsession(s);
197 return noErr;
198
199 fail:
200 sfree(s);
201 return err;
202}
203
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
226void mac_opensession(void)
227{
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 }
255#if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
256 else {
257 StandardFileReply sfr;
258 static const OSType sftypes[] = { 'Sess', 0, 0, 0 };
259
260 StandardGetFile(NULL, 1, sftypes, &sfr);
261 if (!sfr.sfGood) return;
262
263 mac_opensessionfrom(&sfr.sfFile);
264 /* XXX handle error */
265 }
266#endif
267}
268
269void mac_savesession(void)
270{
271 Session *s = mac_windowsession(FrontWindow());
272 void *sesshandle;
273
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);
279}
280
281void mac_savesessionas(void)
282{
283#if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
284 Session *s = mac_windowsession(FrontWindow());
285 StandardFileReply sfr;
286 void *sesshandle;
287
288 StandardPutFile("\pSave session as:",
289 s->hasfile ? s->savefile.name : "\puntitled", &sfr);
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);
300 s->hasfile = TRUE;
301 s->savefile = sfr.sfFile;
302#endif
303}
304
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;
323 Size size;
324 AEDescList docs = { typeNull, NULL };
325 OSErr err;
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
336 err = mac_openlist(docs);
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
357/*
358 * Local Variables:
359 * c-file-style: "simon"
360 * End:
361 */