kandy Library API Documentation

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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
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 //  kdDebug() << "CommandSet::loadFile(): " << filename << endl;
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() << "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() << "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 }
KDE Logo
This file is part of the documentation for kandy Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:37:58 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003