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