summaryrefslogtreecommitdiff
path: root/pith/icache.c
blob: a1c3c72a2e02d1dd313f8e99c73219f6c22bd879 (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#if !defined(lint) && !defined(DOS)
static char rcsid[] = "$Id: icache.c 874 2007-12-15 02:51:06Z hubert@u.washington.edu $";
#endif

/*
 * ========================================================================
 * Copyright 2013-2017 Eduardo Chappa
 * Copyright 2006-2007 University of Washington
 *
 * 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 "../pith/headers.h"
#include "../pith/icache.h"
#include "../pith/mailindx.h"
#include "../pith/flag.h"
#include "../pith/msgno.h"
#include "../pith/status.h"
#include "../pith/pineelt.h"

/*
 * Internal prototypes
 */


/*
 *           * * *  Index entry cache manager  * * *
 */


/*
 * Erase a particular entry in the cache.
 */
void
clear_index_cache_ent(MAILSTREAM *stream, long int msgno, unsigned int flags)
{
    long          rawno = -1L;
    PINELT_S    **peltp;
    MESSAGECACHE *mc;

    if(stream){
	if(flags & IC_USE_RAW_MSGNO)
	  rawno = msgno;
	else
	  rawno = mn_m2raw(sp_msgmap(stream), msgno);

	if(rawno > 0L && rawno <= stream->nmsgs){
	    mc = mail_elt(stream, rawno);
	    if(mc && mc->sparep){
		peltp = (PINELT_S **) &mc->sparep;
		if((*peltp)->ice){
		    /*
		     * This is intended to be a lightweight reset of
		     * just the widths and print_format strings. For example,
		     * the width of the screen changed and nothing else.
		     * We simply unset the widths_done bit and it
		     * is up to the drawer to free and recalculate the
		     * print_format strings and to reset the widths.
		     *
		     * The else case is a clear of the entire cache entry
		     * leaving behind only the empty structure.
		     */
		    if(flags & IC_CLEAR_WIDTHS_DONE){
			(*peltp)->ice->widths_done = 0;

			/* also zero out hash value */
			(*peltp)->ice->id = 0;

			if((*peltp)->ice->tice){
			    (*peltp)->ice->tice->widths_done = 0;

			    /* also zero out hash value */
			    (*peltp)->ice->tice->id = 0;
			}
		    }
		    else
		      clear_ice(&(*peltp)->ice);
		}
	    }
	}
    }
}


void
clear_index_cache(MAILSTREAM *stream, unsigned int flags)
{
    long rawno;

    if(stream){
	set_need_format_setup(stream);
	for(rawno = 1L; rawno <= stream->nmsgs; rawno++)
	  clear_index_cache_ent(stream, rawno, flags | IC_USE_RAW_MSGNO);
    }
}


void
clear_index_cache_for_thread(MAILSTREAM *stream, PINETHRD_S *thrd, MSGNO_S *msgmap)
{
    unsigned long msgno;

    if(!thrd || !stream || thrd->rawno < 1L || thrd->rawno > stream->nmsgs)
      return;

    msgno = mn_raw2m(msgmap, thrd->rawno);

    clear_index_cache_ent(stream, msgno, 0);

    if(thrd->next)
      clear_index_cache_for_thread(stream, fetch_thread(stream, thrd->next),
				   msgmap);

    if(thrd->branch)
      clear_index_cache_for_thread(stream, fetch_thread(stream, thrd->branch),
				   msgmap);
}


void
clear_icache_flags(MAILSTREAM *stream)
{
    sp_set_icache_flags(stream, 0);
}


void
set_need_format_setup(MAILSTREAM *stream)
{
    sp_set_icache_flags(stream, sp_icache_flags(stream) | SP_NEED_FORMAT_SETUP);
}


int
need_format_setup(MAILSTREAM *stream)
{
    return(sp_icache_flags(stream) & SP_NEED_FORMAT_SETUP);
}


void
set_format_includes_msgno(MAILSTREAM *stream)
{
    sp_set_icache_flags(stream, sp_icache_flags(stream) | SP_FORMAT_INCLUDES_MSGNO);
}


int
format_includes_msgno(MAILSTREAM *stream)
{
    return(sp_icache_flags(stream) & SP_FORMAT_INCLUDES_MSGNO);
}


void
set_format_includes_smartdate(MAILSTREAM *stream)
{
    sp_set_icache_flags(stream, sp_icache_flags(stream) | SP_FORMAT_INCLUDES_SMARTDATE);
}


int
format_includes_smartdate(MAILSTREAM *stream)
{
    return(sp_icache_flags(stream) & SP_FORMAT_INCLUDES_SMARTDATE);
}


/*
 * Almost a free_ice, but we leave the memory there for the ICE_S.
 */
void
clear_ice(ICE_S **ice)
{
    if(ice && *ice){
	free_ifield(&(*ice)->ifield);

	if((*ice)->linecolor)
	  free_color_pair(&(*ice)->linecolor);

	if((*ice)->tice)
	  clear_ice(&(*ice)->tice);

	/* do these one at a time so we don't clear tice */
	(*ice)->color_lookup_done = 0;
	(*ice)->to_us = 0;
	(*ice)->cc_us = 0;
	(*ice)->plus = 0;
	(*ice)->id = 0;
    }
}


void
free_ice(ICE_S **ice)
{
    if(ice && *ice){

	if((*ice)->tice)
	  free_ice(&(*ice)->tice);

	clear_ice(ice);

	fs_give((void **) ice);
    }
}


void
free_ifield(IFIELD_S **ifld)
{
    if(ifld && *ifld){
	free_ifield(&(*ifld)->next);
	free_ielem(&(*ifld)->ielem);
	fs_give((void **) ifld);
    }
}


void
free_ielem(IELEM_S **il)
{
    if(il && *il){
	free_ielem(&(*il)->next);
	if((*il)->freeprintf && (*il)->print_format)
	  fs_give((void **) &(*il)->print_format);

	if((*il)->freecolor && (*il)->color)
	  free_color_pair(&(*il)->color);

	if((*il)->freedata && (*il)->data)
	  fs_give((void **) &(*il)->data);
	
	fs_give((void **) il);
    }
}


/*
 * Returns the index cache entry associated with this message.
 * If it doesn't already exist it is instantiated.
 */
ICE_S *
fetch_ice(MAILSTREAM *stream, long unsigned int rawno)
{
    PINELT_S    **peltp;
    MESSAGECACHE *mc;

    if(!stream || rawno < 1L || rawno > stream->nmsgs)
      return NULL;

    if(!(mc = mail_elt(stream, rawno)))
      return NULL;

    /*
     * any private elt data yet?
     */
    if(*(peltp = (PINELT_S **) &mc->sparep) == NULL){
	*peltp = (PINELT_S *) fs_get(sizeof(PINELT_S));
	memset(*peltp, 0, sizeof(PINELT_S));
    }

    if((*peltp)->ice == NULL)
      (*peltp)->ice = new_ice();

    if(need_format_setup(stream) && setup_header_widths)
      (*setup_header_widths)(stream);

    return((*peltp)->ice);
}


ICE_S **
fetch_ice_ptr(MAILSTREAM *stream, long unsigned int rawno)
{
    PINELT_S    **peltp;
    MESSAGECACHE *mc;

    if(!stream || rawno < 1L || rawno > stream->nmsgs)
      return NULL;

    if(!(mc = mail_elt(stream, rawno)))
      return NULL;

    /*
     * any private elt data yet?
     */
    if(*(peltp = (PINELT_S **) &mc->sparep) == NULL){
	*peltp = (PINELT_S *) fs_get(sizeof(PINELT_S));
	memset(*peltp, 0, sizeof(PINELT_S));
    }

    return(&(*peltp)->ice);
}


ICE_S *
copy_ice(ICE_S *src)
{
    ICE_S *head = NULL;

    if(src){
	head                    = new_ice();

	head->color_lookup_done = src->color_lookup_done;
	head->widths_done       = src->widths_done;
	head->to_us             = src->to_us;
	head->cc_us             = src->cc_us;
	head->plus              = src->plus;
	head->id                = src->id;

	if(src->linecolor)
	  head->linecolor = new_color_pair(src->linecolor->fg, src->linecolor->bg);

	if(src->ifield)
	  head->ifield = copy_ifield(src->ifield);

	if(src->tice)
	  head->tice = copy_ice(src->tice);
    }

    return(head);
}


IFIELD_S *
copy_ifield(IFIELD_S *src)
{
    IFIELD_S *head = NULL;

    if(src){
	head          = new_ifield(NULL);

	if(src->next)
	  head->next = copy_ifield(src->next);

	head->ctype   = src->ctype;
	head->width   = src->width;
	head->leftadj = src->leftadj;

	if(src->ielem)
	  head->ielem = copy_ielem(src->ielem);
    }

    return(head);
}


IELEM_S *
copy_ielem(IELEM_S *src)
{
    IELEM_S *head = NULL;

    if(src){
	head          = new_ielem(NULL);

	if(src->next)
	  head->next = copy_ielem(src->next);

	head->type = src->type;
	head->wid  = src->wid;

	if(src->color){
	    head->color     = new_color_pair(src->color->fg, src->color->bg);
	    head->freecolor = 1;
	}

	if(src->data){
	    head->data     = cpystr(src->data);
	    head->datalen  = strlen(head->data);
	    head->freedata = 1;
	}

	if(src->print_format){
	    head->print_format = cpystr(src->print_format);
	    head->freeprintf   = strlen(head->print_format) + 1;
	}
    }

    return(head);
}


ICE_S *
new_ice(void)
{
    ICE_S *ice;

    ice = (ICE_S *) fs_get(sizeof(ICE_S));
    memset(ice, 0, sizeof(ICE_S));
    return(ice);
}


/*
 * Create new IFIELD_S, zero it out, and insert it at end.
 */
IFIELD_S *
new_ifield(IFIELD_S **ifieldp)
{
    IFIELD_S *ifield, *ip;

    ifield = (IFIELD_S *) fs_get(sizeof(*ifield));
    memset(ifield, 0, sizeof(*ifield));

    if(ifieldp){
	ip = *ifieldp;
	if(ip){
	    for(ip = (*ifieldp); ip && ip->next; ip = ip->next)
	      ;

	    ip->next = ifield;
	}
	else
	  *ifieldp = ifield;
    }

    return(ifield);
}


/*
 * Create new IELEM_S, zero it out, and insert it at end.
 */
IELEM_S *
new_ielem(IELEM_S **ielemp)
{
    IELEM_S *ielem, *ip;

    ielem = (IELEM_S *) fs_get(sizeof(*ielem));
    memset(ielem, 0, sizeof(*ielem));

    if(ielemp){
	ip = *ielemp;
	if(ip){
	    for(ip = (*ielemp); ip && ip->next; ip = ip->next)
	      ;

	    ip->next = ielem;
	}
	else
	  *ielemp = ielem;
    }

    return(ielem);
}