diff options
Diffstat (limited to 'src/view/main.rs')
-rw-r--r-- | src/view/main.rs | 335 |
1 files changed, 335 insertions, 0 deletions
diff --git a/src/view/main.rs b/src/view/main.rs new file mode 100644 index 0000000..e1753b4 --- /dev/null +++ b/src/view/main.rs @@ -0,0 +1,335 @@ +use crate::view::telephone::TelephoneView; +use std::collections::HashSet; +use super::name::{NameView}; +use super::address::AddressView; +use genpdf::Element as _; +use genpdf::{elements, style, fonts}; +use qrcodegen::QrCode; +use qrcodegen::QrCodeEcc; +use yew::prelude::*; +use vcard::{VCard, VCardError}; +use super::input_objects::utility::*; +use super::input_objects::name::Name; +use super::input_objects::address::Address; +use super::input_objects::telephone::Telephone; +use super::input_objects::VCardPropertyInputObject; + + +pub struct MainView { + link: ComponentLink<Self>, + error: Vec<String>, + name: Name, + address: Address, + telephone: Telephone, + download: Option<Download>, + selected_option: DownloadOption, +} + +pub enum Msg { + UpdateName(Name), + UpdateAddress(Address), + UpdateTelephone(Telephone), + Generate(DownloadOption), + Nope, +} + +impl Component for MainView { + type Message = Msg; + type Properties = (); + + fn create(_props: Self::Properties, link: ComponentLink<Self>) -> Self { + MainView { + link, + error: vec![], + name: Name::new(), + address: Address::new(), + telephone: Telephone::new(), + download: None, + selected_option: DownloadOption::VCard + } + } + + fn update(&mut self, msg: Self::Message) -> ShouldRender { + self.error.clear(); + match msg { + Msg::UpdateName(value) => { + self.name = value; + self.link.send_message(Msg::Generate(self.selected_option)); + }, + Msg::UpdateAddress(value) => { + self.address = value; + self.link.send_message(Msg::Generate(self.selected_option)); + }, + Msg::UpdateTelephone(value) => { + self.telephone = value; + self.link.send_message(Msg::Generate(self.selected_option)); + }, + Msg::Generate(option) => { + self.selected_option = option; + + let vcard_content = match self.generate_vcard() { + Ok(vcard) => Some(vcard.to_string()), + Err(VCardError::FormatError(err)) => { + self.error.push(err.to_string()); + None + } + Err(VCardError::EmptyFormatName) => { + self.error.push(String::from("At least one of the name fields should be filled out.")); + None + } + }; + + match option { + DownloadOption::VCard => { + if vcard_content.is_some() { + self.download = Some( + Download { + file_name: format!("{}.vcs", self.name.formatted_name()), + content: vcard_content.unwrap().to_string(), + mime_type: MimeType::VCard, + } + ) + } + } + DownloadOption::QrCode => { + if vcard_content.is_some() { + match QrCode::encode_text(vcard_content.as_ref().unwrap(), QrCodeEcc::Low) { + Ok(qr) => self.download = Some( + Download { + file_name: format!("QR-Code VCard {}.svg", self.name.formatted_name()), + content: qr.to_svg_string(4), + mime_type: MimeType::SVG, + } + ), + Err(_) => self.error.push(String::from("Sorry, VCard is too long!")), + }; + } + } + DownloadOption::PDF => { + match self.generate_pdf() { + Ok(pdf) => self.download = Some( + Download { + file_name: format!("Visitenkarten {}.pdf", self.name.formatted_name()), + content: pdf, + mime_type: MimeType::PDF, + } + ), + Err(_) => self.error.push(String::from("Unexpected error while generating the PDF. Please contact me about it.")), + } + } + } + } + Msg::Nope => return false, + }; + if self.error.len() > 0 { + self.download = None; + } + true + } + + fn change(&mut self, _props: Self::Properties) -> ShouldRender { + false + } + + fn view(&self) -> Html { + + let download_options = self.link.callback(|e: ChangeData| + match e { + ChangeData::Select(v) => match v.value().as_str() { + "vcard" => Msg::Generate(DownloadOption::VCard), + "pdf" => Msg::Generate(DownloadOption::PDF), + "qrcode" => Msg::Generate(DownloadOption::QrCode), + _ => Msg::Nope, + }, + _ => Msg::Nope, + } + ); + + html!{ + <> + <main> + <section class="hero"> + <div class="hero-body"> + <div class="container is-max-widescreen"> + <h1 class="title">{ "A Generator for vCards" }</h1> + <h2 class="subtitle">{ "Supports generating vCards (.vcf), print-ready PDF business cards and QR Codes" }</h2> + </div> + </div> + </section> + + <section class="section"> + <div class="container is-max-widescreen"> + + { self.render_errors() } + + <NameView oninput=self.link.callback(|n: Name| Msg::UpdateName(n)) /> + + <AddressView oninput=self.link.callback(|a: Address| Msg::UpdateAddress(a)) /> + + <TelephoneView oninput=self.link.callback(|t: Telephone| Msg::UpdateTelephone(t)) /> + + <div class="block level-left"> + <div class="select level-item"> + <select id="download_options" onchange=download_options> + <option value="vcard">{ "VCard (.vcf)" }</option> + <option value="pdf">{ "Print-ready PDF" }</option> + <option value="qrcode">{ "QR Code" }</option> + </select> + </div> + + { self.render_download() } + </div> + + <div class="block"> + { self.render_preview() } + </div> + + </div> + </section> + </main> + + <footer class="footer"> + <div class="content has-text-centered"> + <p> + <strong>{ "VCard Generator" }</strong> { " by " } <a href="https://jelemux.dev">{ "Jeremias Weber" }</a>{ ". "} + { "The source code is licenced " } <a href="http://opensource.org/licenses/mit-license.php">{ "MIT" }</a>{"."} + </p> + </div> + </footer> + </> + } + } +} + +impl MainView { + fn render_errors(&self) -> Html { + html!{ + <> + { + for self.error.iter().map(|err| + html!{ + <div class="notification is-danger is-light"> + { err } + </div> + } + ) + } + </> + } + } + fn render_download(&self) -> Html { + if self.download.is_some() { + let download = self.download.as_ref().unwrap(); + + html!{ + <a href=download.as_data_link() download=download.file_name class="button is-primary level-item" > + { "Download" } + </a> + } + } else { + html!{} + } + } + fn render_preview(&self) -> Html { + if self.download.is_some() { + let download = self.download.as_ref().unwrap(); + + match download.mime_type { + MimeType::PDF => html!{ + <iframe src=download.as_data_link() alt="PDF Preview" width="400" height="550"/> + }, + MimeType::VCard => { + html!{ + <pre> + <code> { download.content.clone() } </code> + </pre> + } + } + MimeType::SVG => html!{ + <img src=download.as_data_link() alt="Image Preview" class="image is-square" width="300" height="300"/> + }, + } + } else { + html!{} + } + } + fn generate_vcard(&self) -> Result<VCard, VCardError> { + match VCard::from_formatted_name_str(&self.name.formatted_name()) { + Ok(vcard) => { + let mut vcard = vcard; + + let names = { + let mut names = HashSet::new(); + names.insert(self.name.to_vcard_property().unwrap()); + + names + }; + + let addresses = { + let mut addresses = HashSet::new(); + addresses.insert(self.address.to_vcard_property().unwrap()); + + addresses + }; + + let telephones = { + let mut telephones = HashSet::new(); + telephones.insert(self.telephone.to_vcard_property().unwrap()); + + telephones + }; + + vcard.names = Some(vcard::Set::from_hash_set(names).unwrap()); + vcard.addresses = Some(vcard::Set::from_hash_set(addresses).unwrap()); + vcard.telephones = Some(vcard::Set::from_hash_set(telephones).unwrap()); + + Ok(vcard) + } + Err(err) => Err(err), + } + } + fn generate_pdf(&self) -> Result<String, ()>{ + let regular_bytes = include_bytes!("/usr/share/fonts/liberation/LiberationSans-Regular.ttf"); + let regular_font_data = fonts::FontData::new(regular_bytes.to_vec(), Some(printpdf::BuiltinFont::Helvetica)).expect("font data should be correct"); + + let bold_bytes = include_bytes!("/usr/share/fonts/liberation/LiberationSans-Bold.ttf"); + let bold_font_data = fonts::FontData::new(bold_bytes.to_vec(), Some(printpdf::BuiltinFont::HelveticaBold)).expect("font data should be correct"); + + let italic_bytes = include_bytes!("/usr/share/fonts/liberation/LiberationSans-Italic.ttf"); + let italic_font_data = fonts::FontData::new(italic_bytes.to_vec(), Some(printpdf::BuiltinFont::HelveticaOblique)).expect("font data should be correct"); + + let bold_italic_bytes = include_bytes!("/usr/share/fonts/liberation/LiberationSans-BoldItalic.ttf"); + let bold_italic_font_data = fonts::FontData::new(bold_italic_bytes.to_vec(), Some(printpdf::BuiltinFont::HelveticaBoldOblique)).expect("font data should be correct"); + + let font_family = fonts::FontFamily{ + regular: regular_font_data, + bold: bold_font_data, + italic: italic_font_data, + bold_italic: bold_italic_font_data + }; + + let mut doc = genpdf::Document::new(font_family); + + doc.set_title("BCard test"); + doc.set_minimal_conformance(); + doc.set_margins(10); + doc.set_line_spacing(1.25); + + doc.push( + elements::Paragraph::new("genpdf Demo Document") + .aligned(elements::Alignment::Center) + .styled(style::Style::new().bold().with_font_size(20)), + ); + + // TODO fill doc with real data + + let mut buf: Vec<u8> = Vec::new(); + match doc.render(&mut buf) { + Ok(_) => Ok(match String::from_utf8(buf) { + Ok(s) => s, + Err(_) => return Err(()), + }), + Err(_) => Err(()), + } + } +}
\ No newline at end of file |