kandy

atcommand.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 "atcommand.h"
26 
27 #include <kdebug.h>
28 #include <tdelocale.h>
29 
30 ATParameter::ATParameter()
31 {
32  mUserInput = false;
33 }
34 
35 ATParameter::ATParameter(const TQString &value,const TQString &name,
36  bool userInput)
37 {
38  mName = name;
39  mValue = value;
40  mUserInput = userInput;
41 }
42 
43 
44 ATCommand::ATCommand()
45 {
46  mHexOutput = false;
47 
48  construct();
49 }
50 
51 ATCommand::ATCommand(const TQString &cmdString)
52 {
53  setCmdName(i18n("New Command"));
54  setCmdString(cmdString);
55  mHexOutput = false;
56 
57  extractParameters();
58 
59  construct();
60 }
61 
62 ATCommand::ATCommand(const TQString &cmdName,const TQString &cmdString,
63  bool hexOutput)
64 {
65  setCmdName(cmdName);
66  setCmdString(cmdString);
67  mHexOutput = hexOutput;
68 
69  construct();
70 }
71 
72 void ATCommand::construct()
73 {
74  mAutoDelete = false;
75  mResultFieldsList.setAutoDelete(true);
76  mParameters.setAutoDelete(true);
77 }
78 
79 ATCommand::~ATCommand()
80 {
81 // kdDebug() << "~ATCommand: " << cmdString() << endl;
82 }
83 
84 
85 void ATCommand::setCmdName(const TQString &cmdName)
86 {
87  mCmdName = cmdName;
88 }
89 
90 TQString ATCommand::cmdName()
91 {
92  return mCmdName;
93 }
94 
95 
96 void ATCommand::setCmdString(const TQString &cmdString)
97 {
98  mCmdString = cmdString;
99 
100  mId = cmdString;
101  if (mId.startsWith("at")) mId = mId.mid(2);
102  else mCmdString.prepend("at");
103 
104 // kdDebug() << "ATCommand: Id: " << mId << endl;
105 }
106 
107 TQString ATCommand::cmdString()
108 {
109  return mCmdString;
110 }
111 
112 TQString ATCommand::cmd()
113 {
114  if (mParameters.count() > 0) {
115  TQString cmd = cmdString().left(cmdString().find("=") + 1);
116 // kdDebug() << "--1-cmd: " << cmd << endl;
117  for(uint i=0;i<mParameters.count();++i) {
118  cmd += mParameters.at(i)->value();
119  if (i < mParameters.count() - 1) cmd += ",";
120  }
121 // kdDebug() << "--2-cmd: " << cmd << endl;
122  return cmd;
123  } else {
124  return cmdString();
125  }
126 }
127 
128 TQString ATCommand::id()
129 {
130  return mId;
131 }
132 
133 void ATCommand::setHexOutput(bool hexOutput)
134 {
135  mHexOutput = hexOutput;
136 }
137 
138 bool ATCommand::hexOutput()
139 {
140  return mHexOutput;
141 }
142 
143 void ATCommand::setResultString(const TQString &resultString)
144 {
145  mResultString = resultString;
146 
147  mResultFieldsList.clear();
148 
149  TQStringList resultFields = TQStringList::split("\n",mResultString);
150 
151  for(TQStringList::Iterator it = resultFields.begin();
152  it != resultFields.end(); ++it) {
153  setResultFields(*it);
154  }
155 }
156 
157 
158 void ATCommand::setResultFields( TQString fieldsString )
159 {
160  TQString id = mId.upper().left( mId.find( '=' ) );
161 
162 
163  // Truncate the command name prepended to the output by the modem.
164  if ( fieldsString.startsWith( id ) )
165  fieldsString = fieldsString.mid( id.length() + 2 );
166 
167  // If modem output is enclosed by brackets, remove them, too
168  if ( ( fieldsString[ 0 ] == '(' ) && ( fieldsString[ fieldsString.length() - 1 ] == ')' ) )
169  fieldsString = fieldsString.mid( 1, fieldsString.length() - 2 );
170 
171  TQStringList *fields = new TQStringList;
172  TQStringList TmpFields = TQStringList::split( ',', fieldsString );
173  TQString TmpString = "";
174 
175 
176  // Assume a phonebook entry of the mobile phone has the format
177  // <familyname>, <givenname>
178  // Then, the above split() call separtes this entry into 2 distinct fields
179  // leading to an error in MobileGui::fillPhonebook since the number of
180  // fields is != 4.
181  // Hence, the fieldsString needs to be parsed a little bit. Names stored on
182  // the mobile phone are quoted. Commas within a quoted are of the fieldsString
183  // must not be divided into differend fields.
184  for ( TQStringList::Iterator it = TmpFields.begin(); it != TmpFields.end(); it++ )
185  {
186  // Start of a quoted area
187  if ( ( (*it)[ 0 ] == '\"' ) && ( (*it)[ (*it).length() - 1 ] != '\"' ) )
188  TmpString = (*it).copy();
189  else
190  // End of a quoted area
191  if ( ( (*it)[ 0 ] != '\"' ) && ( (*it)[ (*it).length() - 1 ] == '\"' ) )
192  {
193  TmpString += "," + (*it).copy();
194  (*fields).append( TmpString.copy() );
195  TmpString = "";
196  } else
197  // Not within a quoted area
198  if (TmpString.isEmpty())
199  (*fields).append( *it );
200  else
201  // Within a quoted area
202  TmpString += "," + (*it).copy();
203  }
204 
205  mResultFieldsList.append( fields );
206 }
207 
208 
209 TQString ATCommand::resultString()
210 {
211  return mResultString;
212 }
213 
214 TQString ATCommand::resultField(int index)
215 {
216  if (mResultFieldsList.count() == 0) return "";
217 
218  TQStringList *resultFields = mResultFieldsList.at(0);
219 
220  TQStringList::Iterator it = resultFields->at(index);
221  if (it == resultFields->end()) {
222  kdDebug() << "ATCommand::resultField: index " << index << " out of range."
223  << endl;
224  return "";
225  }
226 
227  return *it;
228 }
229 
230 
231 TQPtrList<TQStringList> *ATCommand::resultFields()
232 {
233  return &mResultFieldsList;
234 }
235 
236 void ATCommand::addParameter(ATParameter *p)
237 {
238  mParameters.append(p);
239 }
240 
241 void ATCommand::clearParameters()
242 {
243  mParameters.clear();
244 }
245 
246 TQPtrList<ATParameter> ATCommand::parameters()
247 {
248  return mParameters;
249 }
250 
251 void ATCommand::setParameter(int index,const TQString &value)
252 {
253  if (mParameters.count() <= (unsigned int)index) {
254  kdDebug() << "ATCommand " << cmdName() << " has no Parameter " << index
255  << endl;
256  return;
257  }
258 
259  mParameters.at(index)->setValue(value);
260 }
261 
262 void ATCommand::setParameter(int index,int value)
263 {
264  setParameter(index,TQString::number(value));
265 }
266 
267 TQString ATCommand::processOutput(const TQString &output)
268 {
269  if (hexOutput()) {
270  TQString hexString = output.mid(output.find('\n')+1);
271  int i=0;
272  TQString aChar = hexString.mid(i,2);
273  TQString result;
274  while(!aChar.isEmpty()) {
275  int charValue = aChar.toInt(0,16);
276  TQChar charEncoded(charValue);
277 // result += aChar + ": " + charEncoded + "\n";
278  result += charEncoded;
279  i += 2;
280  aChar = hexString.mid(i,2);
281  }
282  result += "\n";
283  return result;
284  } else {
285  return output;
286  }
287 }
288 
289 TQString ATCommand::processOutput()
290 {
291  return processOutput(mResultString);
292 }
293 
294 void ATCommand::extractParameters()
295 {
296 // kdDebug() << "Arg String: " << cmdString() << endl;
297 
298  int pos = cmdString().find("=");
299  if (pos < 0) return;
300 
301  TQString paraString = cmdString().mid(pos+1);
302 // kdDebug() << "Para String: " << paraString << endl;
303  TQStringList paraList = TQStringList::split(",",paraString);
304 
305  TQStringList::ConstIterator it = paraList.begin();
306  TQStringList::ConstIterator end = paraList.end();
307  int argNum = 1;
308  while(it != end) {
309  addParameter(new ATParameter(*it,i18n("Arg %1").arg(TQString::number(argNum++)),
310  false));
311  ++it;
312  }
313 }