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