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 | return ml->list->pdata[n]; |
---|
25 | } |
---|
26 | |
---|
27 | int owl_messagelist_get_index_by_id(const owl_messagelist *ml, int target_id) |
---|
28 | { |
---|
29 | /* return the message index with id == 'id'. If it doesn't exist return -1. */ |
---|
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 mid; |
---|
41 | } else if (msg_id < target_id) { |
---|
42 | first = mid + 1; |
---|
43 | } else { |
---|
44 | last = mid - 1; |
---|
45 | } |
---|
46 | } |
---|
47 | return -1; |
---|
48 | } |
---|
49 | |
---|
50 | owl_message *owl_messagelist_get_by_id(const owl_messagelist *ml, int target_id) |
---|
51 | { |
---|
52 | /* return the message with id == 'id'. If it doesn't exist return NULL. */ |
---|
53 | int n = owl_messagelist_get_index_by_id(ml, target_id); |
---|
54 | if (n < 0) return NULL; |
---|
55 | return ml->list->pdata[n]; |
---|
56 | } |
---|
57 | |
---|
58 | void owl_messagelist_append_element(owl_messagelist *ml, void *element) |
---|
59 | { |
---|
60 | g_ptr_array_add(ml->list, element); |
---|
61 | } |
---|
62 | |
---|
63 | /* do we really still want this? */ |
---|
64 | int owl_messagelist_delete_element(owl_messagelist *ml, int n) |
---|
65 | { |
---|
66 | /* mark a message as deleted */ |
---|
67 | owl_message_mark_delete(ml->list->pdata[n]); |
---|
68 | return(0); |
---|
69 | } |
---|
70 | |
---|
71 | int owl_messagelist_undelete_element(owl_messagelist *ml, int n) |
---|
72 | { |
---|
73 | /* mark a message as deleted */ |
---|
74 | owl_message_unmark_delete(ml->list->pdata[n]); |
---|
75 | return(0); |
---|
76 | } |
---|
77 | |
---|
78 | void owl_messagelist_delete_and_expunge_element(owl_messagelist *ml, int n) |
---|
79 | { |
---|
80 | owl_message_delete(g_ptr_array_remove_index(ml->list, n)); |
---|
81 | } |
---|
82 | |
---|
83 | int owl_messagelist_expunge(owl_messagelist *ml) |
---|
84 | { |
---|
85 | /* expunge deleted messages */ |
---|
86 | int i; |
---|
87 | GPtrArray *newlist; |
---|
88 | owl_message *m; |
---|
89 | |
---|
90 | newlist = g_ptr_array_new(); |
---|
91 | /*create a new list without messages marked as deleted */ |
---|
92 | for (i = 0; i < ml->list->len; i++) { |
---|
93 | m = ml->list->pdata[i]; |
---|
94 | if (owl_message_is_delete(m)) { |
---|
95 | owl_message_delete(m); |
---|
96 | } else { |
---|
97 | g_ptr_array_add(newlist, m); |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | /* free the old list */ |
---|
102 | g_ptr_array_free(ml->list, true); |
---|
103 | |
---|
104 | /* copy the new list to the old list */ |
---|
105 | ml->list = newlist; |
---|
106 | |
---|
107 | return(0); |
---|
108 | } |
---|
109 | |
---|
110 | void owl_messagelist_invalidate_formats(const owl_messagelist *ml) |
---|
111 | { |
---|
112 | int i; |
---|
113 | owl_message *m; |
---|
114 | |
---|
115 | for (i = 0; i < ml->list->len; i++) { |
---|
116 | m = ml->list->pdata[i]; |
---|
117 | owl_message_invalidate_format(m); |
---|
118 | } |
---|
119 | } |
---|