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