[7d4fbcd] | 1 | #include "owl.h" |
---|
| 2 | |
---|
[fc8a87a] | 3 | void owl_messagelist_create(owl_messagelist *ml) |
---|
[bd3f232] | 4 | { |
---|
[fc8a87a] | 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); |
---|
[7d4fbcd] | 13 | } |
---|
| 14 | |
---|
[3eb599d] | 15 | int owl_messagelist_get_size(const owl_messagelist *ml) |
---|
[bd3f232] | 16 | { |
---|
[fc8a87a] | 17 | return ml->list->len; |
---|
[7d4fbcd] | 18 | } |
---|
| 19 | |
---|
[3eb599d] | 20 | void *owl_messagelist_get_element(const owl_messagelist *ml, int n) |
---|
[bd3f232] | 21 | { |
---|
[901cee9] | 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; |
---|
[fc8a87a] | 27 | return ml->list->pdata[n]; |
---|
[7d4fbcd] | 28 | } |
---|
| 29 | |
---|
[efeec7f] | 30 | int owl_messagelist_get_index_by_id(const owl_messagelist *ml, int target_id) |
---|
[bd3f232] | 31 | { |
---|
[efeec7f] | 32 | /* return the message index with id == 'id'. If it doesn't exist return -1. */ |
---|
[0c8ab5e] | 33 | int first, last, mid, msg_id; |
---|
[7d4fbcd] | 34 | owl_message *m; |
---|
| 35 | |
---|
[0c8ab5e] | 36 | first = 0; |
---|
[fc8a87a] | 37 | last = ml->list->len - 1; |
---|
[0c8ab5e] | 38 | while (first <= last) { |
---|
| 39 | mid = (first + last) / 2; |
---|
[fc8a87a] | 40 | m = ml->list->pdata[mid]; |
---|
[0c8ab5e] | 41 | msg_id = owl_message_get_id(m); |
---|
| 42 | if (msg_id == target_id) { |
---|
[efeec7f] | 43 | return mid; |
---|
[0c8ab5e] | 44 | } else if (msg_id < target_id) { |
---|
| 45 | first = mid + 1; |
---|
| 46 | } else { |
---|
| 47 | last = mid - 1; |
---|
| 48 | } |
---|
[7d4fbcd] | 49 | } |
---|
[efeec7f] | 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]; |
---|
[7d4fbcd] | 59 | } |
---|
| 60 | |
---|
[d427f08] | 61 | void owl_messagelist_append_element(owl_messagelist *ml, void *element) |
---|
[bd3f232] | 62 | { |
---|
[fc8a87a] | 63 | g_ptr_array_add(ml->list, element); |
---|
[7d4fbcd] | 64 | } |
---|
| 65 | |
---|
| 66 | /* do we really still want this? */ |
---|
[bd3f232] | 67 | int owl_messagelist_delete_element(owl_messagelist *ml, int n) |
---|
| 68 | { |
---|
[7d4fbcd] | 69 | /* mark a message as deleted */ |
---|
[fc8a87a] | 70 | owl_message_mark_delete(ml->list->pdata[n]); |
---|
[7d4fbcd] | 71 | return(0); |
---|
| 72 | } |
---|
| 73 | |
---|
[bd3f232] | 74 | int owl_messagelist_undelete_element(owl_messagelist *ml, int n) |
---|
| 75 | { |
---|
[7d4fbcd] | 76 | /* mark a message as deleted */ |
---|
[fc8a87a] | 77 | owl_message_unmark_delete(ml->list->pdata[n]); |
---|
[7d4fbcd] | 78 | return(0); |
---|
| 79 | } |
---|
| 80 | |
---|
[efeec7f] | 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 | |
---|
[bd3f232] | 86 | int owl_messagelist_expunge(owl_messagelist *ml) |
---|
| 87 | { |
---|
[7d4fbcd] | 88 | /* expunge deleted messages */ |
---|
[fc8a87a] | 89 | int i; |
---|
| 90 | GPtrArray *newlist; |
---|
[7d4fbcd] | 91 | owl_message *m; |
---|
| 92 | |
---|
[fc8a87a] | 93 | newlist = g_ptr_array_new(); |
---|
[7d4fbcd] | 94 | /*create a new list without messages marked as deleted */ |
---|
[fc8a87a] | 95 | for (i = 0; i < ml->list->len; i++) { |
---|
| 96 | m = ml->list->pdata[i]; |
---|
[7d4fbcd] | 97 | if (owl_message_is_delete(m)) { |
---|
[91634ec] | 98 | owl_message_delete(m); |
---|
[7d4fbcd] | 99 | } else { |
---|
[fc8a87a] | 100 | g_ptr_array_add(newlist, m); |
---|
[7d4fbcd] | 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | /* free the old list */ |
---|
[fc8a87a] | 105 | g_ptr_array_free(ml->list, true); |
---|
[7d4fbcd] | 106 | |
---|
| 107 | /* copy the new list to the old list */ |
---|
[66a8cd6] | 108 | ml->list = newlist; |
---|
[7d4fbcd] | 109 | |
---|
| 110 | return(0); |
---|
| 111 | } |
---|
[bd3f232] | 112 | |
---|
[3eb599d] | 113 | void owl_messagelist_invalidate_formats(const owl_messagelist *ml) |
---|
[bd3f232] | 114 | { |
---|
[fc8a87a] | 115 | int i; |
---|
[bd3f232] | 116 | owl_message *m; |
---|
| 117 | |
---|
[fc8a87a] | 118 | for (i = 0; i < ml->list->len; i++) { |
---|
| 119 | m = ml->list->pdata[i]; |
---|
[bd3f232] | 120 | owl_message_invalidate_format(m); |
---|
| 121 | } |
---|
| 122 | } |
---|