Line data Source code
1 : /*
2 : * Copyright (C) 2004-2026 Savoir-faire Linux Inc.
3 : *
4 : * This program is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * This program is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 : */
17 : #include "vcard.h"
18 : #include "string_utils.h"
19 : #include "fileutils.h"
20 :
21 : #include <fstream>
22 : #include <filesystem>
23 : #include <map>
24 :
25 : namespace jami {
26 : namespace vCard {
27 :
28 : namespace utils {
29 :
30 : VCardData
31 118 : toMap(std::string_view content)
32 : {
33 118 : VCardData vCard;
34 :
35 118 : std::string_view line;
36 379 : while (jami::getline(content, line)) {
37 143 : if (line.size()) {
38 143 : const auto dblptPos = line.find(':');
39 143 : if (dblptPos == std::string::npos)
40 27 : continue;
41 116 : vCard.emplace(line.substr(0, dblptPos), line.substr(dblptPos + 1));
42 : }
43 : }
44 236 : return vCard;
45 0 : }
46 :
47 : VCardData
48 0 : initVcard()
49 : {
50 : return {
51 0 : {std::string(Property::VCARD_VERSION), "2.1"},
52 0 : {std::string(Property::FORMATTED_NAME), ""},
53 0 : {std::string(Property::PHOTO_PNG), ""},
54 0 : };
55 0 : }
56 :
57 : std::string
58 0 : toString(const VCardData& vCard)
59 : {
60 0 : size_t estimatedSize = 0;
61 0 : for (const auto& [key, value] : vCard) {
62 0 : if (Delimiter::BEGIN_TOKEN_KEY == key || Delimiter::END_TOKEN_KEY == key)
63 0 : continue;
64 0 : estimatedSize += key.size() + value.size() + 2;
65 : }
66 0 : std::string result;
67 0 : result.reserve(estimatedSize + Delimiter::BEGIN_TOKEN.size() + Delimiter::END_LINE_TOKEN.size()
68 0 : + Delimiter::END_TOKEN.size() + Delimiter::END_LINE_TOKEN.size());
69 :
70 0 : result += Delimiter::BEGIN_TOKEN;
71 0 : result += Delimiter::END_LINE_TOKEN;
72 :
73 0 : for (const auto& [key, value] : vCard) {
74 0 : if (Delimiter::BEGIN_TOKEN_KEY == key || Delimiter::END_TOKEN_KEY == key)
75 0 : continue;
76 0 : result += key + ':';
77 0 : result += value + '\n';
78 : }
79 :
80 0 : result += Delimiter::END_TOKEN;
81 0 : result += Delimiter::END_LINE_TOKEN;
82 :
83 0 : return result;
84 0 : }
85 :
86 : void
87 0 : save(const VCardData& vCard, const std::filesystem::path& path, const std::filesystem::path& pathLink)
88 : {
89 0 : std::filesystem::path tmpPath = path;
90 0 : tmpPath += ".tmp";
91 0 : std::ofstream file(tmpPath);
92 0 : if (file.is_open()) {
93 0 : file << vCard::utils::toString(vCard);
94 0 : file.close();
95 0 : std::filesystem::rename(tmpPath, path);
96 0 : fileutils::createFileLink(pathLink, path, true);
97 : }
98 0 : }
99 :
100 : void
101 0 : removeByKey(VCardData& vCard, std::string_view key)
102 : {
103 0 : for (auto it = vCard.begin(); it != vCard.end();) {
104 0 : if (jami::starts_with(it->first, key)) {
105 0 : it = vCard.erase(it);
106 : } else {
107 0 : ++it;
108 : }
109 : }
110 0 : }
111 :
112 : } // namespace utils
113 : } // namespace vCard
114 : } // namespace jami
|