source: messagelist.c @ 5c2ef5b

Last change on this file since 5c2ef5b was 5c2ef5b, checked in by Jason Gross <jgross@mit.edu>, 13 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.3 KB
RevLine 
[7d4fbcd]1#include "owl.h"
2#include <stdlib.h>
3#include <string.h>
4
[fc8a87a]5void owl_messagelist_create(owl_messagelist *ml)
[bd3f232]6{
[fc8a87a]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);
[7d4fbcd]15}
16
[3eb599d]17int owl_messagelist_get_size(const owl_messagelist *ml)
[bd3f232]18{
[fc8a87a]19  return ml->list->len;
[7d4fbcd]20}
21
[3eb599d]22void *owl_messagelist_get_element(const owl_messagelist *ml, int n)
[bd3f232]23{
[5c2ef5b]24  if (n >= ml->list->len) return NULL;
[fc8a87a]25  return ml->list->pdata[n];
[7d4fbcd]26}
27
[3eb599d]28owl_message *owl_messagelist_get_by_id(const owl_messagelist *ml, int target_id)
[bd3f232]29{
[7d4fbcd]30  /* return the message with id == 'id'.  If it doesn't exist return NULL. */
[0c8ab5e]31  int first, last, mid, msg_id;
[7d4fbcd]32  owl_message *m;
33
[0c8ab5e]34  first = 0;
[fc8a87a]35  last = ml->list->len - 1;
[0c8ab5e]36  while (first <= last) {
37    mid = (first + last) / 2;
[fc8a87a]38    m = ml->list->pdata[mid];
[0c8ab5e]39    msg_id = owl_message_get_id(m);
40    if (msg_id == target_id) {
41      return(m);
42    } else if (msg_id < target_id) {
43      first = mid + 1;
44    } else {
45      last = mid - 1;
46    }
[7d4fbcd]47  }
48  return(NULL);
49}
50
[d427f08]51void owl_messagelist_append_element(owl_messagelist *ml, void *element)
[bd3f232]52{
[fc8a87a]53  g_ptr_array_add(ml->list, element);
[7d4fbcd]54}
55
56/* do we really still want this? */
[bd3f232]57int owl_messagelist_delete_element(owl_messagelist *ml, int n)
58{
[7d4fbcd]59  /* mark a message as deleted */
[fc8a87a]60  owl_message_mark_delete(ml->list->pdata[n]);
[7d4fbcd]61  return(0);
62}
63
[bd3f232]64int owl_messagelist_undelete_element(owl_messagelist *ml, int n)
65{
[7d4fbcd]66  /* mark a message as deleted */
[fc8a87a]67  owl_message_unmark_delete(ml->list->pdata[n]);
[7d4fbcd]68  return(0);
69}
70
[bd3f232]71int owl_messagelist_expunge(owl_messagelist *ml)
72{
[7d4fbcd]73  /* expunge deleted messages */
[fc8a87a]74  int i;
75  GPtrArray *newlist;
[7d4fbcd]76  owl_message *m;
77
[fc8a87a]78  newlist = g_ptr_array_new();
[7d4fbcd]79  /*create a new list without messages marked as deleted */
[fc8a87a]80  for (i = 0; i < ml->list->len; i++) {
81    m = ml->list->pdata[i];
[7d4fbcd]82    if (owl_message_is_delete(m)) {
[91634ec]83      owl_message_delete(m);
[7d4fbcd]84    } else {
[fc8a87a]85      g_ptr_array_add(newlist, m);
[7d4fbcd]86    }
87  }
88
89  /* free the old list */
[fc8a87a]90  g_ptr_array_free(ml->list, true);
[7d4fbcd]91
92  /* copy the new list to the old list */
[66a8cd6]93  ml->list = newlist;
[7d4fbcd]94
95  return(0);
96}
[bd3f232]97
[3eb599d]98void owl_messagelist_invalidate_formats(const owl_messagelist *ml)
[bd3f232]99{
[fc8a87a]100  int i;
[bd3f232]101  owl_message *m;
102
[fc8a87a]103  for (i = 0; i < ml->list->len; i++) {
104    m = ml->list->pdata[i];
[bd3f232]105    owl_message_invalidate_format(m);
106  }
107}
Note: See TracBrowser for help on using the repository browser.