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