summaryrefslogtreecommitdiff
path: root/core/obex/obex.cpp
Unidiff
Diffstat (limited to 'core/obex/obex.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--core/obex/obex.cpp187
1 files changed, 187 insertions, 0 deletions
diff --git a/core/obex/obex.cpp b/core/obex/obex.cpp
new file mode 100644
index 0000000..36634ec
--- a/dev/null
+++ b/core/obex/obex.cpp
@@ -0,0 +1,187 @@
1
2#include "obex.h"
3
4/* OPIE */
5#include <opie2/oprocess.h>
6#include <opie2/odebug.h>
7
8/* QT */
9#include <qfileinfo.h>
10
11
12
13using namespace OpieObex;
14
15using namespace Opie::Core;
16/* TRANSLATOR OpieObex::Obex */
17
18Obex::Obex( QObject *parent, const char* name )
19 : QObject(parent, name )
20{
21 m_rec = 0;
22 m_send=0;
23 m_count = 0;
24 m_receive = false;
25 connect( this, SIGNAL(error(int) ), // for recovering to receive
26 SLOT(slotError() ) );
27 connect( this, SIGNAL(sent(bool) ),
28 SLOT(slotError() ) );
29};
30Obex::~Obex() {
31 delete m_rec;
32 delete m_send;
33}
34void Obex::receive() {
35 m_receive = true;
36 m_outp = QString::null;
37 m_rec = new OProcess();
38 *m_rec << "irobex_palm3";
39 // connect to the necessary slots
40 connect(m_rec, SIGNAL(processExited(Opie::Core::OProcess*) ),
41 this, SLOT(slotExited(Opie::Core::OProcess*) ) );
42
43 connect(m_rec, SIGNAL(receivedStdout(Opie::Core::OProcess*, char*, int ) ),
44 this, SLOT(slotStdOut(Opie::Core::OProcess*, char*, int) ) );
45
46 if(!m_rec->start(OProcess::NotifyOnExit, OProcess::AllOutput) ) {
47 emit done( false );
48 delete m_rec;
49 m_rec = 0;
50 }
51}
52
53void Obex::send( const QString& fileName) { // if currently receiving stop it send receive
54 m_count = 0;
55 m_file = fileName;
56 if (m_rec != 0 ) {
57 if (m_rec->isRunning() ) {
58 emit error(-1 );
59 delete m_rec;
60 m_rec = 0;
61
62 }else{
63 emit error( -1 ); // we did not delete yet but it's not running slotExited is pending
64 return;
65 }
66 }
67 sendNow();
68}
69void Obex::sendNow(){
70 if ( m_count >= 25 ) { // could not send
71 emit error(-1 );
72 emit sent(false);
73 return;
74 }
75 // OProcess inititialisation
76 m_send = new OProcess();
77 m_send->setWorkingDirectory( QFileInfo(m_file).dirPath(true) );
78
79 *m_send << "irobex_palm3";
80 *m_send << QFile::encodeName(QFileInfo(m_file).fileName());
81
82 // connect to slots Exited and and StdOut
83 connect(m_send, SIGNAL(processExited(Opie::Core::OProcess*) ),
84 this, SLOT(slotExited(Opie::Core::OProcess*)) );
85 connect(m_send, SIGNAL(receivedStdout(Opie::Core::OProcess*, char*, int )),
86 this, SLOT(slotStdOut(Opie::Core::OProcess*, char*, int) ) );
87
88 // now start it
89 if (!m_send->start(/*OProcess::NotifyOnExit, OProcess::AllOutput*/ ) ) {
90 m_count = 25;
91 emit error(-1 );
92 delete m_send;
93 m_send=0;
94 }
95 // end
96 m_count++;
97 emit currentTry( m_count );
98}
99
100void Obex::slotExited(OProcess* proc ){
101 if (proc == m_rec ) // receive process
102 received();
103 else if ( proc == m_send )
104 sendEnd();
105
106}
107void Obex::slotStdOut(OProcess* proc, char* buf, int len){
108 if ( proc == m_rec ) { // only receive
109 QByteArray ar( len );
110 memcpy( ar.data(), buf, len );
111 m_outp.append( ar );
112 }
113}
114
115void Obex::received() {
116 if (m_rec->normalExit() ) {
117 if ( m_rec->exitStatus() == 0 ) { // we got one
118 QString filename = parseOut();
119 emit receivedFile( filename );
120 }
121 }else{
122 emit done(false);
123 };
124 delete m_rec;
125 m_rec = 0;
126 receive();
127}
128
129void Obex::sendEnd() {
130 if (m_send->normalExit() ) {
131 if ( m_send->exitStatus() == 0 ) {
132 delete m_send;
133 m_send=0;
134 emit sent(true);
135 }else if (m_send->exitStatus() == 255 ) { // it failed maybe the other side wasn't ready
136 // let's try it again
137 delete m_send;
138 m_send = 0;
139 sendNow();
140 }
141 }else {
142 emit error( -1 );
143 delete m_send;
144 m_send = 0;
145 }
146}
147QString Obex::parseOut( ){
148 QString path;
149 QStringList list = QStringList::split("\n", m_outp);
150 QStringList::Iterator it;
151 for (it = list.begin(); it != list.end(); ++it ) {
152 if ( (*it).startsWith("Wrote" ) ) {
153 int pos = (*it).findRev('(' );
154 if ( pos > 0 ) {
155
156 path = (*it).remove( pos, (*it).length() - pos );
157 path = path.mid(6 );
158 path = path.stripWhiteSpace();
159 }
160 }
161 }
162 return path;
163}
164/**
165 * when sent is done slotError is called we will start receive again
166 */
167void Obex::slotError() {
168 if ( m_receive )
169 receive();
170};
171void Obex::setReceiveEnabled( bool receive ) {
172 if ( !receive ) { //
173 m_receive = false;
174 shutDownReceive();
175 }
176}
177
178void Obex::shutDownReceive() {
179 if (m_rec != 0 ) {
180 if (m_rec->isRunning() ) {
181 emit error(-1 );
182 delete m_rec;
183 m_rec = 0;
184 }
185 }
186
187}