mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-07-01 11:32:48 -05:00
Add asynchronous callback capability to the askappend() alert box.
This was harder than verify_ssh_host_key() and askalg() put together, because: (a) askappend() can be called at any time, since it's a side effect of data-logging functions. Therefore there can be an unfinished askappend() alert at any time, and hence the OS X front end has to be prepared to _queue_ other alerts which occur during that time. (b) logging.c has to do something with data that comes in while it's waiting for an answer to askappend(). It buffers it until it knows what the user wants done with it. This involved something of a reorganisation of logging.c. [originally from svn r5344]
This commit is contained in:
@ -27,6 +27,13 @@ extern AppController *controller;
|
||||
* The SessionWindow class, defined in osxwin.m.
|
||||
*/
|
||||
|
||||
struct alert_queue {
|
||||
struct alert_queue *next;
|
||||
NSAlert *alert;
|
||||
void (*callback)(void *, int);
|
||||
void *ctx;
|
||||
};
|
||||
|
||||
@class SessionWindow;
|
||||
@class TerminalView;
|
||||
|
||||
@ -40,8 +47,14 @@ extern AppController *controller;
|
||||
void *ldisc;
|
||||
Backend *back;
|
||||
void *backhandle;
|
||||
/*
|
||||
* The following two members relate to the currently active
|
||||
* alert sheet, if any. They are NULL if there isn't one.
|
||||
*/
|
||||
void (*alert_callback)(void *, int);
|
||||
void *alert_ctx;
|
||||
/* This queues future alerts that need to be shown. */
|
||||
struct alert_queue *alert_qhead, *alert_qtail;
|
||||
}
|
||||
- (id)initWithConfig:(Config)cfg;
|
||||
- (void)drawStartFinish:(BOOL)start;
|
||||
|
@ -306,9 +306,48 @@
|
||||
* Various special-purpose dialog boxes.
|
||||
*/
|
||||
|
||||
int askappend(void *frontend, Filename filename)
|
||||
struct appendstate {
|
||||
void (*callback)(void *ctx, int result);
|
||||
void *ctx;
|
||||
};
|
||||
|
||||
static void askappend_callback(void *ctx, int result)
|
||||
{
|
||||
return 0; /* FIXME */
|
||||
struct appendstate *state = (struct appendstate *)ctx;
|
||||
|
||||
state->callback(state->ctx, (result == NSAlertFirstButtonReturn ? 2 :
|
||||
result == NSAlertSecondButtonReturn ? 1 : 0));
|
||||
sfree(state);
|
||||
}
|
||||
|
||||
int askappend(void *frontend, Filename filename,
|
||||
void (*callback)(void *ctx, int result), void *ctx)
|
||||
{
|
||||
static const char msgtemplate[] =
|
||||
"The session log file \"%s\" already exists. "
|
||||
"You can overwrite it with a new session log, "
|
||||
"append your session log to the end of it, "
|
||||
"or disable session logging for this session.";
|
||||
|
||||
char *text;
|
||||
SessionWindow *win = (SessionWindow *)frontend;
|
||||
struct appendstate *state;
|
||||
NSAlert *alert;
|
||||
|
||||
text = dupprintf(msgtemplate, filename.path);
|
||||
|
||||
state = snew(struct appendstate);
|
||||
state->callback = callback;
|
||||
state->ctx = ctx;
|
||||
|
||||
alert = [[NSAlert alloc] init];
|
||||
[alert setInformativeText:[NSString stringWithCString:text]];
|
||||
[alert addButtonWithTitle:@"Overwrite"];
|
||||
[alert addButtonWithTitle:@"Append"];
|
||||
[alert addButtonWithTitle:@"Disable"];
|
||||
[win startAlert:alert withCallback:askappend_callback andCtx:state];
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct algstate {
|
||||
|
@ -254,6 +254,7 @@
|
||||
if (realhost)
|
||||
sfree(realhost); /* FIXME: do something with this */
|
||||
}
|
||||
back->provide_logctx(backhandle, logctx);
|
||||
|
||||
/*
|
||||
* Create a line discipline. (This must be done after creating
|
||||
@ -784,11 +785,27 @@ printf("n=%d c=U+%04x cm=U+%04x m=%08x\n", n, c, cm, m);
|
||||
- (void)startAlert:(NSAlert *)alert
|
||||
withCallback:(void (*)(void *, int))callback andCtx:(void *)ctx
|
||||
{
|
||||
alert_callback = callback;
|
||||
alert_ctx = ctx; /* NB this is assumed to need freeing! */
|
||||
[alert beginSheetModalForWindow:self modalDelegate:self
|
||||
didEndSelector:@selector(alertSheetDidEnd:returnCode:contextInfo:)
|
||||
contextInfo:NULL];
|
||||
if (alert_ctx || alert_qhead) {
|
||||
/*
|
||||
* Queue this alert to be shown later.
|
||||
*/
|
||||
struct alert_queue *qitem = snew(struct alert_queue);
|
||||
qitem->next = NULL;
|
||||
qitem->alert = alert;
|
||||
qitem->callback = callback;
|
||||
qitem->ctx = ctx;
|
||||
if (alert_qtail)
|
||||
alert_qtail->next = qitem;
|
||||
else
|
||||
alert_qhead = qitem;
|
||||
alert_qtail = qitem;
|
||||
} else {
|
||||
alert_callback = callback;
|
||||
alert_ctx = ctx; /* NB this is assumed to need freeing! */
|
||||
[alert beginSheetModalForWindow:self modalDelegate:self
|
||||
didEndSelector:@selector(alertSheetDidEnd:returnCode:contextInfo:)
|
||||
contextInfo:NULL];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)alertSheetDidEnd:(NSAlert *)alert returnCode:(int)returnCode
|
||||
@ -803,19 +820,30 @@ printf("n=%d c=U+%04x cm=U+%04x m=%08x\n", n, c, cm, m);
|
||||
- (void)alertSheetDidFinishEnding:(id)object
|
||||
{
|
||||
int returnCode = [object intValue];
|
||||
void (*this_callback)(void *, int);
|
||||
void *this_ctx;
|
||||
|
||||
alert_callback(alert_ctx, returnCode); /* transfers ownership of ctx */
|
||||
|
||||
/*
|
||||
* We must save the values of our alert_callback and alert_ctx
|
||||
* fields, in case they are set up again by the callback
|
||||
* function!
|
||||
* If there's an alert in our queue (either already or because
|
||||
* the callback just queued it), start it.
|
||||
*/
|
||||
this_callback = alert_callback;
|
||||
this_ctx = alert_ctx;
|
||||
alert_ctx = NULL;
|
||||
if (alert_qhead) {
|
||||
struct alert_queue *qnext;
|
||||
|
||||
this_callback(this_ctx, returnCode); /* transfers ownership of ctx */
|
||||
alert_callback = alert_qhead->callback;
|
||||
alert_ctx = alert_qhead->ctx;
|
||||
[alert_qhead->alert beginSheetModalForWindow:self modalDelegate:self
|
||||
didEndSelector:@selector(alertSheetDidEnd:returnCode:contextInfo:)
|
||||
contextInfo:NULL];
|
||||
|
||||
qnext = alert_qhead->next;
|
||||
sfree(alert_qhead);
|
||||
alert_qhead = qnext;
|
||||
if (!qnext)
|
||||
alert_qtail = NULL;
|
||||
} else {
|
||||
alert_ctx = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
Reference in New Issue
Block a user