Revert "Working on network-undo"
[dashstudio.git] / src / shell / projectmanager.cpp
blob19521f0db19bbce5c9c10e79f28a707cbd3c5886
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 if( !d->isLocal )
141 d->networkHandler->reset();
144 d->project->setOpen(false);
147 Dash::Project *ProjectManager::project() const
149 return d->project;
152 void ProjectManager::registerModule(Dash::Module *module)
154 d->modules << module;
157 bool ProjectManager::aboutToRedo(YAMF::Command::Base *command)
159 if( d->isLocal )
161 return false;
164 // QString xml = command->toXml();
166 // TODO: Enviar por socket
167 // d->networkHandler->send(xml);
168 d->networkHandler->execute(command);
170 return true;
173 bool ProjectManager::aboutToUndo(YAMF::Command::Base *command)
175 if( d->isLocal )
177 return false;
180 // QString xml = command->toXml();
182 // TODO: Enviar por socket
183 // d->networkHandler->send(xml);
184 qFatal("Undo is not supported yet!");
186 return true;
189 void ProjectManager::executed(const YAMF::Command::Base *command)
191 if( d->isLocal )
193 Dash::CommandParser parser;
194 Dash::DataSource *source = parser.build(command);
196 Q_ASSERT(source != 0);
198 if( !source )
200 return;
203 Q_ASSERT(source->type() != 0);
205 foreach(Dash::Module *module, d->modules)
207 if( module->sources() & source->type() )
209 module->update(source, false);
213 delete source;
215 d->commandCounter++;
219 void ProjectManager::unexecuted(const YAMF::Command::Base *command)
221 if( d->isLocal )
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);
237 d->commandCounter--;
238 delete source;
242 bool ProjectManager::isModified() const
244 return d->commandCounter > 0;
247 void ProjectManager::setLocal(bool l)
249 d->isLocal = l;
252 bool ProjectManager::isLocal() const
254 return d->isLocal;
257 void ProjectManager::showChatWindow()
259 if( !d->isLocal && d->networkHandler)
261 d->networkHandler->showChatWindow();
263 else
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());
298 return true;
301 return false;
304 void ProjectManager::openCollaborativeProject()
306 CONNECT_TO_SERVER;
308 d->networkHandler->openProject();
311 bool ProjectManager::saveProject(const QString &path)
313 if( d->isLocal )
315 if( Dash::ProjectSaver::save(d->project, path) )
317 d->commandCounter = 0;
318 return true;
320 else
322 return false;
326 // TODO: Network
328 return false;
331 bool ProjectManager::loadProject(const QString &path)
333 closeProject();
335 if( Dash::ProjectSaver::load(d->project, path) )
337 d->commandCounter = 0;
339 return true;
342 return false;