diff options
author | Nils Schwabe | 2014-03-06 17:18:26 +0100 |
---|---|---|
committer | Nils Schwabe | 2014-03-06 17:18:26 +0100 |
commit | 821b8abe4d40227ada5bf2a3852dcff440bb53e3 (patch) | |
tree | 00dd62dbc3af40f85d0dd9e4d984441802b3fb8d | |
parent | vmchooser is now able to get vsessions from a given url (diff) | |
download | vmchooser2-821b8abe4d40227ada5bf2a3852dcff440bb53e3.tar.gz vmchooser2-821b8abe4d40227ada5bf2a3852dcff440bb53e3.tar.xz vmchooser2-821b8abe4d40227ada5bf2a3852dcff440bb53e3.zip |
added backup file functionallity
-rw-r--r-- | src/dialog.cpp | 56 | ||||
-rw-r--r-- | src/dialog.h | 2 | ||||
-rw-r--r-- | src/vsession.cpp | 35 |
3 files changed, 73 insertions, 20 deletions
diff --git a/src/dialog.cpp b/src/dialog.cpp index def152c..38711c3 100644 --- a/src/dialog.cpp +++ b/src/dialog.cpp @@ -26,7 +26,7 @@ Dialog::Dialog(QWidget *parent) QRect desktopRect = QApplication::desktop()->availableGeometry(this); oldCenter_ = desktopRect.center(); centerTimer_ = new QTimer(this); - connect(centerTimer_, SIGNAL(timeout()), this, SLOT(on_centerTimer())); + connect(centerTimer_, SIGNAL(timeout()), this, SLOT(onCenterTimer())); centerTimer_->start(1000); } @@ -228,7 +228,7 @@ void Dialog::setTheme() { ui->label_r->setStyleSheet(label_r_style); } -void Dialog::on_centerTimer() { +void Dialog::onCenterTimer() { if (!autoStartEntry_.isEmpty()) { if (this->selectSession(autoStartEntry_)) { this->on_treeView_activated(ui->treeView->selectionModel()->currentIndex()); @@ -248,29 +248,51 @@ void Dialog::on_centerTimer() { } void Dialog::addSessionsAfterDownload(QNetworkReply* reply) { + QString filename = "/tmp/vmchooser2.xml"; + QString backup_filename = "/tmp/vmchooser2_backup.xml"; + if (reply->error() != QNetworkReply::NoError) { - qDebug() << "Error reading from URL: " << reply->error(); - return; - } + if (debugMode) { + qDebug() << "Error reading from URL: " << reply->error(); + } - QString xml_doc(reply->readAll()); + QFile backup_file(backup_filename); + + if (!backup_file.open(QIODevice::ReadOnly)) { + if (debugMode) { + qDebug() << "Cannot read backup file " << backup_filename << " either"; + } + return; + } - QFile file("/tmp/vmchooser2.xml"); - if (!file.open(QIODevice::WriteOnly)) { if (debugMode) { - qDebug() << "Could not write XML to /tmp"; + qDebug() << "Used backup file " << backup_filename; } - return; - } - QByteArray data = xml_doc.toUtf8(); + backup_file.close(); + this->addItems(VSession::readXmlFile(backup_filename), QCoreApplication::instance()->translate("Dialog", "Virtual Sessions")); - if (file.write(data) != data.length()) { - return; - } + } else { - file.close(); + QString xml_doc(reply->readAll()); + QFile file(filename); + + if (!file.open(QIODevice::WriteOnly)) { + if (debugMode) { + qDebug() << "Could not write XML to " << filename; + } + return; + } + + QByteArray data = xml_doc.toUtf8(); + + if (file.write(data) != data.length()) { + return; + } + + file.close(); + } - this->addItems(VSession::readXmlFile(file.fileName()), QCoreApplication::instance()->translate("Dialog", "Virtual Sessions")); + this->addItems(VSession::readXmlFile(filename), QCoreApplication::instance()->translate("Dialog", "Virtual Sessions")); } diff --git a/src/dialog.h b/src/dialog.h index 7e9b1d5..4627a8e 100644 --- a/src/dialog.h +++ b/src/dialog.h @@ -45,7 +45,7 @@ class Dialog : public QDialog { void on_pushButtonStart_clicked(); void on_pushButtonAbort_clicked(); void on_treeView_activated(QModelIndex index); - void on_centerTimer(); + void onCenterTimer(); public slots: void addSessionsAfterDownload(QNetworkReply* reply); diff --git a/src/vsession.cpp b/src/vsession.cpp index 8134bc6..9d3cadb 100644 --- a/src/vsession.cpp +++ b/src/vsession.cpp @@ -305,6 +305,10 @@ QList<Session*> VSession::readXmlFile(const QString& filepath) { QDomDocument doc; QFile file(filepath); + QString backup_filename = "/tmp/vmchooser2_backup.xml"; + QFile backup_file(backup_filename); + + if (!file.open(QIODevice::ReadOnly)) { if (debugMode) { qDebug() << "Cannot read file: " << file.fileName(); @@ -315,9 +319,36 @@ QList<Session*> VSession::readXmlFile(const QString& filepath) { if (debugMode) { qDebug() << "XML file not valid: " << file.fileName(); } - file.close(); - return retval; + + file.close(); + + // try to use backup file + if (!backup_file.open(QIODevice::ReadOnly)) { + if (debugMode) { + qDebug() << "Cannot read backup file " << backup_filename << " either"; + } + return retval; + } + + if (!doc.setContent(&backup_file)) { + if (debugMode) { + qDebug() << "XML file not valid: " << backup_file.fileName(); + } + backup_file.close(); + return retval; + } + + if (debugMode) { + qDebug() << "Used backup file " << backup_filename; + } + + backup_file.close(); + } else { + // file is valid --> create backup file + QFile::remove(backup_filename); + QFile::copy(filepath, backup_filename); } + file.close(); QString dirName(QFileInfo(filepath).dir().absolutePath()); |