~ubuntu-branches/ubuntu/intrepid/kdebluetooth/intrepid-proposed

« back to all changes in this revision

Viewing changes to kdebluetooth/kbtserialchat/kbtserialchat.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Mercatante
  • Date: 2008-08-07 09:49:47 UTC
  • mto: This revision was merged to the branch mainline in revision 56.
  • Revision ID: james.westby@ubuntu.com-20080807094947-pj6q3uxwuv7l844q
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//-*-c++-*-
2
 
/***************************************************************************
3
 
 *   Copyright (C) 2003 by Fred Schaettgen                                 *
4
 
 *   kdebluetooth@schaettgen.de                                            *
5
 
 *                                                                         *
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
 
 ***************************************************************************/
11
 
 
12
 
#include "kbtserialchat.h"
13
 
 
14
 
#include <qlineedit.h>
15
 
#include <qapplication.h>
16
 
#include <qtextedit.h>
17
 
#include <qpushbutton.h>
18
 
#include <klocale.h>
19
 
#include <kmessagebox.h>
20
 
#include <libkbluetooth/serviceselectionwidget.h>
21
 
#include <libkbluetoothd/mruservices.h>
22
 
 
23
 
using namespace KBluetooth;
24
 
 
25
 
MainDialog::MainDialog()
26
 
        : MainDialogBase(0, "kbtserialchat")
27
 
{
28
 
    DeviceAddress addr;
29
 
    int channel;
30
 
    QStringList uuids("0x1101");
31
 
    if (ServiceSelectionWidget::showSelectionDialog(this,
32
 
        uuids, addr, channel) == true) {
33
 
 
34
 
        socket = new RfcommSocket(this, "rfcommsocket");
35
 
        socket->connectToHost(addr, channel);
36
 
 
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()));
45
 
 
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);
50
 
    }
51
 
    else {
52
 
        QApplication::exit(1);
53
 
    }
54
 
}
55
 
 
56
 
 
57
 
MainDialog::MainDialog(DeviceAddress addr, int channel)
58
 
        : MainDialogBase( 0, "kbtserialchat" )
59
 
{
60
 
    socket = new RfcommSocket(this, "rfcommsocket");
61
 
    socket->connectToHost(addr, channel);
62
 
 
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()));
71
 
 
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);
76
 
}
77
 
 
78
 
MainDialog::MainDialog(int s, QString addr, QString name)
79
 
        : MainDialogBase( 0, "kbtserialchat" )
80
 
{
81
 
    socket = new RfcommSocket(this, "rfcommsocket");
82
 
    socket->setSocket(s);
83
 
 
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()));
92
 
 
93
 
    QString text = textEdit->text();
94
 
    textEdit->setText(text + QString("<br>Incoming connection from %1 (%2).")
95
 
        .arg(name).arg(addr));
96
 
}
97
 
 
98
 
 
99
 
void MainDialog::slotSendCommand()
100
 
{
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>" +
106
 
        s + "</font>");
107
 
    lineEdit->setText("");
108
 
    textEdit->scrollToBottom();
109
 
}
110
 
 
111
 
void MainDialog::slotSocketReadyRead()
112
 
{
113
 
    const int len = socket->size();
114
 
    if (len > 0) {
115
 
        char *buf = new char[len];
116
 
        socket->readBlock(buf, len);
117
 
        QCString cs(buf, len);
118
 
        delete []buf;
119
 
        QString text = textEdit->text();
120
 
        textEdit->setText(text + "<br><font color=#880000>" +
121
 
            QString::fromUtf8(cs) + "</font>");
122
 
        textEdit->scrollToBottom();
123
 
    }
124
 
}
125
 
 
126
 
void MainDialog::slotSocketError(int errnum)
127
 
{
128
 
    KMessageBox::information(NULL,
129
 
        QString("Connection error (%1).").arg(errnum),
130
 
        "kbtserialchat");
131
 
    sendButton->setEnabled(false);
132
 
    lineEdit->setEnabled(false);
133
 
}
134
 
 
135
 
void MainDialog::slotSocketConnectionClosed()
136
 
{
137
 
    KMessageBox::information(NULL,
138
 
        "Connection closed.",
139
 
        "kbtserialchat");
140
 
    sendButton->setEnabled(false);
141
 
    lineEdit->setEnabled(false);
142
 
}
143
 
 
144
 
void MainDialog::saveMru(KBluetooth::DeviceAddress addr, int channel)
145
 
{    
146
 
    KBluetoothd::MRUServices::add(
147
 
    QStringList("kbtserialchat") << QString("sdp://[%1]/params?rfcommchannel=%2")
148
 
        .arg(QString(addr)).arg(channel), addr);
149
 
}
150
 
 
151
 
MainDialog::~MainDialog()
152
 
{
153
 
}
154
 
 
155
 
#include "kbtserialchat.moc"