summaryrefslogtreecommitdiff
path: root/saltoqd/contactsmanager.cpp
blob: 19e3c84832ae131941d133d4538fd1a7ce1d278b (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
#include "contactsmanager.h"
#include <QtContacts/QtContacts>
#include <qtcontacts-extensions_impl.h>

QTCONTACTS_USE_NAMESPACE

ContactsManager::ContactsManager(StorageManager *storage, ToqManager *toq) :
	QObject(toq), _toq(toq), _storage(storage),
	_contacts(new QContactManager(this)),
	_refreshTimer(new QTimer(this))
{
	connect(_contacts, &QContactManager::contactsAdded,
			this, &ContactsManager::scheduleRefresh);
	connect(_contacts, &QContactManager::dataChanged,
			this, &ContactsManager::scheduleRefresh);

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

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

qint64 ContactsManager::getRecordIdForContact(uint contactId)
{
	return contactId;
}

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

void ContactsManager::refresh()
{
	qDebug() << "Refreshing contacts";

	QContactSortOrder order;
	order.setBlankPolicy(QContactSortOrder::BlanksLast);
	order.setCaseSensitivity(Qt::CaseInsensitive);
	order.setDetailType(QContactDetail::TypeDisplayLabel, QContactDisplayLabel::FieldLabel);

	QList<QContact> contacts = _contacts->contacts(order);

	QJsonArray records;
	for (const QContact &contact : contacts) {
		QJsonObject payload;
		QContactName cName = contact.detail<QContactName>();
		QContactDisplayLabel cDisplay = contact.detail<QContactDisplayLabel>();
		QContactFavorite cFav = contact.detail<QContactFavorite>();
		QJsonObject name;
		name.insert("Initial", cName.prefix());
		name.insert("First", cName.firstName());
		name.insert("Middle", cName.middleName());
		name.insert("Last", cName.lastName());
		name.insert("Display", cDisplay.label());
		QJsonArray phones;
		for (const QContactPhoneNumber &cPhone : contact.details<QContactPhoneNumber>()) {
			QJsonObject phone;
			QString type;
			if (!cPhone.subTypes().isEmpty()) {
				if (cPhone.subTypes().first() == QContactPhoneNumber::SubTypeMobile) {
					type = "Mobile";
				}
			}
			if (type.isEmpty() && !cPhone.contexts().isEmpty()) {
				switch (cPhone.contexts().first()) {
				case QContactDetail::ContextHome:
					type = "Home";
					break;
				case QContactDetail::ContextWork:
					type = "Work";
					break;
				default:
					break;
				}
			}
			if (type.isEmpty()) {
				type = "Other";
			}
			phone.insert("Type", type);
			phone.insert("Number", cPhone.number());
			phones.append(phone);
		}
		payload.insert("Name", name);
		payload.insert("PhoneNumber", phones);
		payload.insert("IsFav", cFav.isFavorite());
		QJsonObject record;
		record.insert("RecordId", getRecordIdForContact(QtContactsSqliteExtensions::internalContactId(contact.id())));
		record.insert("RecordPayload", payload);
		records.append(record);
	}

	QString storeName("Phub.Phone.Contacts");
	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);

	emit changed();
}