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