atcommand.cpp
00001
00002
00003 #include "atcommand.h"
00004
00005 #include <kdebug.h>
00006 #include <klocale.h>
00007
00008 ATParameter::ATParameter()
00009 {
00010 mUserInput = false;
00011 }
00012
00013 ATParameter::ATParameter(const QString &value,const QString &name,
00014 bool userInput)
00015 {
00016 mName = name;
00017 mValue = value;
00018 mUserInput = userInput;
00019 }
00020
00021
00022 ATCommand::ATCommand()
00023 {
00024 mHexOutput = false;
00025
00026 construct();
00027 }
00028
00029 ATCommand::ATCommand(const QString &cmdString)
00030 {
00031 setCmdName(i18n("New Command"));
00032 setCmdString(cmdString);
00033 mHexOutput = false;
00034
00035 extractParameters();
00036
00037 construct();
00038 }
00039
00040 ATCommand::ATCommand(const QString &cmdName,const QString &cmdString,
00041 bool hexOutput)
00042 {
00043 setCmdName(cmdName);
00044 setCmdString(cmdString);
00045 mHexOutput = hexOutput;
00046
00047 construct();
00048 }
00049
00050 void ATCommand::construct()
00051 {
00052 mAutoDelete = false;
00053 mResultFieldsList.setAutoDelete(true);
00054 mParameters.setAutoDelete(true);
00055 }
00056
00057 ATCommand::~ATCommand()
00058 {
00059
00060 }
00061
00062
00063 void ATCommand::setCmdName(const QString &cmdName)
00064 {
00065 mCmdName = cmdName;
00066 }
00067
00068 QString ATCommand::cmdName()
00069 {
00070 return mCmdName;
00071 }
00072
00073
00074 void ATCommand::setCmdString(const QString &cmdString)
00075 {
00076 mCmdString = cmdString;
00077
00078 mId = cmdString;
00079 if (mId.startsWith("at")) mId = mId.mid(2);
00080 else mCmdString.prepend("at");
00081
00082
00083 }
00084
00085 QString ATCommand::cmdString()
00086 {
00087 return mCmdString;
00088 }
00089
00090 QString ATCommand::cmd()
00091 {
00092 if (mParameters.count() > 0) {
00093 QString cmd = cmdString().left(cmdString().find("=") + 1);
00094
00095 for(uint i=0;i<mParameters.count();++i) {
00096 cmd += mParameters.at(i)->value();
00097 if (i < mParameters.count() - 1) cmd += ",";
00098 }
00099
00100 return cmd;
00101 } else {
00102 return cmdString();
00103 }
00104 }
00105
00106 QString ATCommand::id()
00107 {
00108 return mId;
00109 }
00110
00111 void ATCommand::setHexOutput(bool hexOutput)
00112 {
00113 mHexOutput = hexOutput;
00114 }
00115
00116 bool ATCommand::hexOutput()
00117 {
00118 return mHexOutput;
00119 }
00120
00121 void ATCommand::setResultString(const QString &resultString)
00122 {
00123 mResultString = resultString;
00124
00125 mResultFieldsList.clear();
00126
00127 QStringList resultFields = QStringList::split("\n",mResultString);
00128
00129 for(QStringList::Iterator it = resultFields.begin();
00130 it != resultFields.end(); ++it) {
00131 setResultFields(*it);
00132 }
00133 }
00134
00135 void ATCommand::setResultFields(QString fieldsString)
00136 {
00137 QString id = mId.upper().left(mId.find('='));
00138
00139
00140
00141
00142 if (fieldsString.startsWith(id)) {
00143 fieldsString = fieldsString.mid(id.length() + 2);
00144 }
00145
00146 QStringList *fields = new QStringList;
00147
00148 *fields = QStringList::split(',',fieldsString);
00149
00150 mResultFieldsList.append(fields);
00151
00152
00153
00154
00155
00156
00157
00158
00159 }
00160
00161 QString ATCommand::resultString()
00162 {
00163 return mResultString;
00164 }
00165
00166 QString ATCommand::resultField(int index)
00167 {
00168 if (mResultFieldsList.count() == 0) return "";
00169
00170 QStringList *resultFields = mResultFieldsList.at(0);
00171
00172 QStringList::Iterator it = resultFields->at(index);
00173 if (it == resultFields->end()) {
00174 kdDebug() << "ATCommand::resultField: index " << index << " out of range."
00175 << endl;
00176 return "";
00177 }
00178
00179 return *it;
00180 }
00181
00182
00183 QPtrList<QStringList> *ATCommand::resultFields()
00184 {
00185 return &mResultFieldsList;
00186 }
00187
00188 void ATCommand::addParameter(ATParameter *p)
00189 {
00190 mParameters.append(p);
00191 }
00192
00193 void ATCommand::clearParameters()
00194 {
00195 mParameters.clear();
00196 }
00197
00198 QPtrList<ATParameter> ATCommand::parameters()
00199 {
00200 return mParameters;
00201 }
00202
00203 void ATCommand::setParameter(int index,const QString &value)
00204 {
00205 if (mParameters.count() <= (unsigned int)index) {
00206 kdDebug() << "ATCommand " << cmdName() << " has no Parameter " << index
00207 << endl;
00208 return;
00209 }
00210
00211 mParameters.at(index)->setValue(value);
00212 }
00213
00214 void ATCommand::setParameter(int index,int value)
00215 {
00216 setParameter(index,QString::number(value));
00217 }
00218
00219 QString ATCommand::processOutput(const QString &output)
00220 {
00221 if (hexOutput()) {
00222 QString hexString = output.mid(output.find('\n')+1);
00223 int i=0;
00224 QString aChar = hexString.mid(i,2);
00225 QString result;
00226 while(!aChar.isEmpty()) {
00227 int charValue = aChar.toInt(0,16);
00228 QChar charEncoded(charValue);
00229
00230 result += charEncoded;
00231 i += 2;
00232 aChar = hexString.mid(i,2);
00233 }
00234 result += "\n";
00235 return result;
00236 } else {
00237 return output;
00238 }
00239 }
00240
00241 QString ATCommand::processOutput()
00242 {
00243 return processOutput(mResultString);
00244 }
00245
00246 void ATCommand::extractParameters()
00247 {
00248
00249
00250 int pos = cmdString().find("=");
00251 if (pos < 0) return;
00252
00253 QString paraString = cmdString().mid(pos+1);
00254
00255 QStringList paraList = QStringList::split(",",paraString);
00256
00257 QStringList::ConstIterator it = paraList.begin();
00258 QStringList::ConstIterator end = paraList.end();
00259 int argNum = 1;
00260 while(it != end) {
00261 addParameter(new ATParameter(*it,i18n("Arg %1").arg(QString::number(argNum++)),
00262 false));
00263 ++it;
00264 }
00265 }
This file is part of the documentation for kdelibs Version 3.1.5.