kandy Library API Documentation

commandscheduler.cpp

00001 // $Id: commandscheduler.cpp,v 1.4 2001/09/14 06:57:29 mlaurent Exp $
00002 
00003 #include <kdebug.h>
00004 #include <klocale.h>
00005 
00006 #include "modem.h"
00007 
00008 #include "commandscheduler.h"
00009 #include "commandscheduler.moc"
00010 
00011 CommandScheduler::CommandScheduler(Modem *modem,QObject *parent,
00012                                    const char *name) :
00013   QObject(parent,name),
00014   mModem(modem)
00015 {
00016   connect(mModem,SIGNAL(gotLine(const char *)),
00017           SLOT(processOutput(const char *)));
00018 }
00019 
00020 void CommandScheduler::execute(ATCommand *command)
00021 {
00022   if (!mModem->isOpen()) {
00023     kdDebug() << "Warning! Modem not open." << endl;
00024     return;
00025   }
00026 
00027   mCommandQueue.append(command);
00028 
00029 //  if (mCommandQueue.count() == 1) sendCommand(command->cmdString());
00030   if (mCommandQueue.count() == 1) sendCommand(command->cmd());
00031 }
00032 
00033 void CommandScheduler::execute(const QString &command)
00034 {
00035   ATCommand *cmd = new ATCommand("",command);
00036   cmd->setAutoDelete(true);
00037 
00038   execute(cmd);
00039 }
00040 
00041 void CommandScheduler::executeId(const QString &id)
00042 {
00043   QPtrList<ATCommand> *cmds = mCommandSet.commandList();
00044 
00045   for(uint i=0;i<cmds->count();++i) {
00046     if (cmds->at(i)->id() == id) {
00047       execute(cmds->at(i));
00048       return;
00049     }
00050   }
00051   kdDebug() << "CommandScheduler::executeId(): Id '" << id << "' not found" << endl;
00052 }
00053 
00054 void CommandScheduler::sendCommand(const QString &command)
00055 {
00056   if (command.isEmpty()) {
00057     kdDebug() << "CommandScheduler::sendCommand(): Warning! Empty command."
00058               << endl;
00059     return;
00060   }
00061 
00062 //  kdDebug() << "CommandScheduler:sendCommand(): " << command.latin1() << endl;
00063 
00064   mModem->writeLine(command.latin1());
00065 }
00066 
00067 
00068 void CommandScheduler::processOutput(const char *line)
00069 {
00070   QString l = line;
00071   ATCommand *cmd = mCommandQueue.first();
00072   if (l == "OK") {
00073     mState = WAITING;
00074     emit result(mResult);
00075     cmd->setResultString(mResult);
00076     emit commandProcessed(cmd);
00077     nextCommand();
00078   } else if (l == "ERROR") {
00079     mState = WAITING;
00080     emit result(i18n("Error"));
00081     nextCommand();
00082   } else {
00083     if (mState == WAITING) {
00084       mState = PROCESSING;
00085       mResult = "";
00086     } else if (mState == PROCESSING) {
00087       if (!l.isEmpty()) {
00088         mResult += l;
00089         mResult += "\n";
00090       }
00091     }
00092   }
00093 }
00094 
00095 void CommandScheduler::nextCommand()
00096 {
00097   if (mCommandQueue.first()->autoDelete()) delete mCommandQueue.first();
00098   mCommandQueue.removeFirst();
00099   if (mCommandQueue.count() > 0) {
00100     sendCommand(mCommandQueue.first()->cmd());
00101   }
00102 }
00103 
00104 bool CommandScheduler::loadProfile(const QString& filename)
00105 {
00106   mCommandSet.clear();
00107 
00108   if (!mCommandSet.loadFile(filename)) return false;
00109 
00110   return true;
00111 }
00112 
00113 bool CommandScheduler::saveProfile(const QString& filename)
00114 {
00115   if (!mCommandSet.saveFile(filename)) return false;
00116 
00117   return true;
00118 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 15 11:40:32 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001