summaryrefslogtreecommitdiffstats
path: root/src/views/maillist.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/views/maillist.rs')
-rw-r--r--src/views/maillist.rs139
1 files changed, 50 insertions, 89 deletions
diff --git a/src/views/maillist.rs b/src/views/maillist.rs
index dc13bf3..4f68790 100644
--- a/src/views/maillist.rs
+++ b/src/views/maillist.rs
@@ -1,45 +1,29 @@
use std::path::PathBuf;
-use std::ops::Deref;
+use std::rc::Rc;
-use anyhow::Result;
use anyhow::Context;
+use anyhow::Result;
+use chrono::naive::NaiveDateTime;
use cursive::Cursive;
-use cursive::Printer;
-use cursive::Rect;
-use cursive::View;
-use cursive::XY;
-use cursive::direction::Direction;
-use cursive::view::Nameable;
-use cursive::view::Selector;
-use cursive::event::Event;
-use cursive::event::EventResult;
-use cursive::views::NamedView;
+use cursive::view::SizeConstraint;
+use cursive::views::ResizedView;
use cursive_table_view::TableView;
use cursive_table_view::TableViewItem;
-use chrono::naive::NaiveDateTime;
+use getset::Getters;
use notmuch::Message;
use notmuch::MessageOwner;
-use getset::Getters;
-use cursive::views::ResizedView;
-use cursive::view::SizeConstraint;
-use crate::views::main::MainView;
use crate::views::mail::MailView;
+use crate::runtime::Runtime;
pub struct MaillistView {
+ rt: Rc<Runtime>,
view: ResizedView<TableView<MailListingData, MailListingColumn>>,
}
-impl Deref for MaillistView {
- type Target = TableView<MailListingData, MailListingColumn>;
- fn deref(&self) -> &Self::Target {
- self.view.get_inner()
- }
-}
-
impl MaillistView {
- pub fn create_for(database_path: PathBuf, query: &str, name: String) -> Result<NamedView<Self>> {
- debug!("Getting '{}' from '{}'", query, database_path.display());
+ pub fn for_query(rt: Rc<Runtime>, query: &str) -> Result<impl cursive::view::View> {
+ debug!("Getting '{}' from '{}'", query, rt.config().notmuch_database_path().display());
fn get_header_field_save<'o, O: MessageOwner + 'o>(msg: &Message<'o, O>, field: &str) -> String {
match msg.header(field) {
@@ -53,8 +37,7 @@ impl MaillistView {
}
}
- let items = notmuch::Database::open(&database_path, notmuch::DatabaseMode::ReadOnly)
- .context(format!("Opening database {}", database_path.display()))?
+ let items = rt.database()
.create_query(query)
.context("Creating the search query")?
.search_messages()
@@ -86,12 +69,11 @@ impl MaillistView {
})
})
.collect::<Result<Vec<_>>>()
- .context(format!("Creating MaillinglistView for '{}' on {}", query, database_path.display()))?;
+ .context(format!("Creating MaillinglistView for '{}' on {}", query, rt.config().notmuch_database_path().display()))?;
debug!("Found {} entries", items.len());
- let n = name.clone();
- let db_path = database_path.clone();
- let view = TableView::<MailListingData, MailListingColumn>::new()
+ let mailviewrt = rt.clone();
+ let view = TableView::<MailListingData,MailListingColumn>::new()
.column(MailListingColumn::Date, "Date", |c| c.width(20))
.column(MailListingColumn::Tags, "Tags", |c| c.width(20))
.column(MailListingColumn::From, "From", |c| c)
@@ -101,82 +83,61 @@ impl MaillistView {
.items(items)
.selected_item(0)
.on_submit(move |siv: &mut Cursive, row: usize, _: usize| {
- let (mail_id, filename) = siv.call_on_name(&n, move |table: &mut MaillistView| {
- table.view
- .get_inner_mut()
- .borrow_item(row)
- .map(|data| {
- debug!("Opening: {:?}", data);
- (data.mail_id.clone(), data.filename.clone())
+ let (mail_id, filename) = siv.call_on_name(crate::views::main::MAIN_VIEW_NAME, move |main: &mut crate::views::main::MainView| {
+ main.get_current_mux()
+ .map(|mux| {
+ if let Some(table) = mux.downcast_ref::<TableView<MailListingData, MailListingColumn>>() {
+ table
+ .borrow_item(row)
+ .map(|data| {
+ debug!("Opening: {:?}", data);
+ (data.mail_id.clone(), data.filename.clone())
+ })
+ } else {
+ unimplemented!()
+ }
})
})
.unwrap()
+ .unwrap()
.unwrap();
debug!("Showing mail {}", mail_id);
+ let mv = MailView::create_for(mailviewrt.clone(), mail_id, filename).unwrap();
- // Why do I have to do this? This is UGLY!
- let n = n.clone();
- let db_path = db_path.clone();
- let name = format!("{}-{}", n, mail_id);
- let mv = MailView::create_for(db_path, mail_id, filename, name).unwrap();
-
- siv.call_on_name(crate::views::main::MAIN_MUX_NAME, move |mux: &mut cursive_multiplex::Mux| {
- mux.add_right_of(mv, mux.root().build().unwrap());
+ siv.call_on_name(crate::views::main::MAIN_VIEW_NAME , move |main: &mut crate::views::main::MainView| {
+ main.get_current_tab_mut()
+ .map(|mux: &mut cursive_multiplex::Mux| {
+ mux.add_right_of(mv, mux.focus());
+ })
});
// use the mail ID to get the whole thread and open it as a table item
});
- Ok(MaillistView { view: ResizedView::new(SizeConstraint::Full, SizeConstraint::Full, view )}.with_name(name))
- }
-
- pub fn borrow_item(&mut self, idx: usize) -> Option<&MailListingData> {
- self.view.get_inner_mut().borrow_item(idx)
+ Ok({
+ MaillistView {
+ rt,
+ view: ResizedView::new(SizeConstraint::Full, SizeConstraint::Full, view)
+ }
+ })
}
}
-impl View for MaillistView {
- fn draw(&self, printer: &Printer) {
- self.view.draw(printer)
- }
-
- fn layout(&mut self, xy: XY<usize>) {
- self.view.layout(xy)
- }
-
- fn needs_relayout(&self) -> bool {
- self.view.needs_relayout()
- }
-
- fn required_size(&mut self, constraint: XY<usize>) -> XY<usize> {
- self.view.required_size(constraint)
- }
-
- fn on_event(&mut self, e: Event) -> EventResult {
- self.view.on_event(e)
- }
+impl cursive::view::ViewWrapper for MaillistView {
+ type V = ResizedView<TableView<MailListingData, MailListingColumn>>;
- fn call_on_any<'a>(&mut self, s: &Selector, tpl: &'a mut (dyn FnMut(&mut (dyn View + 'static)) + 'a)) {
- self.view.call_on_any(s, tpl);
- }
-
- fn focus_view(&mut self, s: &Selector) -> Result<(), ()> {
- self.view.focus_view(s)
- }
-
- fn take_focus(&mut self, source: Direction) -> bool {
- self.view.take_focus(source)
- }
-
- fn important_area(&self, view_size: XY<usize>) -> Rect {
- self.view.important_area(view_size)
+ fn with_view<F, R>(&self, f: F) -> Option<R>
+ where F: FnOnce(&Self::V) -> R
+ {
+ Some(f(&self.view))
}
- fn type_name(&self) -> &'static str {
- self.view.type_name()
+ fn with_view_mut<F, R>(&mut self, f: F) -> Option<R>
+ where F: FnOnce(&mut Self::V) -> R
+ {
+ Some(f(&mut self.view))
}
-
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]