source: messagelist.c @ 731e921

release-1.10release-1.9
Last change on this file since 731e921 was 219f52c, checked in by Jason Gross <jgross@mit.edu>, 13 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.0 KB
Line 
1#include "owl.h"
2
3CALLER_OWN owl_messagelist *owl_messagelist_new(void)
4{
5  owl_messagelist *ml = g_new(owl_messagelist, 1);
6  ml->list = g_ptr_array_new();
7  return ml;
8}
9
10void owl_messagelist_delete(owl_messagelist *ml, bool free_messages)
11{
12  if (free_messages)
13    g_ptr_array_foreach(ml->list, (GFunc)owl_message_delete, NULL);
14  g_ptr_array_free(ml->list, true);
15  g_free(ml);
16}
17
18int owl_messagelist_get_size(const owl_messagelist *ml)
19{
20  return ml->list->len;
21}
22
23void *owl_messagelist_get_element(const owl_messagelist *ml, int n)
24{
25  /* we assume things like owl_view_get_element(v, owl_global_get_curmsg(&g))
26   * work even when there are no messages in the message list.  So don't
27   * segfault if someone asks for the zeroth element of an empty list.
28   */
29  if (n >= ml->list->len) return NULL;
30  return ml->list->pdata[n];
31}
32
33int owl_messagelist_get_index_by_id(const owl_messagelist *ml, int target_id)
34{
35  /* return the message index with id == 'id'.  If it doesn't exist return -1. */
36  int first, last, mid, msg_id;
37  owl_message *m;
38
39  first = 0;
40  last = ml->list->len - 1;
41  while (first <= last) {
42    mid = (first + last) / 2;
43    m = ml->list->pdata[mid];
44    msg_id = owl_message_get_id(m);
45    if (msg_id == target_id) {
46      return mid;
47    } else if (msg_id < target_id) {
48      first = mid + 1;
49    } else {
50      last = mid - 1;
51    }
52  }
53  return -1;
54}
55
56owl_message *owl_messagelist_get_by_id(const owl_messagelist *ml, int target_id)
57{
58  /* return the message with id == 'id'.  If it doesn't exist return NULL. */
59  int n = owl_messagelist_get_index_by_id(ml, target_id);
60  if (n < 0) return NULL;
61  return ml->list->pdata[n];
62}
63
64void owl_messagelist_append_element(owl_messagelist *ml, void *element)
65{
66  g_ptr_array_add(ml->list, element);
67}
68
69/* do we really still want this? */
70int owl_messagelist_delete_element(owl_messagelist *ml, int n)
71{
72  /* mark a message as deleted */
73  owl_message_mark_delete(ml->list->pdata[n]);
74  return(0);
75}
76
77int owl_messagelist_undelete_element(owl_messagelist *ml, int n)
78{
79  /* mark a message as deleted */
80  owl_message_unmark_delete(ml->list->pdata[n]);
81  return(0);
82}
83
84void owl_messagelist_delete_and_expunge_element(owl_messagelist *ml, int n)
85{
86  owl_message_delete(g_ptr_array_remove_index(ml->list, n));
87}
88
89int owl_messagelist_expunge(owl_messagelist *ml)
90{
91  /* expunge deleted messages */
92  int i;
93  GPtrArray *newlist;
94  owl_message *m;
95
96  newlist = g_ptr_array_new();
97  /*create a new list without messages marked as deleted */
98  for (i = 0; i < ml->list->len; i++) {
99    m = ml->list->pdata[i];
100    if (owl_message_is_delete(m)) {
101      owl_message_delete(m);
102    } else {
103      g_ptr_array_add(newlist, m);
104    }
105  }
106
107  /* free the old list */
108  g_ptr_array_free(ml->list, true);
109
110  /* copy the new list to the old list */
111  ml->list = newlist;
112
113  return(0);
114}
115
116void owl_messagelist_invalidate_formats(const owl_messagelist *ml)
117{
118  int i;
119  owl_message *m;
120
121  for (i = 0; i < ml->list->len; i++) {
122    m = ml->list->pdata[i];
123    owl_message_invalidate_format(m);
124  }
125}
Note: See TracBrowser for help on using the repository browser.