source: view.c @ ecc958e

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