atraczyk | c25f69d | 2016-11-22 19:55:16 -0500 | [diff] [blame] | 1 | /**************************************************************************
|
| 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 |
|
| 25 | using namespace RingClientUWP;
|
| 26 | using namespace VCardUtils;
|
| 27 |
|
| 28 | using namespace Windows::UI::Core;
|
| 29 |
|
| 30 | std::string
|
| 31 | getBetweenTokens( 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 |
|
| 40 | VCard::VCard(Contact^ owner) : m_Owner(owner)
|
| 41 | {}
|
| 42 |
|
| 43 | int
|
| 44 | VCard::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)) {
|
atraczyk | a57d717 | 2016-11-29 09:58:35 -0500 | [diff] [blame^] | 66 | 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)) {
|
atraczyk | c25f69d | 2016-11-22 19:55:16 -0500 | [diff] [blame] | 72 | 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();
|
atraczyk | a57d717 | 2016-11-29 09:58:35 -0500 | [diff] [blame^] | 85 | 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();
|
atraczyk | c25f69d | 2016-11-22 19:55:16 -0500 | [diff] [blame] | 89 | 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 |
|
| 100 | void
|
| 101 | VCard::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);
|
atraczyk | a57d717 | 2016-11-29 09:58:35 -0500 | [diff] [blame^] | 113 | std::string idkey = Utils::genID(0LL, 99999999LL);
|
atraczyk | c25f69d | 2016-11-22 19:55:16 -0500 | [diff] [blame] | 114 | 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 |
|
| 128 | void
|
| 129 | VCard::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 |
|
| 137 | std::string
|
| 138 | VCard::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 |
|
atraczyk | a57d717 | 2016-11-29 09:58:35 -0500 | [diff] [blame^] | 150 | ret << Property::FN << Symbols::SEPERATOR2 << m_mParts[Property::FN]
|
atraczyk | c25f69d | 2016-11-22 19:55:16 -0500 | [diff] [blame] | 151 | << 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 |
|
| 162 | void
|
| 163 | VCard::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();
|
atraczyk | a57d717 | 2016-11-29 09:58:35 -0500 | [diff] [blame^] | 179 | m_Owner->_avatarImage = Utils::toPlatformString(vcardDir + pngFile);
|
| 180 | MSG_("Done decoding and saving VCard Photo to PNG");
|
atraczyk | c25f69d | 2016-11-22 19:55:16 -0500 | [diff] [blame] | 181 | }
|
| 182 | }
|
| 183 | }
|
| 184 |
|
| 185 | void
|
| 186 | VCard::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 |
|
| 200 | int
|
| 201 | VCard::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 |
|
| 218 | void
|
| 219 | VCard::setData(std::map<std::string, std::string> data)
|
| 220 | {
|
| 221 | m_mParts = data;
|
| 222 | } |