[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 | |
---|
[ef56a67] | 15 | char *owl_view_get_name(owl_view *v) |
---|
| 16 | { |
---|
| 17 | return(v->name); |
---|
| 18 | } |
---|
[c3ab155] | 19 | |
---|
| 20 | /* if the message matches the filter then add to view */ |
---|
| 21 | void owl_view_consider_message(owl_view *v, owl_message *m) |
---|
| 22 | { |
---|
[7d4fbcd] | 23 | if (owl_filter_message_match(v->filter, m)) { |
---|
| 24 | owl_messagelist_append_element(&(v->ml), m); |
---|
| 25 | } |
---|
| 26 | } |
---|
| 27 | |
---|
[c3ab155] | 28 | /* remove all messages, add all the global messages that match the |
---|
| 29 | * filter. |
---|
| 30 | */ |
---|
| 31 | void owl_view_recalculate(owl_view *v) |
---|
| 32 | { |
---|
[7d4fbcd] | 33 | int i, j; |
---|
| 34 | owl_messagelist *gml; |
---|
| 35 | owl_messagelist *ml; |
---|
| 36 | owl_message *m; |
---|
| 37 | |
---|
| 38 | gml=owl_global_get_msglist(&g); |
---|
| 39 | ml=&(v->ml); |
---|
| 40 | |
---|
| 41 | /* nuke the old list */ |
---|
| 42 | owl_list_free_simple((owl_list *) ml); |
---|
| 43 | owl_messagelist_create(&(v->ml)); |
---|
| 44 | |
---|
| 45 | /* find all the messages we want */ |
---|
| 46 | j=owl_messagelist_get_size(gml); |
---|
| 47 | for (i=0; i<j; i++) { |
---|
| 48 | m=owl_messagelist_get_element(gml, i); |
---|
| 49 | if (owl_filter_message_match(v->filter, m)) { |
---|
| 50 | owl_messagelist_append_element(ml, m); |
---|
| 51 | } |
---|
| 52 | } |
---|
| 53 | } |
---|
| 54 | |
---|
[c3ab155] | 55 | void owl_view_new_filter(owl_view *v, owl_filter *f) |
---|
| 56 | { |
---|
| 57 | v->filter=f; |
---|
| 58 | owl_view_recalculate(v); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | void owl_view_set_style(owl_view *v, owl_style *s) |
---|
| 62 | { |
---|
| 63 | v->style=s; |
---|
| 64 | } |
---|
| 65 | |
---|
[ef56a67] | 66 | owl_style *owl_view_get_style(owl_view *v) |
---|
| 67 | { |
---|
| 68 | return(v->style); |
---|
| 69 | } |
---|
| 70 | |
---|
[c3ab155] | 71 | owl_message *owl_view_get_element(owl_view *v, int index) |
---|
| 72 | { |
---|
[7d4fbcd] | 73 | return(owl_messagelist_get_element(&(v->ml), index)); |
---|
| 74 | } |
---|
| 75 | |
---|
[c3ab155] | 76 | void owl_view_delete_element(owl_view *v, int index) |
---|
| 77 | { |
---|
[7d4fbcd] | 78 | owl_messagelist_delete_element(&(v->ml), index); |
---|
| 79 | } |
---|
| 80 | |
---|
[c3ab155] | 81 | void owl_view_undelete_element(owl_view *v, int index) |
---|
| 82 | { |
---|
[7d4fbcd] | 83 | owl_messagelist_undelete_element(&(v->ml), index); |
---|
| 84 | } |
---|
| 85 | |
---|
[c3ab155] | 86 | int owl_view_get_size(owl_view *v) |
---|
| 87 | { |
---|
[7d4fbcd] | 88 | return(owl_messagelist_get_size(&(v->ml))); |
---|
| 89 | } |
---|
| 90 | |
---|
[59cf91c] | 91 | /* Returns the position in the view with a message closest |
---|
| 92 | * to the passed msgid. */ |
---|
[c3ab155] | 93 | int owl_view_get_nearest_to_msgid(owl_view *v, int targetid) |
---|
| 94 | { |
---|
[59cf91c] | 95 | int i, bestdist=-1, bestpos=0, curid, curdist; |
---|
| 96 | |
---|
| 97 | for (i=0; i<owl_view_get_size(v); i++) { |
---|
| 98 | curid = owl_message_get_id(owl_view_get_element(v, i)); |
---|
| 99 | curdist = abs(targetid-curid); |
---|
| 100 | if (bestdist<0 || curdist<bestdist) { |
---|
| 101 | bestdist = curdist; |
---|
| 102 | bestpos = i; |
---|
| 103 | } |
---|
| 104 | } |
---|
[3a2daac] | 105 | return (bestpos); |
---|
[59cf91c] | 106 | } |
---|
| 107 | |
---|
[c3ab155] | 108 | int owl_view_get_nearest_to_saved(owl_view *v) |
---|
| 109 | { |
---|
[3a2daac] | 110 | int cachedid; |
---|
| 111 | |
---|
| 112 | cachedid=owl_filter_get_cachedmsgid(v->filter); |
---|
[59cf91c] | 113 | if (cachedid<0) return(0); |
---|
[3a2daac] | 114 | return (owl_view_get_nearest_to_msgid(v, cachedid)); |
---|
[59cf91c] | 115 | } |
---|
| 116 | |
---|
| 117 | /* saves the current message position in the filter so it can |
---|
| 118 | * be restored later if we switch back to this filter. */ |
---|
[c3ab155] | 119 | void owl_view_save_curmsgid(owl_view *v, int curid) |
---|
| 120 | { |
---|
[59cf91c] | 121 | owl_filter_set_cachedmsgid(v->filter, curid); |
---|
| 122 | } |
---|
| 123 | |
---|
[ef56a67] | 124 | /* fmtext should already be initialized */ |
---|
| 125 | void owl_view_to_fmtext(owl_view *v, owl_fmtext *fm) |
---|
| 126 | { |
---|
| 127 | owl_fmtext_append_normal(fm, "Name: "); |
---|
| 128 | owl_fmtext_append_normal(fm, v->name); |
---|
| 129 | owl_fmtext_append_normal(fm, "\n"); |
---|
| 130 | |
---|
| 131 | owl_fmtext_append_normal(fm, "Filter: "); |
---|
| 132 | owl_fmtext_append_normal(fm, owl_filter_get_name(v->filter)); |
---|
| 133 | owl_fmtext_append_normal(fm, "\n"); |
---|
| 134 | |
---|
| 135 | owl_fmtext_append_normal(fm, "Style: "); |
---|
| 136 | owl_fmtext_append_normal(fm, owl_style_get_name(v->style)); |
---|
| 137 | owl_fmtext_append_normal(fm, "\n"); |
---|
| 138 | } |
---|
| 139 | |
---|
[c3ab155] | 140 | char *owl_view_get_filtname(owl_view *v) |
---|
| 141 | { |
---|
[7d4fbcd] | 142 | return(owl_filter_get_name(v->filter)); |
---|
| 143 | } |
---|
| 144 | |
---|
[c3ab155] | 145 | void owl_view_free(owl_view *v) |
---|
| 146 | { |
---|
[7d4fbcd] | 147 | owl_list_free_simple((owl_list *) &(v->ml)); |
---|
[c3ab155] | 148 | if (v->name) owl_free(v->name); |
---|
[7d4fbcd] | 149 | } |
---|