basic avatar/vcard changes uploads and roster reaction

This commit is contained in:
Blue 2019-10-24 12:42:38 +03:00
parent 652381b067
commit 36c71968bc
8 changed files with 97 additions and 11 deletions

View File

@ -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<QString, QVariant> cd = {
{"avatarState", static_cast<quint8>(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<QString, QVariant> 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);
}

View File

@ -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<QString, QVariant>& data);

View File

@ -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);
}

View File

@ -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<Account*> Accounts;

View File

@ -437,6 +437,7 @@ static const std::map<QString, std::pair<QString, QString>> icons = {
{"view-refresh", {"view-refresh", "view-refresh"}},
{"send", {"document-send", "send"}},
{"clean", {"edit-clear-all", "clean"}},
{"user", {"user", "user"}},
};
};

View File

@ -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);

View File

@ -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)

View File

@ -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();