Improve group support in timeline
[dashstudio.git] / src / dash / projectsaver.cpp
blob97903cf2adadba0572ca0573cda2858d3157fc9f
1 /***************************************************************************
2 * Copyright (C) 2007 by David Cuadrado *
3 * krawek@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
22 #include "projectsaver.h"
24 // Qt
25 #include <QDir>
27 // DLib
28 #include <dcore/zipper.h>
29 #include <dcore/algorithm.h>
30 #include <dcore/debug.h>
32 // YAMF
33 #include <yamf/model/project.h>
34 #include <yamf/model/scene.h>
35 #include <yamf/model/frame.h>
36 #include <yamf/model/library.h>
37 #include <yamf/model/command/manager.h>
39 namespace Dash {
41 static bool deleteProjectDir(const QString &projectDir)
43 if(!projectDir.startsWith(".") && QFile::exists( projectDir ) )
45 QDir dir(projectDir);
47 if( (dir.exists("audio") && dir.exists("video") && dir.exists("images")) || dir.exists("project") )
49 dDebug() << "Removing " << dir.absolutePath() << "...";
51 dir.remove("project");
52 dir.remove("library");
54 foreach(QString scene, dir.entryList(QStringList() << "scene*", QDir::Files ))
56 dir.remove(scene);
59 foreach( QString subdir, QStringList() << "audio" << "video" << "images" << "svg" )
61 if( dir.exists(subdir) )
63 dir.cd(subdir);
64 foreach(QString file, dir.entryList() )
66 QString absolute = dir.absolutePath() + "/" + file;
68 if( !file.startsWith(".") )
70 QFileInfo finfo(absolute);
72 if( finfo.isFile() )
74 QFile::remove(absolute);
78 dir.cdUp();
79 dir.rmdir(subdir);
83 if( ! dir.rmdir(dir.absolutePath()) )
85 dError("project") << "Cannot remove project data directory!";
88 return true;
91 return false;
95 ProjectSaver::ProjectSaver()
100 ProjectSaver::~ProjectSaver()
104 bool ProjectSaver::save(YAMF::Model::Project *project, const QString &path)
106 QDir projectDir = QDir::tempPath()+"/dash."+QString::number(DCore::Algorithm::random());
108 if ( !projectDir.exists() )
110 if ( ! projectDir.mkpath(projectDir.absolutePath()) )
112 dError() << "Cannot create: " << projectDir.absolutePath();
113 return false;
117 QString current = QDir::currentPath();
118 QDir::setCurrent(projectDir.absolutePath());
120 QStringList filesToZip;
122 // Save project
123 QFile prj(projectDir.absolutePath()+"/project");
125 if ( prj.open(QIODevice::WriteOnly | QIODevice::Text))
127 QTextStream ts(&prj);
129 QDomDocument doc;
130 doc.appendChild(project->toXml(doc));
132 ts << doc.toString();
134 prj.close();
136 QFileInfo finfo(prj);
137 filesToZip << finfo.absoluteFilePath();
141 // Save scenes
143 int index = 0;
144 foreach ( YAMF::Model::Scene *scene, project->scenes().visualValues() )
146 QDomDocument doc;
147 doc.appendChild(scene->toXml(doc));
149 QFile scn(projectDir.path()+"/scene"+QString::number(index));
151 if ( scn.open(QIODevice::WriteOnly | QIODevice::Text) )
153 QTextStream st(&scn);
154 st << doc.toString();
155 scn.close();
156 index++;
158 QFileInfo finfo(scn);
159 filesToZip << finfo.absoluteFilePath();
164 // Save library
166 QString libraryPath = projectDir.path()+"/library";
167 project->library()->save(libraryPath);
169 filesToZip << libraryPath;
171 QStringList dirs;
172 dirs << "audio" << "video" << "images" << "svg";
174 foreach(QString dir, dirs)
176 QString path = dir;
177 if( QFile::exists(projectDir.absoluteFilePath(dir)) )
179 filesToZip << path;
184 DCore::Zipper zipper;
186 D_SHOW_VAR(QDir::currentPath());
187 bool ok = zipper.write(filesToZip, path);
188 QDir::setCurrent(current);
190 if( ! ok )
191 return false;
193 deleteProjectDir(projectDir.absolutePath());
195 return true;
198 bool ProjectSaver::load(YAMF::Model::Project *project, const QString &path)
200 QDir projectDir = QDir::tempPath()+"/dash."+QString::number(DCore::Algorithm::random());
202 DCore::Zipper zipper;
204 if( ! zipper.read(path, projectDir.absolutePath() ) )
205 return false;
207 project->clear();
208 project->setOpen(false);
210 // Load project
211 if( projectDir.exists("project") )
213 QFile prj(projectDir.absoluteFilePath("project"));
215 if ( prj.open(QIODevice::ReadOnly | QIODevice::Text) )
217 project->fromXml(QString::fromLocal8Bit(prj.readAll()));
218 prj.close();
222 // Cargar libreria!
224 project->loadLibrary(projectDir.path()+"/library");
227 // Load scenes
229 QStringList scenes = projectDir.entryList(QStringList() << "scene*", QDir::Readable | QDir::Files);
231 foreach(QString scenePath, scenes)
233 scenePath = projectDir.absoluteFilePath(scenePath);
234 YAMF::Model::Scene *scene = project->createScene();
236 QFile f(scenePath);
238 if ( f.open(QIODevice::ReadOnly | QIODevice::Text) )
240 QString xml = QString::fromLocal8Bit(f.readAll());
241 scene->fromXml(xml);
243 f.close();
248 project->setOpen(true);
249 deleteProjectDir(projectDir.absolutePath());
251 project->commandManager()->clear();
253 return true;