00001
00002 #include "btobex.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 BtObex::BtObex( 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 BtObex::~BtObex() {
00031 delete m_rec;
00032 delete m_send;
00033 }
00034 void BtObex::receive() {
00035 m_receive = true;
00036 m_outp = QString::null;
00037 m_rec = new OProcess();
00038
00039
00040 *m_rec << "obexftpd" << "-b";
00041
00042 connect(m_rec, SIGNAL(processExited(Opie::Core::OProcess*) ),
00043 this, SLOT(slotExited(Opie::Core::OProcess*) ) );
00044
00045 connect(m_rec, SIGNAL(receivedStdout(Opie::Core::OProcess*, char*, int ) ),
00046 this, SLOT(slotStdOut(Opie::Core::OProcess*, char*, int) ) );
00047
00048 if(!m_rec->start(OProcess::NotifyOnExit, OProcess::AllOutput) ) {
00049 emit done( false );
00050 delete m_rec;
00051 m_rec = 0;
00052 }
00053 }
00054
00055 void BtObex::send( const QString& fileName, const QString& bdaddr) {
00056
00057 m_count = 0;
00058 m_file = fileName;
00059 m_bdaddr = bdaddr;
00060 if (m_rec != 0 ) {
00061 if (m_rec->isRunning() ) {
00062 emit error(-1 );
00063 delete m_rec;
00064 m_rec = 0;
00065
00066 }else{
00067 emit error( -1 );
00068 return;
00069 }
00070 }
00071 sendNow();
00072 }
00073 void BtObex::sendNow(){
00074 if ( m_count >= 25 ) {
00075 emit error(-1 );
00076 emit sent(false);
00077 return;
00078 }
00079
00080 m_send = new OProcess();
00081 m_send->setWorkingDirectory( QFileInfo(m_file).dirPath(true) );
00082
00083
00084
00085
00086 *m_send << "obextool" << "push";
00087 *m_send << QFile::encodeName(QFileInfo(m_file).fileName());
00088 *m_send << m_bdaddr << "9";
00089
00090
00091 connect(m_send, SIGNAL(processExited(Opie::Core::OProcess*) ),
00092 this, SLOT(slotExited(Opie::Core::OProcess*)) );
00093 connect(m_send, SIGNAL(receivedStdout(Opie::Core::OProcess*, char*, int )),
00094 this, SLOT(slotStdOut(Opie::Core::OProcess*, char*, int) ) );
00095
00096
00097 if (!m_send->start( ) ) {
00098 m_count = 25;
00099 emit error(-1 );
00100 delete m_send;
00101 m_send=0;
00102 }
00103
00104 m_count++;
00105 emit currentTry( m_count );
00106 }
00107
00108 void BtObex::slotExited(OProcess* proc ){
00109 if (proc == m_rec )
00110 received();
00111 else if ( proc == m_send )
00112 sendEnd();
00113
00114 }
00115 void BtObex::slotStdOut(OProcess* proc, char* buf, int len){
00116 if ( proc == m_rec ) {
00117 QByteArray ar( len );
00118 memcpy( ar.data(), buf, len );
00119 m_outp.append( ar );
00120 }
00121 }
00122
00123 void BtObex::received() {
00124 if (m_rec->normalExit() ) {
00125 if ( m_rec->exitStatus() == 0 ) {
00126 QString filename = parseOut();
00127 emit receivedFile( filename );
00128 }
00129 }else{
00130 emit done(false);
00131 };
00132 delete m_rec;
00133 m_rec = 0;
00134 receive();
00135 }
00136
00137 void BtObex::sendEnd() {
00138 if (m_send->normalExit() ) {
00139 if ( m_send->exitStatus() == 0 ) {
00140 delete m_send;
00141 m_send=0;
00142 emit sent(true);
00143 }else if (m_send->exitStatus() == 255 ) {
00144
00145 delete m_send;
00146 m_send = 0;
00147 sendNow();
00148 }
00149 }else {
00150 emit error( -1 );
00151 delete m_send;
00152 m_send = 0;
00153 }
00154 }
00155
00156
00157 QString BtObex::parseOut( ){
00158 QString path;
00159 QStringList list = QStringList::split("\n", m_outp);
00160 QStringList::Iterator it;
00161 for (it = list.begin(); it != list.end(); ++it ) {
00162 if ( (*it).startsWith("Wrote" ) ) {
00163 int pos = (*it).findRev('(' );
00164 if ( pos > 0 ) {
00165
00166 path = (*it).remove( pos, (*it).length() - pos );
00167 path = path.mid(6 );
00168 path = path.stripWhiteSpace();
00169 }
00170 }
00171 }
00172 return path;
00173 }
00177 void BtObex::slotError() {
00178 if ( m_receive )
00179 receive();
00180 };
00181 void BtObex::setReceiveEnabled( bool receive ) {
00182 if ( !receive ) {
00183 m_receive = false;
00184 shutDownReceive();
00185 }
00186 }
00187
00188 void BtObex::shutDownReceive() {
00189 if (m_rec != 0 ) {
00190 if (m_rec->isRunning() ) {
00191 emit error(-1 );
00192 delete m_rec;
00193 m_rec = 0;
00194 }
00195 }
00196
00197 }