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