-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ backup: added backup & restore functionality
Backup & Restore
- Loading branch information
Showing
9 changed files
with
185 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#include "backup.h" | ||
|
||
Backup::Backup(QObject *parent) | ||
: QObject{parent} | ||
{ | ||
} | ||
|
||
void Backup::backup(QString filePath, bool includeSensitiveInfo) | ||
{ | ||
|
||
if (filePath.isEmpty()) { | ||
return; | ||
} | ||
|
||
// adding .json if needed | ||
if(!filePath.endsWith(".json")) { | ||
filePath.append(".json"); | ||
} | ||
|
||
QFile file(filePath); | ||
if (!(file.open(QIODevice::WriteOnly | QIODevice::Text))) { | ||
qDebug() << "[Backup] Cannot open file in write mode"; | ||
file.close(); | ||
return; | ||
} | ||
|
||
qDebug() << "[Backup] Starting backup..."; | ||
// adding all the keys in a json object and converting it into a string | ||
QJsonObject jsonObject; | ||
QSettings settings; | ||
QStringList keys = settings.allKeys(); | ||
|
||
// sensitive info must be excluded | ||
if(!includeSensitiveInfo) { | ||
keys.removeIf([](const QString& key){ | ||
return key.startsWith("osuirc") || key.startsWith("twitch"); | ||
}); | ||
} | ||
|
||
for(const QString& key: keys){ | ||
QString value = settings.value(key).toString(); | ||
jsonObject.insert(key, QJsonValue(value)); | ||
} | ||
QString jsonString = QJsonDocument(jsonObject).toJson(); | ||
|
||
// saving the backup | ||
QTextStream stream(&file); | ||
stream << jsonString; | ||
file.close(); | ||
qDebug() << "[Backup] Backup completed"; | ||
} | ||
|
||
void Backup::restore(QString filePath) | ||
{ | ||
if (filePath.isEmpty()) { | ||
return; | ||
} | ||
|
||
QFile file(filePath); | ||
if (!(file.open(QIODevice::ReadOnly | QIODevice::Text))) { | ||
qDebug() << "[Backup] Cannot open file in read mode"; | ||
file.close(); | ||
return; | ||
} | ||
|
||
qDebug() << "[Backup] Starting backup restore..."; | ||
QTextStream stream(&file); | ||
QString jsonString = stream.readAll(); | ||
file.close(); | ||
|
||
QSettings settings; | ||
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8()); | ||
if (!jsonDoc.isNull()){ | ||
QJsonObject jsonObject = jsonDoc.object(); | ||
for (auto it = jsonObject.begin(); it != jsonObject.end(); ++it) { | ||
QString key = it.key(); | ||
QJsonValue value = it.value(); | ||
|
||
settings.setValue(key, value.toString()); | ||
} | ||
} | ||
qDebug() << "[Backup] Backup restored successfully"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#ifndef BACKUP_H | ||
#define BACKUP_H | ||
|
||
#include <QDebug> | ||
#include <QFile> | ||
#include <QJsonDocument> | ||
#include <QJsonObject> | ||
#include <QJsonValue> | ||
#include <QMessageBox> | ||
#include <QObject> | ||
#include <QSettings> | ||
|
||
class Backup : public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
explicit Backup(QObject *parent = nullptr); | ||
static void backup(QString filePath, bool includeSensitiveInfo); | ||
static void restore(QString filePath); | ||
|
||
}; | ||
|
||
#endif // BACKUP_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters