source: view.c @ ab6d8f0

release-1.10release-1.8release-1.9
Last change on this file since ab6d8f0 was d4927a7, checked in by Anders Kaseorg <andersk@mit.edu>, 14 years ago
Replace owl_strdup with g_strdup. Signed-off-by: Anders Kaseorg <andersk@mit.edu> Reviewed-by: Karl Ramm <kcr@mit.edu>
  • Property mode set to 100644
File size: 3.8 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=g_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_cleanup(&ml->list, NULL);
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 = v->cachedmsgid;
129  if (cachedid<0) return(0);
130  return (owl_view_get_nearest_to_msgid(v, cachedid));
131}
132
133void owl_view_save_curmsgid(owl_view *v, int curid)
134{
135  v->cachedmsgid = curid;
136}
137
138/* fmtext should already be initialized */
139void owl_view_to_fmtext(const owl_view *v, owl_fmtext *fm)
140{
141  owl_fmtext_append_normal(fm, "Name: ");
142  owl_fmtext_append_normal(fm, v->name);
143  owl_fmtext_append_normal(fm, "\n");
144
145  owl_fmtext_append_normal(fm, "Filter: ");
146  owl_fmtext_append_normal(fm, owl_filter_get_name(v->filter));
147  owl_fmtext_append_normal(fm, "\n");
148
149  owl_fmtext_append_normal(fm, "Style: ");
150  owl_fmtext_append_normal(fm, owl_style_get_name(v->style));
151  owl_fmtext_append_normal(fm, "\n");
152}
153
154const char *owl_view_get_filtname(const owl_view *v)
155{
156  return(owl_filter_get_name(v->filter));
157}
158
159void owl_view_cleanup(owl_view *v)
160{
161  owl_list_cleanup(&v->ml.list, NULL);
162  if (v->name) g_free(v->name);
163}
Note: See TracBrowser for help on using the repository browser.