commandscheduler.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 <kdebug.h> 00026 #include <klocale.h> 00027 00028 #include "modem.h" 00029 00030 #include "commandscheduler.h" 00031 #include "commandscheduler.moc" 00032 00033 CommandScheduler::CommandScheduler(Modem *modem,TQObject *parent, 00034 const char *name) : 00035 TQObject(parent,name), 00036 mModem(modem) 00037 { 00038 connect(mModem,TQT_SIGNAL(gotLine(const char *)), 00039 TQT_SLOT(processOutput(const char *))); 00040 } 00041 00042 void CommandScheduler::execute(ATCommand *command) 00043 { 00044 if (!mModem->isOpen()) { 00045 kdDebug(5960) << "Warning! Modem not open." << endl; 00046 return; 00047 } 00048 00049 mCommandQueue.append(command); 00050 00051 // if (mCommandQueue.count() == 1) sendCommand(command->cmdString()); 00052 if (mCommandQueue.count() == 1) sendCommand(command->cmd()); 00053 } 00054 00055 void CommandScheduler::execute(const TQString &command) 00056 { 00057 ATCommand *cmd = new ATCommand("",command); 00058 cmd->setAutoDelete(true); 00059 00060 execute(cmd); 00061 } 00062 00063 void CommandScheduler::executeId(const TQString &id) 00064 { 00065 TQPtrList<ATCommand> *cmds = mCommandSet.commandList(); 00066 00067 for(uint i=0;i<cmds->count();++i) { 00068 if (cmds->at(i)->id() == id) { 00069 execute(cmds->at(i)); 00070 return; 00071 } 00072 } 00073 kdDebug(5960) << "CommandScheduler::executeId(): Id '" << id << "' not found" << endl; 00074 } 00075 00076 void CommandScheduler::sendCommand(const TQString &command) 00077 { 00078 if (command.isEmpty()) { 00079 kdDebug(5960) << "CommandScheduler::sendCommand(): Warning! Empty command." 00080 << endl; 00081 return; 00082 } 00083 00084 kdDebug(5960) << "CommandScheduler:sendCommand(): " << command << endl; 00085 00086 mModem->writeLine(command.latin1()); 00087 } 00088 00089 00090 void CommandScheduler::processOutput(const char *line) 00091 { 00092 TQString l = line; 00093 ATCommand *cmd = mCommandQueue.first(); 00094 if (l == "OK") { 00095 mState = WAITING; 00096 emit result(mResult); 00097 cmd->setResultString(mResult); 00098 emit commandProcessed(cmd); 00099 nextCommand(); 00100 } else if (l == "ERROR") { 00101 mState = WAITING; 00102 emit result(i18n("Error")); 00103 nextCommand(); 00104 } else { 00105 if (mState == WAITING) { 00106 mState = PROCESSING; 00107 mResult = ""; 00108 } else if (mState == PROCESSING) { 00109 if (!l.isEmpty()) { 00110 mResult += l; 00111 mResult += "\n"; 00112 } 00113 } 00114 } 00115 } 00116 00117 void CommandScheduler::nextCommand() 00118 { 00119 if (mCommandQueue.first()->autoDelete()) delete mCommandQueue.first(); 00120 mCommandQueue.removeFirst(); 00121 if (mCommandQueue.count() > 0) { 00122 sendCommand(mCommandQueue.first()->cmd()); 00123 } 00124 } 00125 00126 bool CommandScheduler::loadProfile(const TQString& filename) 00127 { 00128 mCommandSet.clear(); 00129 00130 if (!mCommandSet.loadFile(filename)) return false; 00131 00132 return true; 00133 } 00134 00135 bool CommandScheduler::saveProfile(const TQString& filename) 00136 { 00137 if (!mCommandSet.saveFile(filename)) return false; 00138 00139 return true; 00140 }