source: messagelist.c @ e6d7e4e

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