summaryrefslogtreecommitdiffstats
path: root/kandy/src/commandscheduler.cpp
blob: 3a1001ed3b4bb927caa3b89c6ce7aa408f29f164 (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
/*
    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 <kdebug.h>
#include <klocale.h>

#include "modem.h"

#include "commandscheduler.h"
#include "commandscheduler.moc"

CommandScheduler::CommandScheduler(Modem *modem,TQObject *tqparent,
                                   const char *name) :
  TQObject(tqparent,name),
  mModem(modem)
{
  connect(mModem,TQT_SIGNAL(gotLine(const char *)),
          TQT_SLOT(processOutput(const char *)));
}

void CommandScheduler::execute(ATCommand *command)
{
  if (!mModem->isOpen()) {
    kdDebug(5960) << "Warning! Modem not open." << endl;
    return;
  }

  mCommandQueue.append(command);

//  if (mCommandQueue.count() == 1) sendCommand(command->cmdString());
  if (mCommandQueue.count() == 1) sendCommand(command->cmd());
}

void CommandScheduler::execute(const TQString &command)
{
  ATCommand *cmd = new ATCommand("",command);
  cmd->setAutoDelete(true);

  execute(cmd);
}

void CommandScheduler::executeId(const TQString &id)
{
  TQPtrList<ATCommand> *cmds = mCommandSet.commandList();

  for(uint i=0;i<cmds->count();++i) {
    if (cmds->at(i)->id() == id) {
      execute(cmds->at(i));
      return;
    }
  }
  kdDebug(5960) << "CommandScheduler::executeId(): Id '" << id << "' not found" << endl;
}

void CommandScheduler::sendCommand(const TQString &command)
{
  if (command.isEmpty()) {
    kdDebug(5960) << "CommandScheduler::sendCommand(): Warning! Empty command."
              << endl;
    return;
  }

  kdDebug(5960) << "CommandScheduler:sendCommand(): " << command << endl;

  mModem->writeLine(command.latin1());
}


void CommandScheduler::processOutput(const char *line)
{
  TQString l = line;
  ATCommand *cmd = mCommandQueue.first();
  if (l == "OK") {
    mState = WAITING;
    emit result(mResult);
    cmd->setResultString(mResult);
    emit commandProcessed(cmd);
    nextCommand();
  } else if (l == "ERROR") {
    mState = WAITING;
    emit result(i18n("Error"));
    nextCommand();
  } else {
    if (mState == WAITING) {
      mState = PROCESSING;
      mResult = "";
    } else if (mState == PROCESSING) {
      if (!l.isEmpty()) {
        mResult += l;
        mResult += "\n";
      }
    }
  }
}

void CommandScheduler::nextCommand()
{
  if (mCommandQueue.first()->autoDelete()) delete mCommandQueue.first();
  mCommandQueue.removeFirst();
  if (mCommandQueue.count() > 0) {
    sendCommand(mCommandQueue.first()->cmd());
  }
}

bool CommandScheduler::loadProfile(const TQString& filename)
{
  mCommandSet.clear();

  if (!mCommandSet.loadFile(filename)) return false;

  return true;
}

bool CommandScheduler::saveProfile(const TQString& filename)
{
  if (!mCommandSet.saveFile(filename)) return false;

  return true;
}