source: messagelist.c @ 901cee9

release-1.8release-1.9
Last change on this file since 901cee9 was 901cee9, checked in by Jason Gross <jgross@mit.edu>, 10 years ago
Don't segfault when you expunge all messages from the messagelist When we used owl_list, trying to get an element from an empty list would return NULL. When we moved to GPtrArray, we dropped this checking, without dropping the assumption that, e.g., owl_view_get_element(v, owl_global_get_curmsg(&g)); works everywhere. This commit adds the logic back in to deal with this case. Additionally, don't segfault on things like :unpunt -1.
  • Property mode set to 100644
File size: 2.5 KB
Line 
1#include "owl.h"
2#include <stdlib.h>
3#include <string.h>
4
5void owl_messagelist_create(owl_messagelist *ml)
6{
7  ml->list = g_ptr_array_new();
8}
9
10void owl_messagelist_cleanup(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}
16
17int owl_messagelist_get_size(const owl_messagelist *ml)
18{
19  return ml->list->len;
20}
21
22void *owl_messagelist_get_element(const owl_messagelist *ml, int n)
23{
24  /* we assume things like owl_view_get_element(v, owl_global_get_curmsg(&g))
25   * work even when there are no messages in the message list.  So don't
26   * segfault if someone asks for the zeroth element of an empty list.
27   */
28  if (n >= ml->list->len) return NULL;
29  return ml->list->pdata[n];
30}
31
32owl_message *owl_messagelist_get_by_id(const owl_messagelist *ml, int target_id)
33{
34  /* return the message with id == 'id'.  If it doesn't exist return NULL. */
35  int first, last, mid, msg_id;
36  owl_message *m;
37
38  first = 0;
39  last = ml->list->len - 1;
40  while (first <= last) {
41    mid = (first + last) / 2;
42    m = ml->list->pdata[mid];
43    msg_id = owl_message_get_id(m);
44    if (msg_id == target_id) {
45      return(m);
46    } else if (msg_id < target_id) {
47      first = mid + 1;
48    } else {
49      last = mid - 1;
50    }
51  }
52  return(NULL);
53}
54
55void owl_messagelist_append_element(owl_messagelist *ml, void *element)
56{
57  g_ptr_array_add(ml->list, element);
58}
59
60/* do we really still want this? */
61int owl_messagelist_delete_element(owl_messagelist *ml, int n)
62{
63  /* mark a message as deleted */
64  owl_message_mark_delete(ml->list->pdata[n]);
65  return(0);
66}
67
68int owl_messagelist_undelete_element(owl_messagelist *ml, int n)
69{
70  /* mark a message as deleted */
71  owl_message_unmark_delete(ml->list->pdata[n]);
72  return(0);
73}
74
75int owl_messagelist_expunge(owl_messagelist *ml)
76{
77  /* expunge deleted messages */
78  int i;
79  GPtrArray *newlist;
80  owl_message *m;
81
82  newlist = g_ptr_array_new();
83  /*create a new list without messages marked as deleted */
84  for (i = 0; i < ml->list->len; i++) {
85    m = ml->list->pdata[i];
86    if (owl_message_is_delete(m)) {
87      owl_message_delete(m);
88    } else {
89      g_ptr_array_add(newlist, m);
90    }
91  }
92
93  /* free the old list */
94  g_ptr_array_free(ml->list, true);
95
96  /* copy the new list to the old list */
97  ml->list = newlist;
98
99  return(0);
100}
101
102void owl_messagelist_invalidate_formats(const owl_messagelist *ml)
103{
104  int i;
105  owl_message *m;
106
107  for (i = 0; i < ml->list->len; i++) {
108    m = ml->list->pdata[i];
109    owl_message_invalidate_format(m);
110  }
111}
Note: See TracBrowser for help on using the repository browser.