summaryrefslogtreecommitdiff
path: root/src/view/main.rs
blob: 9ce032a10ab2a8a245bbb2076ac6b19dab28454c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
use crate::model::property_groups::address::*;
use crate::model::property_groups::communication::*;
use crate::model::property_groups::name::*;
use crate::model::property_groups::organizational::*;
use crate::model::property_groups::other_identification::*;
use crate::model::property_groups::telephone::*;
use crate::model::utility::*;
use crate::model::vcard::VCardData;
use crate::model::Error;
use crate::model::VCardPropertyInputGroupObject;
use crate::view::property_group::PropertyGroupInputComponent;
use crate::view::weak_links::WeakComponentLink;
use boolinator::Boolinator;
use chrono::prelude::*;
use genpdf::Element as _;
use genpdf::{elements, fonts, style};
use qrcodegen::QrCode;
use qrcodegen::QrCodeEcc;
use uuid::Uuid;
use vobject::parameters;
use vobject::vcard::VcardBuilder;
use yew::prelude::*;
use yew::services::ConsoleService;
use yewtil::ptr::Mrc;

type NameView = PropertyGroupInputComponent<Name, NameMsg>;
type AddressView = PropertyGroupInputComponent<Address, AddressMsg>;
type OtherIdentificationView =
    PropertyGroupInputComponent<OtherIdentification, OtherIdentificationMsg>;
type OrganizationalView = PropertyGroupInputComponent<Organizational, OrganizationalMsg>;
type TelephoneView = PropertyGroupInputComponent<Telephone, TelephoneMsg>;
type CommunicationView = PropertyGroupInputComponent<Communication, CommunicationMsg>;

pub struct MainView {
    link: ComponentLink<Self>,
    error: Option<Error>,
    download: Option<Download>,
    selected_option: DownloadOption,
    vcard_data: Mrc<VCardData>,

    name_links: Vec<WeakComponentLink<NameView>>,
    address_links: Vec<WeakComponentLink<AddressView>>,
    telephone_links: Vec<WeakComponentLink<TelephoneView>>,
    communcation_links: Vec<WeakComponentLink<CommunicationView>>,
    other_identifications_links: Vec<WeakComponentLink<OtherIdentificationView>>,
    organizational_links: Vec<WeakComponentLink<OrganizationalView>>,

    answer_count: usize,
}

pub enum Msg {
    AddName,
    DelName(usize),
    AddAddress,
    DelAddress(usize),
    AddTelephone,
    DelTelephone(usize),
    AddOtherIdentification,
    DelOtherIdentification(usize),
    AddOrganizational,
    DelOrganizational(usize),
    AddCommunication,
    DelCommunication(usize),

    ChangeDownloadOption(DownloadOption),

    Generate,
    GeneratedName(Name),
    GeneratedOtherIdentification(OtherIdentification),
    GeneratedAddress(Address),
    GeneratedTelephone(Telephone),
    GeneratedOrganizational(Organizational),
    GeneratedCommunication(Communication),
    GenerationComplete,

    Nope,
}

impl Component for MainView {
    type Message = Msg;
    type Properties = ();

    fn create(_props: Self::Properties, link: ComponentLink<Self>) -> Self {
        MainView {
            link,
            error: None,
            download: None,
            selected_option: DownloadOption::VCard,
            vcard_data: Mrc::new(VCardData::new()),

            name_links: vec![WeakComponentLink::default()],
            address_links: vec![WeakComponentLink::default()],
            telephone_links: vec![WeakComponentLink::default()],
            communcation_links: vec![WeakComponentLink::default()],
            other_identifications_links: vec![WeakComponentLink::default()],
            organizational_links: vec![WeakComponentLink::default()],
            answer_count: 0,
        }
    }

    fn update(&mut self, msg: Self::Message) -> ShouldRender {
        self.error = None;

        let shouldrender = match msg {
            Msg::AddName => {
                self.name_links.push(WeakComponentLink::default());
                true
            }
            Msg::DelName(idx) => {
                self.name_links.remove(idx);
                true
            }
            Msg::AddAddress => {
                self.address_links.push(WeakComponentLink::default());
                true
            }
            Msg::DelAddress(idx) => {
                self.address_links.remove(idx);
                true
            }
            Msg::AddTelephone => {
                self.telephone_links.push(WeakComponentLink::default());
                true
            }
            Msg::DelTelephone(idx) => {
                self.telephone_links.remove(idx);
                true
            }
            Msg::AddOtherIdentification => {
                self.other_identifications_links
                    .push(WeakComponentLink::default());
                true
            }
            Msg::DelOtherIdentification(idx) => {
                self.other_identifications_links.remove(idx);
                true
            }
            Msg::AddOrganizational => {
                self.organizational_links.push(WeakComponentLink::default());
                true
            }
            Msg::DelOrganizational(idx) => {
                self.organizational_links.remove(idx);
                true
            }
            Msg::AddCommunication => {
                self.communcation_links.push(WeakComponentLink::default());
                true
            }
            Msg::DelCommunication(idx) => {
                self.communcation_links.remove(idx);
                true
            }
            Msg::ChangeDownloadOption(option) => {
                self.selected_option = option;
                false
            }
            Msg::Generate => {
                if self.selected_option == DownloadOption::VCard
                    || self.selected_option == DownloadOption::QrCode
                {
                    for name_link in self.name_links.iter() {
                        let name_link = name_link.borrow().clone().unwrap();
                        name_link.send_message(NameMsg::Generate);
                    }

                    for address_link in self.address_links.iter() {
                        let address_link = address_link.borrow().clone().unwrap();
                        address_link.send_message(AddressMsg::Generate);
                    }

                    for telephone_link in self.telephone_links.iter() {
                        let telephone_link = telephone_link.borrow().clone().unwrap();
                        telephone_link.send_message(TelephoneMsg::Generate);
                    }

                    for other_identifications_link in self.other_identifications_links.iter() {
                        let other_identifications_link = other_identifications_link.borrow().clone().unwrap();
                        other_identifications_link.send_message(OtherIdentificationMsg::Generate)
                    }

                    for organizational_link in self.organizational_links.iter() {
                        let organizational_link = organizational_link.borrow().clone().unwrap();
                        organizational_link.send_message(OrganizationalMsg::Generate)
                    }

                    for communcation_link in self.communcation_links.iter() {
                        let communcation_link = communcation_link.borrow().clone().unwrap();
                        communcation_link.send_message(CommunicationMsg::Generate)
                    }
                }
                /*
                    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.")),
                        }
                    }
                }
                */

                true
            }
            Msg::GeneratedName(name) => {
                self.answer_count += 1;

                match self.vcard_data.get_mut() {
                    Some(vcard_data) => vcard_data.add_name(name),
                    None => ConsoleService::info(
                        "Error in GeneratedName: Couldn't get mutable borrow of VCardData",
                    ),
                };

                true
            }
            Msg::GeneratedOtherIdentification(other_identification) => {
                self.answer_count += 1;

                match self.vcard_data.get_mut() {
                    Some(vcard_data) => vcard_data.add_other_identification(other_identification),
                    None => ConsoleService::info(
                        "Error in GeneratedOtherIdentification: Couldn't get mutable borrow of VCardData",
                    ),
                };

                true
            }
            Msg::GeneratedAddress(address) => {
                self.answer_count += 1;

                match self.vcard_data.get_mut() {
                    Some(vcard_data) => vcard_data.add_address(address),
                    None => ConsoleService::info(
                        "Error in GeneratedAddress: Couldn't get mutable borrow of VCardData",
                    ),
                };

                true
            }
            Msg::GeneratedTelephone(telephone) => {
                self.answer_count += 1;

                match self.vcard_data.get_mut() {
                    Some(vcard_data) => vcard_data.add_telephone(telephone),
                    None => ConsoleService::info(
                        "Error in GeneratedTelephone: Couldn't get mutable borrow of VCardData",
                    ),
                };

                true
            }
            Msg::GeneratedCommunication(communication) => {
                self.answer_count += 1;

                match self.vcard_data.get_mut() {
                    Some(vcard_data) => vcard_data.add_communication(communication),
                    None => ConsoleService::info(
                        "Error in GeneratedCommunication: Couldn't get mutable borrow of VCardData",
                    ),
                };

                true
            }
            Msg::GeneratedOrganizational(organizational) => {
                self.answer_count += 1;

                match self.vcard_data.get_mut() {
                    Some(vcard_data) => vcard_data.add_organizational(organizational),
                    None => ConsoleService::info("Error in GeneratedOrganizational: Couldn't get mutable borrow of VCardData"),
                };

                true
            }
            Msg::GenerationComplete => {
                self.answer_count = 0;

                let vcard_data = self.vcard_data.clone_inner();

                let mut builder = VcardBuilder::new();

                for name in vcard_data.names {
                    if !name.is_empty() {
                        builder = builder.with_fullname(name.generate_fn()).with_name(
                            parameters!(),
                            (!name.last_name.is_empty()).as_some(name.last_name.clone()),
                            (!name.first_name.is_empty()).as_some(name.first_name.clone()),
                            (!name.middle_name.is_empty()).as_some(name.middle_name.clone()),
                            (!name.prefix.is_empty()).as_some(name.prefix.clone()),
                            (!name.suffix.is_empty()).as_some(name.suffix.clone()),
                        );
                    }
                }

                for address in vcard_data.addresses {
                    if !address.is_empty() {
                        let mut types = String::new();
                        if address.work {
                            types.push_str("WORK");
                        }
                        if address.home {
                            if !types.is_empty() {
                                types.push(',');
                            }
                            types.push_str("HOME")
                        }

                        let params = if types.is_empty() {
                            parameters!()
                        } else {
                            parameters!("TYPE" => types)
                        };

                        builder = builder.with_adr(
                            params,
                            (!address.post_office_box.is_empty())
                                .as_some(address.post_office_box.clone()),
                            (!address.extension.is_empty()).as_some(address.extension.clone()),
                            (!address.street.is_empty()).as_some(address.street.clone()),
                            (!address.locality.is_empty()).as_some(address.locality.clone()),
                            (!address.region.is_empty()).as_some(address.region.clone()),
                            (!address.code.is_empty()).as_some(address.code.clone()),
                            (!address.country.is_empty()).as_some(address.country.clone()),
                        );
                    }
                }

                for telephone in vcard_data.telephones {
                    if !telephone.is_empty() {
                        let mut types = String::new();
                        if telephone.work {
                            types.push_str("WORK");
                        }
                        if telephone.home {
                            if !types.is_empty() {
                                types.push(',');
                            }
                            types.push_str("HOME")
                        }
                        if telephone.text {
                            if !types.is_empty() {
                                types.push(',');
                            }
                            types.push_str("TEXT")
                        }
                        if telephone.voice {
                            if !types.is_empty() {
                                types.push(',');
                            }
                            types.push_str("VOICE")
                        }
                        if telephone.fax {
                            if !types.is_empty() {
                                types.push(',');
                            }
                            types.push_str("FAX")
                        }
                        if telephone.cell {
                            if !types.is_empty() {
                                types.push(',');
                            }
                            types.push_str("CELL")
                        }
                        if telephone.video {
                            if !types.is_empty() {
                                types.push(',');
                            }
                            types.push_str("VIDEO")
                        }
                        if telephone.pager {
                            if !types.is_empty() {
                                types.push(',');
                            }
                            types.push_str("PAGER")
                        }
                        if telephone.text_phone {
                            if !types.is_empty() {
                                types.push(',');
                            }
                            types.push_str("TEXTPHONE")
                        }

                        let params = if types.is_empty() {
                            parameters!()
                        } else {
                            parameters!("TYPE" => types)
                        };

                        builder = builder.with_tel(params, telephone.number.clone());
                    }
                }

                for communication in vcard_data.communications {
                    if !communication.email_address.is_empty() {
                        builder = builder.with_email(communication.email_address.clone());
                    }
                    if !communication.impp.is_empty() {
                        builder = builder.with_impp(communication.impp.clone());
                    }
                }

                for other_identification in vcard_data.other_identifications {
                    if !other_identification.nickname.is_empty() {
                        builder =
                            builder.with_nickname(parameters!(), other_identification.nickname);
                    }

                    match other_identification.photo {
                        Some(file) => builder = builder.with_photo(parameters!(), file.content),
                        None => (),
                    };

                    if !other_identification.anniversary.is_empty() {
                        builder = builder.with_anniversary(other_identification.anniversary);
                    }

                    if !other_identification.birthday.is_empty() {
                        builder = builder.with_bday(parameters!(), other_identification.birthday);
                    }

                    if !other_identification.gender.is_empty() {
                        builder = builder.with_gender(parameters!(), other_identification.gender);
                    }
                }

                for organizational in vcard_data.organizationals {
                    if !organizational.org.is_empty() {
                        builder = builder.with_org(vec![organizational.org]);
                    }

                    match organizational.logo {
                        Some(file) => builder = builder.with_logo(file.content),
                        None => (),
                    };

                    if !organizational.title.is_empty() {
                        builder = builder.with_title(organizational.title);
                    }

                    if !organizational.role.is_empty() {
                        builder = builder.with_role(organizational.role);
                    }

                    if !organizational.member.is_empty() {
                        builder = builder.with_member(organizational.member);
                    }

                    if !organizational.related.is_empty() {
                        builder = builder.with_related(organizational.related);
                    }
                }

                let uid = format!("urn:uuid:{}", Uuid::new_v4());

                let rev = Local::now().to_string();

                match builder
                    .with_version("4.0".to_string())
                    .with_rev(rev)
                    .with_uid(uid)
                    .build()
                {
                    Ok(vcard) => {
                        match self.selected_option {
                            DownloadOption::VCard => {
                                self.download = Some(Download {
                                    file_name: String::from("VCard.vcs"),
                                    content: vobject::write_component(&vcard),
                                    mime_type: MimeType::VCard,
                                });
                            }
                            DownloadOption::QrCode => {
                                match QrCode::encode_text(
                                    &vobject::write_component(&vcard),
                                    QrCodeEcc::Low,
                                ) {
                                    Ok(qr) => {
                                        self.download = Some(Download {
                                            file_name: String::from("QR-Code VCard.svg"),
                                            content: qr.to_svg_string(4),
                                            mime_type: MimeType::SVG,
                                        })
                                    }
                                    Err(_) => {
                                        self.error = Some(Error {
                                            msg: String::from("Sorry, VCard is too long!"),
                                        })
                                    }
                                };
                            }
                            _ => (),
                        };
                    }
                    Err(err) => {
                        self.error = Some(Error {
                            msg: err.to_string(),
                        })
                    }
                };

                match self.vcard_data.get_mut() {
                    Some(vcard_data) => *vcard_data = VCardData::new(),
                    None => ConsoleService::info("Couldn't reset VCardData"),
                };

                true
            }
            Msg::Nope => false,
        };

        if self.answer_count >= self.get_subcomponent_count() {
            self.link.send_message(Msg::GenerationComplete);
        }
        if self.error.is_some() {
            self.download = None;
        }

        shouldrender
    }

    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::ChangeDownloadOption(DownloadOption::VCard),
                "pdf" => Msg::ChangeDownloadOption(DownloadOption::PDF),
                "qrcode" => Msg::ChangeDownloadOption(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_error() }

                            <div class="level">
                                <div class="level-left">
                                    <div class="level-item">
                                        <h2 class="subtitle is-2">{ "Names" }</h2>
                                    </div>
                                    <div class="level-item">
                                        <button onclick=self.link.callback(|_| Msg::AddName) class="button">{ "Add more" }</button>
                                    </div>
                                </div>
                            </div>

                            {
                                for self.name_links.iter().enumerate().map(move |(idx, link)|
                                    html!{
                                        <NameView   weak_link=link
                                                    generated=self.link.callback(
                                                        |n: Name|
                                                            Msg::GeneratedName(n)
                                                    )
                                                    delete=self.link.callback(move |_| Msg::DelName(idx))
                                        />
                                    }
                                )
                            }

                            <div class="level">
                                <div class="level-left">
                                    <div class="level-item">
                                        <h2 class="subtitle is-2">{ "Other Identification Properties" }</h2>
                                    </div>
                                    <div class="level-item">
                                        <button onclick=self.link.callback(|_| Msg::AddOtherIdentification) class="button">{ "Add more" }</button>
                                    </div>
                                </div>
                            </div>

                            {
                                for self.other_identifications_links.iter().enumerate().map(move |(idx, link)|
                                    html!{
                                        <OtherIdentificationView  weak_link=link
                                                    generated=self.link.callback(
                                                        |d: OtherIdentification|
                                                            Msg::GeneratedOtherIdentification(d)
                                                    )
                                                    delete=self.link.callback(move |_| Msg::DelOtherIdentification(idx))
                                        />
                                    }
                                )
                            }

                            <div class="level">
                                <div class="level-left">
                                    <div class="level-item">
                                        <h2 class="subtitle is-2">{ "Addresses" }</h2>
                                    </div>
                                    <div class="level-item">
                                        <button onclick=self.link.callback(|_| Msg::AddAddress) class="button">{ "Add more" }</button>
                                    </div>
                                </div>
                            </div>

                            {
                                for self.address_links.iter().enumerate().map(move |(idx, link)|
                                    html!{
                                        <AddressView    weak_link=link
                                                        generated=self.link.callback(
                                                            |a: Address|
                                                                Msg::GeneratedAddress(a)
                                                        )
                                                        delete=self.link.callback(move |_| Msg::DelAddress(idx))
                                        />
                                    }
                                )
                            }

                            <div class="level">
                                <div class="level-left">
                                    <div class="level-item">
                                        <h2 class="subtitle is-2">{ "Telephone Numbers" }</h2>
                                    </div>
                                    <div class="level-item">
                                        <button onclick=self.link.callback(|_| Msg::AddTelephone) class="button">{ "Add more" }</button>
                                    </div>
                                </div>
                            </div>

                            {
                                for self.telephone_links.iter().enumerate().map(move |(idx, link)|
                                    html!{
                                        <TelephoneView  weak_link=link
                                                        generated=self.link.callback(
                                                            |t: Telephone|
                                                                Msg::GeneratedTelephone(t)
                                                        )
                                                        delete=self.link.callback(move |_| Msg::DelTelephone(idx))
                                        />
                                    }
                                )
                            }

                            <div class="level">
                                <div class="level-left">
                                    <div class="level-item">
                                        <h2 class="subtitle is-2">{ "Communication" }</h2>
                                    </div>
                                    <div class="level-item">
                                        <button onclick=self.link.callback(|_| Msg::AddCommunication) class="button">{ "Add more" }</button>
                                    </div>
                                </div>
                            </div>

                            {
                                for self.communcation_links.iter().enumerate().map(move |(idx, link)|
                                    html!{
                                        <CommunicationView  weak_link=link
                                                        generated=self.link.callback(
                                                            |c: Communication|
                                                                Msg::GeneratedCommunication(c)
                                                        )
                                                        delete=self.link.callback(move |_| Msg::DelCommunication(idx))
                                        />
                                    }
                                )
                            }

                            <div class="level">
                                <div class="level-left">
                                    <div class="level-item">
                                        <h2 class="subtitle is-2">{ "Organisations" }</h2>
                                    </div>
                                    <div class="level-item">
                                        <button onclick=self.link.callback(|_| Msg::AddOrganizational) class="button">{ "Add more" }</button>
                                    </div>
                                </div>
                            </div>

                            {
                                for self.organizational_links.iter().enumerate().map(move |(idx, link)|
                                    html!{
                                        <OrganizationalView weak_link=link
                                                            generated=self.link.callback(
                                                                |o: Organizational|
                                                                    Msg::GeneratedOrganizational(o)
                                                            )
                                                            delete=self.link.callback(move |_| Msg::DelOrganizational(idx))
                                        />
                                    }
                                )
                            }

                            <div class="block level-left">
                                <button onclick=self.link.callback(|_| Msg::Generate) class="button is-primary level-item">{ "Generate" }</button>

                                <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_error(&self) -> Html {
        html! {
            <>
                {
                    match &self.error {
                        Some(error) => {
                            html!{
                                <div class="notification is-danger is-light">
                                    { error.msg.clone() }
                                </div>
                            }
                        },
                        None => html!{},
                    }
                }
            </>
        }
    }
    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-success 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_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();

        let mut decorator = genpdf::SimplePageDecorator::new();
        decorator.set_margins(10);
        doc.set_page_decorator(decorator);

        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(()),
        }
    }
    fn get_subcomponent_count(&self) -> usize {
        self.name_links.len()
            + self.address_links.len()
            + self.telephone_links.len()
            + self.other_identifications_links.len()
            + self.organizational_links.len()
            + self.communcation_links.len()
    }
}