File indexing completed on 2024-04-28 03:53:33

0001 /*
0002     This file is part of the KContacts framework.
0003     SPDX-FileCopyrightText: 2003 Tobias Koenig <tokoe@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "vcard_p.h"
0009 
0010 using namespace KContacts;
0011 
0012 VCard::VCard()
0013 {
0014 }
0015 
0016 VCard::VCard(const VCard &vcard)
0017     : mLineMap(vcard.mLineMap)
0018 {
0019 }
0020 
0021 VCard::~VCard()
0022 {
0023 }
0024 
0025 VCard &VCard::operator=(const VCard &vcard)
0026 {
0027     if (&vcard == this) {
0028         return *this;
0029     }
0030 
0031     mLineMap = vcard.mLineMap;
0032 
0033     return *this;
0034 }
0035 
0036 void VCard::clear()
0037 {
0038     mLineMap.clear();
0039 }
0040 
0041 QStringList VCard::identifiers() const
0042 {
0043     QStringList list;
0044     list.reserve(mLineMap.size());
0045     for (const auto &[lineId, l] : mLineMap) {
0046         list.append(lineId);
0047     }
0048     return list;
0049 }
0050 
0051 void VCard::addLine(const VCardLine &line)
0052 {
0053     auto it = findByLineId(line.identifier());
0054     if (it != mLineMap.end()) {
0055         it->list.append(line);
0056     } else {
0057         const LineData newdata{line.identifier(), {line}};
0058         auto beforeIt = std::lower_bound(mLineMap.begin(), mLineMap.end(), newdata);
0059         mLineMap.insert(beforeIt, newdata);
0060     }
0061 }
0062 
0063 VCardLine::List VCard::lines(const QString &identifier) const
0064 {
0065     auto it = findByLineId(identifier);
0066     return it != mLineMap.cend() ? it->list : VCardLine::List{};
0067 }
0068 
0069 VCardLine VCard::line(const QString &identifier) const
0070 {
0071     auto it = findByLineId(identifier);
0072     return it != mLineMap.cend() && !it->list.isEmpty() ? it->list.at(0) : VCardLine{};
0073 }
0074 
0075 static const char s_verStr[] = "VERSION";
0076 
0077 void VCard::setVersion(Version version)
0078 {
0079 
0080     VCardLine line;
0081     line.setIdentifier(QLatin1String(s_verStr));
0082     if (version == v2_1) {
0083         line.setIdentifier(QStringLiteral("2.1"));
0084     } else if (version == v3_0) {
0085         line.setIdentifier(QStringLiteral("3.0"));
0086     } else if (version == v4_0) {
0087         line.setIdentifier(QStringLiteral("4.0"));
0088     }
0089 
0090     auto it = findByLineId(QLatin1String(s_verStr));
0091     if (it != mLineMap.end()) {
0092         it->list.append(line);
0093     } else {
0094         const LineData newData{QLatin1String(s_verStr), {line}};
0095         auto beforeIt = std::lower_bound(mLineMap.begin(), mLineMap.end(), newData);
0096         mLineMap.insert(beforeIt, newData);
0097     }
0098 }
0099 
0100 VCard::Version VCard::version() const
0101 {
0102     auto versionEntryIt = findByLineId(QLatin1String(s_verStr));
0103     if (versionEntryIt == mLineMap.cend()) {
0104         return v3_0;
0105     }
0106 
0107     const VCardLine line = versionEntryIt->list.at(0);
0108     if (line.value() == QLatin1String("2.1")) {
0109         return v2_1;
0110     }
0111     if (line.value() == QLatin1String("3.0")) {
0112         return v3_0;
0113     }
0114 
0115     return v4_0;
0116 }