summaryrefslogtreecommitdiff
path: root/core/obex/obexsend.cpp
blob: cd8d58ec28aa685f67b27a048d1cc60b1c56a110 (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
#include <qpushbutton.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qtimer.h>


#include <qpe/qcopenvelope_qws.h>

#include "obex.h"
#include "obexsend.h"

using namespace OpieObex;

/* TRANSLATOR OpieObex::SendWidget */


SendWidget::SendWidget( QWidget* parent, const char* name )
    : QWidget( parent, name ) {
    initUI();
}
SendWidget::~SendWidget() {
}
void SendWidget::initUI() {
    m_obex = new Obex(this, "obex");
    connect(m_obex, SIGNAL(error(int) ),
            this, SLOT(slotIrError(int) ) );
    connect(m_obex, SIGNAL(sent(bool) ),
            this, SLOT(slotIrSent(bool) ) );
    connect(m_obex, SIGNAL(currentTry(unsigned int) ),
            this, SLOT(slotIrTry(unsigned int) ) );

    QCopChannel* chan = new QCopChannel("QPE/IrDaAppletBack", this );
    connect(chan, SIGNAL(received(const QCString&,const QByteArray&) ),
            this, SLOT(dispatchIrda(const QCString&,const QByteArray&) ) );

    chan = new QCopChannel("QPE/BluetoothBack", this );
    connect(chan, SIGNAL(received(const QCString&,const QByteArray&) ),
            this, SLOT(dispatchBt(const QCString&,const QByteArray&) ) );

    QVBoxLayout* lay = new QVBoxLayout(this);

    QHBox* nameBox = new QHBox(this);
    QLabel* name = new QLabel(nameBox);
    name->setText( tr("<qt><h1>Sending:</h1></qt>") );
    name->setAlignment( AlignLeft | AlignTop );
    m_lblFile = new QLabel(nameBox);
    lay->addWidget(nameBox, 0);

    QFrame* frame = new QFrame(this);
    frame->setFrameShape( QFrame::HLine );
    frame->setFrameShadow( QFrame::Sunken );
    lay->addWidget(frame, 10);

    QLabel* devices = new QLabel(this);
    devices->setText("<qt><b>Devices:</b></qt>");
    devices->setAlignment( AlignLeft | AlignTop );
    lay->addWidget( devices,10 );

    m_devBox = new DeviceBox(this);
    lay->addWidget( m_devBox, 50 );
    connect(m_devBox, SIGNAL(selectedDevice(int,int) ),
            this, SLOT(slotSelectedDevice(int,int) ) );

    QPushButton *but = new QPushButton(this);
    but->setText(tr("Done") );
    connect(but, SIGNAL(clicked() ),
            this, SLOT(slotDone() ) );

    lay->addWidget( but );
    m_lay = lay;

    // QT does not like if you add items to an layout which already exits....
    // and was layouted invalidate() does not help too
    // so we use RichText....
}

/*
 * in send we'll first set everything up
 * and then wait for a list of devices.
 */
void SendWidget::send( const QString& file, const QString& desc ) {
    m_file = file;
    m_irDa.clear();
    m_start = 0;
    m_lblFile->setText(desc.isEmpty() ? file : desc );

    if ( !QCopChannel::isRegistered("QPE/IrDaApplet") ) {
        m_irDeSearch = m_devBox->addDevice( tr("IrDa is not enabled!"), DeviceBox::Error );
        m_start++;
    }else
        m_irDeSearch = m_devBox->addDevice( tr("Searching for IrDa Devices."), DeviceBox::Search );

    if ( !QCopChannel::isRegistered("QPE/Bluetooth") ) {
        m_btDeSearch = m_devBox->addDevice( tr("Bluetooth is not available"), DeviceBox::Error );
        m_start++;
    }else
        m_btDeSearch = m_devBox->addDevice( tr("Searching for bluetooth Devices."), DeviceBox::Search );

    if (m_start != 2 ) {
        QCopEnvelope e0("QPE/IrDaApplet", "enableIrda()");
        QCopEnvelope e1("QPE/Bluetooth", "enableBluetooth()");
        QCopEnvelope e2("QPE/IrDaApplet", "listDevices()");
        QCopEnvelope e3("QPE/Bluetooth", "listDevices()");
    }
}
void SendWidget::slotIrDaDevices( const QStringList& list) {
    qWarning("slot it irda devices ");
    for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it ) {
        int id = m_devBox->addDevice( (*it), DeviceBox::IrDa, tr("Scheduling for beam.") );
        m_irDa.insert( id, (*it) );
    }
    m_devBox->removeDevice( m_irDeSearch );
    m_irDaIt = m_irDa.begin();

    slotStartIrda();
}

void SendWidget::slotBTDevices( const QMap<QString, QString>& str ) {
    for(QMap<QString, QString>::ConstIterator it = str.begin(); it != str.end(); ++it ) {
        int id = m_devBox->addDevice( it.key(), DeviceBox::BT, tr("Click to beam") );
        m_bt.insert( id, Pair( it.key(), it.data() ) );
    }
    m_devBox->removeDevice( m_btDeSearch );
}
void SendWidget::slotSelectedDevice( int name, int dev ) {
    qWarning("Start beam? %d %d", name, dev );
    if ( name ==  m_irDeSearch ) {
        for (QMap<int, QString>::Iterator it= m_irDa.begin(); it != m_irDa.end(); ++it )
            m_devBox->removeDevice( it.key() );

        QCopEnvelope e2("QPE/IrDaApplet", "listDevices()");
    }
}
void SendWidget::dispatchIrda( const QCString& str, const QByteArray& ar ) {
    qWarning("dispatch irda %s", str.data() );
    if ( str == "devices(QStringList)" ) {
        QDataStream stream( ar, IO_ReadOnly );
        QStringList list;
        stream >> list;
        slotIrDaDevices( list );
    }
}
void SendWidget::dispatchBt( const QCString&, const QByteArray&  ) {

}
void SendWidget::slotIrError( int ) {

}
void SendWidget::slotIrSent( bool b) {
    qWarning("irda sent!!");
    QString text = b ? tr("Sent") : tr("Failure");
    m_devBox->setStatus( m_irDaIt.key(), text );
    ++m_irDaIt;
    slotStartIrda();
}
void SendWidget::slotIrTry(unsigned int trI) {
    m_devBox->setStatus( m_irDaIt.key(), tr("Try %1").arg( QString::number( trI ) ) );
}
void SendWidget::slotStartIrda() {
    if (m_irDaIt == m_irDa.end() ) {
        m_irDeSearch = m_devBox->addDevice(tr("Search again for IrDa."), DeviceBox::Search );
        return;
    }
    m_devBox->setStatus( m_irDaIt.key(), tr("Start sending") );
    m_obex->send( m_file );
}
void SendWidget::closeEvent( QCloseEvent* e) {
    e->accept(); // make sure
    QTimer::singleShot(0, this, SLOT(slotDone() ) );
}
void SendWidget::slotDone() {
    QCopEnvelope e0("QPE/IrDaApplet", "disableIrda()");
    QCopEnvelope e1("QPE/Bluetooth", "disableBluetooth()");
    emit done();
}
QString SendWidget::file()const {
    return m_file;
}
DeviceBox::DeviceBox( QWidget* parent )
    : QTextBrowser( parent ) {

}
DeviceBox::~DeviceBox() {

}
int DeviceBox::addDevice( const QString& name, int dev, const QString& status ) {
    /* return a id for a range of devices */
    int id = idFor ( dev );
    DeviceItem item( name, status, dev,id );
    m_dev.insert( id, item );
    setText( allText() );

    return id;
}
void DeviceBox::removeDevice( int id ) {
    if (!m_dev.contains(id) ) return;

    m_dev.remove( id );
    setText( allText() );
}
void DeviceBox::setStatus( int id, const QString& status ) {
    if ( !m_dev.contains(id) ) return;
    m_dev[id].setStatus(status );
    setText( allText() );
}
void DeviceBox::setSource( const QString& str ) {
    qWarning("SetSource:%d", str.toInt() );
    int id = str.toInt();
    emit selectedDevice( id, m_dev[id].device() );
}
int DeviceBox::idFor ( int id ) {
    static int irId = 1501;
    static int irBT = 1001;
    static int irSr = 501;
    static int irEr = 0;

    int ret = -1;
    switch(id ) {
    case IrDa:
        ret = irId;
        irId++;
        break;
    case BT:
        ret = irBT;
        irBT++;
        break;
    case Search:
        ret = irSr;
        irSr++;
        break;
    case Error:
        ret = irEr;
        irEr++;
        break;
    }
    return ret;
}
QString DeviceBox::allText() {
    QString str;
    typedef QMap<int, DeviceItem> DeviceMap;

    for (QMap<int, DeviceItem>::Iterator it = m_dev.begin(); it != m_dev.end(); ++it ) {
        str += it.data().toString() + "<br>";
    }
    return str;
}

DeviceItem::DeviceItem( const QString& name,
                        const QString& status, int dev,  int id)
{
    m_name = name;
    m_status = status;
    m_dev = dev;
    m_id = id;
}
int DeviceItem::id()const {
    return m_id;
}
QString DeviceItem::name()const {
    return m_name;
}
QString DeviceItem::status()const {
    return m_status;
}
int DeviceItem::device()const {
    return m_dev;
}
QString DeviceItem::pixmap()const{
    QString str;
    switch(m_dev) {
    case DeviceBox::IrDa:
        str ="obex/irda";
        break;
    case DeviceBox::BT:
        str ="obex/bt";
        break;
    case DeviceBox::Search:
        str = "mag";
        break;
    case DeviceBox::Error:
        str = "editdelete";
        break;
    };
    return str;
}
DeviceItem::~DeviceItem() {
}
void DeviceItem::setStatus(const QString& status ) {
    m_status = status;
}
QString DeviceItem::toString()const {
    return "<p><a href=\""+QString::number(m_id) +"\" ><img src=\""+pixmap()+"\" >"+m_name+" "+m_status+" </a></p>" ;
}