File: dbus-chat.pl

package info (click to toggle)
qt4-perl 4.8.4-1.2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,636 kB
  • ctags: 8,100
  • sloc: perl: 42,963; cpp: 28,039; makefile: 160; xml: 98; sh: 4
file content (194 lines) | stat: -rwxr-xr-x 4,734 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/perl

package NicknameDialog;

use strict;
use warnings;
use QtCore4;
use QtGui4;
use QtCore4::isa qw( Qt::Dialog );
use Ui_NicknameDialog;

sub cancelButton {
    return this->ui->cancelButton;
}

sub nickname {
    return this->ui->nickname;
}

sub ui {
    return this->{ui};
}

sub NEW
{
    my ($class, $parent) = @_;
    $class->SUPER::NEW($parent);
    this->{ui} = Ui_NicknameDialog->setupUi(this);
}

package ChatMainWindow;

use strict;
use warnings;
use QtCore4;
use QtGui4;
use QtCore4::isa qw( Qt::MainWindow );
use QtCore4::signals
    message => ['const QString &', 'const QString &'],
    action => ['const QString &', 'const QString &'];

use QtCore4::slots
    messageSlot => ['const QString &', 'const QString &'],
    actionSlot => ['const QString &', 'const QString &'],
    textChangedSlot => ['const QString &'],
    sendClickedSlot => [],
    changeNickname => [],
    aboutQt => [],
    exiting => [];

use Ui_ChatMainWindow;
use NicknameDialog;

sub m_nickname() {
    return this->{m_nickname};
}

sub m_messages() {
    return this->{m_messages};
}

sub ui {
    return this->{ui};
}

use ChatAdaptor;
use ComTrolltechChatInterface;

sub NEW
{
    my ($class, $m_nickname) = @_;
    $class->SUPER::NEW();
    this->{m_nickname} = $m_nickname;

    this->{m_messages} = [];

    this->{ui} = Ui_ChatMainWindow->setupUi(this);
    this->ui->sendButton->setEnabled(0);

    this->connect(this->ui->messageLineEdit, SIGNAL 'textChanged(QString)',
            this, SLOT 'textChangedSlot(QString)');
    this->connect(this->ui->sendButton, SIGNAL 'clicked(bool)', this, SLOT 'sendClickedSlot()');
    this->connect(this->ui->actionChangeNickname, SIGNAL 'triggered(bool)', this, SLOT 'changeNickname()');
    this->connect(this->ui->actionAboutQt, SIGNAL 'triggered(bool)', this, SLOT 'aboutQt()');
    this->connect(qApp, SIGNAL 'lastWindowClosed()', this, SLOT 'exiting()');

    # add our D-Bus interface and connect to D-Bus
    ChatAdaptor(this);
    Qt::DBusConnection::sessionBus()->registerObject('/', this);

    my $iface = ComTrolltechChatInterface('', '', Qt::DBusConnection::sessionBus(), this);
    #connect(iface, SIGNAL 'message(Qt::String,Qt::String)', this, SLOT 'messageSlot(Qt::String,Qt::String)');
    Qt::DBusConnection::sessionBus()->connect('', '', 'com.trolltech.chat', 'message', this, SLOT 'messageSlot(QString,QString)');
    this->connect($iface, SIGNAL 'action(QString,QString)', this, SLOT 'actionSlot(QString,QString)');

    my $dialog = NicknameDialog();
    $dialog->cancelButton->setVisible(0);
    $dialog->exec();
    this->{m_nickname} = $dialog->nickname->text();
    emit this->action(this->m_nickname, 'joins the chat');
}

sub rebuildHistory
{
    my $history = join "\n", @{this->m_messages};
    this->ui->chatHistory->setPlainText($history);
}

sub messageSlot
{
    my ($nickname, $text) = @_;
    my $msg = "<$nickname> $text";
    push @{this->m_messages}, $msg;

    if (scalar @{this->m_messages} > 100) {
        pop @{this->m_messages};
    }
    this->rebuildHistory();
}

sub actionSlot
{
    my ($nickname, $text) = @_;
    my $msg = "* $nickname $text";
    push @{this->m_messages}, $msg;

    if (scalar @{this->m_messages} > 100) {
        pop @{this->m_messages};
    }
    this->rebuildHistory();
}

sub textChangedSlot
{
    my ($newText) = @_;
    this->ui->sendButton->setEnabled($newText);
}

sub sendClickedSlot
{
    # This line is commented out in the source Qt example.
    #emit message(m_nickname, messageLineEdit->text());
    my $msg = Qt::DBusMessage::createSignal('/', 'com.trolltech.chat', 'message');
    no warnings qw(void);
    $msg << Qt::Variant(Qt::String(this->m_nickname)) << Qt::Variant(Qt::String(this->ui->messageLineEdit->text()));
    use warnings;
    Qt::DBusConnection::sessionBus()->send($msg);
    this->ui->messageLineEdit->setText('');
}

sub changeNickname
{
    my $dialog = NicknameDialog(this);
    if ($dialog->exec() == Qt::Dialog::Accepted()) {
        my $old = this->m_nickname;
        this->{m_nickname} = $dialog->nickname->text();
        emit this->action($old, 'is now known as ' . this->m_nickname);
    }
}

sub aboutQt()
{
    Qt::MessageBox::aboutQt(this);
}

sub exiting
{
    emit this->action(this->m_nickname, 'leaves the chat');
}

package main;

use strict;
use warnings;
use QtCore4;
use QtGui4;
use ChatMainWindow;

sub main
{
    my $app = Qt::Application( \@ARGV );

    if (!Qt::DBusConnection::sessionBus()->isConnected()) {
        warn "Cannot connect to the D-Bus session bus.\n" .
             "Please check your system settings and try again.\n";
        return 1;
    }

    my $chat = ChatMainWindow();
    $chat->show();
    return $app->exec();
}

exit main();