1 | #include "owl.h" |
---|
2 | |
---|
3 | static const char fileIdent[] = "$Id$"; |
---|
4 | |
---|
5 | void owl_view_create(owl_view *v, owl_filter *f) { |
---|
6 | v->filter=f; |
---|
7 | owl_messagelist_create(&(v->ml)); |
---|
8 | owl_view_recalculate(v); |
---|
9 | } |
---|
10 | |
---|
11 | void owl_view_consider_message(owl_view *v, owl_message *m) { |
---|
12 | /* if the message matches the filter then add to view */ |
---|
13 | if (owl_filter_message_match(v->filter, m)) { |
---|
14 | owl_messagelist_append_element(&(v->ml), m); |
---|
15 | } |
---|
16 | } |
---|
17 | |
---|
18 | void owl_view_recalculate(owl_view *v) { |
---|
19 | /* add all the global messages that match the filter */ |
---|
20 | int i, j; |
---|
21 | owl_messagelist *gml; |
---|
22 | owl_messagelist *ml; |
---|
23 | owl_message *m; |
---|
24 | |
---|
25 | gml=owl_global_get_msglist(&g); |
---|
26 | ml=&(v->ml); |
---|
27 | |
---|
28 | /* nuke the old list */ |
---|
29 | owl_list_free_simple((owl_list *) ml); |
---|
30 | owl_messagelist_create(&(v->ml)); |
---|
31 | |
---|
32 | /* find all the messages we want */ |
---|
33 | j=owl_messagelist_get_size(gml); |
---|
34 | for (i=0; i<j; i++) { |
---|
35 | m=owl_messagelist_get_element(gml, i); |
---|
36 | if (owl_filter_message_match(v->filter, m)) { |
---|
37 | owl_messagelist_append_element(ml, m); |
---|
38 | } |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | owl_message *owl_view_get_element(owl_view *v, int index) { |
---|
43 | return(owl_messagelist_get_element(&(v->ml), index)); |
---|
44 | } |
---|
45 | |
---|
46 | void owl_view_delete_element(owl_view *v, int index) { |
---|
47 | owl_messagelist_delete_element(&(v->ml), index); |
---|
48 | } |
---|
49 | |
---|
50 | void owl_view_undelete_element(owl_view *v, int index) { |
---|
51 | owl_messagelist_undelete_element(&(v->ml), index); |
---|
52 | } |
---|
53 | |
---|
54 | int owl_view_get_size(owl_view *v) { |
---|
55 | return(owl_messagelist_get_size(&(v->ml))); |
---|
56 | } |
---|
57 | |
---|
58 | /* Returns the position in the view with a message closest |
---|
59 | * to the passed msgid. */ |
---|
60 | int owl_view_get_nearest_to_msgid(owl_view *v, int targetid) { |
---|
61 | int i, bestdist=-1, bestpos=0, curid, curdist; |
---|
62 | |
---|
63 | for (i=0; i<owl_view_get_size(v); i++) { |
---|
64 | curid = owl_message_get_id(owl_view_get_element(v, i)); |
---|
65 | curdist = abs(targetid-curid); |
---|
66 | if (bestdist<0 || curdist<bestdist) { |
---|
67 | bestdist = curdist; |
---|
68 | bestpos = i; |
---|
69 | } |
---|
70 | } |
---|
71 | return bestpos; |
---|
72 | } |
---|
73 | |
---|
74 | int owl_view_get_nearest_to_saved(owl_view *v) { |
---|
75 | int cachedid = owl_filter_get_cachedmsgid(v->filter); |
---|
76 | if (cachedid<0) return(0); |
---|
77 | return owl_view_get_nearest_to_msgid(v, cachedid); |
---|
78 | } |
---|
79 | |
---|
80 | /* saves the current message position in the filter so it can |
---|
81 | * be restored later if we switch back to this filter. */ |
---|
82 | void owl_view_save_curmsgid(owl_view *v, int curid) { |
---|
83 | owl_filter_set_cachedmsgid(v->filter, curid); |
---|
84 | } |
---|
85 | |
---|
86 | char *owl_view_get_filtname(owl_view *v) { |
---|
87 | return(owl_filter_get_name(v->filter)); |
---|
88 | } |
---|
89 | |
---|
90 | void owl_view_free(owl_view *v) { |
---|
91 | owl_list_free_simple((owl_list *) &(v->ml)); |
---|
92 | } |
---|