blob: 2a2e83124b006b722c98f26314624c6222565ae9 [file] [log] [blame]
atraczykc25f69d2016-11-22 19:55:16 -05001/**************************************************************************
2* Copyright (C) 2016 by Savoir-faire Linux *
3* Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> *
4* Author: Traczyk Andreas <traczyk.andreas@savoirfairelinux.com> *
5* *
6* This program is free software; you can redistribute it and/or modify *
7* it under the terms of the GNU General Public License as published by *
8* the Free Software Foundation; either version 3 of the License, or *
9* (at your option) any later version. *
10* *
11* This program is distributed in the hope that it will be useful, *
12* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14* GNU General Public License for more details. *
15* *
16* You should have received a copy of the GNU General Public License *
17* along with this program. If not, see <http://www.gnu.org/licenses/>. *
18**************************************************************************/
19#include <pch.h>
20
21#include "base64.h"
22
23#include <direct.h>
24
25using namespace RingClientUWP;
26using namespace VCardUtils;
27
28using namespace Windows::UI::Core;
29
30std::string
31getBetweenTokens( const std::string& str,
32 const char* token1,
33 const char* token2 = nullptr)
34{
35 std::size_t start = token1 ? (str.find(token1) + strlen(token1)) : 0;
36 std::size_t length = token2 ? (str.find(token2) - 1 - start) : std::string::npos;
37 return str.substr(start, length);
38}
39
40VCard::VCard(Contact^ owner) : m_Owner(owner)
41{}
42
43int
44VCard::receiveChunk(const std::string& args, const std::string& payload)
45{
46 MSG_("receive VCARD chunk");
47
48 int32_t _id = stoi( getBetweenTokens(args, Symbols::ID_TOKEN, Symbols::PART_TOKEN) );
49 uint32_t _part = stoi( getBetweenTokens(args, Symbols::PART_TOKEN, Symbols::OF_TOKEN) );
50 uint32_t _of = stoi( getBetweenTokens(args, Symbols::OF_TOKEN) );
51
52 std::stringstream _payload(payload);
53 std::string _line;
54
55 if (_part == 1) {
56
57 m_mParts.clear();
58
59 while (std::getline(_payload, _line)) {
60 if (_line.find("UID:") != std::string::npos)
61 break;
62 }
63 m_mParts[Property::UID] = _line.substr(4);
64
65 while (std::getline(_payload, _line)) {
atraczyka57d7172016-11-29 09:58:35 -050066 if (_line.find("FN:") != std::string::npos)
67 break;
68 }
69 m_mParts[Property::FN] = _line.substr(3);
70
71 while (std::getline(_payload, _line)) {
atraczykc25f69d2016-11-22 19:55:16 -050072 if (_line.find("PHOTO;") != std::string::npos)
73 break;
74 }
75 // because android client builds vcard differently (TYPE=PNG: vs PNG:)
76 m_mParts[Property::PHOTO].append(_line.substr(_line.find("PNG:") + 4));
77 return VCARD_INCOMPLETE;
78 }
79 else {
80 if (_part == _of) {
81 std::getline(_payload, _line);
82 m_mParts[Property::PHOTO].append(_line);
83 saveToFile();
84 decodeBase64ToPNGFile();
atraczyka57d7172016-11-29 09:58:35 -050085 if (!m_mParts[Property::FN].empty())
86 m_Owner->_displayName = Utils::toPlatformString(m_mParts[Property::FN]);
87 m_Owner->_vcardUID = Utils::toPlatformString(m_mParts[Property::UID]);
88 ViewModel::ContactsViewModel::instance->saveContactsToFile();
atraczykc25f69d2016-11-22 19:55:16 -050089 MSG_("VCARD_COMPLETE");
90 return VCARD_COMPLETE;
91 }
92 else {
93 m_mParts[Property::PHOTO].append(payload);
94 return VCARD_INCOMPLETE;
95 }
96 }
97 return VCARD_CHUNK_ERROR;
98}
99
100void
101VCard::send(std::string callID, const char* vCardFile)
102{
103 int i = 0;
104 const int chunkSize = 4096;
105 std::string vCard;
106 if (vCardFile) {
107 std::ifstream file(vCardFile);
108 vCard.assign(( std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
109 }
110 else
111 vCard = asString();
112 int total = vCard.size() / chunkSize + (vCard.size() % chunkSize ? 1 : 0);
atraczyka57d7172016-11-29 09:58:35 -0500113 std::string idkey = Utils::genID(0LL, 99999999LL);
atraczykc25f69d2016-11-22 19:55:16 -0500114 while ( vCard.size() ) {
115 std::map<std::string, std::string> chunk;
116 std::stringstream key;
117 key << PROFILE_VCF
118 << ";id=" << idkey
119 << ",part=" << std::to_string(i + 1)
120 << ",of=" << std::to_string(total);
121 chunk[key.str()] = vCard.substr(0, chunkSize);
122 vCard = vCard.erase(0, chunkSize);
123 ++i;
124 sendChunk(callID, chunk);
125 }
126}
127
128void
129VCard::sendChunk(std::string callID, std::map<std::string, std::string> chunk)
130{
131 MSG_("sending chunk...");
132 for(auto i : chunk)
133 MSG_("key:" + i.first + "\nvalue:" + i.second);
134 RingD::instance->sendSIPTextMessageVCF(callID, chunk);
135}
136
137std::string
138VCard::asString()
139{
140 std::stringstream ret;
141
142 ret << Symbols::BEGIN_TOKEN << Symbols::END_LINE_TOKEN;
143
144 ret << Property::VERSION << Symbols::SEPERATOR2 << "3.0"
145 << Symbols::END_LINE_TOKEN;
146
147 ret << Property::UID << Symbols::SEPERATOR2 << m_mParts[Property::UID]
148 << Symbols::END_LINE_TOKEN;
149
atraczyka57d7172016-11-29 09:58:35 -0500150 ret << Property::FN << Symbols::SEPERATOR2 << m_mParts[Property::FN]
atraczykc25f69d2016-11-22 19:55:16 -0500151 << Symbols::END_LINE_TOKEN;
152
153 ret << Property::PHOTO << Symbols::SEPERATOR1 << Symbols::PHOTO_ENC
154 << Symbols::SEPERATOR1 << Symbols::PHOTO_TYPE << Symbols::SEPERATOR2
155 << m_mParts[Property::PHOTO] << Symbols::END_LINE_TOKEN;
156
157 ret << Symbols::END_TOKEN;
158
159 return std::string(ret.str());
160}
161
162void
163VCard::decodeBase64ToPNGFile()
164{
165 size_t padding = m_mParts[Property::PHOTO].size() % 4;
166 if (padding)
167 m_mParts[Property::PHOTO].append(padding, 0);
168
169 std::vector<uint8_t> decodedData = ring::base64::decode(m_mParts[Property::PHOTO]);
170 std::string vcardDir(RingD::instance->getLocalFolder() + ".vcards\\");
171 std::string pngFile(m_mParts[Property::UID] + ".png");
172 MSG_("Saving PNG (" + m_mParts[Property::UID] + ") to " + pngFile + "...");
173 if (_mkdir(vcardDir.c_str())) {
174 std::ofstream file(vcardDir + pngFile, std::ios::out | std::ios::trunc | std::ios::binary);
175 if (file.is_open()) {
176 for (auto i : decodedData)
177 file << i;
178 file.close();
atraczyka57d7172016-11-29 09:58:35 -0500179 m_Owner->_avatarImage = Utils::toPlatformString(vcardDir + pngFile);
180 MSG_("Done decoding and saving VCard Photo to PNG");
atraczykc25f69d2016-11-22 19:55:16 -0500181 }
182 }
183}
184
185void
186VCard::encodePNGToBase64()
187{
188 std::string vcardDir(RingD::instance->getLocalFolder() + ".vcards\\");
189 std::string pngFile(m_mParts[Property::UID] + ".png");
190 std::basic_ifstream<uint8_t> file(vcardDir + pngFile, std::ios::in | std::ios::binary);
191 if (file.is_open()) {
192 auto eos = std::istreambuf_iterator<uint8_t>();
193 auto data = std::vector<uint8_t>(std::istreambuf_iterator<uint8_t>(file), eos);
194 m_mParts[Property::PHOTO] = ring::base64::encode(data);
195 file.close();
196 MSG_("Done encoding PNG to b64");
197 }
198}
199
200int
201VCard::saveToFile()
202{
203 std::string vcardDir(RingD::instance->getLocalFolder() + ".vcards\\");
204 std::string vcardFile(m_mParts[Property::UID] + ".vcard");
205 MSG_("Saving VCard (" + m_mParts[Property::UID] + ") to " + vcardFile + "...");
206 if (_mkdir(vcardDir.c_str())) {
207 std::ofstream file(vcardDir + vcardFile, std::ios::out | std::ios::trunc);
208 if (file.is_open()) {
209 file << asString();
210 file.close();
211 MSG_("Done Saving VCard");
212 return 1;
213 }
214 }
215 return 0;
216}
217
218void
219VCard::setData(std::map<std::string, std::string> data)
220{
221 m_mParts = data;
222}