source: messagelist.c @ a38becd

release-1.10release-1.9
Last change on this file since a38becd 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
RevLine 
[7d4fbcd]1#include "owl.h"
2
[219f52c]3CALLER_OWN owl_messagelist *owl_messagelist_new(void)
[bd3f232]4{
[219f52c]5  owl_messagelist *ml = g_new(owl_messagelist, 1);
[fc8a87a]6  ml->list = g_ptr_array_new();
[219f52c]7  return ml;
[fc8a87a]8}
9
[219f52c]10void owl_messagelist_delete(owl_messagelist *ml, bool free_messages)
[fc8a87a]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);
[219f52c]15  g_free(ml);
[7d4fbcd]16}
17
[3eb599d]18int owl_messagelist_get_size(const owl_messagelist *ml)
[bd3f232]19{
[fc8a87a]20  return ml->list->len;
[7d4fbcd]21}
22
[3eb599d]23void *owl_messagelist_get_element(const owl_messagelist *ml, int n)
[bd3f232]24{
[901cee9]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;
[fc8a87a]30  return ml->list->pdata[n];
[7d4fbcd]31}
32
[efeec7f]33int owl_messagelist_get_index_by_id(const owl_messagelist *ml, int target_id)
[bd3f232]34{
[efeec7f]35  /* return the message index with id == 'id'.  If it doesn't exist return -1. */
[0c8ab5e]36  int first, last, mid, msg_id;
[7d4fbcd]37  owl_message *m;
38
[0c8ab5e]39  first = 0;
[fc8a87a]40  last = ml->list->len - 1;
[0c8ab5e]41  while (first <= last) {
42    mid = (first + last) / 2;
[fc8a87a]43    m = ml->list->pdata[mid];
[0c8ab5e]44    msg_id = owl_message_get_id(m);
45    if (msg_id == target_id) {
[efeec7f]46      return mid;
[0c8ab5e]47    } else if (msg_id < target_id) {
48      first = mid + 1;
49    } else {
50      last = mid - 1;
51    }
[7d4fbcd]52  }
[efeec7f]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];
[7d4fbcd]62}
63
[d427f08]64void owl_messagelist_append_element(owl_messagelist *ml, void *element)
[bd3f232]65{
[fc8a87a]66  g_ptr_array_add(ml->list, element);
[7d4fbcd]67}
68
69/* do we really still want this? */
[bd3f232]70int owl_messagelist_delete_element(owl_messagelist *ml, int n)
71{
[7d4fbcd]72  /* mark a message as deleted */
[fc8a87a]73  owl_message_mark_delete(ml->list->pdata[n]);
[7d4fbcd]74  return(0);
75}
76
[bd3f232]77int owl_messagelist_undelete_element(owl_messagelist *ml, int n)
78{
[7d4fbcd]79  /* mark a message as deleted */
[fc8a87a]80  owl_message_unmark_delete(ml->list->pdata[n]);
[7d4fbcd]81  return(0);
82}
83
[efeec7f]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
[bd3f232]89int owl_messagelist_expunge(owl_messagelist *ml)
90{
[7d4fbcd]91  /* expunge deleted messages */
[fc8a87a]92  int i;
93  GPtrArray *newlist;
[7d4fbcd]94  owl_message *m;
95
[fc8a87a]96  newlist = g_ptr_array_new();
[7d4fbcd]97  /*create a new list without messages marked as deleted */
[fc8a87a]98  for (i = 0; i < ml->list->len; i++) {
99    m = ml->list->pdata[i];
[7d4fbcd]100    if (owl_message_is_delete(m)) {
[91634ec]101      owl_message_delete(m);
[7d4fbcd]102    } else {
[fc8a87a]103      g_ptr_array_add(newlist, m);
[7d4fbcd]104    }
105  }
106
107  /* free the old list */
[fc8a87a]108  g_ptr_array_free(ml->list, true);
[7d4fbcd]109
110  /* copy the new list to the old list */
[66a8cd6]111  ml->list = newlist;
[7d4fbcd]112
113  return(0);
114}
[bd3f232]115
[3eb599d]116void owl_messagelist_invalidate_formats(const owl_messagelist *ml)
[bd3f232]117{
[fc8a87a]118  int i;
[bd3f232]119  owl_message *m;
120
[fc8a87a]121  for (i = 0; i < ml->list->len; i++) {
122    m = ml->list->pdata[i];
[bd3f232]123    owl_message_invalidate_format(m);
124  }
125}
Note: See TracBrowser for help on using the repository browser.