Add members to project
[dashstudio.git] / src / dashserver / project / store.cpp
blobd838650655ead7de4eb4dfe40962fa990524e2f1
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 "member.h"
35 #include "dashserver/connection.h"
36 #include "dashserver/users/user.h"
37 #include "dashserver/xdbms/manager.h"
38 #include "dashserver/xdbms/table.h"
40 #include "dash/network/package/transfer.h"
41 #include "dash/network/package/connected.h"
42 #include "dash/network/package/disconnected.h"
44 namespace DashServer {
45 namespace Project {
47 struct Store::Private
49 QHash<QString, Project *> projects;
53 Store::Store() : d(new Private)
58 Store::~Store()
60 qDeleteAll(d->projects);
61 delete d;
64 QStringList Store::projects() const
66 XDBMS::Table *table = XDBMS::Manager::self()->table("projects");
68 return table->objects();
71 Project *Store::project(const QString& name)
73 XDBMS::Table *table = XDBMS::Manager::self()->table("projects");
75 Project *project = new Project;
76 project->setId(name);
77 if(table->load(project))
79 project->sync();
80 project->setProjectName(name);
81 return project;
83 return 0;
86 QList<Project *> Store::allProjects() const
88 QList<Project *> projects;
90 XDBMS::Table *table = XDBMS::Manager::self()->table("projects");
91 foreach(QString projectName, table->objects())
93 Project *project = new Project;
94 project->setId(projectName);
95 table->load(project);
97 project->sync();
98 project->setProjectName(projectName);
100 projects << project;
104 return projects;
107 bool Store::createProject(const QString &name, const QString &author, const QString &description, const QString& owner)
109 if( !d->projects.contains(name) )
111 Project *project = new Project;
112 project->setProjectName(name);
113 project->setAuthor(author);
114 project->setDescription(description);
117 Member *director = new Member;
118 director->setJob(QObject::tr("Director"));
119 director->setUser(owner);
120 director->addPermission(Project::All, Users::Permission::Read | Users::Permission::Write);
121 director->addPermission(Project::Scene, Users::Permission::Read | Users::Permission::Write);
122 director->addPermission(Project::Layer, Users::Permission::Read | Users::Permission::Write);
123 director->addPermission(Project::Frame, Users::Permission::Read | Users::Permission::Write);
124 director->addPermission(Project::Object, Users::Permission::Read | Users::Permission::Write);
126 project->addMember(director);
128 project->setOpen(true);
130 d->projects[name] = project;
132 project->keep();
133 XDBMS::Manager::self()->addObject("projects", project);
135 return project->save();
138 return false;
141 void Store::addToProject(const QString &projectName, DashServer::Connection *cnx)
143 if( d->projects.contains(projectName) )
145 Project *project = d->projects.value(projectName);
147 cnx->setData(ProjectName, projectName);
149 QStringList members;
150 foreach(DashServer::Connection *member, project->members())
152 Dash::Network::Package::Connected pkg(cnx->user()->login());
153 member->sendToClient(pkg.toString(), false);
155 members << member->user()->login();
158 project->addClient(cnx);
160 if( !members.isEmpty() )
162 Dash::Network::Package::Connected pkg(members);
163 dfDebug << pkg.toString();
164 cnx->sendToClient(pkg.toString(), true);
169 bool Store::contains(const QString &projectName) const
171 return d->projects.contains(projectName);
174 bool Store::execute(const QString &xml, DashServer::Connection *source)
176 QString projectName = source->data(ProjectName).toString();
178 if( d->projects.contains(projectName) )
180 Project *project = d->projects.value(projectName);
181 project->execute(xml, source);
183 return true;
186 return false;
189 bool Store::openProject(const QString &projectName, DashServer::Connection *source)
191 if( !d->projects.contains(projectName) )
193 Project *project = new Project;
194 project->setId(projectName);
196 if( ! XDBMS::Manager::self()->load("projects", project) ) return false;
198 project->sync();
199 if( ! project->load() )
200 qFatal("=(");
202 d->projects[projectName] = project;
206 Project *project = d->projects.value(projectName);
208 if( project->save() )
210 addToProject(projectName, source);
211 project->sendToClient(source);
214 return true;
217 return false;
220 QList<Project *> Store::activeProjects() const
222 return d->projects.values();
225 void Store::removeClient(DashServer::Connection *client)
227 QString projectName = client->data(ProjectName).toString();
229 if( d->projects.contains(projectName) )
231 Dash::Network::Package::Disconnected pkg(client->user()->login());
232 sendToMembers(projectName, pkg.toString());
234 Project *project = d->projects.value(projectName);
235 project->removeClient(client);
237 if( project->clientCount() == 0 )
239 d->projects.remove(projectName);
240 delete project;
245 void Store::sendToMembers(const QString &projectName, const QString &pkg)
247 if( d->projects.contains(projectName) )
249 Project *project = d->projects.value(projectName);
250 foreach(DashServer::Connection *cnx, project->members())
252 cnx->sendToClient(pkg, false);
257 void Store::resendProject(const QString &projectName)
259 if( d->projects.contains(projectName) )
261 Project *project = d->projects.value(projectName);
262 project->sendToAll();