1 | #include "owl.h" |
---|
2 | |
---|
3 | void owl_view_create(owl_view *v, const char *name, owl_filter *f, const owl_style *s) |
---|
4 | { |
---|
5 | v->name=g_strdup(name); |
---|
6 | v->filter=f; |
---|
7 | v->style=s; |
---|
8 | owl_messagelist_create(&(v->ml)); |
---|
9 | owl_view_recalculate(v); |
---|
10 | } |
---|
11 | |
---|
12 | const char *owl_view_get_name(const owl_view *v) |
---|
13 | { |
---|
14 | return(v->name); |
---|
15 | } |
---|
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 | { |
---|
20 | if (owl_filter_message_match(v->filter, m)) { |
---|
21 | owl_messagelist_append_element(&(v->ml), m); |
---|
22 | } |
---|
23 | } |
---|
24 | |
---|
25 | /* remove all messages, add all the global messages that match the |
---|
26 | * filter. |
---|
27 | */ |
---|
28 | void owl_view_recalculate(owl_view *v) |
---|
29 | { |
---|
30 | int i, j; |
---|
31 | const owl_messagelist *gml; |
---|
32 | owl_messagelist *ml; |
---|
33 | owl_message *m; |
---|
34 | |
---|
35 | gml=owl_global_get_msglist(&g); |
---|
36 | ml=&(v->ml); |
---|
37 | |
---|
38 | /* nuke the old list, don't free the messages */ |
---|
39 | owl_messagelist_cleanup(ml, false); |
---|
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 | |
---|
52 | void owl_view_new_filter(owl_view *v, owl_filter *f) |
---|
53 | { |
---|
54 | v->filter=f; |
---|
55 | owl_view_recalculate(v); |
---|
56 | } |
---|
57 | |
---|
58 | void owl_view_set_style(owl_view *v, const owl_style *s) |
---|
59 | { |
---|
60 | v->style=s; |
---|
61 | } |
---|
62 | |
---|
63 | const owl_style *owl_view_get_style(const owl_view *v) |
---|
64 | { |
---|
65 | return(v->style); |
---|
66 | } |
---|
67 | |
---|
68 | const char *owl_view_get_style_name(const owl_view *v) { |
---|
69 | return(owl_style_get_name(v->style)); |
---|
70 | } |
---|
71 | |
---|
72 | owl_message *owl_view_get_element(const owl_view *v, int index) |
---|
73 | { |
---|
74 | return(owl_messagelist_get_element(&(v->ml), index)); |
---|
75 | } |
---|
76 | |
---|
77 | void owl_view_delete_element(owl_view *v, int index) |
---|
78 | { |
---|
79 | owl_messagelist_delete_element(&(v->ml), index); |
---|
80 | } |
---|
81 | |
---|
82 | void owl_view_undelete_element(owl_view *v, int index) |
---|
83 | { |
---|
84 | owl_messagelist_undelete_element(&(v->ml), index); |
---|
85 | } |
---|
86 | |
---|
87 | int owl_view_get_size(const owl_view *v) |
---|
88 | { |
---|
89 | return(owl_messagelist_get_size(&(v->ml))); |
---|
90 | } |
---|
91 | |
---|
92 | /* Returns the position in the view with a message closest |
---|
93 | * to the passed msgid. */ |
---|
94 | int owl_view_get_nearest_to_msgid(const owl_view *v, int targetid) |
---|
95 | { |
---|
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; |
---|
109 | } |
---|
110 | } |
---|
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; |
---|
121 | } |
---|
122 | |
---|
123 | int owl_view_get_nearest_to_saved(const owl_view *v) |
---|
124 | { |
---|
125 | int cachedid; |
---|
126 | |
---|
127 | cachedid = v->cachedmsgid; |
---|
128 | if (cachedid<0) return(0); |
---|
129 | return (owl_view_get_nearest_to_msgid(v, cachedid)); |
---|
130 | } |
---|
131 | |
---|
132 | void owl_view_save_curmsgid(owl_view *v, int curid) |
---|
133 | { |
---|
134 | v->cachedmsgid = curid; |
---|
135 | } |
---|
136 | |
---|
137 | /* fmtext should already be initialized */ |
---|
138 | void owl_view_to_fmtext(const owl_view *v, owl_fmtext *fm) |
---|
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 | |
---|
153 | const char *owl_view_get_filtname(const owl_view *v) |
---|
154 | { |
---|
155 | return(owl_filter_get_name(v->filter)); |
---|
156 | } |
---|
157 | |
---|
158 | void owl_view_cleanup(owl_view *v) |
---|
159 | { |
---|
160 | owl_messagelist_cleanup(&v->ml, false); |
---|
161 | g_free(v->name); |
---|
162 | } |
---|