Modified palette file format
[dashstudio.git] / src / dash / projectsaver.cpp
blobc2d0abeeb4a401cfd12836aacbd62169fd6345e5
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 // Dash
40 #include "dash/project.h"
42 namespace Dash {
44 static bool deleteProjectDir(const QString &projectDir)
46 if(!projectDir.startsWith(".") && QFile::exists( projectDir ) )
48 QDir dir(projectDir);
50 if( (dir.exists("audio") && dir.exists("video") && dir.exists("images")) || dir.exists("project") )
52 dDebug() << "Removing " << dir.absolutePath() << "...";
54 dir.remove("project");
55 dir.remove("library");
56 dir.remove("palette");
58 foreach(QString scene, dir.entryList(QStringList() << "scene*", QDir::Files ))
60 dir.remove(scene);
63 foreach( QString subdir, QStringList() << "audio" << "video" << "images" << "svg" )
65 if( dir.exists(subdir) )
67 dir.cd(subdir);
68 foreach(QString file, dir.entryList() )
70 QString absolute = dir.absolutePath() + "/" + file;
72 if( !file.startsWith(".") )
74 QFileInfo finfo(absolute);
76 if( finfo.isFile() )
78 QFile::remove(absolute);
82 dir.cdUp();
83 dir.rmdir(subdir);
87 if( ! dir.rmdir(dir.absolutePath()) )
89 dError("project") << "Cannot remove project data directory!";
92 return true;
95 return false;
99 ProjectSaver::ProjectSaver()
104 ProjectSaver::~ProjectSaver()
108 bool ProjectSaver::save(Dash::Project *project, const QString &path)
110 QDir projectDir = QDir::tempPath()+"/dash."+QString::number(DCore::Algorithm::random());
112 if ( !projectDir.exists() )
114 if ( ! projectDir.mkpath(projectDir.absolutePath()) )
116 dError() << "Cannot create: " << projectDir.absolutePath();
117 return false;
121 QString current = QDir::currentPath();
122 QDir::setCurrent(projectDir.absolutePath());
124 QStringList filesToZip;
126 // Save project
127 QFile prj(projectDir.absolutePath()+"/project");
129 if ( prj.open(QIODevice::WriteOnly | QIODevice::Text))
131 QTextStream ts(&prj);
133 QDomDocument doc;
134 doc.appendChild(project->toXml(doc));
136 ts << doc.toString();
138 prj.close();
140 QFileInfo finfo(prj);
141 filesToZip << finfo.absoluteFilePath();
145 // Save scenes
147 int index = 0;
148 foreach ( YAMF::Model::Scene *scene, project->scenes().visualValues() )
150 QDomDocument doc;
151 doc.appendChild(scene->toXml(doc));
153 QFile scn(projectDir.path()+"/scene"+QString::number(index));
155 if ( scn.open(QIODevice::WriteOnly | QIODevice::Text) )
157 QTextStream st(&scn);
158 st << doc.toString();
159 scn.close();
160 index++;
162 QFileInfo finfo(scn);
163 filesToZip << finfo.absoluteFilePath();
168 // Save library
170 QString libraryPath = projectDir.path()+"/library";
171 project->library()->save(libraryPath);
173 filesToZip << libraryPath;
175 QStringList dirs;
176 dirs << "audio" << "video" << "images" << "svg";
178 foreach(QString dir, dirs)
180 QString path = dir;
181 if( QFile::exists(projectDir.absoluteFilePath(dir)) )
183 filesToZip << path;
188 // Save palette
190 if( QFile::copy(project->projectPalette(), projectDir.path()+"/palette") )
192 filesToZip << "palette";
196 DCore::Zipper zipper;
198 D_SHOW_VAR(QDir::currentPath());
199 bool ok = zipper.write(filesToZip, path);
200 QDir::setCurrent(current);
202 if( ! ok )
203 return false;
205 deleteProjectDir(projectDir.absolutePath());
207 return true;
210 bool ProjectSaver::load(Dash::Project *project, const QString &path)
212 QDir projectDir = QDir::tempPath()+"/dash."+QString::number(DCore::Algorithm::random());
214 DCore::Zipper zipper;
216 if( ! zipper.read(path, projectDir.absolutePath() ) )
217 return false;
219 project->clear();
220 project->setOpen(false);
222 // Load project
223 if( projectDir.exists("project") )
225 QFile prj(projectDir.absoluteFilePath("project"));
227 if ( prj.open(QIODevice::ReadOnly | QIODevice::Text) )
229 project->fromXml(QString::fromLocal8Bit(prj.readAll()));
230 prj.close();
234 // Cargar libreria!
236 project->loadLibrary(projectDir.path()+"/library");
239 // Load scenes
241 QStringList scenes = projectDir.entryList(QStringList() << "scene*", QDir::Readable | QDir::Files);
243 foreach(QString scenePath, scenes)
245 scenePath = projectDir.absoluteFilePath(scenePath);
246 YAMF::Model::Scene *scene = project->createScene();
248 QFile f(scenePath);
250 if ( f.open(QIODevice::ReadOnly | QIODevice::Text) )
252 QString xml = QString::fromLocal8Bit(f.readAll());
253 scene->fromXml(xml);
255 f.close();
260 // Load palette
262 QString path = projectDir.path()+"/palette";
263 if( QFile::exists( path ) )
265 project->importPalette(path);
269 project->setOpen(true);
270 deleteProjectDir(projectDir.absolutePath());
272 project->commandManager()->clear();
274 return true;