1 | #include "owl.h" |
---|
2 | #include <stdlib.h> |
---|
3 | #include <string.h> |
---|
4 | |
---|
5 | void owl_messagelist_create(owl_messagelist *ml) |
---|
6 | { |
---|
7 | ml->list = g_ptr_array_new(); |
---|
8 | } |
---|
9 | |
---|
10 | void 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 | |
---|
17 | int owl_messagelist_get_size(const owl_messagelist *ml) |
---|
18 | { |
---|
19 | return ml->list->len; |
---|
20 | } |
---|
21 | |
---|
22 | void *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 | |
---|
32 | owl_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 | |
---|
55 | void 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? */ |
---|
61 | int 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 | |
---|
68 | int 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 | |
---|
75 | int 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 | |
---|
102 | void 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 | } |
---|