- Support to get/send binary data in Network::Socket
[dashstudio.git] / src / dash / commandparser.cpp
blob3bcf6c622a71f5cf023f49cb9e20403c4dd5aafe
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 "commandparser.h"
24 #include <QXmlStreamReader>
25 #include <QXmlStreamWriter>
27 #include <yamf/model/command/base.h>
28 #include "datasource.h"
30 #include <dcore/debug.h>
32 namespace Dash {
34 struct CommandParser::Private
36 Private() : currentSource(0) {}
38 DataSource *currentSource;
39 QString commandName;
42 QStringList objectCommands;
45 CommandParser::CommandParser() : DCore::XmlParserBase(), d(new Private)
47 d->objectCommands << "addobject" << "removeobject" << "modifyitem" << "editnodesitem" << "convertitem" << "additemfilter" << "addtweening" << "bringforwardsitem" << "bringtofrontitem" << "sendtobackitem" << "sendbackwardsitem" << "groupitem" << "ungroupitem";
51 CommandParser::~CommandParser()
53 delete d;
56 bool CommandParser::startTag(const QString &tag, const QXmlAttributes &atts)
58 if( atts.count() > 0 )
60 DataSource::Values values;
62 for(int i = 0; i < atts.count(); i++)
64 QString qname = atts.qName(i);
65 values[qname] = atts.value(qname);
68 d->currentSource->setValues(tag, values);
71 if( tag == "command" )
73 d->commandName = atts.value("name");
75 if( d->objectCommands.contains(d->commandName) )
77 d->currentSource->setType(DataSource::Object);
79 else if( d->commandName == "insertscene" || d->commandName == "removescene" || d->commandName == "movescene" || d->commandName == "renamescene" )
81 d->currentSource->setType(DataSource::Scene);
83 else if( d->commandName == "insertlayer" || d->commandName == "removelayer" || d->commandName == "locklayer" || d->commandName == "changelayervisibility"|| d->commandName == "movelayer" || d->commandName == "renamelayer")
85 d->currentSource->setType(DataSource::Layer);
87 else if( d->commandName == "insertframe" || d->commandName == "removeframe" || d->commandName == "lockframe" || d->commandName == "changeframevisibility" || d->commandName == "moveframe" || d->commandName == "renameframe" || d->commandName == "expandframe" )
89 d->currentSource->setType(DataSource::Frame);
91 else if( d->commandName == "addlibraryobject" || d->commandName == "removelibraryobject"|| d->commandName == "modifysymbol" )
93 d->currentSource->setType(DataSource::Library);
97 return true;
100 bool CommandParser::endTag(const QString &tag)
102 return true;
105 void CommandParser::text(const QString &text)
107 Q_UNUSED(text);
110 DataSource *CommandParser::build(const YAMF::Command::Base *command)
112 d->currentSource = new DataSource;
113 d->currentSource->setData("command", qVariantFromValue(command) );
116 QString xml = command->toXml();
118 if( ! DCore::XmlParserBase::parse(xml) )
120 dError() << "Failed to parse";
121 return 0;
124 d->currentSource->setData("root", d->commandName);
126 if( d->commandName == "addlibraryobject" )
128 // Rewrite the xml to avoid the file tag
129 QString newXml;
130 QXmlStreamReader reader(xml);
132 xml = QString();
133 QXmlStreamWriter writer(&xml);
135 bool read = true;
136 while(!reader.atEnd())
138 reader.readNext();
139 if( reader.tokenType() == QXmlStreamReader::StartElement )
141 if( reader.name().toString() == "file" )
143 read = false;
146 else if( reader.tokenType() == QXmlStreamReader::EndElement )
148 if( reader.name().toString() == "file" )
150 read = true;
154 if( read )
155 writer.writeCurrentToken(reader);
159 d->currentSource->setData("xml", xml);
162 DataSource *source = d->currentSource;
163 d->currentSource = 0;
165 return source;