1/*
2 * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#include "symbolusagemanager.h"
18#include "settings.h"
19#include "systemconsts.h"
20
21#include <QFile>
22#include <QJsonArray>
23#include <QJsonDocument>
24#include <QJsonObject>
25#include <QMessageBox>
26
27SymbolUsageManager::SymbolUsageManager(QObject *parent) : QObject(parent)
28{
29
30}
31
32void SymbolUsageManager::load()
33{
34 QString filename = includeTrailingPathDelimiter(pSettings->dirs().config())
35 + DEV_SYMBOLUSAGE_FILE;
36 if (!fileExists(filename))
37 return;
38 QFile file(filename);
39 if (!file.open(QFile::ReadOnly)) {
40 QMessageBox::critical(nullptr,
41 tr("Load symbol usage info failed"),
42 tr("Can't open symbol usage file '%1' for read.")
43 .arg(filename));
44 }
45 QByteArray contents = file.readAll();
46 QJsonParseError error;
47 QJsonDocument doc = QJsonDocument::fromJson(contents,&error);
48 if (error.error != QJsonParseError::NoError) {
49 QMessageBox::critical(nullptr,
50 tr("Load symbol usage info failed"),
51 tr("Can't parse symbol usage file '%1': %2")
52 .arg(filename)
53 .arg(error.errorString()));
54 }
55
56 mUsages.clear();
57 QJsonArray array = doc.array();
58 foreach (const QJsonValue& val, array) {
59 QJsonObject obj = val.toObject();
60 QString fullname = obj["symbol"].toString();
61 int count = obj["count"].toInt();
62 PSymbolUsage usage = std::make_shared<SymbolUsage>();
63 usage->fullName = fullname;
64 usage->count = count;
65 mUsages.insert(fullname,usage);
66 }
67}
68
69void SymbolUsageManager::save()
70{
71 QString filename = includeTrailingPathDelimiter(pSettings->dirs().config())
72 + DEV_SYMBOLUSAGE_FILE;
73 QFile file(filename);
74 if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
75 QMessageBox::critical(nullptr,
76 tr("Save symbol usage info failed"),
77 tr("Can't open symbol usage file '%1' for write.")
78 .arg(filename));
79 }
80 QJsonArray array;
81 foreach (const PSymbolUsage& usage,mUsages) {
82 QJsonObject object;
83 object["symbol"]=usage->fullName;
84 object["count"]=usage->count;
85 array.append(object);
86 }
87 QJsonDocument doc;
88 doc.setArray(array);
89 if (file.write(doc.toJson())<0) {
90 QMessageBox::critical(nullptr,
91 tr("Save symbol usage info failed"),
92 tr("Write to symbol usage file '%1' failed.")
93 .arg(filename));
94 }
95}
96
97void SymbolUsageManager::reset()
98{
99 mUsages.clear();
100 save();
101}
102
103PSymbolUsage SymbolUsageManager::findUsage(const QString &fullName) const
104{
105 return mUsages.value(fullName,PSymbolUsage());
106}
107
108void SymbolUsageManager::updateUsage(const QString &symbol, int count)
109{
110 PSymbolUsage usage = mUsages.value(symbol,PSymbolUsage());
111 if (usage) {
112 usage->count = count;
113 } else {
114 usage = std::make_shared<SymbolUsage>();
115 usage->fullName = symbol;
116 usage->count = count;
117 mUsages.insert(symbol,usage);
118 }
119}
120