kandy
commandset.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <qdom.h>
00026 #include <qfile.h>
00027 #include <qtextstream.h>
00028
00029 #include <kdebug.h>
00030
00031 #include "atcommand.h"
00032
00033 #include "commandset.h"
00034
00035 CommandSet::CommandSet()
00036 {
00037 mList.setAutoDelete(true);
00038 }
00039
00040 CommandSet::~CommandSet()
00041 {
00042 }
00043
00044 void CommandSet::addCommand(ATCommand *command)
00045 {
00046 mList.append(command);
00047 }
00048
00049 void CommandSet::deleteCommand(ATCommand *command)
00050 {
00051 mList.removeRef(command);
00052 }
00053
00054 bool CommandSet::loadFile(const QString& filename)
00055 {
00056
00057
00058 QDomDocument doc("Kandy");
00059 QFile f(filename);
00060 if (!f.open(IO_ReadOnly))
00061 return false;
00062 if (!doc.setContent(&f)) {
00063 f.close();
00064 return false;
00065 }
00066 f.close();
00067
00068 QDomNodeList commands = doc.elementsByTagName("command");
00069 for(uint i=0;i<commands.count();++i) {
00070 QDomElement c = commands.item(i).toElement();
00071 if (!c.isNull()) {
00072 ATCommand *cmd = new ATCommand;
00073 loadCommand(cmd,&c);
00074 addCommand(cmd);
00075 }
00076 }
00077
00078 return true;
00079 }
00080
00081 bool CommandSet::saveFile(const QString& filename)
00082 {
00083 kdDebug(5960) << "CommandSet::saveFile(): " << filename << endl;
00084
00085 QDomDocument doc("Kandy");
00086 QDomElement set = doc.createElement("commandset");
00087 doc.appendChild(set);
00088
00089 for(uint i=0; i<mList.count();++i) {
00090 saveCommand(mList.at(i),&doc,&set);
00091 }
00092
00093 QFile xmlfile(filename);
00094 if (!xmlfile.open(IO_WriteOnly)) {
00095 kdDebug(5960) << "Error opening file for write." << endl;
00096 return false;
00097 }
00098 QTextStream ts(&xmlfile);
00099 doc.documentElement().save(ts,2);
00100 xmlfile.close();
00101
00102 return true;
00103 }
00104
00105 void CommandSet::clear()
00106 {
00107 mList.clear();
00108 }
00109
00110 void CommandSet::loadCommand(ATCommand *command,QDomElement *c)
00111 {
00112 command->setCmdName(c->attribute("name","unknown"));
00113 command->setCmdString(c->attribute("string","at"));
00114 command->setHexOutput(c->attribute("hexoutput","n") == "y");
00115
00116 QDomNode n = c->firstChild();
00117 while(!n.isNull()) {
00118 QDomElement e = n.toElement();
00119 if (!e.isNull()) {
00120 ATParameter *p = new ATParameter;
00121 p->setName(e.attribute("name","unnamed"));
00122 p->setValue(e.attribute("value","0"));
00123 p->setUserInput(e.attribute("userinput","n") == "y");
00124
00125 command->addParameter(p);
00126 }
00127 n = n.nextSibling();
00128 }
00129 }
00130
00131 void CommandSet::saveCommand(ATCommand *command,QDomDocument *doc,
00132 QDomElement *parent)
00133 {
00134 QDomElement c = doc->createElement("command");
00135 c.setAttribute("name",command->cmdName());
00136 c.setAttribute("string",command->cmdString());
00137 c.setAttribute("hexoutput",command->hexOutput() ? "y" : "n");
00138 parent->appendChild(c);
00139
00140 QPtrList<ATParameter> paras = command->parameters();
00141 for(uint i=0;i<paras.count();++i) {
00142 saveParameter(paras.at(i),doc,&c);
00143 }
00144 }
00145
00146 void CommandSet::saveParameter(ATParameter *p, QDomDocument *doc,
00147 QDomElement *parent)
00148 {
00149 QDomElement e = doc->createElement("parameter");
00150 e.setAttribute("name",p->name());
00151 e.setAttribute("value",p->value());
00152 e.setAttribute("userinput",p->userInput() ? "y" : "n");
00153 parent->appendChild(e);
00154 }
|