source: messagelist.c @ b8a3e00

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