Initial commit based on GenericQtClient v0.3.0
This commit is contained in:
89
libs/GenericCore/data/filehandler.cpp
Normal file
89
libs/GenericCore/data/filehandler.cpp
Normal file
@ -0,0 +1,89 @@
|
||||
#include "filehandler.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QJsonDocument>
|
||||
#include <QStandardPaths>
|
||||
|
||||
#include "../formats/csvparser.h"
|
||||
|
||||
bool FileHandler::saveToFile(const QJsonDocument& doc, const QString& fileName) {
|
||||
qDebug() << "saving file...";
|
||||
QString path = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).at(0);
|
||||
qDebug() << path;
|
||||
|
||||
QDir dir;
|
||||
if (!dir.exists(path)) {
|
||||
dir.mkpath(path);
|
||||
}
|
||||
// qDebug() << path + fileName;
|
||||
const QString filePath = path + '/' + fileName;
|
||||
QFile file(filePath);
|
||||
|
||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||
qWarning() << "can't open file";
|
||||
return false;
|
||||
}
|
||||
QTextStream out(&file);
|
||||
out.setEncoding(QStringConverter::Utf8);
|
||||
out << doc.toJson(QJsonDocument::Indented);
|
||||
return true;
|
||||
}
|
||||
|
||||
QByteArray FileHandler::loadJSONDataFromFile(const QString fileName) {
|
||||
QFile file;
|
||||
QString path = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).at(0);
|
||||
file.setFileName(path + "/" + fileName);
|
||||
|
||||
QPair<QString, QByteArray> fileContent = getFileContent(path + "/" + fileName);
|
||||
|
||||
return fileContent.second;
|
||||
}
|
||||
|
||||
QList<ModelItemValues> FileHandler::getItemValuesFromCSVFile(const QString& filePath) {
|
||||
QList<ModelItemValues> result;
|
||||
QFile file;
|
||||
file.setFileName(filePath);
|
||||
if (file.exists()) {
|
||||
result = CsvParser::getItemsFromCSVFile(filePath);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool FileHandler::exportToCSVFile(const QList<QStringList>& rows, const QString& filePath) {
|
||||
return CsvParser::exportToCSVFile(rows, filePath);
|
||||
}
|
||||
|
||||
FileHandler::FileHandler() {}
|
||||
|
||||
/** Tries to open the file specified by the file path & returns the content
|
||||
* @brief FileHandler::getFileContent
|
||||
* @param filePath
|
||||
* @return Returns an error string (empty if successful) and the file content
|
||||
*/
|
||||
QPair<QString, QByteArray> FileHandler::getFileContent(const QString& filePath) {
|
||||
QString errorString = "";
|
||||
|
||||
QByteArray fileContent;
|
||||
QFile file;
|
||||
file.setFileName(filePath);
|
||||
if (file.exists()) {
|
||||
qDebug() << "File found, reading content...";
|
||||
const bool successfulOpened = file.open(QIODevice::ReadOnly | QIODevice::Text);
|
||||
if (successfulOpened) {
|
||||
// TODO learn and decide on the differences between "readAll" and using
|
||||
// streams
|
||||
fileContent = file.readAll();
|
||||
file.close();
|
||||
} else {
|
||||
errorString = "File could not be opened!";
|
||||
qWarning() << errorString;
|
||||
}
|
||||
} else {
|
||||
errorString = "File not found. Returning empty result...";
|
||||
qInfo() << errorString;
|
||||
}
|
||||
|
||||
const QPair<QString, QByteArray> result(errorString, fileContent);
|
||||
return result;
|
||||
}
|
||||
28
libs/GenericCore/data/filehandler.h
Normal file
28
libs/GenericCore/data/filehandler.h
Normal file
@ -0,0 +1,28 @@
|
||||
#ifndef FILEHANDLER_H
|
||||
#define FILEHANDLER_H
|
||||
|
||||
#include <QVariant>
|
||||
|
||||
typedef QMap<int, QVariant> ModelItemValues;
|
||||
|
||||
class QJsonDocument;
|
||||
class QString;
|
||||
class QByteArray;
|
||||
|
||||
class FileHandler {
|
||||
public:
|
||||
/// JSON
|
||||
static bool saveToFile(const QJsonDocument& doc, const QString& fileName);
|
||||
static QByteArray loadJSONDataFromFile(const QString fileName);
|
||||
|
||||
/// CSV
|
||||
static QList<ModelItemValues> getItemValuesFromCSVFile(const QString& filePath);
|
||||
static bool exportToCSVFile(const QList<QStringList>& rows, const QString& filePath);
|
||||
|
||||
private:
|
||||
explicit FileHandler();
|
||||
|
||||
static QPair<QString, QByteArray> getFileContent(const QString& filePath);
|
||||
};
|
||||
|
||||
#endif // FILEHANDLER_H
|
||||
43
libs/GenericCore/data/settingshandler.cpp
Normal file
43
libs/GenericCore/data/settingshandler.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
#include "settingshandler.h"
|
||||
|
||||
#include <QSettings>
|
||||
|
||||
QVariantMap SettingsHandler::getSettings(QString group) {
|
||||
QSettings settings;
|
||||
QVariantMap result;
|
||||
|
||||
if (!group.isEmpty()) {
|
||||
settings.beginGroup(group);
|
||||
}
|
||||
|
||||
foreach (QString key, settings.allKeys()) {
|
||||
result.insert(key, settings.value(key));
|
||||
}
|
||||
|
||||
if (!group.isEmpty()) {
|
||||
settings.endGroup();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void SettingsHandler::saveSettings(QVariantMap settingMap, QString group) {
|
||||
qInfo() << "saving settings...";
|
||||
|
||||
QSettings settings;
|
||||
if (!group.isEmpty()) {
|
||||
settings.beginGroup(group);
|
||||
}
|
||||
|
||||
foreach (QString key, settingMap.keys()) {
|
||||
// qDebug() << "saving:" << key << "-" << settingMap.value(key);
|
||||
settings.setValue(key, settingMap.value(key));
|
||||
}
|
||||
if (!group.isEmpty()) {
|
||||
settings.endGroup();
|
||||
}
|
||||
|
||||
settings.sync();
|
||||
}
|
||||
|
||||
SettingsHandler::SettingsHandler() {}
|
||||
15
libs/GenericCore/data/settingshandler.h
Normal file
15
libs/GenericCore/data/settingshandler.h
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef SETTINGSHANDLER_H
|
||||
#define SETTINGSHANDLER_H
|
||||
|
||||
#include <QVariantMap>
|
||||
|
||||
class SettingsHandler {
|
||||
public:
|
||||
static QVariantMap getSettings(QString group = "");
|
||||
static void saveSettings(QVariantMap settingMap, QString group = "");
|
||||
|
||||
private:
|
||||
SettingsHandler();
|
||||
};
|
||||
|
||||
#endif // SETTINGSHANDLER_H
|
||||
Reference in New Issue
Block a user