Commit 877da996 authored by Ronan Abhamon's avatar Ronan Abhamon

feat(app): new Presence type

parent 9b98924c
......@@ -23,6 +23,7 @@ HEADERS = \
src/components/contacts/ContactsListModel.hpp \
src/components/contacts/ContactsListProxyModel.hpp \
src/components/notification/Notification.hpp \
src/components/presence/PresenceModel.hpp \
src/components/settings/AccountSettingsListModel.hpp \
src/components/settings/AccountSettingsModel.hpp \
src/components/settings/SettingsModel.hpp \
......
#include "ContactModel.hpp"
// ===================================================================
ContactModel::PresenceLevel ContactModel:: getPresenceLevel () const {
if (m_presence == Online)
return Green;
if (m_presence == DoNotDisturb)
return Red;
if (m_presence == Offline)
return White;
return Orange;
}
......@@ -3,6 +3,8 @@
#include <QObject>
#include "../presence/PresenceModel.hpp"
// ===================================================================
class ContactModel : public QObject {
......@@ -25,13 +27,13 @@ class ContactModel : public QObject {
);
Q_PROPERTY(
Presence presence
PresenceModel::Presence presence
READ getPresence
CONSTANT
);
Q_PROPERTY(
PresenceLevel presenceLevel
PresenceModel::PresenceLevel presenceLevel
READ getPresenceLevel
CONSTANT
);
......@@ -44,32 +46,11 @@ class ContactModel : public QObject {
);
public:
enum Presence {
Online,
BeRightBack,
Away,
OnThePhone,
OutToLunch,
DoNotDisturb,
Moved,
UsingAnotherMessagingService,
Offline
};
Q_ENUM(Presence);
enum PresenceLevel {
Green,
Orange,
Red,
White
};
Q_ENUM(PresenceLevel);
ContactModel (QObject *parent = Q_NULLPTR) : QObject(parent) { }
ContactModel (
const QString &username,
const QString &avatar,
const Presence &presence,
const PresenceModel::Presence &presence,
const QStringList &sip_addresses
): ContactModel() {
m_username = username;
......@@ -98,11 +79,13 @@ private:
m_avatar = avatar;
}
Presence getPresence () const {
PresenceModel::Presence getPresence () const {
return m_presence;
}
PresenceLevel getPresenceLevel () const;
PresenceModel::PresenceLevel getPresenceLevel () const {
return PresenceModel::getPresenceLevel(m_presence);
}
QStringList getSipAddresses () const {
return m_sip_addresses;
......@@ -114,7 +97,7 @@ private:
QString m_username;
QString m_avatar;
Presence m_presence = Online;
PresenceModel::Presence m_presence = PresenceModel::Online;
QStringList m_sip_addresses;
};
......
......@@ -3,18 +3,19 @@
// ===================================================================
ContactsListModel::ContactsListModel (QObject *parent): QAbstractListModel(parent) {
m_list << new ContactModel("Toto Roi", "", ContactModel::Online, QStringList("toto.linphone.sip.linphone.org"));
m_list << new ContactModel("Mary Boreno", "", ContactModel::Online, QStringList("toto.linphone.sip.linphone.org"));
m_list << new ContactModel("Cecelia Cyler", "", ContactModel::Online, QStringList("toto.linphone.sip.linphone.org"));
m_list << new ContactModel("Daniel Elliott", "", ContactModel::Online, QStringList("toto.linphone.sip.linphone.org"));
m_list << new ContactModel("Effie Forton", "", ContactModel::Online, QStringList("toto.linphone.sip.linphone.org"));
m_list << new ContactModel("Agnes Hurner", "", ContactModel::Online, QStringList("toto.linphone.sip.linphone.org"));
m_list << new ContactModel("Luke Lemin", "", ContactModel::Online, QStringList("toto.linphone.sip.linphone.org"));
m_list << new ContactModel("Claire Manning", "", ContactModel::Online, QStringList("toto.linphone.sip.linphone.org"));
m_list << new ContactModel("Isabella Ahornton", "", ContactModel::Online, QStringList("toto.linphone.sip.linphone.org"));
m_list << new ContactModel("Mary Boreno", "", ContactModel::Online, QStringList("toto.linphone.sip.linphone.org"));
m_list << new ContactModel("Aman Than", "", ContactModel::Online, QStringList("toto.linphone.sip.linphone.org"));
m_list << new ContactModel(" abdoul", "", ContactModel::Online, QStringList("toto.linphone.sip.linphone.org"));
// TMP.
m_list << new ContactModel("Toto Roi", "", PresenceModel::Online, QStringList("toto.linphone.sip.linphone.org"));
m_list << new ContactModel("Mary Boreno", "", PresenceModel::Online, QStringList("toto.linphone.sip.linphone.org"));
m_list << new ContactModel("Cecelia Cyler", "", PresenceModel::Online, QStringList("toto.linphone.sip.linphone.org"));
m_list << new ContactModel("Daniel Elliott", "", PresenceModel::Online, QStringList("toto.linphone.sip.linphone.org"));
m_list << new ContactModel("Effie Forton", "", PresenceModel::Online, QStringList("toto.linphone.sip.linphone.org"));
m_list << new ContactModel("Agnes Hurner", "", PresenceModel::Online, QStringList("toto.linphone.sip.linphone.org"));
m_list << new ContactModel("Luke Lemin", "", PresenceModel::Online, QStringList("toto.linphone.sip.linphone.org"));
m_list << new ContactModel("Claire Manning", "", PresenceModel::Online, QStringList("toto.linphone.sip.linphone.org"));
m_list << new ContactModel("Isabella Ahornton", "", PresenceModel::Online, QStringList("toto.linphone.sip.linphone.org"));
m_list << new ContactModel("Mary Boreno", "", PresenceModel::Online, QStringList("toto.linphone.sip.linphone.org"));
m_list << new ContactModel("Aman Than", "", PresenceModel::Online, QStringList("toto.linphone.sip.linphone.org"));
m_list << new ContactModel(" abdoul", "", PresenceModel::Online, QStringList("toto.linphone.sip.linphone.org"));
}
......@@ -24,7 +25,7 @@ int ContactsListModel::rowCount (const QModelIndex &) const {
QHash<int, QByteArray> ContactsListModel::roleNames () const {
QHash<int, QByteArray> roles;
roles[ContactRole] = "$contact";
roles[Qt::DisplayRole] = "$contact";
return roles;
}
......@@ -34,7 +35,7 @@ QVariant ContactsListModel::data (const QModelIndex &index, int role) const {
if (row < 0 || row >= m_list.count())
return QVariant();
if (role == ContactRole)
if (role == Qt::DisplayRole)
return QVariant::fromValue(m_list[row]);
return QVariant();
......
......@@ -13,10 +13,6 @@ class ContactsListModel : public QAbstractListModel {
Q_OBJECT;
public:
enum Roles {
ContactRole = Qt::UserRole + 1
};
ContactsListModel (QObject *parent = Q_NULLPTR);
int rowCount (const QModelIndex &) const;
......
......@@ -6,6 +6,11 @@
#define MAIN_SIP_ADDRESS_WEIGHT 30.0
#define OTHER_SIP_ADDRESSES_WEIGHT 20.0
#define FACTOR_POS_1 0.90
#define FACTOR_POS_2 0.80
#define FACTOR_POS_3 0.70
#define FACTOR_POS_OTHER 0.60
// ===================================================================
ContactsListModel *ContactsListProxyModel::m_list = nullptr;
......@@ -18,8 +23,8 @@ ContactsListModel *ContactsListProxyModel::m_list = nullptr;
// - [^_.-;@ ] is used to search patterns which starts with
// a separator like ` word`.
//
// - [_.-;@ ] is the main pattern.
const QRegExp ContactsListProxyModel::search_separators("^[^_.-;@ ][_.-;@ ]");
// - [_.-;@ ] is the main pattern (a separator).
const QRegExp ContactsListProxyModel::m_search_separators("^[^_.-;@ ][_.-;@ ]");
// -------------------------------------------------------------------
......@@ -43,19 +48,21 @@ void ContactsListProxyModel::initContactsListModel (ContactsListModel *list) {
bool ContactsListProxyModel::filterAcceptsRow (int source_row, const QModelIndex &source_parent) const {
QModelIndex index = sourceModel()->index(source_row, 0, source_parent);
const ContactModel *contact = qvariant_cast<ContactModel *>(
index.data(ContactsListModel::ContactRole)
index.data()
);
int weight = m_weights[contact] = static_cast<int>(computeContactWeight(*contact));
int weight = m_weights[contact] = static_cast<int>(
computeContactWeight(*contact)
);
return weight > 0;
}
bool ContactsListProxyModel::lessThan (const QModelIndex &left, const QModelIndex &right) const {
const ContactModel *contact_a = qvariant_cast<ContactModel *>(
sourceModel()->data(left, ContactsListModel::ContactRole)
sourceModel()->data(left)
);
const ContactModel *contact_b = qvariant_cast<ContactModel *>(
sourceModel()->data(right, ContactsListModel::ContactRole)
sourceModel()->data(right)
);
float weight_a = m_weights[contact_a];
......@@ -63,46 +70,49 @@ bool ContactsListProxyModel::lessThan (const QModelIndex &left, const QModelInde
// Sort by weight and name.
return (
weight_a > weight_b ||
(weight_a == weight_b && contact_a->m_username <= contact_b->m_username)
weight_a > weight_b || (
weight_a == weight_b &&
contact_a->m_username <= contact_b->m_username
)
);
}
// -------------------------------------------------------------------
float ContactsListProxyModel::computeStringWeight (const QString &string, float percentage) const {
const static int max = std::numeric_limits<int>::max();
int index = -1;
int offset = -1;
// Search pattern.
while ((index = filterRegExp().indexIn(string, index + 1)) != -1) {
// Search n chars between one separator and index.
int tmp_offset = index - string.lastIndexOf(search_separators, index) - 1;
int tmp_offset = index - string.lastIndexOf(m_search_separators, index) - 1;
qDebug() << string << string.lastIndexOf(search_separators, index) << tmp_offset;
if ((tmp_offset != -1 && tmp_offset < offset) || offset == -1)
offset = tmp_offset;
if ((offset = tmp_offset) == 0) // Little optimization.
break;
}
// No weight.
if (offset == -1)
return 0;
// Weight & offset.
switch (offset) {
case 0: return percentage;
case 1: return percentage * 0.90;
case 2: return percentage * 0.80;
case 3: return percentage * 0.70;
case 1: return percentage * FACTOR_POS_1;
case 2: return percentage * FACTOR_POS_2;
case 3: return percentage * FACTOR_POS_3;
default: break;
}
return percentage * 0.60;
return percentage * FACTOR_POS_OTHER;
}
float ContactsListProxyModel::computeContactWeight (const ContactModel &contact) const {
float weight = computeStringWeight(contact.m_username, USERNAME_WEIGHT);
// It exists at least one sip address.
const QStringList &addresses = contact.m_sip_addresses;
weight += computeStringWeight(addresses[0], MAIN_SIP_ADDRESS_WEIGHT);
......@@ -112,7 +122,5 @@ float ContactsListProxyModel::computeContactWeight (const ContactModel &contact)
for (auto it = ++addresses.constBegin(); it != addresses.constEnd(); ++it)
weight += computeStringWeight(*it, OTHER_SIP_ADDRESSES_WEIGHT / size);
qDebug() << contact.m_username << weight;
return weight;
}
......@@ -22,7 +22,7 @@ private:
float computeStringWeight (const QString &string, float percentage) const;
float computeContactWeight (const ContactModel &contact) const;
static const QRegExp search_separators;
static const QRegExp m_search_separators;
// The contacts list is shared between `ContactsListProxyModel`
// it's necessary to initialize it with `initContactsListModel`.
......
#ifndef PRESENCE_MODEL_H_
#define PRESENCE_MODEL_H_
#include <QObject>
// ===================================================================
class PresenceModel : public QObject {
Q_OBJECT;
public:
enum Presence {
Online,
BeRightBack,
Away,
OnThePhone,
OutToLunch,
DoNotDisturb,
Moved,
UsingAnotherMessagingService,
Offline
};
Q_ENUM(Presence);
enum PresenceLevel {
Green,
Orange,
Red,
White
};
Q_ENUM(PresenceLevel);
PresenceModel (QObject *parent = Q_NULLPTR) { }
static PresenceLevel getPresenceLevel (const Presence &presence) {
if (presence == Online)
return Green;
if (presence == DoNotDisturb)
return Red;
if (presence == Offline)
return White;
return Orange;
}
};
#endif // PRESENCE_MODEL_H_
......@@ -50,8 +50,8 @@ void setTrayIcon (QQmlApplicationEngine &engine) {
}
void registerTypes () {
qmlRegisterUncreatableType<ContactModel>(
"Linphone", 1, 0, "ContactModel", "ContactModel is uncreatable"
qmlRegisterUncreatableType<PresenceModel>(
"Linphone", 1, 0, "Presence", "Presence is uncreatable"
);
ContactsListProxyModel::initContactsListModel(new ContactsListModel());
......
......@@ -8,16 +8,16 @@ Icon {
property int level: -1
function _getColorString () {
if (level === ContactModel.Green) {
if (level === Presence.Green) {
return 'green'
}
if (level === ContactModel.Orange) {
if (level === Presence.Orange) {
return 'orange'
}
if (level === ContactModel.Red) {
if (level === Presence.Red) {
return 'red'
}
if (level === ContactModel.White) {
if (level === Presence.White) {
return 'white'
}
}
......
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