1 /***************************************************************************
2 * Copyright (C) 2007 by David Cuadrado *
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. *
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. *
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"
28 #include <dcore/zipper.h>
29 #include <dcore/algorithm.h>
30 #include <dcore/debug.h>
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>
40 #include "dash/project.h"
44 static bool deleteProjectDir(const QString
&projectDir
)
46 if(!projectDir
.startsWith(".") && QFile::exists( 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
))
63 foreach( QString subdir
, QStringList() << "audio" << "video" << "images" << "svg" )
65 if( dir
.exists(subdir
) )
68 foreach(QString file
, dir
.entryList() )
70 QString absolute
= dir
.absolutePath() + "/" + file
;
72 if( !file
.startsWith(".") )
74 QFileInfo
finfo(absolute
);
78 QFile::remove(absolute
);
87 if( ! dir
.rmdir(dir
.absolutePath()) )
89 dError("project") << "Cannot remove project data directory!";
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();
121 QString current
= QDir::currentPath();
122 QDir::setCurrent(projectDir
.absolutePath());
124 QStringList filesToZip
;
127 QFile
prj(projectDir
.absolutePath()+"/project");
129 if ( prj
.open(QIODevice::WriteOnly
| QIODevice::Text
))
131 QTextStream
ts(&prj
);
134 doc
.appendChild(project
->toXml(doc
));
136 ts
<< doc
.toString();
140 QFileInfo
finfo(prj
);
141 filesToZip
<< finfo
.absoluteFilePath();
148 foreach ( YAMF::Model::Scene
*scene
, project
->scenes().visualValues() )
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();
162 QFileInfo
finfo(scn
);
163 filesToZip
<< finfo
.absoluteFilePath();
170 QString libraryPath
= projectDir
.path()+"/library";
171 project
->library()->save(libraryPath
);
173 filesToZip
<< libraryPath
;
176 dirs
<< "audio" << "video" << "images" << "svg";
178 foreach(QString dir
, dirs
)
181 if( QFile::exists(projectDir
.absoluteFilePath(dir
)) )
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
);
205 deleteProjectDir(projectDir
.absolutePath());
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() ) )
220 project
->setOpen(false);
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()));
236 project
->loadLibrary(projectDir
.path()+"/library");
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();
250 if ( f
.open(QIODevice::ReadOnly
| QIODevice::Text
) )
252 QString xml
= QString::fromLocal8Bit(f
.readAll());
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();