From 36c71968bc46e0230e688cc897d72bd54a1964fe Mon Sep 17 00:00:00 2001 From: blue Date: Thu, 24 Oct 2019 12:42:38 +0300 Subject: [PATCH] basic avatar/vcard changes uploads and roster reaction --- core/account.cpp | 88 +++++++++++++++++++++++++++++++++++++++----- core/account.h | 1 + core/squawk.cpp | 10 +++++ core/squawk.h | 1 + global.h | 1 + main.cpp | 1 + ui/models/roster.cpp | 4 +- ui/widgets/vcard.cpp | 2 +- 8 files changed, 97 insertions(+), 11 deletions(-) diff --git a/core/account.cpp b/core/account.cpp index 04d2947..a6b0bde 100644 --- a/core/account.cpp +++ b/core/account.cpp @@ -103,15 +103,15 @@ Account::Account(const QString& p_login, const QString& p_server, const QString& if (!avatar->exists()) { delete avatar; avatar = new QFile(path + "/avatar.jpg"); - QString type = "jpg"; + type = "jpg"; if (!avatar->exists()) { delete avatar; avatar = new QFile(path + "/avatar.jpeg"); - QString type = "jpeg"; + type = "jpeg"; if (!avatar->exists()) { delete avatar; avatar = new QFile(path + "/avatar.gif"); - QString type = "gif"; + type = "gif"; } } } @@ -1337,7 +1337,6 @@ void Core::Account::onVCardReceived(const QXmppVCardIq& card) if (confItr == conferences.end()) { if (jid == getLogin() + "@" + getServer()) { onOwnVCardReceived(card); - } else { qDebug() << "received vCard" << jid << "doesn't belong to any of known contacts or conferences, skipping"; } @@ -1375,13 +1374,18 @@ void Core::Account::onVCardReceived(const QXmppVCardIq& card) vCard.setAvatarType(Shared::Avatar::empty); } + QMap cd = { + {"avatarState", static_cast(vCard.getAvatarType())}, + {"avatarPath", vCard.getAvatarPath()} + }; + emit changeContact(jid, cd); emit receivedVCard(jid, vCard); } void Core::Account::onOwnVCardReceived(const QXmppVCardIq& card) { QByteArray ava = card.photo(); - bool changed = false; + bool avaChanged = false; QString path = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/" + name + "/"; if (ava.size() > 0) { QCryptographicHash sha1(QCryptographicHash::Sha1); @@ -1407,7 +1411,7 @@ void Core::Account::onOwnVCardReceived(const QXmppVCardIq& card) newAvatar.close(); avatarHash = newHash; avatarType = newType.preferredSuffix(); - changed = true; + avaChanged = true; } else { qDebug() << "Received new avatar for account" << name << "but can't save it"; if (oldToRemove) { @@ -1425,7 +1429,7 @@ void Core::Account::onOwnVCardReceived(const QXmppVCardIq& card) newAvatar.close(); avatarHash = newHash; avatarType = newType.preferredSuffix(); - changed = true; + avaChanged = true; } else { qDebug() << "Received new avatar for account" << name << "but can't save it"; } @@ -1436,12 +1440,14 @@ void Core::Account::onOwnVCardReceived(const QXmppVCardIq& card) if (!oldAvatar.remove()) { qDebug() << "Received vCard for account" << name << "without avatar, but can't get rid of the file, doing nothing"; } else { - changed = true; + avatarType = ""; + avatarHash = ""; + avaChanged = true; } } } - if (changed) { + if (avaChanged) { QMap change; if (avatarType.size() > 0) { presence.setPhotoHash(avatarHash.toUtf8()); @@ -1453,6 +1459,7 @@ void Core::Account::onOwnVCardReceived(const QXmppVCardIq& card) change.insert("avatarPath", ""); } client.setClientPresence(presence); + emit changed(change); } ownVCardRequestInProgress = false; @@ -1504,3 +1511,66 @@ void Core::Account::requestVCard(const QString& jid) } } } + +void Core::Account::uploadVCard(const Shared::VCard& card) +{ + QXmppVCardIq iq; + iq.setFirstName(card.getFirstName()); + iq.setMiddleName(card.getMiddleName()); + iq.setLastName(card.getLastName()); + iq.setNickName(card.getNickName()); + iq.setBirthday(card.getBirthday()); + iq.setDescription(card.getDescription()); + + bool avatarChanged = false; + if (card.getAvatarType() == Shared::Avatar::empty) { + if (avatarType.size() > 0) { + avatarChanged = true; + } + } else { + QString newPath = card.getAvatarPath(); + QString oldPath = getAvatarPath(); + QByteArray data; + QString type; + if (newPath != oldPath) { + QFile avatar(newPath); + if (!avatar.open(QFile::ReadOnly)) { + qDebug() << "An attempt to upload new vCard to account" << name + << "but it wasn't possible to read file" << newPath + << "which was supposed to be new avatar, uploading old avatar"; + if (avatarType.size() > 0) { + QFile oA(oldPath); + if (!oA.open(QFile::ReadOnly)) { + qDebug() << "Couldn't read old avatar of account" << name << ", uploading empty avatar"; + avatarChanged = true; + } else { + data = oA.readAll(); + } + } + } else { + data = avatar.readAll(); + avatarChanged = true; + } + } else { + if (avatarType.size() > 0) { + QFile oA(oldPath); + if (!oA.open(QFile::ReadOnly)) { + qDebug() << "Couldn't read old avatar of account" << name << ", uploading empty avatar"; + avatarChanged = true; + } else { + data = oA.readAll(); + } + } + } + + if (data.size() > 0) { + QMimeDatabase db; + type = db.mimeTypeForData(data).name(); + iq.setPhoto(data); + iq.setPhotoType(type); + } + } + + client.vCardManager().setClientVCard(iq); + onOwnVCardReceived(iq); +} diff --git a/core/account.h b/core/account.h index b5b8c46..371a561 100644 --- a/core/account.h +++ b/core/account.h @@ -88,6 +88,7 @@ public: void removeRoomRequest(const QString& jid); void addRoomRequest(const QString& jid, const QString& nick, const QString& password, bool autoJoin); void requestVCard(const QString& jid); + void uploadVCard(const Shared::VCard& card); signals: void changed(const QMap& data); diff --git a/core/squawk.cpp b/core/squawk.cpp index 35977a4..9f421c9 100644 --- a/core/squawk.cpp +++ b/core/squawk.cpp @@ -543,3 +543,13 @@ void Core::Squawk::requestVCard(const QString& account, const QString& jid) } itr->second->requestVCard(jid); } + +void Core::Squawk::uploadVCard(const QString& account, const Shared::VCard& card) +{ + AccountsMap::const_iterator itr = amap.find(account); + if (itr == amap.end()) { + qDebug() << "An attempt to upload vcard to non existing account" << account << ", skipping"; + return; + } + itr->second->uploadVCard(card); +} diff --git a/core/squawk.h b/core/squawk.h index 9176f28..88ea860 100644 --- a/core/squawk.h +++ b/core/squawk.h @@ -93,6 +93,7 @@ public slots: void fileLocalPathRequest(const QString& messageId, const QString& url); void downloadFileRequest(const QString& messageId, const QString& url); void requestVCard(const QString& account, const QString& jid); + void uploadVCard(const QString& account, const Shared::VCard& card); private: typedef std::deque Accounts; diff --git a/global.h b/global.h index b20e616..0daa20b 100644 --- a/global.h +++ b/global.h @@ -437,6 +437,7 @@ static const std::map> icons = { {"view-refresh", {"view-refresh", "view-refresh"}}, {"send", {"document-send", "send"}}, {"clean", {"edit-clear-all", "clean"}}, + {"user", {"user", "user"}}, }; }; diff --git a/main.cpp b/main.cpp index 49a9875..1c455bc 100644 --- a/main.cpp +++ b/main.cpp @@ -106,6 +106,7 @@ int main(int argc, char *argv[]) QObject::connect(&w, &Squawk::removeContactFromGroupRequest, squawk, &Core::Squawk::removeContactFromGroupRequest); QObject::connect(&w, &Squawk::renameContactRequest, squawk, &Core::Squawk::renameContactRequest); QObject::connect(&w, &Squawk::requestVCard, squawk, &Core::Squawk::requestVCard); + QObject::connect(&w, &Squawk::uploadVCard, squawk, &Core::Squawk::uploadVCard); QObject::connect(squawk, &Core::Squawk::newAccount, &w, &Squawk::newAccount); QObject::connect(squawk, &Core::Squawk::addContact, &w, &Squawk::addContact); diff --git a/ui/models/roster.cpp b/ui/models/roster.cpp index 33998dc..6e49104 100644 --- a/ui/models/roster.cpp +++ b/ui/models/roster.cpp @@ -98,6 +98,7 @@ QVariant Models::Roster::data (const QModelIndex& index, int role) const result = acc->getStatusIcon(false); } else if (col == 1) { QString path = acc->getAvatarPath(); + if (path.size() > 0) { result = QIcon(path); } @@ -641,7 +642,8 @@ void Models::Roster::removeContact(const QString& account, const QString& jid, c void Models::Roster::onChildChanged(Models::Item* item, int row, int col) { QModelIndex index = createIndex(row, 0, item); - emit dataChanged(index, index); + QModelIndex index2 = createIndex(row, 1, item); + emit dataChanged(index, index2); } void Models::Roster::onChildIsAboutToBeInserted(Models::Item* parent, int first, int last) diff --git a/ui/widgets/vcard.cpp b/ui/widgets/vcard.cpp index 552e078..1da6c2e 100644 --- a/ui/widgets/vcard.cpp +++ b/ui/widgets/vcard.cpp @@ -66,7 +66,7 @@ VCard::VCard(const QString& jid, bool edit, QWidget* parent): } connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &VCard::onButtonBoxAccepted); - connect(m_ui->buttonBox, &QDialogButtonBox::rejected, m_ui->buttonBox, &QDialogButtonBox::deleteLater); + connect(m_ui->buttonBox, &QDialogButtonBox::rejected, this, &VCard::close); avatarButtonMargins = m_ui->avatarButton->size();