summaryrefslogtreecommitdiff
path: root/noncore/net/opietooth/manager/obexftpdialog.cpp
blob: c77d49d162ce5e0340f42e443e680ce791c2cb93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* $Id$ */
/* OBEX file browser dialog */
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
/*
 * This code uses and is based on ObexFTP project code: http://triq.net/obexftp/
 */
#include <sys/stat.h>
#include <qpushbutton.h>
#include <qmessagebox.h>
#include <qmultilineedit.h>
#include <qlistview.h>
#include <qprogressbar.h>
#include <qlabel.h>
#include <qlayout.h>
#include <errno.h>
#include "obexftpdialog.h"
#include "filelistitem.h"

#include <qpe/qpeapplication.h>
#include <qpe/resource.h>
#include <qpe/config.h>
#include <opie2/odebug.h>
#include <opie2/ofileselector.h>

using namespace Opie::Core;
using namespace Opie::Ui;

using namespace OpieTooth;

#define MAX_PROGRESS 14 //Maximal progress bar

static void info_cb(int event, const char *msg, int len, void* data);

/*
 * Public constructor
 * device - bluetooth address of the device
 * port - port to connect to
 */
ObexFtpDialog::ObexFtpDialog(const QString& device, int port, 
    QWidget* parent, const char* name, bool modal, WFlags fl) 
    : ObexFtpDialogBase(parent, name, modal, fl), m_device(device), 
    m_port(port), curdir("")
{
    client = NULL;
    transport = OBEX_TRANS_BLUETOOTH;
    use_conn = TRUE;
    use_path = TRUE;
    progressStatus = 0;
    localCurdir = "/";
    browseLog->setEdited(FALSE);
    fileList->setSorting(1);
    fileList->clear();
    fileProgress->setTotalSteps(MAX_PROGRESS);
    statusBar->clear();
    localLayout = new QVBoxLayout(localFs);
    localLayout->setSpacing( 0 );
    localLayout->setMargin( 0 );
    destFile = new OFileSelector(localFs, 
        OFileSelector::FileSelector, 
        OFileSelector::ExtendedAll, localCurdir, "");
    destFile->setCloseVisible(false);
    destFile->setNewVisible(false);
    localLayout->addWidget(destFile);
    connect(browseOK, SIGNAL(clicked()), SLOT(slotBrowse()));
    connect(fileList, SIGNAL(clicked(QListViewItem*)), 
        SLOT(slotCd(QListViewItem*)));
    connect(getButton, 
        SIGNAL(clicked()),
        SLOT(getFile()));
    connect(destFile, 
        SIGNAL(dirSelected (const QString&)),
        SLOT(updateDir(const QString&)));
}

ObexFtpDialog::~ObexFtpDialog()
{
    if (client != NULL) {
        obexftp_disconnect(client);
        obexftp_close(client);
    }
}

/*
 * Do device browsing
 */
void ObexFtpDialog::slotBrowse()
{
    stat_entry_t* ent; //Directory entry
    void *dir; //Directory to read
    const uint8_t use_uuid[] = __UUID_FBS_bytes;
    int len = sizeof(UUID_FBS);
    FileListItem* root; //root node
    stat_entry_t* st; //File statistics
    int fsize; //file size
    
    status(tr("Connecting to ") + m_device);
    odebug << "Browse device " << m_device << oendl;
    browseLog->clear();
    fileList->clear();
    progressStatus = 0;
    fileProgress->reset();
    if (!cli_connect_uuid(use_uuid, len)) {
        log("Connection failed");
        errBox("Connection failed");
        status("Connection failed");
        return;
    }
    else {
        log(QString("Connected to ") + m_device);
        status(QString("Connected to ") + m_device);
    }
    /* List folder */
    root = new FileListItem(fileList, NULL);
    dir = obexftp_opendir(client, curdir);
	while ((ent = obexftp_readdir(dir)) != NULL) {
        FileListItem* a; //List view item
#if 0 //Causes sigsegv        
    	if (ent->mode != 16877) {
            st = obexftp_stat(client, ent->name);
            fsize = st->size;
        }
        else
#endif        
            fsize = 0;
		log(QString(ent->name) + QString(" ") + 
            QString::number(ent->mode));
        
        a = new FileListItem(fileList, ent, fsize);
	}
	obexftp_closedir(dir);
}

//Error message box
int ObexFtpDialog::errBox(QCString msg)
{
    return QMessageBox::critical(this, tr("ObexFTP error"), msg);
}

int ObexFtpDialog::errBox(QString msg)
{
    return QMessageBox::critical(this, tr("ObexFTP error"), msg);
}

int ObexFtpDialog::errBox(const char* msg)
{
    return QMessageBox::critical(this, tr("ObexFTP error"), tr(msg));
}

//Text in the status bar
void ObexFtpDialog::status(QCString msg)
{
    statusBar->setText(msg);
    statusBar->repaint();
}

void ObexFtpDialog::status(QString msg)
{
    statusBar->setText(msg);
    statusBar->repaint();
}

void ObexFtpDialog::status(const char* msg)
{
    statusBar->setText(msg);
    statusBar->repaint();
}

/*
 * Change directory with item under the cursor
 */
void ObexFtpDialog::slotCd(QListViewItem* item)
{
    FileListItem* file = (FileListItem*)item;
    int idx;
    if (file == NULL)
        return;
    odebug << "Item " << file->text(0) << " clicked" << oendl;
    if (file->gettype() == IS_DIR) {
        if (file->text(0) == "../") {
            if (curdir.right(1) == "/")
                curdir.remove(curdir.length() - 1, 1);
            idx = curdir.findRev('/');
            if (idx >= 0)
                curdir.remove(idx, curdir.length() - idx);
            else
                curdir = "";
        }
        else {
            if (curdir != "" && curdir.right(1) != "/")
                curdir += "/";
            curdir += file->text(0);
        }
        odebug << "Browse " << curdir << oendl;
        if (obexftp_setpath(client, curdir, 0) < 0)
            log("CD failed");
        slotBrowse();
    }
}

/*
 * Get the file
 */
void ObexFtpDialog::getFile()
{
    FileListItem* file = (FileListItem*)fileList->selectedItem();
    int result;
    if (file == NULL)
        return;
    file2get = "/";
    local = localCurdir;
    if (local == "") {
        errBox("Select a destination first");
        return;
    }
    if (local.right(1) != "/")
        local += "/";
    if (file->gettype() == IS_FILE) {
        file2get += curdir;
        if (curdir != "" && curdir.right(1) != "/")
            file2get += "/";
        file2get += file->text(0);
        local += file->text(0);
        odebug << "Copy " << file2get << " to " << local << oendl;
        progressStatus = 0;
        fileProgress->reset();
        status(tr("Receiving file ") + file2get);
        result = obexftp_get(client, local, file2get);
        if (result < 0) {
    		log(file2get + QString(" receive ERROR")); 
            errBox(file2get + QString(" receive ERROR"));
            status(file2get + QString(" receive ERROR"));
        }
        else {
            log(file2get + QString(" received")); 
            status(file2get + QString(" received")); 
            destFile->reread();
        }
    }
}

/* connect with given uuid. re-connect every time */
int ObexFtpDialog::cli_connect_uuid(const uint8_t *uuid, int uuid_len)
{
	int retry;
	if (client != NULL)
        return TRUE;
    /* Open */
    client = obexftp_open (transport, NULL, info_cb, this);
    if(client == NULL) {
        errBox("Error opening obexftp-client");
        return FALSE;
    }
    if (!use_conn)
        client->quirks &= ~OBEXFTP_CONN_HEADER;
    if (!use_path)
        client->quirks &= ~OBEXFTP_SPLIT_SETPATH;
    for (retry = 0; retry < 3; retry++) {
        /* Connect */
        switch (transport) {
        case OBEX_TRANS_IRDA:
            if (obexftp_connect_uuid(client, NULL, 0, uuid, uuid_len) >= 0)
                return TRUE;
            break;
        case OBEX_TRANS_BLUETOOTH:
            if (obexftp_connect_uuid(client, m_device, m_port, 
                uuid, uuid_len) >= 0)
                return TRUE;
            break;
        default:
            errBox("Transport type unknown");
            return FALSE;
        }
        log(tr("Still trying to connect"));
    }
    client = NULL;

	return FALSE;
}

/*
 * Put a message to the log window
 */
void ObexFtpDialog::log(QString str)
{
    browseLog->append(str);
}

void ObexFtpDialog::log(QCString str)
{
    browseLog->append(str);
}

void ObexFtpDialog::log(QString& str)
{
    browseLog->append(str);
}

void ObexFtpDialog::log(const char* str)
{
    browseLog->append(str);
}

void ObexFtpDialog::incProgress()
{
    odebug << "Progress " << progressStatus << oendl;
    if (progressStatus < MAX_PROGRESS) {
        fileProgress->setProgress(progressStatus++);
    }
}

void ObexFtpDialog::doneProgress()
{
    progressStatus = 0;
    fileProgress->reset();
}

void ObexFtpDialog::updateDir(const QString& newdir)
{
    localCurdir = newdir;
}

/*
 * Event callback function
 */
static void info_cb(int event, const char *msg, int len, void* data)
{
    ObexFtpDialog* dlg = (ObexFtpDialog*)data;
    QCString cmsg(msg, len); //Message to display

	switch (event) {

	case OBEXFTP_EV_ERRMSG:
		dlg->log(QCString("Error: ") + cmsg);
		break;

	case OBEXFTP_EV_ERR:
		dlg->log(QCString("failed: ") + cmsg);
        dlg->doneProgress();
		break;
	case OBEXFTP_EV_OK:
		dlg->log(QCString("done"));
        dlg->doneProgress();
		break;

	case OBEXFTP_EV_CONNECTING:
		dlg->log(QCString("Connecting..."));
		break;
	case OBEXFTP_EV_DISCONNECTING:
		dlg->log(QCString("Disconnecting..."));
		break;
	case OBEXFTP_EV_SENDING:
		dlg->log(QCString("Sending ") + msg);
		break;
	case OBEXFTP_EV_RECEIVING:
		dlg->log(QCString("Receiving ") + msg);
		break;

	case OBEXFTP_EV_LISTENING:
		dlg->log(QCString("Waiting for incoming connection"));
		break;

	case OBEXFTP_EV_CONNECTIND:
		dlg->log(QCString("Incoming connection"));
		break;
	case OBEXFTP_EV_DISCONNECTIND:
		dlg->log(QCString("Disconnecting"));
		break;

	case OBEXFTP_EV_INFO:
        // 64 bit problems ?
		dlg->log(QString("Got info ") + QString::number((int)msg)); 
		break;

	case OBEXFTP_EV_BODY:
		break;

	case OBEXFTP_EV_PROGRESS:
        dlg->incProgress();
		break;
	}
}

//eof