summaryrefslogtreecommitdiff
path: root/saltoqd/commmanager.cpp
blob: b078c0c090fbd178cb1aebe9de6f35c8f84d79d5 (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <QtCore/QJsonArray>
#include <CommHistory/ConversationModel>
#include "contactsmanager.h"
#include "commmanager.h"

using namespace CommHistory;

static const int RECORD_LIMIT = 20;
static const int PER_GROUP_LIMIT = 10;

CommManager::CommManager(MDConfGroup *settings, StorageManager *storage, ContactsManager *contacts, ToqManager *toq) :
	QObject(toq), _toq(toq), _contacts(contacts), _storage(storage), _settings(settings),
	_calls(new CallModel(this)),
	_convs(new GroupModel(this)),
	_refreshTimer(new QTimer(this))
{
	_calls->setQueryMode(EventModel::AsyncQuery);
	_calls->setSorting(CallModel::SortByTime);
	_calls->setResolveContacts(true);
	_calls->setLimit(RECORD_LIMIT);

	_convs->setQueryMode(EventModel::AsyncQuery);
	_convs->setLimit(RECORD_LIMIT);

	_refreshTimer->setSingleShot(true);
	_refreshTimer->setInterval(2000);

	connect(_calls, SIGNAL(modelReady(bool)),
			this, SLOT(scheduleRefresh()));
	connect(_calls, SIGNAL(modelReset()),
			this, SLOT(scheduleRefresh()));
	connect(_calls, SIGNAL(rowsInserted(QModelIndex,int,int)),
			this, SLOT(scheduleRefresh()));
	connect(_calls, SIGNAL(rowsRemoved(QModelIndex,int,int)),
			this, SLOT(scheduleRefresh()));

	connect(_convs, SIGNAL(modelReady(bool)),
			this, SLOT(scheduleRefresh()));
	connect(_convs, SIGNAL(modelReset()),
			this, SLOT(scheduleRefresh()));
	connect(_convs, SIGNAL(rowsInserted(QModelIndex,int,int)),
			this, SLOT(scheduleRefresh()));
	connect(_convs, SIGNAL(rowsRemoved(QModelIndex,int,int)),
			this, SLOT(scheduleRefresh()));

	connect(_refreshTimer, &QTimer::timeout,
			this, &CommManager::refresh);

	if (!_calls->getEvents()) {
		qWarning() << "Could not get the call log";
	}
	if (!_convs->getGroups()) {
		qWarning() << "Could not get conversation groups";
	}
}

void CommManager::scheduleRefresh()
{
	if (!_refreshTimer->isActive()) {
		_refreshTimer->start();
	}
}

void CommManager::refresh()
{
	qDebug() << "Refreshing recent comms";
	QMultiMap<QDateTime, QJsonObject> events;

	int rows = _calls->rowCount();
	for (int i = 0; i < rows; i++) {
		QModelIndex index = _calls->index(i, 0);
		Event e = _calls->event(index);
		QList<Event::Contact> contacts = e.contacts();
		QString name;
		if (!contacts.empty()) {
			name = contacts.first().second;
		}
		if (name.isEmpty()) {
			name = e.remoteUid();
		}
		if (name.isEmpty()) {
			name = tr("Private number");
		}
		QDateTime dt = e.startTime();
		QJsonObject obj;
		obj.insert("Name", name);
		obj.insert("ContactId", _contacts->getRecordIdForContact(e.contactId()));

		QJsonArray records;
		if (_calls->hasChildren(index)) {
			const int subrows = _calls->rowCount(index);
			for (int j = 0; j < subrows; j++) {
				QModelIndex index2 = _calls->index(j, 0, index);
				const Event e = _calls->event(index2);
				QJsonObject record;
				record.insert("CommsType", QLatin1String("Call"));
				record.insert("ReceivedTime", qint64(e.startTime().toTime_t()));
				record.insert("CallerId", e.remoteUid());
				record.insert("ItemId", e.id());
				QJsonObject details;
				details.insert("Duration", e.startTime().secsTo(e.endTime()));
				details.insert("PhoneType", QLatin1String("Other")); // TODO
				switch (e.direction()) {
				case Event::Inbound:
					details.insert("Direction", QLatin1String("Incoming"));
					break;
				case Event::Outbound:
					details.insert("Direction", QLatin1String("Outgoing"));
					break;
				default:
					details.insert("Direction", QLatin1String("Unknown"));
					break;
				}
				details.insert("IsMissedCall", e.isMissedCall());
				record.insert("CommsDetails", details);
				records.append(record);
			}
		}

		obj.insert("CommsRecords", records);

		events.insert(dt, obj);
	}

	QScopedPointer<ConversationModel> conv(new ConversationModel);
	conv->setTreeMode(false);
	conv->setLimit(PER_GROUP_LIMIT);
	conv->setQueryMode(EventModel::SyncQuery);

	rows = _convs->rowCount();
	for (int i = 0; i < rows; i++) {
		Group g = _convs->group(_convs->index(i, 0));

		QList<Event::Contact> contacts = g.contacts();
		QString name;
		if (!contacts.empty()) {
			name = contacts.first().second;
		}
		if (name.isEmpty() && !g.remoteUids().isEmpty()) {
			name = g.remoteUids().first();
		}
		if (name.isEmpty()) {
			name = tr("Unknown contact");
		}

		QJsonObject obj;
		obj.insert("Name", name);
		obj.insert("ContactId", _contacts->getRecordIdForContact(g.contactId()));

		QJsonArray records;

		if (conv->getEvents(g.id())) {
			const int subrows = conv->rowCount();
			for (int j = 0; j < subrows; j++) {
				Event e = conv->event(j);
				QJsonObject record;
				record.insert("CommsType", QLatin1String("Text"));
				record.insert("ReceivedTime", qint64(e.startTime().toTime_t()));
				record.insert("CallerId", e.remoteUid());
				record.insert("ItemId", e.id());
				QJsonObject details;
				details.insert("Message", e.freeText());
				details.insert("privileged", int(0)); // TODO
				switch (e.direction()) {
				case Event::Inbound:
					details.insert("Direction", int(1));
					break;
				case Event::Outbound:
					details.insert("Direction", int(2));
					break;
				default:
					details.insert("Direction", int(0));
					break;
				}
				details.insert("IsRead", e.isRead());
				record.insert("CommsDetails", details);
				records.append(record);
			}
		} else {
			qWarning() << "Failed to get events for group id" << g.id();
			QJsonObject record;
			record.insert("CommsType", QLatin1String("Text"));
			record.insert("ReceivedTime", qint64(g.startTime().toTime_t()));
			record.insert("CallerId", g.remoteUids().first());
			record.insert("ItemId", g.id());
			QJsonObject details;
			details.insert("Message", g.lastMessageText());
			details.insert("privileged", int(0)); // TODO
			details.insert("Direction", int(0));
			details.insert("IsRead", g.unreadMessages() == 0);
			record.insert("CommsDetails", details);
			records.append(record);
		}

		obj.insert("CommsRecords", records);

		events.insert(g.endTime(), obj);
	}

	QJsonArray records;

	int i = 0;
	auto it = events.end();
	while (it != events.begin()) {
		--it;

		QJsonObject record;
		record.insert("RecordId", i);
		record.insert("RecordPayload", it.value());
		records.append(record);

		if (++i >= RECORD_LIMIT) break;
	}

	QString storeName("Phub.Phone.RecentComms");
	QJsonObject store;
	store.insert("Name", storeName);
	store.insert("Records", records);

	QJsonObject root;
	root.insert("DataStore", store);

	qDebug() << "Got" << records.size() << "records";

	_storage->updateStore(storeName, root);
}