Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_files.h"
Toshihiro Shimizu 890ddd
#include <qscriptengine></qscriptengine>
Toshihiro Shimizu 890ddd
#include <qfile></qfile>
Toshihiro Shimizu 890ddd
#include <qfileinfo></qfileinfo>
Toshihiro Shimizu 890ddd
#include <qdiriterator></qdiriterator>
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace TScriptBinding
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===========================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FilePath::FilePath(const QString &filePath)
Toshihiro Shimizu 890ddd
	: m_filePath(filePath)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FilePath::FilePath(const TFilePath &filePath)
Toshihiro Shimizu 890ddd
	: m_filePath(QString::fromStdWString(filePath.getWideString()))
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FilePath::~FilePath()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue FilePath::ctor(QScriptContext *context, QScriptEngine *engine)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FilePath *file = new FilePath();
Toshihiro Shimizu 890ddd
	if (context->argumentCount() == 1) {
Toshihiro Shimizu 890ddd
		file->m_filePath = context->argument(0).toString();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return file->create(engine, file);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue FilePath::toString() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return tr("\"%1\"").arg(m_filePath);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString FilePath::getExtension() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return QString::fromStdString(getToonzFilePath().getType());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue FilePath::setExtension(const QString &extension)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath fp = getToonzFilePath().withType(extension.toStdString());
Toshihiro Shimizu 890ddd
	m_filePath = QString::fromStdWString(fp.getWideString());
Toshihiro Shimizu 890ddd
	return context()->thisObject();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString FilePath::getName() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return QString::fromStdString(getToonzFilePath().getName());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FilePath::setName(const QString &name)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath fp = getToonzFilePath().withName(name.toStdString());
Toshihiro Shimizu 890ddd
	m_filePath = QString::fromStdWString(fp.getWideString());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue FilePath::getParentDirectory() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FilePath *result = new FilePath(getToonzFilePath().getParentDir());
Toshihiro Shimizu 890ddd
	return create(engine(), result);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FilePath::setParentDirectory(const QScriptValue &folder)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath fp;
Toshihiro Shimizu 890ddd
	QScriptValue err = checkFilePath(context(), folder, fp);
Toshihiro Shimizu 890ddd
	if (!err.isError()) {
Toshihiro Shimizu 890ddd
		m_filePath = QString::fromStdWString(getToonzFilePath().withParentDir(fp).getWideString());
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue FilePath::withExtension(const QString &extension)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath fp = getToonzFilePath().withType(extension.toStdString());
Toshihiro Shimizu 890ddd
	return create(engine(), new FilePath(fp));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue FilePath::withName(const QString &extension)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath fp = getToonzFilePath().withName(extension.toStdString());
Toshihiro Shimizu 890ddd
	return create(engine(), new FilePath(fp));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue FilePath::withParentDirectory(const QScriptValue &parentDirectoryArg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath parentDirectory;
Toshihiro Shimizu 890ddd
	QScriptValue err = checkFilePath(context(), parentDirectoryArg, parentDirectory);
Toshihiro Shimizu 890ddd
	if (err.isError())
Toshihiro Shimizu 890ddd
		return err;
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return create(engine(), new FilePath(getToonzFilePath().withParentDir(parentDirectory)));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool FilePath::exists() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return QFile(m_filePath).exists();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QDateTime FilePath::lastModified() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return QFileInfo(m_filePath).lastModified();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath FilePath::getToonzFilePath() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return TFilePath(m_filePath.toStdWString());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool FilePath::isDirectory() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return QFileInfo(m_filePath).isDir();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue FilePath::concat(const QScriptValue &value) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath fp;
Toshihiro Shimizu 890ddd
	QScriptValue err;
Toshihiro Shimizu 890ddd
	err = checkFilePath(context(), value, fp);
Toshihiro Shimizu 890ddd
	if (err.isError())
Toshihiro Shimizu 890ddd
		return err;
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
    if(!isDirectory())
Toshihiro Shimizu 890ddd
    {
Toshihiro Shimizu 890ddd
      QScriptValue v = toString();
Toshihiro Shimizu 890ddd
      return context()->throwError(tr("%1 is not a directory").arg(toString().toString()));
Toshihiro Shimizu 890ddd
    }
Toshihiro Shimizu 890ddd
    */
Toshihiro Shimizu 890ddd
	if (fp.isAbsolute())
Toshihiro Shimizu 890ddd
		return context()->throwError(tr("can't concatenate an absolute path : %1").arg(value.toString()));
Toshihiro Shimizu 890ddd
	fp = getToonzFilePath() + fp;
Toshihiro Shimizu 890ddd
	return create(engine(), new FilePath(fp));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue FilePath::files() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!isDirectory()) {
Toshihiro Shimizu 890ddd
		return context()->throwError(tr("%1 is not a directory").arg(toString().toString()));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	TFilePathSet fpset;
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		TSystem::readDirectory(fpset, getToonzFilePath());
Toshihiro Shimizu 890ddd
		QScriptValue result = engine()->newArray();
Toshihiro Shimizu 890ddd
		quint32 index = 0;
Toshihiro Shimizu 890ddd
		for (TFilePathSet::iterator it = fpset.begin(); it != fpset.end(); ++it) {
Toshihiro Shimizu 890ddd
			FilePath *res = new FilePath(*it);
Toshihiro Shimizu 890ddd
			result.setProperty(index++, res->create<filepath>(engine()));</filepath>
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		return result;
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
		return context()->throwError(tr("can't read directory %1").arg(toString().toString()));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue checkFilePath(QScriptContext *context, const QScriptValue &value, TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FilePath *filePath = qscriptvalue_cast<filepath *="">(value);</filepath>
Toshihiro Shimizu 890ddd
	if (filePath) {
Toshihiro Shimizu 890ddd
		fp = filePath->getToonzFilePath();
Toshihiro Shimizu 890ddd
	} else if (value.isString()) {
Toshihiro Shimizu 890ddd
		fp = TFilePath(value.toString().toStdWString());
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		return context->throwError(QObject::tr("Argument doesn't look like a file path : %1").arg(value.toString()));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return QScriptValue();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace TScriptBinding