MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //ui->chatTextBox->setEnabled(false);
    ui->sendTextBox->setEnabled(false);
    ui->sendPushButton->setEnabled(false);

    ui->chatTextBox->setReadOnly(true);

    connect(ui->joinPushButton, SIGNAL(clicked()), this, SLOT(joinChat()));
    connect(ui->sendPushButton, SIGNAL(clicked()), this, SLOT(sendMessage()));
    connect(&timer, SIGNAL(timeout()), this, SLOT(recvMessage()));

    /*Fill in remote sockaddr_in */
    bzero(&remoteAddress, sizeof(remoteAddress));
    remoteAddress.sin_family = PF_INET;
    remoteAddress.sin_port = htons(PORT);
    remoteAddress.sin_addr.s_addr = htonl(INADDR_BROADCAST);//inet_addr("192.168.0.255");

    /*Fill in server's sockaddr_in */
    bzero(&serverAddress, sizeof(serverAddress));
    serverAddress.sin_family = PF_INET;
    serverAddress.sin_port = htons(PORT);
    serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);


    sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (sockfd == -1)
        QMessageBox::information(this, "Error: socket", strerror(errno), QMessageBox::Ok);

    int broadcastOn = 1;
    if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &broadcastOn, 4) == -1)
        QMessageBox::information(this, "Error: setsockopt", strerror(errno), QMessageBox::Ok);

    if (bind(sockfd, (struct sockaddr* ) &serverAddress, sizeof(serverAddress)) == -1)
        QMessageBox::information(this, "Error: bind", strerror(errno), QMessageBox::Ok);

    this->setFixedSize(this->size());
}
AIMJoinChatUI::AIMJoinChatUI( AIMAccount* account, QWidget* parent )
    : KDialog( parent )
{

    setCaption(  i18n( "Join AIM Chat Room" ) );
    setButtons( KDialog::Cancel | KDialog::User1 );
    setDefaultButton( KDialog::User1 );
    setButtonGuiItem( KDialog::User1, KGuiItem( i18n("Join") ) );
    showButtonSeparator( true );

    kDebug(OSCAR_AIM_DEBUG) << "Account " << account->accountId()
                   << " joining a chat room" << endl;

    m_account = account;

    QWidget* w = new QWidget( this );
    m_joinUI = new Ui::AIMJoinChatBase();
    m_joinUI->setupUi( w );

    setMainWidget( w  );

    QObject::connect( this, SIGNAL(user1Clicked()), this, SLOT(joinChat()) );
    QObject::connect( this, SIGNAL(cancelClicked()), this, SLOT(closeClicked()) );
}
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //ui->chatTextBox->setEnabled(false);
    ui->sendTextBox->setEnabled(false);
    ui->sendPushButton->setEnabled(false);

    ui->chatTextBox->setReadOnly(true);

    connect(ui->joinPushButton, SIGNAL(clicked()), this, SLOT(joinChat()));
    connect(ui->sendPushButton, SIGNAL(clicked()), this, SLOT(sendMessage()));
    connect(&timer, SIGNAL(timeout()), this, SLOT(recvMessage()));

    /*Fill in remote sockaddr_in */
    bzero(&remoteAddress, sizeof(remoteAddress));
    remoteAddress.sin_family = PF_INET;
    remoteAddress.sin_port = htons(PORT);
    remoteAddress.sin_addr.s_addr = inet_addr("226.0.0.1");

    /*Fill in server's sockaddr_in */
    bzero(&serverAddress, sizeof(serverAddress));
    serverAddress.sin_family = PF_INET;
    serverAddress.sin_port = htons(PORT);
    serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);


    bzero(&iaddr, sizeof(iaddr));
    iaddr.s_addr = INADDR_ANY; // use DEFAULT interface

    /* for multicast */
    imreq.imr_multiaddr.s_addr = inet_addr("226.0.0.1");
    imreq.imr_interface.s_addr = INADDR_ANY; // use DEFAULT interface

    sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (sockfd == -1)
        QMessageBox::information(this, "Error: socket", strerror(errno), QMessageBox::Ok);

    int broadcastOn = 1;
    if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &broadcastOn, 4) == -1)
        QMessageBox::information(this, "Error: setsockopt", strerror(errno), QMessageBox::Ok);

    if (bind(sockfd, (struct sockaddr* ) &serverAddress, sizeof(serverAddress)) == -1)
        QMessageBox::information(this, "Error: bind", strerror(errno), QMessageBox::Ok);

    /* Join multicast group */
    if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&imreq, sizeof(struct ip_mreq)))
        QMessageBox::information(this, "Error: setsockopt", strerror(errno), QMessageBox::Ok);

    /* Set the outgoing interface to default interface */
    if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_IF, &iaddr, sizeof(struct in_addr)))
        QMessageBox::information(this, "Error: setsockopt", strerror(errno), QMessageBox::Ok);

    unsigned char ttl = 3;
    /* Set multicast packet TTL to 3; default TTL is 1 */
    if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(unsigned char)))
        QMessageBox::information(this, "Error: setsockopt", strerror(errno), QMessageBox::Ok);

    unsigned char one = 1;
    /* send multicast traffic to myself too */
    if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_LOOP, &one, sizeof(unsigned char)))
        QMessageBox::information(this, "Error: setsockopt", strerror(errno), QMessageBox::Ok);

    this->setFixedSize(this->size());
}