source: view.c @ 53d6269

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