Commit d7c93488 authored by Aniket Kumar's avatar Aniket Kumar 🤵 Committed by Simon Redman
Browse files

added new arbitrary contact feature

parent d26cfd42
......@@ -151,7 +151,8 @@ QStandardItem * ConversationListModel::conversationForThreadId(qint32 threadId)
return nullptr;
}
QStandardItem * ConversationListModel::getConversationForAddress(const QString& address) {
QStandardItem * ConversationListModel::getConversationForAddress(const QString& address)
{
for(int i = 0; i < rowCount(); ++i) {
const auto& it = item(i, 0);
if (!it->data(MultitargetRole).toBool()) {
......@@ -235,7 +236,8 @@ void ConversationListModel::createRowFromMessage(const ConversationMessage& mess
appendRow(item);
}
void ConversationListModel::displayContacts() {
void ConversationListModel::displayContacts()
{
const QList<QSharedPointer<KPeople::PersonData>> personDataList = SmsHelper::getAllPersons();
for(const auto& person : personDataList) {
......@@ -264,27 +266,17 @@ void ConversationListModel::displayContacts() {
}
}
bool ConversationListModel::isPhoneNumberValid(const QString& number) {
return SmsHelper::isPhoneNumberValid(number);
}
QString ConversationListModel::getDisplayNameForAddress(const QString &address) {
const auto item = getConversationForAddress(address);
if (!item) {
return QString();
}
return item->data(Qt::DisplayRole).toString();
}
void ConversationListModel::createConversationForAddress(const QString& address) {
void ConversationListModel::createConversationForAddress(const QString& address)
{
QStandardItem* item = new QStandardItem();
item->setText(address);
const QString canonicalizedAddress = SmsHelper::canonicalizePhoneNumber(address);
item->setText(canonicalizedAddress);
QList<ConversationAddress> addresses;
addresses.append(ConversationAddress(address));
addresses.append(ConversationAddress(canonicalizedAddress));
item->setData(QVariant::fromValue(addresses), AddressesRole);
QString displayBody = i18n("%1", address);
QString displayBody = i18n("%1", canonicalizedAddress);
item->setData(displayBody, Qt::ToolTipRole);
item->setData(false, MultitargetRole);
item->setData(qint64(INVALID_THREAD_ID), ConversationIdRole);
......@@ -292,3 +284,8 @@ void ConversationListModel::createConversationForAddress(const QString& address)
item->setData(address, SenderRole);
appendRow(item);
}
bool ConversationListModel::isPhoneNumberValid(const QString& number)
{
return SmsHelper::isPhoneNumberValid(number);
}
......@@ -57,15 +57,6 @@ public:
Q_SCRIPTABLE void refresh();
/**
* This method gets name of conversations or contact if it find any matching address
* Needed for checking if the converstion already or contact already exist or no before adding an arbbitrary contact
*/
Q_INVOKABLE QString getDisplayNameForAddress(const QString& address);
/* This method creates conversation with an arbitrary address */
Q_INVOKABLE void createConversationForAddress(const QString& address);
/**
* This method ensurse whether the phone number format is valid or not
* TODO: This is here because I don't know how to make the QML call the smshelper directly
......@@ -73,6 +64,11 @@ public:
*/
Q_INVOKABLE bool isPhoneNumberValid(const QString& number);
/**
* This method creates conversation with an arbitrary address
*/
Q_INVOKABLE void createConversationForAddress(const QString& address);
public Q_SLOTS:
void handleCreatedConversation(const QDBusVariant& msg);
void handleConversationUpdated(const QDBusVariant& msg);
......
......@@ -21,6 +21,7 @@
#include "conversationssortfilterproxymodel.h"
#include "conversationlistmodel.h"
#include "smshelper.h"
#include <QString>
#include <QLoggingCategory>
......@@ -31,16 +32,16 @@ Q_LOGGING_CATEGORY(KDECONNECT_SMS_CONVERSATIONS_SORT_FILTER_PROXY_MODEL, "kdecon
ConversationsSortFilterProxyModel::ConversationsSortFilterProxyModel()
{
setFilterRole(ConversationListModel::DateRole);
setFilterRole(ConversationListModel::ConversationIdRole);
}
ConversationsSortFilterProxyModel::~ConversationsSortFilterProxyModel(){}
void ConversationsSortFilterProxyModel::setOurFilterRole(int role)
void ConversationsSortFilterProxyModel::setConversationsFilterRole(int role)
{
setFilterRole(role);
}
ConversationsSortFilterProxyModel::~ConversationsSortFilterProxyModel() {}
bool ConversationsSortFilterProxyModel::lessThan(const QModelIndex& leftIndex, const QModelIndex& rightIndex) const
{
QVariant leftDataTimeStamp = sourceModel()->data(leftIndex, ConversationListModel::DateRole);
......@@ -61,5 +62,22 @@ bool ConversationsSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QM
if (filterRole() == Qt::DisplayRole) {
return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
}
return sourceModel()->data(index, ConversationListModel::DateRole) != INVALID_THREAD_ID;
if (filterRole() == ConversationListModel::SenderRole && !sourceModel()->data(index, ConversationListModel::MultitargetRole).toBool()) {
return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
}
return sourceModel()->data(index, ConversationListModel::ConversationIdRole) != INVALID_THREAD_ID;
}
bool ConversationsSortFilterProxyModel::isPhoneNumberExists(const QString &address)
{
for(int i = 0; i < rowCount(); ++i) {
if (!data(index(i, 0), ConversationListModel::MultitargetRole).toBool()) {
QVariant senderAddress = data(index(i, 0), ConversationListModel::SenderRole);
if (SmsHelper::isPhoneNumberMatch(senderAddress.toString(), address)) {
return true;
}
}
}
return false;
}
......@@ -48,7 +48,13 @@ public:
sortNow();
}
Q_INVOKABLE void setOurFilterRole(int role);
Q_INVOKABLE void setConversationsFilterRole(int role);
/**
* This method gets name of conversations or contact if it find any matching address
* Needed for checking if the converstion already or contact already exist or no before adding an arbbitrary contact
*/
Q_INVOKABLE bool isPhoneNumberExists(const QString& address);
ConversationsSortFilterProxyModel();
~ConversationsSortFilterProxyModel();
......
......@@ -125,7 +125,6 @@ Kirigami.ScrollablePage
readonly property bool deviceConnected: devicesCombo.enabled
readonly property QtObject device: devicesCombo.currentIndex >= 0 ? devicesModel.data(devicesModel.index(devicesCombo.currentIndex, 0), DevicesModel.DeviceRole) : null
readonly property alias lastDeviceId: conversationListModel.deviceId
property string displayName
Component {
id: chatView
......@@ -148,7 +147,6 @@ Kirigami.ScrollablePage
}
}
header: TextField {
/**
* Used as the filter of the list of messages
......@@ -160,22 +158,21 @@ Kirigami.ScrollablePage
z: 10
onTextChanged: {
if (filter.text != "") {
view.model.setOurFilterRole(Qt.DisplayRole)
if (conversationListModel.isPhoneNumberValid(filter.text)) {
addButton.visible = true
view.model.setConversationsFilterRole(ConversationListModel.SenderRole)
} else {
addButton.visible = false
filter.width = view.width
view.model.setConversationsFilterRole(Qt.DisplayRole)
}
} else {
view.model.setOurFilterRole(ConversationListModel.ConversationIdRole)
view.model.setConversationsFilterRole(ConversationListModel.ConversationIdRole)
filter.width = view.width
}
view.model.setFilterFixedString(filter.text)
view.currentIndex = 0
if (conversationListModel.isPhoneNumberValid(filter.text)) {
addButton.visible = true
addButton.focus = true
} else {
addButton.visible = false
addButton.focus = false
filter.width = view.width
}
}
onAccepted: {
view.currentItem.startChat()
......@@ -208,11 +205,9 @@ Kirigami.ScrollablePage
onClicked: {
// We have to disable the filter temporarily in order to avoid getting key inputs accidently while processing the request
view.headerItem.enabled = false
// If the address entered by the user already exists, fetch the name of the contact otherwise it will return empty string
displayName = conversationListModel.getDisplayNameForAddress(view.headerItem.text)
if (displayName != "") {
view.headerItem.text = displayName
} else {
// If the address entered by the user already exists then ignore adding new contact
if (!view.model.isPhoneNumberExists(view.headerItem.text)) {
conversationListModel.createConversationForAddress(view.headerItem.text)
}
......@@ -224,6 +219,10 @@ Kirigami.ScrollablePage
event.clicked = true
addButton.onClicked()
}
Shortcut {
sequence: "Ctrl+A"
onActivated: addButton.forceActiveFocus()
}
}
delegate: Kirigami.AbstractListItem
......
......@@ -129,9 +129,11 @@ QString SmsHelper::canonicalizePhoneNumber(const QString& phoneNumber)
bool SmsHelper::isPhoneNumberValid(const QString& phoneNumber)
{
// This regular expression matches a wide range of international Phone number formats, minimum of 3 digits and maximum upto 15 digits
QRegularExpression validNumberPattern(QStringLiteral("^((\\+?(\\d{2}))\\s?)?((\\d{2})|(\\((\\d{2})\\))\\s?)?(\\d{3,15})(\\-(\\d{3,15}))?$"));
return validNumberPattern.match(phoneNumber).hasMatch();
QString canonicalizedPhoneNumber(phoneNumber);
// This regular expression matches a wide range of international Phone numbers, minimum of 3 digits and maximum upto 15 digits
QRegularExpression validNumberPattern(QStringLiteral("^(\\d{3,15})$"));
return validNumberPattern.match(canonicalizedPhoneNumber).hasMatch();
}
class PersonsCache : public QObject {
......
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