forked from qt-creator/qt-creator
Wizards: Refactor GeneratedFile in a extra file
Change-Id: I58f7053405c79774154c2f0447944533e6b10152 Reviewed-on: http://codereview.qt-project.org/4685 Reviewed-by: Alessandro Portale <alessandro.portale@nokia.com> Reviewed-by: Eike Ziller <eike.ziller@nokia.com>
This commit is contained in:
@@ -61,146 +61,8 @@
|
||||
|
||||
enum { debugWizard = 0 };
|
||||
|
||||
/*!
|
||||
\class Core::GeneratedFile
|
||||
\brief Represents a file generated by a wizard.
|
||||
|
||||
The Wizard class will check for each file whether it already exists and will
|
||||
report any errors that may occur during creation of the files.
|
||||
|
||||
\sa Core::BaseFileWizardParameters, Core::BaseFileWizard, Core::StandardFileWizard
|
||||
\sa Core::Internal::WizardEventLoop
|
||||
*/
|
||||
|
||||
namespace Core {
|
||||
|
||||
class GeneratedFilePrivate : public QSharedData
|
||||
{
|
||||
public:
|
||||
GeneratedFilePrivate() : binary(false) {}
|
||||
explicit GeneratedFilePrivate(const QString &p);
|
||||
QString path;
|
||||
QByteArray contents;
|
||||
QString editorId;
|
||||
bool binary;
|
||||
GeneratedFile::Attributes attributes;
|
||||
};
|
||||
|
||||
GeneratedFilePrivate::GeneratedFilePrivate(const QString &p) :
|
||||
path(QDir::cleanPath(p)),
|
||||
binary(false),
|
||||
attributes(0)
|
||||
{
|
||||
}
|
||||
|
||||
GeneratedFile::GeneratedFile() :
|
||||
m_d(new GeneratedFilePrivate)
|
||||
{
|
||||
}
|
||||
|
||||
GeneratedFile::GeneratedFile(const QString &p) :
|
||||
m_d(new GeneratedFilePrivate(p))
|
||||
{
|
||||
}
|
||||
|
||||
GeneratedFile::GeneratedFile(const GeneratedFile &rhs) :
|
||||
m_d(rhs.m_d)
|
||||
{
|
||||
}
|
||||
|
||||
GeneratedFile &GeneratedFile::operator=(const GeneratedFile &rhs)
|
||||
{
|
||||
if (this != &rhs)
|
||||
m_d.operator=(rhs.m_d);
|
||||
return *this;
|
||||
}
|
||||
|
||||
GeneratedFile::~GeneratedFile()
|
||||
{
|
||||
}
|
||||
|
||||
QString GeneratedFile::path() const
|
||||
{
|
||||
return m_d->path;
|
||||
}
|
||||
|
||||
void GeneratedFile::setPath(const QString &p)
|
||||
{
|
||||
m_d->path = QDir::cleanPath(p);
|
||||
}
|
||||
|
||||
QString GeneratedFile::contents() const
|
||||
{
|
||||
return QString::fromUtf8(m_d->contents);
|
||||
}
|
||||
|
||||
void GeneratedFile::setContents(const QString &c)
|
||||
{
|
||||
m_d->contents = c.toUtf8();
|
||||
}
|
||||
|
||||
QByteArray GeneratedFile::binaryContents() const
|
||||
{
|
||||
return m_d->contents;
|
||||
}
|
||||
|
||||
void GeneratedFile::setBinaryContents(const QByteArray &c)
|
||||
{
|
||||
m_d->contents = c;
|
||||
}
|
||||
|
||||
bool GeneratedFile::isBinary() const
|
||||
{
|
||||
return m_d->binary;
|
||||
}
|
||||
|
||||
void GeneratedFile::setBinary(bool b)
|
||||
{
|
||||
m_d->binary = b;
|
||||
}
|
||||
|
||||
QString GeneratedFile::editorId() const
|
||||
{
|
||||
return m_d->editorId;
|
||||
}
|
||||
|
||||
void GeneratedFile::setEditorId(const QString &k)
|
||||
{
|
||||
m_d->editorId = k;
|
||||
}
|
||||
|
||||
bool GeneratedFile::write(QString *errorMessage) const
|
||||
{
|
||||
// Ensure the directory
|
||||
const QFileInfo info(m_d->path);
|
||||
const QDir dir = info.absoluteDir();
|
||||
if (!dir.exists()) {
|
||||
if (!dir.mkpath(dir.absolutePath())) {
|
||||
*errorMessage = BaseFileWizard::tr("Unable to create the directory %1.").arg(dir.absolutePath());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Write out
|
||||
QIODevice::OpenMode flags = QIODevice::WriteOnly|QIODevice::Truncate;
|
||||
if (!isBinary())
|
||||
flags |= QIODevice::Text;
|
||||
|
||||
Utils::FileSaver saver(m_d->path, flags);
|
||||
saver.write(m_d->contents);
|
||||
return saver.finalize(errorMessage);
|
||||
}
|
||||
|
||||
GeneratedFile::Attributes GeneratedFile::attributes() const
|
||||
{
|
||||
return m_d->attributes;
|
||||
}
|
||||
|
||||
void GeneratedFile::setAttributes(Attributes a)
|
||||
{
|
||||
m_d->attributes = a;
|
||||
}
|
||||
|
||||
static int indexOfFile(const GeneratedFiles &f, const QString &path)
|
||||
{
|
||||
const int size = f.size();
|
||||
|
||||
Reference in New Issue
Block a user