source: messagelist.c @ caac19d

Last change on this file since caac19d was 67b959c, checked in by Jason Gross <jgross@mit.edu>, 13 years ago
Added a delete-and-expunge command. This command is analogous to the delete command, except that it also expunges only the {current,given} message. It also takes an optional --quiet parameter, because I plan to use it internally and don't want spew on success. Perhaps there's a better way to accomplish this... I couldn't figure out how the existing delete command is supposed to work internally. The logic behind the delete-and-expunge command is as follows: We always update both the message list and the display. So model the command that will do much of the work, owl_function_delete_and_expunge_message, off of owl_function_expunge. owl_function_delete_and_expunge_message takes one argument, which is the position of the item in the message list. It deletes it from the message list with a new function owl_messagelist_delete_and_expunge_element. This calls delete on the underlying owl_list, so the errors (if any) on an index that is too big might be unhelpful. owl_function_delete_and_expunge_cur and owl_function_delete_and_expunge_by_id both get the index of the relevant message (and a function owl_messagelist_get_index_by_id is added to messagelist.c to help the latter). They owl_function_error if no such message exists, and conditionally owl_function_makemsg on success.
  • Property mode set to 100644
File size: 2.7 KB
RevLine 
[7d4fbcd]1#include "owl.h"
2#include <stdlib.h>
3#include <string.h>
4
[fc8a87a]5void owl_messagelist_create(owl_messagelist *ml)
[bd3f232]6{
[fc8a87a]7  ml->list = g_ptr_array_new();
8}
9
10void 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]17int owl_messagelist_get_size(const owl_messagelist *ml)
[bd3f232]18{
[fc8a87a]19  return ml->list->len;
[7d4fbcd]20}
21
[3eb599d]22void *owl_messagelist_get_element(const owl_messagelist *ml, int n)
[bd3f232]23{
[fc8a87a]24  return ml->list->pdata[n];
[7d4fbcd]25}
26
[67b959c]27int owl_messagelist_get_index_by_id(const owl_messagelist *ml, int target_id)
[bd3f232]28{
[67b959c]29  /* return the message index with id == 'id'.  If it doesn't exist return -1. */
[0c8ab5e]30  int first, last, mid, msg_id;
[7d4fbcd]31  owl_message *m;
32
[0c8ab5e]33  first = 0;
[fc8a87a]34  last = ml->list->len - 1;
[0c8ab5e]35  while (first <= last) {
36    mid = (first + last) / 2;
[fc8a87a]37    m = ml->list->pdata[mid];
[0c8ab5e]38    msg_id = owl_message_get_id(m);
39    if (msg_id == target_id) {
[67b959c]40      return mid;
[0c8ab5e]41    } else if (msg_id < target_id) {
42      first = mid + 1;
43    } else {
44      last = mid - 1;
45    }
[7d4fbcd]46  }
[67b959c]47  return -1;
48}
49
50owl_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];
[7d4fbcd]56}
57
[d427f08]58void owl_messagelist_append_element(owl_messagelist *ml, void *element)
[bd3f232]59{
[fc8a87a]60  g_ptr_array_add(ml->list, element);
[7d4fbcd]61}
62
63/* do we really still want this? */
[bd3f232]64int owl_messagelist_delete_element(owl_messagelist *ml, int n)
65{
[7d4fbcd]66  /* mark a message as deleted */
[fc8a87a]67  owl_message_mark_delete(ml->list->pdata[n]);
[7d4fbcd]68  return(0);
69}
70
[bd3f232]71int owl_messagelist_undelete_element(owl_messagelist *ml, int n)
72{
[7d4fbcd]73  /* mark a message as deleted */
[fc8a87a]74  owl_message_unmark_delete(ml->list->pdata[n]);
[7d4fbcd]75  return(0);
76}
77
[67b959c]78void 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
[bd3f232]83int owl_messagelist_expunge(owl_messagelist *ml)
84{
[7d4fbcd]85  /* expunge deleted messages */
[fc8a87a]86  int i;
87  GPtrArray *newlist;
[7d4fbcd]88  owl_message *m;
89
[fc8a87a]90  newlist = g_ptr_array_new();
[7d4fbcd]91  /*create a new list without messages marked as deleted */
[fc8a87a]92  for (i = 0; i < ml->list->len; i++) {
93    m = ml->list->pdata[i];
[7d4fbcd]94    if (owl_message_is_delete(m)) {
[91634ec]95      owl_message_delete(m);
[7d4fbcd]96    } else {
[fc8a87a]97      g_ptr_array_add(newlist, m);
[7d4fbcd]98    }
99  }
100
101  /* free the old list */
[fc8a87a]102  g_ptr_array_free(ml->list, true);
[7d4fbcd]103
104  /* copy the new list to the old list */
[66a8cd6]105  ml->list = newlist;
[7d4fbcd]106
107  return(0);
108}
[bd3f232]109
[3eb599d]110void owl_messagelist_invalidate_formats(const owl_messagelist *ml)
[bd3f232]111{
[fc8a87a]112  int i;
[bd3f232]113  owl_message *m;
114
[fc8a87a]115  for (i = 0; i < ml->list->len; i++) {
116    m = ml->list->pdata[i];
[bd3f232]117    owl_message_invalidate_format(m);
118  }
119}
Note: See TracBrowser for help on using the repository browser.