Skip to content

Commit

Permalink
[libcommhistory] Add a recipient event model to fetch events from uns…
Browse files Browse the repository at this point in the history
…aved numbers. Contributes to TJC#143264
  • Loading branch information
dcaliste authored and blammit committed Nov 30, 2020
1 parent 0d440d5 commit f034d37
Show file tree
Hide file tree
Showing 12 changed files with 486 additions and 41 deletions.
30 changes: 27 additions & 3 deletions declarative/src/contacteventmodel.cpp
Expand Up @@ -30,13 +30,14 @@
*/

#include "contacteventmodel.h"
#include "commonutils.h"
#include <QTimer>

using namespace CommHistory;

ContactEventModel::ContactEventModel(QObject *parent)
: SingleContactEventModel(parent)
, m_contactId(-1)
, m_contactId(0)
{
}

Expand All @@ -51,8 +52,31 @@ void ContactEventModel::setContactId(int contactId)
QTimer::singleShot(0, this, SLOT(reload()));
}

void ContactEventModel::reload()
void ContactEventModel::setFallbackPhoneId(const QString &fallbackPhoneId)
{
getEvents(m_contactId);
if (fallbackPhoneId == m_fallbackPhoneId)
return;

m_fallbackPhoneId = fallbackPhoneId;
emit fallbackPhoneIdChanged();

if (!m_contactId) {
QTimer::singleShot(0, this, SLOT(reload()));
}
}

void ContactEventModel::reload()
{
if (m_contactId > 0) {
getEvents(m_contactId);
} else if (!m_fallbackPhoneId.isEmpty()) {
// LocalUID is set to QString because we don't care about which sim
// the call was received on.
// SingleContactEventModel::getEvents(Recipient) also exists and
// is matching a stored contact from recipient phone number. We actually
// need here to get events for a recipient event if not saved, so we
// must call the RecipientEventModel::getEvents() explicitely to avoid
// calling implicitely the SingleContactEventModel variant.
RecipientEventModel::getEvents(Recipient(QString(), m_fallbackPhoneId));
}
}
6 changes: 6 additions & 0 deletions declarative/src/contacteventmodel.h
Expand Up @@ -40,21 +40,27 @@ class ContactEventModel : public CommHistory::SingleContactEventModel
{
Q_OBJECT
Q_PROPERTY(int contactId READ contactId WRITE setContactId NOTIFY contactIdChanged)
Q_PROPERTY(QString fallbackPhoneId READ fallbackPhoneId WRITE setFallbackPhoneId NOTIFY fallbackPhoneIdChanged)

public:
ContactEventModel(QObject *parent = 0);

int contactId() const { return m_contactId; }
void setContactId(int contactId);

QString fallbackPhoneId() const { return m_fallbackPhoneId; }
void setFallbackPhoneId(const QString &fallbackPhoneId);

signals:
void contactIdChanged();
void fallbackPhoneIdChanged();

public slots:
void reload();

private:
int m_contactId;
QString m_fallbackPhoneId;
};

#endif
112 changes: 112 additions & 0 deletions src/recipienteventmodel.cpp
@@ -0,0 +1,112 @@
/******************************************************************************
**
** This file is part of libcommhistory.
**
** Copyright (C) 2020 D. Caliste.
** Contact: Damien Caliste <dcaliste@free.fr>
**
** This library is free software; you can redistribute it and/or modify it
** under the terms of the GNU Lesser General Public License version 2.1 as
** published by the Free Software Foundation.
**
** This library is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
** or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
** License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this library; if not, write to the Free Software Foundation, Inc.,
** 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
**
******************************************************************************/

#include "recipienteventmodel.h"

#include "databaseio_p.h"
#include "eventmodel_p.h"
#include "recipienteventmodel_p.h"

#include <QSqlQuery>

namespace CommHistory {

RecipientEventModelPrivate::RecipientEventModelPrivate(RecipientEventModel *model)
: EventModelPrivate(model)
{
}

bool RecipientEventModelPrivate::acceptsEvent(const Event &event) const
{
return m_recipients.intersects(event.recipients());
}

void RecipientEventModelPrivate::fetchEvents()
{
if (!m_recipients.isEmpty()) {
// Get the events that match these addresses
QStringList clauses;
QVariantList values;
for (RecipientList::const_iterator it = m_recipients.constBegin();
it != m_recipients.constEnd(); ++it) {
if (it->localUid().isEmpty()) {
clauses.append(QString("(remoteUid LIKE ? AND localUid LIKE '%1%%')").arg(RING_ACCOUNT));
values.append(QString("%%%1%%").arg(minimizePhoneNumber(it->remoteUid())));
} else {
clauses.append("(remoteUid = ? AND localUid = ?)");
values.append(it->remoteUid());
values.append(it->localUid());
}
}

QString where("WHERE ( ");
where.append(clauses.join(" OR "));
where.append(" ) ORDER BY Events.endTime DESC, Events.id DESC");

QSqlQuery query = prepareQuery(DatabaseIOPrivate::eventQueryBase() + where);

foreach (const QVariant &value, values)
query.addBindValue(value);

executeQuery(query);
} else {
modelUpdatedSlot(true);
}
}

RecipientEventModel::RecipientEventModel(QObject *parent)
: EventModel(*new RecipientEventModelPrivate(this), parent)
{
setResolveContacts(ResolveImmediately);
}

RecipientEventModel::RecipientEventModel(RecipientEventModelPrivate &dd, QObject *parent)
: EventModel(dd, parent)
{
setResolveContacts(ResolveImmediately);
}

RecipientEventModel::~RecipientEventModel()
{
}

bool RecipientEventModel::getEvents(const RecipientList &recipients)
{
Q_D(RecipientEventModel);

beginResetModel();
d->clearEvents();
d->isReady = false;
endResetModel();

d->m_recipients = recipients;
d->fetchEvents();
return true;
}

bool RecipientEventModel::getEvents(const Recipient &recipient)
{
return getEvents(RecipientList() << recipient);
}

} // namespace CommHistory

83 changes: 83 additions & 0 deletions src/recipienteventmodel.h
@@ -0,0 +1,83 @@
/******************************************************************************
**
** This file is part of libcommhistory.
**
** Copyright (C) 2020 D. Caliste
** Contact: Damien Caliste <dcaliste@free.fr>
**
** This library is free software; you can redistribute it and/or modify it
** under the terms of the GNU Lesser General Public License version 2.1 as
** published by the Free Software Foundation.
**
** This library is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
** or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
** License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this library; if not, write to the Free Software Foundation, Inc.,
** 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
**
******************************************************************************/

#ifndef COMMHISTORY_RECIPIENT_EVENT_MODEL_H
#define COMMHISTORY_RECIPIENT_EVENT_MODEL_H

#include "libcommhistoryexport.h"
#include "eventmodel.h"

namespace CommHistory {

class RecipientEventModelPrivate;

/*!
* \class RecipientEventModel
* \brief Model representing events from one or several local / remote uids.
*/
class LIBCOMMHISTORY_EXPORT RecipientEventModel : public EventModel
{
Q_OBJECT

public:
/*!
* Model constructor.
*
* \param parent Parent object.
*/
RecipientEventModel(QObject *parent = 0);

/*!
* Destructor.
*/
~RecipientEventModel();

/*!
* Populate model with events matching the recipient list.
* It's not necessary that the recipients are contact resolved.
*
* \param recipients, local / remote uid pairs to be fetched.
*
* \return true if successful, otherwise false
*/
bool getEvents(const RecipientList &recipients);
/*!
* Populate model with events matching the given recipient.
* It's not necessary that the recipient is contact resolved.
*
* \param recipient, local / remote uid pairs to be fetched.
*
* \return true if successful, otherwise false
*/
bool getEvents(const Recipient &recipient);

protected:
RecipientEventModel(RecipientEventModelPrivate &dd, QObject *parent = 0);

private:
Q_DECLARE_PRIVATE(RecipientEventModel)
};

} // namespace CommHistory

#endif // COMMHISTORY_RECIPIENT_EVENT_MODEL_H

48 changes: 48 additions & 0 deletions src/recipienteventmodel_p.h
@@ -0,0 +1,48 @@
/******************************************************************************
**
** This file is part of libcommhistory.
**
** Copyright (C) 2020 D. Caliste.
** Contact: Damien Caliste <dcaliste@free.fr>
**
** This library is free software; you can redistribute it and/or modify it
** under the terms of the GNU Lesser General Public License version 2.1 as
** published by the Free Software Foundation.
**
** This library is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
** or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
** License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this library; if not, write to the Free Software Foundation, Inc.,
** 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
**
******************************************************************************/

#ifndef COMMHISTORY_RECIPIENT_EVENT_MODEL_P_H
#define COMMHISTORY_RECIPIENT_EVENT_MODEL_P_H

#include "recipienteventmodel.h"
#include "eventmodel_p.h"

namespace CommHistory {

class LIBCOMMHISTORY_EXPORT RecipientEventModelPrivate : public EventModelPrivate
{
Q_OBJECT

Q_DECLARE_PUBLIC(RecipientEventModel)

public:
RecipientEventModelPrivate(RecipientEventModel *model = 0);

virtual bool acceptsEvent(const Event &event) const;
void fetchEvents();

RecipientList m_recipients;
};

}

#endif

0 comments on commit f034d37

Please sign in to comment.