[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 | |
---|
[f1e629d] | 71 | char *owl_view_get_style_name(owl_view *v) { |
---|
| 72 | return(owl_style_get_name(v->style)); |
---|
| 73 | } |
---|
| 74 | |
---|
[c3ab155] | 75 | owl_message *owl_view_get_element(owl_view *v, int index) |
---|
| 76 | { |
---|
[7d4fbcd] | 77 | return(owl_messagelist_get_element(&(v->ml), index)); |
---|
| 78 | } |
---|
| 79 | |
---|
[c3ab155] | 80 | void owl_view_delete_element(owl_view *v, int index) |
---|
| 81 | { |
---|
[7d4fbcd] | 82 | owl_messagelist_delete_element(&(v->ml), index); |
---|
| 83 | } |
---|
| 84 | |
---|
[c3ab155] | 85 | void owl_view_undelete_element(owl_view *v, int index) |
---|
| 86 | { |
---|
[7d4fbcd] | 87 | owl_messagelist_undelete_element(&(v->ml), index); |
---|
| 88 | } |
---|
| 89 | |
---|
[c3ab155] | 90 | int owl_view_get_size(owl_view *v) |
---|
| 91 | { |
---|
[7d4fbcd] | 92 | return(owl_messagelist_get_size(&(v->ml))); |
---|
| 93 | } |
---|
| 94 | |
---|
[59cf91c] | 95 | /* Returns the position in the view with a message closest |
---|
| 96 | * to the passed msgid. */ |
---|
[c3ab155] | 97 | int owl_view_get_nearest_to_msgid(owl_view *v, int targetid) |
---|
| 98 | { |
---|
[59cf91c] | 99 | int i, bestdist=-1, bestpos=0, curid, curdist; |
---|
| 100 | |
---|
| 101 | for (i=0; i<owl_view_get_size(v); i++) { |
---|
| 102 | curid = owl_message_get_id(owl_view_get_element(v, i)); |
---|
| 103 | curdist = abs(targetid-curid); |
---|
| 104 | if (bestdist<0 || curdist<bestdist) { |
---|
| 105 | bestdist = curdist; |
---|
| 106 | bestpos = i; |
---|
| 107 | } |
---|
| 108 | } |
---|
[3a2daac] | 109 | return (bestpos); |
---|
[59cf91c] | 110 | } |
---|
| 111 | |
---|
[c3ab155] | 112 | int owl_view_get_nearest_to_saved(owl_view *v) |
---|
| 113 | { |
---|
[3a2daac] | 114 | int cachedid; |
---|
| 115 | |
---|
| 116 | cachedid=owl_filter_get_cachedmsgid(v->filter); |
---|
[59cf91c] | 117 | if (cachedid<0) return(0); |
---|
[3a2daac] | 118 | return (owl_view_get_nearest_to_msgid(v, cachedid)); |
---|
[59cf91c] | 119 | } |
---|
| 120 | |
---|
| 121 | /* saves the current message position in the filter so it can |
---|
| 122 | * be restored later if we switch back to this filter. */ |
---|
[c3ab155] | 123 | void owl_view_save_curmsgid(owl_view *v, int curid) |
---|
| 124 | { |
---|
[59cf91c] | 125 | owl_filter_set_cachedmsgid(v->filter, curid); |
---|
| 126 | } |
---|
| 127 | |
---|
[ef56a67] | 128 | /* fmtext should already be initialized */ |
---|
| 129 | void owl_view_to_fmtext(owl_view *v, owl_fmtext *fm) |
---|
| 130 | { |
---|
| 131 | owl_fmtext_append_normal(fm, "Name: "); |
---|
| 132 | owl_fmtext_append_normal(fm, v->name); |
---|
| 133 | owl_fmtext_append_normal(fm, "\n"); |
---|
| 134 | |
---|
| 135 | owl_fmtext_append_normal(fm, "Filter: "); |
---|
| 136 | owl_fmtext_append_normal(fm, owl_filter_get_name(v->filter)); |
---|
| 137 | owl_fmtext_append_normal(fm, "\n"); |
---|
| 138 | |
---|
| 139 | owl_fmtext_append_normal(fm, "Style: "); |
---|
| 140 | owl_fmtext_append_normal(fm, owl_style_get_name(v->style)); |
---|
| 141 | owl_fmtext_append_normal(fm, "\n"); |
---|
| 142 | } |
---|
| 143 | |
---|
[c3ab155] | 144 | char *owl_view_get_filtname(owl_view *v) |
---|
| 145 | { |
---|
[7d4fbcd] | 146 | return(owl_filter_get_name(v->filter)); |
---|
| 147 | } |
---|
| 148 | |
---|
[c3ab155] | 149 | void owl_view_free(owl_view *v) |
---|
| 150 | { |
---|
[7d4fbcd] | 151 | owl_list_free_simple((owl_list *) &(v->ml)); |
---|
[c3ab155] | 152 | if (v->name) owl_free(v->name); |
---|
[7d4fbcd] | 153 | } |
---|