2 * osxdlg.m: various PuTTY dialog boxes for OS X.
5 #import <Cocoa/Cocoa.h>
12 * The `ConfigWindow' class is used to start up a new PuTTY
17 @interface ConfigTree : NSObject
23 - (void)addPath:(char *)path;
26 @implementation ConfigTree
32 nitems = itemsize = 0;
35 - (void)addPath:(char *)path
37 if (nitems >= itemsize) {
39 paths = sresize(paths, itemsize, NSString *);
40 levels = sresize(levels, itemsize, int);
42 paths[nitems] = [[NSString stringWithCString:path] retain];
43 levels[nitems] = ctrl_path_elements(path) - 1;
50 for (i = 0; i < nitems; i++)
58 - (id)iterateChildren:(int)index ofItem:(id)item count:(int *)count
63 for (i = 0; i < nitems; i++)
78 if (i >= nitems || levels[i] != plevel+1)
84 } while (i < nitems && levels[i] > plevel+1);
90 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
92 return [self iterateChildren:index ofItem:item count:NULL];
94 - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
97 /* pass nitems+1 to ensure we run off the end */
98 [self iterateChildren:nitems+1 ofItem:item count:&count];
101 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
103 return [self outlineView:outlineView numberOfChildrenOfItem:item] > 0;
105 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
108 * Trim off all path elements except the last one.
110 NSArray *components = [item componentsSeparatedByString:@"/"];
111 return [components objectAtIndex:[components count]-1];
115 @implementation ConfigWindow
116 - (id)initWithConfig:(Config)aCfg
118 NSScrollView *scrollview;
120 ConfigTree *treedata;
126 ctrlbox = ctrl_new_box();
127 setup_config_box(ctrlbox, FALSE /*midsession*/, aCfg.protocol,
128 0 /* protcfginfo */);
129 unix_setup_config_box(ctrlbox, FALSE /*midsession*/, aCfg.protocol);
131 cfg = aCfg; /* structure copy */
133 self = [super initWithContentRect:NSMakeRect(0,0,300,300)
134 styleMask:(NSTitledWindowMask | NSMiniaturizableWindowMask |
135 NSClosableWindowMask)
136 backing:NSBackingStoreBuffered
138 [self setTitle:@"PuTTY Configuration"];
140 [self setIgnoresMouseEvents:NO];
142 dv = fe_dlg_init(&cfg, self, self, @selector(configBoxFinished:));
144 scrollview = [[NSScrollView alloc] initWithFrame:NSMakeRect(20,20,10,10)];
145 treeview = [[NSOutlineView alloc] initWithFrame:[scrollview frame]];
146 [scrollview setBorderType:NSLineBorder];
147 [scrollview setDocumentView:treeview];
148 [[self contentView] addSubview:scrollview];
149 [scrollview setHasVerticalScroller:YES];
150 [scrollview setAutohidesScrollers:YES];
151 /* FIXME: the below is untested. Test it then remove this notice. */
152 [treeview setAllowsColumnReordering:NO];
153 [treeview setAllowsColumnResizing:NO];
154 [treeview setAllowsMultipleSelection:NO];
155 [treeview setAllowsEmptySelection:NO];
156 [treeview setAllowsColumnSelection:YES];
158 treedata = [[[ConfigTree alloc] init] retain];
160 col = [[NSTableColumn alloc] initWithIdentifier:nil];
161 [treeview addTableColumn:col];
162 [treeview setOutlineTableColumn:col];
164 [[treeview headerView] setFrame:NSMakeRect(0,0,0,0)];
167 * Create the controls.
173 for (i = 0; i < ctrlbox->nctrlsets; i++) {
174 struct controlset *s = ctrlbox->ctrlsets[i];
179 create_ctrls(dv, [self contentView], s, &mw, &mh);
186 int j = path ? ctrl_path_compare(s->pathname, path) : 0;
188 if (j != INT_MAX) { /* add to treeview, start new panel */
192 * We expect never to find an implicit path
193 * component. For example, we expect never to
194 * see A/B/C followed by A/D/E, because that
195 * would _implicitly_ create A/D. All our path
196 * prefixes are expected to contain actual
197 * controls and be selectable in the treeview;
198 * so we would expect to see A/D _explicitly_
199 * before encountering A/D/E.
201 assert(j == ctrl_path_elements(s->pathname) - 1);
203 c = strrchr(s->pathname, '/');
209 [treedata addPath:s->pathname];
215 create_ctrls(dv, [self contentView], s, &mw, &mh);
216 if (wmin < mw + 3*20+150)
217 wmin = mw + 3*20+150;
219 if (hmin < panelht - 20)
229 [treeview setDataSource:treedata];
230 for (i = [treeview numberOfRows]; i-- ;)
231 [treeview expandItem:[treeview itemAtRow:i] expandChildren:YES];
233 [treeview sizeToFit];
234 r = [treeview frame];
235 if (hmin < r.size.height)
236 hmin = r.size.height;
239 [self setContentSize:NSMakeSize(wmin, hmin+60+by)];
240 [scrollview setFrame:NSMakeRect(20, 40+by, 150, hmin)];
241 [treeview setDelegate:self];
245 * Now place the controls.
252 for (i = 0; i < ctrlbox->nctrlsets; i++) {
253 struct controlset *s = ctrlbox->ctrlsets[i];
256 by -= VSPACING + place_ctrls(dv, s, 20, by, wmin-40);
258 if (!path || strcmp(s->pathname, path))
261 panelht += VSPACING + place_ctrls(dv, s, 2*20+150,
270 select_panel(dv, ctrlbox, [[treeview itemAtRow:0] cString]);
272 [treeview reloadData];
274 dlg_refresh(NULL, dv);
276 [self center]; /* :-) */
280 - (void)configBoxFinished:(id)object
282 int ret = [object intValue]; /* it'll be an NSNumber */
284 [controller performSelectorOnMainThread:
285 @selector(newSessionWithConfig:)
286 withObject:[NSData dataWithBytes:&cfg length:sizeof(cfg)]
291 - (void)outlineViewSelectionDidChange:(NSNotification *)notification
293 const char *path = [[treeview itemAtRow:[treeview selectedRow]] cString];
294 select_panel(dv, ctrlbox, path);
296 - (BOOL)outlineView:(NSOutlineView *)outlineView
297 shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
299 return NO; /* no editing! */
303 /* ----------------------------------------------------------------------
304 * Various special-purpose dialog boxes.
308 void (*callback)(void *ctx, int result);
312 static void askappend_callback(void *ctx, int result)
314 struct appendstate *state = (struct appendstate *)ctx;
316 state->callback(state->ctx, (result == NSAlertFirstButtonReturn ? 2 :
317 result == NSAlertSecondButtonReturn ? 1 : 0));
321 int askappend(void *frontend, Filename filename,
322 void (*callback)(void *ctx, int result), void *ctx)
324 static const char msgtemplate[] =
325 "The session log file \"%s\" already exists. "
326 "You can overwrite it with a new session log, "
327 "append your session log to the end of it, "
328 "or disable session logging for this session.";
331 SessionWindow *win = (SessionWindow *)frontend;
332 struct appendstate *state;
335 text = dupprintf(msgtemplate, filename.path);
337 state = snew(struct appendstate);
338 state->callback = callback;
341 alert = [[NSAlert alloc] init];
342 [alert setInformativeText:[NSString stringWithCString:text]];
343 [alert addButtonWithTitle:@"Overwrite"];
344 [alert addButtonWithTitle:@"Append"];
345 [alert addButtonWithTitle:@"Disable"];
346 [win startAlert:alert withCallback:askappend_callback andCtx:state];
352 void (*callback)(void *ctx, int result);
356 static void askalg_callback(void *ctx, int result)
358 struct algstate *state = (struct algstate *)ctx;
360 state->callback(state->ctx, result == NSAlertFirstButtonReturn);
364 int askalg(void *frontend, const char *algtype, const char *algname,
365 void (*callback)(void *ctx, int result), void *ctx)
367 static const char msg[] =
368 "The first %s supported by the server is "
369 "%s, which is below the configured warning threshold.\n"
370 "Continue with connection?";
373 SessionWindow *win = (SessionWindow *)frontend;
374 struct algstate *state;
377 text = dupprintf(msg, algtype, algname);
379 state = snew(struct algstate);
380 state->callback = callback;
383 alert = [[NSAlert alloc] init];
384 [alert setInformativeText:[NSString stringWithCString:text]];
385 [alert addButtonWithTitle:@"Yes"];
386 [alert addButtonWithTitle:@"No"];
387 [win startAlert:alert withCallback:askalg_callback andCtx:state];
392 struct hostkeystate {
393 char *host, *keytype, *keystr;
395 void (*callback)(void *ctx, int result);
399 static void verify_ssh_host_key_callback(void *ctx, int result)
401 struct hostkeystate *state = (struct hostkeystate *)ctx;
403 if (result == NSAlertThirdButtonReturn) /* `Accept' */
404 store_host_key(state->host, state->port,
405 state->keytype, state->keystr);
406 state->callback(state->ctx, result != NSAlertFirstButtonReturn);
408 sfree(state->keytype);
409 sfree(state->keystr);
413 int verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
414 char *keystr, char *fingerprint,
415 void (*callback)(void *ctx, int result), void *ctx)
417 static const char absenttxt[] =
418 "The server's host key is not cached. You have no guarantee "
419 "that the server is the computer you think it is.\n"
420 "The server's %s key fingerprint is:\n"
422 "If you trust this host, press \"Accept\" to add the key to "
423 "PuTTY's cache and carry on connecting.\n"
424 "If you want to carry on connecting just once, without "
425 "adding the key to the cache, press \"Connect Once\".\n"
426 "If you do not trust this host, press \"Cancel\" to abandon the "
428 static const char wrongtxt[] =
429 "WARNING - POTENTIAL SECURITY BREACH!\n"
430 "The server's host key does not match the one PuTTY has "
431 "cached. This means that either the server administrator "
432 "has changed the host key, or you have actually connected "
433 "to another computer pretending to be the server.\n"
434 "The new %s key fingerprint is:\n"
436 "If you were expecting this change and trust the new key, "
437 "press \"Accept\" to update PuTTY's cache and continue connecting.\n"
438 "If you want to carry on connecting but without updating "
439 "the cache, press \"Connect Once\".\n"
440 "If you want to abandon the connection completely, press "
441 "\"Cancel\" to cancel. Pressing \"Cancel\" is the ONLY guaranteed "
446 SessionWindow *win = (SessionWindow *)frontend;
447 struct hostkeystate *state;
453 ret = verify_host_key(host, port, keytype, keystr);
458 text = dupprintf((ret == 2 ? wrongtxt : absenttxt), keytype, fingerprint);
460 state = snew(struct hostkeystate);
461 state->callback = callback;
463 state->host = dupstr(host);
465 state->keytype = dupstr(keytype);
466 state->keystr = dupstr(keystr);
468 alert = [[NSAlert alloc] init];
469 [alert setInformativeText:[NSString stringWithCString:text]];
470 [alert addButtonWithTitle:@"Cancel"];
471 [alert addButtonWithTitle:@"Connect Once"];
472 [alert addButtonWithTitle:@"Accept"];
473 [win startAlert:alert withCallback:verify_ssh_host_key_callback
479 void old_keyfile_warning(void)
482 * This should never happen on OS X. We hope.
486 static void connection_fatal_callback(void *ctx, int result)
488 SessionWindow *win = (SessionWindow *)ctx;
490 [win endSession:FALSE];
493 void connection_fatal(void *frontend, char *p, ...)
495 SessionWindow *win = (SessionWindow *)frontend;
501 msg = dupvprintf(p, ap);
504 alert = [[NSAlert alloc] init];
505 [alert setInformativeText:[NSString stringWithCString:msg]];
506 [alert addButtonWithTitle:@"Proceed"];
507 [win startAlert:alert withCallback:connection_fatal_callback