Merged KIO::JobFlags branch
[kdepim.git] / kmobiletools / mobile-kioslaves / obex / kio_obexprotocol.cpp
blobd38544936c89b3f5f82ec615667fe6d568eec9f5
1 /***************************************************************************
2 * Copyright (C) 2006 by *
3 * Marcin Przylucki <marcin.przylucki@kdemail.net> *
4 * Marco Gulino <marco@kmobiletools.org> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
19 * Boston, MA 02110-1301, USA. *
20 ***************************************************************************/
23 #include "kio_obexprotocol.h"
24 #include "obexwrapper.h"
25 #include "obeximpl.h"
27 #include <q3cstring.h>
28 #include <q3socket.h>
29 #include <qdatetime.h>
30 #include <qbitarray.h>
32 #include <stdlib.h>
33 #include <math.h>
34 #include <unistd.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <netdb.h>
39 #include <kapplication.h>
40 #include <kdebug.h>
41 #include <kmessagebox.h>
42 #include <kinstance.h>
43 #include <kglobal.h>
44 #include <kstandarddirs.h>
45 #include <klocale.h>
46 #include <kurl.h>
47 #include <ksock.h>
48 #include <qfile.h>
51 using namespace KIO;
54 kio_obexProtocol::kio_obexProtocol(const Q3CString &pool_socket, const Q3CString &app_socket)
55 : SlaveBase("kio_obex2", pool_socket, app_socket)
57 kDebug() <<"kio_obexProtocol::kio_obexProtocol()";
62 kio_obexProtocol::~kio_obexProtocol()
64 kDebug() <<"kio_obexProtocol::~kio_obexProtocol()";
65 m_impl.wrapper()->disconnectClient();
68 void kio_obexProtocol::setHost( const QString & host, int port, const QString &user, const QString &pass )
70 kDebug() <<"kio_obexProtocol::setHost(host=" << host <<", port=" << port <<", user=" << user <<", pass=" << pass <<")";
71 QString bhost=user;
72 bhost=bhost.replace('\\', "/");
73 if(bhost.contains("nodevice")) bhost.clear() ;
74 /**
75 this is what i have in mind:
76 * host = the "device" parameter of obexwrapper
77 * port = the "port" parameter
78 * user = the transport, i.e. bt or bluetooth, or irda, usb, and so on...
80 OBEXImpl::transport transport=OBEXImpl::null;
81 if(host.contains( "bt") || host.contains( "bluetooth") ) transport=OBEXImpl::Bluetooth;
82 if(host.contains( "irda") ) transport=OBEXImpl::IrDA;
83 if(host.contains( "inet") ) transport=OBEXImpl::Inet;
84 if(host.contains( "usb") ) transport=OBEXImpl::USB;
85 if(host.contains( "fd") ) transport=OBEXImpl::FD;
86 if(host.contains( "custom") ) transport=OBEXImpl::Custom;
87 if(transport==OBEXImpl::null) transport=OBEXImpl::Bluetooth; // backward compatibility with the kdebluetooth obex kioslave
88 m_impl.setHostConfig(bhost, port, transport);
92 void kio_obexProtocol::listDir(const KUrl &url)
94 kDebug() <<"obexProtocol::listDir:" << url;
95 //if (! setHostConfig( url.host() ) )return;
97 KIO::UDSEntry entry;
98 KIO::UDSEntryList mobile_entries;
100 bool ok = m_impl.listDirectory(url,mobile_entries);
101 if (!ok)
103 error( m_impl.lastErrorCode(), m_impl.lastErrorMessage() );
104 return;
106 totalSize(mobile_entries.count()+1);
108 m_impl.createTopLevelEntry(entry);
110 kDebug() <<"obexProtocol::listDir: mobile_entries.count()+1 =" << mobile_entries.count()+1;
112 listEntry(entry, false);
114 KIO::UDSEntryListIterator it = mobile_entries.begin();
115 KIO::UDSEntryListIterator end = mobile_entries.end();
117 Q3ValueListIterator<UDSAtom> ait;
118 Q3ValueListIterator<UDSAtom> aend;
120 for(; it!=end; ++it)
122 listEntry(*it, false);
125 entry.clear();
126 listEntry(entry, true);
128 finished();
129 disconnectTimeout();
133 void kio_obexProtocol::stat(const KUrl &url)
135 kDebug() <<"**************** obexProtocol::stat:" << url;
137 //if (! setHostConfig( url.host() ) )return;
139 KIO::UDSEntry entry;
140 if ( url.path().isEmpty() || url.path() == "/" )
142 m_impl.createTopLevelEntry( entry );
143 statEntry( entry );
144 finished();
145 return;
147 bool ok = m_impl.statEntry( url, entry );
148 if (!ok)
150 m_impl.createTopLevelEntry(entry);
151 statEntry(entry);
152 finished();
153 return;
155 statEntry( entry );
157 finished();
158 disconnectTimeout();
162 void kio_obexProtocol::listRoot(const KUrl &url)
164 kDebug() <<"obexProtocol::listRoot()";
165 listDir(url);
170 \fn kio_obexProtocol::get(const KUrl &url)
173 void kio_obexProtocol::get(const KUrl &url)
175 kDebug() <<"***************** kio_obex getFile:" << url;
177 if ( m_impl.getFile(url) ) return;
179 int size = m_impl.wrapper()->bufferSize();
181 char* buffer = (char*)m_impl.wrapper()->getClient()->buf_data;
183 kDebug() <<"**************************GetFile processed; size returned:" << size;
185 if(size<=0)
187 finished();
188 return;
191 QByteArray m_data;
192 m_data.setRawData( buffer, size);
193 data( m_data );
194 processedSize( m_data.size() );
196 m_data.resetRawData( buffer, size );
197 data(QByteArray());
198 finished();
199 disconnectTimeout();
204 \fn kio_obexProtocol::put(const KUrl &url, int permissions, bool overwrite, bool resume)
206 void kio_obexProtocol::put(const KUrl &url, int permissions, KIO::JobFlags flags)
208 kDebug() <<"kio_obexProtocol::put() URL:" << url <<"; permissions:" << permissions <<"; overwrite:" << (flags & KIO::Overwrite) <<"; resume:" << (flags & KIO::Resume);
210 QByteArray buffer;
211 dataReq();
212 int ret = 0;
214 int result = readData( buffer );
215 if ( result ) {
216 ret = m_impl.wrapper()->putFile( url.path(), buffer.data(), buffer.size() );
219 kDebug() <<"SendData result code:" << ret;
221 finished();
222 disconnectTimeout();
227 \fn kio_obexProtocol::del (const KUrl &url, bool isfile)
229 void kio_obexProtocol::del (const KUrl &url, bool isfile)
231 kDebug() <<"Deleting" << url <<"; is file?" << isfile <<"; result:" << m_impl.wrapper()->deleteFile( url.path() );
232 finished();
233 disconnectTimeout();
237 void kio_obexProtocol::disconnectTimeout()
239 kDebug() <<"Finished, trying to disconnect.";
240 QByteArray cmd( 1 );
241 cmd[0] = 'd';
242 setTimeoutSpecialCommand( 2, cmd );
245 void kio_obexProtocol::special(const QByteArray &data)
247 if ( data.size() == 1 && data.at(0) == 'd' ) {
248 // we got a disconnect command
249 infoMessage( i18n( "Disconnecting" ) );
250 m_impl.wrapper()->disconnectClient();
251 infoMessage( i18n( "Disconnected" ) );
256 void kio_obexProtocol::mkdir( const KUrl &url, int permissions )
258 kDebug() <<"Makeing dir" << url <<"; permissions" << permissions <<"; result:" << m_impl.wrapper()->mkDir( url.path() );
259 finished();
260 disconnectTimeout();
264 extern "C"
266 int kdemain(int argc, char **argv)
268 KInstance instance( "kio_obex2" );
270 kDebug(7101) <<"*** Starting kio_obex";
272 if (argc != 4)
274 kDebug(7101) <<"Usage: kio_obex protocol domain-socket1 domain-socket2";
275 exit(-1);
278 kio_obexProtocol slave(argv[2], argv[3]);
279 slave.dispatchLoop();
281 kDebug(7101) <<"*** kio_obex Done";
282 return 0;