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