00001
00002
00003
00004
00005
00006 #include "obex.h"
00007 #include "btobex.h"
00008 #include "obexsend.h"
00009 using namespace OpieObex;
00010
00011
00012 #include <opie2/odebug.h>
00013 #include <qpe/qcopenvelope_qws.h>
00014 #include <qpe/resource.h>
00015
00016 using namespace Opie::Core;
00017
00018
00019 #include <qlabel.h>
00020 #include <qpushbutton.h>
00021 #include <qpixmap.h>
00022 #include <qlistview.h>
00023 #include <qtimer.h>
00024
00025
00026
00027 SendWidget::SendWidget( QWidget* parent, const char* name )
00028 : obexSendBase( parent, name ) {
00029 initUI();
00030 }
00031 SendWidget::~SendWidget() {
00032 }
00033 void SendWidget::initUI() {
00034 m_obex = new Obex(this, "obex");
00035 connect(m_obex, SIGNAL(error(int) ),
00036 this, SLOT(slotIrError(int) ) );
00037 connect(m_obex, SIGNAL(sent(bool) ),
00038 this, SLOT(slotIrSent(bool) ) );
00039 connect(m_obex, SIGNAL(currentTry(unsigned int) ),
00040 this, SLOT(slotIrTry(unsigned int) ) );
00041
00042 QCopChannel* chan = new QCopChannel("QPE/IrDaAppletBack", this );
00043 connect(chan, SIGNAL(received(const QCString&,const QByteArray&) ),
00044 this, SLOT(dispatchIrda(const QCString&,const QByteArray&) ) );
00045
00046 m_btobex = new BtObex(this, "btobex");
00047 connect(m_btobex, SIGNAL(error(int) ),
00048 this, SLOT(slotBtError(int) ) );
00049 connect(m_btobex, SIGNAL(sent(bool) ),
00050 this, SLOT(slotBtSent(bool) ) );
00051 connect(m_btobex, SIGNAL(currentTry(unsigned int) ),
00052 this, SLOT(slotBtTry(unsigned int) ) );
00053
00054 chan = new QCopChannel("QPE/BluetoothBack", this );
00055 connect(chan, SIGNAL(received(const QCString&,const QByteArray&) ),
00056 this, SLOT(dispatchBt(const QCString&,const QByteArray&) ) );
00057
00058 }
00059
00060
00061
00062
00063
00064 void SendWidget::send( const QString& file, const QString& desc ) {
00065 m_file = file;
00066 m_irDa.clear();
00067 m_start = 0;
00068
00069 fileToSend->setText(desc.isEmpty() ? file : desc );
00070 scan_for_receivers();
00071 }
00072
00073 int SendWidget::addReceiver(const char *r, const char *icon)
00074 {
00075 QListViewItem * item = new QListViewItem( receiverList, 0 );
00076 item->setText( 0, r);
00077 item->setPixmap( 1, Resource::loadPixmap( icon ) );
00078
00079 int id=receivers.count();
00080 receivers[id]=item;
00081 return id;
00082 }
00083
00084 bool SendWidget::receiverSelected(int id)
00085 {
00086 return receivers[id]->pixmap(2);
00087 }
00088
00089 void SendWidget::setReceiverStatus( int id, const QString& status ) {
00090 if ( !receivers.contains(id) ) return;
00091 receivers[id]->setText(3, status );
00092 }
00093
00094 void SendWidget::slotIrDaDevices( const QStringList& list) {
00095 for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it ) {
00096 int id = addReceiver(*it, "obex/irda.png");
00097 m_irDa.insert( id, (*it) );
00098 }
00099 irdaStatus->setText( tr("ready."));
00100 m_irDaIt = m_irDa.begin();
00101
00102 }
00103
00104 void SendWidget::slotBTDevices( const QMap<QString, QString>& str ) {
00105 for(QMap<QString, QString>::ConstIterator it = str.begin(); it != str.end(); ++it ) {
00106 int id = addReceiver(it.key(), "obex/bt.png");
00107 m_bt.insert( id, Pair( it.key(), it.data() ) );
00108 }
00109 btStatus->setText(tr("ready."));
00110 m_btIt = m_bt.begin();
00111
00112 }
00113 void SendWidget::slotSelectedDevice( int, int ) {
00114
00115
00116
00117
00118
00119
00120 }
00121 void SendWidget::dispatchIrda( const QCString& str, const QByteArray& ar ) {
00122 if ( str == "devices(QStringList)" ) {
00123 QDataStream stream( ar, IO_ReadOnly );
00124 QStringList list;
00125 stream >> list;
00126 slotIrDaDevices( list );
00127 }
00128 }
00129 void SendWidget::slotIrError( int ) {
00130 irdaStatus->setText(tr("error :("));
00131 }
00132 void SendWidget::slotIrSent( bool b) {
00133 QString text = b ? tr("Sent") : tr("Failure");
00134 setReceiverStatus( m_irDaIt.key(), text );
00135 ++m_irDaIt;
00136 slotStartIrda();
00137 }
00138 void SendWidget::slotIrTry(unsigned int trI) {
00139 setReceiverStatus(m_irDaIt.key(), tr("Try %1").arg( QString::number( trI ) ));
00140 }
00141 void SendWidget::slotStartIrda() {
00142 if ( !m_irDa.count() ) return;
00143 if ( m_irDaIt == m_irDa.end() ) {
00144 irdaStatus->setText(tr("complete."));
00145 return;
00146 }
00147 setReceiverStatus( m_irDaIt.key(), tr("Start sending") );
00148 m_obex->send( m_file );
00149 }
00150
00151 void SendWidget::dispatchBt( const QCString& str, const QByteArray& ar ) {
00152 if ( str == "devices(QStringMap)" ) {
00153 QDataStream stream( ar, IO_ReadOnly );
00154 QMap<QString, QString> btmap;
00155 stream >> btmap;
00156 slotBTDevices( btmap );
00157 }
00158 }
00159 void SendWidget::slotBtError( int ) {
00160 btStatus->setText(tr("error :("));
00161 }
00162 void SendWidget::slotBtSent( bool b) {
00163 QString text = b ? tr("Sent") : tr("Failure");
00164 setReceiverStatus( m_btIt.key(), text );
00165 ++m_btIt;
00166 slotStartBt();
00167 }
00168 void SendWidget::slotBtTry(unsigned int trI) {
00169 setReceiverStatus( m_btIt.key(), tr("Try %1").arg( QString::number( trI ) ) );
00170 }
00171 void SendWidget::slotStartBt() {
00172
00173 while((m_btIt != m_bt.end()) && !receiverSelected(m_btIt.key()))
00174 ++m_btIt;
00175 if (m_btIt == m_bt.end() ) {
00176 btStatus->setText(tr("complete."));
00177 return;
00178 }
00179 setReceiverStatus( m_btIt.key(), tr("Start sending") );
00180 m_btobex->send( m_file, m_btIt.data().second() );
00181 }
00182
00183 void SendWidget::send_to_receivers() {
00184 slotStartIrda();
00185 slotStartBt();
00186 }
00187
00188 void SendWidget::scan_for_receivers()
00189 {
00190
00191 sendButton->setDisabled( true );
00192
00193 if ( !QCopChannel::isRegistered("QPE/IrDaApplet") )
00194 {
00195 irdaStatus->setText(tr("not enabled."));
00196 }
00197 else
00198 {
00199 QCopEnvelope e1("QPE/IrDaApplet", "enableIrda()");
00200 irdaStatus->setText(tr("searching..."));
00201 sendButton->setEnabled( true );
00202 QCopEnvelope e2("QPE/IrDaApplet", "listDevices()");
00203 }
00204
00205 if ( !QCopChannel::isRegistered("QPE/Bluetooth") )
00206 {
00207 btStatus->setText(tr("not enabled."));
00208 }
00209 else
00210 {
00211 QCopEnvelope e1("QPE/Bluetooth", "enableBluetooth()");
00212 btStatus->setText(tr("searching..."));
00213 sendButton->setEnabled( true );
00214 QCopEnvelope e3("QPE/Bluetooth", "listDevices()");
00215 }
00216 }
00217
00218 void SendWidget::toggle_receiver(QListViewItem* item)
00219 {
00220
00221 if(item->pixmap(2))
00222 item->setPixmap(2,QPixmap());
00223 else
00224 item->setPixmap(2,Resource::loadPixmap("backup/check.png"));
00225 }
00226
00227
00228 void SendWidget::closeEvent( QCloseEvent* e) {
00229 e->accept();
00230 QTimer::singleShot(0, this, SLOT(userDone() ) );
00231 }
00232 void SendWidget::userDone() {
00233 QCopEnvelope e0("QPE/IrDaApplet", "disableIrda()");
00234 QCopEnvelope e1("QPE/Bluetooth", "disableBluetooth()");
00235 emit done();
00236 }
00237 QString SendWidget::file()const {
00238 return m_file;
00239 }