source: view.c @ ebc6f77

release-1.9
Last change on this file since ebc6f77 was 219f52c, checked in by Jason Gross <jgross@mit.edu>, 10 years ago
Allocate owl_messagelist on the heap This lets us return NULL from owl_global_get_messagelist before the messagelist has been initilized. Since g has static duration, g.ml is automatically initialized to NULL.
  • Property mode set to 100644
File size: 3.7 KB
RevLine 
[7d4fbcd]1#include "owl.h"
2
[1fdab04]3void owl_view_create(owl_view *v, const char *name, owl_filter *f, const owl_style *s)
[c3ab155]4{
[d4927a7]5  v->name=g_strdup(name);
[7d4fbcd]6  v->filter=f;
[c3ab155]7  v->style=s;
[219f52c]8  v->ml = owl_messagelist_new();
[7d4fbcd]9  owl_view_recalculate(v);
10}
11
[9e5c9f3]12const char *owl_view_get_name(const owl_view *v)
[ef56a67]13{
14  return(v->name);
15}
[c3ab155]16
17/* if the message matches the filter then add to view */
18void owl_view_consider_message(owl_view *v, owl_message *m)
19{
[7d4fbcd]20  if (owl_filter_message_match(v->filter, m)) {
[219f52c]21    owl_messagelist_append_element(v->ml, m);
[7d4fbcd]22  }
23}
24
[c3ab155]25/* remove all messages, add all the global messages that match the
26 * filter.
27 */
28void owl_view_recalculate(owl_view *v)
29{
[7d4fbcd]30  int i, j;
[3eb599d]31  const owl_messagelist *gml;
[7d4fbcd]32  owl_message *m;
33
34  gml=owl_global_get_msglist(&g);
35
[fc8a87a]36  /* nuke the old list, don't free the messages */
[219f52c]37  owl_messagelist_delete(v->ml, false);
38  v->ml = owl_messagelist_new();
[7d4fbcd]39
40  /* find all the messages we want */
41  j=owl_messagelist_get_size(gml);
42  for (i=0; i<j; i++) {
43    m=owl_messagelist_get_element(gml, i);
44    if (owl_filter_message_match(v->filter, m)) {
[219f52c]45      owl_messagelist_append_element(v->ml, m);
[7d4fbcd]46    }
47  }
48}
49
[c3ab155]50void owl_view_new_filter(owl_view *v, owl_filter *f)
51{
52  v->filter=f;
53  owl_view_recalculate(v);
54}
55
[1fdab04]56void owl_view_set_style(owl_view *v, const owl_style *s)
[c3ab155]57{
58  v->style=s;
59}
60
[9e5c9f3]61const owl_style *owl_view_get_style(const owl_view *v)
[ef56a67]62{
63  return(v->style);
64}
65
[9e5c9f3]66const char *owl_view_get_style_name(const owl_view *v) {
[f1e629d]67  return(owl_style_get_name(v->style));
68}
69
[9e5c9f3]70owl_message *owl_view_get_element(const owl_view *v, int index)
[c3ab155]71{
[219f52c]72  return owl_messagelist_get_element(v->ml, index);
[7d4fbcd]73}
74
[c3ab155]75void owl_view_delete_element(owl_view *v, int index)
76{
[219f52c]77  owl_messagelist_delete_element(v->ml, index);
[7d4fbcd]78}
79
[c3ab155]80void owl_view_undelete_element(owl_view *v, int index)
81{
[219f52c]82  owl_messagelist_undelete_element(v->ml, index);
[7d4fbcd]83}
84
[9e5c9f3]85int owl_view_get_size(const owl_view *v)
[c3ab155]86{
[219f52c]87  return owl_messagelist_get_size(v->ml);
[7d4fbcd]88}
89
[59cf91c]90/* Returns the position in the view with a message closest
91 * to the passed msgid. */
[9e5c9f3]92int owl_view_get_nearest_to_msgid(const owl_view *v, int targetid)
[c3ab155]93{
[801b7ac]94  int first, last, mid = 0, max, bestdist, curid = 0;
95
96  first = 0;
97  last = max = owl_view_get_size(v) - 1;
98  while (first <= last) {
99    mid = (first + last) / 2;
100    curid = owl_message_get_id(owl_view_get_element(v, mid));
101    if (curid == targetid) {
102      return(mid);
103    } else if (curid < targetid) {
104      first = mid + 1;
105    } else {
106      last = mid - 1;
[59cf91c]107    }
108  }
[801b7ac]109  bestdist = abs(targetid-curid);
110  if (curid < targetid && mid+1 < max) {
111    curid = owl_message_get_id(owl_view_get_element(v, mid+1));
112    mid = (bestdist < abs(targetid-curid)) ? mid : mid+1;
113  }
114  else if (curid > targetid && mid-1 >= 0) {
115    curid = owl_message_get_id(owl_view_get_element(v, mid-1));
116    mid = (bestdist < abs(targetid-curid)) ? mid : mid-1;
117  }
118  return mid;
[59cf91c]119}
120
[9e5c9f3]121int owl_view_get_nearest_to_saved(const owl_view *v)
[c3ab155]122{
[3a2daac]123  int cachedid;
124
[c3a47c9]125  cachedid = v->cachedmsgid;
[59cf91c]126  if (cachedid<0) return(0);
[3a2daac]127  return (owl_view_get_nearest_to_msgid(v, cachedid));
[59cf91c]128}
129
[c3ab155]130void owl_view_save_curmsgid(owl_view *v, int curid)
131{
[c3a47c9]132  v->cachedmsgid = curid;
[59cf91c]133}
134
[ef56a67]135/* fmtext should already be initialized */
[9e5c9f3]136void owl_view_to_fmtext(const owl_view *v, owl_fmtext *fm)
[ef56a67]137{
138  owl_fmtext_append_normal(fm, "Name: ");
139  owl_fmtext_append_normal(fm, v->name);
140  owl_fmtext_append_normal(fm, "\n");
141
142  owl_fmtext_append_normal(fm, "Filter: ");
143  owl_fmtext_append_normal(fm, owl_filter_get_name(v->filter));
144  owl_fmtext_append_normal(fm, "\n");
145
146  owl_fmtext_append_normal(fm, "Style: ");
147  owl_fmtext_append_normal(fm, owl_style_get_name(v->style));
148  owl_fmtext_append_normal(fm, "\n");
149}
150
[9e5c9f3]151const char *owl_view_get_filtname(const owl_view *v)
[c3ab155]152{
[7d4fbcd]153  return(owl_filter_get_name(v->filter));
154}
155
[94b0b1d]156void owl_view_cleanup(owl_view *v)
[c3ab155]157{
[219f52c]158  owl_messagelist_delete(v->ml, false);
[3b8a563]159  g_free(v->name);
[7d4fbcd]160}
Note: See TracBrowser for help on using the repository browser.