source: view.c @ 4d26776

release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 4d26776 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
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 */
[4d86e06]40  owl_list_free_simple(&ml->list);
[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
128  cachedid=owl_filter_get_cachedmsgid(v->filter);
[59cf91c]129  if (cachedid<0) return(0);
[3a2daac]130  return (owl_view_get_nearest_to_msgid(v, cachedid));
[59cf91c]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. */
[c3ab155]135void owl_view_save_curmsgid(owl_view *v, int curid)
136{
[59cf91c]137  owl_filter_set_cachedmsgid(v->filter, curid);
138}
139
[ef56a67]140/* fmtext should already be initialized */
[9e5c9f3]141void owl_view_to_fmtext(const owl_view *v, owl_fmtext *fm)
[ef56a67]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
[9e5c9f3]156const char *owl_view_get_filtname(const owl_view *v)
[c3ab155]157{
[7d4fbcd]158  return(owl_filter_get_name(v->filter));
159}
160
[c3ab155]161void owl_view_free(owl_view *v)
162{
[4d86e06]163  owl_list_free_simple(&v->ml.list);
[c3ab155]164  if (v->name) owl_free(v->name);
[7d4fbcd]165}
Note: See TracBrowser for help on using the repository browser.