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