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 "projectmanager.h"
24 #include "networkhandler.h"
27 #include <dcore/algorithm.h>
28 #include <dcore/zipper.h>
33 #include <yamf/model/scene.h>
34 #include <yamf/model/layer.h>
35 #include <yamf/model/audiolayer.h>
36 #include <yamf/model/frame.h>
37 #include <yamf/model/object.h>
38 #include <yamf/model/library.h>
40 #include <yamf/model/command/manager.h>
41 #include <yamf/model/command/base.h>
44 #include "dash/project.h"
45 #include "dash/module.h"
46 #include "dash/commandparser.h"
47 #include "dash/projectsaver.h"
49 #include "connectdialog.h"
57 #define CHECK_CONNECTION {bool ok = false; if( d->networkHandler ) if( d->networkHandler->isValid() ) ok = true; if( !ok ) { DGui::Osd::self()->display(QObject::tr("You are not connected to server."), DGui::Osd::Error ); return; }}
59 #define CONNECT_TO_SERVER {bool ok = false; if( d->networkHandler ) if( d->networkHandler->isOpen() ) ok = true; if( !ok ) { if(!connectToServer()) return; }}
61 ProjectParams::ProjectParams() : isLocal(true), port(5123)
65 ProjectParams::~ProjectParams()
70 struct ProjectManager::Private
72 Private() : isLocal(true), project(0), commandCounter(0), networkHandler(0) {}
75 Dash::Project
*project
;
77 QList
<Dash::Module
*> modules
;
81 NetworkHandler
*networkHandler
;
84 ProjectManager::ProjectManager(QObject
*parent
)
85 : QObject(parent
), d(new Private
)
87 d
->project
= new Dash::Project(this);
88 d
->project
->commandManager()->setObserver(this);
92 ProjectManager::~ProjectManager()
94 delete d
->networkHandler
;
98 void ProjectManager::createProject(const ProjectParams
¶ms
)
101 d
->project
->setProjectName(params
.projectName
);
102 d
->project
->setAuthor(params
.author
);
103 d
->project
->setDescription(params
.description
);
105 d
->isLocal
= params
.isLocal
;
107 if( !d
->isLocal
) // FIXME: Do not create the project
109 this->connectToServer(params
.server
, params
.port
, params
.login
, params
.password
);
111 d
->networkHandler
->newProject(¶ms
);
114 d
->project
->setOpen(true);
117 void ProjectManager::createScene(const QString
&name
, int frames
)
119 if( ! d
->project
->isOpen() || !d
->isLocal
) return;
121 YAMF::Model::Scene
*scn
= d
->project
->createScene(-1, name
);
122 YAMF::Model::Layer
*lyr
= scn
->createLayer();
124 if( frames
< 1 ) frames
= 1;
130 void ProjectManager::closeProject()
134 foreach(Dash::Module
*module
, d
->modules
)
139 d
->project
->setOpen(false);
142 Dash::Project
*ProjectManager::project() const
147 void ProjectManager::registerModule(Dash::Module
*module
)
149 d
->modules
<< module
;
152 bool ProjectManager::aboutToRedo(YAMF::Command::Base
*command
)
159 // QString xml = command->toXml();
161 // TODO: Enviar por socket
162 // d->networkHandler->send(xml);
163 d
->networkHandler
->execute(command
);
168 bool ProjectManager::aboutToUndo(YAMF::Command::Base
*command
)
175 // QString xml = command->toXml();
177 // TODO: Enviar por socket
178 // d->networkHandler->send(xml);
179 qFatal("Undo is not supported yet!");
184 void ProjectManager::executed(const YAMF::Command::Base
*command
)
188 Dash::CommandParser parser
;
189 Dash::DataSource
*source
= parser
.build(command
);
191 Q_ASSERT(source
!= 0);
198 Q_ASSERT(source
->type() != 0);
200 foreach(Dash::Module
*module
, d
->modules
)
202 if( module
->sources() & source
->type() )
204 module
->update(source
, false);
214 void ProjectManager::unexecuted(const YAMF::Command::Base
*command
)
218 Dash::CommandParser parser
;
220 Dash::DataSource
*source
= parser
.build(command
);
222 if( !source
) return;
224 foreach(Dash::Module
*module
, d
->modules
)
226 if( module
->sources() & source
->type() )
228 module
->update(source
, true);
237 bool ProjectManager::isModified() const
239 return d
->commandCounter
> 0;
242 void ProjectManager::setLocal(bool l
)
247 bool ProjectManager::isLocal() const
252 void ProjectManager::showChatWindow()
254 if( !d
->isLocal
&& d
->networkHandler
)
256 d
->networkHandler
->showChatWindow();
260 DGui::Osd::self()->display(tr("Cannot open a chat window working on a local project..."), DGui::Osd::Error
);
264 void ProjectManager::connectToServer(const QString
&host
, quint16 port
, const QString
&login
, const QString
&password
)
266 delete d
->networkHandler
;
267 d
->networkHandler
= new NetworkHandler(this);
268 d
->networkHandler
->connectToHost(host
, port
, login
, password
);
271 bool ProjectManager::connectToServer()
273 ConnectDialog dialog
;
275 if( dialog
.exec() == QDialog::Accepted
)
277 connectToServer(dialog
.server(), dialog
.port(), dialog
.login(), dialog
.password());
284 void ProjectManager::openCollaborativeProject()
288 d
->networkHandler
->openProject();
291 bool ProjectManager::saveProject(const QString
&path
)
295 if( Dash::ProjectSaver::save(d
->project
, path
) )
297 d
->commandCounter
= 0;
311 bool ProjectManager::loadProject(const QString
&path
)
314 d
->project
->setOpen(false);
316 if( Dash::ProjectSaver::load(d
->project
, path
) )
318 d
->commandCounter
= 0;