1 | #include "owl.h" |
---|
2 | |
---|
3 | void owl_messagelist_create(owl_messagelist *ml) |
---|
4 | { |
---|
5 | ml->list = g_ptr_array_new(); |
---|
6 | } |
---|
7 | |
---|
8 | void owl_messagelist_cleanup(owl_messagelist *ml, bool free_messages) |
---|
9 | { |
---|
10 | if (free_messages) |
---|
11 | g_ptr_array_foreach(ml->list, (GFunc)owl_message_delete, NULL); |
---|
12 | g_ptr_array_free(ml->list, true); |
---|
13 | } |
---|
14 | |
---|
15 | int owl_messagelist_get_size(const owl_messagelist *ml) |
---|
16 | { |
---|
17 | return ml->list->len; |
---|
18 | } |
---|
19 | |
---|
20 | void *owl_messagelist_get_element(const owl_messagelist *ml, int n) |
---|
21 | { |
---|
22 | /* we assume things like owl_view_get_element(v, owl_global_get_curmsg(&g)) |
---|
23 | * work even when there are no messages in the message list. So don't |
---|
24 | * segfault if someone asks for the zeroth element of an empty list. |
---|
25 | */ |
---|
26 | if (n >= ml->list->len) return NULL; |
---|
27 | return ml->list->pdata[n]; |
---|
28 | } |
---|
29 | |
---|
30 | int owl_messagelist_get_index_by_id(const owl_messagelist *ml, int target_id) |
---|
31 | { |
---|
32 | /* return the message index with id == 'id'. If it doesn't exist return -1. */ |
---|
33 | int first, last, mid, msg_id; |
---|
34 | owl_message *m; |
---|
35 | |
---|
36 | first = 0; |
---|
37 | last = ml->list->len - 1; |
---|
38 | while (first <= last) { |
---|
39 | mid = (first + last) / 2; |
---|
40 | m = ml->list->pdata[mid]; |
---|
41 | msg_id = owl_message_get_id(m); |
---|
42 | if (msg_id == target_id) { |
---|
43 | return mid; |
---|
44 | } else if (msg_id < target_id) { |
---|
45 | first = mid + 1; |
---|
46 | } else { |
---|
47 | last = mid - 1; |
---|
48 | } |
---|
49 | } |
---|
50 | return -1; |
---|
51 | } |
---|
52 | |
---|
53 | owl_message *owl_messagelist_get_by_id(const owl_messagelist *ml, int target_id) |
---|
54 | { |
---|
55 | /* return the message with id == 'id'. If it doesn't exist return NULL. */ |
---|
56 | int n = owl_messagelist_get_index_by_id(ml, target_id); |
---|
57 | if (n < 0) return NULL; |
---|
58 | return ml->list->pdata[n]; |
---|
59 | } |
---|
60 | |
---|
61 | void owl_messagelist_append_element(owl_messagelist *ml, void *element) |
---|
62 | { |
---|
63 | g_ptr_array_add(ml->list, element); |
---|
64 | } |
---|
65 | |
---|
66 | /* do we really still want this? */ |
---|
67 | int owl_messagelist_delete_element(owl_messagelist *ml, int n) |
---|
68 | { |
---|
69 | /* mark a message as deleted */ |
---|
70 | owl_message_mark_delete(ml->list->pdata[n]); |
---|
71 | return(0); |
---|
72 | } |
---|
73 | |
---|
74 | int owl_messagelist_undelete_element(owl_messagelist *ml, int n) |
---|
75 | { |
---|
76 | /* mark a message as deleted */ |
---|
77 | owl_message_unmark_delete(ml->list->pdata[n]); |
---|
78 | return(0); |
---|
79 | } |
---|
80 | |
---|
81 | void owl_messagelist_delete_and_expunge_element(owl_messagelist *ml, int n) |
---|
82 | { |
---|
83 | owl_message_delete(g_ptr_array_remove_index(ml->list, n)); |
---|
84 | } |
---|
85 | |
---|
86 | int owl_messagelist_expunge(owl_messagelist *ml) |
---|
87 | { |
---|
88 | /* expunge deleted messages */ |
---|
89 | int i; |
---|
90 | GPtrArray *newlist; |
---|
91 | owl_message *m; |
---|
92 | |
---|
93 | newlist = g_ptr_array_new(); |
---|
94 | /*create a new list without messages marked as deleted */ |
---|
95 | for (i = 0; i < ml->list->len; i++) { |
---|
96 | m = ml->list->pdata[i]; |
---|
97 | if (owl_message_is_delete(m)) { |
---|
98 | owl_message_delete(m); |
---|
99 | } else { |
---|
100 | g_ptr_array_add(newlist, m); |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | /* free the old list */ |
---|
105 | g_ptr_array_free(ml->list, true); |
---|
106 | |
---|
107 | /* copy the new list to the old list */ |
---|
108 | ml->list = newlist; |
---|
109 | |
---|
110 | return(0); |
---|
111 | } |
---|
112 | |
---|
113 | void owl_messagelist_invalidate_formats(const owl_messagelist *ml) |
---|
114 | { |
---|
115 | int i; |
---|
116 | owl_message *m; |
---|
117 | |
---|
118 | for (i = 0; i < ml->list->len; i++) { |
---|
119 | m = ml->list->pdata[i]; |
---|
120 | owl_message_invalidate_format(m); |
---|
121 | } |
---|
122 | } |
---|