source: messagelist.c @ a962f5c

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