cef180ab |
1 | /* $Id: macdlg.c,v 1.9 2003/02/01 23:55:00 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> |
6cb61a05 |
35 | #include <Dialogs.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" |
44 | #include "mac.h" |
45 | #include "macresid.h" |
2b8dca9e |
46 | #include "storage.h" |
6cb61a05 |
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); |
98f59a35 |
56 | s->hasfile = FALSE; |
6cb61a05 |
57 | |
cef180ab |
58 | s->settings_window = |
59 | GetDialogWindow(GetNewDialog(wSettings, NULL, (WindowPtr)-1)); |
6cb61a05 |
60 | |
61 | SetWRefCon(s->settings_window, (long)s); |
62 | ShowWindow(s->settings_window); |
63 | } |
64 | |
5211281f |
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 | |
d70e3b83 |
79 | static OSErr mac_opensessionfrom(FSSpec *fss) |
80 | { |
81 | FInfo fi; |
2b8dca9e |
82 | Session *s; |
2b8dca9e |
83 | void *sesshandle; |
d70e3b83 |
84 | OSErr err; |
2b8dca9e |
85 | |
86 | s = smalloc(sizeof(*s)); |
87 | memset(s, 0, sizeof(*s)); |
88 | |
d70e3b83 |
89 | err = FSpGetFInfo(fss, &fi); |
90 | if (err != noErr) return err; |
91 | if (fi.fdFlags & kIsStationery) |
98f59a35 |
92 | s->hasfile = FALSE; |
93 | else { |
94 | s->hasfile = TRUE; |
d70e3b83 |
95 | s->savefile = *fss; |
98f59a35 |
96 | } |
2b8dca9e |
97 | |
d70e3b83 |
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 | |
2b8dca9e |
107 | mac_startsession(s); |
d70e3b83 |
108 | return noErr; |
2b8dca9e |
109 | |
110 | fail: |
111 | sfree(s); |
d70e3b83 |
112 | return err; |
113 | } |
114 | |
cef180ab |
115 | void mac_opensession(void) |
116 | { |
117 | #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */ |
d70e3b83 |
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 */ |
cef180ab |
126 | #endif |
2b8dca9e |
127 | } |
128 | |
b537dd42 |
129 | void mac_savesession(void) |
130 | { |
98f59a35 |
131 | Session *s = (Session *)GetWRefCon(FrontWindow()); |
132 | void *sesshandle; |
b537dd42 |
133 | |
98f59a35 |
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); |
b537dd42 |
139 | } |
140 | |
141 | void mac_savesessionas(void) |
142 | { |
cef180ab |
143 | #if !TARGET_API_MAC_CARBON /* XXX Navigation Services */ |
b537dd42 |
144 | Session *s = (Session *)GetWRefCon(FrontWindow()); |
145 | StandardFileReply sfr; |
146 | void *sesshandle; |
147 | |
98f59a35 |
148 | StandardPutFile("\pSave session as:", |
149 | s->hasfile ? s->savefile.name : "\puntitled", &sfr); |
b537dd42 |
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); |
98f59a35 |
160 | s->hasfile = TRUE; |
161 | s->savefile = sfr.sfFile; |
cef180ab |
162 | #endif |
b537dd42 |
163 | } |
164 | |
d70e3b83 |
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++) { |
00c0aa61 |
203 | err = AEGetNthPtr(&docs, i + 1, typeFSS, |
204 | &keywd, &type, &fss, sizeof(fss), &size); |
d70e3b83 |
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 | |
6cb61a05 |
229 | void mac_activatedlg(WindowPtr window, EventRecord *event) |
230 | { |
231 | DialogItemType itemtype; |
232 | Handle itemhandle; |
233 | short item; |
234 | Rect itemrect; |
235 | int active; |
cef180ab |
236 | DialogRef dialog = GetDialogFromWindow(window); |
6cb61a05 |
237 | |
238 | active = (event->modifiers & activeFlag) != 0; |
cef180ab |
239 | GetDialogItem(dialog, wiSettingsOpen, &itemtype, &itemhandle, &itemrect); |
6cb61a05 |
240 | HiliteControl((ControlHandle)itemhandle, active ? 0 : 255); |
cef180ab |
241 | DialogSelect(event, &dialog, &item); |
6cb61a05 |
242 | } |
243 | |
244 | void mac_clickdlg(WindowPtr window, EventRecord *event) |
245 | { |
246 | short item; |
247 | Session *s = (Session *)GetWRefCon(window); |
cef180ab |
248 | DialogRef dialog = GetDialogFromWindow(window); |
6cb61a05 |
249 | |
cef180ab |
250 | if (DialogSelect(event, &dialog, &item)) |
6cb61a05 |
251 | switch (item) { |
252 | case wiSettingsOpen: |
cef180ab |
253 | HideWindow(window); |
6cb61a05 |
254 | mac_startsession(s); |
255 | break; |
256 | } |
257 | } |
258 | |
259 | /* |
260 | * Local Variables: |
261 | * c-file-style: "simon" |
262 | * End: |
263 | */ |