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 ***************************************************************************/
21 #include "networkhandler.h"
24 #include <QXmlStreamReader>
25 #include <QXmlStreamWriter>
26 #include <QTemporaryFile>
29 #include <dcore/debug.h>
35 #include <yamf/model/command/processor.h>
36 #include <yamf/model/command/base.h>
39 #include "dash/project.h"
41 #include "dash/network/socket.h"
42 #include "dash/network/filereceiver.h"
44 #include "dash/network/package/newproject.h"
45 #include "dash/network/package/connect.h"
46 #include "dash/network/package/listprojects.h"
47 #include "dash/network/package/openproject.h"
49 #include "dash/network/parser/ackparser.h"
51 #include "dash/network/parser/projectlistparser.h"
52 #include "dash/network/parser/projectparser.h"
54 #include "projectmanager.h"
56 #include "ui_displayprojects.h"
58 #include "comm/comm.h"
59 #include "comm/chatwindow.h"
61 NetworkHandler::NetworkHandler(ProjectManager
*manager
, QObject
*parent
) : QObject(parent
), m_manager(manager
), m_socket(new Dash::Network::Socket
), m_dispatcher(new Dash::Network::Package::Dispatcher
), m_waitingResponse(false)
63 m_processor
= new YAMF::Command::Processor(manager
->project());
64 m_socket
->addObserver(this);
65 m_receiver
= new Dash::Network::FileReceiver
;
66 m_socket
->addObserver(m_receiver
);
68 m_comm
= new Comm(this);
71 connect(m_receiver
, SIGNAL(finished(const QString
&)), this, SLOT(onReceiverFinished(const QString
&)));
75 NetworkHandler::~NetworkHandler()
83 void NetworkHandler::connectToHost(const QString
&host
, quint16 port
, const QString
&login
, const QString
&password
)
87 m_socket
->connectToHost(host
, port
);
88 Dash::Network::Package::Connect
cnx(login
, password
, 0);
89 m_socket
->send(cnx
.toString().toLocal8Bit());
92 void NetworkHandler::send(const QString
&xml
)
95 if( !m_sign
.isEmpty() )
97 QXmlStreamReader
reader(xml
);
98 QXmlStreamWriter
writer(&tosend
);
101 while( ! reader
.atEnd() )
105 writer
.writeCurrentToken(reader
);
106 if( reader
.tokenType() == QXmlStreamReader::StartElement
&& !isRoot
)
108 dfDebug
<< "Writing sing on " << reader
.name().toString();
109 writer
.writeAttribute("sign", m_sign
);
119 dfDebug
<< "SENDING: " << tosend
<< " SIGN IS : " << m_sign
;
121 m_socket
->send(tosend
.toLocal8Bit());
124 void NetworkHandler::addObserver(Dash::Network::Package::Observer
*observer
)
126 m_dispatcher
->addObserver(observer
);
129 void NetworkHandler::removeObserver(Dash::Network::Package::Observer
*observer
)
131 m_dispatcher
->removeObserver(observer
);
134 void NetworkHandler::execute(YAMF::Command::Base
*cmd
)
136 bool isEmpty
= m_commands
.isEmpty();
138 dfDebug
<< "ENCOLANDO: " << cmd
->toXml();
140 m_commands
.enqueue(cmd
);
141 if( isEmpty
&& !m_waitingResponse
&& !m_sign
.isEmpty() )
143 m_waitingResponse
= true;
148 void NetworkHandler::newProject(const ProjectParams
*params
)
150 Dash::Network::Package::NewProject
newProjectPackage(params
->projectName
, params
->author
, params
->description
);
151 this->send(newProjectPackage
.toString());
154 void NetworkHandler::openProject()
156 Dash::Network::Package::ListProjects list
;
157 this->send(list
.toString());
160 void NetworkHandler::openProject(const QString
&projectName
)
162 m_currentPackage
= "openproject";
163 Dash::Network::Package::OpenProject
project(projectName
);
164 this->send(project
.toString());
167 bool NetworkHandler::isOpen() const
169 return m_socket
->state() == QAbstractSocket::ConnectedState
;
172 bool NetworkHandler::isValid() const
174 return this->isOpen() && !m_sign
.isEmpty();
177 void NetworkHandler::showChatWindow()
181 m_comm
->chatWindow()->show();
185 DGui::Osd::self()->display(tr("You are not authenticated."), DGui::Osd::Error
);
189 void NetworkHandler::readed(const QString
&txt
)
191 dfDebug
<< "READED: " << txt
;
196 QXmlStreamReader
reader(txt
);
197 while( !reader
.atEnd() )
199 if( reader
.readNext() == QXmlStreamReader::StartElement
)
201 root
= reader
.name().toString();
208 dWarning() << "Invalid package: " << txt
;
209 dWarning() << "Error was: " << reader
.errorString();
214 if( !root
.isEmpty() )
218 Dash::Network::Parser::AckParser parser
;
219 if( parser
.parse(txt
) )
221 m_sign
= parser
.sign();
222 DGui::Osd::self()->display(parser
.motd(), DGui::Osd::Info
);
224 if( !m_commands
.isEmpty() )
226 m_waitingResponse
= true;
227 send(m_commands
.first()->toXml());
231 else if( root
== "command" )
233 D_SHOW_VAR(m_commands
.size());
234 bool executed
= false;
236 if( m_waitingResponse
&& !m_commands
.isEmpty() )
238 m_waitingResponse
= false;
240 QXmlStreamReader
reader(txt
);
244 QString sign
= reader
.attributes().value("sign").toString();
248 m_manager
->setLocal(true);
249 m_commands
.dequeue()->redo(); // FIXME: add to stack?
250 m_manager
->setLocal(false);
254 if( ! m_commands
.isEmpty() )
256 m_waitingResponse
= true;
257 send(m_commands
.first()->toXml());
263 if( !executed
) // External command
265 m_manager
->setLocal(true);
266 m_processor
->execute(txt
);
267 m_manager
->setLocal(false);
270 else if( root
== "projectlist" )
272 Dash::Network::Parser::ProjectList projectList
;
274 if( projectList
.parse(txt
) )
277 Ui::DisplayProjects ui
;
280 foreach( Dash::Network::Parser::ProjectList::Project p
, projectList
.projects())
282 QTreeWidgetItem
*item
= new QTreeWidgetItem(ui
.projects
);
283 item
->setText(0, p
.name
);
284 item
->setText(1, p
.author
);
285 item
->setText(2, p
.description
);
288 if( choose
.exec() == QDialog::Accepted
)
290 if( QTreeWidgetItem
*ci
= ui
.projects
->currentItem() )
292 QString projectName
= ci
->text(0);
294 openProject(projectName
);
300 DGui::Osd::self()->display(QObject::tr("Error displaying the project list"), DGui::Osd::Error
);
303 else if( root
== "project" ) // FIXME: Remove
306 Dash::Network::Parser::Project projectParser
;
310 if( projectParser
.parse(txt
) && file
.open() )
312 file
.write(projectParser
.projectData());
315 m_manager
->setLocal(true);
316 m_manager
->loadProject(file
.fileName());
317 m_manager
->setLocal(false);
321 DGui::Osd::self()->display(QObject::tr("Error loading project"), DGui::Osd::Error
);
324 else if( root
== "transfer" )
326 QXmlStreamReader
reader(txt
);
327 while(! reader
.atEnd() )
329 if( reader
.readNext() == QXmlStreamReader::StartElement
)
331 int bytes
= reader
.attributes().value("bytes").toString().toInt();
333 m_socket
->setFormat(Dash::Network::Socket::Binary
);
334 m_receiver
->start(bytes
);
338 else if( root
== "connected" )
340 QXmlStreamReader
reader(txt
);
342 while(! reader
.atEnd() )
344 if( reader
.readNext() == QXmlStreamReader::StartElement
)
346 if(reader
.name() == "user")
348 logins
<< reader
.attributes().value("login").toString();
352 m_comm
->chatWindow()->addUsers(logins
);
354 else if( root
== "disconnected" )
358 QXmlStreamReader
reader(txt
);
359 while(! reader
.atEnd() )
361 if( reader
.readNext() == QXmlStreamReader::StartElement
)
363 if(reader
.name() == "user")
365 login
= reader
.attributes().value("login").toString();
371 m_comm
->chatWindow()->removeUser(login
);
375 m_dispatcher
->addSource(root
, txt
);
376 m_dispatcher
->process();
381 void NetworkHandler::dataReaded(const QByteArray
&data
)
390 void NetworkHandler::onReceiverFinished(const QString
&fileName
)
392 m_socket
->setFormat(Dash::Network::Socket::Text
);
394 dfDebug
<< m_currentPackage
<< " file: " << fileName
;
396 if( m_currentPackage
== "openproject" )
398 m_manager
->setLocal(true);
400 qDebug() << QFile::exists(fileName
);
402 if( !m_manager
->loadProject(fileName
) )
404 qFatal("Cannot load the project!");
406 m_manager
->setLocal(false);
407 QFile::remove(fileName
);