21
21
***************************************************************************/
23
23
#include "kio_obexftp.h"
24
#include "obexftpmanager.h"
25
#include "obexftpsession.h"
24
#include "kdedobexftp.h"
28
27
#include <KComponentData>
29
28
#include <KCmdLineArgs>
30
29
#include <KAboutData>
31
#include <KTemporaryFile>
32
33
#include <KApplication>
34
#define ENSURE_SESSION_CREATED(url) if (!m_session) { \
35
if (!createSession(url)) { \
40
35
extern "C" int KDE_EXPORT kdemain(int argc, char **argv)
42
37
KAboutData about("kioobexftp","kioobexftp", ki18n("kioobexftp"), 0);
57
52
KioFtp::KioFtp(const QByteArray &pool, const QByteArray &app)
58
: SlaveBase("obexftp", pool, app), m_manager(0), m_session(0)
53
: SlaveBase("obexftp", pool, app)
55
m_settingHost = false;
60
57
m_timer = new QTimer();
61
58
m_timer->setInterval(100);
62
connect(m_timer, SIGNAL(timeout()), this, SLOT(updateProcess()));
60
m_kded = new org::kde::ObexFtp("org.kde.kded", "/modules/obexftpdaemon", QDBusConnection::sessionBus(), 0);
61
connect(m_kded, SIGNAL(sessionConnected(QString)), SLOT(sessionConnected(QString)));
67
m_session->Disconnect().waitForFinished();
68
m_session->Close().waitForFinished();
73
bool KioFtp::createSession(const KUrl &address)
75
if (address.host().isEmpty()) {
76
kDebug() << "No host";
77
error (KIO::ERR_UNKNOWN_HOST, address.prettyUrl());
81
infoMessage(i18n("Connecting to the remote device..."));
84
m_address = address.host();
85
kDebug() << "Got address: " << m_address;
87
m_manager = new org::openobex::Manager("org.openobex", "/org/openobex", QDBusConnection::sessionBus(), 0);
88
connect(m_manager, SIGNAL(SessionConnected(QDBusObjectPath)), this, SLOT(sessionCreated(QDBusObjectPath)));
90
kDebug() << "Creaing the bluetooth session: ";
91
QDBusPendingReply <QDBusObjectPath > rep = m_manager->CreateBluetoothSession(QString(m_address).replace("-", ":"), "00:00:00:00:00:00", "ftp");
95
kDebug() << "SessionError: " << rep.error().message();
96
kDebug() << "SessionPath: " << rep.value().path();
98
const QString sessioPath = rep.value().path();
99
m_session = new org::openobex::Session("org.openobex", sessioPath, QDBusConnection::sessionBus(), 0);
101
kDebug() << "Create session ends";
105
void KioFtp::changeDirectory(const KUrl& url)
107
kDebug() << "ChangeUrl: " << url.path();
108
QStringList list = url.path().split("/");
110
kDebug() << "List of itens: " << list;
112
m_session->ChangeCurrentFolderToRoot().waitForFinished();
113
kDebug() << "We're in root now";
115
Q_FOREACH(const QString &dir, list) {
116
if (!dir.isEmpty() && dir != m_address) {
117
kDebug() << "Changing to: " << dir;
118
QDBusPendingReply <void > a = m_session->ChangeCurrentFolder(dir);
120
kDebug() << "Change Error: " << a.error().message();
122
kDebug() << "Skyping" << dir;
127
69
void KioFtp::launchProgressBar()
71
connect(m_timer, SIGNAL(timeout()), this, SLOT(updateProcess()));
145
90
void KioFtp::listDir(const KUrl &url)
147
92
kDebug() << "listdir: " << url;
148
ENSURE_SESSION_CREATED(url)
150
94
infoMessage(i18n("Retrieving information from remote device..."));
152
if (url.fileName() != "/" && !url.fileName().isEmpty()) {
153
changeDirectory(url);
156
QDBusPendingReply<QString> folder = m_session->RetrieveFolderListing();
96
blockUntilNotBusy(url.host());
97
QDBusPendingReply<QString> folder = m_kded->listDir(url.host(), url.path());
157
98
folder.waitForFinished();
159
100
kDebug() << folder.value();
172
113
kDebug() << "copy: " << src.url() << " to " << dest.url();
173
connect(m_session, SIGNAL(TransferProgress(qulonglong)), this, SLOT(TransferProgress(qulonglong)));
174
connect(m_session, SIGNAL(TransferCompleted()), this, SLOT(TransferCompleted()));
175
connect(m_session, SIGNAL(ErrorOccurred(QString,QString)), this, SLOT(ErrorOccurred(QString,QString)));
177
if (src.scheme() == "obexftp") {
178
if (m_statMap.contains(src.prettyUrl())) {
179
if (m_statMap.value(src.prettyUrl()).isDir()) {
180
kDebug() << "Skipping to copy: " << src.prettyUrl();
181
error( KIO::ERR_IS_DIRECTORY, src.prettyUrl());
182
disconnect(m_session, SIGNAL(TransferProgress(qulonglong)), this, SLOT(TransferProgress(qulonglong)));
183
disconnect(m_session, SIGNAL(TransferCompleted()), this, SLOT(TransferCompleted()));
184
disconnect(m_session, SIGNAL(ErrorOccurred(QString,QString)), this, SLOT(ErrorOccurred(QString,QString)));
188
ENSURE_SESSION_CREATED(src)
189
changeDirectory(src.directory());
190
kDebug() << "CopyingRemoteFile....";
191
m_session->CopyRemoteFile(src.fileName(), dest.path());
192
kDebug() << "Copied";
193
} else if (dest.scheme() == "obexftp") {
194
ENSURE_SESSION_CREATED(dest)
195
changeDirectory(dest.directory());
196
kDebug() << "Sendingfile....";
197
m_session->SendFile(src.path());
198
kDebug() << "Copied";
115
copyHelper(src, dest);
130
void KioFtp::get(const KUrl& url)
132
KTemporaryFile tempFile;
133
tempFile.setSuffix(url.fileName());
134
tempFile.open();//Create the file
135
kDebug() << tempFile.fileName();
137
copyHelper(url, KUrl(tempFile.fileName()));
139
kDebug() << "Getting mimetype";
140
KMimeType::Ptr mime = KMimeType::findByPath(tempFile.fileName());
141
mimeType(mime->name());
142
kDebug() << "Mime: " << mime->name();
144
kDebug() << tempFile.size();
145
totalSize(tempFile.size());
147
data(tempFile.readAll());
217
153
void KioFtp::setHost(const QString &host, quint16 port, const QString &user, const QString &pass)
240
184
Q_UNUSED(permissions)
242
186
kDebug() << "MkDir: " << url.url();
243
ENSURE_SESSION_CREATED(url)
244
changeDirectory(url.directory());
245
m_session->CreateFolder(url.fileName()).waitForFinished();
187
blockUntilNotBusy(url.host());
188
m_kded->createFolder(url.host(), url.path()).waitForFinished();
249
void KioFtp::slave_status()
251
kDebug() << "Slave status";
252
KIO::SlaveBase::slave_status();
255
192
void KioFtp::stat(const KUrl &url)
257
194
kDebug() << "Stat: " << url.url();
258
195
kDebug() << "Stat Dir: " << url.directory();
259
196
kDebug() << "Stat File: " << url.fileName();
260
ENSURE_SESSION_CREATED(url)
261
if (url.directory() == "/" && url.fileName().isEmpty()) {
197
kDebug() << "Empty Dir: " << url.directory().isEmpty();
199
if ((url.directory() == "/" || url.directory().isEmpty()) && url.fileName().isEmpty()) {
200
kDebug() << "Url is root";
262
201
KIO::UDSEntry entry;
263
202
entry.insert(KIO::UDSEntry::UDS_NAME, QString::fromLatin1("/"));
264
203
entry.insert(KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR);
276
215
kDebug() << "statMap NOT contains the url";
277
changeDirectory(url.directory());
279
217
kDebug() << "RetrieveFolderListing";
280
QDBusPendingReply<QString> folder = m_session->RetrieveFolderListing();
218
blockUntilNotBusy(url.host());
219
QDBusPendingReply<QString> folder = m_kded->listDir(url.host(), url.directory());
281
220
kDebug() << "retireve called";
282
221
folder.waitForFinished();
283
222
kDebug() << "RetrieveError: " << folder.error().message();
284
223
kDebug() << folder.value();
286
processXmlEntries(url, folder.value(), "statCallback");
225
processXmlEntries(url.upUrl(), folder.value(), "statCallback");
229
kDebug() << "Finished";
358
void KioFtp::sessionCreated(const QDBusObjectPath& path)
361
kDebug() << "session Created!";
362
disconnect(m_manager, SIGNAL(SessionConnected(QDBusObjectPath)), this, SLOT(sessionCreated(QDBusObjectPath)));
366
298
void KioFtp::TransferProgress(qulonglong transfered)
368
300
processedSize(transfered);
369
302
kDebug() << "TransferProgress: ";
372
305
void KioFtp::TransferCompleted()
374
307
kDebug() << "TransferCompleted: ";
375
disconnect(m_session, SIGNAL(TransferProgress(qulonglong)), this, SLOT(TransferProgress(qulonglong)));
376
disconnect(m_session, SIGNAL(TransferCompleted()), this, SLOT(TransferCompleted()));
377
disconnect(m_session, SIGNAL(ErrorOccurred(QString,QString)), this, SLOT(ErrorOccurred(QString,QString)));
308
disconnect(m_kded, SIGNAL(Cancelled()), this, SLOT(TransferCancelled()));
309
disconnect(m_kded, SIGNAL(transferProgress(qulonglong)), this, SLOT(TransferProgress(qulonglong)));
310
disconnect(m_kded, SIGNAL(transferCompleted()), this, SLOT(TransferCompleted()));
311
disconnect(m_kded, SIGNAL(errorOccurred(QString,QString)), this, SLOT(ErrorOccurred(QString,QString)));
316
void KioFtp::TransferCancelled()
318
kDebug() << "TransferCancelled";
319
disconnect(m_kded, SIGNAL(Cancelled()), this, SLOT(TransferCancelled()));
320
disconnect(m_kded, SIGNAL(transferProgress(qulonglong)), this, SLOT(TransferProgress(qulonglong)));
321
disconnect(m_kded, SIGNAL(transferCompleted()), this, SLOT(TransferCompleted()));
322
disconnect(m_kded, SIGNAL(errorOccurred(QString,QString)), this, SLOT(ErrorOccurred(QString,QString)));
323
error(KIO::ERR_USER_CANCELED, "");
381
328
void KioFtp::ErrorOccurred(const QString &name, const QString &msg)
383
disconnect(m_session, SIGNAL(TransferProgress(qulonglong)), this, SLOT(TransferProgress(qulonglong)));
384
disconnect(m_session, SIGNAL(TransferCompleted()), this, SLOT(TransferCompleted()));
385
disconnect(m_session, SIGNAL(ErrorOccurred(QString,QString)), this, SLOT(ErrorOccurred(QString,QString)));
330
// disconnect(m_session, SIGNAL(TransferProgress(qulonglong)), this, SLOT(TransferProgress(qulonglong)));
331
// disconnect(m_session, SIGNAL(TransferCompleted()), this, SLOT(TransferCompleted()));
332
// disconnect(m_session, SIGNAL(ErrorOccurred(QString,QString)), this, SLOT(ErrorOccurred(QString,QString)));
387
334
kDebug() << "ERROR ERROR: " << name;
388
335
kDebug() << "ERROR ERROR: " << msg;
392
339
m_eventLoop.exit();
343
void KioFtp::sessionConnected(QString address)
345
kDebug() << "Session connected: " << address;
352
void KioFtp::copyHelper(const KUrl& src, const KUrl& dest)
354
connect(m_kded, SIGNAL(Cancelled()), this, SLOT(TransferCancelled()));
355
connect(m_kded, SIGNAL(transferProgress(qulonglong)), this, SLOT(TransferProgress(qulonglong)));
356
connect(m_kded, SIGNAL(transferCompleted()), this, SLOT(TransferCompleted()));
357
connect(m_kded, SIGNAL(errorOccurred(QString,QString)), this, SLOT(ErrorOccurred(QString,QString)));
359
if (src.scheme() == "obexftp" && dest.scheme() == "obexftp") {
360
error(KIO::ERR_UNSUPPORTED_ACTION, src.prettyUrl());
364
if (src.scheme() == "obexftp") {
365
kDebug() << "scheme is obexftp";
366
kDebug() << src.prettyUrl();
367
//Just in case the url is not in the stat, some times happens...
368
if (!m_statMap.contains(src.prettyUrl())) {
369
kDebug() << "The url is not in the cache, stating it";
373
if (m_statMap.value(src.prettyUrl()).isDir()) {
374
kDebug() << "Skipping to copy: " << src.prettyUrl();
375
error( KIO::ERR_IS_DIRECTORY, src.prettyUrl());
376
disconnect(m_kded, SIGNAL(Cancelled()), this, SLOT(TransferCancelled()));
377
disconnect(m_kded, SIGNAL(transferProgress(qulonglong)), this, SLOT(TransferProgress(qulonglong)));
378
disconnect(m_kded, SIGNAL(transferCompleted()), this, SLOT(TransferCompleted()));
379
disconnect(m_kded, SIGNAL(errorOccurred(QString,QString)), this, SLOT(ErrorOccurred(QString,QString)));
383
kDebug() << "CopyingRemoteFile....";
384
blockUntilNotBusy(src.host());
385
m_kded->copyRemoteFile(src.host(), src.path(), dest.path());
386
} else if (dest.scheme() == "obexftp") {
387
kDebug() << "Sendingfile....";
388
blockUntilNotBusy(dest.host());
389
m_kded->sendFile(dest.host(), src.path(), dest.directory());
393
kDebug() << "Copy end";
397
void KioFtp::blockUntilNotBusy(QString address)
399
if (m_kded->isBusy(address).value()) {
400
infoMessage(i18n("The device is busy, waiting..."));
401
while (m_kded->isBusy(address).value() == true) {
402
kDebug() << "Blocking, kded is busy";
406
kDebug() << "kded is free";
409
void KioFtp::wasKilledCheck()
412
kDebug() << "slave was killed!";
413
m_kded->Cancel(m_address).waitForFinished();;
416
kDebug() << "Slave is alive";