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