Ring Daemon
Loading...
Searching...
No Matches
vcard.cpp
Go to the documentation of this file.
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
25namespace jami {
26namespace vCard {
27
28namespace utils {
29
31toMap(std::string_view content)
32{
33 VCardData vCard;
34
35 std::string_view line;
36 while (jami::getline(content, line)) {
37 if (line.size()) {
38 const auto dblptPos = line.find(':');
39 if (dblptPos == std::string::npos)
40 continue;
41 vCard.emplace(line.substr(0, dblptPos), line.substr(dblptPos + 1));
42 }
43 }
44 return vCard;
45}
46
49{
50 return {
51 {std::string(Property::VCARD_VERSION), "2.1"},
52 {std::string(Property::FORMATTED_NAME), ""},
53 {std::string(Property::PHOTO_PNG), ""},
54 };
55}
56
57std::string
58toString(const VCardData& vCard)
59{
60 size_t estimatedSize = 0;
61 for (const auto& [key, value] : vCard) {
63 continue;
64 estimatedSize += key.size() + value.size() + 2;
65 }
66 std::string result;
69
70 result += Delimiter::BEGIN_TOKEN;
72
73 for (const auto& [key, value] : vCard) {
75 continue;
76 result += key + ':';
77 result += value + '\n';
78 }
79
80 result += Delimiter::END_TOKEN;
82
83 return result;
84}
85
86void
87save(const VCardData& vCard, const std::filesystem::path& path, const std::filesystem::path& pathLink)
88{
89 std::filesystem::path tmpPath = path;
90 tmpPath += ".tmp";
91 std::ofstream file(tmpPath);
92 if (file.is_open()) {
94 file.close();
95 std::filesystem::rename(tmpPath, path);
97 }
98}
99
100void
101removeByKey(VCardData& vCard, std::string_view key)
102{
103 for (auto it = vCard.begin(); it != vCard.end();) {
104 if (jami::starts_with(it->first, key)) {
105 it = vCard.erase(it);
106 } else {
107 ++it;
108 }
109 }
110}
111
112} // namespace utils
113} // namespace vCard
114} // namespace jami
bool createFileLink(const std::filesystem::path &linkFile, const std::filesystem::path &target, bool hard)
void removeByKey(VCardData &vCard, std::string_view key)
Definition vcard.cpp:101
std::map< std::string, std::string, std::less<> > VCardData
Definition vcard.h:89
std::string toString(const VCardData &vCard)
Definition vcard.cpp:58
VCardData toMap(std::string_view content)
Payload to vCard.
Definition vcard.cpp:31
void save(const VCardData &vCard, const std::filesystem::path &path, const std::filesystem::path &pathLink)
Definition vcard.cpp:87
VCardData initVcard()
Definition vcard.cpp:48
void emitSignal(Args... args)
Definition jami_signal.h:64
bool getline(std::string_view &str, std::string_view &line, char delim='\n')
Similar to @getline_full but skips empty results.
static bool starts_with(std::string_view str, std::string_view prefix)
static constexpr std::string_view END_LINE_TOKEN
Definition vcard.h:30
static constexpr std::string_view BEGIN_TOKEN
Definition vcard.h:31
static constexpr std::string_view BEGIN_TOKEN_KEY
Definition vcard.h:33
static constexpr std::string_view END_TOKEN
Definition vcard.h:32
static constexpr std::string_view END_TOKEN_KEY
Definition vcard.h:34
static constexpr std::string_view VCARD_VERSION
Definition vcard.h:41
static constexpr std::string_view PHOTO_PNG
Definition vcard.h:74
static constexpr std::string_view FORMATTED_NAME
Definition vcard.h:49