summaryrefslogtreecommitdiff
path: root/src/model/vcard.rs
blob: ea662242437466c35808d0457e76d74596737d68 (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
use vobject::vcard::VcardBuilder;
use chrono::Local;
use uuid::Uuid;
use vobject::Vcard;
use crate::model::property_groups::address::Address;
use crate::model::property_groups::communication::Communication;
use crate::model::property_groups::name::Name;
use crate::model::property_groups::organizational::Organizational;
use crate::model::property_groups::other_identification::OtherIdentification;
use crate::model::property_groups::telephone::Telephone;
use vobject::error::Result as VResult;
use crate::model::VCardPropertyInputGroupObject;
use boolinator::Boolinator;
use vobject::parameters;
use genpdf::Element as _;
use genpdf::{elements, fonts, style};

/// Type that represents the data structure of a vcard.
#[derive(Clone, Debug)]
pub struct VCardData {
    pub names: Vec<Name>,
    pub addresses: Vec<Address>,
    pub telephones: Vec<Telephone>,
    pub communications: Vec<Communication>,
    pub other_identifications: Vec<OtherIdentification>,
    pub organizationals: Vec<Organizational>,
}

macro_rules! make_vec_adder_fn {
    ( fn $fnname:ident $property:ident => $($arg_name:ident : $arg_type:ty),* ) => {
        pub fn $fnname(&mut self, $( $arg_name : $arg_type ),*) {
            $(self.$property.push($arg_name);)*
        }
    };
}

impl VCardData {
    pub fn new() -> Self {
        Self {
            names: Vec::new(),
            addresses: Vec::new(),
            telephones: Vec::new(),
            communications: Vec::new(),
            other_identifications: Vec::new(),
            organizationals: Vec::new(),
        }
    }

    make_vec_adder_fn!( fn add_name names => name: Name );
    make_vec_adder_fn!( fn add_address addresses => address: Address );
    make_vec_adder_fn!( fn add_telephone telephones => telephone: Telephone );
    make_vec_adder_fn!( fn add_communication communications => communication: Communication );
    make_vec_adder_fn!( fn add_other_identification other_identifications => other_identification: OtherIdentification );
    make_vec_adder_fn!( fn add_organizational organizationals => organizational: Organizational );

    pub fn build_vcard(&self) -> VResult<Vcard> {

        let vcard_data = self.clone();
        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();

        builder
            .with_version("4.0".to_string())
            .with_rev(rev)
            .with_uid(uid)
            .build()
    }

    pub 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("wasm-card 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)),
        );

        let mut card_data = elements::Paragraph::new("");

        let vcard_data = self.clone();

        if let Some(name) = vcard_data.names.get(0) {
            card_data.push_styled(
                    name.generate_fn(), 
                    style::Effect::Bold
            );
        }
        card_data.push(
            ""
        );
        if let Some(communication) = vcard_data.communications.get(0) {
            card_data.push(
                communication.email_address.clone()
            );
        }
        card_data.push(
            ""
        );
        if let Some(address) = vcard_data.addresses.get(0) {
            card_data.push(
                address.street.clone()
            );
            card_data.push(
                format!("{} {} {}", address.code, address.locality, address.extension)
            );
            card_data.push(
                format!("{} {}", address.region, address.country)
            );
        }
        card_data.push(
            ""
        );
        for telephone in vcard_data.telephones {
            card_data.push(
                telephone.number
            );
        }

        let framed_card_data = elements::FramedElement::new(
            card_data
        );

        let mut table = elements::TableLayout::new(vec![1,1]);
        table.set_cell_decorator(
            elements::FrameCellDecorator::new(false, false, false)
        );
        for _i in 0..4 {
            table
                .row()
                .element(framed_card_data.clone())
                .element(framed_card_data.clone())
                .push()
                .expect("invalid table row");
        }

        doc.push(table);

        // 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(()),
        }
    }
}