diff --git a/tests/linphone.pro b/tests/linphone.pro index 32e6b7104..13a383222 100644 --- a/tests/linphone.pro +++ b/tests/linphone.pro @@ -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 \ diff --git a/tests/src/components/contacts/ContactModel.cpp b/tests/src/components/contacts/ContactModel.cpp index 3fb88708e..46de9a4f5 100644 --- a/tests/src/components/contacts/ContactModel.cpp +++ b/tests/src/components/contacts/ContactModel.cpp @@ -1,14 +1,3 @@ #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; -} diff --git a/tests/src/components/contacts/ContactModel.hpp b/tests/src/components/contacts/ContactModel.hpp index 2f59d765a..bb2ecf42d 100644 --- a/tests/src/components/contacts/ContactModel.hpp +++ b/tests/src/components/contacts/ContactModel.hpp @@ -3,6 +3,8 @@ #include +#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; }; diff --git a/tests/src/components/contacts/ContactsListModel.cpp b/tests/src/components/contacts/ContactsListModel.cpp index 2e5b9b865..e454393ef 100644 --- a/tests/src/components/contacts/ContactsListModel.cpp +++ b/tests/src/components/contacts/ContactsListModel.cpp @@ -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 ContactsListModel::roleNames () const { QHash 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(); diff --git a/tests/src/components/contacts/ContactsListModel.hpp b/tests/src/components/contacts/ContactsListModel.hpp index d7e3a7976..e14e85bc0 100644 --- a/tests/src/components/contacts/ContactsListModel.hpp +++ b/tests/src/components/contacts/ContactsListModel.hpp @@ -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; diff --git a/tests/src/components/contacts/ContactsListProxyModel.cpp b/tests/src/components/contacts/ContactsListProxyModel.cpp index 132ff79cf..b80935156 100644 --- a/tests/src/components/contacts/ContactsListProxyModel.cpp +++ b/tests/src/components/contacts/ContactsListProxyModel.cpp @@ -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( - index.data(ContactsListModel::ContactRole) + index.data() ); - int weight = m_weights[contact] = static_cast(computeContactWeight(*contact)); + int weight = m_weights[contact] = static_cast( + computeContactWeight(*contact) + ); return weight > 0; } bool ContactsListProxyModel::lessThan (const QModelIndex &left, const QModelIndex &right) const { const ContactModel *contact_a = qvariant_cast( - sourceModel()->data(left, ContactsListModel::ContactRole) + sourceModel()->data(left) ); const ContactModel *contact_b = qvariant_cast( - 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::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; } diff --git a/tests/src/components/contacts/ContactsListProxyModel.hpp b/tests/src/components/contacts/ContactsListProxyModel.hpp index 4b566e704..1c1844081 100644 --- a/tests/src/components/contacts/ContactsListProxyModel.hpp +++ b/tests/src/components/contacts/ContactsListProxyModel.hpp @@ -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`. diff --git a/tests/src/components/presence/PresenceModel.hpp b/tests/src/components/presence/PresenceModel.hpp new file mode 100644 index 000000000..58a632e9c --- /dev/null +++ b/tests/src/components/presence/PresenceModel.hpp @@ -0,0 +1,47 @@ +#ifndef PRESENCE_MODEL_H_ +#define PRESENCE_MODEL_H_ + +#include + +// =================================================================== + +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_ diff --git a/tests/src/main.cpp b/tests/src/main.cpp index 1b469a30a..62ecce756 100644 --- a/tests/src/main.cpp +++ b/tests/src/main.cpp @@ -50,8 +50,8 @@ void setTrayIcon (QQmlApplicationEngine &engine) { } void registerTypes () { - qmlRegisterUncreatableType( - "Linphone", 1, 0, "ContactModel", "ContactModel is uncreatable" + qmlRegisterUncreatableType( + "Linphone", 1, 0, "Presence", "Presence is uncreatable" ); ContactsListProxyModel::initContactsListModel(new ContactsListModel()); diff --git a/tests/ui/modules/Linphone/Contact/PresenceLevel.qml b/tests/ui/modules/Linphone/Contact/PresenceLevel.qml index e7dcc581d..3441f3f68 100644 --- a/tests/ui/modules/Linphone/Contact/PresenceLevel.qml +++ b/tests/ui/modules/Linphone/Contact/PresenceLevel.qml @@ -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' } }