[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 | { |
---|
[801b7ac] | 99 | int first, last, mid = 0, max, bestdist, curid = 0; |
---|
| 100 | |
---|
| 101 | first = 0; |
---|
| 102 | last = max = owl_view_get_size(v) - 1; |
---|
| 103 | while (first <= last) { |
---|
| 104 | mid = (first + last) / 2; |
---|
| 105 | curid = owl_message_get_id(owl_view_get_element(v, mid)); |
---|
| 106 | if (curid == targetid) { |
---|
| 107 | return(mid); |
---|
| 108 | } else if (curid < targetid) { |
---|
| 109 | first = mid + 1; |
---|
| 110 | } else { |
---|
| 111 | last = mid - 1; |
---|
[59cf91c] | 112 | } |
---|
| 113 | } |
---|
[801b7ac] | 114 | bestdist = abs(targetid-curid); |
---|
| 115 | if (curid < targetid && mid+1 < max) { |
---|
| 116 | curid = owl_message_get_id(owl_view_get_element(v, mid+1)); |
---|
| 117 | mid = (bestdist < abs(targetid-curid)) ? mid : mid+1; |
---|
| 118 | } |
---|
| 119 | else if (curid > targetid && mid-1 >= 0) { |
---|
| 120 | curid = owl_message_get_id(owl_view_get_element(v, mid-1)); |
---|
| 121 | mid = (bestdist < abs(targetid-curid)) ? mid : mid-1; |
---|
| 122 | } |
---|
| 123 | return mid; |
---|
[59cf91c] | 124 | } |
---|
| 125 | |
---|
[c3ab155] | 126 | int owl_view_get_nearest_to_saved(owl_view *v) |
---|
| 127 | { |
---|
[3a2daac] | 128 | int cachedid; |
---|
| 129 | |
---|
| 130 | cachedid=owl_filter_get_cachedmsgid(v->filter); |
---|
[59cf91c] | 131 | if (cachedid<0) return(0); |
---|
[3a2daac] | 132 | return (owl_view_get_nearest_to_msgid(v, cachedid)); |
---|
[59cf91c] | 133 | } |
---|
| 134 | |
---|
| 135 | /* saves the current message position in the filter so it can |
---|
| 136 | * be restored later if we switch back to this filter. */ |
---|
[c3ab155] | 137 | void owl_view_save_curmsgid(owl_view *v, int curid) |
---|
| 138 | { |
---|
[59cf91c] | 139 | owl_filter_set_cachedmsgid(v->filter, curid); |
---|
| 140 | } |
---|
| 141 | |
---|
[ef56a67] | 142 | /* fmtext should already be initialized */ |
---|
| 143 | void owl_view_to_fmtext(owl_view *v, owl_fmtext *fm) |
---|
| 144 | { |
---|
| 145 | owl_fmtext_append_normal(fm, "Name: "); |
---|
| 146 | owl_fmtext_append_normal(fm, v->name); |
---|
| 147 | owl_fmtext_append_normal(fm, "\n"); |
---|
| 148 | |
---|
| 149 | owl_fmtext_append_normal(fm, "Filter: "); |
---|
| 150 | owl_fmtext_append_normal(fm, owl_filter_get_name(v->filter)); |
---|
| 151 | owl_fmtext_append_normal(fm, "\n"); |
---|
| 152 | |
---|
| 153 | owl_fmtext_append_normal(fm, "Style: "); |
---|
| 154 | owl_fmtext_append_normal(fm, owl_style_get_name(v->style)); |
---|
| 155 | owl_fmtext_append_normal(fm, "\n"); |
---|
| 156 | } |
---|
| 157 | |
---|
[c3ab155] | 158 | char *owl_view_get_filtname(owl_view *v) |
---|
| 159 | { |
---|
[7d4fbcd] | 160 | return(owl_filter_get_name(v->filter)); |
---|
| 161 | } |
---|
| 162 | |
---|
[c3ab155] | 163 | void owl_view_free(owl_view *v) |
---|
| 164 | { |
---|
[7d4fbcd] | 165 | owl_list_free_simple((owl_list *) &(v->ml)); |
---|
[c3ab155] | 166 | if (v->name) owl_free(v->name); |
---|
[7d4fbcd] | 167 | } |
---|