summaryrefslogtreecommitdiff
path: root/saltoqd/commmanager.cpp
blob: 271749aa56571b81d922a1aafb69dc067a16a8f9 (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
#include "commmanager.h"

using namespace CommHistory;

CommManager::CommManager(StorageManager *storage, ToqManager *toq) :
	QObject(toq), _toq(toq), _storage(storage),
	_calls(new CallModel(this)),
	_convs(new GroupModel(this)),
	_refreshTimer(new QTimer(this))
{
	_calls->setTreeMode(false);
	_calls->setQueryMode(EventModel::AsyncQuery);
	_calls->setSorting(CallModel::SortByTime);
	_calls->setResolveContacts(true);
	_calls->setLimit(20);

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

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

	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(_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 now";
	QMultiMap<QDateTime, QJsonObject> events;

	int rows = _calls->rowCount();
	for (int i = 0; i < rows; i++) {
		Event e = _calls->event(i);
		qDebug() << "Got call" << e.toString();
		QDateTime dt = e.startTime();
		QJsonObject obj;
		obj.insert("CommsType", QLatin1String("Call"));
		obj.insert("ReceivedTime", qint64(dt.toTime_t()));
		obj.insert("CallerId", e.contactName());
		obj.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());
		obj.insert("CommsDetails", details);

		events.insert(dt, obj);
	}

	rows = _convs->rowCount();
	for (int i = 0; i < rows; i++) {
		Group g = _convs->group(_convs->index(i, 0));
		qDebug() << "Chat" << g.contactName();
	}
}