source: view.c @ 6329cd5

release-1.10release-1.9
Last change on this file since 6329cd5 was f271129, checked in by Jason Gross <jgross@mit.edu>, 13 years ago
Fix up headers The additions to owl.h and some of the removals were done by Alejandro Sedeño <asedeno@mit.edu> in commit 77a0258b3919468fc9d7f7602588ac427ab36e6c. Notes: * I think owl.c lost the need for sys/time.h when we punted select() in favor of glib's main loop. * We don't actually need to include things like stdarg.h, stdio.h, glib/gstdio.h, glib-object.h. I think they get indirectly included via owl.h and/or glib.h. They're left in (or added in to) the files that use functions/types from them. * I'm not entirely sure what sys/socket.h is doing in message.c. It is there from the initial commit. I suspect it might have had something to do with the call to getnameinfo. message.c compiles without it, but http://pubs.opengroup.org/onlinepubs/009695399/functions/getnameinfo.html suggests that we're supposed to include it? *shrugs* I'm leaving it in, for now. (Rather, I'll leave one copy of the #include in.)
  • Property mode set to 100644
File size: 3.8 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  owl_messagelist_create(&(v->ml));
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_messagelist *ml;
33  owl_message *m;
34
35  gml=owl_global_get_msglist(&g);
36  ml=&(v->ml);
37
38  /* nuke the old list, don't free the messages */
39  owl_messagelist_cleanup(ml, false);
40  owl_messagelist_create(&(v->ml));
41
42  /* find all the messages we want */
43  j=owl_messagelist_get_size(gml);
44  for (i=0; i<j; i++) {
45    m=owl_messagelist_get_element(gml, i);
46    if (owl_filter_message_match(v->filter, m)) {
47      owl_messagelist_append_element(ml, m);
48    }
49  }
50}
51
52void owl_view_new_filter(owl_view *v, owl_filter *f)
53{
54  v->filter=f;
55  owl_view_recalculate(v);
56}
57
58void owl_view_set_style(owl_view *v, const owl_style *s)
59{
60  v->style=s;
61}
62
63const owl_style *owl_view_get_style(const owl_view *v)
64{
65  return(v->style);
66}
67
68const char *owl_view_get_style_name(const owl_view *v) {
69  return(owl_style_get_name(v->style));
70}
71
72owl_message *owl_view_get_element(const owl_view *v, int index)
73{
74  return(owl_messagelist_get_element(&(v->ml), index));
75}
76
77void owl_view_delete_element(owl_view *v, int index)
78{
79  owl_messagelist_delete_element(&(v->ml), index);
80}
81
82void owl_view_undelete_element(owl_view *v, int index)
83{
84  owl_messagelist_undelete_element(&(v->ml), index);
85}
86
87int owl_view_get_size(const owl_view *v)
88{
89  return(owl_messagelist_get_size(&(v->ml)));
90}
91
92/* Returns the position in the view with a message closest
93 * to the passed msgid. */
94int owl_view_get_nearest_to_msgid(const owl_view *v, int targetid)
95{
96  int first, last, mid = 0, max, bestdist, curid = 0;
97
98  first = 0;
99  last = max = owl_view_get_size(v) - 1;
100  while (first <= last) {
101    mid = (first + last) / 2;
102    curid = owl_message_get_id(owl_view_get_element(v, mid));
103    if (curid == targetid) {
104      return(mid);
105    } else if (curid < targetid) {
106      first = mid + 1;
107    } else {
108      last = mid - 1;
109    }
110  }
111  bestdist = abs(targetid-curid);
112  if (curid < targetid && mid+1 < max) {
113    curid = owl_message_get_id(owl_view_get_element(v, mid+1));
114    mid = (bestdist < abs(targetid-curid)) ? mid : mid+1;
115  }
116  else if (curid > targetid && mid-1 >= 0) {
117    curid = owl_message_get_id(owl_view_get_element(v, mid-1));
118    mid = (bestdist < abs(targetid-curid)) ? mid : mid-1;
119  }
120  return mid;
121}
122
123int owl_view_get_nearest_to_saved(const owl_view *v)
124{
125  int cachedid;
126
127  cachedid = v->cachedmsgid;
128  if (cachedid<0) return(0);
129  return (owl_view_get_nearest_to_msgid(v, cachedid));
130}
131
132void owl_view_save_curmsgid(owl_view *v, int curid)
133{
134  v->cachedmsgid = curid;
135}
136
137/* fmtext should already be initialized */
138void owl_view_to_fmtext(const owl_view *v, owl_fmtext *fm)
139{
140  owl_fmtext_append_normal(fm, "Name: ");
141  owl_fmtext_append_normal(fm, v->name);
142  owl_fmtext_append_normal(fm, "\n");
143
144  owl_fmtext_append_normal(fm, "Filter: ");
145  owl_fmtext_append_normal(fm, owl_filter_get_name(v->filter));
146  owl_fmtext_append_normal(fm, "\n");
147
148  owl_fmtext_append_normal(fm, "Style: ");
149  owl_fmtext_append_normal(fm, owl_style_get_name(v->style));
150  owl_fmtext_append_normal(fm, "\n");
151}
152
153const char *owl_view_get_filtname(const owl_view *v)
154{
155  return(owl_filter_get_name(v->filter));
156}
157
158void owl_view_cleanup(owl_view *v)
159{
160  owl_messagelist_cleanup(&v->ml, false);
161  g_free(v->name);
162}
Note: See TracBrowser for help on using the repository browser.