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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 00020 As a special exception, permission is given to link this program 00021 with any edition of TQt, and distribute the resulting executable, 00022 without including the source code for TQt in the source distribution. 00023 */ 00024 00025 #include "atcommand.h" 00026 00027 #include <kdebug.h> 00028 #include <tdelocale.h> 00029 00030 ATParameter::ATParameter() 00031 { 00032 mUserInput = false; 00033 } 00034 00035 ATParameter::ATParameter(const TQString &value,const TQString &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 TQString &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 TQString &cmdName,const TQString &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 TQString &cmdName) 00086 { 00087 mCmdName = cmdName; 00088 } 00089 00090 TQString ATCommand::cmdName() 00091 { 00092 return mCmdName; 00093 } 00094 00095 00096 void ATCommand::setCmdString(const TQString &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 TQString ATCommand::cmdString() 00108 { 00109 return mCmdString; 00110 } 00111 00112 TQString ATCommand::cmd() 00113 { 00114 if (mParameters.count() > 0) { 00115 TQString 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 TQString 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 TQString &resultString) 00144 { 00145 mResultString = resultString; 00146 00147 mResultFieldsList.clear(); 00148 00149 TQStringList resultFields = TQStringList::split("\n",mResultString); 00150 00151 for(TQStringList::Iterator it = resultFields.begin(); 00152 it != resultFields.end(); ++it) { 00153 setResultFields(*it); 00154 } 00155 } 00156 00157 00158 void ATCommand::setResultFields( TQString fieldsString ) 00159 { 00160 TQString id = mId.upper().left( mId.find( '=' ) ); 00161 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 // If modem output is enclosed by brackets, remove them, too 00168 if ( ( fieldsString[ 0 ] == '(' ) && ( fieldsString[ fieldsString.length() - 1 ] == ')' ) ) 00169 fieldsString = fieldsString.mid( 1, fieldsString.length() - 2 ); 00170 00171 TQStringList *fields = new TQStringList; 00172 TQStringList TmpFields = TQStringList::split( ',', fieldsString ); 00173 TQString TmpString = ""; 00174 00175 00176 // Assume a phonebook entry of the mobile phone has the format 00177 // <familyname>, <givenname> 00178 // Then, the above split() call separtes this entry into 2 distinct fields 00179 // leading to an error in MobileGui::fillPhonebook since the number of 00180 // fields is != 4. 00181 // Hence, the fieldsString needs to be parsed a little bit. Names stored on 00182 // the mobile phone are quoted. Commas within a quoted are of the fieldsString 00183 // must not be divided into differend fields. 00184 for ( TQStringList::Iterator it = TmpFields.begin(); it != TmpFields.end(); it++ ) 00185 { 00186 // Start of a quoted area 00187 if ( ( (*it)[ 0 ] == '\"' ) && ( (*it)[ (*it).length() - 1 ] != '\"' ) ) 00188 TmpString = (*it).copy(); 00189 else 00190 // End of a quoted area 00191 if ( ( (*it)[ 0 ] != '\"' ) && ( (*it)[ (*it).length() - 1 ] == '\"' ) ) 00192 { 00193 TmpString += "," + (*it).copy(); 00194 (*fields).append( TmpString.copy() ); 00195 TmpString = ""; 00196 } else 00197 // Not within a quoted area 00198 if (TmpString.isEmpty()) 00199 (*fields).append( *it ); 00200 else 00201 // Within a quoted area 00202 TmpString += "," + (*it).copy(); 00203 } 00204 00205 mResultFieldsList.append( fields ); 00206 } 00207 00208 00209 TQString ATCommand::resultString() 00210 { 00211 return mResultString; 00212 } 00213 00214 TQString ATCommand::resultField(int index) 00215 { 00216 if (mResultFieldsList.count() == 0) return ""; 00217 00218 TQStringList *resultFields = mResultFieldsList.at(0); 00219 00220 TQStringList::Iterator it = resultFields->at(index); 00221 if (it == resultFields->end()) { 00222 kdDebug() << "ATCommand::resultField: index " << index << " out of range." 00223 << endl; 00224 return ""; 00225 } 00226 00227 return *it; 00228 } 00229 00230 00231 TQPtrList<TQStringList> *ATCommand::resultFields() 00232 { 00233 return &mResultFieldsList; 00234 } 00235 00236 void ATCommand::addParameter(ATParameter *p) 00237 { 00238 mParameters.append(p); 00239 } 00240 00241 void ATCommand::clearParameters() 00242 { 00243 mParameters.clear(); 00244 } 00245 00246 TQPtrList<ATParameter> ATCommand::parameters() 00247 { 00248 return mParameters; 00249 } 00250 00251 void ATCommand::setParameter(int index,const TQString &value) 00252 { 00253 if (mParameters.count() <= (unsigned int)index) { 00254 kdDebug() << "ATCommand " << cmdName() << " has no Parameter " << index 00255 << endl; 00256 return; 00257 } 00258 00259 mParameters.at(index)->setValue(value); 00260 } 00261 00262 void ATCommand::setParameter(int index,int value) 00263 { 00264 setParameter(index,TQString::number(value)); 00265 } 00266 00267 TQString ATCommand::processOutput(const TQString &output) 00268 { 00269 if (hexOutput()) { 00270 TQString hexString = output.mid(output.find('\n')+1); 00271 int i=0; 00272 TQString aChar = hexString.mid(i,2); 00273 TQString result; 00274 while(!aChar.isEmpty()) { 00275 int charValue = aChar.toInt(0,16); 00276 TQChar charEncoded(charValue); 00277 // result += aChar + ": " + charEncoded + "\n"; 00278 result += charEncoded; 00279 i += 2; 00280 aChar = hexString.mid(i,2); 00281 } 00282 result += "\n"; 00283 return result; 00284 } else { 00285 return output; 00286 } 00287 } 00288 00289 TQString ATCommand::processOutput() 00290 { 00291 return processOutput(mResultString); 00292 } 00293 00294 void ATCommand::extractParameters() 00295 { 00296 // kdDebug() << "Arg String: " << cmdString() << endl; 00297 00298 int pos = cmdString().find("="); 00299 if (pos < 0) return; 00300 00301 TQString paraString = cmdString().mid(pos+1); 00302 // kdDebug() << "Para String: " << paraString << endl; 00303 TQStringList paraList = TQStringList::split(",",paraString); 00304 00305 TQStringList::ConstIterator it = paraList.begin(); 00306 TQStringList::ConstIterator end = paraList.end(); 00307 int argNum = 1; 00308 while(it != end) { 00309 addParameter(new ATParameter(*it,i18n("Arg %1").arg(TQString::number(argNum++)), 00310 false)); 00311 ++it; 00312 } 00313 }