/
callhistory.cpp
170 lines (141 loc) · 4.97 KB
/
callhistory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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;
}
}