1 /***************************************************************************
2 * Copyright (C) 2006 by *
3 * Marcin Przylucki <marcin.przylucki@kdemail.net> *
4 * Marco Gulino <marco@kmobiletools.org> *
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. *
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. *
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"
27 #include <q3cstring.h>
29 #include <qdatetime.h>
30 #include <qbitarray.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
39 #include <kapplication.h>
41 #include <kmessagebox.h>
42 #include <kinstance.h>
44 #include <kstandarddirs.h>
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
<<")";
72 bhost
=bhost
.replace('\\', "/");
73 if(bhost
.contains("nodevice")) bhost
.clear() ;
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;
98 KIO::UDSEntryList mobile_entries
;
100 bool ok
= m_impl
.listDirectory(url
,mobile_entries
);
103 error( m_impl
.lastErrorCode(), m_impl
.lastErrorMessage() );
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
;
122 listEntry(*it
, false);
126 listEntry(entry
, true);
133 void kio_obexProtocol::stat(const KUrl
&url
)
135 kDebug() <<"**************** obexProtocol::stat:" << url
;
137 //if (! setHostConfig( url.host() ) )return;
140 if ( url
.path().isEmpty() || url
.path() == "/" )
142 m_impl
.createTopLevelEntry( entry
);
147 bool ok
= m_impl
.statEntry( url
, entry
);
150 m_impl
.createTopLevelEntry(entry
);
162 void kio_obexProtocol::listRoot(const KUrl
&url
)
164 kDebug() <<"obexProtocol::listRoot()";
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
;
192 m_data
.setRawData( buffer
, size
);
194 processedSize( m_data
.size() );
196 m_data
.resetRawData( buffer
, size
);
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
);
214 int result
= readData( buffer
);
216 ret
= m_impl
.wrapper()->putFile( url
.path(), buffer
.data(), buffer
.size() );
219 kDebug() <<"SendData result code:" << ret
;
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() );
237 void kio_obexProtocol::disconnectTimeout()
239 kDebug() <<"Finished, trying to disconnect.";
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() );
266 int kdemain(int argc
, char **argv
)
268 KInstance
instance( "kio_obex2" );
270 kDebug(7101) <<"*** Starting kio_obex";
274 kDebug(7101) <<"Usage: kio_obex protocol domain-socket1 domain-socket2";
278 kio_obexProtocol
slave(argv
[2], argv
[3]);
279 slave
.dispatchLoop();
281 kDebug(7101) <<"*** kio_obex Done";