kandy Library API Documentation

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