From 941c1a8eb875684e83f9b101ecf7a6f3efe31536 Mon Sep 17 00:00:00 2001 From: Kimmo Varis Date: Thu, 8 Jul 2010 19:49:04 +0300 Subject: [PATCH] GUI: Improve error handling for reading/writing project file. --- gui/projectfile.cpp | 9 ++++++++- gui/projectfile.h | 9 +++++++++ gui/projectfiledialog.cpp | 28 +++++++++++++++++++++++++--- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/gui/projectfile.cpp b/gui/projectfile.cpp index 619bd8226..9cf2cc7fb 100644 --- a/gui/projectfile.cpp +++ b/gui/projectfile.cpp @@ -55,13 +55,17 @@ bool ProjectFile::Read(const QString &filename) QXmlStreamReader xmlReader(&file); bool insideProject = false; + bool projectTagFound = false; while (!xmlReader.atEnd()) { switch (xmlReader.readNext()) { case QXmlStreamReader::StartElement: if (xmlReader.name() == ProjectElementName) + { insideProject = true; + projectTagFound = true; + } // Find include directory from inside project element if (insideProject && xmlReader.name() == IncludDirElementName) @@ -93,7 +97,10 @@ bool ProjectFile::Read(const QString &filename) } file.close(); - return true; + if (projectTagFound) + return true; + else + return false; } QStringList ProjectFile::GetIncludeDirs() const diff --git a/gui/projectfile.h b/gui/projectfile.h index dc58fa3e2..c5ca75685 100644 --- a/gui/projectfile.h +++ b/gui/projectfile.h @@ -77,6 +77,15 @@ public: */ bool Write(const QString &filename = QString()); + /** + * @brief Set filename for the project file. + * @param filename Filename to use. + */ + void SetFilename(const QString &filename) + { + mFilename = filename; + } + protected: /** * @brief Read list of include directories from XML. diff --git a/gui/projectfiledialog.cpp b/gui/projectfiledialog.cpp index cf896782d..92403e40a 100644 --- a/gui/projectfiledialog.cpp +++ b/gui/projectfiledialog.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include "projectfiledialog.h" #include "projectfile.h" @@ -41,7 +42,17 @@ ProjectFileDialog::ProjectFileDialog(const QString &path, QWidget *parent) void ProjectFileDialog::ReadProjectFile() { - mPFile->Read(); + if (!mPFile->Read()) + { + QMessageBox msg(QMessageBox::Critical, + tr("Cppcheck"), + tr("Could not read the project file."), + QMessageBox::Ok, + this); + msg.exec(); + mFileName = QString(); + mPFile->SetFilename(mFileName); + } QStringList includes = mPFile->GetIncludeDirs(); QString includestr; @@ -70,6 +81,7 @@ void ProjectFileDialog::DialogAccepted() return; UpdateProjectFileData(); + bool writesuccess = false; if (mFileName.isEmpty()) { const QString filter = tr("Project files (*.cppcheck);;All files(*.*)"); @@ -80,12 +92,22 @@ void ProjectFileDialog::DialogAccepted() if (!filepath.isEmpty()) { - mPFile->Write(filepath); + writesuccess = mPFile->Write(filepath); } } else { - mPFile->Write(); + writesuccess = mPFile->Write(); + } + + if (!writesuccess) + { + QMessageBox msg(QMessageBox::Critical, + tr("Cppcheck"), + tr("Could not write the project file."), + QMessageBox::Ok, + this); + msg.exec(); } mDataSaved = true; }