package Server;

use strict;
use warnings;
use QtCore4;
use QtGui4;
use QtNetwork4;
use QtCore4::isa qw( Qt::Dialog );
use QtCore4::slots
    sendFortune => [];
use constant { RAND_MAX => 2147483647 };

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

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

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

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

sub NEW
    my ($class, $parent) = @_;
    this->{statusLabel} = Qt::Label();
    this->{quitButton} = Qt::PushButton(this->tr('Quit'));

    this->{server} = Qt::LocalServer(this);
    if (!server->listen('fortune')) {
        Qt::MessageBox::critical(this, this->tr('Fortune Server'),
                      sprintf this->tr('Unable to start the server: %s.'),

    statusLabel->setText(this->tr("The server is running.\n" .
                            'Run the Fortune Client example now.'));

    this->{fortunes} = [
             this->tr('You\'ve been leading a dog\'s life. Stay off the furniture.'),
             this->tr('You\'ve got to think about tomorrow.'),
             this->tr('You will be surprised by a loud noise.'),
             this->tr('You will feel hungry again in another hour.'),
             this->tr('You might have mail.'),
             this->tr('You cannot kill time without injuring eternity.'),
             this->tr('Computers are not intelligent. They only think they are.')

    this->connect(quitButton, SIGNAL 'clicked()', this, SLOT 'close()');
    this->connect(server, SIGNAL 'newConnection()', this, SLOT 'sendFortune()');

    my $buttonLayout = Qt::HBoxLayout();

    my $mainLayout = Qt::VBoxLayout();

    setWindowTitle(this->tr('Fortune Server'));

sub sendFortune
    my $block = Qt::ByteArray();
    my $out = Qt::DataStream($block, Qt::IODevice::WriteOnly());
    $out << Qt::Ushort(0);
    $out << Qt::String(fortunes->[rand(RAND_MAX) % scalar @{fortunes()}]);
    $out << Qt::Ushort($block->size() - length(pack('S',0)));

    my $clientConnection = server->nextPendingConnection();