[5f37310] | 1 | #include <stdlib.h> |
---|
[7d4fbcd] | 2 | #include "owl.h" |
---|
| 3 | |
---|
[1fdab04] | 4 | void owl_view_create(owl_view *v, const char *name, owl_filter *f, const owl_style *s) |
---|
[c3ab155] | 5 | { |
---|
| 6 | v->name=owl_strdup(name); |
---|
[7d4fbcd] | 7 | v->filter=f; |
---|
[c3ab155] | 8 | v->style=s; |
---|
[7d4fbcd] | 9 | owl_messagelist_create(&(v->ml)); |
---|
| 10 | owl_view_recalculate(v); |
---|
| 11 | } |
---|
| 12 | |
---|
[9e5c9f3] | 13 | const char *owl_view_get_name(const owl_view *v) |
---|
[ef56a67] | 14 | { |
---|
| 15 | return(v->name); |
---|
| 16 | } |
---|
[c3ab155] | 17 | |
---|
| 18 | /* if the message matches the filter then add to view */ |
---|
| 19 | void owl_view_consider_message(owl_view *v, owl_message *m) |
---|
| 20 | { |
---|
[7d4fbcd] | 21 | if (owl_filter_message_match(v->filter, m)) { |
---|
| 22 | owl_messagelist_append_element(&(v->ml), m); |
---|
| 23 | } |
---|
| 24 | } |
---|
| 25 | |
---|
[c3ab155] | 26 | /* remove all messages, add all the global messages that match the |
---|
| 27 | * filter. |
---|
| 28 | */ |
---|
| 29 | void owl_view_recalculate(owl_view *v) |
---|
| 30 | { |
---|
[7d4fbcd] | 31 | int i, j; |
---|
[3eb599d] | 32 | const owl_messagelist *gml; |
---|
[7d4fbcd] | 33 | owl_messagelist *ml; |
---|
| 34 | owl_message *m; |
---|
| 35 | |
---|
| 36 | gml=owl_global_get_msglist(&g); |
---|
| 37 | ml=&(v->ml); |
---|
| 38 | |
---|
| 39 | /* nuke the old list */ |
---|
[5e5f08f] | 40 | owl_list_cleanup(&ml->list, NULL); |
---|
[7d4fbcd] | 41 | owl_messagelist_create(&(v->ml)); |
---|
| 42 | |
---|
| 43 | /* find all the messages we want */ |
---|
| 44 | j=owl_messagelist_get_size(gml); |
---|
| 45 | for (i=0; i<j; i++) { |
---|
| 46 | m=owl_messagelist_get_element(gml, i); |
---|
| 47 | if (owl_filter_message_match(v->filter, m)) { |
---|
| 48 | owl_messagelist_append_element(ml, m); |
---|
| 49 | } |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | |
---|
[c3ab155] | 53 | void owl_view_new_filter(owl_view *v, owl_filter *f) |
---|
| 54 | { |
---|
| 55 | v->filter=f; |
---|
| 56 | owl_view_recalculate(v); |
---|
| 57 | } |
---|
| 58 | |
---|
[1fdab04] | 59 | void owl_view_set_style(owl_view *v, const owl_style *s) |
---|
[c3ab155] | 60 | { |
---|
| 61 | v->style=s; |
---|
| 62 | } |
---|
| 63 | |
---|
[9e5c9f3] | 64 | const owl_style *owl_view_get_style(const owl_view *v) |
---|
[ef56a67] | 65 | { |
---|
| 66 | return(v->style); |
---|
| 67 | } |
---|
| 68 | |
---|
[9e5c9f3] | 69 | const char *owl_view_get_style_name(const owl_view *v) { |
---|
[f1e629d] | 70 | return(owl_style_get_name(v->style)); |
---|
| 71 | } |
---|
| 72 | |
---|
[9e5c9f3] | 73 | owl_message *owl_view_get_element(const owl_view *v, int index) |
---|
[c3ab155] | 74 | { |
---|
[7d4fbcd] | 75 | return(owl_messagelist_get_element(&(v->ml), index)); |
---|
| 76 | } |
---|
| 77 | |
---|
[c3ab155] | 78 | void owl_view_delete_element(owl_view *v, int index) |
---|
| 79 | { |
---|
[7d4fbcd] | 80 | owl_messagelist_delete_element(&(v->ml), index); |
---|
| 81 | } |
---|
| 82 | |
---|
[c3ab155] | 83 | void owl_view_undelete_element(owl_view *v, int index) |
---|
| 84 | { |
---|
[7d4fbcd] | 85 | owl_messagelist_undelete_element(&(v->ml), index); |
---|
| 86 | } |
---|
| 87 | |
---|
[9e5c9f3] | 88 | int owl_view_get_size(const owl_view *v) |
---|
[c3ab155] | 89 | { |
---|
[7d4fbcd] | 90 | return(owl_messagelist_get_size(&(v->ml))); |
---|
| 91 | } |
---|
| 92 | |
---|
[59cf91c] | 93 | /* Returns the position in the view with a message closest |
---|
| 94 | * to the passed msgid. */ |
---|
[9e5c9f3] | 95 | int owl_view_get_nearest_to_msgid(const owl_view *v, int targetid) |
---|
[c3ab155] | 96 | { |
---|
[801b7ac] | 97 | int first, last, mid = 0, max, bestdist, curid = 0; |
---|
| 98 | |
---|
| 99 | first = 0; |
---|
| 100 | last = max = owl_view_get_size(v) - 1; |
---|
| 101 | while (first <= last) { |
---|
| 102 | mid = (first + last) / 2; |
---|
| 103 | curid = owl_message_get_id(owl_view_get_element(v, mid)); |
---|
| 104 | if (curid == targetid) { |
---|
| 105 | return(mid); |
---|
| 106 | } else if (curid < targetid) { |
---|
| 107 | first = mid + 1; |
---|
| 108 | } else { |
---|
| 109 | last = mid - 1; |
---|
[59cf91c] | 110 | } |
---|
| 111 | } |
---|
[801b7ac] | 112 | bestdist = abs(targetid-curid); |
---|
| 113 | if (curid < targetid && mid+1 < max) { |
---|
| 114 | curid = owl_message_get_id(owl_view_get_element(v, mid+1)); |
---|
| 115 | mid = (bestdist < abs(targetid-curid)) ? mid : mid+1; |
---|
| 116 | } |
---|
| 117 | else if (curid > targetid && mid-1 >= 0) { |
---|
| 118 | curid = owl_message_get_id(owl_view_get_element(v, mid-1)); |
---|
| 119 | mid = (bestdist < abs(targetid-curid)) ? mid : mid-1; |
---|
| 120 | } |
---|
| 121 | return mid; |
---|
[59cf91c] | 122 | } |
---|
| 123 | |
---|
[9e5c9f3] | 124 | int owl_view_get_nearest_to_saved(const owl_view *v) |
---|
[c3ab155] | 125 | { |
---|
[3a2daac] | 126 | int cachedid; |
---|
| 127 | |
---|
[c3a47c9] | 128 | cachedid = v->cachedmsgid; |
---|
[59cf91c] | 129 | if (cachedid<0) return(0); |
---|
[3a2daac] | 130 | return (owl_view_get_nearest_to_msgid(v, cachedid)); |
---|
[59cf91c] | 131 | } |
---|
| 132 | |
---|
[c3ab155] | 133 | void owl_view_save_curmsgid(owl_view *v, int curid) |
---|
| 134 | { |
---|
[c3a47c9] | 135 | v->cachedmsgid = curid; |
---|
[59cf91c] | 136 | } |
---|
| 137 | |
---|
[ef56a67] | 138 | /* fmtext should already be initialized */ |
---|
[9e5c9f3] | 139 | void owl_view_to_fmtext(const owl_view *v, owl_fmtext *fm) |
---|
[ef56a67] | 140 | { |
---|
| 141 | owl_fmtext_append_normal(fm, "Name: "); |
---|
| 142 | owl_fmtext_append_normal(fm, v->name); |
---|
| 143 | owl_fmtext_append_normal(fm, "\n"); |
---|
| 144 | |
---|
| 145 | owl_fmtext_append_normal(fm, "Filter: "); |
---|
| 146 | owl_fmtext_append_normal(fm, owl_filter_get_name(v->filter)); |
---|
| 147 | owl_fmtext_append_normal(fm, "\n"); |
---|
| 148 | |
---|
| 149 | owl_fmtext_append_normal(fm, "Style: "); |
---|
| 150 | owl_fmtext_append_normal(fm, owl_style_get_name(v->style)); |
---|
| 151 | owl_fmtext_append_normal(fm, "\n"); |
---|
| 152 | } |
---|
| 153 | |
---|
[9e5c9f3] | 154 | const char *owl_view_get_filtname(const owl_view *v) |
---|
[c3ab155] | 155 | { |
---|
[7d4fbcd] | 156 | return(owl_filter_get_name(v->filter)); |
---|
| 157 | } |
---|
| 158 | |
---|
[94b0b1d] | 159 | void owl_view_cleanup(owl_view *v) |
---|
[c3ab155] | 160 | { |
---|
[5e5f08f] | 161 | owl_list_cleanup(&v->ml.list, NULL); |
---|
[c3ab155] | 162 | if (v->name) owl_free(v->name); |
---|
[7d4fbcd] | 163 | } |
---|