Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/toonzfolders.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include "tenv.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
turtletooth 8d9ba3
#include "toonz/preferences.h"
Jeremy Bullock e59b1f
#include <qstandardpaths></qstandardpaths>
shun-iwasawa 7f4819
#include <qdir></qdir>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace TEnv;
Toshihiro Shimizu 890ddd
Jeremy Bullock e59b1f
//-------------------------------------------------------------------
Jeremy Bullock e59b1f
namespace {
Jeremy Bullock e59b1f
TFilePath getMyDocumentsPath() {
Jeremy Bullock e59b1f
  QString documentsPath =
Jeremy Bullock e59b1f
      QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation)[0];
Jeremy Bullock e59b1f
  return TFilePath(documentsPath);
Jeremy Bullock e59b1f
}
Jeremy Bullock e59b1f
Jeremy Bullock e59b1f
// Desktop Path
Jeremy Bullock e59b1f
TFilePath getDesktopPath() {
Jeremy Bullock e59b1f
  QString desktopPath =
Jeremy Bullock e59b1f
      QStandardPaths::standardLocations(QStandardPaths::DesktopLocation)[0];
Jeremy Bullock e59b1f
  return TFilePath(desktopPath);
Jeremy Bullock e59b1f
}
shun-iwasawa 7f4819
}  // namespace
Jeremy Bullock e59b1f
//-------------------------------------------------------------------
Jeremy Bullock e59b1f
Shinya Kitaoka 120a6e
TFilePath ToonzFolder::getModulesDir() {
Shinya Kitaoka 120a6e
  return getProfileFolder() + "layouts";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePathSet ToonzFolder::getProjectsFolders() {
Jeremy Bullock e59b1f
  int location = Preferences::instance()->getProjectRoot();
Jeremy Bullock e59b1f
  QString path = Preferences::instance()->getCustomProjectRoot();
Jeremy Bullock e59b1f
  TFilePathSet fps;
Jeremy Bullock e59b1f
  int projectPaths = Preferences::instance()->getProjectRoot();
Jeremy Bullock e59b1f
  bool stuff       = projectPaths & 0x08;
Jeremy Bullock e59b1f
  bool documents   = projectPaths & 0x04;
Jeremy Bullock e59b1f
  bool desktop     = projectPaths & 0x02;
Jeremy Bullock e59b1f
  bool custom      = projectPaths & 0x01;
Jeremy Bullock e59b1f
Jeremy Bullock e59b1f
  // make sure at least something is there
Jeremy Bullock e59b1f
  if (!desktop && !custom && !documents) stuff = 1;
Jeremy Bullock e59b1f
  TFilePathSet tempFps =
Shinya Kitaoka 120a6e
      getSystemVarPathSetValue(getSystemVarPrefix() + "PROJECTS");
Jeremy Bullock e59b1f
  if (stuff) {
Jeremy Bullock e59b1f
    for (TFilePath tempPath : tempFps) {
Jeremy Bullock e59b1f
      if (TSystem::doesExistFileOrLevel(TFilePath(tempPath))) {
Jeremy Bullock e59b1f
        fps.push_back(TFilePath(tempPath));
Jeremy Bullock e59b1f
      }
Jeremy Bullock e59b1f
    }
shun-iwasawa e3b189
    if (tempFps.size() == 0)
shun-iwasawa e3b189
      fps.push_back(TEnv::getStuffDir() +
shun-iwasawa e3b189
                    TEnv::getSystemPathMap().at("PROJECTS"));
Jeremy Bullock e59b1f
  }
Jeremy Bullock e59b1f
  if (documents) {
Jeremy Bullock e59b1f
    fps.push_back(getMyDocumentsPath() + "OpenToonz");
Jeremy Bullock e59b1f
    if (!TSystem::doesExistFileOrLevel(getMyDocumentsPath() + "OpenToonz")) {
Jeremy Bullock e59b1f
      TSystem::mkDir(getMyDocumentsPath() + "OpenToonz");
Jeremy Bullock e59b1f
    }
Jeremy Bullock e59b1f
  }
Jeremy Bullock e59b1f
  if (desktop) {
Jeremy Bullock e59b1f
    fps.push_back(getDesktopPath() + "OpenToonz");
Jeremy Bullock e59b1f
    if (!TSystem::doesExistFileOrLevel(getDesktopPath() + "OpenToonz")) {
Jeremy Bullock e59b1f
      TSystem::mkDir(getDesktopPath() + "OpenToonz");
Jeremy Bullock e59b1f
    }
Jeremy Bullock e59b1f
  }
Jeremy Bullock e59b1f
  if (custom) {
Jeremy Bullock e59b1f
    QStringList paths = path.split("**");
Jeremy Bullock e59b1f
    for (QString tempPath : paths) {
Jeremy Bullock e59b1f
      if (TSystem::doesExistFileOrLevel(TFilePath(tempPath))) {
Jeremy Bullock e59b1f
        fps.push_back(TFilePath(tempPath));
Jeremy Bullock e59b1f
      }
Jeremy Bullock e59b1f
    }
Jeremy Bullock e59b1f
  }
Shinya Kitaoka 120a6e
  return fps;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath ToonzFolder::getFirstProjectsFolder() {
Shinya Kitaoka 120a6e
  TFilePathSet fps = getProjectsFolders();
Shinya Kitaoka 120a6e
  if (fps.empty())
Shinya Kitaoka 120a6e
    return TFilePath();
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return *fps.begin();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath ToonzFolder::getLibraryFolder() {
Shinya Kitaoka 120a6e
  TFilePath fp = getSystemVarPathValue(getSystemVarPrefix() + "LIBRARY");
shun-iwasawa e3b189
  if (fp == TFilePath())
shun-iwasawa e3b189
    fp = getStuffDir() + TEnv::getSystemPathMap().at("LIBRARY");
Shinya Kitaoka 120a6e
  return fp;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath ToonzFolder::getStudioPaletteFolder() {
Shinya Kitaoka 120a6e
  TFilePath fp = getSystemVarPathValue(getSystemVarPrefix() + "STUDIOPALETTE");
shun-iwasawa e3b189
  if (fp == TFilePath())
shun-iwasawa e3b189
    fp = getStuffDir() + TEnv::getSystemPathMap().at("STUDIOPALETTE");
Shinya Kitaoka 120a6e
  return fp;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath ToonzFolder::getFxPresetFolder() {
Shinya Kitaoka 120a6e
  TFilePath fp = getSystemVarPathValue(getSystemVarPrefix() + "FXPRESETS");
shun-iwasawa e3b189
  if (fp == TFilePath())
shun-iwasawa e3b189
    fp = getStuffDir() + TEnv::getSystemPathMap().at("FXPRESETS");
Shinya Kitaoka 120a6e
  return fp;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath ToonzFolder::getCacheRootFolder() {
shun-iwasawa 7f4819
  static enum STATE { FIRSTTIME, OK, NG } state = FIRSTTIME;
shun-iwasawa 7f4819
  QString cacheDir =
shun-iwasawa 7f4819
      QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
shun-iwasawa 7f4819
  if (state == FIRSTTIME) {
shun-iwasawa 7f4819
    if (QDir(cacheDir).mkpath("."))
shun-iwasawa 7f4819
      state = OK;
shun-iwasawa 7f4819
    else
shun-iwasawa 7f4819
      state = NG;
shun-iwasawa 7f4819
  }
shun-iwasawa 7f4819
  return (state == OK) ? TFilePath(cacheDir) : TFilePath();
justburner a66c51
}
justburner a66c51
justburner a66c51
TFilePath ToonzFolder::getCrashReportFolder() {
justburner a66c51
  static enum STATE { FIRSTTIME, OK, NG } state = FIRSTTIME;
justburner a66c51
  QString crashDir =
justburner a66c51
      QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) +
justburner a66c51
      "/crash";
justburner a66c51
  if (state == FIRSTTIME) {
justburner a66c51
    if (QDir(crashDir).mkpath("."))
justburner a66c51
      state = OK;
justburner a66c51
    else
justburner a66c51
      state = NG;
justburner a66c51
  }
justburner a66c51
  return (state == OK) ? TFilePath(crashDir) : TFilePath();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath ToonzFolder::getProfileFolder() {
Shinya Kitaoka 120a6e
  TFilePath fp = getSystemVarPathValue(getSystemVarPrefix() + "PROFILES");
shun-iwasawa e3b189
  if (fp == TFilePath())
shun-iwasawa e3b189
    fp = getStuffDir() + TEnv::getSystemPathMap().at("PROFILES");
Shinya Kitaoka 120a6e
  return fp;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath ToonzFolder::getReslistPath(bool forCleanup) {
Jeremy Bullock 1d47b8
  return getConfigDir() + (forCleanup ? "cleanupreslist.txt" : "reslist.txt");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath ToonzFolder::getTemplateModuleDir() {
Shinya Kitaoka 120a6e
  // return getModulesDir() + getModuleName();
Shinya Kitaoka 120a6e
  return getModulesDir() + "settings";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath ToonzFolder::getMyModuleDir() {
Shinya Kitaoka 120a6e
  TFilePath fp(getTemplateModuleDir());
Shinya Kitaoka 120a6e
  return fp.withName(fp.getWideName() + L"." +
Shinya Kitaoka 120a6e
                     TSystem::getUserName().toStdWString());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath ToonzFolder::getModuleFile(TFilePath filename) {
Shinya Kitaoka 120a6e
  TFilePath fp = getMyModuleDir() + filename;
Shinya Kitaoka 120a6e
  if (TFileStatus(fp).doesExist()) return fp;
Shinya Kitaoka 120a6e
  fp = getTemplateModuleDir() + filename;
Shinya Kitaoka 120a6e
  return fp;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath ToonzFolder::getModuleFile(std::string fn) {
Shinya Kitaoka 120a6e
  return ToonzFolder::getModuleFile(TFilePath(fn));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
// turtle
Shinya Kitaoka 120a6e
TFilePath ToonzFolder::getRoomsDir() {
Shinya Kitaoka 120a6e
  return getProfileFolder() + "layouts/rooms";
turtletooth 8d9ba3
}
turtletooth 8d9ba3
Shinya Kitaoka 120a6e
TFilePath ToonzFolder::getTemplateRoomsDir() {
Shinya Kitaoka 120a6e
  return getRoomsDir() +
Shinya Kitaoka 120a6e
         Preferences::instance()->getCurrentRoomChoice().toStdWString();
Shinya Kitaoka 120a6e
  // TFilePath fp(getMyModuleDir() + TFilePath(mySettingsFileName));
Shinya Kitaoka 120a6e
  // return getRoomsDir() + getModuleName();
turtletooth 8d9ba3
}
turtletooth 8d9ba3
Shinya Kitaoka 120a6e
TFilePath ToonzFolder::getMyRoomsDir() {
Shinya Kitaoka 120a6e
  // TFilePath fp(getTemplateRoomsDir());
Shinya Kitaoka 120a6e
  TFilePath fp(getProfileFolder());
Shinya Kitaoka 120a6e
  return fp.withName(
Shinya Kitaoka 120a6e
      fp.getWideName() + L"/layouts/personal/" +
Shinya Kitaoka 120a6e
      Preferences::instance()->getCurrentRoomChoice().toStdWString() + L"." +
Shinya Kitaoka 120a6e
      TSystem::getUserName().toStdWString());
turtletooth 8d9ba3
}
turtletooth 8d9ba3
Shinya Kitaoka 120a6e
TFilePath ToonzFolder::getRoomsFile(TFilePath filename) {
Shinya Kitaoka 120a6e
  TFilePath fp = getMyRoomsDir() + filename;
Shinya Kitaoka 120a6e
  if (TFileStatus(fp).doesExist()) return fp;
Shinya Kitaoka 120a6e
  fp = getTemplateRoomsDir() + filename;
Shinya Kitaoka 120a6e
  return fp;
turtletooth 8d9ba3
}
turtletooth 8d9ba3
Shinya Kitaoka 120a6e
TFilePath ToonzFolder::getRoomsFile(std::string fn) {
Shinya Kitaoka 120a6e
  return ToonzFolder::getRoomsFile(TFilePath(fn));
turtletooth 8d9ba3
}
turtletooth 8d9ba3
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
FolderListenerManager::FolderListenerManager() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
FolderListenerManager::~FolderListenerManager() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
FolderListenerManager *FolderListenerManager::instance() {
Shinya Kitaoka 120a6e
  static FolderListenerManager _instance;
Shinya Kitaoka 120a6e
  return &_instance;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void FolderListenerManager::notifyFolderChanged(const TFilePath &path) {
Shinya Kitaoka 120a6e
  for (std::set<listener *="">::iterator i = m_listeners.begin();</listener>
Shinya Kitaoka 120a6e
       i != m_listeners.end(); ++i)
Shinya Kitaoka 120a6e
    (*i)->onFolderChanged(path);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void FolderListenerManager::addListener(Listener *listener) {
Shinya Kitaoka 120a6e
  m_listeners.insert(listener);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void FolderListenerManager::removeListener(Listener *listener) {
Shinya Kitaoka 120a6e
  m_listeners.erase(listener);
Toshihiro Shimizu 890ddd
}