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 ***************************************************************************/
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
{
39 struct Project::Private
41 YAMF::Command::Processor
*processor
;
43 QSet
<DashServer::Connection
*> connections
;
45 QSet
<QString
> designers
;
49 Project::Project() : YAMF::Model::Project(), d(new Private
)
51 d
->processor
= new YAMF::Command::Processor(this);
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 );
76 source
->sendToClient( "<command />", true );
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() )
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
) )
112 client
->sendBinaryData(f
.read(1024));
118 void Project::sendToAll()
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
) )
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();
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());
171 setProjectName(id());
172 setDescription(value("description"));
173 setAuthor(value("author"));
175 dfDebug
<< value("author");
177 foreach(QString owner
, list("owners"))
182 foreach(QString designer
, list("designers"))
184 addDesigner(designer
);
190 QString pname
= projectName();
192 if( pname
.isEmpty() ) return false;
194 return Dash::ProjectSaver::save(this, fileName());
199 QString pname
= projectName();
201 if( pname
.isEmpty() ) return false;
205 return Dash::ProjectSaver::load(this, fileName());
208 QString
Project::fileName() const
210 QString pname
= projectName();
212 if( pname
.isEmpty() )
215 return XDBMS::Manager::self()->databaseDir().absoluteFilePath(pname
+".dsh");
218 QSet
<QString
> Project::designers() const
223 void Project::addDesigner(const QString
&name
)
225 d
->designers
<< name
;
228 QSet
<QString
> Project::owners() const
233 void Project::addOwner(const QString
&name
)