Compare commits
15 Commits
f392efb54f
...
3d92d3179f
| Author | SHA1 | Date | |
|---|---|---|---|
| 3d92d3179f | |||
| 0feaf09d83 | |||
| ceaf6b5fbd | |||
| 760c778c3b | |||
| b1f7fbff8b | |||
| b76523ec75 | |||
| 178850e0bc | |||
| 0a2b0d840b | |||
| 54d537dc9b | |||
| a9a4b39da1 | |||
| 5b62f9461b | |||
| 5530aff2f3 | |||
| c1ee2135df | |||
| a6648b7d1e | |||
| dbfaee27dc |
@ -38,6 +38,7 @@ add_library(${TARGET_APP} STATIC
|
||||
network/apiroutes.h
|
||||
# 3rd party libraries
|
||||
../3rdParty/rapidcsv/src/rapidcsv.h
|
||||
utils/messagehandler.h
|
||||
)
|
||||
|
||||
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
@ -45,6 +45,7 @@ QList<ModelItemValues> FileHandler::getItemValuesFromCSVFile(const QString& file
|
||||
QFile file;
|
||||
file.setFileName(filePath);
|
||||
if (file.exists()) {
|
||||
// TODO inform UI on CSV import errors
|
||||
result = CsvParser::getItemsFromCSVFile(filePath);
|
||||
}
|
||||
return result;
|
||||
|
||||
@ -40,4 +40,48 @@ void SettingsHandler::saveSettings(QVariantMap settingMap, QString group) {
|
||||
settings.sync();
|
||||
}
|
||||
|
||||
void SettingsHandler::deleteSettings(QStringList keys, QString group) {
|
||||
qInfo() << "deleting settings...";
|
||||
|
||||
QSettings settings;
|
||||
if (!group.isEmpty()) {
|
||||
qDebug() << "starting group:" << group;
|
||||
settings.beginGroup(group);
|
||||
}
|
||||
|
||||
foreach (QString key, keys) {
|
||||
qDebug() << "removing:" << key;
|
||||
settings.remove(key);
|
||||
}
|
||||
if (!group.isEmpty()) {
|
||||
settings.endGroup();
|
||||
}
|
||||
|
||||
settings.sync();
|
||||
}
|
||||
|
||||
QVariantMap SettingsHandler::getChangeset(QVariantMap newSettings, QString group) {
|
||||
const QVariantMap oldSettings = getSettings(group);
|
||||
|
||||
QVariantMap result;
|
||||
|
||||
for (QVariantMap::const_iterator iter = newSettings.begin(); iter != newSettings.end(); ++iter) {
|
||||
qDebug() << iter.key() << iter.value();
|
||||
QString key = iter.key();
|
||||
QVariant newValue = iter.value();
|
||||
QVariant oldValue = oldSettings.value(key);
|
||||
|
||||
if (oldValue == newValue) {
|
||||
qInfo() << "oldValue == newValue -> ignoring...";
|
||||
} else {
|
||||
const QString debugString =
|
||||
QString("oldValue != newValue -> adding '%1' to changeset...").arg(key);
|
||||
qInfo() << debugString;
|
||||
result.insert(key, newValue);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
SettingsHandler::SettingsHandler() {}
|
||||
|
||||
@ -7,6 +7,9 @@ class SettingsHandler {
|
||||
public:
|
||||
static QVariantMap getSettings(QString group = "");
|
||||
static void saveSettings(QVariantMap settingMap, QString group = "");
|
||||
static void deleteSettings(QStringList keys, QString group = "");
|
||||
|
||||
static QVariantMap getChangeset(QVariantMap newSettings, QString group = "");
|
||||
|
||||
private:
|
||||
SettingsHandler();
|
||||
|
||||
@ -52,7 +52,11 @@ bool CsvParser::isCsvCompatible(const rapidcsv::Document& doc) {
|
||||
qInfo() << "Checking CSV document for compatiblity...";
|
||||
const std::vector<std::string> columnNames = doc.GetColumnNames();
|
||||
for (const QString& headerName : GET_HEADER_NAMES()) {
|
||||
bool isHeaderNameFound = false;
|
||||
if (OPTIONAL_CSV_HEADERS.contains(headerName)) {
|
||||
/// no need to have a column for the optional values
|
||||
continue;
|
||||
}
|
||||
/// these column must be found in CSV document
|
||||
if (std::find(columnNames.begin(), columnNames.end(), headerName) != columnNames.end()) {
|
||||
qDebug() << QString("Header found in column names: %1").arg(headerName);
|
||||
} else {
|
||||
@ -86,14 +90,13 @@ QHash<QString, std::vector<std::string>> CsvParser::extractColumnValues(
|
||||
const rapidcsv::Document& doc) {
|
||||
QHash<QString, std::vector<std::string>> columnValueMap;
|
||||
for (const QString& columnName : headerNames) {
|
||||
// TODO add support for optional columns
|
||||
// if (optionalCsvHeaderNames.contains(columnName)) {
|
||||
// const std::vector<std::string> columnNames = doc.GetColumnNames();
|
||||
// int columnIdx = doc.GetColumnIdx(columnName.toStdString());
|
||||
// if (columnIdx == -1) {
|
||||
// continue;
|
||||
// }
|
||||
// }
|
||||
if (OPTIONAL_CSV_HEADERS.contains(columnName)) {
|
||||
const std::vector<std::string> columnNames = doc.GetColumnNames();
|
||||
int columnIdx = doc.GetColumnIdx(columnName.toStdString());
|
||||
if (columnIdx == -1) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
const std::vector<std::string> columnValues =
|
||||
doc.GetColumn<std::string>(columnName.toStdString());
|
||||
columnValueMap.insert(columnName, columnValues);
|
||||
|
||||
@ -74,6 +74,30 @@ QJsonObject JsonParser::itemValuesToJsonObject(const ModelItemValues& itemValues
|
||||
return result;
|
||||
}
|
||||
|
||||
QByteArray JsonParser::userCredentialsToJsonDocument(const QString email, const QString password) {
|
||||
QJsonDocument jsonDoc;
|
||||
QJsonObject rootObject;
|
||||
|
||||
QJsonObject userObject;
|
||||
userObject.insert("email", email);
|
||||
userObject.insert("password", password);
|
||||
|
||||
rootObject.insert("user", userObject);
|
||||
jsonDoc.setObject(rootObject);
|
||||
|
||||
return jsonDoc.toJson(QJsonDocument::Compact);
|
||||
}
|
||||
|
||||
QVariant JsonParser::getValueFromJson(const QByteArray& jsonData,
|
||||
const QString key,
|
||||
const QString objectName) {
|
||||
QJsonDocument doc = QJsonDocument::fromJson(jsonData);
|
||||
QJsonObject rootObject = doc.object();
|
||||
QJsonObject userObject = rootObject.value(objectName).toObject();
|
||||
|
||||
return userObject.value(key);
|
||||
}
|
||||
|
||||
JsonParser::JsonParser() {}
|
||||
|
||||
QJsonArray JsonParser::extractItemArray(const QJsonDocument& doc, const QString& objectName) {
|
||||
@ -98,12 +122,16 @@ ModelItemValues JsonParser::jsonObjectToItemValues(const QJsonObject& itemJsonOb
|
||||
values.insert(keyValuePair.first, keyValuePair.second);
|
||||
}
|
||||
|
||||
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
||||
while (i.hasNext()) {
|
||||
const UserRoles role = i.next();
|
||||
std::pair<int, QVariant> keyValuePair = getKeyValuePair(itemJsonObject, role);
|
||||
values.insert(keyValuePair.first, keyValuePair.second);
|
||||
for (auto iter = itemJsonObject.constBegin(), end = itemJsonObject.constEnd(); iter != end;
|
||||
++iter) {
|
||||
const QString roleName = iter.key();
|
||||
if (ROLE_NAMES.values().contains(roleName)) {
|
||||
const int role = ROLE_NAMES.key(roleName.toLatin1());
|
||||
std::pair<int, QVariant> keyValuePair = getKeyValuePair(itemJsonObject, role);
|
||||
values.insert(keyValuePair.first, keyValuePair.second);
|
||||
}
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
|
||||
@ -20,6 +20,11 @@ class JsonParser {
|
||||
const QString& objectName = "");
|
||||
static QJsonObject itemValuesToJsonObject(const ModelItemValues& itemValues);
|
||||
|
||||
static QByteArray userCredentialsToJsonDocument(const QString email, const QString password);
|
||||
static QVariant getValueFromJson(const QByteArray& jsonData,
|
||||
const QString key,
|
||||
const QString objectName = "");
|
||||
|
||||
private:
|
||||
explicit JsonParser();
|
||||
|
||||
|
||||
164
genericcore.cpp
164
genericcore.cpp
@ -14,6 +14,7 @@
|
||||
#include "constants.h"
|
||||
#include "data/filehandler.h"
|
||||
#include "data/settingshandler.h"
|
||||
#include "formats/jsonparser.h"
|
||||
#include "model/generalsortfiltermodel.h"
|
||||
#include "model/metadata.h"
|
||||
#include "model/tablemodel.h"
|
||||
@ -39,7 +40,7 @@ GenericCore::GenericCore() {
|
||||
m_modelUndoStack = new QUndoStack(this);
|
||||
|
||||
setupModels();
|
||||
setupServerConfiguration();
|
||||
setupServerCommunication();
|
||||
}
|
||||
|
||||
GenericCore::~GenericCore() { qDebug() << "Destroying core..."; }
|
||||
@ -98,6 +99,67 @@ std::shared_ptr<GeneralSortFilterModel> GenericCore::getSortFilterModel() const
|
||||
return m_sortFilterModel;
|
||||
}
|
||||
|
||||
void GenericCore::importCSVFile(const QString& filePath) {
|
||||
qInfo() << "importing items from CSV...";
|
||||
qDebug() << "filePath:" << filePath;
|
||||
const QList<ModelItemValues> itemValuesList = FileHandler::getItemValuesFromCSVFile(filePath);
|
||||
if (itemValuesList.isEmpty()) {
|
||||
qDebug() << "No items found. Doing nothing...";
|
||||
displayStatusMessage("No items found in CSV file. Either empty or not compatible.");
|
||||
return;
|
||||
}
|
||||
// qDebug() << "CSV file content:" << itemValuesList;
|
||||
m_mainModel->insertItems(m_mainModel->rowCount(), itemValuesList);
|
||||
const QString messageString =
|
||||
QString(tr("Imported %1 item(s) from CSV file.")).arg(itemValuesList.size());
|
||||
displayStatusMessage(messageString);
|
||||
}
|
||||
|
||||
bool GenericCore::exportCSVFile(const QString& filePath) {
|
||||
qInfo() << "exporting items to CSV...";
|
||||
qDebug() << "filePath:" << filePath;
|
||||
const QList<QStringList> itemsAsStringLists = m_mainModel->getItemsAsStringLists();
|
||||
return FileHandler::exportToCSVFile(itemsAsStringLists, filePath);
|
||||
}
|
||||
|
||||
QVariantMap GenericCore::getSettings(QString group) const {
|
||||
return SettingsHandler::getSettings(group);
|
||||
}
|
||||
|
||||
void GenericCore::applySettings(QVariantMap settingMap, QString group) {
|
||||
const QVariantMap changeset = SettingsHandler::getChangeset(settingMap, group);
|
||||
|
||||
if (changeset.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (group == "Server") {
|
||||
const bool urlChanged = changeset.contains("url");
|
||||
const bool emailChanged = changeset.contains("email");
|
||||
const bool passwordChanged = changeset.contains("password");
|
||||
if (urlChanged || emailChanged || passwordChanged) {
|
||||
if (!changeset.contains("authToken")) {
|
||||
qInfo() << "Account settings changed, but no new token present. Deleting old token...";
|
||||
SettingsHandler::deleteSettings({"authToken"}, "Server");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SettingsHandler::saveSettings(settingMap, group);
|
||||
|
||||
if (group == "Server") {
|
||||
applyServerConfiguration();
|
||||
}
|
||||
}
|
||||
|
||||
bool GenericCore::isSyncServerSetup() const {
|
||||
if (m_serverCommunicator) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save items to default file (in standard location).
|
||||
* @brief GenericCore::saveItems Saves item fo file.
|
||||
@ -115,48 +177,32 @@ void GenericCore::saveItems() {
|
||||
}
|
||||
}
|
||||
|
||||
void GenericCore::importCSVFile(const QString& filePath) {
|
||||
qInfo() << "importing items from CSV...";
|
||||
qDebug() << "filePath:" << filePath;
|
||||
const QList<ModelItemValues> itemValuesList = FileHandler::getItemValuesFromCSVFile(filePath);
|
||||
// TODO inform UI on errors
|
||||
if (itemValuesList.isEmpty()) {
|
||||
qDebug() << "No items found. Doing nothing...";
|
||||
return;
|
||||
}
|
||||
// qDebug() << "CSV file content:" << itemValuesList;
|
||||
m_mainModel->insertItems(m_mainModel->rowCount(), itemValuesList);
|
||||
void GenericCore::onLoginSuccessful(const QByteArray jsonData) {
|
||||
emit displayStatusMessage("Login successful.");
|
||||
qInfo() << "Storing auth token...";
|
||||
QString token = JsonParser::getValueFromJson(jsonData, "token", "user").toString();
|
||||
SettingsHandler::saveSettings({{"authToken", token}}, "Server");
|
||||
applyServerConfiguration();
|
||||
}
|
||||
|
||||
bool GenericCore::exportCSVFile(const QString& filePath) {
|
||||
qInfo() << "exporting items to CSV...";
|
||||
qDebug() << "filePath:" << filePath;
|
||||
const QList<QStringList> itemsAsStringLists = m_mainModel->getItemsAsStringLists();
|
||||
return FileHandler::exportToCSVFile(itemsAsStringLists, filePath);
|
||||
void GenericCore::onLoginFailure(const QString errorString) {
|
||||
emit displayStatusMessage(QString("Error: %1").arg(errorString));
|
||||
}
|
||||
|
||||
QVariantMap GenericCore::getSettings(QString group) const {
|
||||
return SettingsHandler::getSettings(group);
|
||||
}
|
||||
|
||||
void GenericCore::applySettings(QVariantMap settingMap, QString group) {
|
||||
SettingsHandler::saveSettings(settingMap, group);
|
||||
|
||||
if (group == "Server") {
|
||||
setupServerConfiguration();
|
||||
}
|
||||
}
|
||||
|
||||
bool GenericCore::isSyncServerSetup() const {
|
||||
if (m_serverCommunicator) {
|
||||
return true;
|
||||
void GenericCore::onNotAuthorized(const QString /*path*/) {
|
||||
const QVariantMap serverSettings = SettingsHandler::getSettings("Server");
|
||||
const QString tokenValue = serverSettings.value("authToken").toString();
|
||||
if (!tokenValue.isEmpty()) {
|
||||
SettingsHandler::deleteSettings({"authToken"}, "Server");
|
||||
displayStatusMessage("Not authorized! Deleted token. Please retry.");
|
||||
} else {
|
||||
return false;
|
||||
displayStatusMessage("Not authorized! But no token was present. Please check your settings.");
|
||||
}
|
||||
applyServerConfiguration();
|
||||
}
|
||||
|
||||
void GenericCore::onSendItemTriggered(const QByteArray& jsonData) {
|
||||
m_serverCommunicator->postItems(jsonData);
|
||||
m_serverCommunicator->sendItem(jsonData);
|
||||
}
|
||||
|
||||
void GenericCore::onItemsFetched(const QByteArray jsonData) {
|
||||
@ -170,20 +216,20 @@ void GenericCore::onItemsFetchFailure(const QString errorString) {
|
||||
emit displayStatusMessage(QString("Error: %1").arg(errorString));
|
||||
}
|
||||
|
||||
void GenericCore::onPostRequestSuccessful(const QByteArray responseData) {
|
||||
void GenericCore::onSendItemSuccessful(const QByteArray responseData) {
|
||||
const QString message = m_mainModel->updateItemsFromJson(responseData);
|
||||
emit displayStatusMessage(message);
|
||||
}
|
||||
|
||||
void GenericCore::onPostRequestFailure(const QString errorString) {
|
||||
void GenericCore::onSendItemFailure(const QString errorString) {
|
||||
emit displayStatusMessage(QString("Error: %1").arg(errorString));
|
||||
}
|
||||
|
||||
void GenericCore::onDeleteRequestSuccessful(const QByteArray responseData) {
|
||||
void GenericCore::onDeleteItemSuccessful(const QByteArray responseData) {
|
||||
qWarning() << "TODO: Process success response!!!";
|
||||
}
|
||||
|
||||
void GenericCore::onDeleteRequestFailure(const QString errorString) {
|
||||
void GenericCore::onDeleteItemFailure(const QString errorString) {
|
||||
qWarning() << "TODO: Process error response!!!";
|
||||
}
|
||||
|
||||
@ -244,28 +290,37 @@ QString GenericCore::getMaintenanceToolFilePath() const {
|
||||
return filePath;
|
||||
}
|
||||
|
||||
void GenericCore::setupServerConfiguration() {
|
||||
void GenericCore::setupServerCommunication() {
|
||||
m_serverCommunicator = make_unique<ServerCommunicator>(this);
|
||||
/// request connections
|
||||
connect(this, &GenericCore::fetchItemsFromServer, m_serverCommunicator.get(),
|
||||
&ServerCommunicator::fetchItems);
|
||||
connect(this, &GenericCore::postItemToServer, this, &GenericCore::onSendItemTriggered);
|
||||
connect(this, &GenericCore::sendItemToServer, m_serverCommunicator.get(),
|
||||
&ServerCommunicator::sendItem);
|
||||
connect(this, &GenericCore::deleteItemFromServer, m_serverCommunicator.get(),
|
||||
&ServerCommunicator::deleteItem);
|
||||
|
||||
/// response connections
|
||||
connect(m_serverCommunicator.get(), &ServerCommunicator::loginSuccessful, this,
|
||||
&GenericCore::onLoginSuccessful);
|
||||
connect(m_serverCommunicator.get(), &ServerCommunicator::loginFailure, this,
|
||||
&GenericCore::onLoginFailure);
|
||||
|
||||
connect(m_serverCommunicator.get(), &ServerCommunicator::notAuthorized, this,
|
||||
&GenericCore::onNotAuthorized);
|
||||
|
||||
connect(m_serverCommunicator.get(), &ServerCommunicator::itemsFetched, this,
|
||||
&GenericCore::onItemsFetched);
|
||||
connect(m_serverCommunicator.get(), &ServerCommunicator::itemsFetchFailure, this,
|
||||
&GenericCore::onItemsFetchFailure);
|
||||
connect(m_serverCommunicator.get(), &ServerCommunicator::postRequestSuccessful, this,
|
||||
&GenericCore::onPostRequestSuccessful);
|
||||
connect(m_serverCommunicator.get(), &ServerCommunicator::postRequestFailure, this,
|
||||
&GenericCore::onPostRequestFailure);
|
||||
connect(m_serverCommunicator.get(), &ServerCommunicator::deleteRequestSuccessful, this,
|
||||
&GenericCore::onDeleteRequestSuccessful);
|
||||
connect(m_serverCommunicator.get(), &ServerCommunicator::deleteRequestFailure, this,
|
||||
&GenericCore::onDeleteRequestFailure);
|
||||
connect(m_serverCommunicator.get(), &ServerCommunicator::sendItemSuccessful, this,
|
||||
&GenericCore::onSendItemSuccessful);
|
||||
connect(m_serverCommunicator.get(), &ServerCommunicator::sendItemFailure, this,
|
||||
&GenericCore::onSendItemFailure);
|
||||
connect(m_serverCommunicator.get(), &ServerCommunicator::deleteItemSuccessful, this,
|
||||
&GenericCore::onDeleteItemSuccessful);
|
||||
connect(m_serverCommunicator.get(), &ServerCommunicator::deleteItemFailure, this,
|
||||
&GenericCore::onDeleteItemFailure);
|
||||
|
||||
applyServerConfiguration();
|
||||
}
|
||||
@ -273,9 +328,14 @@ void GenericCore::setupServerConfiguration() {
|
||||
void GenericCore::applyServerConfiguration() {
|
||||
const QVariantMap serverSettings = SettingsHandler::getSettings("Server");
|
||||
const QString urlValue = serverSettings.value("url").toString();
|
||||
if (!urlValue.isEmpty()) {
|
||||
const QString emailValue = serverSettings.value("email").toString();
|
||||
const QString passwordValue = serverSettings.value("password").toString();
|
||||
m_serverCommunicator->setServerConfiguration(urlValue, emailValue, passwordValue);
|
||||
if (urlValue.isEmpty()) {
|
||||
SettingsHandler::deleteSettings({"authToken"}, "Server");
|
||||
} else {
|
||||
/// urlValue in NOT empty
|
||||
const QString emailValue = serverSettings.value("email").toString();
|
||||
const QString passwordValue = serverSettings.value("password").toString();
|
||||
const QString authTokenValue = serverSettings.value("authToken").toString();
|
||||
m_serverCommunicator->setServerConfiguration(urlValue, emailValue, passwordValue,
|
||||
authTokenValue);
|
||||
}
|
||||
}
|
||||
|
||||
@ -29,7 +29,6 @@ class GenericCore : public QObject {
|
||||
std::shared_ptr<TableModel> getModel() const;
|
||||
std::shared_ptr<GeneralSortFilterModel> getSortFilterModel() const;
|
||||
|
||||
void saveItems();
|
||||
void importCSVFile(const QString& filePath);
|
||||
bool exportCSVFile(const QString& filePath);
|
||||
|
||||
@ -38,18 +37,25 @@ class GenericCore : public QObject {
|
||||
bool isSyncServerSetup() const;
|
||||
|
||||
public slots:
|
||||
void saveItems();
|
||||
|
||||
void onLoginSuccessful(const QByteArray jsonData);
|
||||
void onLoginFailure(const QString errorString);
|
||||
|
||||
void onNotAuthorized(const QString /*path*/);
|
||||
|
||||
void onSendItemTriggered(const QByteArray& jsonData);
|
||||
void onItemsFetched(const QByteArray jsonData);
|
||||
void onItemsFetchFailure(const QString errorString);
|
||||
void onPostRequestSuccessful(const QByteArray responseData);
|
||||
void onPostRequestFailure(const QString errorString);
|
||||
void onDeleteRequestSuccessful(const QByteArray responseData);
|
||||
void onDeleteRequestFailure(const QString errorString);
|
||||
void onSendItemSuccessful(const QByteArray responseData);
|
||||
void onSendItemFailure(const QString errorString);
|
||||
void onDeleteItemSuccessful(const QByteArray responseData);
|
||||
void onDeleteItemFailure(const QString errorString);
|
||||
|
||||
signals:
|
||||
void displayStatusMessage(QString message);
|
||||
void fetchItemsFromServer();
|
||||
void postItemToServer(const QByteArray& jsonData);
|
||||
void sendItemToServer(const QByteArray& jsonData);
|
||||
void deleteItemFromServer(const QString& id);
|
||||
|
||||
private:
|
||||
@ -66,7 +72,7 @@ class GenericCore : public QObject {
|
||||
|
||||
/// Network communication
|
||||
std::unique_ptr<ServerCommunicator> m_serverCommunicator;
|
||||
void setupServerConfiguration();
|
||||
void setupServerCommunication();
|
||||
void applyServerConfiguration();
|
||||
};
|
||||
|
||||
|
||||
@ -27,6 +27,7 @@ QString GeneralSortFilterModel::getUuid(const QModelIndex& itemIndex) const {
|
||||
return data(itemIndex, IdRole).toString();
|
||||
}
|
||||
|
||||
// NEXT remove when deprecated
|
||||
QByteArray GeneralSortFilterModel::jsonDataForServer(const QModelIndex& proxyIndex) {
|
||||
const QModelIndex sourceIndex = mapToSource(proxyIndex);
|
||||
return m_tableModel->jsonDataForServer(sourceIndex);
|
||||
|
||||
@ -25,21 +25,28 @@ enum UserRoles {
|
||||
|
||||
static UserRoles DEFAULT_ROLE = NameRole;
|
||||
// TODO ?rename USER_FACING_ROLES -> MAIN_ROLES ?
|
||||
static QList<UserRoles> USER_FACING_ROLES = {NameRole, DescriptionRole, InfoRole,
|
||||
TypeRole, AmountRole, FactorRole};
|
||||
static QHash<int, QByteArray> ROLE_NAMES = {
|
||||
{NameRole, "name"}, {DescriptionRole, "description"},
|
||||
{InfoRole, "info"}, {TypeRole, "type"},
|
||||
{AmountRole, "amount"}, {FactorRole, "factor"},
|
||||
{ToStringRole, "ToString"}, {IdRole, "id"}};
|
||||
static QList<UserRoles> USER_FACING_ROLES = {NameRole, DescriptionRole, InfoRole, TypeRole,
|
||||
AmountRole, FactorRole, IdRole};
|
||||
|
||||
static QList<UserRoles> READ_ONLY_ROLES = {IdRole};
|
||||
|
||||
static QHash<int, QByteArray> ROLE_NAMES = {
|
||||
{NameRole, "name"}, {DescriptionRole, "description"},
|
||||
{InfoRole, "info"}, {TypeRole, "type"},
|
||||
{AmountRole, "amount"}, {FactorRole, "factor"},
|
||||
{ToStringRole, "ToString"}, {IdRole, "id"},
|
||||
{Qt::DisplayRole, "display"}, {Qt::EditRole, "edit"}};
|
||||
|
||||
static QList<UserRoles> STRING_ROLES = {NameRole, DescriptionRole, InfoRole, IdRole};
|
||||
static QList<UserRoles> INT_ROLES = {AmountRole};
|
||||
static QList<UserRoles> DOUBLE_ROLES = {FactorRole};
|
||||
static QList<UserRoles> NUMBER_ROLES = INT_ROLES + DOUBLE_ROLES;
|
||||
|
||||
static const QList<UserRoles> TYPE_ROLES = {TypeRole};
|
||||
static const QList<QString> TYPES = {"A", "B", "C", ""};
|
||||
|
||||
static const QStringList OPTIONAL_CSV_HEADERS = {"description", "info"};
|
||||
|
||||
/// JSON keys
|
||||
static const QString ITEMS_KEY_STRING = "items";
|
||||
static const QString ITEM_KEY_STRING = "item";
|
||||
@ -68,6 +75,9 @@ static UserRoles GET_ROLE_FOR_COLUMN(const int column) {
|
||||
case 5:
|
||||
return FactorRole;
|
||||
break;
|
||||
case 6:
|
||||
return IdRole;
|
||||
break;
|
||||
default:
|
||||
qWarning() << QString("No role found for column %1! Returning 'NameRole'...").arg(column);
|
||||
return NameRole;
|
||||
|
||||
@ -40,10 +40,19 @@ TableModel::TableModel(QUndoStack* undoStack, QObject* parent)
|
||||
, m_undoStack(undoStack) {}
|
||||
|
||||
Qt::ItemFlags TableModel::flags(const QModelIndex& index) const {
|
||||
Qt::ItemFlags result = QAbstractTableModel::flags(index);
|
||||
if (!index.isValid()) {
|
||||
return QAbstractTableModel::flags(index);
|
||||
return result;
|
||||
}
|
||||
return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
|
||||
|
||||
const int column = index.column();
|
||||
const int roleForColumn = GET_ROLE_FOR_COLUMN(column);
|
||||
/// roles which aren't editable by the user
|
||||
if (READ_ONLY_ROLES.contains(roleForColumn)) {
|
||||
return result;
|
||||
}
|
||||
|
||||
return result | Qt::ItemIsEditable;
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> TableModel::roleNames() const { return ROLE_NAMES; }
|
||||
@ -104,10 +113,9 @@ QVariant TableModel::headerData(int section, Qt::Orientation orientation, int ro
|
||||
}
|
||||
|
||||
bool TableModel::setData(const QModelIndex& index, const QVariant& value, int role) {
|
||||
if (role == Qt::EditRole && checkIndex(index)) {
|
||||
const int column = index.column();
|
||||
const int roleForColumn = GET_ROLE_FOR_COLUMN(column);
|
||||
return setItemData(index, {{roleForColumn, value}});
|
||||
if (checkIndex(index)) {
|
||||
const int adjustedRole = getAppropriateRoleForIndex(index, role);
|
||||
return setItemData(index, {{adjustedRole, value}});
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -303,13 +311,6 @@ QMap<int, QVariant> TableModel::onlyChangedValues(const QModelIndex& index,
|
||||
const QVariant oldValue = index.data(role);
|
||||
// TODO check if role is a editable role?
|
||||
if (oldValue != newValue) {
|
||||
bool emptyValueIsEqualToZero = isEmptyValueEqualToZero(role);
|
||||
// REFACTOR the next if statement is too complex
|
||||
if (emptyValueIsEqualToZero && oldValue == QVariant() && newValue == 0) {
|
||||
qDebug() << "oldValue:" << oldValue << "& newValue:" << newValue
|
||||
<< "mean the same. Ignoring...";
|
||||
continue;
|
||||
}
|
||||
qDebug() << "oldValue:" << oldValue << "!= newValue:" << newValue;
|
||||
result.insert(role, newValue);
|
||||
} else {
|
||||
@ -329,9 +330,26 @@ bool TableModel::isEmptyValueEqualToZero(const int role) const {
|
||||
}
|
||||
}
|
||||
|
||||
int TableModel::getAppropriateRoleForIndex(const QModelIndex& index, const int role) const {
|
||||
/// cases:
|
||||
/// 1. Qt::DisplayRole, Qt::EditRole
|
||||
/// -> get role for column
|
||||
/// 2. other roles
|
||||
/// -> use role as given
|
||||
const int column = index.column();
|
||||
const int roleForColumn = GET_ROLE_FOR_COLUMN(column);
|
||||
switch (role) {
|
||||
case Qt::DisplayRole:
|
||||
case Qt::EditRole:
|
||||
return roleForColumn;
|
||||
break;
|
||||
default:
|
||||
return role;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
QModelIndex TableModel::searchItemIndex(const ModelItemValues givenItemValues) const {
|
||||
// iterate over indexes to search item : see searchItem(...);
|
||||
// for (const shared_ptr<ModelItem>& item : m_items) {
|
||||
for (int row = 0; row < rowCount(); ++row) {
|
||||
qDebug() << "Processing item at row" << row << "...";
|
||||
QModelIndex itemIndex = index(row, 0);
|
||||
|
||||
@ -71,6 +71,7 @@ class TableModel : public QAbstractTableModel {
|
||||
QMap<int, QVariant> onlyChangedValues(const QModelIndex& index,
|
||||
const QMap<int, QVariant>& roleValueMap) const;
|
||||
bool isEmptyValueEqualToZero(const int role) const;
|
||||
int getAppropriateRoleForIndex(const QModelIndex& index, const int role) const;
|
||||
QModelIndex searchItemIndex(const ModelItemValues givenItemValues) const;
|
||||
bool isItemEqualToItemValues(const QModelIndex& itemIndex,
|
||||
const ModelItemValues givenItemValues) const;
|
||||
|
||||
@ -7,6 +7,8 @@
|
||||
|
||||
static const QString apiPrefix = "/api/";
|
||||
|
||||
static const QString ROUTE_LOG_IN = apiPrefix + "log_in";
|
||||
|
||||
static const QString ROUTE_ITEMS = apiPrefix + "items";
|
||||
|
||||
#endif // APIROUTES_H
|
||||
|
||||
@ -6,6 +6,8 @@
|
||||
#include <QJsonObject>
|
||||
#include <QRestReply>
|
||||
|
||||
#include "../formats/jsonparser.h"
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
|
||||
ServerCommunicator::ServerCommunicator(QObject* parent)
|
||||
@ -36,71 +38,186 @@ void ServerCommunicator::setUrl(const QUrl& url) {
|
||||
emit urlChanged();
|
||||
}
|
||||
|
||||
void ServerCommunicator::fetchItems() {
|
||||
/// Set up a GET request
|
||||
m_restManager->get(m_serviceApi->createRequest(ROUTE_ITEMS), this, [this](QRestReply& reply) {
|
||||
if (reply.isSuccess()) {
|
||||
qInfo() << "Fetching items successful.";
|
||||
const QJsonDocument doc = reply.readJson().value();
|
||||
emit itemsFetched(doc.toJson());
|
||||
void ServerCommunicator::setServerConfiguration(const QString url,
|
||||
const QString email,
|
||||
const QString password,
|
||||
const QString authToken) {
|
||||
setUrl(url);
|
||||
|
||||
m_email = email;
|
||||
m_password = password;
|
||||
m_authToken = authToken;
|
||||
|
||||
if (authToken.isEmpty()) {
|
||||
if (validLoginCredentials()) {
|
||||
const QByteArray userCredentials =
|
||||
JsonParser::userCredentialsToJsonDocument(m_email, m_password);
|
||||
sendPostRequest(ROUTE_LOG_IN, userCredentials);
|
||||
}
|
||||
} else {
|
||||
/// authToken not empty:
|
||||
m_serviceApi->setBearerToken(authToken.toLatin1());
|
||||
}
|
||||
}
|
||||
|
||||
void ServerCommunicator::fetchItems() { sendGetRequest(ROUTE_ITEMS); }
|
||||
|
||||
void ServerCommunicator::sendItem(const QByteArray& jsonData) {
|
||||
sendPostRequest(ROUTE_ITEMS, jsonData);
|
||||
}
|
||||
|
||||
void ServerCommunicator::deleteItem(const QString& id) {
|
||||
const QString path = QString("%1/%2").arg(ROUTE_ITEMS, id);
|
||||
sendDeleteRequest(path);
|
||||
}
|
||||
|
||||
bool ServerCommunicator::validLoginCredentials() {
|
||||
if (url().isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if (m_email.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if (m_password.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void ServerCommunicator::sendGetRequest(const QString& path) {
|
||||
// TODO check for valid path, instead of emptiness
|
||||
if (path.isEmpty()) {
|
||||
qDebug() << "Empty path -> Not sending a request.";
|
||||
return;
|
||||
}
|
||||
|
||||
const QNetworkRequest request = m_serviceApi->createRequest(path);
|
||||
m_restManager->get(request, this, [this, path](QRestReply& reply) {
|
||||
if (reply.isSuccess()) {
|
||||
qInfo() << "Request successful.";
|
||||
const QJsonDocument doc = reply.readJson().value();
|
||||
onGetReplySuccessful(path, doc);
|
||||
} else {
|
||||
if (reply.hasError()) {
|
||||
const QString errorString = reply.errorString();
|
||||
qCritical() << "ERROR:" << errorString;
|
||||
emit itemsFetchFailure(errorString);
|
||||
qWarning() << "Network error:" << errorString;
|
||||
onGetReplyFailure(path, errorString);
|
||||
} else {
|
||||
int statusCode = reply.httpStatus();
|
||||
qCritical() << "ERROR:" << statusCode;
|
||||
emit itemsFetchFailure(QString::number(statusCode));
|
||||
emit itemsFetchFailure(QString("HTTP status code: %1").arg(statusCode));
|
||||
qWarning() << "Request not successful:" << statusCode;
|
||||
if (statusCode == 401) {
|
||||
notAuthorized(path);
|
||||
} else {
|
||||
onGetReplyFailure(path, QString("HTTP status code: %1").arg(statusCode));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void ServerCommunicator::postItems(const QByteArray& jsonData) {
|
||||
QNetworkReply* reply = m_restManager->post(m_serviceApi->createRequest(ROUTE_ITEMS), jsonData);
|
||||
void ServerCommunicator::onGetReplySuccessful(const QString& path, const QJsonDocument doc) {
|
||||
if (path == ROUTE_ITEMS) {
|
||||
emit itemsFetched(doc.toJson());
|
||||
} else {
|
||||
qWarning() << "Can't match request path:" << path;
|
||||
}
|
||||
}
|
||||
|
||||
QObject::connect(reply, &QNetworkReply::finished, [=]() {
|
||||
if (reply->error() == QNetworkReply::NoError) {
|
||||
QByteArray responseData = reply->readAll();
|
||||
const QString message = QString("POST successful! Response: %1").arg(responseData);
|
||||
qInfo() << message;
|
||||
emit postRequestSuccessful(responseData);
|
||||
void ServerCommunicator::onGetReplyFailure(const QString& path, const QString errorString) {
|
||||
if (path == ROUTE_ITEMS) {
|
||||
emit itemsFetchFailure(errorString);
|
||||
} else {
|
||||
qWarning() << "Can't match request path:" << path;
|
||||
}
|
||||
}
|
||||
|
||||
void ServerCommunicator::sendPostRequest(const QString& path, const QByteArray data) {
|
||||
const QNetworkRequest request = m_serviceApi->createRequest(path);
|
||||
|
||||
m_restManager->post(request, data, this, [this, path](QRestReply& reply) {
|
||||
if (reply.isSuccess()) {
|
||||
int statusCode = reply.httpStatus();
|
||||
qInfo() << "Request successful. Status code:" << statusCode;
|
||||
const QJsonDocument doc = reply.readJson().value();
|
||||
onPostReplySuccessful(path, doc);
|
||||
} else {
|
||||
const QString message = QString("Error: %1").arg(reply->errorString());
|
||||
qDebug() << message;
|
||||
emit postRequestFailure(message);
|
||||
if (reply.hasError()) {
|
||||
const QString errorString = reply.errorString();
|
||||
qWarning() << "Network error:" << errorString;
|
||||
onPostReplyFailure(path, errorString);
|
||||
} else {
|
||||
int statusCode = reply.httpStatus();
|
||||
qWarning() << "Request not successful:" << statusCode;
|
||||
qInfo() << "Content:" << reply.readJson();
|
||||
if (statusCode == 401) {
|
||||
notAuthorized(path);
|
||||
} else {
|
||||
onPostReplyFailure(path, QString("HTTP status code: %1").arg(statusCode));
|
||||
}
|
||||
}
|
||||
}
|
||||
reply->deleteLater();
|
||||
});
|
||||
}
|
||||
|
||||
void ServerCommunicator::deleteItem(const QString& id) {
|
||||
const QString deleteRoute = QString("%1/%2").arg(ROUTE_ITEMS, id);
|
||||
QNetworkReply* reply = m_restManager->deleteResource(m_serviceApi->createRequest(deleteRoute));
|
||||
void ServerCommunicator::onPostReplySuccessful(const QString& path, const QJsonDocument doc) {
|
||||
if (path == ROUTE_ITEMS) {
|
||||
emit sendItemSuccessful(doc.toJson());
|
||||
} else if (path == ROUTE_LOG_IN) {
|
||||
qCritical() << "Login success:" << doc.toJson(QJsonDocument::Compact);
|
||||
emit loginSuccessful(doc.toJson(QJsonDocument::Compact));
|
||||
} else {
|
||||
qWarning() << "Can't match request path:" << path;
|
||||
}
|
||||
}
|
||||
|
||||
QObject::connect(reply, &QNetworkReply::finished, [=]() {
|
||||
if (reply->error() == QNetworkReply::NoError) {
|
||||
QByteArray responseData = reply->readAll();
|
||||
const QString message = QString("DELETE successful! Response: %1").arg(responseData);
|
||||
qInfo() << message;
|
||||
emit deleteRequestSuccessful(responseData);
|
||||
void ServerCommunicator::onPostReplyFailure(const QString& path, const QString errorString) {
|
||||
if (path == ROUTE_ITEMS) {
|
||||
emit sendItemFailure(errorString);
|
||||
} else if (path == ROUTE_LOG_IN) {
|
||||
qCritical() << "Login failure:" << errorString;
|
||||
emit loginFailure(errorString);
|
||||
} else {
|
||||
qWarning() << "Can't match request path:" << path;
|
||||
}
|
||||
}
|
||||
|
||||
void ServerCommunicator::sendDeleteRequest(const QString& path) {
|
||||
const QNetworkRequest request = m_serviceApi->createRequest(path);
|
||||
m_restManager->deleteResource(request, this, [this, path](QRestReply& reply) {
|
||||
if (reply.isSuccess()) {
|
||||
qInfo() << "Request successful.";
|
||||
const QJsonDocument doc = reply.readJson().value();
|
||||
onDeleteReplySuccessful(path, doc);
|
||||
} else {
|
||||
const QString message = QString("Error: %1").arg(reply->errorString());
|
||||
qDebug() << message;
|
||||
emit deleteRequestFailure(message);
|
||||
if (reply.hasError()) {
|
||||
const QString errorString = reply.errorString();
|
||||
qWarning() << "Network error:" << errorString;
|
||||
onDeleteReplyFailure(path, errorString);
|
||||
} else {
|
||||
int statusCode = reply.httpStatus();
|
||||
qWarning() << "Request not successful:" << statusCode;
|
||||
if (statusCode == 401) {
|
||||
notAuthorized(path);
|
||||
} else {
|
||||
onDeleteReplyFailure(path, QString("HTTP status code: %1").arg(statusCode));
|
||||
}
|
||||
}
|
||||
}
|
||||
reply->deleteLater();
|
||||
});
|
||||
}
|
||||
|
||||
void ServerCommunicator::setServerConfiguration(const QString url,
|
||||
const QString email,
|
||||
const QString password) {
|
||||
setUrl(url);
|
||||
|
||||
m_email = email;
|
||||
m_password = password;
|
||||
void ServerCommunicator::onDeleteReplySuccessful(const QString& path, const QJsonDocument doc) {
|
||||
if (path.startsWith(ROUTE_ITEMS)) {
|
||||
emit deleteItemSuccessful(doc.toJson());
|
||||
} else {
|
||||
qWarning() << "Can't match request path:" << path;
|
||||
}
|
||||
}
|
||||
|
||||
void ServerCommunicator::onDeleteReplyFailure(const QString& path, const QString errorString) {
|
||||
if (path.startsWith(ROUTE_ITEMS)) {
|
||||
emit deleteItemFailure(errorString);
|
||||
} else {
|
||||
qWarning() << "Can't match request path:" << path;
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,22 +16,33 @@ class ServerCommunicator : public QObject {
|
||||
QUrl url() const;
|
||||
void setUrl(const QUrl& url);
|
||||
|
||||
void setServerConfiguration(const QString url, const QString email, const QString password);
|
||||
void setServerConfiguration(const QString url,
|
||||
const QString email,
|
||||
const QString password,
|
||||
const QString authToken);
|
||||
|
||||
public slots:
|
||||
void fetchItems();
|
||||
void postItems(const QByteArray& jsonData);
|
||||
void sendItem(const QByteArray& jsonData);
|
||||
void deleteItem(const QString& id);
|
||||
// NEXT editItem(const QByteArray& jsonData)
|
||||
|
||||
signals:
|
||||
void urlChanged();
|
||||
|
||||
void loginSuccessful(const QByteArray responseData);
|
||||
void loginFailure(const QString errorString);
|
||||
|
||||
void notAuthorized(const QString path);
|
||||
|
||||
void itemsFetched(const QByteArray jsonDoc);
|
||||
void itemsFetchFailure(const QString errorString);
|
||||
void postRequestSuccessful(const QByteArray responseData);
|
||||
void postRequestFailure(const QString errorString);
|
||||
void deleteRequestSuccessful(const QByteArray responseData);
|
||||
void deleteRequestFailure(const QString errorString);
|
||||
|
||||
void sendItemSuccessful(const QByteArray responseData);
|
||||
void sendItemFailure(const QString errorString);
|
||||
|
||||
void deleteItemSuccessful(const QByteArray responseData);
|
||||
void deleteItemFailure(const QString errorString);
|
||||
|
||||
private:
|
||||
QNetworkAccessManager m_netManager;
|
||||
@ -40,7 +51,21 @@ class ServerCommunicator : public QObject {
|
||||
|
||||
QString m_email;
|
||||
QString m_password;
|
||||
// QString m_authToken;
|
||||
QString m_authToken;
|
||||
|
||||
bool validLoginCredentials();
|
||||
|
||||
void sendGetRequest(const QString& path);
|
||||
void onGetReplySuccessful(const QString& path, const QJsonDocument doc);
|
||||
void onGetReplyFailure(const QString& path, const QString errorString);
|
||||
|
||||
void sendPostRequest(const QString& path, const QByteArray data);
|
||||
void onPostReplySuccessful(const QString& path, const QJsonDocument doc);
|
||||
void onPostReplyFailure(const QString& path, const QString errorString);
|
||||
|
||||
void sendDeleteRequest(const QString& path);
|
||||
void onDeleteReplySuccessful(const QString& path, const QJsonDocument doc);
|
||||
void onDeleteReplyFailure(const QString& path, const QString errorString);
|
||||
};
|
||||
|
||||
#endif // SERVERCOMMUNICATOR_H
|
||||
|
||||
164
utils/messagehandler.h
Normal file
164
utils/messagehandler.h
Normal file
@ -0,0 +1,164 @@
|
||||
#ifndef MESSAGEHANDLER_H
|
||||
#define MESSAGEHANDLER_H
|
||||
/**
|
||||
* Color and formatting codes
|
||||
* @see: http://misc.flogisoft.com/bash/tip_colors_and_formatting
|
||||
*/
|
||||
|
||||
#include <QObject>
|
||||
|
||||
// qSetMessagePattern("%{file}(%{line}): %{message}");
|
||||
// qSetMessagePattern("%{type}(%{line}):\t%{message}");
|
||||
// qSetMessagePattern("%{type}%{file}(%{line}):\t%{message}");
|
||||
|
||||
void consoleHandlerColoredVerbose(QtMsgType type,
|
||||
const QMessageLogContext& context,
|
||||
const QString& msg) {
|
||||
QByteArray localMsg = msg.toLocal8Bit();
|
||||
switch (type) {
|
||||
case QtDebugMsg:
|
||||
// fprintf(stderr, "\033[1;30mDebug: (%s:%u, %s) \t%s\n\033[0m", context.file,
|
||||
// context.line, context.function, localMsg.constData()); // bold
|
||||
fprintf(stderr, "\033[107;30mDebug: (%s:%u, %s) \t%s\n\033[0m", context.file, context.line,
|
||||
context.function, localMsg.constData());
|
||||
break;
|
||||
case QtInfoMsg:
|
||||
fprintf(stderr, "\033[107;32mInfo: (%s:%u) \t%s\n\033[0m", context.file, context.line,
|
||||
localMsg.constData());
|
||||
break;
|
||||
case QtWarningMsg:
|
||||
fprintf(stderr, "\033[43;30mWarning: (%s:%u, %s) \t%s\n\033[0m", context.file, context.line,
|
||||
context.function, localMsg.constData());
|
||||
break;
|
||||
case QtCriticalMsg:
|
||||
fprintf(stderr, "\033[41;30mCritical: (%s:%u, %s) \t%s\n\033[0m", context.file, context.line,
|
||||
context.function, localMsg.constData());
|
||||
break;
|
||||
case QtFatalMsg:
|
||||
fprintf(stderr, "\033[41;30mFatal: (%s:%u, %s) \t%s\n\033[0m", context.file, context.line,
|
||||
context.function, localMsg.constData());
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
void consoleHandlerColoredVerboseInDarkTheme(QtMsgType type,
|
||||
const QMessageLogContext& context,
|
||||
const QString& msg) {
|
||||
QByteArray localMsg = msg.toLocal8Bit();
|
||||
switch (type) {
|
||||
case QtDebugMsg:
|
||||
// fprintf(stderr, "\033[1;30mDebug: (%s:%u, %s) \t%s\n\033[0m", context.file,
|
||||
// context.line, context.function, localMsg.constData()); // bold
|
||||
fprintf(stderr, "\033[107;37mDebug: (%s:%u, %s) \t%s\n\033[0m", context.file, context.line,
|
||||
context.function, localMsg.constData());
|
||||
break;
|
||||
case QtInfoMsg:
|
||||
fprintf(stderr, "\033[107;32mInfo: (%s:%u) \t%s\n\033[0m", context.file, context.line,
|
||||
localMsg.constData());
|
||||
break;
|
||||
case QtWarningMsg:
|
||||
fprintf(stderr, "\033[43;30mWarning: (%s:%u, %s) \t%s\n\033[0m", context.file, context.line,
|
||||
context.function, localMsg.constData());
|
||||
break;
|
||||
case QtCriticalMsg:
|
||||
fprintf(stderr, "\033[41;30mCritical: (%s:%u, %s) \t%s\n\033[0m", context.file, context.line,
|
||||
context.function, localMsg.constData());
|
||||
break;
|
||||
case QtFatalMsg:
|
||||
fprintf(stderr, "\033[41;30mFatal: (%s:%u, %s) \t%s\n\033[0m", context.file, context.line,
|
||||
context.function, localMsg.constData());
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
void consoleHandlerColored(QtMsgType type, const QMessageLogContext& context, const QString& msg) {
|
||||
QByteArray localMsg = msg.toLocal8Bit();
|
||||
switch (type) {
|
||||
case QtDebugMsg:
|
||||
fprintf(stderr, "\033[1;30mDebug: (%s:%u) \t%s\n\033[0m", context.file, context.line,
|
||||
localMsg.constData());
|
||||
break;
|
||||
case QtInfoMsg:
|
||||
fprintf(stderr, "\033[0;30mInfo: (%s:%u) \t%s\n\033[0m", context.file, context.line,
|
||||
localMsg.constData());
|
||||
break;
|
||||
case QtWarningMsg:
|
||||
fprintf(stderr, "\033[1;33mWarning: (%s:%u) \t%s\n\033[0m", context.file, context.line,
|
||||
localMsg.constData());
|
||||
break;
|
||||
case QtCriticalMsg:
|
||||
fprintf(stderr, "\033[31mCritical: (%s:%u) \t%s\n\033[0m", context.file, context.line,
|
||||
localMsg.constData());
|
||||
break;
|
||||
case QtFatalMsg:
|
||||
fprintf(stderr, "\033[31mFatal: (%s:%u) \t%s\n\033[0m", context.file, context.line,
|
||||
localMsg.constData());
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
void myMessageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg) {
|
||||
QByteArray localMsg = msg.toLocal8Bit();
|
||||
switch (type) {
|
||||
case QtDebugMsg:
|
||||
fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line,
|
||||
context.function);
|
||||
break;
|
||||
case QtInfoMsg:
|
||||
fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line,
|
||||
context.function);
|
||||
break;
|
||||
case QtWarningMsg:
|
||||
fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line,
|
||||
context.function);
|
||||
break;
|
||||
case QtCriticalMsg:
|
||||
fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file,
|
||||
context.line, context.function);
|
||||
break;
|
||||
case QtFatalMsg:
|
||||
fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line,
|
||||
context.function);
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef Q_OS_ANDROID
|
||||
#include <android/log.h>
|
||||
|
||||
const char* const applicationName = "Pensieve";
|
||||
void androidMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg) {
|
||||
QString report = msg;
|
||||
if (context.file && !QString(context.file).isEmpty()) {
|
||||
report += " in file ";
|
||||
report += QString(context.file);
|
||||
report += " line ";
|
||||
report += QString::number(context.line);
|
||||
}
|
||||
if (context.function && !QString(context.function).isEmpty()) {
|
||||
report += +" function ";
|
||||
report += QString(context.function);
|
||||
}
|
||||
const char* const local = report.toLocal8Bit().constData();
|
||||
switch (type) {
|
||||
case QtDebugMsg:
|
||||
__android_log_write(ANDROID_LOG_DEBUG, applicationName, local);
|
||||
break;
|
||||
case QtInfoMsg:
|
||||
__android_log_write(ANDROID_LOG_INFO, applicationName, local);
|
||||
break;
|
||||
case QtWarningMsg:
|
||||
__android_log_write(ANDROID_LOG_WARN, applicationName, local);
|
||||
break;
|
||||
case QtCriticalMsg:
|
||||
__android_log_write(ANDROID_LOG_ERROR, applicationName, local);
|
||||
break;
|
||||
case QtFatalMsg:
|
||||
default:
|
||||
__android_log_write(ANDROID_LOG_FATAL, applicationName, local);
|
||||
abort();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // MESSAGEHANDLER_H
|
||||
Reference in New Issue
Block a user