2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-09-12 16:52:54 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-09-12 16:52:54 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-09-12 16:52:54 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2011-09-12 16:52:54 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-09-12 16:52:54 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-09-12 16:52:54 +02:00
|
|
|
|
|
|
|
|
#include "generatedfile.h"
|
|
|
|
|
|
2019-10-03 13:03:13 +02:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
|
|
2011-09-12 16:52:54 +02:00
|
|
|
#include <utils/fileutils.h>
|
2019-10-03 13:03:13 +02:00
|
|
|
#include <utils/textfileformat.h>
|
2011-09-12 16:52:54 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QCoreApplication>
|
2019-10-03 13:03:13 +02:00
|
|
|
#include <QDir>
|
|
|
|
|
#include <QString>
|
2011-09-12 16:52:54 +02:00
|
|
|
|
2020-06-26 13:59:38 +02:00
|
|
|
using namespace Utils;
|
|
|
|
|
|
2011-09-12 16:52:54 +02:00
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\class Core::GeneratedFile
|
2020-06-12 16:04:30 +02:00
|
|
|
\inheaderfile coreplugin/generatedfile.h
|
2020-03-18 13:32:02 +01:00
|
|
|
\inmodule QtCreator
|
2020-06-12 16:04:30 +02:00
|
|
|
|
2013-06-05 14:29:24 +02:00
|
|
|
\brief The GeneratedFile class represents a file generated by a wizard.
|
2011-09-12 16:52:54 +02:00
|
|
|
|
2020-03-18 13:32:02 +01:00
|
|
|
The BaseFileWizard class checks whether each file already exists and
|
2013-09-06 16:38:53 +02:00
|
|
|
reports any errors that may occur during creation of the files.
|
2011-09-12 16:52:54 +02:00
|
|
|
|
2020-03-18 13:32:02 +01:00
|
|
|
\sa Core::WizardDialogParameters, Core::BaseFileWizard,
|
2011-09-12 16:52:54 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
class GeneratedFilePrivate : public QSharedData
|
|
|
|
|
{
|
|
|
|
|
public:
|
2018-07-21 21:11:46 +02:00
|
|
|
GeneratedFilePrivate() = default;
|
2011-09-12 16:52:54 +02:00
|
|
|
explicit GeneratedFilePrivate(const QString &p);
|
|
|
|
|
QString path;
|
|
|
|
|
QByteArray contents;
|
2011-11-10 11:36:51 +01:00
|
|
|
Id editorId;
|
2018-07-21 21:11:46 +02:00
|
|
|
bool binary = false;
|
2011-09-12 16:52:54 +02:00
|
|
|
GeneratedFile::Attributes attributes;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
GeneratedFilePrivate::GeneratedFilePrivate(const QString &p) :
|
|
|
|
|
path(QDir::cleanPath(p)),
|
2018-07-21 21:11:46 +02:00
|
|
|
attributes({})
|
2011-09-12 16:52:54 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GeneratedFile::GeneratedFile() :
|
|
|
|
|
m_d(new GeneratedFilePrivate)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GeneratedFile::GeneratedFile(const QString &p) :
|
|
|
|
|
m_d(new GeneratedFilePrivate(p))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-21 21:11:46 +02:00
|
|
|
GeneratedFile::GeneratedFile(const GeneratedFile &rhs) = default;
|
2011-09-12 16:52:54 +02:00
|
|
|
|
|
|
|
|
GeneratedFile &GeneratedFile::operator=(const GeneratedFile &rhs)
|
|
|
|
|
{
|
|
|
|
|
if (this != &rhs)
|
|
|
|
|
m_d.operator=(rhs.m_d);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-21 21:11:46 +02:00
|
|
|
GeneratedFile::~GeneratedFile() = default;
|
2011-09-12 16:52:54 +02:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-10 11:36:51 +01:00
|
|
|
Id GeneratedFile::editorId() const
|
2011-09-12 16:52:54 +02:00
|
|
|
{
|
|
|
|
|
return m_d->editorId;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-01 11:08:26 +02:00
|
|
|
void GeneratedFile::setEditorId(Id id)
|
2011-09-12 16:52:54 +02:00
|
|
|
{
|
2011-11-10 11:36:51 +01:00
|
|
|
m_d->editorId = id;
|
2011-09-12 16:52:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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())) {
|
2019-10-03 13:03:13 +02:00
|
|
|
*errorMessage = QCoreApplication::translate("BaseFileWizard",
|
|
|
|
|
"Unable to create the directory %1.")
|
|
|
|
|
.arg(QDir::toNativeSeparators(dir.absolutePath()));
|
2011-09-12 16:52:54 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write out
|
2019-10-03 13:03:13 +02:00
|
|
|
if (isBinary()) {
|
|
|
|
|
QIODevice::OpenMode flags = QIODevice::WriteOnly | QIODevice::Truncate;
|
|
|
|
|
Utils::FileSaver saver(m_d->path, flags);
|
|
|
|
|
saver.write(m_d->contents);
|
|
|
|
|
return saver.finalize(errorMessage);
|
|
|
|
|
}
|
2011-09-12 16:52:54 +02:00
|
|
|
|
2019-10-03 13:03:13 +02:00
|
|
|
Utils::TextFileFormat format;
|
|
|
|
|
format.codec = EditorManager::defaultTextCodec();
|
2019-09-03 22:37:25 +02:00
|
|
|
format.lineTerminationMode = EditorManager::defaultLineEnding();
|
2019-10-03 13:03:13 +02:00
|
|
|
return format.writeFile(m_d->path, contents(), errorMessage);
|
2011-09-12 16:52:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GeneratedFile::Attributes GeneratedFile::attributes() const
|
|
|
|
|
{
|
|
|
|
|
return m_d->attributes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GeneratedFile::setAttributes(Attributes a)
|
|
|
|
|
{
|
|
|
|
|
m_d->attributes = a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Core
|