00001
00002 #include "obex.h"
00003
00004
00005 #include <opie2/oprocess.h>
00006 #include <opie2/odebug.h>
00007
00008
00009 #include <qfileinfo.h>
00010
00011
00012
00013 using namespace OpieObex;
00014
00015 using namespace Opie::Core;
00016
00017
00018 Obex::Obex( QObject *parent, const char* name )
00019 : QObject(parent, name )
00020 {
00021 m_rec = 0;
00022 m_send=0;
00023 m_count = 0;
00024 m_receive = false;
00025 connect( this, SIGNAL(error(int) ),
00026 SLOT(slotError() ) );
00027 connect( this, SIGNAL(sent(bool) ),
00028 SLOT(slotError() ) );
00029 };
00030 Obex::~Obex() {
00031 delete m_rec;
00032 delete m_send;
00033 }
00034 void Obex::receive() {
00035 m_receive = true;
00036 m_outp = QString::null;
00037 m_rec = new OProcess();
00038 *m_rec << "irobex_palm3";
00039
00040 connect(m_rec, SIGNAL(processExited(Opie::Core::OProcess*) ),
00041 this, SLOT(slotExited(Opie::Core::OProcess*) ) );
00042
00043 connect(m_rec, SIGNAL(receivedStdout(Opie::Core::OProcess*, char*, int ) ),
00044 this, SLOT(slotStdOut(Opie::Core::OProcess*, char*, int) ) );
00045
00046 if(!m_rec->start(OProcess::NotifyOnExit, OProcess::AllOutput) ) {
00047 emit done( false );
00048 delete m_rec;
00049 m_rec = 0;
00050 }
00051 }
00052
00053 void Obex::send( const QString& fileName) {
00054 m_count = 0;
00055 m_file = fileName;
00056 if (m_rec != 0 ) {
00057 if (m_rec->isRunning() ) {
00058 emit error(-1 );
00059 delete m_rec;
00060 m_rec = 0;
00061
00062 }else{
00063 emit error( -1 );
00064 return;
00065 }
00066 }
00067 sendNow();
00068 }
00069 void Obex::sendNow(){
00070 if ( m_count >= 25 ) {
00071 emit error(-1 );
00072 emit sent(false);
00073 return;
00074 }
00075
00076 m_send = new OProcess();
00077 m_send->setWorkingDirectory( QFileInfo(m_file).dirPath(true) );
00078
00079 *m_send << "irobex_palm3";
00080 *m_send << QFile::encodeName(QFileInfo(m_file).fileName());
00081
00082
00083 connect(m_send, SIGNAL(processExited(Opie::Core::OProcess*) ),
00084 this, SLOT(slotExited(Opie::Core::OProcess*)) );
00085 connect(m_send, SIGNAL(receivedStdout(Opie::Core::OProcess*, char*, int )),
00086 this, SLOT(slotStdOut(Opie::Core::OProcess*, char*, int) ) );
00087
00088
00089 if (!m_send->start( ) ) {
00090 m_count = 25;
00091 emit error(-1 );
00092 delete m_send;
00093 m_send=0;
00094 }
00095
00096 m_count++;
00097 emit currentTry( m_count );
00098 }
00099
00100 void Obex::slotExited(OProcess* proc ){
00101 if (proc == m_rec )
00102 received();
00103 else if ( proc == m_send )
00104 sendEnd();
00105
00106 }
00107 void Obex::slotStdOut(OProcess* proc, char* buf, int len){
00108 if ( proc == m_rec ) {
00109 QByteArray ar( len );
00110 memcpy( ar.data(), buf, len );
00111 m_outp.append( ar );
00112 }
00113 }
00114
00115 void Obex::received() {
00116 if (m_rec->normalExit() ) {
00117 if ( m_rec->exitStatus() == 0 ) {
00118 QString filename = parseOut();
00119 emit receivedFile( filename );
00120 }
00121 }else{
00122 emit done(false);
00123 };
00124 delete m_rec;
00125 m_rec = 0;
00126 receive();
00127 }
00128
00129 void Obex::sendEnd() {
00130 if (m_send->normalExit() ) {
00131 if ( m_send->exitStatus() == 0 ) {
00132 delete m_send;
00133 m_send=0;
00134 emit sent(true);
00135 }else if (m_send->exitStatus() == 255 ) {
00136
00137 delete m_send;
00138 m_send = 0;
00139 sendNow();
00140 }
00141 }else {
00142 emit error( -1 );
00143 delete m_send;
00144 m_send = 0;
00145 }
00146 }
00147 QString Obex::parseOut( ){
00148 QString path;
00149 QStringList list = QStringList::split("\n", m_outp);
00150 QStringList::Iterator it;
00151 for (it = list.begin(); it != list.end(); ++it ) {
00152 if ( (*it).startsWith("Wrote" ) ) {
00153 int pos = (*it).findRev('(' );
00154 if ( pos > 0 ) {
00155
00156 path = (*it).remove( pos, (*it).length() - pos );
00157 path = path.mid(6 );
00158 path = path.stripWhiteSpace();
00159 }
00160 }
00161 }
00162 return path;
00163 }
00167 void Obex::slotError() {
00168 if ( m_receive )
00169 receive();
00170 };
00171 void Obex::setReceiveEnabled( bool receive ) {
00172 if ( !receive ) {
00173 m_receive = false;
00174 shutDownReceive();
00175 }
00176 }
00177
00178 void Obex::shutDownReceive() {
00179 if (m_rec != 0 ) {
00180 if (m_rec->isRunning() ) {
00181 emit error(-1 );
00182 delete m_rec;
00183 m_rec = 0;
00184 }
00185 }
00186
00187 }