forked from andijakl/nfcinteractor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnfcstats.cpp
143 lines (124 loc) · 4.73 KB
/
nfcstats.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
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Andreas Jakl (andreas.jakl@nokia.com)
**
** Released under Nokia Example Code License.
** See license.txt in the main project folder.
**
****************************************************************************/
#include "nfcstats.h"
NfcStats::NfcStats(QObject *parent) :
QObject(parent)
{
loadStats();
}
int NfcStats::tagReadCount()
{
return m_tagReadCount;
}
void NfcStats::incTagReadCount()
{
m_tagReadCount++;
saveStats();
}
QHash<NfcTypes::MessageType, int> NfcStats::msgWrittenCountAll()
{
return m_writtenMsgCount;
}
int NfcStats::msgWrittenCount(NfcTypes::MessageType msgType)
{
return m_writtenMsgCount.value(msgType, 0);
}
int NfcStats::advMsgWrittenCount()
{
return m_writtenMsgCount.value(NfcTypes::MsgSms, 0) +
m_writtenMsgCount.value(NfcTypes::MsgBusinessCard, 0) +
m_writtenMsgCount.value(NfcTypes::MsgSocialNetwork, 0) +
m_writtenMsgCount.value(NfcTypes::MsgGeo, 0) +
m_writtenMsgCount.value(NfcTypes::MsgStore, 0) +
m_writtenMsgCount.value(NfcTypes::MsgCustom, 0);
}
bool NfcStats::isAdvMsgType(NfcTypes::MessageType msgType)
{
return (msgType == NfcTypes::MsgSms ||
msgType == NfcTypes::MsgBusinessCard ||
msgType == NfcTypes::MsgSocialNetwork ||
msgType == NfcTypes::MsgGeo ||
msgType == NfcTypes::MsgStore ||
msgType == NfcTypes::MsgCustom);
}
void NfcStats::resetComposedMsgCount()
{
m_composedMsgCount.clear();
}
void NfcStats::incComposedMsgCount(NfcTypes::MessageType msgType)
{
// Update the count in the hash by one
// (using 0 as the default if this tag hasn't been
// written yet)
const int count = m_composedMsgCount.value(msgType, 0) + 1;
// Insert into the hash
// (overwrites an existing value)
m_composedMsgCount.insert(msgType, count);
}
void NfcStats::commitComposedToWrittenCount()
{
// Loop over composed messages present in the current NDEF message,
// and store them to the actually written messages
QHash<NfcTypes::MessageType,int>::const_iterator i = m_composedMsgCount.constBegin();
while (i != m_composedMsgCount.constEnd()) {
incWrittenMsgCount(i.key(), i.value());
++i;
}
saveStats();
}
void NfcStats::incWrittenMsgCount(NfcTypes::MessageType msgType, const int numMsgPresent)
{
const int count = m_writtenMsgCount.value(msgType, 0) + numMsgPresent;
m_writtenMsgCount.insert(msgType, count);
}
void NfcStats::loadStats()
{
QSettings settings(SETTINGS_ORG, SETTINGS_APP, this);
m_tagReadCount = settings.value("TagReadCount", 0).toInt();
// Ok, we could loop over the enum, treating it as an int
// But let's rather add them individually, for safety reasons
// in case something changes in the future.
loadCount(&settings, NfcTypes::MsgSmartPoster);
loadCount(&settings, NfcTypes::MsgUri);
loadCount(&settings, NfcTypes::MsgText);
loadCount(&settings, NfcTypes::MsgSms);
loadCount(&settings, NfcTypes::MsgBusinessCard);
loadCount(&settings, NfcTypes::MsgSocialNetwork);
loadCount(&settings, NfcTypes::MsgGeo);
loadCount(&settings, NfcTypes::MsgStore);
loadCount(&settings, NfcTypes::MsgImage);
loadCount(&settings, NfcTypes::MsgCustom);
}
void NfcStats::loadCount(QSettings* settings, const NfcTypes::MessageType msgType)
{
m_writtenMsgCount.insert(msgType, settings->value(QString("msg") + QString::number((int)msgType), 0).toInt());
}
void NfcStats::saveStats()
{
QSettings settings(SETTINGS_ORG, SETTINGS_APP, this);
settings.setValue("TagReadCount", m_tagReadCount);
//qDebug() << "Save tag read count: " << m_tagReadCount;
saveCount(&settings, NfcTypes::MsgSmartPoster);
saveCount(&settings, NfcTypes::MsgUri);
saveCount(&settings, NfcTypes::MsgText);
saveCount(&settings, NfcTypes::MsgSms);
saveCount(&settings, NfcTypes::MsgBusinessCard);
saveCount(&settings, NfcTypes::MsgSocialNetwork);
saveCount(&settings, NfcTypes::MsgGeo);
saveCount(&settings, NfcTypes::MsgStore);
saveCount(&settings, NfcTypes::MsgImage);
saveCount(&settings, NfcTypes::MsgCustom);
}
void NfcStats::saveCount(QSettings* settings, const NfcTypes::MessageType msgType)
{
//qDebug() << "Save written message count: " << QString("msg") << QString::number((int)msgType) << m_writtenMsgCount.value(msgType, 0);
settings->setValue(QString("msg") + QString::number((int)msgType), m_writtenMsgCount.value(msgType, 0));
}