~ubuntu-branches/ubuntu/natty/bluedevil/natty

« back to all changes in this revision

Viewing changes to .pc/kubuntu_01_fix_obexdaemon_connect_crash.diff/src/daemon/obexftpkded/ObexFtpDaemon.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Rohan Garg
  • Date: 2010-11-13 00:09:01 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20101113000901-prx5l5lhfj3j7rap
Tags: 1.0-0ubuntu1
* New upstream release
  - Drop kubuntu_01_fix_obexdaemon_connect_crash.diff, applied upstream
* Closes LP: #658728 "bluedevil translations not being used"

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *   Copyright (C) 2010 Alejandro Fiestas Olivares <alex@eyeos.org>        *
3
 
 *   Copyright (C) 2010 UFO Coders <info@ufocoders.com>                    *
4
 
 *                                                                         *
5
 
 *   This program is free software; you can redistribute it and/or modify  *
6
 
 *   it under the terms of the GNU General Public License as published by  *
7
 
 *   the Free Software Foundation; either version 2 of the License, or     *
8
 
 *   (at your option) any later version.                                   *
9
 
 *                                                                         *
10
 
 *   This program is distributed in the hope that it will be useful,       *
11
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13
 
 *   GNU General Public License for more details.                          *
14
 
 *                                                                         *
15
 
 *   You should have received a copy of the GNU General Public License     *
16
 
 *   along with this program; if not, write to the                         *
17
 
 *   Free Software Foundation, Inc.,                                       *
18
 
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
19
 
 ***************************************************************************/
20
 
 
21
 
#include "ObexFtpDaemon.h"
22
 
#include "obexftpmanager.h"
23
 
#include "obexsession.h"
24
 
 
25
 
#include <QVariantMap>
26
 
#include <QHash>
27
 
 
28
 
#include <kdemacros.h>
29
 
#include <KDebug>
30
 
#include <KAboutData>
31
 
#include <KPluginFactory>
32
 
#include <kfileplacesmodel.h>
33
 
#include <kprocess.h>
34
 
 
35
 
#include <bluedevil/bluedevilmanager.h>
36
 
#include <bluedevil/bluedeviladapter.h>
37
 
 
38
 
#define ENSURE_SESSION_CREATED(address) if (!d->m_sessionMap.contains(address)) { \
39
 
        kDebug() << "The address " << address << " doesn't has a session"; \
40
 
        stablishConnection(address); \
41
 
        return; \
42
 
    } \
43
 
    if (d->m_sessionMap[address]->status() == ObexSession::Connecting) { \
44
 
        kDebug() << "The session is waiting to be connected"; \
45
 
        return; \
46
 
    }
47
 
 
48
 
using namespace BlueDevil;
49
 
K_PLUGIN_FACTORY(ObexFtpFactory,
50
 
                 registerPlugin<ObexFtpDaemon>();)
51
 
K_EXPORT_PLUGIN(ObexFtpFactory("obexftpdaemon", "obexftpdaemon"))
52
 
 
53
 
struct ObexFtpDaemon::Private
54
 
{
55
 
    enum Status {
56
 
        Online = 0,
57
 
        Offline
58
 
    } m_status;
59
 
 
60
 
    QHash <QString, ObexSession*> m_sessionMap;
61
 
 
62
 
    org::openobex::Manager  *m_manager;
63
 
 
64
 
    QEventLoop loop;
65
 
};
66
 
 
67
 
ObexFtpDaemon::ObexFtpDaemon(QObject *parent, const QList<QVariant>&)
68
 
    : KDEDModule(parent)
69
 
    , d(new Private)
70
 
{
71
 
    KAboutData aboutData(
72
 
        "obexftpdaemon",
73
 
        "obexftpdaemon",
74
 
        ki18n("ObexFtp Daemon"),
75
 
        "1.0",
76
 
        ki18n("ObexFtp Daemon"),
77
 
        KAboutData::License_GPL,
78
 
        ki18n("(c) 2010, UFO Coders")
79
 
    );
80
 
 
81
 
    aboutData.addAuthor(ki18n("Alejandro Fiestas Olivares"), ki18n("Maintainer"), "alex@ufocoders.com",
82
 
        "http://www.afiestas.org");
83
 
 
84
 
    connect(Manager::self(), SIGNAL(defaultAdapterChanged(Adapter*)),
85
 
            this, SLOT(defaultAdapterChanged(Adapter*)));
86
 
 
87
 
    d->m_status = Private::Offline;
88
 
    if (Manager::self()->defaultAdapter()) {
89
 
        onlineMode();
90
 
    }
91
 
 
92
 
    qDBusRegisterMetaType<QStringMap>();
93
 
    qRegisterMetaType<QStringMap>("QStringMap");
94
 
 
95
 
    connect(d->m_manager, SIGNAL(SessionConnected(QDBusObjectPath)), this, SLOT(SessionConnected(QDBusObjectPath)));
96
 
    connect(d->m_manager, SIGNAL(SessionClosed(QDBusObjectPath)), this, SLOT(SessionClosed(QDBusObjectPath)));
97
 
}
98
 
 
99
 
ObexFtpDaemon::~ObexFtpDaemon()
100
 
{
101
 
    if (d->m_status == Private::Online) {
102
 
        offlineMode();
103
 
    }
104
 
    delete d;
105
 
}
106
 
 
107
 
void ObexFtpDaemon::onlineMode()
108
 
{
109
 
    kDebug();
110
 
    if (d->m_status == Private::Online) {
111
 
        kDebug() << "Already in onlineMode";
112
 
        return;
113
 
    }
114
 
 
115
 
    d->m_manager = new org::openobex::Manager("org.openobex", "/org/openobex", QDBusConnection::sessionBus(), 0);
116
 
 
117
 
    d->m_status = Private::Online;
118
 
}
119
 
 
120
 
void ObexFtpDaemon::offlineMode()
121
 
{
122
 
    kDebug() << "Offline mode";
123
 
    if (d->m_status == Private::Offline) {
124
 
        kDebug() << "Already in offlineMode";
125
 
        return;
126
 
    }
127
 
 
128
 
    QHash<QString, ObexSession*>::const_iterator i = d->m_sessionMap.constBegin();
129
 
    while (i != d->m_sessionMap.constEnd()) {
130
 
        if (d->m_sessionMap[i.key()]) {
131
 
            d->m_sessionMap[i.key()]->Disconnect().waitForFinished();
132
 
            d->m_sessionMap[i.key()]->Close().waitForFinished();
133
 
            delete d->m_sessionMap[i.key()];
134
 
        }
135
 
        d->m_sessionMap.remove(i.key());
136
 
    }
137
 
 
138
 
    d->m_status = Private::Offline;
139
 
}
140
 
 
141
 
void ObexFtpDaemon::defaultAdapterChanged(Adapter *adapter)
142
 
{
143
 
    if (adapter) {
144
 
        onlineMode();
145
 
    } else {
146
 
        offlineMode();
147
 
    }
148
 
}
149
 
 
150
 
void ObexFtpDaemon::stablishConnection(QString dirtyAddress)
151
 
{
152
 
    QString address = cleanAddress(dirtyAddress);
153
 
 
154
 
    kDebug() << "Address: " << address;
155
 
    if (d->m_status == Private::Offline) {
156
 
        kDebug() << "We're offline, so do nothing";
157
 
        return;
158
 
    }
159
 
 
160
 
    if (address.isEmpty()) {
161
 
        kDebug() << "Address is Empty";
162
 
    }
163
 
 
164
 
    //We already have a session for that address
165
 
    if (d->m_sessionMap.contains(address)) {
166
 
        //But this session is waiting for being connected
167
 
        if (d->m_sessionMap[address]->status() == ObexSession::Connecting) {
168
 
            kDebug() << "Session for this address is waiting for being connected";
169
 
            return;
170
 
        }
171
 
 
172
 
        kDebug() << "We already have a session, so do nothing";
173
 
        emit sessionConnected(address);
174
 
        return;
175
 
    }
176
 
 
177
 
    kDebug() << "Telling to the manager to create the session";
178
 
 
179
 
    QDBusPendingReply <QDBusObjectPath > rep = d->m_manager->CreateBluetoothSession(address, "00:00:00:00:00:00", "ftp");
180
 
 
181
 
    d->m_sessionMap[address] = new ObexSession("org.openobex", rep.value().path(), QDBusConnection::sessionBus(), 0);
182
 
    kDebug() << "Path: " << rep.value().path();
183
 
}
184
 
 
185
 
void ObexFtpDaemon::changeCurrentFolder(QString address, QString path)
186
 
{
187
 
    kDebug();
188
 
    d->m_sessionMap[address]->resetTimer();
189
 
    d->m_sessionMap[address]->ChangeCurrentFolderToRoot().waitForFinished();
190
 
 
191
 
    QStringList list = path.split("/");
192
 
    Q_FOREACH(const QString &dir, list) {
193
 
        if (!dir.isEmpty() && dir != address) {
194
 
            kDebug() << "Changing to: " << dir;
195
 
            QDBusPendingReply <void > a = d->m_sessionMap[address]->ChangeCurrentFolder(dir);
196
 
            a.waitForFinished();
197
 
            kDebug()  << "Change Error: " << a.error().message();
198
 
        } else {
199
 
            kDebug() << "Skyping" << dir;
200
 
        }
201
 
    }
202
 
}
203
 
 
204
 
QString ObexFtpDaemon::listDir(QString dirtyAddress, QString path)
205
 
{
206
 
    kDebug();
207
 
    QString address = cleanAddress(dirtyAddress);
208
 
    if (!d->m_sessionMap.contains(address)) {
209
 
        kDebug() << "The address " << address << " doesn't has a session";
210
 
        stablishConnection(address);
211
 
        return QString();
212
 
    }
213
 
    if (d->m_sessionMap[address]->status() == ObexSession::Connecting) {
214
 
        kDebug() << "The session is waiting to be connected";
215
 
        return QString();
216
 
    }
217
 
 
218
 
    address.replace("-", ":");
219
 
    changeCurrentFolder(address, path);
220
 
 
221
 
    d->m_sessionMap[address]->resetTimer();
222
 
    QString ret = d->m_sessionMap[address]->RetrieveFolderListing().value();
223
 
 
224
 
    kDebug() << ret;
225
 
 
226
 
    return ret;
227
 
}
228
 
 
229
 
void ObexFtpDaemon::copyRemoteFile(QString dirtyAddress, QString fileName, QString destPath)
230
 
{
231
 
    kDebug() << destPath;
232
 
    QString address = cleanAddress(dirtyAddress);
233
 
    ENSURE_SESSION_CREATED(address);
234
 
 
235
 
    KUrl url = KUrl(fileName);
236
 
    changeCurrentFolder(address, url.directory());
237
 
    kDebug() << d->m_sessionMap[address]->GetCurrentPath().value();
238
 
    kDebug() << url.fileName();
239
 
    d->m_sessionMap[address]->resetTimer();
240
 
    d->m_sessionMap[address]->CopyRemoteFile(url.fileName(), destPath);
241
 
}
242
 
 
243
 
void ObexFtpDaemon::sendFile(QString dirtyAddress, QString localPath, QString destPath)
244
 
{
245
 
    QString address = cleanAddress(dirtyAddress);
246
 
 
247
 
    kDebug();
248
 
    ENSURE_SESSION_CREATED(address);
249
 
    changeCurrentFolder(address, destPath);
250
 
 
251
 
    d->m_sessionMap[address]->resetTimer();
252
 
    d->m_sessionMap[address]->SendFile(localPath);
253
 
}
254
 
 
255
 
void ObexFtpDaemon::createFolder(QString dirtyAddress, QString path)
256
 
{
257
 
    kDebug();
258
 
    QString address = cleanAddress(dirtyAddress);
259
 
    ENSURE_SESSION_CREATED(address);
260
 
 
261
 
    KUrl url(path);
262
 
    changeCurrentFolder(address, url.directory());
263
 
 
264
 
    d->m_sessionMap[address]->resetTimer();
265
 
    d->m_sessionMap[address]->CreateFolder(url.fileName()).waitForFinished();
266
 
}
267
 
 
268
 
void ObexFtpDaemon::deleteRemoteFile(QString dirtyAddress, QString path)
269
 
{
270
 
    kDebug();
271
 
    QString address = cleanAddress(dirtyAddress);
272
 
    ENSURE_SESSION_CREATED(address);
273
 
 
274
 
    KUrl url(path);
275
 
    changeCurrentFolder(address, url.directory());
276
 
 
277
 
    d->m_sessionMap[address]->resetTimer();
278
 
    d->m_sessionMap[address]->DeleteRemoteFile(url.fileName()).waitForFinished();;
279
 
}
280
 
 
281
 
bool ObexFtpDaemon::isBusy(QString dirtyAddress)
282
 
{
283
 
    kDebug();
284
 
    QString address = cleanAddress(dirtyAddress);
285
 
    if (!d->m_sessionMap.contains(address)) {
286
 
        kDebug() << "The address " << address << " doesn't has a session";
287
 
        stablishConnection(address);
288
 
        return true;//Fake the busy state, so stablishConneciton can work
289
 
    }
290
 
    if (d->m_sessionMap[address]->status() == ObexSession::Connecting) {
291
 
        kDebug() << "The session is waiting to be connected";
292
 
        return true;
293
 
    }
294
 
 
295
 
    d->m_sessionMap[address]->resetTimer();
296
 
    return d->m_sessionMap[address]->IsBusy().value();
297
 
}
298
 
 
299
 
void ObexFtpDaemon::Cancel(QString dirtyAddress)
300
 
{
301
 
    QString address = cleanAddress(dirtyAddress);
302
 
    ENSURE_SESSION_CREATED(address)
303
 
 
304
 
    d->m_sessionMap[address]->resetTimer();
305
 
    d->m_sessionMap[address]->Cancel();
306
 
}
307
 
 
308
 
 
309
 
void ObexFtpDaemon::SessionConnected(QDBusObjectPath path)
310
 
{
311
 
    kDebug() << "SessionConnected!" << path.path();
312
 
 
313
 
    QString address = getAddressFromSession(path.path());
314
 
 
315
 
    d->m_sessionMap[address]->setStatus(ObexSession::Connected);
316
 
 
317
 
    connect(d->m_sessionMap[address], SIGNAL(sessionTimeout()), this, SLOT(sessionDisconnected()));
318
 
    connect(d->m_sessionMap[address], SIGNAL(Closed()), this, SLOT(sessionDisconnected()));
319
 
    connect(d->m_sessionMap[address], SIGNAL(Disconnected()), this, SLOT(sessionDisconnected()));
320
 
    connect(d->m_sessionMap[address], SIGNAL(Cancelled()), this, SIGNAL(Cancelled()));
321
 
    connect(d->m_sessionMap[address], SIGNAL(TransferCompleted()), this, SIGNAL(transferCompleted()));
322
 
    connect(d->m_sessionMap[address], SIGNAL(TransferProgress(qulonglong)), this, SIGNAL(transferProgress(qulonglong)));
323
 
    connect(d->m_sessionMap[address], SIGNAL(ErrorOccurred(QString,QString)), this, SIGNAL(errorOccurred(QString,QString)));
324
 
 
325
 
    emit sessionConnected(address);
326
 
}
327
 
 
328
 
void ObexFtpDaemon::SessionClosed(QDBusObjectPath path)
329
 
{
330
 
    kDebug();
331
 
    QHash<QString, ObexSession*>::const_iterator i = d->m_sessionMap.constBegin();
332
 
    while (i != d->m_sessionMap.constEnd()) {
333
 
        //If the session is connected, so not 0
334
 
        if (i.value()->path() == path.path()) {
335
 
            kDebug() << "Removing : " << i.key();
336
 
            d->m_sessionMap.remove(i.key());
337
 
            delete i.value();
338
 
            return;
339
 
        }
340
 
        ++i;
341
 
    }
342
 
 
343
 
    kDebug() << "Attempt to remove a nto existing session";
344
 
}
345
 
 
346
 
void ObexFtpDaemon::sessionDisconnected()
347
 
{
348
 
    kDebug() << "Session disconnected";
349
 
    ObexSession* session =  static_cast <ObexSession*>(sender());
350
 
    kDebug() << session->path();
351
 
    kDebug() << session->status();
352
 
 
353
 
    d->m_sessionMap.remove(d->m_sessionMap.key(session));
354
 
    delete session;
355
 
}
356
 
 
357
 
QString ObexFtpDaemon::getAddressFromSession(QString path)
358
 
{
359
 
    kDebug() << path;
360
 
    QStringMap info = d->m_manager->GetSessionInfo(QDBusObjectPath(path)).value();
361
 
    return info["BluetoothTargetAddress"];
362
 
}
363
 
 
364
 
QString ObexFtpDaemon::cleanAddress(QString& dirtyAddress) const
365
 
{
366
 
    dirtyAddress.replace("-", ":");
367
 
    return dirtyAddress.toLower();
368
 
}