2
/***************************************************************************
3
* Copyright (C) 2003 by Fred Schaettgen *
4
* kdebluetooth@schaettgen.de *
6
* This program is free software; you can redistribute it and/or modify *
7
* it under the terms of the GNU General Public License as published by *
8
* the Free Software Foundation; either version 2 of the License, or *
9
* (at your option) any later version. *
10
***************************************************************************/
12
#include "kbtserialchat.h"
14
#include <qlineedit.h>
15
#include <qapplication.h>
16
#include <qtextedit.h>
17
#include <qpushbutton.h>
19
#include <kmessagebox.h>
20
#include <libkbluetooth/serviceselectionwidget.h>
21
#include <libkbluetoothd/mruservices.h>
23
using namespace KBluetooth;
25
MainDialog::MainDialog()
26
: MainDialogBase(0, "kbtserialchat")
30
QStringList uuids("0x1101");
31
if (ServiceSelectionWidget::showSelectionDialog(this,
32
uuids, addr, channel) == true) {
34
socket = new RfcommSocket(this, "rfcommsocket");
35
socket->connectToHost(addr, channel);
37
connect(sendButton, SIGNAL(clicked()),
38
this, SLOT(slotSendCommand()));
39
connect(socket, SIGNAL(readyRead()),
40
this, SLOT(slotSocketReadyRead()));
41
connect(socket, SIGNAL(error(int)),
42
this, SLOT(slotSocketError(int)));
43
connect(socket, SIGNAL(connectionClosed()),
44
this, SLOT(slotSocketConnectionClosed()));
46
QString text = textEdit->text();
47
textEdit->setText(text + QString("<br>Connected to %1 on channel %2.").
48
arg(QString(addr)).arg(channel));
49
saveMru(addr, channel);
52
QApplication::exit(1);
57
MainDialog::MainDialog(DeviceAddress addr, int channel)
58
: MainDialogBase( 0, "kbtserialchat" )
60
socket = new RfcommSocket(this, "rfcommsocket");
61
socket->connectToHost(addr, channel);
63
connect(sendButton, SIGNAL(clicked()),
64
this, SLOT(slotSendCommand()));
65
connect(socket, SIGNAL(readyRead()),
66
this, SLOT(slotSocketReadyRead()));
67
connect(socket, SIGNAL(error(int)),
68
this, SLOT(slotSocketError(int)));
69
connect(socket, SIGNAL(connectionClosed()),
70
this, SLOT(slotSocketConnectionClosed()));
72
QString text = textEdit->text();
73
textEdit->setText(text + QString("<br>Connected to %1 on channel %2.").
74
arg(QString(addr)).arg(channel));
75
saveMru(addr, channel);
78
MainDialog::MainDialog(int s, QString addr, QString name)
79
: MainDialogBase( 0, "kbtserialchat" )
81
socket = new RfcommSocket(this, "rfcommsocket");
84
connect(sendButton, SIGNAL(clicked()),
85
this, SLOT(slotSendCommand()));
86
connect(socket, SIGNAL(readyRead()),
87
this, SLOT(slotSocketReadyRead()));
88
connect(socket, SIGNAL(error(int)),
89
this, SLOT(slotSocketError()));
90
connect(socket, SIGNAL(connectionClosed()),
91
this, SLOT(slotSocketConnectionClosed()));
93
QString text = textEdit->text();
94
textEdit->setText(text + QString("<br>Incoming connection from %1 (%2).")
95
.arg(name).arg(addr));
99
void MainDialog::slotSendCommand()
101
QString s = lineEdit->text() + "\r\n";
102
QCString buf(s.utf8());
103
socket->writeBlock((const char*)buf, buf.size());
104
QString text = textEdit->text();
105
textEdit->setText(text + "<br><font color=#008800>" +
107
lineEdit->setText("");
108
textEdit->scrollToBottom();
111
void MainDialog::slotSocketReadyRead()
113
const int len = socket->size();
115
char *buf = new char[len];
116
socket->readBlock(buf, len);
117
QCString cs(buf, len);
119
QString text = textEdit->text();
120
textEdit->setText(text + "<br><font color=#880000>" +
121
QString::fromUtf8(cs) + "</font>");
122
textEdit->scrollToBottom();
126
void MainDialog::slotSocketError(int errnum)
128
KMessageBox::information(NULL,
129
QString("Connection error (%1).").arg(errnum),
131
sendButton->setEnabled(false);
132
lineEdit->setEnabled(false);
135
void MainDialog::slotSocketConnectionClosed()
137
KMessageBox::information(NULL,
138
"Connection closed.",
140
sendButton->setEnabled(false);
141
lineEdit->setEnabled(false);
144
void MainDialog::saveMru(KBluetooth::DeviceAddress addr, int channel)
146
KBluetoothd::MRUServices::add(
147
QStringList("kbtserialchat") << QString("sdp://[%1]/params?rfcommchannel=%2")
148
.arg(QString(addr)).arg(channel), addr);
151
MainDialog::~MainDialog()
155
#include "kbtserialchat.moc"