GUI: Improve error handling for reading/writing project file.

This commit is contained in:
Kimmo Varis 2010-07-08 19:49:04 +03:00
parent 25a9c75287
commit 941c1a8eb8
3 changed files with 42 additions and 4 deletions

View File

@ -55,13 +55,17 @@ bool ProjectFile::Read(const QString &filename)
QXmlStreamReader xmlReader(&file); QXmlStreamReader xmlReader(&file);
bool insideProject = false; bool insideProject = false;
bool projectTagFound = false;
while (!xmlReader.atEnd()) while (!xmlReader.atEnd())
{ {
switch (xmlReader.readNext()) switch (xmlReader.readNext())
{ {
case QXmlStreamReader::StartElement: case QXmlStreamReader::StartElement:
if (xmlReader.name() == ProjectElementName) if (xmlReader.name() == ProjectElementName)
{
insideProject = true; insideProject = true;
projectTagFound = true;
}
// Find include directory from inside project element // Find include directory from inside project element
if (insideProject && xmlReader.name() == IncludDirElementName) if (insideProject && xmlReader.name() == IncludDirElementName)
@ -93,7 +97,10 @@ bool ProjectFile::Read(const QString &filename)
} }
file.close(); file.close();
return true; if (projectTagFound)
return true;
else
return false;
} }
QStringList ProjectFile::GetIncludeDirs() const QStringList ProjectFile::GetIncludeDirs() const

View File

@ -77,6 +77,15 @@ public:
*/ */
bool Write(const QString &filename = QString()); 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: protected:
/** /**
* @brief Read list of include directories from XML. * @brief Read list of include directories from XML.

View File

@ -19,6 +19,7 @@
#include <QStringList> #include <QStringList>
#include <QFile> #include <QFile>
#include <QFileDialog> #include <QFileDialog>
#include <QMessageBox>
#include "projectfiledialog.h" #include "projectfiledialog.h"
#include "projectfile.h" #include "projectfile.h"
@ -41,7 +42,17 @@ ProjectFileDialog::ProjectFileDialog(const QString &path, QWidget *parent)
void ProjectFileDialog::ReadProjectFile() 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(); QStringList includes = mPFile->GetIncludeDirs();
QString includestr; QString includestr;
@ -70,6 +81,7 @@ void ProjectFileDialog::DialogAccepted()
return; return;
UpdateProjectFileData(); UpdateProjectFileData();
bool writesuccess = false;
if (mFileName.isEmpty()) if (mFileName.isEmpty())
{ {
const QString filter = tr("Project files (*.cppcheck);;All files(*.*)"); const QString filter = tr("Project files (*.cppcheck);;All files(*.*)");
@ -80,12 +92,22 @@ void ProjectFileDialog::DialogAccepted()
if (!filepath.isEmpty()) if (!filepath.isEmpty())
{ {
mPFile->Write(filepath); writesuccess = mPFile->Write(filepath);
} }
} }
else 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; mDataSaved = true;
} }