source: view.c @ e2cbbbe

release-1.10release-1.6release-1.7release-1.8release-1.9
Last change on this file since e2cbbbe was 94b0b1d, checked in by Anders Kaseorg <andersk@mit.edu>, 15 years ago
Rename owl_view_free to owl_view_cleanup. Signed-off-by: Anders Kaseorg <andersk@mit.edu> Reviewed-by: Nelson Elhage <nelhage@mit.edu>
  • Property mode set to 100644
File size: 3.8 KB
RevLine 
[5f37310]1#include <stdlib.h>
[7d4fbcd]2#include "owl.h"
3
[1fdab04]4void owl_view_create(owl_view *v, const char *name, owl_filter *f, const owl_style *s)
[c3ab155]5{
6  v->name=owl_strdup(name);
[7d4fbcd]7  v->filter=f;
[c3ab155]8  v->style=s;
[7d4fbcd]9  owl_messagelist_create(&(v->ml));
10  owl_view_recalculate(v);
11}
12
[9e5c9f3]13const char *owl_view_get_name(const owl_view *v)
[ef56a67]14{
15  return(v->name);
16}
[c3ab155]17
18/* if the message matches the filter then add to view */
19void owl_view_consider_message(owl_view *v, owl_message *m)
20{
[7d4fbcd]21  if (owl_filter_message_match(v->filter, m)) {
22    owl_messagelist_append_element(&(v->ml), m);
23  }
24}
25
[c3ab155]26/* remove all messages, add all the global messages that match the
27 * filter.
28 */
29void owl_view_recalculate(owl_view *v)
30{
[7d4fbcd]31  int i, j;
[3eb599d]32  const owl_messagelist *gml;
[7d4fbcd]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 */
[5e5f08f]40  owl_list_cleanup(&ml->list, NULL);
[7d4fbcd]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
[c3ab155]53void owl_view_new_filter(owl_view *v, owl_filter *f)
54{
55  v->filter=f;
56  owl_view_recalculate(v);
57}
58
[1fdab04]59void owl_view_set_style(owl_view *v, const owl_style *s)
[c3ab155]60{
61  v->style=s;
62}
63
[9e5c9f3]64const owl_style *owl_view_get_style(const owl_view *v)
[ef56a67]65{
66  return(v->style);
67}
68
[9e5c9f3]69const char *owl_view_get_style_name(const owl_view *v) {
[f1e629d]70  return(owl_style_get_name(v->style));
71}
72
[9e5c9f3]73owl_message *owl_view_get_element(const owl_view *v, int index)
[c3ab155]74{
[7d4fbcd]75  return(owl_messagelist_get_element(&(v->ml), index));
76}
77
[c3ab155]78void owl_view_delete_element(owl_view *v, int index)
79{
[7d4fbcd]80  owl_messagelist_delete_element(&(v->ml), index);
81}
82
[c3ab155]83void owl_view_undelete_element(owl_view *v, int index)
84{
[7d4fbcd]85  owl_messagelist_undelete_element(&(v->ml), index);
86}
87
[9e5c9f3]88int owl_view_get_size(const owl_view *v)
[c3ab155]89{
[7d4fbcd]90  return(owl_messagelist_get_size(&(v->ml)));
91}
92
[59cf91c]93/* Returns the position in the view with a message closest
94 * to the passed msgid. */
[9e5c9f3]95int owl_view_get_nearest_to_msgid(const owl_view *v, int targetid)
[c3ab155]96{
[801b7ac]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;
[59cf91c]110    }
111  }
[801b7ac]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;
[59cf91c]122}
123
[9e5c9f3]124int owl_view_get_nearest_to_saved(const owl_view *v)
[c3ab155]125{
[3a2daac]126  int cachedid;
127
[c3a47c9]128  cachedid = v->cachedmsgid;
[59cf91c]129  if (cachedid<0) return(0);
[3a2daac]130  return (owl_view_get_nearest_to_msgid(v, cachedid));
[59cf91c]131}
132
[c3ab155]133void owl_view_save_curmsgid(owl_view *v, int curid)
134{
[c3a47c9]135  v->cachedmsgid = curid;
[59cf91c]136}
137
[ef56a67]138/* fmtext should already be initialized */
[9e5c9f3]139void owl_view_to_fmtext(const owl_view *v, owl_fmtext *fm)
[ef56a67]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
[9e5c9f3]154const char *owl_view_get_filtname(const owl_view *v)
[c3ab155]155{
[7d4fbcd]156  return(owl_filter_get_name(v->filter));
157}
158
[94b0b1d]159void owl_view_cleanup(owl_view *v)
[c3ab155]160{
[5e5f08f]161  owl_list_cleanup(&v->ml.list, NULL);
[c3ab155]162  if (v->name) owl_free(v->name);
[7d4fbcd]163}
Note: See TracBrowser for help on using the repository browser.