| |
| |
| #include "toonz/tproject.h" |
| |
| |
| #include "toonz/sceneproperties.h" |
| #include "toonz/toonzscene.h" |
| #include "toonz/txsheet.h" |
| #include "toonz/observer.h" |
| #include "toonz/toonzfolders.h" |
| #include "toonz/cleanupparameters.h" |
| |
| |
| #include "tenv.h" |
| |
| |
| #include "tsystem.h" |
| #include "tstream.h" |
| #include "tfilepath_io.h" |
| #include "tconvert.h" |
| |
| |
| #include <QFileInfo> |
| #include <QDir> |
| |
| |
| #include <fstream> |
| #include <stdlib.h> |
| |
| using namespace std; |
| |
| |
| |
| |
| const std::wstring prjSuffix[4] = {L"_otprj", L"_prj63ml", L"_prj6", L"_prj"}; |
| const std::wstring xmlExt = L".xml"; |
| const int prjSuffixCount = 4; |
| |
| |
| |
| const string |
| TProject::Inputs = "inputs", |
| |
| TProject::Drawings = "drawings", |
| |
| TProject::Scenes = "scenes", |
| |
| TProject::Scripts = "scripts", |
| |
| TProject::Extras = "extras", |
| |
| TProject::Outputs = "outputs", |
| |
| TProject::Palettes = "palettes"; |
| |
| const TFilePath |
| TProject::SandboxProjectName("sandbox"); |
| |
| TProjectP currentProject; |
| |
| |
| |
| namespace |
| { |
| |
| |
| |
| |
| |
| |
| |
| TFilePath makeRelative(TFilePath ref, TFilePath fp) |
| { |
| if (!fp.isAbsolute()) |
| return fp; |
| TFilePath dots; |
| for (;;) { |
| if (ref.isAncestorOf(fp)) { |
| TFilePath relativePath = dots + (fp - ref); |
| return relativePath; |
| } |
| if (ref.isRoot()) |
| return fp; |
| ref = ref.getParentDir(); |
| dots = dots + ".."; |
| } |
| } |
| |
| |
| |
| TFilePath makeAbsolute(TFilePath ref, TFilePath fp) |
| { |
| if (fp.isAbsolute()) |
| return fp; |
| const TFilePath twoDots(".."); |
| while (twoDots.isAncestorOf(fp)) { |
| TFilePath refParent = ref.getParentDir(); |
| if (refParent == TFilePath()) |
| break; |
| ref = refParent; |
| fp = fp - twoDots; |
| } |
| fp = ref + fp; |
| |
| return fp; |
| } |
| |
| |
| |
| TEnv::StringVar currentProjectPath("CurrentProject", ""); |
| |
| |
| |
| |
| |
| std::wstring getProjectSuffix(const TFilePath &path) |
| { |
| const std::wstring &name = path.getWideName(); |
| int idx = name.find_last_of(L'_'); |
| if (idx == string::npos) |
| return L""; |
| |
| return name.substr(idx); |
| } |
| |
| |
| |
| |
| |
| |
| TFilePath getLatestVersionProjectPath(const TFilePath &path) |
| { |
| const std::wstring &suffix = getProjectSuffix(path); |
| for (int i = 1; i < prjSuffixCount; ++i) |
| if (suffix == prjSuffix[i]) { |
| const std::wstring &name = path.getWideName(); |
| int pos = name.size() - suffix.size(); |
| return path.withName(path.getWideName().substr(0, pos) + prjSuffix[0]); |
| } |
| |
| return path; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| TFilePath searchProjectPath(TFilePath folder) |
| { |
| assert(folder.isAbsolute()); |
| wstring projectName = folder.getWideName(); |
| |
| |
| TFilePath projectPath; |
| for (int i = 0; i < prjSuffixCount; ++i) { |
| projectPath = folder + TFilePath(projectName + prjSuffix[i] + xmlExt); |
| if (TFileStatus(projectPath).doesExist()) |
| return projectPath; |
| } |
| |
| |
| return folder + TFilePath(projectName + prjSuffix[0] + xmlExt); |
| } |
| |
| |
| |
| bool isFolderUnderVersionControl(const TFilePath &folderPath) |
| { |
| QDir dir(QString::fromStdWString(folderPath.getWideString())); |
| return dir.entryList(QDir::AllDirs | QDir::Hidden).contains(".svn"); |
| } |
| |
| |
| |
| void hideOlderProjectFiles(const TFilePath &folderPath) |
| { |
| const std::wstring &name = folderPath.getWideName(); |
| |
| TFilePath path; |
| for (int i = 1; i < prjSuffixCount; ++i) { |
| path = folderPath + (name + prjSuffix[i] + xmlExt); |
| if (TFileStatus(path).doesExist()) |
| TSystem::renameFile(path.withType("xml_"), path); |
| } |
| } |
| |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| TProject::TProject() |
| : m_name(), m_path(), m_sprop(new TSceneProperties()) |
| { |
| } |
| |
| |
| |
| TProject::~TProject() |
| { |
| delete m_sprop; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| void TProject::setFolder(string name, TFilePath path) |
| { |
| std::map<string, TFilePath>::iterator it; |
| it = m_folders.find(name); |
| if (it == m_folders.end()) { |
| m_folderNames.push_back(name); |
| m_folders[name] = path; |
| } else { |
| it->second = path; |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| void TProject::setFolder(string name) |
| { |
| setFolder(name, TFilePath(name)); |
| } |
| |
| |
| |
| |
| |
| |
| TFilePath TProject::getFolder(string name) const |
| { |
| std::map<string, TFilePath>::const_iterator it; |
| it = m_folders.find(name); |
| if (it != m_folders.end()) |
| return it->second; |
| else |
| return TFilePath(); |
| } |
| |
| |
| |
| |
| |
| TFilePath TProject::getScenesPath() const |
| { |
| TFilePath scenes = getFolder(Scenes); |
| return makeAbsolute(getProjectFolder(), scenes); |
| } |
| |
| |
| |
| |
| |
| |
| TFilePath TProject::getFolder(int index) const |
| { |
| if (0 <= index && index < (int)m_folderNames.size()) |
| return getFolder(m_folderNames[index]); |
| else |
| return TFilePath(); |
| } |
| |
| |
| |
| |
| |
| bool TProject::isConstantFolder(int index) const |
| { |
| TFilePath fp = getFolder(index); |
| return fp.getWideString().find(L"$scene") == wstring::npos; |
| } |
| |
| |
| |
| int TProject::getFolderCount() const |
| { |
| return m_folders.size(); |
| } |
| |
| |
| |
| string TProject::getFolderName(int index) const |
| { |
| if (0 <= index && index < (int)m_folderNames.size()) |
| return m_folderNames[index]; |
| else |
| return ""; |
| } |
| |
| |
| |
| |
| |
| |
| int TProject::getFolderIndex(string name) const |
| { |
| std::vector<string>::const_iterator it; |
| it = std::find( |
| m_folderNames.begin(), |
| m_folderNames.end(), |
| name); |
| if (it == m_folderNames.end()) |
| return -1; |
| return std::distance(it, m_folderNames.begin()); |
| } |
| |
| |
| |
| |
| |
| |
| bool TProject::isCurrent() const |
| { |
| TFilePath currentProjectPath = |
| TProjectManager::instance()->getCurrentProjectPath(); |
| if (getProjectPath() == currentProjectPath) |
| return true; |
| else |
| return getLatestVersionProjectPath(currentProjectPath) == |
| getLatestVersionProjectPath(getProjectPath()); |
| } |
| |
| |
| |
| |
| void TProject::setSceneProperties(const TSceneProperties &sprop) |
| { |
| m_sprop->assign(&sprop); |
| } |
| |
| |
| |
| |
| |
| |
| TFilePath TProject::decode(TFilePath fp) const |
| { |
| for (;;) { |
| wstring fpstr = fp.getWideString(); |
| int j = fpstr.find(L"$project"); |
| if (j == (int)wstring::npos) |
| break; |
| fpstr.replace(j, 8, getName().getWideString()); |
| fp = TFilePath(fpstr); |
| } |
| return makeAbsolute(getProjectFolder(), fp); |
| } |
| |
| |
| |
| void TProject::setUseScenePath(string folderName, bool on) |
| { |
| m_useScenePathFlags[folderName] = on; |
| } |
| |
| |
| |
| bool TProject::getUseScenePath(string folderName) const |
| { |
| std::map<string, bool>::const_iterator it; |
| it = m_useScenePathFlags.find(folderName); |
| return it != m_useScenePathFlags.end() ? it->second : false; |
| } |
| |
| |
| |
| |
| |
| int TProject::getFolderIndexFromPath(const TFilePath &folderDir) |
| { |
| TFilePath scenePath = decode(getFolder(Scenes)); |
| bool sceneDependentScenePath = false; |
| if (scenePath.getName().find("$scene") != string::npos) { |
| scenePath = scenePath.getParentDir(); |
| sceneDependentScenePath = true; |
| } |
| int folderIndex; |
| for (folderIndex = 0; folderIndex < getFolderCount(); folderIndex++) |
| if (isConstantFolder(folderIndex)) { |
| TFilePath fp = decode(getFolder(folderIndex)); |
| if (fp == folderDir) |
| return folderIndex; |
| } else { |
| TFilePath fp = decode(getFolder(folderIndex)); |
| wstring a = fp.getWideString(); |
| wstring b = folderDir.getWideString(); |
| int alen = a.length(); |
| int blen = b.length(); |
| int i = a.find(L"$scene"); |
| assert(i != (int)wstring::npos); |
| if (i == (int)wstring::npos) |
| continue; |
| int j = i + 1; |
| while (j < alen && isalnum(a[j])) |
| j++; |
| |
| int k = j + blen - alen; |
| if (!(0 <= i && i < k && k <= blen)) |
| continue; |
| assert(i < blen); |
| if (i > 0 && a.substr(0, i) != b.substr(0, i)) |
| continue; |
| if (k < blen && (j >= alen || a.substr(j) != b.substr(k))) |
| continue; |
| wstring v = b.substr(i, k - i); |
| TFilePath scene(v + L".tnz"); |
| if (sceneDependentScenePath) |
| scene = scenePath + scene.getWideName() + scene; |
| else |
| scene = scenePath + scene; |
| if (TFileStatus(scene).doesExist()) |
| return folderIndex; |
| } |
| return -1; |
| } |
| |
| |
| |
| |
| wstring TProject::getFolderNameFromPath(const TFilePath &folderDir) |
| { |
| int index = getFolderIndexFromPath(folderDir); |
| if (index < 0) |
| return L""; |
| if (getFolder(index).isAbsolute()) |
| return toWideString("+" + getFolderName(index)); |
| else |
| return folderDir.getWideName(); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| bool TProject::save(const TFilePath &projectPath) |
| { |
| assert(isAProjectPath(projectPath)); |
| |
| TProjectManager *pm = TProjectManager::instance(); |
| m_name = pm->projectPathToProjectName(projectPath); |
| m_path = getLatestVersionProjectPath(projectPath); |
| TFilePath projectFolder = projectPath.getParentDir(); |
| |
| if (!TFileStatus(projectFolder).doesExist()) { |
| try { |
| TSystem::mkDir(projectFolder); |
| } catch (...) { |
| return false; |
| } |
| } |
| |
| TFilePath sceneFolder = decode(getFolder(TProject::Scenes)); |
| TFilePath scenesDescPath = sceneFolder + "scenes.xml"; |
| |
| TFileStatus fs(projectPath); |
| if (fs.doesExist() && !fs.isWritable()) { |
| throw TSystemException(projectPath, "Cannot save the project settings. The file is read-only."); |
| return false; |
| } |
| TFileStatus fs2(scenesDescPath); |
| if (fs2.doesExist() && !fs2.isWritable()) { |
| throw TSystemException(projectPath, "Cannot save the project settings. The scenes file is read-only."); |
| return false; |
| } |
| |
| TOStream os(m_path); |
| os.openChild("project"); |
| os.openChild("version"); |
| os << 70 << 1; |
| os.closeChild(); |
| os.openChild("folders"); |
| int i = 0; |
| for (i = 0; i < getFolderCount(); i++) { |
| TFilePath folderRelativePath = getFolder(i); |
| if (folderRelativePath == TFilePath()) |
| continue; |
| std::map<string, string> attr; |
| string folderName = getFolderName(i); |
| attr["name"] = folderName; |
| attr["path"] = toString(folderRelativePath); |
| if (getUseScenePath(folderName)) |
| attr["useScenePath"] = "yes"; |
| os.openCloseChild("folder", attr); |
| } |
| os.closeChild(); |
| |
| os.openChild("sceneProperties"); |
| getSceneProperties().saveData(os); |
| os.closeChild(); |
| os.closeChild(); |
| |
| |
| for (i = 0; i < getFolderCount(); i++) |
| if (isConstantFolder(i)) { |
| TFilePath fp = getFolder(i); |
| if (fp == TFilePath()) |
| continue; |
| fp = decode(fp); |
| |
| if (!TFileStatus(fp).doesExist()) { |
| try { |
| TSystem::mkDir(fp); |
| } catch (...) { |
| } |
| } |
| } |
| |
| |
| std::vector<std::string> foldernames; |
| pm->getFolderNames(foldernames); |
| for (int f = 0; f < foldernames.size(); f++) { |
| TFilePath folderpath = decode(getFolder(foldernames.at(f))); |
| if (folderpath.isEmpty() |
| ||!isConstantFolder(f) ) |
| continue; |
| |
| TFilePath xmlPath = folderpath + "scenes.xml"; |
| TFileStatus xmlfs(xmlPath); |
| if (xmlfs.doesExist() && !xmlfs.isWritable()) |
| continue; |
| |
| TFilePath relativeProjectFolder = makeRelative(folderpath, m_path.getParentDir()); |
| |
| TOStream os2(xmlPath); |
| std::map<string, string> attr; |
| attr["type"] = "projectFolder"; |
| os2.openChild("parentProject", attr); |
| os2 << relativeProjectFolder; |
| os2.closeChild(); |
| } |
| |
| |
| |
| |
| if (!isFolderUnderVersionControl(projectFolder)) |
| hideOlderProjectFiles(projectFolder); |
| |
| return true; |
| } |
| |
| |
| |
| bool TProject::save() |
| { |
| return save(m_path); |
| } |
| |
| |
| |
| |
| |
| void TProject::load(const TFilePath &projectPath) |
| { |
| assert(isAProjectPath(projectPath)); |
| |
| TFilePath latestProjectPath = getLatestVersionProjectPath(projectPath); |
| TFilePath inputProjectPath = searchProjectPath(projectPath.getParentDir()); |
| |
| TProjectManager *pm = TProjectManager::instance(); |
| m_name = pm->projectPathToProjectName(latestProjectPath); |
| m_path = latestProjectPath; |
| |
| m_folderNames.clear(); |
| m_folders.clear(); |
| m_useScenePathFlags.clear(); |
| delete m_sprop; |
| m_sprop = new TSceneProperties(); |
| |
| |
| TIStream is(inputProjectPath); |
| if (!is) |
| return; |
| |
| string tagName; |
| if (!is.matchTag(tagName) || tagName != "project") |
| return; |
| |
| while (is.matchTag(tagName)) { |
| if (tagName == "folders") { |
| while (is.matchTag(tagName)) { |
| if (tagName == "folder") { |
| string name = is.getTagAttribute("name"); |
| TFilePath path(is.getTagAttribute("path")); |
| setFolder(name, path); |
| string useScenePath = is.getTagAttribute("useScenePath"); |
| setUseScenePath(name, useScenePath == "yes"); |
| } else |
| throw TException("expected <folder>"); |
| } |
| is.matchEndTag(); |
| } else if (tagName == "version") { |
| int major, minor; |
| is >> major >> minor; |
| is.setVersion(VersionNumber(major, minor)); |
| is.matchEndTag(); |
| } else if (tagName == "sceneProperties") { |
| TSceneProperties sprop; |
| try { |
| sprop.loadData(is, true); |
| } catch (...) { |
| } |
| setSceneProperties(sprop); |
| is.matchEndTag(); |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| bool TProject::isAProjectPath(const TFilePath &fp) |
| { |
| if (fp.isAbsolute() && fp.getType() == "xml") { |
| const std::wstring &fpName = fp.getWideName(); |
| const std::wstring &folderName = fp.getParentDir().getWideName(); |
| for (int i = 0; i < prjSuffixCount; ++i) |
| if (fpName == (folderName + prjSuffix[i])) |
| return true; |
| } |
| |
| return false; |
| } |
| |
| |
| |
| namespace |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| TProjectManager::TProjectManager() |
| : m_tabMode(false), m_tabKidsMode(false) |
| { |
| } |
| |
| |
| |
| TProjectManager::~TProjectManager() |
| { |
| } |
| |
| |
| |
| |
| TProjectManager *TProjectManager::instance() |
| { |
| static TProjectManager _instance; |
| return &_instance; |
| } |
| |
| |
| |
| |
| |
| void TProjectManager::addProjectsRoot(const TFilePath &root) |
| { |
| |
| if (std::find(m_projectsRoots.begin(), m_projectsRoots.end(), root) == m_projectsRoots.end()) |
| m_projectsRoots.push_back(root); |
| } |
| |
| |
| |
| |
| |
| |
| void TProjectManager::addSVNProjectsRoot(const TFilePath &root) |
| { |
| assert(TFileStatus(root).isDirectory()); |
| if (std::find(m_svnProjectsRoots.begin(), m_svnProjectsRoots.end(), root) == m_svnProjectsRoots.end()) |
| m_svnProjectsRoots.push_back(root); |
| } |
| |
| |
| |
| void TProjectManager::addDefaultProjectsRoot() |
| { |
| addProjectsRoot(TEnv::getStuffDir() + "projects"); |
| } |
| |
| |
| |
| TFilePath TProjectManager::getCurrentProjectRoot() |
| { |
| TFilePath currentProjectPath = getCurrentProjectPath(); |
| int i; |
| for (i = 0; i < (int)m_projectsRoots.size(); i++) |
| if (m_projectsRoots[i].isAncestorOf(currentProjectPath)) |
| return m_projectsRoots[i]; |
| for (i = 0; i < (int)m_svnProjectsRoots.size(); i++) |
| if (m_svnProjectsRoots[i].isAncestorOf(currentProjectPath)) |
| return m_svnProjectsRoots[i]; |
| if (m_projectsRoots.empty()) |
| addDefaultProjectsRoot(); |
| return m_projectsRoots[0]; |
| } |
| |
| |
| |
| |
| |
| TFilePath TProjectManager::projectPathToProjectName(const TFilePath &projectPath) |
| { |
| assert(projectPath.isAbsolute()); |
| TFilePath projectFolder = projectPath.getParentDir(); |
| if (m_projectsRoots.empty()) |
| addDefaultProjectsRoot(); |
| int i; |
| for (i = 0; i < (int)m_projectsRoots.size(); i++) { |
| if (m_projectsRoots[i].isAncestorOf(projectFolder)) |
| return projectFolder - m_projectsRoots[i]; |
| } |
| for (i = 0; i < (int)m_svnProjectsRoots.size(); i++) { |
| if (m_svnProjectsRoots[i].isAncestorOf(projectFolder)) |
| return projectFolder - m_svnProjectsRoots[i]; |
| } |
| |
| return projectFolder.withoutParentDir(); |
| } |
| |
| |
| |
| |
| TFilePath TProjectManager::projectNameToProjectPath(const TFilePath &projectName) |
| { |
| assert(!TProject::isAProjectPath(projectName)); |
| assert(!projectName.isAbsolute()); |
| if (m_projectsRoots.empty()) |
| addDefaultProjectsRoot(); |
| if (projectName == TProject::SandboxProjectName) |
| return searchProjectPath(TEnv::getStuffDir() + projectName); |
| return searchProjectPath(m_projectsRoots[0] + projectName); |
| } |
| |
| |
| |
| |
| TFilePath TProjectManager::projectFolderToProjectPath(const TFilePath &projectFolder) |
| { |
| assert(projectFolder.isAbsolute()); |
| return searchProjectPath(projectFolder); |
| } |
| |
| |
| |
| |
| |
| TFilePath TProjectManager::getProjectPathByName(const TFilePath &projectName) |
| { |
| assert(!TProject::isAProjectPath(projectName)); |
| assert(!projectName.isAbsolute()); |
| |
| if (m_projectsRoots.empty()) |
| addDefaultProjectsRoot(); |
| if (projectName == TProject::SandboxProjectName) |
| return searchProjectPath(TEnv::getStuffDir() + projectName); |
| int i, n = (int)m_projectsRoots.size(); |
| for (i = 0; i < n; i++) { |
| TFilePath projectPath = searchProjectPath(m_projectsRoots[i] + projectName); |
| assert(TProject::isAProjectPath(projectPath)); |
| if (TFileStatus(projectPath).doesExist()) |
| return projectPath; |
| } |
| for (i = 0; i < (int)m_svnProjectsRoots.size(); i++) { |
| TFilePath projectPath = searchProjectPath(m_svnProjectsRoots[i] + projectName); |
| assert(TProject::isAProjectPath(projectPath)); |
| if (TFileStatus(projectPath).doesExist()) |
| return projectPath; |
| } |
| return TFilePath(); |
| } |
| |
| |
| |
| |
| |
| void TProjectManager::getFolderNames(std::vector<string> &names) |
| { |
| names.clear(); |
| TFilePath fp = ToonzFolder::getProfileFolder() + "project_folders.txt"; |
| try { |
| Tifstream is(fp); |
| if (is) |
| for (;;) { |
| char buffer[1024]; |
| is.getline(buffer, sizeof(buffer)); |
| if (is.eof()) |
| break; |
| char *s = buffer; |
| while (*s == ' ' || *s == '\t') |
| s++; |
| char *t = s; |
| while (*t && *t != '\r' && *t != '\n') |
| t++; |
| while (t > s && (t[-1] == ' ' || t[-1] == '\t')) |
| t--; |
| t[0] = '\0'; |
| if (s[0]) |
| names.push_back(string(s)); |
| } |
| } catch (...) { |
| } |
| const string stdNames[] = { |
| TProject::Inputs, TProject::Drawings, TProject::Scenes, |
| TProject::Extras, TProject::Outputs, TProject::Scripts}; |
| for (int i = 0; i < (int)tArrayCount(stdNames); i++) { |
| string name = stdNames[i]; |
| |
| if (std::find(names.begin(), names.end(), name) == names.end()) |
| names.push_back(name); |
| } |
| } |
| |
| |
| |
| |
| void TProjectManager::setCurrentProjectPath(const TFilePath &fp) |
| { |
| assert(TProject::isAProjectPath(fp)); |
| currentProjectPath = toString(fp.getWideString()); |
| currentProject = TProjectP(); |
| notifyListeners(); |
| } |
| |
| |
| |
| |
| |
| |
| TFilePath TProjectManager::getCurrentProjectPath() |
| { |
| TFilePath fp(currentProjectPath); |
| if (fp == TFilePath()) |
| fp = projectNameToProjectPath(TProject::SandboxProjectName); |
| if (!TProject::isAProjectPath(fp)) { |
| |
| if (!fp.isAbsolute()) |
| fp = getProjectPathByName(fp); |
| } |
| fp = searchProjectPath(fp.getParentDir()); |
| if (!TFileStatus(fp).doesExist()) |
| fp = projectNameToProjectPath(TProject::SandboxProjectName); |
| fp = getLatestVersionProjectPath(fp); |
| string s = toString(fp); |
| if (s != (string)currentProjectPath) |
| currentProjectPath = s; |
| return fp; |
| } |
| |
| |
| |
| |
| |
| TProjectP TProjectManager::getCurrentProject() |
| { |
| if (currentProject.getPointer() == 0) { |
| TFilePath fp = getCurrentProjectPath(); |
| assert(TProject::isAProjectPath(fp)); |
| currentProject = new TProject(); |
| currentProject->load(fp); |
| } |
| return currentProject; |
| } |
| |
| |
| |
| |
| |
| |
| TProjectP TProjectManager::loadSceneProject(const TFilePath &scenePath) |
| { |
| |
| |
| |
| |
| TFilePath folder = scenePath.getParentDir(); |
| TFilePath sceneDesc; |
| bool found = true; |
| for (;;) { |
| sceneDesc = folder + "scenes.xml"; |
| if (TFileStatus(sceneDesc).doesExist()) |
| break; |
| if (folder.isRoot()) { |
| found = false; |
| break; |
| } |
| folder = folder.getParentDir(); |
| } |
| |
| |
| TFilePath projectPath; |
| if (found) { |
| try { |
| TIStream is(sceneDesc); |
| string tagName; |
| is.matchTag(tagName); |
| string type = is.getTagAttribute("type"); |
| TFilePath projectFolderPath; |
| is >> projectFolderPath; |
| if (type == "") { |
| projectFolderPath = TFilePath(".."); |
| } |
| is.matchEndTag(); |
| projectPath = makeAbsolute(folder, projectFolderPath); |
| |
| TFilePath path; |
| for (int i = 0; i < prjSuffixCount; ++i) { |
| path = projectPath + (projectPath.getWideName() + prjSuffix[i] + xmlExt); |
| if (TFileStatus(path).doesExist()) |
| break; |
| } |
| |
| projectPath = path; |
| |
| } catch (...) { |
| } |
| if (projectPath == TFilePath()) |
| return 0; |
| } else |
| projectPath = getSandboxProjectPath(); |
| |
| if (!TProject::isAProjectPath(projectPath)) { |
| |
| if (!projectPath.isAbsolute()) |
| projectPath = getProjectPathByName(projectPath); |
| else |
| return 0; |
| } |
| if (!TFileStatus(projectPath).doesExist()) |
| return 0; |
| |
| TProject *project = new TProject(); |
| project->load(projectPath); |
| return project; |
| } |
| |
| |
| |
| void TProjectManager::notifyListeners() |
| { |
| for (std::set<Listener *>::iterator i = |
| m_listeners.begin(); |
| i != m_listeners.end(); |
| ++i) |
| (*i)->onProjectSwitched(); |
| } |
| |
| |
| |
| void TProjectManager::notifyProjectChanged() |
| { |
| for (std::set<Listener *>::iterator i = |
| m_listeners.begin(); |
| i != m_listeners.end(); |
| ++i) |
| (*i)->onProjectChanged(); |
| } |
| |
| |
| |
| void TProjectManager::addListener(Listener *listener) |
| { |
| m_listeners.insert(listener); |
| } |
| |
| |
| |
| void TProjectManager::removeListener(Listener *listener) |
| { |
| m_listeners.erase(listener); |
| } |
| |
| |
| |
| |
| |
| void TProjectManager::initializeScene(ToonzScene *scene) |
| { |
| TProject *project = scene->getProject(); |
| TSceneProperties *sprop = scene->getProperties(); |
| |
| TFilePath currentProjectPath = getCurrentProjectPath(); |
| project->load(currentProjectPath); |
| |
| sprop->assign(&project->getSceneProperties()); |
| CleanupParameters::GlobalParameters.assign(project->getSceneProperties().getCleanupParameters()); |
| |
| |
| scene->setUntitled(); |
| sprop->cloneCamerasTo(scene->getTopXsheet()->getStageObjectTree()); |
| sprop->onInitialize(); |
| |
| } |
| |
| |
| |
| void TProjectManager::saveTemplate(ToonzScene *scene) |
| { |
| TSceneProperties props; |
| props.assign(scene->getProperties()); |
| props.cloneCamerasFrom(scene->getXsheet()->getStageObjectTree()); |
| |
| TProjectP currentProject = getCurrentProject(); |
| currentProject->setSceneProperties(props); |
| currentProject->save(); |
| } |
| |
| |
| |
| void TProjectManager::createSandboxIfNeeded() |
| { |
| TFilePath path = getSandboxProjectPath(); |
| if (!TFileStatus(path).doesExist()) { |
| TProjectP project = createStandardProject(); |
| try { |
| project->save(path); |
| } catch (...) { |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| TProjectP TProjectManager::createStandardProject() |
| { |
| TProject *project = new TProject(); |
| |
| std::vector<std::string> names; |
| getFolderNames(names); |
| std::vector<std::string>::iterator it; |
| for (it = names.begin(); it != names.end(); ++it) |
| project->setFolder(*it); |
| return project; |
| } |
| |
| |
| TFilePath TProjectManager::getSandboxProjectFolder() |
| { |
| return getSandboxProjectPath().getParentDir(); |
| } |
| |
| TFilePath TProjectManager::getSandboxProjectPath() |
| { |
| return getProjectPathByName(TProject::SandboxProjectName); |
| } |
| |
| bool TProjectManager::isProject(const TFilePath &projectFolder) |
| { |
| TFilePath projectPath = projectFolderToProjectPath(projectFolder); |
| return TFileStatus(projectPath).doesExist(); |
| } |
| |