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 | owl_message *owl_view_get_element(owl_view *v, int index) |
---|
72 | { |
---|
73 | return(owl_messagelist_get_element(&(v->ml), index)); |
---|
74 | } |
---|
75 | |
---|
76 | void owl_view_delete_element(owl_view *v, int index) |
---|
77 | { |
---|
78 | owl_messagelist_delete_element(&(v->ml), index); |
---|
79 | } |
---|
80 | |
---|
81 | void owl_view_undelete_element(owl_view *v, int index) |
---|
82 | { |
---|
83 | owl_messagelist_undelete_element(&(v->ml), index); |
---|
84 | } |
---|
85 | |
---|
86 | int owl_view_get_size(owl_view *v) |
---|
87 | { |
---|
88 | return(owl_messagelist_get_size(&(v->ml))); |
---|
89 | } |
---|
90 | |
---|
91 | /* Returns the position in the view with a message closest |
---|
92 | * to the passed msgid. */ |
---|
93 | int owl_view_get_nearest_to_msgid(owl_view *v, int targetid) |
---|
94 | { |
---|
95 | int i, bestdist=-1, bestpos=0, curid, curdist; |
---|
96 | |
---|
97 | for (i=0; i<owl_view_get_size(v); i++) { |
---|
98 | curid = owl_message_get_id(owl_view_get_element(v, i)); |
---|
99 | curdist = abs(targetid-curid); |
---|
100 | if (bestdist<0 || curdist<bestdist) { |
---|
101 | bestdist = curdist; |
---|
102 | bestpos = i; |
---|
103 | } |
---|
104 | } |
---|
105 | return (bestpos); |
---|
106 | } |
---|
107 | |
---|
108 | int owl_view_get_nearest_to_saved(owl_view *v) |
---|
109 | { |
---|
110 | int cachedid; |
---|
111 | |
---|
112 | cachedid=owl_filter_get_cachedmsgid(v->filter); |
---|
113 | if (cachedid<0) return(0); |
---|
114 | return (owl_view_get_nearest_to_msgid(v, cachedid)); |
---|
115 | } |
---|
116 | |
---|
117 | /* saves the current message position in the filter so it can |
---|
118 | * be restored later if we switch back to this filter. */ |
---|
119 | void owl_view_save_curmsgid(owl_view *v, int curid) |
---|
120 | { |
---|
121 | owl_filter_set_cachedmsgid(v->filter, curid); |
---|
122 | } |
---|
123 | |
---|
124 | /* fmtext should already be initialized */ |
---|
125 | void owl_view_to_fmtext(owl_view *v, owl_fmtext *fm) |
---|
126 | { |
---|
127 | owl_fmtext_append_normal(fm, "Name: "); |
---|
128 | owl_fmtext_append_normal(fm, v->name); |
---|
129 | owl_fmtext_append_normal(fm, "\n"); |
---|
130 | |
---|
131 | owl_fmtext_append_normal(fm, "Filter: "); |
---|
132 | owl_fmtext_append_normal(fm, owl_filter_get_name(v->filter)); |
---|
133 | owl_fmtext_append_normal(fm, "\n"); |
---|
134 | |
---|
135 | owl_fmtext_append_normal(fm, "Style: "); |
---|
136 | owl_fmtext_append_normal(fm, owl_style_get_name(v->style)); |
---|
137 | owl_fmtext_append_normal(fm, "\n"); |
---|
138 | } |
---|
139 | |
---|
140 | char *owl_view_get_filtname(owl_view *v) |
---|
141 | { |
---|
142 | return(owl_filter_get_name(v->filter)); |
---|
143 | } |
---|
144 | |
---|
145 | void owl_view_free(owl_view *v) |
---|
146 | { |
---|
147 | owl_list_free_simple((owl_list *) &(v->ml)); |
---|
148 | if (v->name) owl_free(v->name); |
---|
149 | } |
---|