Carbonise. Until we support Navigation Services, loading and saving sessions
[u/mdw/putty] / mac / macdlg.c
1 /* $Id: macdlg.c,v 1.9 2003/02/01 23:55:00 ben Exp $ */
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 <Dialogs.h>
36 #include <Resources.h>
37 #include <StandardFile.h>
38 #include <Windows.h>
39
40 #include <assert.h>
41 #include <string.h>
42
43 #include "putty.h"
44 #include "mac.h"
45 #include "macresid.h"
46 #include "storage.h"
47
48 void mac_newsession(void)
49 {
50 Session *s;
51
52 /* This should obviously be initialised by other means */
53 s = smalloc(sizeof(*s));
54 memset(s, 0, sizeof(*s));
55 do_defaults(NULL, &s->cfg);
56 s->hasfile = FALSE;
57
58 s->settings_window =
59 GetDialogWindow(GetNewDialog(wSettings, NULL, (WindowPtr)-1));
60
61 SetWRefCon(s->settings_window, (long)s);
62 ShowWindow(s->settings_window);
63 }
64
65 void mac_dupsession(void)
66 {
67 Session *s1 = (Session *)GetWRefCon(FrontWindow());
68 Session *s2;
69
70 s2 = smalloc(sizeof(*s2));
71 memset(s2, 0, sizeof(*s2));
72 s2->cfg = s1->cfg;
73 s2->hasfile = s1->hasfile;
74 s2->savefile = s1->savefile;
75
76 mac_startsession(s2);
77 }
78
79 static OSErr mac_opensessionfrom(FSSpec *fss)
80 {
81 FInfo fi;
82 Session *s;
83 void *sesshandle;
84 OSErr err;
85
86 s = smalloc(sizeof(*s));
87 memset(s, 0, sizeof(*s));
88
89 err = FSpGetFInfo(fss, &fi);
90 if (err != noErr) return err;
91 if (fi.fdFlags & kIsStationery)
92 s->hasfile = FALSE;
93 else {
94 s->hasfile = TRUE;
95 s->savefile = *fss;
96 }
97
98 sesshandle = open_settings_r_fsp(fss);
99 if (sesshandle == NULL) {
100 /* XXX need a way to pass up an error number */
101 err = -9999;
102 goto fail;
103 }
104 load_open_settings(sesshandle, TRUE, &s->cfg);
105 close_settings_r(sesshandle);
106
107 mac_startsession(s);
108 return noErr;
109
110 fail:
111 sfree(s);
112 return err;
113 }
114
115 void mac_opensession(void)
116 {
117 #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
118 StandardFileReply sfr;
119 static const OSType sftypes[] = { 'Sess', 0, 0, 0 };
120
121 StandardGetFile(NULL, 1, sftypes, &sfr);
122 if (!sfr.sfGood) return;
123
124 mac_opensessionfrom(&sfr.sfFile);
125 /* XXX handle error */
126 #endif
127 }
128
129 void mac_savesession(void)
130 {
131 Session *s = (Session *)GetWRefCon(FrontWindow());
132 void *sesshandle;
133
134 assert(s->hasfile);
135 sesshandle = open_settings_w_fsp(&s->savefile);
136 if (sesshandle == NULL) return; /* XXX report error */
137 save_open_settings(sesshandle, TRUE, &s->cfg);
138 close_settings_w(sesshandle);
139 }
140
141 void mac_savesessionas(void)
142 {
143 #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */
144 Session *s = (Session *)GetWRefCon(FrontWindow());
145 StandardFileReply sfr;
146 void *sesshandle;
147
148 StandardPutFile("\pSave session as:",
149 s->hasfile ? s->savefile.name : "\puntitled", &sfr);
150 if (!sfr.sfGood) return;
151
152 if (!sfr.sfReplacing) {
153 FSpCreateResFile(&sfr.sfFile, PUTTY_CREATOR, SESS_TYPE, sfr.sfScript);
154 if (ResError() != noErr) return; /* XXX report error */
155 }
156 sesshandle = open_settings_w_fsp(&sfr.sfFile);
157 if (sesshandle == NULL) return; /* XXX report error */
158 save_open_settings(sesshandle, TRUE, &s->cfg);
159 close_settings_w(sesshandle);
160 s->hasfile = TRUE;
161 s->savefile = sfr.sfFile;
162 #endif
163 }
164
165 pascal OSErr mac_aevt_oapp(const AppleEvent *req, AppleEvent *reply,
166 long refcon)
167 {
168 DescType type;
169 Size size;
170
171 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
172 &type, NULL, 0, &size) == noErr)
173 return errAEParamMissed;
174
175 /* XXX we should do something here. */
176 return noErr;
177 }
178
179 pascal OSErr mac_aevt_odoc(const AppleEvent *req, AppleEvent *reply,
180 long refcon)
181 {
182 DescType type;
183 AEKeyword keywd;
184 Size size;
185 AEDescList docs = { typeNull, NULL };
186 OSErr err;
187 long ndocs, i;
188 FSSpec fss;
189
190 err = AEGetParamDesc(req, keyDirectObject, typeAEList, &docs);
191 if (err != noErr) goto out;
192
193 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
194 &type, NULL, 0, &size) == noErr) {
195 err = errAEParamMissed;
196 goto out;
197 }
198
199 err = AECountItems(&docs, &ndocs);
200 if (err != noErr) goto out;
201
202 for (i = 0; i < ndocs; i++) {
203 err = AEGetNthPtr(&docs, i + 1, typeFSS,
204 &keywd, &type, &fss, sizeof(fss), &size);
205 if (err != noErr) goto out;
206 err = mac_opensessionfrom(&fss);
207 if (err != noErr) goto out;
208 }
209
210 out:
211 AEDisposeDesc(&docs);
212 return err;
213 }
214
215 pascal OSErr mac_aevt_pdoc(const AppleEvent *req, AppleEvent *reply,
216 long refcon)
217 {
218 DescType type;
219 Size size;
220
221 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
222 &type, NULL, 0, &size) == noErr)
223 return errAEParamMissed;
224
225 /* We can't meaningfully do anything here. */
226 return errAEEventNotHandled;
227 }
228
229 void mac_activatedlg(WindowPtr window, EventRecord *event)
230 {
231 DialogItemType itemtype;
232 Handle itemhandle;
233 short item;
234 Rect itemrect;
235 int active;
236 DialogRef dialog = GetDialogFromWindow(window);
237
238 active = (event->modifiers & activeFlag) != 0;
239 GetDialogItem(dialog, wiSettingsOpen, &itemtype, &itemhandle, &itemrect);
240 HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
241 DialogSelect(event, &dialog, &item);
242 }
243
244 void mac_clickdlg(WindowPtr window, EventRecord *event)
245 {
246 short item;
247 Session *s = (Session *)GetWRefCon(window);
248 DialogRef dialog = GetDialogFromWindow(window);
249
250 if (DialogSelect(event, &dialog, &item))
251 switch (item) {
252 case wiSettingsOpen:
253 HideWindow(window);
254 mac_startsession(s);
255 break;
256 }
257 }
258
259 /*
260 * Local Variables:
261 * c-file-style: "simon"
262 * End:
263 */