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
)
141 d
->networkHandler
->reset();
144 d
->project
->setOpen(false);
147 Dash::Project
*ProjectManager::project() const
152 void ProjectManager::registerModule(Dash::Module
*module
)
154 d
->modules
<< module
;
157 bool ProjectManager::aboutToRedo(YAMF::Command::Base
*command
)
164 // QString xml = command->toXml();
166 // TODO: Enviar por socket
167 // d->networkHandler->send(xml);
168 d
->networkHandler
->execute(command
);
173 bool ProjectManager::aboutToUndo(YAMF::Command::Base
*command
)
180 // QString xml = command->toXml();
182 // TODO: Enviar por socket
183 // d->networkHandler->send(xml);
184 qFatal("Undo is not supported yet!");
189 void ProjectManager::executed(const YAMF::Command::Base
*command
)
193 Dash::CommandParser parser
;
194 Dash::DataSource
*source
= parser
.build(command
);
196 Q_ASSERT(source
!= 0);
203 Q_ASSERT(source
->type() != 0);
205 foreach(Dash::Module
*module
, d
->modules
)
207 if( module
->sources() & source
->type() )
209 module
->update(source
, false);
219 void ProjectManager::unexecuted(const YAMF::Command::Base
*command
)
223 Dash::CommandParser parser
;
225 Dash::DataSource
*source
= parser
.build(command
);
227 if( !source
) return;
229 foreach(Dash::Module
*module
, d
->modules
)
231 if( module
->sources() & source
->type() )
233 module
->update(source
, true);
242 bool ProjectManager::isModified() const
244 return d
->commandCounter
> 0;
247 void ProjectManager::setLocal(bool l
)
252 bool ProjectManager::isLocal() const
257 void ProjectManager::showChatWindow()
259 if( !d
->isLocal
&& d
->networkHandler
)
261 d
->networkHandler
->showChatWindow();
265 DGui::Osd::self()->display(tr("Cannot open a chat window working on a local project..."), DGui::Osd::Error
);
269 void ProjectManager::addPackageObserver(Dash::Network::Package::Observer
*observer
)
271 d
->networkHandler
->addObserver(observer
);
274 void ProjectManager::removePackageObserver(Dash::Network::Package::Observer
*observer
)
276 d
->networkHandler
->removeObserver(observer
);
279 NetworkHandler
*ProjectManager::networkHandler() const
281 return d
->networkHandler
;
284 void ProjectManager::connectToServer(const QString
&host
, quint16 port
, const QString
&login
, const QString
&password
)
286 delete d
->networkHandler
;
287 d
->networkHandler
= new NetworkHandler(this);
288 d
->networkHandler
->connectToHost(host
, port
, login
, password
);
291 bool ProjectManager::connectToServer()
293 ConnectDialog dialog
;
295 if( dialog
.exec() == QDialog::Accepted
)
297 connectToServer(dialog
.server(), dialog
.port(), dialog
.login(), dialog
.password());
304 void ProjectManager::openCollaborativeProject()
308 d
->networkHandler
->openProject();
311 bool ProjectManager::saveProject(const QString
&path
)
315 if( Dash::ProjectSaver::save(d
->project
, path
) )
317 d
->commandCounter
= 0;
331 bool ProjectManager::loadProject(const QString
&path
)
335 if( Dash::ProjectSaver::load(d
->project
, path
) )
337 d
->commandCounter
= 0;