57b73ad9d96174e909eb998839a4bde36a690756
[u/mdw/putty] / mac / macdlg.c
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
50 static void mac_closedlg(WindowPtr);
51 static void mac_enddlg(WindowPtr, int);
52
53 void mac_newsession(void)
54 {
55 Session *s;
56 WinInfo *wi;
57 static struct sesslist sesslist;
58 Str255 mactitle;
59
60 s = snew(Session);
61 memset(s, 0, sizeof(*s));
62 do_defaults(NULL, &s->cfg);
63 s->hasfile = FALSE;
64
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();
72 setup_config_box(s->ctrlbox, &sesslist, FALSE, 0, 0);
73
74 s->settings_ctrls.data = &s->cfg;
75 s->settings_ctrls.end = &mac_enddlg;
76 macctrl_layoutbox(s->ctrlbox, s->settings_window, &s->settings_ctrls);
77
78 wi = snew(WinInfo);
79 memset(wi, 0, sizeof(*wi));
80 wi->s = s;
81 wi->mcs = &s->settings_ctrls;
82 wi->wtype = wSettings;
83 wi->update = &macctrl_update;
84 wi->click = &macctrl_click;
85 wi->key = &macctrl_key;
86 wi->activate = &macctrl_activate;
87 wi->adjustmenus = &macctrl_adjustmenus;
88 wi->close = &mac_closedlg;
89 SetWRefCon(s->settings_window, (long)wi);
90 c2pstrcpy(mactitle, "PuTTY Configuration");
91 SetWTitle(s->settings_window, mactitle);
92 ShowWindow(s->settings_window);
93 }
94
95 static 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
105 static 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 }
116
117 void mac_dupsession(void)
118 {
119 Session *s1 = mac_windowsession(FrontWindow());
120 Session *s2;
121
122 s2 = snew(Session);
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
131 static OSErr mac_opensessionfrom(FSSpec *fss)
132 {
133 FInfo fi;
134 Session *s;
135 void *sesshandle;
136 OSErr err;
137
138 s = snew(Session);
139 memset(s, 0, sizeof(*s));
140
141 err = FSpGetFInfo(fss, &fi);
142 if (err != noErr) return err;
143 if (fi.fdFlags & kIsStationery)
144 s->hasfile = FALSE;
145 else {
146 s->hasfile = TRUE;
147 s->savefile = *fss;
148 }
149
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
159 mac_startsession(s);
160 return noErr;
161
162 fail:
163 sfree(s);
164 return err;
165 }
166
167 static 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
189 void mac_opensession(void)
190 {
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 }
218 #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
219 else {
220 StandardFileReply sfr;
221 static const OSType sftypes[] = { 'Sess', 0, 0, 0 };
222
223 StandardGetFile(NULL, 1, sftypes, &sfr);
224 if (!sfr.sfGood) return;
225
226 mac_opensessionfrom(&sfr.sfFile);
227 /* XXX handle error */
228 }
229 #endif
230 }
231
232 void mac_savesession(void)
233 {
234 Session *s = mac_windowsession(FrontWindow());
235 void *sesshandle;
236
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);
242 }
243
244 void mac_savesessionas(void)
245 {
246 #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
247 Session *s = mac_windowsession(FrontWindow());
248 StandardFileReply sfr;
249 void *sesshandle;
250
251 StandardPutFile("\pSave session as:",
252 s->hasfile ? s->savefile.name : "\puntitled", &sfr);
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);
263 s->hasfile = TRUE;
264 s->savefile = sfr.sfFile;
265 #endif
266 }
267
268 pascal 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
282 pascal OSErr mac_aevt_odoc(const AppleEvent *req, AppleEvent *reply,
283 long refcon)
284 {
285 DescType type;
286 Size size;
287 AEDescList docs = { typeNull, NULL };
288 OSErr err;
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
299 err = mac_openlist(docs);
300
301 out:
302 AEDisposeDesc(&docs);
303 return err;
304 }
305
306 pascal 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
320 /*
321 * Local Variables:
322 * c-file-style: "simon"
323 * End:
324 */