1 | #include <stdlib.h> |
---|
2 | #include "owl.h" |
---|
3 | |
---|
4 | static const char fileIdent[] = "$Id$"; |
---|
5 | |
---|
6 | void owl_view_create(owl_view *v, char *name, owl_filter *f, owl_style *s) |
---|
7 | { |
---|
8 | v->name=owl_strdup(name); |
---|
9 | v->filter=f; |
---|
10 | v->style=s; |
---|
11 | owl_messagelist_create(&(v->ml)); |
---|
12 | owl_view_recalculate(v); |
---|
13 | } |
---|
14 | |
---|
15 | char *owl_view_get_name(owl_view *v) |
---|
16 | { |
---|
17 | return(v->name); |
---|
18 | } |
---|
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 | { |
---|
23 | if (owl_filter_message_match(v->filter, m)) { |
---|
24 | owl_messagelist_append_element(&(v->ml), m); |
---|
25 | } |
---|
26 | } |
---|
27 | |
---|
28 | /* remove all messages, add all the global messages that match the |
---|
29 | * filter. |
---|
30 | */ |
---|
31 | void owl_view_recalculate(owl_view *v) |
---|
32 | { |
---|
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 | |
---|
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 | |
---|
66 | owl_style *owl_view_get_style(owl_view *v) |
---|
67 | { |
---|
68 | return(v->style); |
---|
69 | } |
---|
70 | |
---|
71 | char *owl_view_get_style_name(owl_view *v) { |
---|
72 | return(owl_style_get_name(v->style)); |
---|
73 | } |
---|
74 | |
---|
75 | owl_message *owl_view_get_element(owl_view *v, int index) |
---|
76 | { |
---|
77 | return(owl_messagelist_get_element(&(v->ml), index)); |
---|
78 | } |
---|
79 | |
---|
80 | void owl_view_delete_element(owl_view *v, int index) |
---|
81 | { |
---|
82 | owl_messagelist_delete_element(&(v->ml), index); |
---|
83 | } |
---|
84 | |
---|
85 | void owl_view_undelete_element(owl_view *v, int index) |
---|
86 | { |
---|
87 | owl_messagelist_undelete_element(&(v->ml), index); |
---|
88 | } |
---|
89 | |
---|
90 | int owl_view_get_size(owl_view *v) |
---|
91 | { |
---|
92 | return(owl_messagelist_get_size(&(v->ml))); |
---|
93 | } |
---|
94 | |
---|
95 | /* Returns the position in the view with a message closest |
---|
96 | * to the passed msgid. */ |
---|
97 | int owl_view_get_nearest_to_msgid(owl_view *v, int targetid) |
---|
98 | { |
---|
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; |
---|
112 | } |
---|
113 | } |
---|
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; |
---|
124 | } |
---|
125 | |
---|
126 | int owl_view_get_nearest_to_saved(owl_view *v) |
---|
127 | { |
---|
128 | int cachedid; |
---|
129 | |
---|
130 | cachedid=owl_filter_get_cachedmsgid(v->filter); |
---|
131 | if (cachedid<0) return(0); |
---|
132 | return (owl_view_get_nearest_to_msgid(v, cachedid)); |
---|
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. */ |
---|
137 | void owl_view_save_curmsgid(owl_view *v, int curid) |
---|
138 | { |
---|
139 | owl_filter_set_cachedmsgid(v->filter, curid); |
---|
140 | } |
---|
141 | |
---|
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 | |
---|
158 | char *owl_view_get_filtname(owl_view *v) |
---|
159 | { |
---|
160 | return(owl_filter_get_name(v->filter)); |
---|
161 | } |
---|
162 | |
---|
163 | void owl_view_free(owl_view *v) |
---|
164 | { |
---|
165 | owl_list_free_simple((owl_list *) &(v->ml)); |
---|
166 | if (v->name) owl_free(v->name); |
---|
167 | } |
---|