[5f37310] | 1 | #include <stdlib.h> |
---|
[7d4fbcd] | 2 | #include "owl.h" |
---|
| 3 | |
---|
[1aee7d9] | 4 | static const char fileIdent[] = "$Id$"; |
---|
| 5 | |
---|
[7d4fbcd] | 6 | void owl_view_create(owl_view *v, owl_filter *f) { |
---|
| 7 | v->filter=f; |
---|
| 8 | owl_messagelist_create(&(v->ml)); |
---|
| 9 | owl_view_recalculate(v); |
---|
| 10 | } |
---|
| 11 | |
---|
| 12 | void owl_view_consider_message(owl_view *v, owl_message *m) { |
---|
| 13 | /* if the message matches the filter then add to view */ |
---|
| 14 | if (owl_filter_message_match(v->filter, m)) { |
---|
| 15 | owl_messagelist_append_element(&(v->ml), m); |
---|
| 16 | } |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | void owl_view_recalculate(owl_view *v) { |
---|
| 20 | /* add all the global messages that match the filter */ |
---|
| 21 | int i, j; |
---|
| 22 | owl_messagelist *gml; |
---|
| 23 | owl_messagelist *ml; |
---|
| 24 | owl_message *m; |
---|
| 25 | |
---|
| 26 | gml=owl_global_get_msglist(&g); |
---|
| 27 | ml=&(v->ml); |
---|
| 28 | |
---|
| 29 | /* nuke the old list */ |
---|
| 30 | owl_list_free_simple((owl_list *) ml); |
---|
| 31 | owl_messagelist_create(&(v->ml)); |
---|
| 32 | |
---|
| 33 | /* find all the messages we want */ |
---|
| 34 | j=owl_messagelist_get_size(gml); |
---|
| 35 | for (i=0; i<j; i++) { |
---|
| 36 | m=owl_messagelist_get_element(gml, i); |
---|
| 37 | if (owl_filter_message_match(v->filter, m)) { |
---|
| 38 | owl_messagelist_append_element(ml, m); |
---|
| 39 | } |
---|
| 40 | } |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | owl_message *owl_view_get_element(owl_view *v, int index) { |
---|
| 44 | return(owl_messagelist_get_element(&(v->ml), index)); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | void owl_view_delete_element(owl_view *v, int index) { |
---|
| 48 | owl_messagelist_delete_element(&(v->ml), index); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | void owl_view_undelete_element(owl_view *v, int index) { |
---|
| 52 | owl_messagelist_undelete_element(&(v->ml), index); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | int owl_view_get_size(owl_view *v) { |
---|
| 56 | return(owl_messagelist_get_size(&(v->ml))); |
---|
| 57 | } |
---|
| 58 | |
---|
[59cf91c] | 59 | /* Returns the position in the view with a message closest |
---|
| 60 | * to the passed msgid. */ |
---|
| 61 | int owl_view_get_nearest_to_msgid(owl_view *v, int targetid) { |
---|
| 62 | int i, bestdist=-1, bestpos=0, curid, curdist; |
---|
| 63 | |
---|
| 64 | for (i=0; i<owl_view_get_size(v); i++) { |
---|
| 65 | curid = owl_message_get_id(owl_view_get_element(v, i)); |
---|
| 66 | curdist = abs(targetid-curid); |
---|
| 67 | if (bestdist<0 || curdist<bestdist) { |
---|
| 68 | bestdist = curdist; |
---|
| 69 | bestpos = i; |
---|
| 70 | } |
---|
| 71 | } |
---|
[3a2daac] | 72 | return (bestpos); |
---|
[59cf91c] | 73 | } |
---|
| 74 | |
---|
| 75 | int owl_view_get_nearest_to_saved(owl_view *v) { |
---|
[3a2daac] | 76 | int cachedid; |
---|
| 77 | |
---|
| 78 | cachedid=owl_filter_get_cachedmsgid(v->filter); |
---|
[59cf91c] | 79 | if (cachedid<0) return(0); |
---|
[3a2daac] | 80 | return (owl_view_get_nearest_to_msgid(v, cachedid)); |
---|
[59cf91c] | 81 | } |
---|
| 82 | |
---|
| 83 | /* saves the current message position in the filter so it can |
---|
| 84 | * be restored later if we switch back to this filter. */ |
---|
| 85 | void owl_view_save_curmsgid(owl_view *v, int curid) { |
---|
| 86 | owl_filter_set_cachedmsgid(v->filter, curid); |
---|
| 87 | } |
---|
| 88 | |
---|
[7d4fbcd] | 89 | char *owl_view_get_filtname(owl_view *v) { |
---|
| 90 | return(owl_filter_get_name(v->filter)); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | void owl_view_free(owl_view *v) { |
---|
| 94 | owl_list_free_simple((owl_list *) &(v->ml)); |
---|
| 95 | } |
---|