Commit 6c939d1f authored by Ming Rui Zhang's avatar Ming Rui Zhang Committed by Andreas Traczyk
Browse files

contact: rename the vcf file if using peer uri fails

- if a contact uri contains special characters, Qfile will fail,
  we instead encode it in Base64 format and retry
- change conatct added signals and slots to send profile info instead
  of directing sending uri

Change-Id: I631ea69f672d2f4912b1784828a7d5ca5393ed8f
parent ec0c0388
......@@ -29,6 +29,7 @@
#include <datatransfer_interface.h>
#include <QImage>
#include <QByteArray>
#include <QBuffer>
#include <QJsonObject>
#include <QJsonDocument>
......@@ -221,14 +222,17 @@ setProfile(const std::string& accountId,
auto vcard = vcard::profileToVcard(profileInfo);
auto accountLocalPath = getPath() + QString::fromStdString(accountId) + "/";
QString filePath;
QFile file;
if (isPeer) {
filePath = accountLocalPath + "profiles/" + QString::fromStdString(profileInfo.uri) + ".vcf";
filePath = accountLocalPath + "profiles/" +
QString(QByteArray::fromStdString(profileInfo.uri).toBase64()) + ".vcf";
file.setFileName(filePath);
} else {
filePath = accountLocalPath + "profile" + ".vcf";
file.setFileName(filePath);
}
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly)) {
qWarning() << "Can't open file: " << filePath;
qWarning().noquote() << "Can't open file: " << filePath;
return;
}
QTextStream(&file) << QString::fromStdString(vcard);
......@@ -298,12 +302,23 @@ buildContactFromProfile(const std::string & accountId,
QString filePath;
filePath = accountLocalPath + "profiles/" + QString::fromStdString(peer_uri) + ".vcf";
QFile file(filePath);
bool deleteOld = false;
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Can't open file: " << filePath;
return { profileInfo, "", true, false };
qWarning().noquote() << "Can't open file: " << filePath << ". Trying Base64 file path.";
filePath = accountLocalPath + "profiles/" + QString(QByteArray::fromStdString(peer_uri).toBase64()) + ".vcf";
file.setFileName(filePath);
if (!file.open(QIODevice::ReadOnly)) {
qWarning().noquote() << "Can't open file (base64 file path): " << filePath;
return { profileInfo, "", true, false };
}
} else {
deleteOld = true;
}
QTextStream in(&file);
QByteArray vcard = in.readAll().toUtf8();
if (deleteOld) {
file.remove();
}
const auto vCard = lrc::vCard::utils::toHashMap(vcard);
const auto alias = vCard[vCard::Property::FORMATTED_NAME];
const auto photo = (vCard.find(vCard::Property::PHOTO_PNG) == vCard.end()) ?
......
......@@ -183,7 +183,7 @@ public Q_SLOTS:
* Listen from contactModel when a new contact is added
* @param uri
*/
void slotContactAdded(const std::string& uri);
void slotContactAdded(const std::string& contactUri);
/**
* Listen from contactModel when a pending contact is accepted
* @param uri
......@@ -1408,33 +1408,34 @@ ConversationModelPimpl::sendContactRequest(const std::string& contactUri)
}
void
ConversationModelPimpl::slotContactAdded(const std::string& uri)
ConversationModelPimpl::slotContactAdded(const std::string& contactUri)
{
auto type = linked.owner.profileInfo.type;
profile::Info profileInfo{ contactUri, {}, {}, type };
try {
auto contact = linked.owner.contactModel->getContact(uri);
auto contact = linked.owner.contactModel->getContact(contactUri);
type = contact.profileInfo.type;
profileInfo.alias = contact.profileInfo.alias;
} catch (...) {}
profile::Info profileInfo{ uri, {}, {}, type };
storage::createOrUpdateProfile(linked.owner.id, profileInfo, true);
auto conv = storage::getConversationsWithPeer(db, uri);
auto conv = storage::getConversationsWithPeer(db, profileInfo.uri);
if (conv.empty()) {
// pass conversation UID through only element
conv.emplace_back(storage::beginConversationWithPeer(db, uri));
conv.emplace_back(storage::beginConversationWithPeer(db, profileInfo.uri));
}
// Add the conversation if not already here
if (indexOf(conv[0]) == -1) {
addConversationWith(conv[0], uri);
addConversationWith(conv[0], profileInfo.uri);
emit linked.newConversation(conv[0]);
}
// delete temporary conversation if it exists and it has the uri of the added contact as uid
if (indexOf(uri) >= 0) {
conversations.erase(conversations.begin() + indexOf(uri));
if (indexOf(profileInfo.uri) >= 0) {
conversations.erase(conversations.begin() + indexOf(profileInfo.uri));
}
sortConversations();
emit linked.conversationReady(uri);
emit linked.conversationReady(profileInfo.uri);
emit linked.modelSorted();
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment