@@ -71,6 +71,33 @@ namespace {
71
71
return result;
72
72
}
73
73
74
+ static void serialize (QDomElement parent, const PHistorical &historical, const char *tagName)
75
+ {
76
+ std::visit (
77
+ [&](auto const &v) mutable {
78
+ if (v.isNull ()) {
79
+ return ;
80
+ }
81
+ auto doc = parent.ownerDocument ();
82
+ auto bday = parent.appendChild (doc.createElement (QLatin1String (tagName))).toElement ();
83
+ historical.first .addTo (bday);
84
+ using Tv = std::decay_t <decltype (v)>;
85
+ if constexpr (std::is_same_v<Tv, QString>) {
86
+ VCardHelper::addTextElement (doc, bday, QLatin1String (" text" ), QStringList { v });
87
+ } else if constexpr (std::is_same_v<Tv, QDate>) {
88
+ VCardHelper::addTextElement (doc, bday, QLatin1String (" date" ),
89
+ QStringList { v.toString (Qt::ISODate) });
90
+ } else if constexpr (std::is_same_v<Tv, QDateTime>) {
91
+ VCardHelper::addTextElement (doc, bday, QLatin1String (" date-time" ),
92
+ QStringList { v.toString (Qt::ISODate) });
93
+ } else if constexpr (std::is_same_v<Tv, QTime>) {
94
+ VCardHelper::addTextElement (doc, bday, QLatin1String (" time" ),
95
+ QStringList { v.toString (Qt::ISODate) });
96
+ }
97
+ },
98
+ historical.second );
99
+ }
100
+
74
101
template <typename ListType>
75
102
static void addTextElement (QDomDocument &document, QDomElement &parent, const QString &tagName,
76
103
const ListType &texts)
@@ -86,10 +113,10 @@ namespace {
86
113
}
87
114
88
115
template <typename T>
89
- static void serializeList (QDomDocument &document, QDomElement &parent,
90
- const QList<std::pair<Parameters, T>> &list, const QString &tagName,
91
- const QString &innerTagName = QLatin1String(" text" ))
116
+ static void serializeList (QDomElement &parent, const QList<std::pair<Parameters, T>> &list,
117
+ const QString &tagName, const QString &innerTagName = QLatin1String(" text" ))
92
118
{
119
+ auto document = parent.ownerDocument ();
93
120
for (const auto &entry : list) {
94
121
QDomElement element = document.createElement (tagName);
95
122
entry.first .addTo (element);
@@ -219,6 +246,36 @@ namespace {
219
246
}
220
247
}
221
248
};
249
+
250
+ static void unserialize (const QDomElement &parent, const char *tagName, PHistorical &to)
251
+ {
252
+ QDomElement source = parent.firstChildElement (QLatin1String (tagName));
253
+ if (source.isNull ()) {
254
+ return ;
255
+ }
256
+ to.first = Parameters (source.firstChildElement (QLatin1String (" parameters" )));
257
+ auto v = VCardHelper::extractText (source, " date" );
258
+ if (v.isNull ()) {
259
+ v = VCardHelper::extractText (source, " date-time" );
260
+ if (v.isNull ()) {
261
+ v = VCardHelper::extractText (source, " time" );
262
+ if (v.isNull ()) {
263
+ to.second = VCardHelper::extractText (source, " text" );
264
+ } else {
265
+ to.second = QTime::fromString (v, Qt::ISODate);
266
+ }
267
+ } else {
268
+ to.second = QDateTime::fromString (v, Qt::ISODate);
269
+ }
270
+ } else {
271
+ to.second = QDate::fromString (v, Qt::ISODate);
272
+ }
273
+ }
274
+
275
+ static bool isNull (const PHistorical &h)
276
+ {
277
+ return std::visit ([](auto const &v) { return v.isNull (); }, h.second );
278
+ }
222
279
};
223
280
224
281
} // namespace
@@ -417,8 +474,8 @@ class VCard::VCardData : public QSharedData {
417
474
PNames names; // at most 1
418
475
PStringLists nickname;
419
476
PAdvUris photo;
420
- PDate bday; // at most 1
421
- PDate anniversary; // at most 1
477
+ PHistorical bday; // at most 1
478
+ PHistorical anniversary; // at most 1
422
479
VCard4::Gender gender;
423
480
QString genderComment;
424
481
@@ -495,17 +552,8 @@ class VCard::VCardData : public QSharedData {
495
552
VCardHelper::fillContainer (element, " email" , emails);
496
553
VCardHelper::fillContainer (element, " key" , key);
497
554
498
- QDomElement bdayElement = element.firstChildElement (QLatin1String (" bday" ));
499
- if (!bdayElement.isNull ()) {
500
- bday.first = Parameters (bdayElement.firstChildElement (QLatin1String (" parameters" )));
501
- bday.second = QDate::fromString (VCardHelper::extractText (bdayElement, " date" ), Qt::ISODate);
502
- }
503
-
504
- QDomElement anniversaryElement = element.firstChildElement (QLatin1String (" anniversary" ));
505
- if (!anniversaryElement.isNull ()) {
506
- anniversary.first = Parameters (anniversaryElement.firstChildElement (QLatin1String (" parameters" )));
507
- anniversary.second = QDate::fromString (VCardHelper::extractText (anniversaryElement, " date" ), Qt::ISODate);
508
- }
555
+ VCardHelper::unserialize (element, " bday" , bday);
556
+ VCardHelper::unserialize (element, " anniversary" , anniversary);
509
557
510
558
QDomElement genderElement = element.firstChildElement (QLatin1String (" sex" ));
511
559
if (!genderElement.isNull ()) {
@@ -559,7 +607,7 @@ class VCard::VCardData : public QSharedData {
559
607
{
560
608
return fullName.isEmpty () && names.second .isEmpty () && nickname.isEmpty () && emails.isEmpty () && tels.isEmpty ()
561
609
&& org.isEmpty () && title.isEmpty () && role.isEmpty () && note.isEmpty () && urls.isEmpty ()
562
- && bday. second . isNull () && anniversary. second . isNull () && gender == VCard4::Gender::Undefined
610
+ && VCardHelper:: isNull (bday ) && VCardHelper:: isNull (anniversary ) && gender == VCard4::Gender::Undefined
563
611
&& uid.isEmpty () && kind.isEmpty () && categories.isEmpty () && busyTimeUrl.isEmpty ()
564
612
&& calendarRequestUri.isEmpty () && calendarUri.isEmpty () && clientPidMap.isEmpty () && geo.isEmpty ()
565
613
&& impp.isEmpty () && key.isEmpty () && lang.isEmpty () && logo.isEmpty () && member.isEmpty ()
@@ -591,34 +639,22 @@ QDomElement VCard::toXmlElement(QDomDocument &document) const
591
639
592
640
QDomElement vCardElement = document.createElement (QLatin1String (" vcard" ));
593
641
594
- VCardHelper::serializeList (document, vCardElement, d->fullName , QLatin1String (" fn" ));
642
+ VCardHelper::serializeList (vCardElement, d->fullName , QLatin1String (" fn" ));
595
643
if (!d->names .second .isEmpty ()) {
596
644
auto e = vCardElement.appendChild (d->names .second .toXmlElement (document)).toElement ();
597
645
d->names .first .addTo (e);
598
646
}
599
- VCardHelper::serializeList (document, vCardElement, d->nickname , QLatin1String (" nickname" ), QLatin1String (" text" ));
600
- VCardHelper::serializeList (document, vCardElement, d->emails , QLatin1String (" email" ), QLatin1String (" text" ));
601
- VCardHelper::serializeList (document, vCardElement, d->tels , QLatin1String (" tel" ), QLatin1String (" uri" ));
602
- VCardHelper::serializeList (document, vCardElement, d->org , QLatin1String (" org" ), QLatin1String (" text" ));
603
- VCardHelper::serializeList (document, vCardElement, d->title , QLatin1String (" title" ), QLatin1String (" text" ));
604
- VCardHelper::serializeList (document, vCardElement, d->role , QLatin1String (" role" ), QLatin1String (" text" ));
605
- VCardHelper::serializeList (document, vCardElement, d->note , QLatin1String (" note" ), QLatin1String (" text" ));
606
- VCardHelper::serializeList (document, vCardElement, d->urls , QLatin1String (" url" ), QLatin1String (" uri" ));
607
-
608
- if (!d->bday .second .isNull ()) {
609
- QDomElement bdayElement = document.createElement (QLatin1String (" bday" ));
610
- d->bday .first .addTo (bdayElement);
611
- VCardHelper::addTextElement (document, bdayElement, QLatin1String (" date" ), d->bday .second .toString (Qt::ISODate));
612
- vCardElement.appendChild (bdayElement);
613
- }
614
-
615
- if (!d->anniversary .second .isNull ()) {
616
- QDomElement anniversaryElement = document.createElement (QLatin1String (" anniversary" ));
617
- d->anniversary .first .addTo (anniversaryElement);
618
- VCardHelper::addTextElement (document, anniversaryElement, QLatin1String (" date" ),
619
- d->anniversary .second .toString (Qt::ISODate));
620
- vCardElement.appendChild (anniversaryElement);
621
- }
647
+ VCardHelper::serializeList (vCardElement, d->nickname , QLatin1String (" nickname" ), QLatin1String (" text" ));
648
+ VCardHelper::serializeList (vCardElement, d->emails , QLatin1String (" email" ), QLatin1String (" text" ));
649
+ VCardHelper::serializeList (vCardElement, d->tels , QLatin1String (" tel" ), QLatin1String (" uri" ));
650
+ VCardHelper::serializeList (vCardElement, d->org , QLatin1String (" org" ), QLatin1String (" text" ));
651
+ VCardHelper::serializeList (vCardElement, d->title , QLatin1String (" title" ), QLatin1String (" text" ));
652
+ VCardHelper::serializeList (vCardElement, d->role , QLatin1String (" role" ), QLatin1String (" text" ));
653
+ VCardHelper::serializeList (vCardElement, d->note , QLatin1String (" note" ), QLatin1String (" text" ));
654
+ VCardHelper::serializeList (vCardElement, d->urls , QLatin1String (" url" ), QLatin1String (" uri" ));
655
+
656
+ VCardHelper::serialize (vCardElement, d->bday , " bday" );
657
+ VCardHelper::serialize (vCardElement, d->bday , " anniversary" );
622
658
623
659
if (d->gender != VCard4::Gender::Undefined) {
624
660
QDomElement genderElement = document.createElement (QLatin1String (" gender" ));
@@ -629,29 +665,27 @@ QDomElement VCard::toXmlElement(QDomDocument &document) const
629
665
vCardElement.appendChild (genderElement);
630
666
}
631
667
632
- VCardHelper::serializeList (document, vCardElement, d->categories , QLatin1String (" categories" ),
633
- QLatin1String (" text" ));
634
- VCardHelper::serializeList (document, vCardElement, d->busyTimeUrl , QLatin1String (" fburl" ), QLatin1String (" uri" ));
635
- VCardHelper::serializeList (document, vCardElement, d->calendarRequestUri , QLatin1String (" caladruri" ),
636
- QLatin1String (" uri" ));
637
- VCardHelper::serializeList (document, vCardElement, d->calendarUri , QLatin1String (" caluri" ), QLatin1String (" uri" ));
668
+ VCardHelper::serializeList (vCardElement, d->categories , QLatin1String (" categories" ), QLatin1String (" text" ));
669
+ VCardHelper::serializeList (vCardElement, d->busyTimeUrl , QLatin1String (" fburl" ), QLatin1String (" uri" ));
670
+ VCardHelper::serializeList (vCardElement, d->calendarRequestUri , QLatin1String (" caladruri" ), QLatin1String (" uri" ));
671
+ VCardHelper::serializeList (vCardElement, d->calendarUri , QLatin1String (" caluri" ), QLatin1String (" uri" ));
638
672
for (auto it = d->clientPidMap .cbegin (); it != d->clientPidMap .cend (); ++it) {
639
673
auto m = vCardElement.appendChild (document.createElement (QLatin1String (" clientpidmap" )));
640
674
m.appendChild (document.createElement (QLatin1String (" sourceid" )))
641
675
.appendChild (document.createTextNode (QString::number (it.key ())));
642
676
m.appendChild (document.createElement (QLatin1String (" uri" ))).appendChild (document.createTextNode (it.value ()));
643
677
}
644
- VCardHelper::serializeList (document, vCardElement, d->geo , QLatin1String (" geo" ), QLatin1String (" uri" ));
645
- VCardHelper::serializeList (document, vCardElement, d->impp , QLatin1String (" impp" ), QLatin1String (" uri" ));
646
- VCardHelper::serializeList (document, vCardElement, d->key , QLatin1String (" key" ), QLatin1String (" uri" ));
647
- VCardHelper::serializeList (document, vCardElement, d->lang , QLatin1String (" lang" ), QLatin1String (" language-tag" ));
648
- VCardHelper::serializeList (document, vCardElement, d->logo , QLatin1String (" logo" ), QLatin1String (" uri" ));
649
- VCardHelper::serializeList (document, vCardElement, d->member , QLatin1String (" member" ), QLatin1String (" uri" ));
650
- VCardHelper::serializeList (document, vCardElement, d->photo , QLatin1String (" photo" ), QLatin1String (" uri" ));
651
- VCardHelper::serializeList (document, vCardElement, d->related , QLatin1String (" related" ), QLatin1String (" text" ));
652
- VCardHelper::serializeList (document, vCardElement, d->timeZone , QLatin1String (" tz" ), QLatin1String (" text" ));
653
- VCardHelper::serializeList (document, vCardElement, d->sound , QLatin1String (" sound" ), QLatin1String (" uri" ));
654
- VCardHelper::serializeList (document, vCardElement, d->source , QLatin1String (" source" ), QLatin1String (" uri" ));
678
+ VCardHelper::serializeList (vCardElement, d->geo , QLatin1String (" geo" ), QLatin1String (" uri" ));
679
+ VCardHelper::serializeList (vCardElement, d->impp , QLatin1String (" impp" ), QLatin1String (" uri" ));
680
+ VCardHelper::serializeList (vCardElement, d->key , QLatin1String (" key" ), QLatin1String (" uri" ));
681
+ VCardHelper::serializeList (vCardElement, d->lang , QLatin1String (" lang" ), QLatin1String (" language-tag" ));
682
+ VCardHelper::serializeList (vCardElement, d->logo , QLatin1String (" logo" ), QLatin1String (" uri" ));
683
+ VCardHelper::serializeList (vCardElement, d->member , QLatin1String (" member" ), QLatin1String (" uri" ));
684
+ VCardHelper::serializeList (vCardElement, d->photo , QLatin1String (" photo" ), QLatin1String (" uri" ));
685
+ VCardHelper::serializeList (vCardElement, d->related , QLatin1String (" related" ), QLatin1String (" text" ));
686
+ VCardHelper::serializeList (vCardElement, d->timeZone , QLatin1String (" tz" ), QLatin1String (" text" ));
687
+ VCardHelper::serializeList (vCardElement, d->sound , QLatin1String (" sound" ), QLatin1String (" uri" ));
688
+ VCardHelper::serializeList (vCardElement, d->source , QLatin1String (" source" ), QLatin1String (" uri" ));
655
689
656
690
if (d->rev .isValid ()) {
657
691
auto revE = vCardElement.appendChild (document.createElement (QLatin1String (" rev" )))
@@ -805,17 +839,17 @@ void VCard::setUrls(const PUris &urls)
805
839
d->urls = urls;
806
840
}
807
841
808
- PDate VCard::bday () const { return d ? d->bday : PDate (); }
842
+ PHistorical VCard::bday () const { return d ? d->bday : PHistorical (); }
809
843
810
- void VCard::setBday (const PDate &bday)
844
+ void VCard::setBday (const PHistorical &bday)
811
845
{
812
846
INIT_D ();
813
847
d->bday = bday;
814
848
}
815
849
816
- PDate VCard::anniversary () const { return d ? d->anniversary : PDate (); }
850
+ PHistorical VCard::anniversary () const { return d ? d->anniversary : PHistorical (); }
817
851
818
- void VCard::setAnniversary (const PDate &anniversary)
852
+ void VCard::setAnniversary (const PHistorical &anniversary)
819
853
{
820
854
INIT_D ();
821
855
d->anniversary = anniversary;
0 commit comments