melib: Fix incorrect thread child linking

embed
Manos Pitsidianakis 2018-09-08 16:43:24 +03:00
parent e3457c40d6
commit 2fcd014bfe
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
4 changed files with 105 additions and 44 deletions

View File

@ -27,7 +27,8 @@ use mailbox::email::*;
extern crate fnv;
use self::fnv::{FnvHashMap, FnvHashSet};
use std::cell::RefCell;
use std::cell::{Ref, RefCell};
use std::cmp::Ordering;
use std::iter::FromIterator;
use std::ops::Index;
use std::result::Result as StdResult;
@ -161,7 +162,7 @@ impl ThreadNode {
#[derive(Clone, Debug, Default, Deserialize)]
pub struct Threads {
thread_nodes: Vec<ThreadNode>,
root_set: Vec<usize>,
root_set: RefCell<Vec<usize>>,
message_ids: FnvHashMap<String, usize>,
hash_set: FnvHashSet<EnvelopeHash>,
@ -178,6 +179,24 @@ impl PartialEq for ThreadNode {
}
}
pub struct RootIterator<'a> {
pos: usize,
root_set: Ref<'a, Vec<usize>>,
}
impl<'a> Iterator for RootIterator<'a> {
type Item = usize;
fn next(&mut self) -> Option<usize> {
{
if self.pos == self.root_set.len() {
return None;
}
self.pos += 1;
return Some(self.root_set[self.pos - 1]);
}
}
}
impl Threads {
pub fn new(collection: &mut FnvHashMap<EnvelopeHash, Envelope>) -> Threads {
/* To reconstruct thread information from the mails we need: */
@ -226,11 +245,9 @@ impl Threads {
root_set.push(*v);
}
}
root_set.sort_by(|a, b| thread_nodes[*b].date.cmp(&thread_nodes[*a].date));
let mut t = Threads {
thread_nodes,
root_set,
root_set: RefCell::new(root_set),
message_ids,
hash_set,
..Default::default()
@ -259,7 +276,7 @@ impl Threads {
}
fn build_collection(&mut self, collection: &FnvHashMap<EnvelopeHash, Envelope>) {
for i in &self.root_set {
for i in self.root_set.borrow().iter() {
node_build(
*i,
&mut self.thread_nodes,
@ -284,6 +301,41 @@ impl Threads {
sort: (SortField, SortOrder),
collection: &FnvHashMap<EnvelopeHash, Envelope>,
) {
let root_set = &mut self.root_set.borrow_mut();
root_set.sort_by(|a, b| match sort {
(SortField::Date, SortOrder::Desc) => {
let a = &self.thread_nodes[*a];
let b = &self.thread_nodes[*b];
b.date.cmp(&a.date)
}
(SortField::Date, SortOrder::Asc) => {
let a = &self.thread_nodes[*a];
let b = &self.thread_nodes[*b];
a.date.cmp(&b.date)
}
(SortField::Subject, SortOrder::Desc) => {
let a = &self.thread_nodes[*a].message();
let b = &self.thread_nodes[*b].message();
if a.is_none() || b.is_none() {
return Ordering::Equal;
}
let ma = &collection[&a.unwrap()];
let mb = &collection[&b.unwrap()];
ma.subject().cmp(&mb.subject())
}
(SortField::Subject, SortOrder::Asc) => {
let a = &self.thread_nodes[*a].message();
let b = &self.thread_nodes[*b].message();
if a.is_none() || b.is_none() {
return Ordering::Equal;
}
let ma = &collection[&a.unwrap()];
let mb = &collection[&b.unwrap()];
mb.subject().cmp(&ma.subject())
}
});
}
pub fn sort_by(
@ -292,7 +344,6 @@ impl Threads {
subsort: (SortField, SortOrder),
collection: &FnvHashMap<EnvelopeHash, Envelope>,
) {
/*
if *self.sort.borrow() != sort {
self.inner_sort_by(sort, collection);
*self.sort.borrow_mut() = sort;
@ -301,11 +352,10 @@ impl Threads {
self.inner_subsort_by(subsort, collection);
*self.subsort.borrow_mut() = subsort;
}
*/
}
pub fn thread_to_mail(&self, i: usize) -> EnvelopeHash {
let thread = &self.thread_nodes[self.root_set[i]];
let thread = &self.thread_nodes[self.root_set.borrow()[i]];
thread.message().unwrap()
}
@ -313,8 +363,19 @@ impl Threads {
&self.thread_nodes
}
pub fn root_set(&self) -> &Vec<usize> {
&self.root_set
pub fn root_len(&self) -> usize {
self.root_set.borrow().len()
}
pub fn root_set(&self, idx: usize) -> usize {
self.root_set.borrow()[idx]
}
pub fn root_iter<'a>(&'a self) -> RootIterator<'a> {
RootIterator {
pos: 0,
root_set: self.root_set.borrow(),
}
}
pub fn has_sibling(&self, i: usize) -> bool {
@ -381,13 +442,7 @@ fn link_envelope(
/* The index of the reference we are currently examining, start from current message */
let mut ref_ptr = t_idx;
let mut iasf = 0;
for &refn in envelope.references().iter().rev() {
if iasf == 1 {
/*FIXME: Skips anything other than direct parents */
continue;
}
iasf += 1;
let r_id = String::from_utf8_lossy(refn.raw()).into();
let parent_id = if message_ids.contains_key(&r_id) {
let parent_id = message_ids[&r_id];
@ -395,7 +450,7 @@ fn link_envelope(
|| thread_nodes[ref_ptr].is_descendant(thread_nodes, &thread_nodes[parent_id]))
{
thread_nodes[ref_ptr].parent = Some(parent_id);
if thread_nodes[parent_id].children.is_empty() {
if !thread_nodes[parent_id].children.contains(&ref_ptr) {
thread_nodes[parent_id].children.push(ref_ptr);
}
@ -503,7 +558,7 @@ fn node_build(
let mut has_unseen = thread_nodes[idx].has_unseen;
for c in thread_nodes[idx].children.clone().iter() {
node_build(*c, thread_nodes, indentation, *c, collection);
node_build(*c, thread_nodes, indentation, idx, collection);
has_unseen |= thread_nodes[*c].has_unseen;
}
thread_nodes[idx].has_unseen = has_unseen;

View File

@ -121,7 +121,7 @@ impl CompactListing {
.as_ref()
.unwrap();
self.length = mailbox.collection.threads.root_set().len();
self.length = mailbox.collection.threads.root_len();
self.content = CellBuffer::new(MAX_COLS, self.length + 1, Cell::with_char(' '));
if self.length == 0 {
write_string_to_grid(
@ -136,14 +136,16 @@ impl CompactListing {
}
let threads = &mailbox.collection.threads;
threads.sort_by(self.sort, self.subsort, &mailbox.collection);
for (idx, root_idx) in threads.root_set().iter().enumerate() {
let thread_node = &threads.thread_nodes()[*root_idx];
for (idx, root_idx) in threads.root_iter().enumerate() {
let thread_node = &threads.thread_nodes()[root_idx];
let i = if let Some(i) = thread_node.message() {
i
} else {
threads.thread_nodes()[thread_node.children()[0]]
.message()
.unwrap()
let mut iter_ptr = thread_node.children()[0];
while threads.thread_nodes()[iter_ptr].message().is_none() {
iter_ptr = threads.thread_nodes()[iter_ptr].children()[0];
}
threads.thread_nodes()[iter_ptr].message().unwrap()
};
let root_envelope: &Envelope = &mailbox.collection[&i];
let fg_color = if thread_node.has_unseen() {
@ -180,14 +182,16 @@ impl CompactListing {
.as_ref()
.unwrap();
let threads = &mailbox.collection.threads;
let thread_node = threads.root_set()[idx];
let thread_node = threads.root_set(idx);
let thread_node = &threads.thread_nodes()[thread_node];
let i = if let Some(i) = thread_node.message() {
i
} else {
threads.thread_nodes()[thread_node.children()[0]]
.message()
.unwrap()
let mut iter_ptr = thread_node.children()[0];
while threads.thread_nodes()[iter_ptr].message().is_none() {
iter_ptr = threads.thread_nodes()[iter_ptr].children()[0];
}
threads.thread_nodes()[iter_ptr].message().unwrap()
};
let root_envelope: &Envelope = &mailbox.collection[&i];
let fg_color = if !root_envelope.is_seen() {

View File

@ -98,7 +98,7 @@ impl ThreadListing {
.as_ref()
.unwrap();
self.length = mailbox.collection.threads.root_set().len();
self.length = mailbox.collection.threads.root_len();
self.content = CellBuffer::new(MAX_COLS, self.length + 1, Cell::with_char(' '));
if self.length == 0 {
write_string_to_grid(
@ -118,12 +118,12 @@ impl ThreadListing {
let threads = &mailbox.collection.threads;
threads.sort_by(self.sort, self.subsort, &mailbox.collection);
let thread_nodes: &Vec<ThreadNode> = &threads.thread_nodes();
let mut iter = threads.root_set().into_iter().peekable();
let len = threads.root_set().len().to_string().chars().count();
let mut iter = threads.root_iter().peekable();
let len = threads.root_len().to_string().chars().count();
/* This is just a desugared for loop so that we can use .peek() */
let mut idx = 0;
while let Some(i) = iter.next() {
let thread_node = &thread_nodes[*i];
let thread_node = &thread_nodes[i];
if !thread_node.has_message() {
continue;
@ -136,7 +136,7 @@ impl ThreadListing {
}
match iter.peek() {
Some(&x) if thread_nodes[*x].indentation() == indentation => {
Some(&x) if thread_nodes[x].indentation() == indentation => {
indentations.pop();
indentations.push(true);
}
@ -145,7 +145,7 @@ impl ThreadListing {
indentations.push(false);
}
}
if threads.has_sibling(*i) {
if threads.has_sibling(i) {
indentations.pop();
indentations.push(true);
}
@ -167,7 +167,7 @@ impl ThreadListing {
envelope,
idx,
indentation,
*i,
i,
threads,
&indentations,
len,
@ -185,11 +185,11 @@ impl ThreadListing {
}
match iter.peek() {
Some(&x) if thread_nodes[*x].indentation() > indentation => {
Some(&x) if thread_nodes[x].indentation() > indentation => {
indentations.push(false);
}
Some(&x) if thread_nodes[*x].indentation() < indentation => {
for _ in 0..(indentation - thread_nodes[*x].indentation()) {
Some(&x) if thread_nodes[x].indentation() < indentation => {
for _ in 0..(indentation - thread_nodes[x].indentation()) {
indentations.pop();
}
}

View File

@ -63,12 +63,14 @@ impl ThreadView {
.as_ref()
.unwrap();
let threads = &mailbox.collection.threads;
let thread_node = &threads.thread_nodes()[threads.root_set()[coordinates.2]];
let thread_node = &threads.thread_nodes()[threads.root_set(coordinates.2)];
if thread_node.message().is_some() {
stack.push((0, threads.root_set()[coordinates.2]));
stack.push((0, threads.root_set(coordinates.2)));
} else {
stack.push((1, thread_node.children()[0]));
for &c in thread_node.children().iter() {
stack.push((1, c));
}
}
let mut view = ThreadView {
dirty: true,
@ -313,7 +315,7 @@ impl ThreadView {
.as_ref()
.unwrap();
let threads = &mailbox.collection.threads;
let thread_node = &threads.thread_nodes()[threads.root_set()[self.coordinates.2]];
let thread_node = &threads.thread_nodes()[threads.root_set(self.coordinates.2)];
let i = if let Some(i) = thread_node.message() {
i
} else {
@ -432,7 +434,7 @@ impl ThreadView {
.as_ref()
.unwrap();
let threads = &mailbox.collection.threads;
let thread_node = &threads.thread_nodes()[threads.root_set()[self.coordinates.2]];
let thread_node = &threads.thread_nodes()[threads.root_set(self.coordinates.2)];
let i = if let Some(i) = thread_node.message() {
i
} else {