forked from barnowl/barnowl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
view.c
160 lines (135 loc) · 3.72 KB
/
view.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "owl.h"
void owl_view_create(owl_view *v, const char *name, owl_filter *f, const owl_style *s)
{
v->name=g_strdup(name);
v->filter=f;
v->style=s;
v->ml = owl_messagelist_new();
owl_view_recalculate(v);
}
const char *owl_view_get_name(const owl_view *v)
{
return(v->name);
}
/* if the message matches the filter then add to view */
void owl_view_consider_message(owl_view *v, owl_message *m)
{
if (owl_filter_message_match(v->filter, m)) {
owl_messagelist_append_element(v->ml, m);
}
}
/* remove all messages, add all the global messages that match the
* filter.
*/
void owl_view_recalculate(owl_view *v)
{
int i, j;
const owl_messagelist *gml;
owl_message *m;
gml=owl_global_get_msglist(&g);
/* nuke the old list, don't free the messages */
owl_messagelist_delete(v->ml, false);
v->ml = owl_messagelist_new();
/* find all the messages we want */
j=owl_messagelist_get_size(gml);
for (i=0; i<j; i++) {
m=owl_messagelist_get_element(gml, i);
if (owl_filter_message_match(v->filter, m)) {
owl_messagelist_append_element(v->ml, m);
}
}
}
void owl_view_new_filter(owl_view *v, owl_filter *f)
{
v->filter=f;
owl_view_recalculate(v);
}
void owl_view_set_style(owl_view *v, const owl_style *s)
{
v->style=s;
}
const owl_style *owl_view_get_style(const owl_view *v)
{
return(v->style);
}
const char *owl_view_get_style_name(const owl_view *v) {
return(owl_style_get_name(v->style));
}
owl_message *owl_view_get_element(const owl_view *v, int index)
{
return owl_messagelist_get_element(v->ml, index);
}
void owl_view_delete_element(owl_view *v, int index)
{
owl_messagelist_delete_element(v->ml, index);
}
void owl_view_undelete_element(owl_view *v, int index)
{
owl_messagelist_undelete_element(v->ml, index);
}
int owl_view_get_size(const owl_view *v)
{
return owl_messagelist_get_size(v->ml);
}
/* Returns the position in the view with a message closest
* to the passed msgid. */
int owl_view_get_nearest_to_msgid(const owl_view *v, int targetid)
{
int first, last, mid = 0, max, bestdist, curid = 0;
first = 0;
last = max = owl_view_get_size(v) - 1;
while (first <= last) {
mid = (first + last) / 2;
curid = owl_message_get_id(owl_view_get_element(v, mid));
if (curid == targetid) {
return(mid);
} else if (curid < targetid) {
first = mid + 1;
} else {
last = mid - 1;
}
}
bestdist = abs(targetid-curid);
if (curid < targetid && mid+1 < max) {
curid = owl_message_get_id(owl_view_get_element(v, mid+1));
mid = (bestdist < abs(targetid-curid)) ? mid : mid+1;
}
else if (curid > targetid && mid-1 >= 0) {
curid = owl_message_get_id(owl_view_get_element(v, mid-1));
mid = (bestdist < abs(targetid-curid)) ? mid : mid-1;
}
return mid;
}
int owl_view_get_nearest_to_saved(const owl_view *v)
{
int cachedid;
cachedid = v->cachedmsgid;
if (cachedid<0) return(0);
return (owl_view_get_nearest_to_msgid(v, cachedid));
}
void owl_view_save_curmsgid(owl_view *v, int curid)
{
v->cachedmsgid = curid;
}
/* fmtext should already be initialized */
void owl_view_to_fmtext(const owl_view *v, owl_fmtext *fm)
{
owl_fmtext_append_normal(fm, "Name: ");
owl_fmtext_append_normal(fm, v->name);
owl_fmtext_append_normal(fm, "\n");
owl_fmtext_append_normal(fm, "Filter: ");
owl_fmtext_append_normal(fm, owl_filter_get_name(v->filter));
owl_fmtext_append_normal(fm, "\n");
owl_fmtext_append_normal(fm, "Style: ");
owl_fmtext_append_normal(fm, owl_style_get_name(v->style));
owl_fmtext_append_normal(fm, "\n");
}
const char *owl_view_get_filtname(const owl_view *v)
{
return(owl_filter_get_name(v->filter));
}
void owl_view_cleanup(owl_view *v)
{
owl_messagelist_delete(v->ml, false);
g_free(v->name);
}