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);
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

View File

@ -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.

View File

@ -19,6 +19,7 @@
#include <QStringList>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#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;
}