New class Dash::Project
[dashstudio.git] / src / shell / projectmanager.cpp
blob77a9d51a54c4713bba4c19e794ee46129664ef04
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 "projectmanager.h"
24 #include "networkhandler.h"
26 // DLIB
27 #include <dcore/algorithm.h>
28 #include <dcore/zipper.h>
30 #include <dgui/osd.h>
32 // YAMF
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>
43 // DASH
44 #include "dash/project.h"
45 #include "dash/module.h"
46 #include "dash/commandparser.h"
47 #include "dash/projectsaver.h"
49 #include "connectdialog.h"
51 // Qt
52 #include <QWidget>
53 #include <QDir>
54 #include <QFileInfo>
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) {}
74 bool isLocal;
75 Dash::Project *project;
77 QList<Dash::Module *> modules;
79 int commandCounter;
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;
95 delete d;
98 void ProjectManager::createProject(const ProjectParams &params)
100 d->project->clear();
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(&params);
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;
126 while(frames--)
127 lyr->createFrame();
130 void ProjectManager::closeProject()
132 d->project->clear();
134 foreach(Dash::Module *module, d->modules)
136 module->reset();
139 d->project->setOpen(false);
142 Dash::Project *ProjectManager::project() const
144 return d->project;
147 void ProjectManager::registerModule(Dash::Module *module)
149 d->modules << module;
152 bool ProjectManager::aboutToRedo(YAMF::Command::Base *command)
154 if( d->isLocal )
156 return false;
159 // QString xml = command->toXml();
161 // TODO: Enviar por socket
162 // d->networkHandler->send(xml);
163 d->networkHandler->execute(command);
165 return true;
168 bool ProjectManager::aboutToUndo(YAMF::Command::Base *command)
170 if( d->isLocal )
172 return false;
175 // QString xml = command->toXml();
177 // TODO: Enviar por socket
178 // d->networkHandler->send(xml);
179 qFatal("Undo is not supported yet!");
181 return true;
184 void ProjectManager::executed(const YAMF::Command::Base *command)
186 if( d->isLocal )
188 Dash::CommandParser parser;
189 Dash::DataSource *source = parser.build(command);
191 Q_ASSERT(source != 0);
193 if( !source )
195 return;
198 Q_ASSERT(source->type() != 0);
200 foreach(Dash::Module *module, d->modules)
202 if( module->sources() & source->type() )
204 module->update(source, false);
208 delete source;
210 d->commandCounter++;
214 void ProjectManager::unexecuted(const YAMF::Command::Base *command)
216 if( d->isLocal )
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);
232 d->commandCounter--;
233 delete source;
237 bool ProjectManager::isModified() const
239 return d->commandCounter > 0;
242 void ProjectManager::setLocal(bool l)
244 d->isLocal = l;
247 bool ProjectManager::isLocal() const
249 return d->isLocal;
252 void ProjectManager::showChatWindow()
254 if( !d->isLocal && d->networkHandler)
256 d->networkHandler->showChatWindow();
258 else
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());
278 return true;
281 return false;
284 void ProjectManager::openCollaborativeProject()
286 CONNECT_TO_SERVER;
288 d->networkHandler->openProject();
291 bool ProjectManager::saveProject(const QString &path)
293 if( d->isLocal )
295 if( Dash::ProjectSaver::save(d->project, path) )
297 d->commandCounter = 0;
298 return true;
300 else
302 return false;
306 // TODO: Network
308 return false;
311 bool ProjectManager::loadProject(const QString &path)
313 d->project->clear();
314 d->project->setOpen(false);
316 if( Dash::ProjectSaver::load(d->project, path) )
318 d->commandCounter = 0;
320 return true;
323 return false;