From: Kevin McCarthy Date: Fri, 11 Aug 2017 01:18:20 +0000 (-0700) Subject: Add helpers to add and remove actx entries. (see #3728) X-Git-Tag: neomutt-20170907~10^2~8 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1bdca239b7507ccc603c69bf88ffa924cee59e66;p=neomutt Add helpers to add and remove actx entries. (see #3728) Use the helper in compose update_idx(), to consolidate the resize logic and simplify the code. Separate out the actx "free" routine from a routine to empty out the idx. The index regeneration routines should flush and rebuild the index without having to renerate the actx structure. --- diff --git a/attach.c b/attach.c index 369643f56..660d39039 100644 --- a/attach.c +++ b/attach.c @@ -41,6 +41,7 @@ #include "mailbox.h" #include "mime.h" #include "mutt_curses.h" +#include "mutt_menu.h" #include "mx.h" #include "ncrypt/ncrypt.h" #include "options.h" @@ -1103,15 +1104,26 @@ bail0: } } -void mutt_free_attach_context(struct AttachCtx **pactx) +void mutt_actx_add_attach(struct AttachCtx *actx, struct AttachPtr *attach, struct Menu *menu) { int i; - struct AttachCtx *actx = NULL; - if (!pactx || !*pactx) - return; + if (actx->idxlen == actx->idxmax) + { + actx->idxmax += 5; + safe_realloc(&actx->idx, sizeof(struct AttachPtr *) * actx->idxmax); + for (i = actx->idxlen; i < actx->idxmax; i++) + actx->idx[i] = NULL; + if (menu) + menu->data = actx->idx; + } - actx = *pactx; + actx->idx[actx->idxlen++] = attach; +} + +void mutt_actx_free_entries(struct AttachCtx *actx) +{ + int i; for (i = 0; i < actx->idxlen; i++) { @@ -1120,7 +1132,19 @@ void mutt_free_attach_context(struct AttachCtx **pactx) FREE(&actx->idx[i]->tree); FREE(&actx->idx[i]); } - FREE(&actx->idx); + actx->idxlen = 0; +} + +void mutt_free_attach_context(struct AttachCtx **pactx) +{ + struct AttachCtx *actx = NULL; + + if (!pactx || !*pactx) + return; + + actx = *pactx; + mutt_actx_free_entries(actx); + FREE(&actx->idx); FREE(pactx); } diff --git a/attach.h b/attach.h index 2c7d6bbe9..ea8c1349e 100644 --- a/attach.h +++ b/attach.h @@ -71,6 +71,8 @@ void mutt_attach_resend(FILE *fp, struct Header *hdr, struct AttachCtx *actx, st void mutt_attach_forward(FILE *fp, struct Header *hdr, struct AttachCtx *actx, struct Body *cur, int flags); void mutt_attach_reply(FILE *fp, struct Header *hdr, struct AttachCtx *actx, struct Body *cur, int flags); +void mutt_actx_add_attach (struct AttachCtx *actx, struct AttachPtr *attach, struct Menu *menu); +void mutt_actx_free_entries (struct AttachCtx *actx); void mutt_free_attach_context(struct AttachCtx **pactx); #endif /* _MUTT_ATTACH_H */ diff --git a/compose.c b/compose.c index d4b7dbcd7..7533a48ab 100644 --- a/compose.c +++ b/compose.c @@ -520,14 +520,14 @@ static int delete_attachment(struct Menu *menu, short *idxlen, int x) return 0; } -static void update_idx(struct Menu *menu, struct AttachCtx *actx) +static void update_idx(struct Menu *menu, struct AttachCtx *actx, struct AttachPtr *new) { - actx->idx[actx->idxlen]->level = - (actx->idxlen > 0) ? actx->idx[actx->idxlen - 1]->level : 0; + new->level = (actx->idxlen > 0) ? actx->idx[actx->idxlen - 1]->level : 0; if (actx->idxlen) - actx->idx[actx->idxlen - 1]->content->next = actx->idx[actx->idxlen]->content; - actx->idx[actx->idxlen]->content->aptr = actx->idx[actx->idxlen]; - menu->current = actx->idxlen++; + actx->idx[actx->idxlen - 1]->content->next = new->content; + new->content->aptr = new; + mutt_actx_add_attach(actx, new, menu); + menu->current = actx->idxlen - 1; mutt_update_tree(actx); menu->max = actx->idxlen; } @@ -714,6 +714,7 @@ int mutt_compose_menu(struct Header *msg, /* structure for new message */ char fname[_POSIX_PATH_MAX]; struct Menu *menu = NULL; struct AttachCtx *actx = NULL; + struct AttachPtr *new = NULL; int i, close = 0; int r = -1; /* return value */ int op = 0; @@ -933,12 +934,7 @@ int mutt_compose_menu(struct Header *msg, /* structure for new message */ /* attachments may have been added */ if (actx->idxlen && actx->idx[actx->idxlen - 1]->content->next) { - for (i = 0; i < actx->idxlen; i++) - { - FREE(&actx->idx[i]->tree); - FREE(&actx->idx[i]); - } - actx->idxlen = 0; + mutt_actx_free_entries(actx); mutt_gen_attach_list(actx, msg->content, -1, 0, 1); menu->data = actx->idx; menu->max = actx->idxlen; @@ -951,20 +947,14 @@ int mutt_compose_menu(struct Header *msg, /* structure for new message */ case OP_COMPOSE_ATTACH_KEY: if (!(WithCrypto & APPLICATION_PGP)) break; - if (actx->idxlen == actx->idxmax) - { - safe_realloc(&actx->idx, sizeof(struct AttachPtr *) * (actx->idxmax += 5)); - menu->data = actx->idx; - } - - actx->idx[actx->idxlen] = safe_calloc(1, sizeof(struct AttachPtr)); - if ((actx->idx[actx->idxlen]->content = crypt_pgp_make_key_attachment(NULL)) != NULL) + new = safe_calloc(1, sizeof(struct AttachPtr)); + if ((new->content = crypt_pgp_make_key_attachment(NULL)) != NULL) { - update_idx(menu, actx); + update_idx(menu, actx, new); menu->redraw |= REDRAW_INDEX; } else - FREE(&actx->idx[actx->idxlen]); + FREE(&new); menu->redraw |= REDRAW_STATUS; @@ -986,28 +976,22 @@ int mutt_compose_menu(struct Header *msg, /* structure for new message */ *fname == '\0') break; - if (actx->idxlen + numfiles >= actx->idxmax) - { - safe_realloc(&actx->idx, sizeof(struct AttachPtr *) * (actx->idxmax += 5 + numfiles)); - menu->data = actx->idx; - } - error = 0; if (numfiles > 1) mutt_message(_("Attaching selected files...")); for (i = 0; i < numfiles; i++) { char *att = files[i]; - actx->idx[actx->idxlen] = safe_calloc(1, sizeof(struct AttachPtr)); - actx->idx[actx->idxlen]->unowned = true; - actx->idx[actx->idxlen]->content = mutt_make_file_attach(att); - if (actx->idx[actx->idxlen]->content != NULL) - update_idx(menu, actx); + new = (struct AttachPtr *) safe_calloc(1, sizeof(struct AttachPtr)); + new->unowned = 1; + new->content = mutt_make_file_attach(att); + if (new->content != NULL) + update_idx(menu, actx, new); else { error = 1; mutt_error(_("Unable to attach %s!"), att); - FREE(&actx->idx[actx->idxlen]); + FREE(&new); } FREE(&files[i]); } @@ -1116,26 +1100,19 @@ int mutt_compose_menu(struct Header *msg, /* structure for new message */ break; } - if (actx->idxlen + Context->tagged >= actx->idxmax) - { - safe_realloc(&actx->idx, - sizeof(struct AttachPtr *) * (actx->idxmax += 5 + Context->tagged)); - menu->data = actx->idx; - } - for (i = 0; i < Context->msgcount; i++) { h = Context->hdrs[i]; if (h->tagged) { - actx->idx[actx->idxlen] = safe_calloc(1, sizeof(struct AttachPtr)); - actx->idx[actx->idxlen]->content = mutt_make_message_attach(Context, h, 1); - if (actx->idx[actx->idxlen]->content != NULL) - update_idx(menu, actx); + new = (struct AttachPtr *) safe_calloc(1, sizeof(struct AttachPtr)); + new->content = mutt_make_message_attach(Context, h, 1); + if (new->content != NULL) + update_idx(menu, actx, new); else { mutt_error(_("Unable to attach!")); - FREE(&actx->idx[actx->idxlen]); + FREE(&new); } } } @@ -1419,28 +1396,23 @@ int mutt_compose_menu(struct Header *msg, /* structure for new message */ mutt_error(_("Unknown Content-Type %s"), type); continue; } - if (actx->idxlen == actx->idxmax) - { - safe_realloc(&actx->idx, sizeof(struct AttachPtr *) * (actx->idxmax += 5)); - menu->data = actx->idx; - } - - actx->idx[actx->idxlen] = safe_calloc(1, sizeof(struct AttachPtr)); + new = (struct AttachPtr *) safe_calloc(1, sizeof(struct AttachPtr)); /* Touch the file */ if (!(fp = safe_fopen(fname, "w"))) { mutt_error(_("Can't create file %s"), fname); - FREE(&actx->idx[actx->idxlen]); + FREE(&new); continue; } safe_fclose(&fp); - if ((actx->idx[actx->idxlen]->content = mutt_make_file_attach(fname)) == NULL) + if ((new->content = mutt_make_file_attach(fname)) == NULL) { mutt_error(_("What we have here is a failure to make an attachment")); + FREE(&new); continue; } - update_idx(menu, actx); + update_idx(menu, actx, new); actx->idx[menu->current]->content->type = itype; mutt_str_replace(&actx->idx[menu->current]->content->subtype, p); diff --git a/recvattach.c b/recvattach.c index a6806dd55..97ea54adc 100644 --- a/recvattach.c +++ b/recvattach.c @@ -116,13 +116,6 @@ void mutt_gen_attach_list(struct AttachCtx *actx, struct Body *m, int parent_typ for (; m; m = m->next) { - if (actx->idxlen == actx->idxmax) - { - safe_realloc(&actx->idx, sizeof(struct AttachPtr *) * (actx->idxmax += 5)); - for (int i = actx->idxlen; i < actx->idxmax; i++) - actx->idx[i] = NULL; - } - if (m->type == TYPEMULTIPART && m->parts && (compose || (parent_type == -1 && (mutt_strcasecmp("alternative", m->subtype) != 0))) && (!(WithCrypto & APPLICATION_PGP) || !mutt_is_multipart_encrypted(m))) @@ -131,10 +124,8 @@ void mutt_gen_attach_list(struct AttachCtx *actx, struct Body *m, int parent_typ } else { - if (!actx->idx[actx->idxlen]) - actx->idx[actx->idxlen] = safe_calloc(1, sizeof(struct AttachPtr)); - - new = actx->idx[actx->idxlen++]; + new = safe_calloc(1, sizeof(struct AttachPtr)); + mutt_actx_add_attach(actx, new, NULL); new->content = m; m->aptr = new; new->parent_type = parent_type; @@ -819,10 +810,7 @@ void mutt_print_attachment_list(FILE *fp, int tag, struct Body *top) static void update_attach_index(struct AttachCtx *actx, struct Body *cur, struct Menu *menu) { - while (--(actx->idxlen) >= 0) - actx->idx[actx->idxlen]->content = NULL; - actx->idxlen = 0; - + mutt_actx_free_entries(actx); mutt_gen_attach_list(actx, cur, -1, 0, 0); menu->max = actx->idxlen;