Abstract Class :
//****************
//File Plugin
class FileInterface
{
public:
virtual ~FileInterface() {};
//file open and close methods
virtual QFile* OpenFile(const QString fileName, QIODevice::OpenModeFlag
fileMode) = 0;
virtual bool WriteText(QFile *file, const QString data) = 0;
virtual void Close(QFile *file) = 0;
virtual QString TranslateIOError(const int errorCode, const QString
fileName) = 0;
};
Q_DECLARE_INTERFACE(FileInterface,"annex.File/1.0")
//End File Plugin
New Class derived from abstract
#ifndef FILE_H
#define FILE_H
#include <QPluginLoader>
//GUI interface
#include <QMessageBox>
//File handling
#include <QFile>
#include <QFileInfo>
//Include the interface file
#include "../../boffice/plugins.h"
//forward declare
//class QFile;
class File : public QFile, public FileInterface
{
Q_INTERFACES(FileInterface)
public:
//constructer, copy constructer, destructor
File(QWidget *parent);
~File();
//file open and close methods
QFile* OpenFile(const QString fileName, QIODevice::OpenModeFlag
fileMode);
bool WriteText(QFile *file, const QString data) const;
void Close(QFile *file);
QString TranslateIOError(const int errorCode, const QString fileName)
const;
private:
QMessageBox *m_mb;
QWidget *m_parent;
};
#endif
#include "file.h"
//File handling
#include <QFileInfo>
//default constructor and des
File::File( QWidget *parent )
{
m_parent = parent;
//setup the message box
m_mb = new QMessageBox(m_parent);
m_mb->setWindowTitle("Annex");
m_mb->setIcon(QMessageBox::Critical);
};
File::~File()
{
delete m_mb;
m_mb = 0;
};
QFile* File::OpenFile(const QString fileName, QIODevice::OpenModeFlag
fileMode)
{
QFile *fileToOpen = new QFile(fileName);
QString errorMsg = "";
int fileStatus = 0;
if (fileToOpen->open(fileMode) == TRUE)
{
//file was open, can return the pointer
}
else
{
//file open failed
fileStatus = fileToOpen->error();
errorMsg = TranslateIOError(fileStatus,fileName);
if (errorMsg != "")
{
//display the error message if the string is not empty
m_mb->setText(errorMsg);
m_mb->exec();
}
else
{
//error could not be translated, print own error
m_mb->setText("File open for [" + fileName + "] failed !");
m_mb->exec();
}
}
return(fileToOpen);
};
bool File::WriteText(QFile *file, const QString data) const
{
QFileInfo *fileInfo = new QFileInfo(*file);
const QString fileName = fileInfo->fileName();
long bufferSize = 0;
long fileWriteBuffer = 0;
bool wrote = FALSE;
QString errorMsg = "";
//write data to file
bufferSize = data.length();
fileWriteBuffer = file->write(data.toAscii(),bufferSize);
if (fileWriteBuffer == -1)
{
//write error occured
errorMsg = TranslateIOError(file->error(),fileName);
m_mb->setText(errorMsg);
m_mb->exec();
}
else
{
if (fileWriteBuffer == 0 && bufferSize != 0)
{
//no data was written to disk
errorMsg ="[0] of [" + QString::number(bufferSize) + "] Bytes data wrote
to disk !";
m_mb->setText(errorMsg);
m_mb->exec();
}
else
{
file->flush();
wrote = TRUE;
}
}
delete fileInfo;
fileInfo = 0;
return(wrote);
};
void File::Close(QFile *file)
{
if (file->isOpen())
{
file->flush();
file->close();
}
};
QString File::TranslateIOError(const int errorCode, const QString fileName)
const
{
QString errorMsg = "";
switch (errorCode)
{
case QFile::NoError:
errorMsg = "";
break;
case QFile::ReadError:
errorMsg = "File [" + fileName + "] open failed with [Read Error] !";
break;
case QFile::WriteError:
errorMsg = "File [" + fileName + "] open failed with [Write Error] !";
break;
case QFile::FatalError:
errorMsg = "File [" + fileName + "] open failed with [Fatal Error] !";
break;
case QFile::ResourceError:
errorMsg = "File [" + fileName + "] open failed with [Resource Error] !";
break;
case QFile::OpenError:
errorMsg = "File [" + fileName + "] open failed with [Open Error] !";
break;
case QFile::AbortError:
errorMsg = "File [" + fileName + "] open failed with [The operation was
aborted] !";
break;
case QFile::TimeOutError:
errorMsg = "File [" + fileName + "] open failed with [Time Out Error] !";
break;
case QFile::UnspecifiedError:
errorMsg = "File [" + fileName + "] open failed with [Unspecified Error]
!";
break;
case QFile::RemoveError:
errorMsg = "File [" + fileName + "] could not be removed !";
break;
case QFile::RenameError:
errorMsg = "File [" + fileName + "] could not be renamed !";
break;
case QFile:
ositionError:
errorMsg = "Position in file [" + fileName + "] could not be changed !";
break;
case QFile::ResizeError:
errorMsg = "File [" + fileName + "] could not be resized !";
break;
case QFile:
ermissionsError:
errorMsg = "File [" + fileName + "] could not be accessed due to
permissions !";
break;
case QFile::CopyError:
errorMsg = "File [" + fileName + "] could not be copied !";
break;
default:
errorMsg = "File [" + fileName + "] could not be opened [Default] !";
break;
}
return(errorMsg);
};
Q_EXPORT_PLUGIN2(FileInterface,File);