- Implemented query and update project
[dashstudio.git] / src / dashserver / project / project.cpp
blobff9b790fa2f1238347ffce11a8c303e0b403bac5
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 "project.h"
24 #include <QSet>
26 #include <yamf/model/command/processor.h>
28 #include "dashserver/connection.h"
29 #include "dashserver/xdbms/manager.h"
31 #include "dash/projectsaver.h"
32 #include "dash/network/package/transfer.h"
34 #include <dcore/debug.h>
36 namespace DashServer {
37 namespace Project {
39 struct Project::Private
41 YAMF::Command::Processor *processor;
43 QSet<DashServer::Connection *> connections;
45 QSet<QString> designers;
46 QSet<QString> owners;
49 Project::Project() : YAMF::Model::Project(), d(new Private)
51 d->processor = new YAMF::Command::Processor(this);
55 Project::~Project()
57 delete d->processor;
58 delete d;
61 void Project::execute(const QString &xml, DashServer::Connection *source)
63 if( d->connections.contains(source) )
65 if( d->processor->execute(xml) )
67 QString tosend = source->signXml(xml);
69 foreach(DashServer::Connection *cnx, d->connections)
71 cnx->sendToClient( tosend, false );
74 else
76 source->sendToClient( "<command />", true );
79 else
81 qWarning("INVALID SOURCE");
85 void Project::addClient(DashServer::Connection *cnx)
87 d->connections << cnx;
90 void Project::removeClient(DashServer::Connection *client)
92 d->connections.remove(client);
94 if( d->connections.isEmpty() )
96 save();
100 void Project::sendToClient(DashServer::Connection *client)
102 // FIXME: Poner en un thread
104 QFile f(this->fileName());
105 Dash::Network::Package::Transfer transfer(f.size());
106 client->sendToClient(transfer.toString(), true);
108 if( f.open(QIODevice::ReadOnly) )
110 while(!f.atEnd())
112 client->sendBinaryData(f.read(1024));
118 void Project::sendToAll()
120 save();
123 QFile f(this->fileName());
125 foreach(DashServer::Connection *client, d->connections)
127 Dash::Network::Package::Transfer transfer(f.size());
128 client->sendToClient(transfer.toString(), true);
131 if( f.open(QIODevice::ReadOnly) )
133 while(!f.atEnd())
135 foreach(DashServer::Connection *client, d->connections)
137 client->sendBinaryData(f.read(1024));
144 QSet<DashServer::Connection *> Project::members() const
146 return d->connections;
149 int Project::clientCount() const
151 return d->connections.size();
154 void Project::keep()
156 if( projectName().isEmpty() ) return;
158 // QString id = projectName().toLower().replace(' ', '_');;
160 setId(projectName());
162 add("description", description());
163 add("author", author());
164 addList("owners", d->owners.toList());
165 addList("designers", d->designers.toList());
169 void Project::sync()
171 setProjectName(id());
172 setDescription(value("description"));
173 setAuthor(value("author"));
175 dfDebug << value("author");
177 foreach(QString owner, list("owners"))
179 addOwner(owner);
182 foreach(QString designer, list("designers"))
184 addDesigner(designer);
188 bool Project::save()
190 QString pname = projectName();
192 if( pname.isEmpty() ) return false;
194 return Dash::ProjectSaver::save(this, fileName());
197 bool Project::load()
199 QString pname = projectName();
201 if( pname.isEmpty() ) return false;
203 clear();
205 return Dash::ProjectSaver::load(this, fileName());
208 QString Project::fileName() const
210 QString pname = projectName();
212 if( pname.isEmpty() )
213 return QString();
215 return XDBMS::Manager::self()->databaseDir().absoluteFilePath(pname+".dsh");
218 QSet<QString> Project::designers() const
220 return d->designers;
223 void Project::addDesigner(const QString &name)
225 d->designers << name;
228 QSet<QString> Project::owners() const
230 return d->owners;
233 void Project::addOwner(const QString &name)
235 d->owners << name;