Commit 4f0d4737 authored by Ronan's avatar Ronan
Browse files

feat(views/App/MainWindow/ContactEdit): supports companies & urls

parent 42faa0a0
......@@ -18,6 +18,25 @@ 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);
auto it = find_if(
list.cbegin(), list.cend(),
[&match](const shared_ptr<T> &entry) {
return match == entry->getValue();
}
);
return *it;
}
// -------------------------------------------------------------------
ContactModel::ContactModel (shared_ptr<linphone::Friend> linphone_friend) {
linphone_friend->setData("contact-model", *this);
m_linphone_friend = linphone_friend;
......@@ -201,12 +220,54 @@ void ContactModel::updateSipAddress (const QString &old_sip_address, const QStri
QVariantList ContactModel::getCompanies () const {
QVariantList list;
for (const auto &company : m_linphone_friend->getVcard()->getBelcard()->getOrganizations())
for (const auto &company : m_linphone_friend->getVcard()->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));
qInfo() << QStringLiteral("Add new company: `%1`.").arg(company);
m_linphone_friend->edit();
belCard->addRole(value);
m_linphone_friend->done();
emit contactUpdated();
}
bool ContactModel::removeCompany (const QString &company) {
shared_ptr<belcard::BelCard> belCard = m_linphone_friend->getVcard()->getBelcard();
shared_ptr<belcard::BelCardRole> value = findBelCardValue(belCard->getRoles(), company);
if (!value) {
qWarning() << QStringLiteral("Unable to remove company: `%1`.").arg(company);
return false;
}
qInfo() << QStringLiteral("Remove company: `%1`.").arg(company);
m_linphone_friend->edit();
belCard->removeRole(value);
m_linphone_friend->done();
emit contactUpdated();
return true;
}
void ContactModel::updateCompany (const QString &old_company, const QString &company) {
if (old_company == company || !removeCompany(old_company))
return;
addCompany(company);
}
// -------------------------------------------------------------------
QVariantList ContactModel::getEmails () const {
......@@ -220,14 +281,14 @@ QVariantList ContactModel::getEmails () const {
void ContactModel::addEmail (const QString &email) {
shared_ptr<belcard::BelCard> belCard = m_linphone_friend->getVcard()->getBelcard();
shared_ptr<belcard::BelCardEmail> belCardEmail =
shared_ptr<belcard::BelCardEmail> value =
belcard::BelCardGeneric::create<belcard::BelCardEmail>();
belCardEmail->setValue(Utils::qStringToLinphoneString(email));
value->setValue(Utils::qStringToLinphoneString(email));
qInfo() << QStringLiteral("Add new email: `%1`.").arg(email);
m_linphone_friend->edit();
belCard->addEmail(belCardEmail);
belCard->addEmail(value);
m_linphone_friend->done();
emit contactUpdated();
......@@ -235,26 +296,17 @@ void ContactModel::addEmail (const QString &email) {
bool ContactModel::removeEmail (const QString &email) {
shared_ptr<belcard::BelCard> belCard = m_linphone_friend->getVcard()->getBelcard();
list<shared_ptr<belcard::BelCardEmail> > emails = belCard->getEmails();
string match = Utils::qStringToLinphoneString(email);
shared_ptr<belcard::BelCardEmail> value = findBelCardValue(belCard->getEmails(), email);
auto it = find_if(
emails.cbegin(), emails.cend(),
[&match](const shared_ptr<belcard::BelCardEmail> &email) {
return match == email->getValue();
}
);
if (it == emails.cend()) {
qWarning() << QStringLiteral("Unable to remove email: `%1`.")
.arg(email);
if (!value) {
qWarning() << QStringLiteral("Unable to remove email: `%1`.").arg(email);
return false;
}
qInfo() << QStringLiteral("Remove email: `%1`.").arg(email);
m_linphone_friend->edit();
belCard->removeEmail(*it);
belCard->removeEmail(value);
m_linphone_friend->done();
emit contactUpdated();
......@@ -280,6 +332,48 @@ QVariantList ContactModel::getUrls () const {
return list;
}
void 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));
qInfo() << QStringLiteral("Add new url: `%1`.").arg(url);
m_linphone_friend->edit();
belCard->addURL(value);
m_linphone_friend->done();
emit contactUpdated();
}
bool ContactModel::removeUrl (const QString &url) {
shared_ptr<belcard::BelCard> belCard = m_linphone_friend->getVcard()->getBelcard();
shared_ptr<belcard::BelCardURL> value = findBelCardValue(belCard->getURLs(), url);
if (!value) {
qWarning() << QStringLiteral("Unable to remove url: `%1`.").arg(url);
return false;
}
qInfo() << QStringLiteral("Remove url: `%1`.").arg(url);
m_linphone_friend->edit();
belCard->removeURL(value);
m_linphone_friend->done();
emit contactUpdated();
return true;
}
void ContactModel::updateUrl (const QString &old_url, const QString &url) {
if (old_url == url || !removeUrl(old_url))
return;
addUrl(url);
}
// -------------------------------------------------------------------
QList<QVariantMap> ContactModel::getAddresses () const {
......
......@@ -14,43 +14,12 @@ class ContactModel : public QObject {
Q_OBJECT;
Q_PROPERTY(
QString username
READ getUsername
WRITE setUsername
NOTIFY contactUpdated
);
Q_PROPERTY(
QString avatar
READ getAvatar
WRITE setAvatar
NOTIFY contactUpdated
);
Q_PROPERTY(
QVariantList sipAddresses
READ getSipAddresses
NOTIFY contactUpdated
);
Q_PROPERTY(
QVariantList companies
READ getCompanies
NOTIFY contactUpdated
);
Q_PROPERTY(
QVariantList emails
READ getEmails
NOTIFY contactUpdated
);
Q_PROPERTY(
QVariantList urls
READ getUrls
NOTIFY contactUpdated
);
Q_PROPERTY(QString username READ getUsername WRITE setUsername NOTIFY contactUpdated);
Q_PROPERTY(QString avatar READ getAvatar WRITE setAvatar NOTIFY contactUpdated);
Q_PROPERTY(QVariantList sipAddresses READ getSipAddresses NOTIFY contactUpdated);
Q_PROPERTY(QVariantList companies READ getCompanies NOTIFY contactUpdated);
Q_PROPERTY(QVariantList emails READ getEmails NOTIFY contactUpdated);
Q_PROPERTY(QVariantList urls READ getUrls NOTIFY contactUpdated);
Q_PROPERTY(
QList<QVariantMap> addresses
......@@ -84,10 +53,18 @@ public slots:
bool removeSipAddress (const QString &sip_address);
void updateSipAddress (const QString &old_sip_address, const QString &sip_address);
void addCompany (const QString &company);
bool removeCompany (const QString &company);
void updateCompany (const QString &old_company, const QString &company);
void addEmail (const QString &email);
bool removeEmail (const QString &email);
void updateEmail (const QString &old_email, const QString &email);
void addUrl (const QString &url);
bool removeUrl (const QString &url);
void updateUrl (const QString &old_url, const QString &url);
signals:
void contactUpdated ();
......@@ -99,14 +76,9 @@ private:
void setAvatar (const QString &path);
QVariantList getSipAddresses () const;
QVariantList getCompanies () const;
void setCompanies (const QVariantList &companies);
QVariantList getEmails () const;
QVariantList getUrls () const;
void setUrls (const QVariantList &urls);
QList<QVariantMap> getAddresses () const;
void setAddresses (const QList<QVariantMap> &addresses);
......
......@@ -203,6 +203,11 @@ ColumnLayout {
defaultData: _contact.companies
placeholder: qsTr('companiesInput')
title: qsTr('companies')
onChanged: default_value.length === 0
? _contact.addCompany(new_value)
: _contact.updateCompany(default_value, new_value)
onRemoved: _contact.removeCompany(value)
}
ListForm {
......@@ -224,6 +229,11 @@ ColumnLayout {
defaultData: _contact.urls
placeholder: qsTr('webSitesInput')
title: qsTr('webSites')
onChanged: default_value.length === 0
? _contact.addUrl(new_value)
: _contact.updateUrl(default_value, new_value)
onRemoved: _contact.removeUrl(value)
}
}
}
......
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