summaryrefslogtreecommitdiff
path: root/noncore/net/opietooth/manager/obexftpdialog.cpp
Unidiff
Diffstat (limited to 'noncore/net/opietooth/manager/obexftpdialog.cpp') (more/less context) (show whitespace changes)
-rw-r--r--noncore/net/opietooth/manager/obexftpdialog.cpp47
1 files changed, 43 insertions, 4 deletions
diff --git a/noncore/net/opietooth/manager/obexftpdialog.cpp b/noncore/net/opietooth/manager/obexftpdialog.cpp
index 80a81b4..f479ca2 100644
--- a/noncore/net/opietooth/manager/obexftpdialog.cpp
+++ b/noncore/net/opietooth/manager/obexftpdialog.cpp
@@ -1,60 +1,65 @@
1/* $Id$ */ 1/* $Id$ */
2/* OBEX file browser dialog */ 2/* OBEX file browser dialog */
3/*************************************************************************** 3/***************************************************************************
4 * * 4 * *
5 * This program is free software; you can redistribute it and/or modify * 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 * 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 * 7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. * 8 * (at your option) any later version. *
9 * * 9 * *
10 ***************************************************************************/ 10 ***************************************************************************/
11/* 11/*
12 * This code uses and is based on ObexFTP project code: http://triq.net/obexftp/ 12 * This code uses and is based on ObexFTP project code: http://triq.net/obexftp/
13 */ 13 */
14#include <sys/stat.h> 14#include <sys/stat.h>
15#include <sys/types.h> 15#include <sys/types.h>
16#include <unistd.h> 16#include <unistd.h>
17#include <string.h> 17#include <string.h>
18#include <errno.h> 18#include <errno.h>
19#include <qpushbutton.h> 19#include <qpushbutton.h>
20#include <qmessagebox.h> 20#include <qmessagebox.h>
21#include <qmultilineedit.h> 21#include <qmultilineedit.h>
22#include <qlineedit.h>
22#include <qspinbox.h> 23#include <qspinbox.h>
23#include <qcombobox.h> 24#include <qcombobox.h>
24#include <qlistview.h> 25#include <qlistview.h>
25#include <qprogressbar.h> 26#include <qprogressbar.h>
26#include <qlabel.h> 27#include <qlabel.h>
27#include <qlayout.h> 28#include <qlayout.h>
28#include <errno.h> 29#include <errno.h>
30#include <qfile.h>
31#include <qtextstream.h>
32#include <qstringlist.h>
29#include "obexftpdialog.h" 33#include "obexftpdialog.h"
30#include "filelistitem.h" 34#include "filelistitem.h"
31 35
32#include <qpe/qpeapplication.h> 36#include <qpe/qpeapplication.h>
33#include <qpe/resource.h> 37#include <qpe/resource.h>
34#include <qpe/config.h> 38#include <qpe/config.h>
35#include <opie2/odebug.h> 39#include <opie2/odebug.h>
36#include <opie2/ofileselector.h> 40#include <opie2/ofileselector.h>
41#include <opie2/ofiledialog.h>
37 42
38using namespace Opie::Core; 43using namespace Opie::Core;
39using namespace Opie::Ui; 44using namespace Opie::Ui;
40 45
41using namespace OpieTooth; 46using namespace OpieTooth;
42 47
43#define MAX_PROGRESS 14 //Maximal progress bar 48#define MAX_PROGRESS 14 //Maximal progress bar
44 49
45static void info_cb(int event, const char *msg, int len, void* data); 50static void info_cb(int event, const char *msg, int len, void* data);
46 51
47/* 52/*
48 * Public constructor 53 * Public constructor
49 * device - bluetooth address of the device 54 * device - bluetooth address of the device
50 * port - port to connect to 55 * port - port to connect to
51 */ 56 */
52ObexFtpDialog::ObexFtpDialog(const QString& device, int port, 57ObexFtpDialog::ObexFtpDialog(const QString& device, int port,
53 QWidget* parent, const char* name, bool modal, WFlags fl) 58 QWidget* parent, const char* name, bool modal, WFlags fl)
54 : ObexFtpDialogBase(parent, name, modal, fl), m_device(device), 59 : ObexFtpDialogBase(parent, name, modal, fl), m_device(device),
55 m_port(port), curdir("") 60 m_port(port), curdir("")
56{ 61{
57 client = NULL; 62 client = NULL;
58 nRetries = 3; 63 nRetries = 3;
59 transport = OBEX_TRANS_BLUETOOTH; 64 transport = OBEX_TRANS_BLUETOOTH;
60 use_conn = TRUE; 65 use_conn = TRUE;
@@ -67,97 +72,103 @@ ObexFtpDialog::ObexFtpDialog(const QString& device, int port,
67 fileProgress->setTotalSteps(MAX_PROGRESS); 72 fileProgress->setTotalSteps(MAX_PROGRESS);
68 statusBar->clear(); 73 statusBar->clear();
69 localLayout = new QVBoxLayout(localFs); 74 localLayout = new QVBoxLayout(localFs);
70 localLayout->setSpacing( 0 ); 75 localLayout->setSpacing( 0 );
71 localLayout->setMargin( 0 ); 76 localLayout->setMargin( 0 );
72 destFile = new OFileSelector(localFs, 77 destFile = new OFileSelector(localFs,
73 OFileSelector::FileSelector, 78 OFileSelector::FileSelector,
74 OFileSelector::ExtendedAll, localCurdir, ""); 79 OFileSelector::ExtendedAll, localCurdir, "");
75 destFile->setCloseVisible(false); 80 destFile->setCloseVisible(false);
76 destFile->setNewVisible(false); 81 destFile->setNewVisible(false);
77 localLayout->addWidget(destFile); 82 localLayout->addWidget(destFile);
78 nReries->setValue(nRetries); 83 nReries->setValue(nRetries);
79 connect(browseOK, SIGNAL(clicked()), SLOT(slotBrowse())); 84 connect(browseOK, SIGNAL(clicked()), SLOT(slotBrowse()));
80 connect(fileList, SIGNAL(clicked(QListViewItem*)), 85 connect(fileList, SIGNAL(clicked(QListViewItem*)),
81 SLOT(slotCd(QListViewItem*))); 86 SLOT(slotCd(QListViewItem*)));
82 connect(getButton, 87 connect(getButton,
83 SIGNAL(clicked()), 88 SIGNAL(clicked()),
84 SLOT(getFile())); 89 SLOT(getFile()));
85 connect(putButton, 90 connect(putButton,
86 SIGNAL(clicked()), 91 SIGNAL(clicked()),
87 SLOT(putFile())); 92 SLOT(putFile()));
88 connect(destFile, 93 connect(destFile,
89 SIGNAL(dirSelected (const QString&)), 94 SIGNAL(dirSelected (const QString&)),
90 SLOT(updateDir(const QString&))); 95 SLOT(updateDir(const QString&)));
96 connect(saveButton,
97 SIGNAL(clicked()),
98 SLOT(slotSaveLog()));
99 connect(browseButton,
100 SIGNAL(clicked()),
101 SLOT(slotBrowseLog()));
91} 102}
92 103
93ObexFtpDialog::~ObexFtpDialog() 104ObexFtpDialog::~ObexFtpDialog()
94{ 105{
95 if (client != NULL) { 106 if (client != NULL) {
96 obexftp_disconnect(client); 107 obexftp_disconnect(client);
97 obexftp_close(client); 108 obexftp_close(client);
98 } 109 }
99} 110}
100 111
101/* 112/*
102 * Do device browsing 113 * Do device browsing
103 */ 114 */
104void ObexFtpDialog::slotBrowse() 115void ObexFtpDialog::slotBrowse()
105{ 116{
106 stat_entry_t* ent; //Directory entry 117 stat_entry_t* ent; //Directory entry
107 void *dir; //Directory to read 118 void *dir; //Directory to read
108 const uint8_t* use_uuid; //uuid 119 const uint8_t* use_uuid; //uuid
109 int len; //uuid length 120 int len; //uuid length
110 FileListItem* root; //root node 121 FileListItem* root; //root node
111 int fsize; //file size 122 int fsize; //file size
112 bool numOk; //true if the string is a number 123 bool numOk; //true if the string is a number
113 int tmp; //just a temp var 124 int tmp; //just a temp var
114 125
115 status(tr("Connecting to ") + m_device); 126 status(tr("Connecting to ") + m_device);
116 odebug << "Browse device " << m_device << oendl; 127 odebug << "Browse device " << m_device << oendl;
117 browseLog->clear(); 128 browseLog->clear();
118 fileList->clear(); 129 fileList->clear();
119 progressStatus = 0; 130 progressStatus = 0;
120 fileProgress->setTotalSteps(MAX_PROGRESS); 131 fileProgress->setTotalSteps(MAX_PROGRESS);
121 fileProgress->reset(); 132 fileProgress->reset();
122 133
123 //Get parameters 134 //Get parameters
124 tmp = nReries->text().toInt(&numOk); 135 tmp = nReries->text().toInt(&numOk);
125 if (numOk) 136 if (numOk)
126 nRetries = tmp; 137 nRetries = tmp;
127 if (uuidType->currentText() == "FBS") { 138 if (uuidType->currentText() == "FBS") {
128 use_uuid = UUID_FBS; 139 use_uuid = UUID_FBS;
129 len = sizeof(UUID_FBS); 140 len = sizeof(UUID_FBS);
130 } else if (uuidType->currentText() == "S45") { 141 } else if (uuidType->currentText() == "S45") {
131 use_uuid = UUID_S45; 142 use_uuid = UUID_S45;
132 len = sizeof(UUID_S45); 143 len = sizeof(UUID_S45);
133 } else { 144 } else {
134 use_uuid = UUID_FBS; 145 use_uuid = UUID_FBS;
135 len = sizeof(UUID_FBS); 146 len = sizeof(UUID_FBS);
136 } 147 }
137 148
138 if (!cli_connect_uuid(use_uuid, len)) { 149 if (!cli_connect_uuid(use_uuid, len)) {
139 log("Connection failed"); 150 log(tr("Connection failed: ") + tr(strerror(errno)));
140 errBox("Connection failed"); 151 errBox("Connection failed");
141 status("Connection failed"); 152 status("Connection failed");
142 return; 153 return;
143 } 154 }
144 else { 155 else {
145 log(QString("Connected to ") + m_device); 156 log(QString("Connected to ") + m_device);
146 status(QString("Connected to ") + m_device); 157 status(QString("Connected to ") + m_device);
147 } 158 }
148 /* List folder */ 159 /* List folder */
149 root = new FileListItem(fileList, NULL); 160 root = new FileListItem(fileList, NULL);
150 dir = obexftp_opendir(client, curdir); 161 dir = obexftp_opendir(client, curdir);
151 while ((ent = obexftp_readdir(dir)) != NULL) { 162 while ((ent = obexftp_readdir(dir)) != NULL) {
152 FileListItem* a; //List view item 163 FileListItem* a; //List view item
153 if (ent->mode != 16877) { 164 if (ent->mode != 16877) {
154 fsize = ent->size; 165 fsize = ent->size;
155 } 166 }
156 else 167 else
157 fsize = 0; 168 fsize = 0;
158 log(QString(ent->name) + QString(" ") + 169 log(QString(ent->name) + QString(" ") +
159 QString::number(ent->mode)); 170 QString::number(ent->mode));
160 171
161 a = new FileListItem(fileList, ent, fsize); 172 a = new FileListItem(fileList, ent, fsize);
162 } 173 }
163 obexftp_closedir(dir); 174 obexftp_closedir(dir);
@@ -204,88 +215,88 @@ void ObexFtpDialog::status(const char* msg)
204void ObexFtpDialog::slotCd(QListViewItem* item) 215void ObexFtpDialog::slotCd(QListViewItem* item)
205{ 216{
206 FileListItem* file = (FileListItem*)item; 217 FileListItem* file = (FileListItem*)item;
207 int idx; 218 int idx;
208 if (file == NULL) 219 if (file == NULL)
209 return; 220 return;
210 odebug << "Item " << file->text(0) << " clicked" << oendl; 221 odebug << "Item " << file->text(0) << " clicked" << oendl;
211 if (file->gettype() == IS_DIR) { 222 if (file->gettype() == IS_DIR) {
212 if (file->text(0) == "../") { 223 if (file->text(0) == "../") {
213 if (curdir.right(1) == "/") 224 if (curdir.right(1) == "/")
214 curdir.remove(curdir.length() - 1, 1); 225 curdir.remove(curdir.length() - 1, 1);
215 idx = curdir.findRev('/'); 226 idx = curdir.findRev('/');
216 if (idx >= 0) 227 if (idx >= 0)
217 curdir.remove(idx, curdir.length() - idx); 228 curdir.remove(idx, curdir.length() - idx);
218 else 229 else
219 curdir = ""; 230 curdir = "";
220 } 231 }
221 else { 232 else {
222 if (curdir != "" && curdir.right(1) != "/") 233 if (curdir != "" && curdir.right(1) != "/")
223 curdir += "/"; 234 curdir += "/";
224 curdir += file->text(0); 235 curdir += file->text(0);
225 } 236 }
226 odebug << "Browse " << curdir << oendl; 237 odebug << "Browse " << curdir << oendl;
227 if (obexftp_setpath(client, curdir, 0) < 0) 238 if (obexftp_setpath(client, curdir, 0) < 0)
228 log("CD failed"); 239 log(tr("CD failed: ") + tr(strerror(errno)));
229 slotBrowse(); 240 slotBrowse();
230 } 241 }
231} 242}
232 243
233/* 244/*
234 * Get the file 245 * Get the file
235 */ 246 */
236void ObexFtpDialog::getFile() 247void ObexFtpDialog::getFile()
237{ 248{
238 FileListItem* file = (FileListItem*)fileList->selectedItem(); 249 FileListItem* file = (FileListItem*)fileList->selectedItem();
239 int result; 250 int result;
240 if (file == NULL) 251 if (file == NULL)
241 return; 252 return;
242 file2get = "/"; 253 file2get = "/";
243 local = localCurdir; 254 local = localCurdir;
244 if (local == "") { 255 if (local == "") {
245 errBox("Select a destination first"); 256 errBox("Select a destination first");
246 return; 257 return;
247 } 258 }
248 if (local.right(1) != "/") 259 if (local.right(1) != "/")
249 local += "/"; 260 local += "/";
250 if (file->gettype() == IS_FILE) { 261 if (file->gettype() == IS_FILE) {
251 if (client == NULL) { 262 if (client == NULL) {
252 errBox("No connection established"); 263 errBox("No connection established");
253 return; 264 return;
254 } 265 }
255 file2get += curdir; 266 file2get += curdir;
256 if (curdir != "" && curdir.right(1) != "/") 267 if (curdir != "" && curdir.right(1) != "/")
257 file2get += "/"; 268 file2get += "/";
258 file2get += file->text(0); 269 file2get += file->text(0);
259 local += file->text(0); 270 local += file->text(0);
260 odebug << "Copy " << file2get << " to " << local << oendl; 271 odebug << "Copy " << file2get << " to " << local << oendl;
261 progressStatus = 0; 272 progressStatus = 0;
262 fileProgress->setTotalSteps(file->getsize() / 1024); 273 fileProgress->setTotalSteps(file->getsize() / 1024);
263 fileProgress->reset(); 274 fileProgress->reset();
264 status(tr("Receiving file ") + file2get); 275 status(tr("Receiving file ") + file2get);
265 result = obexftp_get(client, local, file2get); 276 result = obexftp_get(client, local, file2get);
266 if (result < 0) { 277 if (result < 0) {
267 log(file2get + QString(" receive ERROR")); 278 log(file2get + QString(" receive ERROR: ") + tr(strerror(errno)));
268 errBox(file2get + QString(" receive ERROR")); 279 errBox(file2get + QString(" receive ERROR"));
269 status(file2get + QString(" receive ERROR")); 280 status(file2get + QString(" receive ERROR"));
270 } 281 }
271 else { 282 else {
272 log(file2get + QString(" received")); 283 log(file2get + QString(" received"));
273 status(file2get + QString(" received")); 284 status(file2get + QString(" received"));
274 destFile->reread(); 285 destFile->reread();
275 } 286 }
276 } 287 }
277} 288}
278 289
279/* 290/*
280 * Put the file 291 * Put the file
281 */ 292 */
282void ObexFtpDialog::putFile() 293void ObexFtpDialog::putFile()
283{ 294{
284 int result; //OPeration result 295 int result; //OPeration result
285 int idx; //Index of a symbol in the string 296 int idx; //Index of a symbol in the string
286 struct stat localFStat; //Local file information 297 struct stat localFStat; //Local file information
287 298
288 if (client == NULL) { 299 if (client == NULL) {
289 errBox("No connection established"); 300 errBox("No connection established");
290 return; 301 return;
291 } 302 }
@@ -293,49 +304,49 @@ void ObexFtpDialog::putFile()
293 if (local == "") { 304 if (local == "") {
294 errBox("No file slected"); 305 errBox("No file slected");
295 return; 306 return;
296 } 307 }
297 result = stat(local, &localFStat); 308 result = stat(local, &localFStat);
298 if (result < 0) { 309 if (result < 0) {
299 errBox(tr("Wrong file selected ") + local + tr(" ") + 310 errBox(tr("Wrong file selected ") + local + tr(" ") +
300 tr(strerror(errno))); 311 tr(strerror(errno)));
301 return; 312 return;
302 } 313 }
303 idx = local.findRev('/'); 314 idx = local.findRev('/');
304 if (idx > 0) { 315 if (idx > 0) {
305 file2get = local.right(local.length() - idx - 1); 316 file2get = local.right(local.length() - idx - 1);
306 } 317 }
307 else 318 else
308 file2get = local; 319 file2get = local;
309 320
310 odebug << "Copy " << local << " to " << file2get << oendl; 321 odebug << "Copy " << local << " to " << file2get << oendl;
311 progressStatus = 0; 322 progressStatus = 0;
312 fileProgress->setTotalSteps(localFStat.st_size / 1024); 323 fileProgress->setTotalSteps(localFStat.st_size / 1024);
313 fileProgress->reset(); 324 fileProgress->reset();
314 status(tr("Sending file ") + local); 325 status(tr("Sending file ") + local);
315 result = obexftp_put_file(client, local, file2get); 326 result = obexftp_put_file(client, local, file2get);
316 if (result < 0) { 327 if (result < 0) {
317 log(local + QString(" send ERROR")); 328 log(local + QString(" send ERROR: ") + tr(strerror(errno)));
318 errBox(local + QString(" send ERROR")); 329 errBox(local + QString(" send ERROR"));
319 status(local + QString(" send ERROR")); 330 status(local + QString(" send ERROR"));
320 } 331 }
321 else { 332 else {
322 log(local + QString(" sent")); 333 log(local + QString(" sent"));
323 status(local + QString(" sent")); 334 status(local + QString(" sent"));
324 } 335 }
325} 336}
326 337
327/* connect with given uuid. re-connect every time */ 338/* connect with given uuid. re-connect every time */
328int ObexFtpDialog::cli_connect_uuid(const uint8_t *uuid, int uuid_len) 339int ObexFtpDialog::cli_connect_uuid(const uint8_t *uuid, int uuid_len)
329{ 340{
330 int retry; 341 int retry;
331 if (client != NULL) 342 if (client != NULL)
332 return TRUE; 343 return TRUE;
333 /* Open */ 344 /* Open */
334 client = obexftp_open (transport, NULL, info_cb, this); 345 client = obexftp_open (transport, NULL, info_cb, this);
335 if(client == NULL) { 346 if(client == NULL) {
336 errBox("Error opening obexftp-client"); 347 errBox("Error opening obexftp-client");
337 return FALSE; 348 return FALSE;
338 } 349 }
339 if (!use_conn) 350 if (!use_conn)
340 client->quirks &= ~OBEXFTP_CONN_HEADER; 351 client->quirks &= ~OBEXFTP_CONN_HEADER;
341 if (!use_path) 352 if (!use_path)
@@ -382,48 +393,76 @@ void ObexFtpDialog::log(QString& str)
382} 393}
383 394
384void ObexFtpDialog::log(const char* str) 395void ObexFtpDialog::log(const char* str)
385{ 396{
386 browseLog->append(str); 397 browseLog->append(str);
387} 398}
388 399
389void ObexFtpDialog::incProgress() 400void ObexFtpDialog::incProgress()
390{ 401{
391 if (progressStatus < fileProgress->totalSteps()) 402 if (progressStatus < fileProgress->totalSteps())
392 fileProgress->setProgress(progressStatus++); 403 fileProgress->setProgress(progressStatus++);
393} 404}
394 405
395void ObexFtpDialog::doneProgress() 406void ObexFtpDialog::doneProgress()
396{ 407{
397 progressStatus = 0; 408 progressStatus = 0;
398 fileProgress->reset(); 409 fileProgress->reset();
399} 410}
400 411
401void ObexFtpDialog::updateDir(const QString& newdir) 412void ObexFtpDialog::updateDir(const QString& newdir)
402{ 413{
403 localCurdir = newdir; 414 localCurdir = newdir;
404} 415}
405 416
417/**
418 * Save Log to the specified file
419 */
420void ObexFtpDialog::slotSaveLog()
421{
422 QFile logFile(saveLogEdit->text());
423 if (!logFile.open(IO_WriteOnly)) {
424 errBox(tr("Unable to open file ") + saveLogEdit->text() + tr(" ") +
425 tr(strerror(errno)));
426 return;
427 }
428 QTextStream stream(&logFile);
429 stream << browseLog->text() << endl;
430 QMessageBox::information(this, tr("Saving"),
431 tr("Log file saved to ") + saveLogEdit->text());
432}
433
434void ObexFtpDialog::slotBrowseLog()
435{
436 MimeTypes types;
437 QStringList all;
438 all << "*/*";
439 types.insert("All Files", all );
440
441 QString str = OFileDialog::getOpenFileName( 1,"/","", types, 0 );
442 saveLogEdit->setText(str);
443}
444
406/* 445/*
407 * Event callback function 446 * Event callback function
408 */ 447 */
409static void info_cb(int event, const char *msg, int len, void* data) 448static void info_cb(int event, const char *msg, int len, void* data)
410{ 449{
411 ObexFtpDialog* dlg = (ObexFtpDialog*)data; 450 ObexFtpDialog* dlg = (ObexFtpDialog*)data;
412 QCString cmsg(msg, len); //Message to display 451 QCString cmsg(msg, len); //Message to display
413 452
414 switch (event) { 453 switch (event) {
415 454
416 case OBEXFTP_EV_ERRMSG: 455 case OBEXFTP_EV_ERRMSG:
417 dlg->log(QCString("Error: ") + cmsg); 456 dlg->log(QCString("Error: ") + cmsg);
418 break; 457 break;
419 458
420 case OBEXFTP_EV_ERR: 459 case OBEXFTP_EV_ERR:
421 dlg->log(QCString("failed: ") + cmsg); 460 dlg->log(QCString("failed: ") + cmsg);
422 dlg->doneProgress(); 461 dlg->doneProgress();
423 break; 462 break;
424 case OBEXFTP_EV_OK: 463 case OBEXFTP_EV_OK:
425 dlg->log(QCString("done")); 464 dlg->log(QCString("done"));
426 dlg->doneProgress(); 465 dlg->doneProgress();
427 break; 466 break;
428 467
429 case OBEXFTP_EV_CONNECTING: 468 case OBEXFTP_EV_CONNECTING: