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