Commit 76def6d0 authored by Ronan's avatar Ronan
Browse files

refactoring unstable

parent a314d1fd
......@@ -58,7 +58,8 @@ set(SOURCES
src/components/camera/Camera.cpp
src/components/chat/ChatModel.cpp
src/components/chat/ChatProxyModel.cpp
src/components/contacts/ContactModel.cpp
src/components/contact/ContactModel.cpp
src/components/contact/VcardModel.cpp
src/components/contacts/ContactsListModel.cpp
src/components/contacts/ContactsListProxyModel.cpp
src/components/core/CoreManager.cpp
......@@ -80,7 +81,8 @@ set(HEADERS
src/components/camera/Camera.hpp
src/components/chat/ChatModel.hpp
src/components/chat/ChatProxyModel.hpp
src/components/contacts/ContactModel.hpp
src/components/contact/ContactModel.hpp
src/components/contact/VcardModel.hpp
src/components/contacts/ContactsListModel.hpp
src/components/contacts/ContactsListProxyModel.hpp
src/components/core/CoreManager.hpp
......
......@@ -109,6 +109,10 @@ void App::registerTypes () {
"Linphone", 1, 0, "ContactModel", "ContactModel is uncreatable"
);
qmlRegisterUncreatableType<VcardModel>(
"Linphone", 1, 0, "VcardModel", "VcardModel is uncreatable"
);
ContactsListProxyModel::initContactsListModel(new ContactsListModel());
qmlRegisterType<ContactsListProxyModel>("Linphone", 1, 0, "ContactsListProxyModel");
......
#include "../../app/App.hpp"
#include "ContactModel.hpp"
using namespace std;
// =============================================================================
const char *ContactModel::NAME = "contact-model";
ContactModel::ContactModel (shared_ptr<linphone::Friend> linphone_friend) {
linphone_friend->setData(NAME, *this);
m_linphone_friend = linphone_friend;
m_vcard = make_shared<VcardModel>(linphone_friend->getVcard());
App::getInstance()->getEngine()->setObjectOwnership(m_vcard.get(), QQmlEngine::CppOwnership);
}
Presence::PresenceStatus ContactModel::getPresenceStatus () const {
return Presence::PresenceStatus::Offline;
}
Presence::PresenceLevel ContactModel::getPresenceLevel () const {
return Presence::getPresenceLevel(getPresenceStatus());
}
#ifndef CONTACT_MODEL_H_
#define CONTACT_MODEL_H_
#include <linphone++/linphone.hh>
#include <QObject>
#include "../presence/Presence.hpp"
#include "VcardModel.hpp"
// =============================================================================
class ContactModel : public QObject {
Q_OBJECT;
Q_PROPERTY(Presence::PresenceStatus presenceStatus READ getPresenceStatus NOTIFY contactUpdated);
Q_PROPERTY(Presence::PresenceLevel presenceLevel READ getPresenceLevel NOTIFY contactUpdated);
Q_PROPERTY(VcardModel * vcard READ getVcardModelPtr NOTIFY contactUpdated);
friend class ContactsListModel;
friend class ContactsListProxyModel;
public:
ContactModel (std::shared_ptr<linphone::Friend> linphone_friend);
static const char *NAME;
public slots:
void startEdit () {
m_linphone_friend->edit();
}
void endEdit () {
m_linphone_friend->done();
}
void abortEdit () {
// TODO.
// m_linphone_friend->abort();
}
signals:
void contactUpdated ();
private:
Presence::PresenceStatus getPresenceStatus () const;
Presence::PresenceLevel getPresenceLevel () const;
std::shared_ptr<VcardModel> getVcardModel () const {
return m_vcard;
}
VcardModel *getVcardModelPtr () const {
return m_vcard.get();
}
std::shared_ptr<VcardModel> m_vcard;
std::shared_ptr<linphone::Friend> m_linphone_friend;
};
Q_DECLARE_METATYPE(ContactModel *);
#endif // CONTACT_MODEL_H_
#include <belcard/belcard.hpp>
#include <QFileInfo>
#include <QImageReader>
#include <QUuid>
#include <QtDebug>
#include <belcard/belcard.hpp>
#include <QUuid>
#include "../../app/AvatarProvider.hpp"
#include "../../app/Database.hpp"
#include "../../utils.hpp"
#include "../core/CoreManager.hpp"
#include "ContactModel.hpp"
#include "VcardModel.hpp"
#define VCARD_SCHEME "linphone-desktop:/"
using namespace std;
// ===================================================================
// =============================================================================
template<class T>
inline shared_ptr<T> findBelCardValue (
const list<shared_ptr<T> > &list,
const QString &value
) {
string match = Utils::qStringToLinphoneString(value);
inline shared_ptr<T> findBelCardValue (const list<shared_ptr<T> > &list, const QString &value) {
string match = ::Utils::qStringToLinphoneString(value);
auto it = find_if(
list.cbegin(), list.cend(),
[&match](const shared_ptr<T> &entry) {
return match == entry->getValue();
}
);
list.cbegin(), list.cend(), [&match](const shared_ptr<T> &entry) {
return match == entry->getValue();
}
);
return *it;
}
// -------------------------------------------------------------------
// -----------------------------------------------------------------------------
ContactModel::ContactModel (
shared_ptr<linphone::Friend> linphone_friend,
bool is_detached
) {
linphone_friend->setData("contact-model", *this);
m_linphone_friend = linphone_friend;
m_is_detached = is_detached;
QString VcardModel::getUsername () const {
return ::Utils::linphoneStringToQString(m_vcard->getFullName());
}
// -------------------------------------------------------------------
void ContactModel::edit () {
if (!m_is_detached)
m_linphone_friend->edit();
}
void ContactModel::done () {
if (!m_is_detached)
m_linphone_friend->done();
}
// -------------------------------------------------------------------
QString ContactModel::getUsername () const {
return Utils::linphoneStringToQString(
m_linphone_friend->getName()
);
}
void ContactModel::setUsername (const QString &username) {
void VcardModel::setUsername (const QString &username) {
if (username.length() == 0 || username == getUsername())
return;
edit();
if (!m_linphone_friend->setName(Utils::qStringToLinphoneString(username)))
emit contactUpdated();
done();
m_vcard->setFullName(::Utils::qStringToLinphoneString(username));
emit vcardUpdated();
}
// -------------------------------------------------------------------
// -----------------------------------------------------------------------------
QString ContactModel::getAvatar () const {
QString VcardModel::getAvatar () const {
// Find desktop avatar.
list<shared_ptr<belcard::BelCardPhoto> > photos =
m_linphone_friend->getVcard()->getBelcard()->getPhotos();
list<shared_ptr<belcard::BelCardPhoto> > photos = m_vcard->getBelcard()->getPhotos();
auto it = find_if(
photos.cbegin(), photos.cend(), [](const shared_ptr<belcard::BelCardPhoto> &photo) {
return !photo->getValue().compare(0, sizeof(VCARD_SCHEME) - 1, VCARD_SCHEME);
}
);
photos.cbegin(), photos.cend(), [](const shared_ptr<belcard::BelCardPhoto> &photo) {
return !photo->getValue().compare(0, sizeof(VCARD_SCHEME) - 1, VCARD_SCHEME);
}
);
// No path found.
if (it == photos.cend())
return "";
// Returns right path.
return QStringLiteral("image://%1/%2")
.arg(AvatarProvider::PROVIDER_ID)
.arg(Utils::linphoneStringToQString(
(*it)->getValue().substr(sizeof(VCARD_SCHEME) - 1)
));
return QStringLiteral("image://%1/%2").arg(AvatarProvider::PROVIDER_ID).arg(
::Utils::linphoneStringToQString((*it)->getValue().substr(sizeof(VCARD_SCHEME) - 1))
);
}
void ContactModel::setAvatar (const QString &path) {
void VcardModel::setAvatar (const QString &path) {
// 1. Try to copy photo in avatars folder.
QFile file(path);
......@@ -115,32 +78,31 @@ void ContactModel::setAvatar (const QString &path) {
.arg(uuid.mid(1, uuid.length() - 2)) // Remove `{}`.
.arg(info.suffix());
QString dest = Utils::linphoneStringToQString(Database::getAvatarsPath()) +
file_id;
QString dest = ::Utils::linphoneStringToQString(Database::getAvatarsPath()) + file_id;
if (!file.copy(dest))
return;
qInfo() << QStringLiteral("Update avatar of `%1`. (path=%2)")
.arg(getUsername()).arg(dest);
qInfo() << QStringLiteral("Update avatar of `%1`. (path=%2)").arg(getUsername()).arg(dest);
// 2. Edit vcard.
edit();
shared_ptr<belcard::BelCard> belCard = m_linphone_friend->getVcard()->getBelcard();
shared_ptr<belcard::BelCard> belCard = m_vcard->getBelcard();
list<shared_ptr<belcard::BelCardPhoto> > photos = belCard->getPhotos();
// 3. Remove oldest photo.
auto it = find_if(
photos.begin(), photos.end(), [](const shared_ptr<belcard::BelCardPhoto> &photo) {
return !photo->getValue().compare(0, sizeof(VCARD_SCHEME) - 1, VCARD_SCHEME);
}
);
photos.begin(), photos.end(), [](const shared_ptr<belcard::BelCardPhoto> &photo) {
return !photo->getValue().compare(0, sizeof(VCARD_SCHEME) - 1, VCARD_SCHEME);
}
);
if (it != photos.end()) {
QString image_path(Utils::linphoneStringToQString(
Database::getAvatarsPath() + (*it)->getValue().substr(sizeof(VCARD_SCHEME) - 1)
));
QString image_path(
::Utils::linphoneStringToQString(
Database::getAvatarsPath() + (*it)->getValue().substr(sizeof(VCARD_SCHEME) - 1)
)
);
if (!QFile::remove(image_path))
qWarning() << QStringLiteral("Unable to remove `%1`.").arg(image_path);
belCard->removePhoto(*it);
......@@ -149,59 +111,49 @@ void ContactModel::setAvatar (const QString &path) {
// 4. Update.
shared_ptr<belcard::BelCardPhoto> photo =
belcard::BelCardGeneric::create<belcard::BelCardPhoto>();
photo->setValue(VCARD_SCHEME + Utils::qStringToLinphoneString(file_id));
photo->setValue(VCARD_SCHEME + ::Utils::qStringToLinphoneString(file_id));
belCard->addPhoto(photo);
done();
emit vcardUpdated();
}
emit contactUpdated();
// -----------------------------------------------------------------------------
return;
QVariantMap VcardModel::getAddress () const {
// TODO
}
// -------------------------------------------------------------------
void VcardModel::setAddress (const QVariantMap &address) {
// TODO
}
QVariantList ContactModel::getSipAddresses () const {
QVariantList VcardModel::getSipAddresses () const {
QVariantList list;
for (const auto &address : m_linphone_friend->getAddresses())
list.append(Utils::linphoneStringToQString(address->asString()));
for (const auto &address : m_vcard->getSipAddresses())
list.append(::Utils::linphoneStringToQString(address->asString()));
return list;
}
bool ContactModel::addSipAddress (const QString &sip_address) {
shared_ptr<linphone::Address> address =
CoreManager::getInstance()->getCore()->createAddress(
Utils::qStringToLinphoneString(sip_address)
);
if (!address) {
qWarning() << QStringLiteral("Unable to add invalid sip address: `%1`.").arg(sip_address);
return false;
}
bool VcardModel::addSipAddress (const QString &sip_address) {
qInfo() << QStringLiteral("Add new sip address: `%1`.").arg(sip_address);
m_vcard->addSipAddress(::Utils::qStringToLinphoneString(sip_address));
edit();
m_linphone_friend->addAddress(address);
done();
emit contactUpdated();
emit vcardUpdated();
return true;
}
void ContactModel::removeSipAddress (const QString &sip_address) {
list<shared_ptr<linphone::Address> > addresses = m_linphone_friend->getAddresses();
string match = Utils::qStringToLinphoneString(sip_address);
void VcardModel::removeSipAddress (const QString &sip_address) {
list<shared_ptr<linphone::Address> > addresses = m_vcard->getSipAddresses();
string match = ::Utils::qStringToLinphoneString(sip_address);
auto it = find_if(
addresses.cbegin(), addresses.cend(),
[&match](const shared_ptr<linphone::Address> &address) {
return match == address->asString();
}
);
addresses.cbegin(), addresses.cend(), [&match](const shared_ptr<linphone::Address> &address) {
return match == address->asString();
}
);
if (it == addresses.cend()) {
qWarning() << QStringLiteral("Unable to found sip address: `%1`.")
......@@ -216,17 +168,12 @@ void ContactModel::removeSipAddress (const QString &sip_address) {
}
qInfo() << QStringLiteral("Remove sip address: `%1`.").arg(sip_address);
m_vcard->removeSipAddress((*it)->asStringUriOnly());
edit();
m_linphone_friend->removeAddress(*it);
done();
emit contactUpdated();
return;
emit vcardUpdated();
}
bool ContactModel::updateSipAddress (const QString &old_sip_address, const QString &sip_address) {
bool VcardModel::updateSipAddress (const QString &old_sip_address, const QString &sip_address) {
if (old_sip_address == sip_address || !addSipAddress(sip_address))
return false;
......@@ -235,34 +182,32 @@ bool ContactModel::updateSipAddress (const QString &old_sip_address, const QStri
return true;
}
// -------------------------------------------------------------------
// -----------------------------------------------------------------------------
QVariantList ContactModel::getCompanies () const {
QVariantList VcardModel::getCompanies () const {
QVariantList list;
for (const auto &company : m_linphone_friend->getVcard()->getBelcard()->getRoles())
list.append(Utils::linphoneStringToQString(company->getValue()));
for (const auto &company : m_vcard->getBelcard()->getRoles())
list.append(::Utils::linphoneStringToQString(company->getValue()));
return list;
}
void ContactModel::addCompany (const QString &company) {
shared_ptr<belcard::BelCard> belCard = m_linphone_friend->getVcard()->getBelcard();
shared_ptr<belcard::BelCardRole> value =
belcard::BelCardGeneric::create<belcard::BelCardRole>();
value->setValue(Utils::qStringToLinphoneString(company));
bool VcardModel::addCompany (const QString &company) {
shared_ptr<belcard::BelCard> belCard = m_vcard->getBelcard();
shared_ptr<belcard::BelCardRole> value = belcard::BelCardGeneric::create<belcard::BelCardRole>();
value->setValue(::Utils::qStringToLinphoneString(company));
qInfo() << QStringLiteral("Add new company: `%1`.").arg(company);
edit();
belCard->addRole(value);
done();
emit contactUpdated();
emit vcardUpdated();
return true;
}
void ContactModel::removeCompany (const QString &company) {
shared_ptr<belcard::BelCard> belCard = m_linphone_friend->getVcard()->getBelcard();
void VcardModel::removeCompany (const QString &company) {
shared_ptr<belcard::BelCard> belCard = m_vcard->getBelcard();
shared_ptr<belcard::BelCardRole> value = findBelCardValue(belCard->getRoles(), company);
if (!value) {
......@@ -271,53 +216,47 @@ void ContactModel::removeCompany (const QString &company) {
}
qInfo() << QStringLiteral("Remove company: `%1`.").arg(company);
edit();
belCard->removeRole(value);
done();
emit contactUpdated();
emit vcardUpdated();
}
void ContactModel::updateCompany (const QString &old_company, const QString &company) {
if (old_company == company)
return;
bool VcardModel::updateCompany (const QString &old_company, const QString &company) {
if (old_company == company || !addCompany(company))
return false;
addCompany(company);
removeCompany(old_company);
return true;
}
// -------------------------------------------------------------------
// -----------------------------------------------------------------------------
QVariantList ContactModel::getEmails () const {
QVariantList VcardModel::getEmails () const {
QVariantList list;
for (const auto &email : m_linphone_friend->getVcard()->getBelcard()->getEmails())
list.append(Utils::linphoneStringToQString(email->getValue()));
for (const auto &email : m_vcard->getBelcard()->getEmails())
list.append(::Utils::linphoneStringToQString(email->getValue()));
return list;
}
bool ContactModel::addEmail (const QString &email) {
shared_ptr<belcard::BelCard> belCard = m_linphone_friend->getVcard()->getBelcard();
bool VcardModel::addEmail (const QString &email) {
shared_ptr<belcard::BelCard> belCard = m_vcard->getBelcard();
shared_ptr<belcard::BelCardEmail> value =
belcard::BelCardGeneric::create<belcard::BelCardEmail>();
value->setValue(Utils::qStringToLinphoneString(email));
value->setValue(::Utils::qStringToLinphoneString(email));
qInfo() << QStringLiteral("Add new email: `%1`.").arg(email);
edit();
belCard->addEmail(value);
done();
emit contactUpdated();
emit vcardUpdated();
// TODO: Check if email is valid.
return true;
}
void ContactModel::removeEmail (const QString &email) {
shared_ptr<belcard::BelCard> belCard = m_linphone_friend->getVcard()->getBelcard();
void VcardModel::removeEmail (const QString &email) {
shared_ptr<belcard::BelCard> belCard = m_vcard->getBelcard();
shared_ptr<belcard::BelCardEmail> value = findBelCardValue(belCard->getEmails(), email);
if (!value) {
......@@ -326,15 +265,12 @@ void ContactModel::removeEmail (const QString &email) {
}
qInfo() << QStringLiteral("Remove email: `%1`.").arg(email);
edit();
belCard->removeEmail(value);
done();
emit contactUpdated();
emit vcardUpdated();
}
bool ContactModel::updateEmail (const QString &old_email, const QString &email) {
bool VcardModel::updateEmail (const QString &old_email, const QString &email) {
if (old_email == email || !addEmail(email))
return false;
......@@ -343,39 +279,32 @@ bool ContactModel::updateEmail (const QString &old_email, const QString &email)
return true;
}
// -------------------------------------------------------------------
// -----------------------------------------------------------------------------
QVariantList ContactModel::getUrls () const {
QVariantList VcardModel::getUrls () const {
QVariantList list;
qDebug() << "card" << m_linphone_friend->getVcard().get();
for (const auto &url : m_linphone_friend->getVcard()->getBelcard()->getURLs())
list.append(Utils::linphoneStringToQString(url->getValue()));
for (const auto &url : m_vcard->getBelcard()->getURLs())
list.append(::Utils::linphoneStringToQString(url->getValue()));
return list;
}
bool ContactModel::addUrl (const QString &url) {
shared_ptr<belcard::BelCard> belCard = m_linphone_friend->getVcard()->getBelcard();
shared_ptr<belcard::BelCardURL> value =
belcard::BelCardGeneric::create<belcard::BelCardURL>();
value->setValue(Utils::qStringToLinphoneString(url));
bool VcardModel::addUrl (const QString &url) {
shared_ptr<belcard::BelCard> belCard = m_vcard->getBelcard();
shared_ptr<belcard::BelCardURL> value = belcard::BelCardGeneric::create<belcard::BelCardURL>();
value->setValue(::Utils::qStringToLinphoneString(url));
qInfo() << QStringLiteral("Add new url: `%1`.").arg(url);
edit();
belCard->addURL(value);
done();
emit contactUpdated();
emit vcardUpdated();
// TODO: Check if url is valid.
return true;
}
void ContactModel::removeUrl (const QString &url) {
shared_ptr<belcard::BelCard> belCard = m_linphone_friend->getVcard()->getBelcard();
void VcardModel::removeUrl (const QString &url) {
shared_ptr<belcard::BelCard> belCard = m_vcard->getBelcard();
shared_ptr<belcard::BelCardURL> value = findBelCardValue(belCard->getURLs(), url);
if (!value) {
......@@ -384,15 +313,12 @@ void ContactModel::removeUrl (const QString &url) {
}
qInfo() << QStringLiteral("Remove url: `%1`.").arg(url);
edit();
belCard->removeURL(value);