Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'omp-jb50563' into 'master'
[libcommhistory] Create CallHistory class. Contributes to JB#50563 See merge request mer-core/libcommhistory!46
- Loading branch information
Showing
10 changed files
with
326 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/****************************************************************************** | ||
** | ||
** This file is part of libcommhistory. | ||
** | ||
** Copyright (c) 2020 Open Mobile Platform LLC. | ||
** | ||
** 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 "callhistory_p.h" | ||
#include "databaseio_p.h" | ||
#include "event.h" | ||
|
||
#include <QtDebug> | ||
#include <QSqlQuery> | ||
#include <QSqlError> | ||
#include <QSqlDatabase> | ||
|
||
namespace { | ||
|
||
const QDateTime epoch(QDate(1970, 1, 1), QTime(0, 0), Qt::UTC); | ||
|
||
QString buildEventsQuery(CommHistory::CallEvent::CallType callType, | ||
const QDateTime &startTime, | ||
const QDateTime &endTime) | ||
{ | ||
qint64 startTimeSecs = (startTime.isValid() ? startTime : QDateTime::fromMSecsSinceEpoch(0)).toMSecsSinceEpoch() / 1000; | ||
qint64 endTimeSecs = (endTime.isValid() ? endTime : QDateTime::currentDateTimeUtc()).toMSecsSinceEpoch() / 1000; | ||
|
||
QStringList conditions; | ||
|
||
conditions.append(QStringLiteral("startTime >= %1").arg(startTimeSecs)); | ||
conditions.append(QStringLiteral("startTime <= %1").arg(endTimeSecs)); | ||
conditions.append(QStringLiteral("type=%1").arg(CommHistory::Event::EventType::CallEvent)); | ||
|
||
switch (callType) { | ||
case CommHistory::CallEvent::UnknownCallType: | ||
break; | ||
case CommHistory::CallEvent::ReceivedCallType: | ||
conditions.append(QString::fromLatin1("direction=%1 AND isMissedCall=0").arg(CommHistory::Event::Inbound)); | ||
break; | ||
case CommHistory::CallEvent::MissedCallType: | ||
conditions.append(QString::fromLatin1("direction=%1 AND isMissedCall=1").arg(CommHistory::Event::Inbound)); | ||
break; | ||
case CommHistory::CallEvent::DialedCallType: | ||
conditions.append(QString::fromLatin1("direction=%1").arg(CommHistory::Event::Outbound)); | ||
break; | ||
} | ||
|
||
QString group; | ||
static const QString groupTemplate = QStringLiteral(" GROUP BY strftime('%1', datetime(startTime, 'unixepoch'))"); | ||
|
||
QString q = "SELECT startTime, endTime, remoteUid from Events"; | ||
if (!conditions.isEmpty()) { | ||
q += " WHERE " + conditions.join(" AND "); | ||
} | ||
|
||
return q + group; | ||
} | ||
|
||
QList<CommHistory::CallHistory::Result> readQueryResult(QSqlQuery *query) | ||
{ | ||
QList<CommHistory::CallHistory::Result> results; | ||
while (query->next()) { | ||
CommHistory::CallHistory::Result result; | ||
result.when = QDateTime::fromMSecsSinceEpoch(query->value(0).toLongLong() * 1000).toUTC(); | ||
result.finish = QDateTime::fromMSecsSinceEpoch(query->value(1).toLongLong() * 1000).toUTC(); | ||
result.phoneNumber = query->value(2).toString(); | ||
results.append(result); | ||
} | ||
return results; | ||
} | ||
|
||
} | ||
|
||
namespace CommHistory { | ||
|
||
CallHistoryPrivate::CallHistoryPrivate(CallHistory *parent) | ||
: QObject(parent) | ||
, q(parent) | ||
, callType(CallEvent::UnknownCallType) | ||
{ | ||
} | ||
|
||
CallHistory::CallHistory(QObject *parent) | ||
: QObject(parent) | ||
, d(new CallHistoryPrivate(this)) | ||
{ | ||
} | ||
|
||
void CallHistory::setStartTime(const QDateTime &dt) | ||
{ | ||
if (dt != d->startTime) { | ||
d->startTime = dt; | ||
emit startTimeChanged(); | ||
} | ||
} | ||
|
||
QDateTime CallHistory::startTime() const | ||
{ | ||
return d->startTime; | ||
} | ||
|
||
void CallHistory::setEndTime(const QDateTime &dt) | ||
{ | ||
if (dt != d->endTime) { | ||
d->endTime = dt; | ||
emit endTimeChanged(); | ||
} | ||
} | ||
|
||
QDateTime CallHistory::endTime() const | ||
{ | ||
return d->endTime; | ||
} | ||
|
||
void CallHistory::setCallType(CallEvent::CallType type) | ||
{ | ||
if (type != d->callType) { | ||
d->callType = type; | ||
emit callTypeChanged(); | ||
} | ||
} | ||
|
||
CallEvent::CallType CallHistory::callType() const | ||
{ | ||
return d->callType; | ||
} | ||
|
||
QList<CommHistory::CallHistory::Result> CallHistory::results() const | ||
{ | ||
return d->results; | ||
} | ||
|
||
bool CallHistory::reload() | ||
{ | ||
d->results.clear(); | ||
|
||
if (d->startTime.isValid() && d->endTime.isValid() && d->endTime <= d->startTime) { | ||
qWarning() << "Error: end time" << d->endTime.toString() | ||
<< "is not after start time" << d->startTime.toString(); | ||
return false; | ||
} | ||
|
||
QString queryString = buildEventsQuery(d->callType, d->startTime, d->endTime); | ||
|
||
QSqlQuery query = DatabaseIOPrivate::prepareQuery(queryString); | ||
if (!query.exec()) { | ||
qWarning() << "Failed to execute query:" << query.lastQuery(); | ||
qWarning() << "Error was:" << query.lastError(); | ||
return false; | ||
} | ||
|
||
d->results = readQueryResult(&query); | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/****************************************************************************** | ||
** | ||
** This file is part of libcommhistory. | ||
** | ||
** Copyright (c) 2020 Open Mobile Platform LLC. | ||
** | ||
** 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_CALLHISTORY_H | ||
#define COMMHISTORY_CALLHISTORY_H | ||
|
||
#include <QObject> | ||
#include <QDateTime> | ||
|
||
#include "libcommhistoryexport.h" | ||
#include "callevent.h" | ||
|
||
namespace CommHistory { | ||
|
||
class CallHistoryPrivate; | ||
|
||
class LIBCOMMHISTORY_EXPORT CallHistory : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
struct Result { | ||
QDateTime when; | ||
QDateTime finish; | ||
QString phoneNumber; | ||
}; | ||
|
||
explicit CallHistory(QObject *parent = 0); | ||
|
||
void setStartTime(const QDateTime &dt); | ||
QDateTime startTime() const; | ||
|
||
void setEndTime(const QDateTime &dt); | ||
QDateTime endTime() const; | ||
|
||
void setCallType(CallEvent::CallType type); | ||
CallEvent::CallType callType() const; | ||
|
||
QList<CommHistory::CallHistory::Result> results() const; | ||
|
||
bool reload(); | ||
|
||
Q_SIGNALS: | ||
void startTimeChanged(); | ||
void endTimeChanged(); | ||
void callTypeChanged(); | ||
|
||
private: | ||
CallHistoryPrivate *d; | ||
}; | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/****************************************************************************** | ||
** | ||
** This file is part of libcommhistory. | ||
** | ||
** Copyright (c) 2020 Open Mobile Platform LLC. | ||
** | ||
** 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 CALLHISTORY_P_H | ||
#define CALLHISTORY_P_H | ||
|
||
#include "callhistory.h" | ||
|
||
namespace CommHistory { | ||
|
||
class CallHistoryPrivate: public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
explicit CallHistoryPrivate(CallHistory *parent); | ||
|
||
QList<CommHistory::CallHistory::Result> results; | ||
CallHistory *q; | ||
QDateTime startTime; | ||
QDateTime endTime; | ||
CallEvent::CallType callType; | ||
}; | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/****************************************************************************** | ||
** | ||
** This file is part of libcommhistory. | ||
** | ||
** Copyright (c) 2020 Open Mobile Platform LLC. | ||
** | ||
** 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 "callhistory.h" |
Oops, something went wrong.