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>
41 static bool deleteProjectDir(const QString
&projectDir
)
43 if(!projectDir
.startsWith(".") && QFile::exists( 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
))
59 foreach( QString subdir
, QStringList() << "audio" << "video" << "images" << "svg" )
61 if( dir
.exists(subdir
) )
64 foreach(QString file
, dir
.entryList() )
66 QString absolute
= dir
.absolutePath() + "/" + file
;
68 if( !file
.startsWith(".") )
70 QFileInfo
finfo(absolute
);
74 QFile::remove(absolute
);
83 if( ! dir
.rmdir(dir
.absolutePath()) )
85 dError("project") << "Cannot remove project data directory!";
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();
117 QString current
= QDir::currentPath();
118 QDir::setCurrent(projectDir
.absolutePath());
120 QStringList filesToZip
;
123 QFile
prj(projectDir
.absolutePath()+"/project");
125 if ( prj
.open(QIODevice::WriteOnly
| QIODevice::Text
))
127 QTextStream
ts(&prj
);
130 doc
.appendChild(project
->toXml(doc
));
132 ts
<< doc
.toString();
136 QFileInfo
finfo(prj
);
137 filesToZip
<< finfo
.absoluteFilePath();
144 foreach ( YAMF::Model::Scene
*scene
, project
->scenes().visualValues() )
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();
158 QFileInfo
finfo(scn
);
159 filesToZip
<< finfo
.absoluteFilePath();
166 QString libraryPath
= projectDir
.path()+"/library";
167 project
->library()->save(libraryPath
);
169 filesToZip
<< libraryPath
;
172 dirs
<< "audio" << "video" << "images" << "svg";
174 foreach(QString dir
, dirs
)
177 if( QFile::exists(projectDir
.absoluteFilePath(dir
)) )
184 DCore::Zipper zipper
;
186 D_SHOW_VAR(QDir::currentPath());
187 bool ok
= zipper
.write(filesToZip
, path
);
188 QDir::setCurrent(current
);
193 deleteProjectDir(projectDir
.absolutePath());
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() ) )
208 project
->setOpen(false);
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()));
224 project
->loadLibrary(projectDir
.path()+"/library");
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();
238 if ( f
.open(QIODevice::ReadOnly
| QIODevice::Text
) )
240 QString xml
= QString::fromLocal8Bit(f
.readAll());
248 project
->setOpen(true);
249 deleteProjectDir(projectDir
.absolutePath());
251 project
->commandManager()->clear();