kandy

commandset.cpp

00001 /*
00002     This file is part of Kandy.
00003 
00004     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of TQt, and distribute the resulting executable,
00022     without including the source code for TQt in the source distribution.
00023 */
00024 
00025 #include <tqdom.h>
00026 #include <tqfile.h>
00027 #include <tqtextstream.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 TQString& filename)
00055 {
00056 //  kdDebug(5960) << "CommandSet::loadFile(): " << filename << endl;
00057 
00058   TQDomDocument doc("Kandy");
00059   TQFile 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   TQDomNodeList commands = doc.elementsByTagName("command");
00069   for(uint i=0;i<commands.count();++i) {
00070     TQDomElement 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 TQString& filename)
00082 {
00083   kdDebug(5960) << "CommandSet::saveFile(): " << filename << endl;
00084 
00085   TQDomDocument doc("Kandy");
00086   TQDomElement 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   TQFile xmlfile(filename);
00094   if (!xmlfile.open(IO_WriteOnly)) {
00095     kdDebug(5960) << "Error opening file for write." << endl;
00096     return false;
00097   }
00098   TQTextStream 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,TQDomElement *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   TQDomNode n = c->firstChild();
00117   while(!n.isNull()) {
00118     TQDomElement 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,TQDomDocument *doc,
00132                              TQDomElement *parent)
00133 {
00134   TQDomElement 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   TQPtrList<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, TQDomDocument *doc,
00147                                TQDomElement *parent)
00148 {
00149   TQDomElement 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 }