25 #include "atcommand.h"
28 #include <tdelocale.h>
30 ATParameter::ATParameter()
35 ATParameter::ATParameter(
const TQString &value,
const TQString &name,
40 mUserInput = userInput;
44 ATCommand::ATCommand()
51 ATCommand::ATCommand(
const TQString &cmdString)
53 setCmdName(i18n(
"New Command"));
54 setCmdString(cmdString);
62 ATCommand::ATCommand(
const TQString &cmdName,
const TQString &cmdString,
66 setCmdString(cmdString);
67 mHexOutput = hexOutput;
72 void ATCommand::construct()
75 mResultFieldsList.setAutoDelete(
true);
76 mParameters.setAutoDelete(
true);
79 ATCommand::~ATCommand()
85 void ATCommand::setCmdName(
const TQString &cmdName)
90 TQString ATCommand::cmdName()
96 void ATCommand::setCmdString(
const TQString &cmdString)
98 mCmdString = cmdString;
101 if (mId.startsWith(
"at")) mId = mId.mid(2);
102 else mCmdString.prepend(
"at");
107 TQString ATCommand::cmdString()
112 TQString ATCommand::cmd()
114 if (mParameters.count() > 0) {
115 TQString cmd = cmdString().left(cmdString().find(
"=") + 1);
117 for(uint i=0;i<mParameters.count();++i) {
118 cmd += mParameters.at(i)->value();
119 if (i < mParameters.count() - 1) cmd +=
",";
128 TQString ATCommand::id()
133 void ATCommand::setHexOutput(
bool hexOutput)
135 mHexOutput = hexOutput;
138 bool ATCommand::hexOutput()
143 void ATCommand::setResultString(
const TQString &resultString)
145 mResultString = resultString;
147 mResultFieldsList.clear();
149 TQStringList resultFields = TQStringList::split(
"\n",mResultString);
151 for(TQStringList::Iterator it = resultFields.begin();
152 it != resultFields.end(); ++it) {
153 setResultFields(*it);
158 void ATCommand::setResultFields( TQString fieldsString )
160 TQString
id = mId.upper().left( mId.find(
'=' ) );
164 if ( fieldsString.startsWith(
id ) )
165 fieldsString = fieldsString.mid(
id.length() + 2 );
168 if ( ( fieldsString[ 0 ] ==
'(' ) && ( fieldsString[ fieldsString.length() - 1 ] ==
')' ) )
169 fieldsString = fieldsString.mid( 1, fieldsString.length() - 2 );
171 TQStringList *fields =
new TQStringList;
172 TQStringList TmpFields = TQStringList::split(
',', fieldsString );
173 TQString TmpString =
"";
184 for ( TQStringList::Iterator it = TmpFields.begin(); it != TmpFields.end(); it++ )
187 if ( ( (*it)[ 0 ] ==
'\"' ) && ( (*it)[ (*it).length() - 1 ] !=
'\"' ) )
188 TmpString = (*it).copy();
191 if ( ( (*it)[ 0 ] !=
'\"' ) && ( (*it)[ (*it).length() - 1 ] ==
'\"' ) )
193 TmpString +=
"," + (*it).copy();
194 (*fields).append( TmpString.copy() );
198 if (TmpString.isEmpty())
199 (*fields).append( *it );
202 TmpString +=
"," + (*it).copy();
205 mResultFieldsList.append( fields );
209 TQString ATCommand::resultString()
211 return mResultString;
214 TQString ATCommand::resultField(
int index)
216 if (mResultFieldsList.count() == 0)
return "";
218 TQStringList *resultFields = mResultFieldsList.at(0);
220 TQStringList::Iterator it = resultFields->at(index);
221 if (it == resultFields->end()) {
222 kdDebug() <<
"ATCommand::resultField: index " << index <<
" out of range."
231 TQPtrList<TQStringList> *ATCommand::resultFields()
233 return &mResultFieldsList;
236 void ATCommand::addParameter(ATParameter *p)
238 mParameters.append(p);
241 void ATCommand::clearParameters()
246 TQPtrList<ATParameter> ATCommand::parameters()
251 void ATCommand::setParameter(
int index,
const TQString &value)
253 if (mParameters.count() <= (
unsigned int)index) {
254 kdDebug() <<
"ATCommand " << cmdName() <<
" has no Parameter " << index
259 mParameters.at(index)->setValue(value);
262 void ATCommand::setParameter(
int index,
int value)
264 setParameter(index,TQString::number(value));
267 TQString ATCommand::processOutput(
const TQString &output)
270 TQString hexString = output.mid(output.find(
'\n')+1);
272 TQString aChar = hexString.mid(i,2);
274 while(!aChar.isEmpty()) {
275 int charValue = aChar.toInt(0,16);
276 TQChar charEncoded(charValue);
278 result += charEncoded;
280 aChar = hexString.mid(i,2);
289 TQString ATCommand::processOutput()
291 return processOutput(mResultString);
294 void ATCommand::extractParameters()
298 int pos = cmdString().find(
"=");
301 TQString paraString = cmdString().mid(pos+1);
303 TQStringList paraList = TQStringList::split(
",",paraString);
305 TQStringList::ConstIterator it = paraList.begin();
306 TQStringList::ConstIterator end = paraList.end();
309 addParameter(
new ATParameter(*it,i18n(
"Arg %1").arg(TQString::number(argNum++)),