- Implemented comm module properly
[dashstudio.git] / src / dashserver / project / store.cpp
blob20624b86c962bdf5152f6c909ec8f20e6446b027
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 "store.h"
24 #include <QVariant>
25 #include <QDir>
27 #include <dcore/debug.h>
28 #include <dcore/algorithm.h>
30 #include "dash/projectsaver.h"
32 #include "project.h"
33 #include "dashserver/connection.h"
34 #include "dashserver/xdbms/manager.h"
35 #include "dashserver/xdbms/table.h"
37 #include "dash/network/package/transfer.h"
39 namespace DashServer {
40 namespace Project {
42 struct Store::Private
44 QHash<QString, Project *> projects;
48 Store::Store() : d(new Private)
53 Store::~Store()
55 qDeleteAll(d->projects);
56 delete d;
59 QStringList Store::projects() const
61 XDBMS::Table *table = XDBMS::Manager::self()->table("projects");
63 return table->objects();
66 QList<Project *> Store::allProjects() const
68 QList<Project *> projects;
70 XDBMS::Table *table = XDBMS::Manager::self()->table("projects");
71 foreach(QString projectName, table->objects())
73 Project *project = new Project;
74 project->setId(projectName);
75 table->load(project);
77 project->sync();
78 project->setProjectName(projectName);
80 projects << project;
84 return projects;
87 bool Store::createProject(const QString &name, const QString &author, const QString &description)
89 if( !d->projects.contains(name) )
91 Project *project = new Project;
92 project->setProjectName(name);
93 project->setAuthor(author);
94 project->setDescription(description);
95 project->setOpen(true);
97 d->projects[name] = project;
99 project->keep();
100 XDBMS::Manager::self()->addObject("projects", project);
102 return project->save();
105 return false;
108 void Store::addToProject(const QString &projectName, DashServer::Connection *cnx)
110 if( d->projects.contains(projectName) )
112 Project *project = d->projects.value(projectName);
114 cnx->setData(ProjectName, projectName);
115 project->addClient(cnx);
119 bool Store::contains(const QString &projectName) const
121 return d->projects.contains(projectName);
124 bool Store::execute(const QString &xml, DashServer::Connection *source)
126 QString projectName = source->data(ProjectName).toString();
128 if( d->projects.contains(projectName) )
130 Project *project = d->projects.value(projectName);
131 project->execute(xml, source);
133 return true;
136 return false;
139 bool Store::openProject(const QString &projectName, DashServer::Connection *source)
141 if( !d->projects.contains(projectName) )
143 Project *project = new Project;
144 project->setId(projectName);
146 if( ! XDBMS::Manager::self()->load("projects", project) ) return false;
148 project->sync();
149 if( ! project->load() )
150 qFatal("=(");
152 d->projects[projectName] = project;
156 Project *project = d->projects.value(projectName);
158 if( project->save() )
160 project->sendToClient(source);
161 addToProject(projectName, source);
164 return true;
167 return false;
170 QList<Project *> Store::activeProjects() const
172 return d->projects.values();
175 void Store::removeClient(DashServer::Connection *client)
177 QString projectName = client->data(ProjectName).toString();
179 if( d->projects.contains(projectName) )
181 Project *project = d->projects.value(projectName);
182 project->removeClient(client);
184 if( project->clientCount() == 0 )
186 d->projects.remove(projectName);
187 delete project;
192 void Store::sendToMembers(const QString &projectName, const QString &pkg)
194 if( d->projects.contains(projectName) )
196 Project *project = d->projects.value(projectName);
197 foreach(DashServer::Connection *cnx, project->members())
199 cnx->sendToClient(pkg, false);
204 void Store::resendProject(const QString &projectName)
206 if( d->projects.contains(projectName) )
208 Project *project = d->projects.value(projectName);
209 project->sendToAll();