DB backup/restore, improved error handling for NV

This commit is contained in:
Muki 2016-11-19 15:40:29 +01:00
parent 8889c7388a
commit 84dee548e3
5 changed files with 107 additions and 3 deletions

View file

@ -113,6 +113,44 @@ bool DatabaseManager::openDB()
return db.open();
}
bool DatabaseManager::makeBackup()
{
Settings *s = Settings::instance();
if (backupFilePath.isEmpty()) {
backupFilePath = s->getSettingsDir();
backupFilePath.append(QDir::separator()).append("settings_backup.db");
backupFilePath = QDir::toNativeSeparators(backupFilePath);
}
if (QFile::exists(backupFilePath)) {
//qDebug() << "DB backup file exists and will be overwrite!";
QFile::remove(backupFilePath);
}
return QFile::copy(dbFilePath, backupFilePath);
}
bool DatabaseManager::restoreBackup()
{
if (!QFile::exists(backupFilePath)) {
qWarning() << "DB backup file doesn't exist!";
return false;
}
if (!deleteDB()) {
qWarning() << "Current DB file can not be deleted!";
return false;
}
if (!QFile::rename(backupFilePath, dbFilePath)) {
qWarning() << "Can not rename DB backup file!";
return false;
}
return openDB();
}
bool DatabaseManager::deleteDB()
{
db.close();

View file

@ -177,6 +177,9 @@ public:
Q_INVOKABLE void init();
Q_INVOKABLE void newInit();
bool makeBackup();
bool restoreBackup();
bool isSynced();
void cleanDashboards();
@ -336,6 +339,7 @@ signals:
private:
QSqlDatabase db;
QString dbFilePath;
QString backupFilePath;
void checkError(const QSqlError &error);

View file

@ -154,11 +154,17 @@ void Fetcher::cancel()
busyType == Fetcher::CheckingCredentialsWaiting) {
setBusy(false);
} else {
// Restoring backup
Settings *s = Settings::instance();
if (!s->db->restoreBackup()) {
qWarning() << "Unable to restore DB backup!";
}
if (currentReply != NULL)
currentReply->close();
else
setBusy(false);
//emit canceled();
}
}
@ -179,7 +185,6 @@ bool Fetcher::checkCredentials()
#endif
setBusy(true, Fetcher::CheckingCredentials);
return true;
}

View file

@ -104,6 +104,7 @@ Q_SIGNALS:
503 - User ID is empty
504 - No network
505 - Refresh token error
506 - DB backup error
600 - Error while parsing JSON
601 - Unknown JSON response
*/

View file

@ -111,6 +111,14 @@ void NvFetcher::startFetching()
storedStreamList = s->db->readStreamModuleTabListWithoutDate();
//Backup
if (!s->db->makeBackup()) {
qWarning() << "Unable to make DB backup!";
emit error(506);
setBusy(false);
return;
}
s->db->cleanDashboards();
s->db->cleanTabs();
s->db->cleanModules();
@ -798,6 +806,13 @@ void NvFetcher::finishedDashboards()
{
//qDebug() << data;
if (currentReply->error()) {
// Restoring backup
Settings *s = Settings::instance();
if (!s->db->restoreBackup()) {
qWarning() << "Unable to restore DB backup!";
}
emit error(500);
setBusy(false);
return;
@ -831,6 +846,13 @@ void NvFetcher::finishedTabs()
{
//qDebug() << data;
if (currentReply->error()) {
// Restoring backup
Settings *s = Settings::instance();
if (!s->db->restoreBackup()) {
qWarning() << "Unable to restore DB backup!";
}
emit error(500);
setBusy(false);
return;
@ -893,6 +915,13 @@ void NvFetcher::finishedFeeds()
{
//qDebug() << data;
if (currentReply->error()) {
// Restoring backup
Settings *s = Settings::instance();
if (!s->db->restoreBackup()) {
qWarning() << "Unable to restore DB backup!";
}
emit error(500);
setBusy(false);
return;
@ -928,6 +957,13 @@ void NvFetcher::finishedFeedsReadlater()
{
//qDebug() << data;
if (currentReply->error()) {
// Restoring backup
Settings *s = Settings::instance();
if (!s->db->restoreBackup()) {
qWarning() << "Unable to restore DB backup!";
}
emit error(500);
setBusy(false);
return;
@ -960,6 +996,13 @@ void NvFetcher::finishedFeedsUpdate()
{
//qDebug() << data;
if (currentReply->error()) {
// Restoring backup
Settings *s = Settings::instance();
if (!s->db->restoreBackup()) {
qWarning() << "Unable to restore DB backup!";
}
emit error(500);
setBusy(false);
return;
@ -1064,6 +1107,8 @@ void NvFetcher::startJob(Job job)
return;
}
Settings *s = Settings::instance();
disconnect(this, SIGNAL(finished()), 0, 0);
currentJob = job;
//qDebug() << "Job:" << job;
@ -1072,7 +1117,6 @@ void NvFetcher::startJob(Job job)
if (jsonObj.contains("success") && !jsonObj["success"].toBool()) {
// If credentials other than Netvibes, prompting for re-auth
Settings *s = Settings::instance();
if (s->getSigninType()>0) {
qWarning() << "Cookie expires!";
s->setCookie("");
@ -1082,6 +1126,12 @@ void NvFetcher::startJob(Job job)
}
qWarning() << "Netvibes API error!" << jsonObj;
// Restoring backup
if (!s->db->restoreBackup()) {
qWarning() << "Unable to restore DB backup!";
}
setBusy(false);
emit error(500);
//update();
@ -1089,6 +1139,12 @@ void NvFetcher::startJob(Job job)
}
} else {
qWarning() << "Error parsing Json!";
// Restoring backup
if (!s->db->restoreBackup()) {
qWarning() << "Unable to restore DB backup!";
}
emit error(600);
setBusy(false);
return;