summaryrefslogtreecommitdiff
path: root/alpine/flagmaint.c
blob: 423e580addcae6feb2111808c5ab33a58a43fda4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#if !defined(lint) && !defined(DOS)
static char rcsid[] = "$Id: flagmaint.c 807 2007-11-09 01:21:33Z hubert@u.washington.edu $";
#endif

/*
 * ========================================================================
 * Copyright 2006-2007 University of Washington
 * Copyright 2013-2019 Eduardo Chappa
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * ========================================================================
 */

#include "headers.h"
#include "flagmaint.h"
#include "confscroll.h"
#include "../pith/state.h"
#include "../pith/flag.h"
#include "../pith/status.h"
#include "../pith/mailcmd.h"
#include "../pith/icache.h"


#define FLAG_ADD_RETURN 15


/*
 * Internal prototypes
 */
int	 flag_checkbox_tool(struct pine *, int, CONF_S **, unsigned);


/*----------------------------------------------------------------------
   Function to control flag set/clearing

   Basically, turn the flags into a fake list of features...

   Returns 0 unless user has added a keyword, then 1.

 ----*/
int
flag_maintenance_screen(struct pine *ps, struct flag_screen *flags)
{
    int		  i, lv, lc, maxwidth, offset, need, rv = 0;
    char	  tmp[1200], **p, *spacer;
    CONF_S	 *ctmpa, *first_line;
    struct	  flag_table  *fp;
    OPT_SCREEN_S  screen;

try_again:
    maxwidth = MAX(MIN((ps->ttyo ? ps->ttyo->screen_cols : 80), 150), 30);
    first_line = NULL;
    ctmpa = NULL;

    for(p = flags->explanation; p && *p; p++){
	new_confline(&ctmpa);
	ctmpa->keymenu   = &flag_keymenu;
	ctmpa->help      = NO_HELP;
	ctmpa->tool      = flag_checkbox_tool;
	ctmpa->flags    |= CF_NOSELECT;
	ctmpa->valoffset = 0;
	ctmpa->value     = cpystr(_(*p));
    }

    /* Now wire flags checkboxes together */
    for(lv = 0, lc = 0, fp = (flags->flag_table ? *flags->flag_table : NULL);
	fp && fp->name; fp++){	/* longest name */
	if(fp->flag != F_COMMENT){
	    if(lv < (i = utf8_width(_(fp->name))))
	      lv = i;
	    if(fp->comment && lc < (i = utf8_width(fp->comment)))
	      lc = i;
	}
    }
    
    lv = MIN(lv,100);
    lc = MIN(lc,100);
    if(lc > 0)
      spacer = "    ";
    else
      spacer = "";

    offset = 6;
    if((need = offset + 5 + lv + strlen(spacer) + lc) > maxwidth){
	offset -= (need - maxwidth);
	offset = MAX(0,offset);
	if((need = offset + 5 + lv + strlen(spacer) + lc) > maxwidth){
	    spacer = " ";
	    if((need = offset + 5 + lv + strlen(spacer) + lc) > maxwidth){
		lc -= (need - maxwidth);
		lc = MAX(0,lc);
		if(lc == 0)
		  spacer = "";
	    }
	}
    }

    new_confline(&ctmpa);
    ctmpa->keymenu   = &flag_keymenu;
    ctmpa->help      = NO_HELP;
    ctmpa->tool      = flag_checkbox_tool;
    ctmpa->flags    |= CF_NOSELECT;
    ctmpa->valoffset = 0;
    ctmpa->value     = cpystr("");

    new_confline(&ctmpa);
    ctmpa->keymenu   = &flag_keymenu;
    ctmpa->help      = NO_HELP;
    ctmpa->tool      = flag_checkbox_tool;
    ctmpa->flags    |= CF_NOSELECT;
    ctmpa->valoffset = 0;
    utf8_snprintf(tmp, sizeof(tmp), "%*.*w  %s", offset+3, offset+3, _("Set"), _("Flag/Keyword Name"));
    tmp[sizeof(tmp)-1] = '\0';
    ctmpa->value = cpystr(tmp);

    new_confline(&ctmpa);
    ctmpa->keymenu   = &flag_keymenu;
    ctmpa->help      = NO_HELP;
    ctmpa->tool      = flag_checkbox_tool;
    ctmpa->flags    |= CF_NOSELECT;
    ctmpa->valoffset = 0;
    snprintf(tmp, sizeof(tmp), "%*.*s---  %.*s",
	    offset, offset, "",
	    (int)(lv+lc+strlen(spacer)), repeat_char(lv+lc+strlen(spacer), '-'));
    tmp[sizeof(tmp)-1] = '\0';
    ctmpa->value = cpystr(tmp);

    for(fp = (flags->flag_table ? *flags->flag_table : NULL);
	fp && fp->name; fp++){	/* build the list */
	new_confline(&ctmpa);
	if(!first_line && (fp->flag != F_COMMENT))
	  first_line = ctmpa;

	ctmpa->keymenu   = &flag_keymenu;
	ctmpa->tool      = flag_checkbox_tool;
	ctmpa->valoffset = offset;

	if(fp->flag == F_COMMENT){
	    ctmpa->help   = NO_HELP;
	    ctmpa->flags |= CF_NOSELECT;
	    ctmpa->value  = cpystr(fp->name);
	}
	else{
	    ctmpa->help		  = fp->help;
	    ctmpa->d.f.ftbl	  = flags->flag_table;
	    ctmpa->d.f.fp	  = fp;

	    utf8_snprintf(tmp, sizeof(tmp), "[%c]  %-*.*w%s%-*.*w",
		    (fp->set == 0) ? ' ' : (fp->set == 1) ? 'X' : '?',
		    lv, lv, _(fp->name),
		    spacer, lc, lc, fp->comment ? fp->comment : "");
	    ctmpa->value = cpystr(tmp);
	}
    }
      
    memset(&screen, 0, sizeof(screen));
    /*
     * TRANSLATORS: FLAG MAINTENANCE is a screen title.
     * Print something1 using something2.  configuration is something1
     */
    if(conf_scroll_screen(ps, &screen, first_line,
			  _("FLAG MAINTENANCE"),
			  _("configuration"), 0, NULL) == FLAG_ADD_RETURN){
	int flags, r;
	char keyword[500];
	char nickname[500];
	char prompt[500];
	char *error = NULL;
	KEYWORD_S *kw;
	HelpType help;

	/*
	 * User is asking to add a new keyword. We will add it to the
	 * mailbox if necessary and add it to the keywords list from
	 * Setup/Config. Then we will modify the flag_table and present
	 * the flag modification screen again.
	 */

	ps->mangled_screen = 1;
	keyword[0] = '\0';
	flags = OE_APPEND_CURRENT;
	help = NO_HELP;

	do{
	    if(error){
		q_status_message(SM_ORDER, 3, 4, error);
		fs_give((void **) &error);
	    }

	    strncpy(prompt, _("Keyword to be added : "), sizeof(prompt)-1);
	    prompt[sizeof(prompt)-1] = '\0';
	    r = optionally_enter(keyword, -FOOTER_ROWS(ps_global), 0,
				 sizeof(keyword), prompt, NULL, help, &flags);

	    if(r == 3)
	      help = help == NO_HELP ? h_type_keyword : NO_HELP;
	    else if(r == 1){
		cmd_cancelled("Add Keyword");
		goto try_again;
	    }

	    removing_leading_and_trailing_white_space(keyword);

	}while(r == 3 || keyword_check(keyword, &error));

	for(kw = ps->keywords; kw; kw = kw->next){
	    if(kw->kw && !strucmp(kw->kw, keyword)){
		q_status_message(SM_ORDER, 3, 4, _("Keyword already configured, changing nickname"));
		break;
	    }
	}

	snprintf(prompt, sizeof(prompt), _("Optional nickname for \"%s\" : "), keyword);

	nickname[0] = '\0';
	help = NO_HELP;

	do{
	    r = optionally_enter(nickname, -FOOTER_ROWS(ps_global), 0,
				 sizeof(nickname), prompt, NULL, help, &flags);

	    if(r == 3)
	      help = help == NO_HELP ? h_type_keyword_nickname : NO_HELP;
	    else if(r == 1){
		cmd_cancelled("Add Keyword");
		goto try_again;
	    }

	    removing_leading_and_trailing_white_space(nickname);

	}while(r == 3);

	if(keyword[0]){
	    char ***alval;
	    int offset = -1;
	    struct variable *var;

	    var = &ps_global->vars[V_KEYWORDS];
	    alval = ALVAL(var, Main);

	    for(kw = ps->keywords; kw; kw = kw->next){
		offset++;
		if(kw->kw && !strucmp(kw->kw, keyword)){
		    /* looks like it should already exist at offset */
		    break;
		}
	    }

	    if(!kw)
	      offset = -1;

	    if(offset >= 0 && (*alval) && (*alval)[offset]){
		fs_give((void **) &(*alval)[offset]);
		(*alval)[offset] = put_pair(nickname, keyword);
	    }
	    else if(!*alval){
		offset = 0;
		*alval = (char **) fs_get(2*sizeof(char *));
		(*alval)[offset] = put_pair(nickname, keyword);
		(*alval)[offset+1] = NULL;
	    }
	    else{
		for(offset=0; (*alval)[offset]; offset++)
		  ;

		fs_resize((void **) alval, (offset + 2) * sizeof(char *));
		(*alval)[offset] = put_pair(nickname, keyword);
		(*alval)[offset+1] = NULL;
	    }

	    set_current_val(var, TRUE, FALSE);
	    if(ps_global->prc)
	      ps_global->prc->outstanding_pinerc_changes = 1;

	    if(ps_global->keywords)
	      free_keyword_list(&ps_global->keywords);

	    if(var->current_val.l && var->current_val.l[0])
	      ps_global->keywords = init_keyword_list(var->current_val.l);

	    clear_index_cache(ps_global->mail_stream, 0);

	    rv = 1;
	}
    }

    ps->mangled_screen = 1;

    return(rv);
}


/*
 * Message flag manipulation tool
 * 
 * 
 * returns:  -1 on unrecognized cmd, 0 if no change, 1 if change
 */
int
flag_checkbox_tool(struct pine *ps, int cmd, CONF_S **cl, unsigned int flags)
{
    int rv = 0, state;

    switch(cmd){
      case MC_TOGGLE:				/* mark/unmark feature */
	state = (*cl)->d.f.fp->set;
	state = (state == 1) ? 0 : (!state && ((*cl)->d.f.fp->ukn)) ? 2 : 1;
	(*cl)->value[1] = (state == 0) ? ' ' : ((state == 1) ? 'X': '?');
	(*cl)->d.f.fp->set = state;
	rv = 1;
	break;

      case MC_ADD:
	rv = FLAG_ADD_RETURN;
	break;

      case MC_EXIT:				/* exit */
	rv = simple_exit_cmd(flags);
	break;

      default :
	rv = -1;
	break;
    }

    return(rv);
}