Commit 8fa25830 authored by LNJ's avatar LNJ 💬

Add VCardModel to display user profile information

Co-authored-by: Robert Maerkisch's avatarRobert Maerkisch <zatroxde@protonmail.ch>
parent be85a6b4
......@@ -18,6 +18,7 @@ set(KAIDAN_SOURCES
src/PresenceCache.cpp
src/DiscoveryManager.cpp
src/VCardManager.cpp
src/VCardModel.cpp
src/LogHandler.cpp
src/StatusBar.cpp
src/UploadManager.cpp
......
......@@ -188,6 +188,11 @@ QString ClientWorker::generateRandomString(unsigned int length) const
return randomString;
}
VCardManager *ClientWorker::getVCardManager() const
{
return vCardManager;
}
void ClientWorker::setCsiState(Qt::ApplicationState state)
{
if (state == Qt::ApplicationActive)
......
......@@ -117,6 +117,8 @@ public:
ClientWorker(Caches *caches, Kaidan *kaidan, bool enableLogging, QGuiApplication *app,
QObject *parent = nullptr);
VCardManager *getVCardManager() const;
public slots:
/**
* Main function of the client thread
......
......@@ -269,6 +269,11 @@ void Kaidan::notifyLoginUriNotFound()
emit passiveNotificationRequested(tr("No valid login QR code found."));
}
ClientWorker *Kaidan::getClient() const
{
return m_client;
}
Kaidan *Kaidan::instance()
{
return s_instance;
......
......@@ -75,6 +75,8 @@ class Kaidan : public QObject
Q_PROPERTY(bool uploadServiceFound READ getUploadServiceFound NOTIFY uploadServiceFoundChanged)
public:
static Kaidan *instance();
Kaidan(QGuiApplication *app, bool enableLogging = true, QObject *parent = nullptr);
~Kaidan();
......@@ -192,6 +194,8 @@ public:
return m_utils;
}
ClientWorker *getClient() const;
/**
* Adds XMPP URI to open as soon as possible
*/
......@@ -212,8 +216,6 @@ public:
return uploadServiceFound;
}
static Kaidan *instance();
signals:
void avatarStorageChanged();
......@@ -279,7 +281,7 @@ signals:
* Is required when the avatar (or other information) of a JID are
* requested and the JID is not in the roster.
*/
void vCardRequested(QString jid);
void vCardRequested(const QString &jid);
/**
* XMPP URI received
......
......@@ -30,6 +30,7 @@
#include "VCardManager.h"
#include "AvatarFileStorage.h"
#include "Kaidan.h"
#include <QXmppClient.h>
#include <QXmppUtils.h>
#include <QXmppVCardIq.h>
......@@ -39,6 +40,7 @@ VCardManager::VCardManager(QXmppClient *client, AvatarFileStorage *avatars, QObj
{
connect(&manager, &QXmppVCardManager::vCardReceived, this, &VCardManager::handleVCard);
connect(client, &QXmppClient::presenceReceived, this, &VCardManager::handlePresence);
connect(Kaidan::instance(), &Kaidan::vCardRequested, this, &VCardManager::fetchVCard);
// Currently we're not requesting the own VCard on every connection because it is probably
// way too resource intensive on mobile connections with many reconnects.
......@@ -49,15 +51,20 @@ VCardManager::VCardManager(QXmppClient *client, AvatarFileStorage *avatars, QObj
// User Avatar to vCard-Based Avatars Conversion)
}
void VCardManager::fetchVCard(QString jid)
void VCardManager::fetchVCard(const QString& jid)
{
client->vCardManager().requestVCard(jid);
if (client->state() == QXmppClient::ConnectedState)
client->vCardManager().requestVCard(jid);
else
qWarning() << "[VCardManager] Could not fetch VCard: Not connected to a server";
}
void VCardManager::handleVCard(const QXmppVCardIq &iq)
{
if (!iq.photo().isEmpty())
avatarStorage->addAvatar(QXmppUtils::jidToBareJid(iq.from()), iq.photo());
emit vCardReceived(iq);
}
void VCardManager::handlePresence(const QXmppPresence &presence)
......
......@@ -40,13 +40,15 @@ class QXmppClient;
class VCardManager : public QObject
{
Q_OBJECT
public:
VCardManager(QXmppClient *client, AvatarFileStorage *avatars, QObject *parent = nullptr);
/**
* Will request the VCard from the server
*/
void fetchVCard(QString jid);
void fetchVCard(const QString& jid);
/**
* Handles incoming VCards and processes them (save avatar, etc.)
......@@ -58,6 +60,9 @@ public:
*/
void handlePresence(const QXmppPresence &presence);
signals:
void vCardReceived(const QXmppVCardIq &vCard);
private:
QXmppClient *client;
QXmppVCardManager &manager;
......
/*
* Kaidan - A user-friendly XMPP client for every device!
*
* Copyright (C) 2016-2019 Kaidan developers and contributors
* (see the LICENSE file for a full list of copyright authors)
*
* Kaidan is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition, as a special exception, the author of Kaidan gives
* permission to link the code of its release with the OpenSSL
* project's "OpenSSL" library (or with modified versions of it that
* use the same license as the "OpenSSL" library), and distribute the
* linked executables. You must obey the GNU General Public License in
* all respects for all of the code used other than "OpenSSL". If you
* modify this file, you may extend this exception to your version of
* the file, but you are not obligated to do so. If you do not wish to
* do so, delete this exception statement from your version.
*
* Kaidan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kaidan. If not, see <http://www.gnu.org/licenses/>.
*/
#include "VCardModel.h"
#include "Kaidan.h"
#include "VCardManager.h"
VCardModel::VCardModel(QObject *parent)
: QAbstractListModel(parent)
{
connect(
Kaidan::instance()->getClient()->getVCardManager(),
&VCardManager::vCardReceived,
this,
&VCardModel::handleVCardReceived
);
}
QHash<int, QByteArray> VCardModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[Key] = "key";
roles[Value] = "value";
return roles;
}
int VCardModel::rowCount(const QModelIndex &parent) const
{
// For list models only the root node (an invalid parent) should return the
// list's size. For all other (valid) parents, rowCount() should return 0 so
// that it does not become a tree model.
if (parent.isValid())
return 0;
return m_vCard.size();
}
QVariant VCardModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return {};
switch(role) {
case Key:
return m_vCard.at(index.row()).key();
case Value:
return m_vCard.at(index.row()).value();
}
return {};
}
void VCardModel::handleVCardReceived(const QXmppVCardIq &vCard)
{
if (vCard.from() == m_jid) {
beginResetModel();
m_vCard.clear();
if (!vCard.fullName().isEmpty())
m_vCard << Item(tr("Name"), vCard.fullName());
if (!vCard.nickName().isEmpty())
m_vCard << Item(tr("Nickname"), vCard.nickName());
if (!vCard.description().isEmpty())
m_vCard << Item(tr("About"), vCard.description());
if (!vCard.email().isEmpty())
m_vCard << Item(tr("Email"), vCard.email());
if (!vCard.birthday().isNull() && vCard.birthday().isValid())
m_vCard << Item(tr("Birthday"), vCard.birthday().toString());
if (!vCard.url().isEmpty())
m_vCard << Item(tr("Website"), vCard.url());
endResetModel();
}
}
QString VCardModel::jid() const
{
return m_jid;
}
void VCardModel::setJid(const QString &jid)
{
m_jid = jid;
emit jidChanged();
Kaidan::instance()->vCardRequested(jid);
}
VCardModel::Item::Item(const QString &key, const QString &value)
: m_key(key), m_value(value)
{
}
QString VCardModel::Item::key() const
{
return m_key;
}
void VCardModel::Item::setKey(const QString &key)
{
m_key = key;
}
QString VCardModel::Item::value() const
{
return m_value;
}
void VCardModel::Item::setValue(const QString &value)
{
m_value = value;
}
/*
* Kaidan - A user-friendly XMPP client for every device!
*
* Copyright (C) 2016-2019 Kaidan developers and contributors
* (see the LICENSE file for a full list of copyright authors)
*
* Kaidan is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition, as a special exception, the author of Kaidan gives
* permission to link the code of its release with the OpenSSL
* project's "OpenSSL" library (or with modified versions of it that
* use the same license as the "OpenSSL" library), and distribute the
* linked executables. You must obey the GNU General Public License in
* all respects for all of the code used other than "OpenSSL". If you
* modify this file, you may extend this exception to your version of
* the file, but you are not obligated to do so. If you do not wish to
* do so, delete this exception statement from your version.
*
* Kaidan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kaidan. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef VCARDMODEL_H
#define VCARDMODEL_H
#include <QAbstractListModel>
#include <QXmppVCardIq.h>
class VCardModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(QString jid READ jid WRITE setJid NOTIFY jidChanged)
public:
enum Roles {
Key,
Value
};
class Item
{
public:
Item() = default;
Item(const QString &key, const QString &value);
QString key() const;
void setKey(const QString &key);
QString value() const;
void setValue(const QString &value);
private:
QString m_key;
QString m_value;
};
explicit VCardModel(QObject *parent = nullptr);
QHash<int, QByteArray> roleNames() const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QString jid() const;
void setJid(const QString &jid);
signals:
void jidChanged();
private slots:
void handleVCardReceived(const QXmppVCardIq &vCard);
private:
QVector<Item> m_vCard;
QString m_jid;
};
#endif // VCARDMODEL_H
......@@ -60,6 +60,7 @@
#include "EmojiModel.h"
#include "Utils.h"
#include "QrCodeScannerFilter.h"
#include "VCardModel.h"
#ifdef STATIC_BUILD
#include "static_plugins.h"
......@@ -185,6 +186,7 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
qRegisterMetaType<std::function<void(RosterItem&)>>("std::function<void(RosterItem&)>");
qRegisterMetaType<std::function<void(Message&)>>("std::function<void(Message&)>");
qRegisterMetaType<ClientWorker::Credentials>("ClientWorker::Credentials");
qRegisterMetaType<QXmppVCardIq>("QXmppVCardIq");
// Enums for c++ member calls using enums
qRegisterMetaType<Enums::ConnectionState>();
qRegisterMetaType<Enums::DisconnectionReason>();
......@@ -283,6 +285,7 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
qmlRegisterType<EmojiModel>("EmojiModel", 0, 1, "EmojiModel");
qmlRegisterType<EmojiProxyModel>("EmojiModel", 0, 1, "EmojiProxyModel");
qmlRegisterType<QrCodeScannerFilter>(APPLICATION_ID, 1, 0, "QrCodeScannerFilter");
qmlRegisterType<VCardModel>(APPLICATION_ID, 1, 0, "VCardModel");
qmlRegisterUncreatableType<QAbstractItemModel>("EmojiModel", 0, 1, "QAbstractItemModel", "Used by proxy models");
qmlRegisterUncreatableType<Emoji>("EmojiModel", 0, 1, "Emoji", "Used by emoji models");
......
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