forked from nostar/DroidStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogHandler.cpp
More file actions
220 lines (180 loc) · 6.98 KB
/
LogHandler.cpp
File metadata and controls
220 lines (180 loc) · 6.98 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
Copyright (C) 2024 Rohith Namboothiri
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "LogHandler.h"
#include <QDir>
#include <QDebug>
#include <QTextStream>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QStandardPaths>
#include <QFileInfo>
LogHandler::LogHandler(QObject *parent) : QObject(parent)
{
}
QString LogHandler::getFilePath(const QString &fileName) const
{
QString dirPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QDir dir(dirPath);
if (!dir.exists()) {
if (!dir.mkpath(dirPath)) {
qDebug() << "Failed to create directory:" << dirPath;
return QString();
}
}
QString filePath = dirPath + "/" + fileName;
qDebug() << "Log file path:" << filePath; // Log the file path
return filePath;
}
bool LogHandler::saveLog(const QString &fileName, const QJsonArray &logData)
{
QString filePath = getFilePath(fileName);
if (filePath.isEmpty()) {
return false; // Failed to create directory, so return false
}
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly)) {
qDebug() << "Failed to open file for writing:" << file.errorString();
return false;
}
QJsonDocument doc(logData);
file.write(doc.toJson());
file.close();
qDebug() << "Log saved successfully.";
return true;
}
QJsonArray LogHandler::loadLog(const QString &fileName)
{
QString filePath = getFilePath(fileName);
QJsonArray logData;
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << "Failed to open file for reading:" << file.errorString();
return logData; // Return empty array if the file can't be opened
}
QByteArray data = file.readAll();
QJsonDocument doc(QJsonDocument::fromJson(data));
if (!doc.isNull() && doc.isArray()) {
logData = doc.array();
qDebug() << "Log loaded successfully.";
} else {
qDebug() << "Failed to parse JSON log.";
}
file.close();
return logData;
}
bool LogHandler::clearLog(const QString &fileName)
{
QString filePath = getFilePath(fileName);
QFile file(filePath);
if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
file.close();
qDebug() << "Log cleared successfully.";
return true;
}
qDebug() << "Failed to clear log:" << file.errorString();
return false;
}
QString LogHandler::getDSLogPath() const {
QString externalStoragePath = "/storage/emulated/0/Download";
QString dsLogPath = externalStoragePath + "/DSLog";
QDir dsLogDir(dsLogPath);
qDebug() << "External storage path: " << externalStoragePath; // Debugging line
qDebug() << "DSLog path: " << dsLogPath; // Debugging line
if (!dsLogDir.exists()) {
if (!dsLogDir.mkpath(dsLogPath)) {
qDebug() << "Failed to create DSLog directory.";
return QString();
} else {
qDebug() << "DSLog directory created successfully.";
}
} else {
qDebug() << "DSLog directory already exists.";
}
return dsLogPath;
}
bool LogHandler::exportLogToCsv(const QString &fileName, const QJsonArray &logData) {
QString dsLogPath = getDSLogPath();
if (dsLogPath.isEmpty()) {
qDebug() << "DSLog path is not available.";
return false;
}
// Ensure that only the filename is appended to the directory path
QString filePath = dsLogPath + "/" + QFileInfo(fileName).fileName();
qDebug() << "Attempting to save file at: " << filePath;
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "Failed to open file for writing:" << file.errorString();
return false;
}
QTextStream out(&file);
// Write the CSV headers
out << "Sr.No,Callsign,DMR ID,TGID,Handle,Country,Time\n";
// Write the log data to the CSV file
for (int i = 0; i < logData.size(); ++i) {
QJsonObject entry = logData[i].toObject();
out << entry["serialNumber"].toInt() << ","
<< entry["callsign"].toString() << ","
<< entry["dmrID"].toInt() << ","
<< entry["tgid"].toInt() << ","
<< entry["fname"].toString() << ","
<< entry["country"].toString() << ","
<< entry["currentTime"].toString() << "\n";
}
file.close();
qDebug() << "Log exported successfully to" << filePath;
return true;
}
bool LogHandler::exportLogToAdif(const QString &fileName, const QJsonArray &logData) {
QString dsLogPath = getDSLogPath();
if (dsLogPath.isEmpty()) {
qDebug() << "DSLog path is not available.";
return false;
}
QString filePath = dsLogPath + "/" + QFileInfo(fileName).fileName(); // Ensure the file name is properly handled
qDebug() << "Attempting to save ADIF file at: " << filePath;
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "Failed to open file for writing:" << file.errorString();
return false;
}
QTextStream out(&file);
// Write the ADIF headers
out << "ADIF Export\n";
out << "<EOH>\n"; // End of Header
// Write each QSO record in ADIF format
for (int i = 0; i < logData.size(); ++i) {
QJsonObject entry = logData[i].toObject();
// Extract and format date and time
QString currentTime = entry["currentTime"].toString();
QString qsoDate = currentTime.left(10).remove('-'); // Format: YYYYMMDD
QString timeOn = currentTime.mid(11, 8).remove(':'); // Format: HHMMSS
// Write each QSO record with valid ADIF tags
out << "<CALL:" << entry["callsign"].toString().length() << ">" << entry["callsign"].toString();
out << "<BAND:4>70CM"; // Band is hardcoded as "70CM"
out << "<MODE:12>DIGITALVOICE"; // Mode is set to "DIGITALVOICE"
// Include the first name in the ADIF record
out << "<NAME:" << entry["fname"].toString().length() << ">" << entry["fname"].toString();
out << "<QSO_DATE:" << qsoDate.length() << ">" << qsoDate;
out << "<TIME_ON:6>" << timeOn;
out << "<EOR>\n"; // End of Record
}
file.close();
qDebug() << "Log exported successfully to" << filePath;
return true;
}
// Extract and display a user-friendly path
QString LogHandler::getFriendlyPath(const QString &fullPath) const {
return fullPath.mid(fullPath.indexOf("/Download/"));
}