27 #include <QCryptographicHash>
30 #include <QStringList>
34 #include <QMainWindow>
35 #include <QPushButton>
36 #include <QAbstractSlider>
37 #include <QAbstractSpinBox>
39 #include <QJsonObject>
40 #include <QJsonDocument>
48 UsageStatsPlugin::UsageStatsPlugin()
49 : sendUsageStats(true)
50 , sendPrivateData(true)
51 , installationUUID(
"")
54 loop =
new QEventLoop(
this);
55 connect(&netMngr, &QNetworkAccessManager::finished, loop, &QEventLoop::quit);
58 UsageStatsPlugin::~UsageStatsPlugin()
66 qSettings->beginGroup(QLatin1String("UsageStatistics"));
67 sendUsageStats = (qSettings->value(QLatin1String("SendUsageStats"), sendUsageStats).toBool());
69 (qSettings->value(QLatin1String("SendPrivateData"), sendPrivateData).toBool());
71 installationUUID = QUuid(qSettings->value(QLatin1String("InstallationUUID"), "").toString());
72 if (installationUUID.isNull()) {
73 installationUUID = QUuid::createUuid();
75 debugLogLevel = (qSettings->value(QLatin1String(
"DebugLogLevel"), debugLogLevel).toInt());
77 qSettings->endGroup();
83 qSettings->beginGroup(QLatin1String("UsageStatistics"));
84 qSettings->setValue(QLatin1String("SendUsageStats"), sendUsageStats);
85 qSettings->setValue(QLatin1String("SendPrivateData"), sendPrivateData);
86 qSettings->setValue(QLatin1String("InstallationUUID"), installationUUID.toString());
87 qSettings->setValue(QLatin1String("DebugLogLevel"), debugLogLevel);
89 qSettings->endGroup();
96 bool UsageStatsPlugin::initialize(
const QStringList &arguments, QString *errorString)
100 Core::ICore::instance()->readSettings(this);
109 pluginManager = ExtensionSystem::PluginManager::instance();
110 Q_ASSERT(pluginManager);
112 &UsageStatsPlugin::pluginsLoadEnded);
115 bool UsageStatsPlugin::coreAboutToClose()
119 QTimer::singleShot(10000, loop, &QEventLoop::quit);
120 QUrl url(
"http://dronin-autotown.appspot.com/usageStats");
121 QNetworkRequest request(url);
122 request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/json; charset=utf-8");
123 netMngr.post(request, processJson());
128 void UsageStatsPlugin::pluginsLoadEnded()
135 UploaderGadgetFactory *uploader = pluginManager->getObject<UploaderGadgetFactory>();
137 if (sendUsageStats) {
139 &UsageStatsPlugin::onDebugMessage,
140 static_cast<Qt::ConnectionType>(Qt::QueuedConnection
141 | Qt::UniqueConnection) );
144 &UsageStatsPlugin::addNewBoardSeen, Qt::UniqueConnection);
146 searchForWidgets(mw,
true);
150 &UsageStatsPlugin::addNewBoardSeen);
152 searchForWidgets(mw,
false);
160 b.
time = QDateTime::currentDateTime();
163 boardLogList.append(b);
166 void UsageStatsPlugin::searchForWidgets(QObject *mw,
bool conn)
168 foreach (QObject *obj, mw->children()) {
169 QAbstractButton *button = qobject_cast<QAbstractButton *>(obj);
170 QAbstractSlider *slider = qobject_cast<QAbstractSlider *>(obj);
171 QScrollBar *bar = qobject_cast<QScrollBar *>(obj);
172 QTabBar *tab = qobject_cast<QTabBar *>(obj);
175 connect(button, &QAbstractButton::clicked,
this, &UsageStatsPlugin::onButtonClicked,
176 Qt::UniqueConnection);
178 disconnect(button, &QAbstractButton::clicked,
this,
179 &UsageStatsPlugin::onButtonClicked);
181 }
else if (slider && !bar) {
183 connect(slider, &QAbstractSlider::valueChanged,
this,
184 &UsageStatsPlugin::onSliderValueChanged, Qt::UniqueConnection);
186 disconnect(slider, &QAbstractSlider::valueChanged,
this,
187 &UsageStatsPlugin::onSliderValueChanged);
191 connect(tab, &QTabBar::currentChanged,
this, &UsageStatsPlugin::onTabCurrentChanged,
192 Qt::UniqueConnection);
194 disconnect(tab, &QTabBar::currentChanged,
this,
195 &UsageStatsPlugin::onTabCurrentChanged);
198 searchForWidgets(obj, conn);
203 void UsageStatsPlugin::onButtonClicked()
206 QAbstractButton *button = qobject_cast<QAbstractButton *>(sender());
208 info.
data1 = button->text();
209 info.
data2 = QString::number(button->isChecked());
210 info.
className = button->metaObject()->className();
211 info.
parentName = button->parent()->objectName();
212 info.
time = QDateTime::currentDateTime();
214 widgetLogList.append(info);
217 void UsageStatsPlugin::onSliderValueChanged(
int value)
220 QAbstractSlider *slider = qobject_cast<QAbstractSlider *>(sender());
222 info.
data1 = QString::number(value);
223 info.
className = slider->metaObject()->className();
224 info.
parentName = slider->parent()->objectName();
225 info.
time = QDateTime::currentDateTime();
227 widgetLogList.append(info);
230 void UsageStatsPlugin::onTabCurrentChanged(
int value)
233 QTabBar *tab = qobject_cast<QTabBar *>(sender());
235 info.
data2 = QString::number(value);
236 info.
data1 = tab->tabText(value);
237 info.
className = tab->metaObject()->className();
238 info.
parentName = tab->parent()->objectName();
239 info.
time = QDateTime::currentDateTime();
241 widgetLogList.append(info);
244 QByteArray UsageStatsPlugin::processJson()
248 json[
"shareIP"] =
"true";
250 json[
"shareIP"] =
"false";
253 json[
"currentOS"] = QSysInfo::prettyProductName();
254 json[
"currentArch"] = QSysInfo::currentCpuArchitecture();
255 json[
"buildInfo"] = QSysInfo::buildAbi();
257 if (!installationUUID.isNull())
258 json[
"installationUUID"] = getInstallationUUID();
260 QJsonArray boardArray;
261 foreach (
boardLog board, boardLogList) {
263 b[
"time"] = board.
time.toUTC().toString(
"yyyy-MM-ddThh:mm:ss.zzzZ");
274 QCryptographicHash::hash(QByteArray::fromHex(board.
board.
cpu_serial.toUtf8()),
275 QCryptographicHash::Sha256)
277 boardArray.append(b);
279 json[
"boardsSeen"] = boardArray;
280 QJsonArray widgetArray;
283 j[
"time"] = w.
time.toUTC().toString(
"yyyy-MM-ddThh:mm:ss.zzzZ");
290 j[
"checked"] = w.
data2;
293 j[
"value"] = w.
data1;
297 j[
"index"] = w.
data2;
302 widgetArray.append(j);
304 json[
"widgets"] = widgetArray;
306 QJsonArray debugLogArray;
309 d[
"time"] = msg.
time.toUTC().toString(
"yyyy-MM-ddThh:mm:ss.zzzZ");
310 d[
"level"] = msg.
level;
313 d[
"file"] = msg.
file;
314 d[
"line"] = msg.
line;
316 debugLogArray.append(d);
318 json[
"debugLog"] = debugLogArray;
319 json[
"debugLevel"] = debugLogLevel;
321 return QJsonDocument(json).toJson();
323 bool UsageStatsPlugin::getSendPrivateData()
const
325 return sendPrivateData;
328 void UsageStatsPlugin::setSendPrivateData(
bool value)
330 sendPrivateData = value;
333 bool UsageStatsPlugin::getSendUsageStats()
const
335 return sendUsageStats;
338 void UsageStatsPlugin::setSendUsageStats(
bool value)
340 sendUsageStats = value;
343 QString UsageStatsPlugin::getInstallationUUID()
const
345 return installationUUID.toString().remove(QRegExp(
"[{}]*"));
348 int UsageStatsPlugin::getDebugLogLevel()
const
350 return debugLogLevel;
353 void UsageStatsPlugin::setDebugLogLevel(
int value)
355 debugLogLevel = value;
359 const QString &
file,
const int line,
const QString &
function)
361 if (level < debugLogLevel)
383 info.
time = QDateTime::currentDateTime();
390 debugMessageList.append(info);
394 : Core::ICoreListener(parent)
401 return m_parent->coreAboutToClose();
const char *const GCS_REVISION_PRETTY_STR
void message(DebugEngine::Level level, const QString &msg, const QString &file="", const int line=0, const QString &function="")
deviceDescriptorStruct device
AppCloseHook(UsageStatsPlugin *parent)
const char *const GCS_VERSION_LONG
virtual QMainWindow * mainWindow() const =0
Returns the main application window.
static ICore * instance()
void newBoardSeen(deviceInfo board, deviceDescriptorStruct device)
The Config Info is a helper-class to handle version changes in GCS configuration files.
QPointer< Core::IBoardType > board
static DebugEngine * getInstance()
virtual void saveSettings(IConfigurablePlugin *plugin, QSettings *qs=nullptr)=0
void addAutoReleasedObject(QObject *obj)