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