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