kandy

commandset.cpp
1 /*
2  This file is part of Kandy.
3 
4  Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20  As a special exception, permission is given to link this program
21  with any edition of TQt, and distribute the resulting executable,
22  without including the source code for TQt in the source distribution.
23 */
24 
25 #include <tqdom.h>
26 #include <tqfile.h>
27 #include <tqtextstream.h>
28 
29 #include <kdebug.h>
30 
31 #include "atcommand.h"
32 
33 #include "commandset.h"
34 
35 CommandSet::CommandSet()
36 {
37  mList.setAutoDelete(true);
38 }
39 
40 CommandSet::~CommandSet()
41 {
42 }
43 
44 void CommandSet::addCommand(ATCommand *command)
45 {
46  mList.append(command);
47 }
48 
49 void CommandSet::deleteCommand(ATCommand *command)
50 {
51  mList.removeRef(command);
52 }
53 
54 bool CommandSet::loadFile(const TQString& filename)
55 {
56 // kdDebug(5960) << "CommandSet::loadFile(): " << filename << endl;
57 
58  TQDomDocument doc("Kandy");
59  TQFile f(filename);
60  if (!f.open(IO_ReadOnly))
61  return false;
62  if (!doc.setContent(&f)) {
63  f.close();
64  return false;
65  }
66  f.close();
67 
68  TQDomNodeList commands = doc.elementsByTagName("command");
69  for(uint i=0;i<commands.count();++i) {
70  TQDomElement c = commands.item(i).toElement();
71  if (!c.isNull()) {
72  ATCommand *cmd = new ATCommand;
73  loadCommand(cmd,&c);
74  addCommand(cmd);
75  }
76  }
77 
78  return true;
79 }
80 
81 bool CommandSet::saveFile(const TQString& filename)
82 {
83  kdDebug(5960) << "CommandSet::saveFile(): " << filename << endl;
84 
85  TQDomDocument doc("Kandy");
86  TQDomElement set = doc.createElement("commandset");
87  doc.appendChild(set);
88 
89  for(uint i=0; i<mList.count();++i) {
90  saveCommand(mList.at(i),&doc,&set);
91  }
92 
93  TQFile xmlfile(filename);
94  if (!xmlfile.open(IO_WriteOnly)) {
95  kdDebug(5960) << "Error opening file for write." << endl;
96  return false;
97  }
98  TQTextStream ts(&xmlfile);
99  doc.documentElement().save(ts,2);
100  xmlfile.close();
101 
102  return true;
103 }
104 
105 void CommandSet::clear()
106 {
107  mList.clear();
108 }
109 
110 void CommandSet::loadCommand(ATCommand *command,TQDomElement *c)
111 {
112  command->setCmdName(c->attribute("name","unknown"));
113  command->setCmdString(c->attribute("string","at"));
114  command->setHexOutput(c->attribute("hexoutput","n") == "y");
115 
116  TQDomNode n = c->firstChild();
117  while(!n.isNull()) {
118  TQDomElement e = n.toElement();
119  if (!e.isNull()) {
120  ATParameter *p = new ATParameter;
121  p->setName(e.attribute("name","unnamed"));
122  p->setValue(e.attribute("value","0"));
123  p->setUserInput(e.attribute("userinput","n") == "y");
124 
125  command->addParameter(p);
126  }
127  n = n.nextSibling();
128  }
129 }
130 
131 void CommandSet::saveCommand(ATCommand *command,TQDomDocument *doc,
132  TQDomElement *parent)
133 {
134  TQDomElement c = doc->createElement("command");
135  c.setAttribute("name",command->cmdName());
136  c.setAttribute("string",command->cmdString());
137  c.setAttribute("hexoutput",command->hexOutput() ? "y" : "n");
138  parent->appendChild(c);
139 
140  TQPtrList<ATParameter> paras = command->parameters();
141  for(uint i=0;i<paras.count();++i) {
142  saveParameter(paras.at(i),doc,&c);
143  }
144 }
145 
146 void CommandSet::saveParameter(ATParameter *p, TQDomDocument *doc,
147  TQDomElement *parent)
148 {
149  TQDomElement e = doc->createElement("parameter");
150  e.setAttribute("name",p->name());
151  e.setAttribute("value",p->value());
152  e.setAttribute("userinput",p->userInput() ? "y" : "n");
153  parent->appendChild(e);
154 }
This class provides an abstraction of an AT command.
Definition: atcommand.h:54