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