summaryrefslogtreecommitdiffstats
path: root/kandy/src/atcommand.cpp
blob: 2fcf5f24c72bfddea4ca49672f07ba4a87ef3ecc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
    This file is part of Kandy.

    Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

    As a special exception, permission is given to link this program
    with any edition of TQt, and distribute the resulting executable,
    without including the source code for TQt in the source distribution.
*/

#include "atcommand.h"

#include <kdebug.h>
#include <klocale.h>

ATParameter::ATParameter()
{
  mUserInput = false;
}

ATParameter::ATParameter(const TQString &value,const TQString &name,
                         bool userInput)
{
  mName = name;
  mValue = value;
  mUserInput = userInput;
}


ATCommand::ATCommand()
{
  mHexOutput = false;

  construct();
}

ATCommand::ATCommand(const TQString &cmdString)
{
  setCmdName(i18n("New Command"));
  setCmdString(cmdString);
  mHexOutput = false;
  
  extractParameters();
  
  construct();
}

ATCommand::ATCommand(const TQString &cmdName,const TQString &cmdString,
                     bool hexOutput)
{
  setCmdName(cmdName);
  setCmdString(cmdString);
  mHexOutput = hexOutput;

  construct();
}

void ATCommand::construct()
{
  mAutoDelete = false;
  mResultFieldsList.setAutoDelete(true);
  mParameters.setAutoDelete(true);
}

ATCommand::~ATCommand()
{
//  kdDebug() << "~ATCommand: " << cmdString() << endl;
}


void ATCommand::setCmdName(const TQString &cmdName)
{
  mCmdName = cmdName;
}

TQString ATCommand::cmdName()
{
  return mCmdName;
}


void ATCommand::setCmdString(const TQString &cmdString)
{
  mCmdString = cmdString;

  mId = cmdString;
  if (mId.startsWith("at")) mId = mId.mid(2);
  else mCmdString.prepend("at");
  
//  kdDebug() << "ATCommand: Id: " << mId << endl;
}

TQString ATCommand::cmdString()
{
  return mCmdString;
}

TQString ATCommand::cmd()
{
  if (mParameters.count() > 0) {
    TQString cmd = cmdString().left(cmdString().find("=") + 1);
//    kdDebug() << "--1-cmd: " << cmd << endl;
    for(uint i=0;i<mParameters.count();++i) {
      cmd += mParameters.at(i)->value();
      if (i < mParameters.count() - 1) cmd += ",";
    }
//    kdDebug() << "--2-cmd: " << cmd << endl;
    return cmd;
  } else {
    return cmdString();
  }
}

TQString ATCommand::id()
{
  return mId;
}

void ATCommand::setHexOutput(bool hexOutput)
{
  mHexOutput = hexOutput;
}

bool ATCommand::hexOutput()
{
  return mHexOutput;
}

void ATCommand::setResultString(const TQString &resultString)
{
  mResultString = resultString;

  mResultFieldsList.clear();

  TQStringList resultFields = TQStringList::split("\n",mResultString);

  for(TQStringList::Iterator it = resultFields.begin();
      it != resultFields.end(); ++it) {
    setResultFields(*it);
  }
}


void ATCommand::setResultFields( TQString fieldsString )
{
  TQString id = mId.upper().left( mId.find( '=' ) );
  

  // Truncate the command name prepended to the output by the modem.
  if ( fieldsString.startsWith( id ) )
    fieldsString = fieldsString.mid( id.length() + 2 );

  // If modem output is enclosed by brackets, remove them, too
  if ( ( fieldsString[ 0 ] == '(' ) && ( fieldsString[ fieldsString.length() - 1 ] == ')' ) )
    fieldsString = fieldsString.mid( 1, fieldsString.length() - 2 );

  TQStringList *fields = new TQStringList;
  TQStringList TmpFields = TQStringList::split( ',', fieldsString );
  TQString TmpString = "";
  

  // Assume a phonebook entry of the mobile phone has the format
  //   <familyname>, <givenname>
  // Then, the above split() call separtes this entry into 2 distinct fields
  // leading to an error in MobileGui::fillPhonebook since the number of
  // fields is != 4.
  // Hence, the fieldsString needs to be parsed a little bit. Names stored on
  // the mobile phone are quoted. Commas within a quoted are of the fieldsString
  // must not be divided into differend fields.
  for ( TQStringList::Iterator it = TmpFields.begin(); it != TmpFields.end(); it++ )
  {
    // Start of a quoted area
    if ( ( (*it)[ 0 ] == '\"' ) && ( (*it)[ (*it).length() - 1 ] != '\"' ) )
      TmpString = (*it).copy();
    else
    // End of a quoted area
    if ( ( (*it)[ 0 ] != '\"' ) && ( (*it)[ (*it).length() - 1 ] == '\"' ) )
    {
      TmpString += "," + (*it).copy();
      (*fields).append( TmpString.copy() );
      TmpString = "";
    } else
    // Not within a quoted area
    if (TmpString.isEmpty())
      (*fields).append( *it );
    else
    // Within a quoted area
      TmpString += "," + (*it).copy();
  }

  mResultFieldsList.append( fields );
}


TQString ATCommand::resultString()
{
  return mResultString;
}

TQString ATCommand::resultField(int index)
{
  if (mResultFieldsList.count() == 0) return "";

  TQStringList *resultFields = mResultFieldsList.at(0);

  TQStringList::Iterator it = resultFields->at(index);
  if (it == resultFields->end()) {
    kdDebug() << "ATCommand::resultField: index " << index << " out of range."
              << endl;
    return "";
  }

  return *it;
}


TQPtrList<TQStringList> *ATCommand::resultFields()
{
   return &mResultFieldsList;
}

void ATCommand::addParameter(ATParameter *p)
{
  mParameters.append(p);
}

void ATCommand::clearParameters()
{
  mParameters.clear();
}

TQPtrList<ATParameter> ATCommand::parameters()
{
  return mParameters;
}

void ATCommand::setParameter(int index,const TQString &value)
{
  if (mParameters.count() <= (unsigned int)index) {
    kdDebug() << "ATCommand " << cmdName() << " has no Parameter " << index
              << endl;
    return;
  }
  
  mParameters.at(index)->setValue(value);
}

void ATCommand::setParameter(int index,int value)
{
  setParameter(index,TQString::number(value));
}

TQString ATCommand::processOutput(const TQString &output)
{
  if (hexOutput()) {
    TQString hexString = output.mid(output.find('\n')+1);
    int i=0;
    TQString aChar = hexString.mid(i,2);
    TQString result;
    while(!aChar.isEmpty()) {
      int charValue = aChar.toInt(0,16);
      TQChar charEncoded(charValue);
//      result += aChar + ": " + charEncoded + "\n";
      result += charEncoded;
      i += 2;
      aChar = hexString.mid(i,2);
    }
    result += "\n";
    return result;
  } else {
    return output;
  }
}

TQString ATCommand::processOutput()
{
  return processOutput(mResultString);
}

void ATCommand::extractParameters()
{
//  kdDebug() << "Arg String: " << cmdString() << endl;
  
  int pos = cmdString().find("=");
  if (pos < 0) return;
  
  TQString paraString = cmdString().mid(pos+1);
//  kdDebug() << "Para String: " << paraString << endl;
  TQStringList paraList = TQStringList::split(",",paraString);
  
  TQStringList::ConstIterator it = paraList.begin();
  TQStringList::ConstIterator end = paraList.end();
  int argNum = 1;
  while(it != end) {
    addParameter(new ATParameter(*it,i18n("Arg %1").arg(TQString::number(argNum++)),
                                 false));
    ++it;
  }
}