MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setListFile();
    model = new TableModel(this);
    model->setSettings(&settings);
    model->readFromFile(listfile);
    ui->tableView->setModel(model);
    readSettings();

    if (settings.trayicon)
    {
        trayicon = new QSystemTrayIcon(this->windowIcon(), this);
        connect(trayicon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
                    this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));

        traymenu = new QMenu(this);
        traymenu->addAction(ui->actionStart);
        traymenu->addAction(ui->actionStop);
        traymenu->addAction(ui->actionExit);

        trayicon->setContextMenu(traymenu);
        trayicon->setToolTip(tr("Upcoder"));
        trayicon->show();
    } else
    {
        trayicon = 0;
        traymenu = 0;
    }

    time = new QTime();

    settingsdialog = 0;
    resdialog = 0;

    connect(ui->actionSettings, SIGNAL(triggered()), this, SLOT(settingsPressed()));
    connect(ui->actionAdd, SIGNAL(triggered()), this, SLOT(addPressed()));
    connect(ui->actionDelete, SIGNAL(triggered()), this, SLOT(deletePressed()));
    connect(ui->actionClear, SIGNAL(triggered()), model, SLOT(clear()));
    connect(ui->actionStart, SIGNAL(triggered()), model, SLOT(startUploading()));
    connect(ui->actionStop, SIGNAL(triggered()), model, SLOT(stopUploading()));
    connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(exitApplication()));
    connect(ui->actionAbout_Qt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
    connect(ui->actionShow_links_to_files, SIGNAL(triggered()), this, SLOT(showLinks()));
    connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(aboutPressed()));
    connect(ui->tableView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(showLinks()));

    connect(model, SIGNAL(statusChanged(QString)), ui->statusBar, SLOT(showMessage(QString)));
    connect(model, SIGNAL(progress(qint64,qint64)), this, SLOT(uploadProgress(qint64,qint64)));
    connect(model, SIGNAL(uploadingStarted()), this, SLOT(uploadingStarted()));
    connect(model, SIGNAL(allUploadsStarted()), this, SLOT(allUploadsStarted()));
    connect(model, SIGNAL(allUploadsFinished()), this, SLOT(allUploadsFinished()));
}
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    readSettings();
    ui->actionSound->setChecked(settings.soundsenabled);

    irc = new QIrc();
    if (!irc->createSession())
    {
        QMessageBox::critical(this, tr("Error"), tr("Can't create session"));
        close();
        return;
    }
    connect(irc, SIGNAL(sigConnected()), this, SLOT(sltConnected()));
    connect(irc, SIGNAL(sigMessage(QString,QString)), this, SLOT(sltMessage(QString,QString)));
    connect(irc, SIGNAL(sigChannelJoined(QString,QString)), this, SLOT(sltChannelJoined(QString,QString)));
    connect(irc, SIGNAL(sigChannelParted(QString,QString,QString)), this, SLOT(sltChannelParted(QString,QString,QString)));
    connect(irc, SIGNAL(sigChannelNames(QString,QString,QStringList)), this, SLOT(sltChannelNames(QString,QString,QStringList)));
    connect(irc, SIGNAL(sigChannelMessage(QString,QString,QString)), this, SLOT(sltChannelMessage(QString,QString,QString)));
    connect(irc, SIGNAL(sigPrivateMessage(QString,QString,QString)), this, SLOT(sltPrivateMessage(QString,QString,QString)));
    connect(irc, SIGNAL(sigNick(QString,QString)), this, SLOT(sltNick(QString,QString)));
    connect(irc, SIGNAL(sigNotice(QString,QString,QString)), this, SLOT(sltNotice(QString,QString,QString)));
    connect(irc, SIGNAL(sigTopic(QString,QString,QString)), this, SLOT(sltTopic(QString,QString,QString)));
    connect(irc, SIGNAL(sigTopicSet(QString,QString,QString,QString)), this, SLOT(sltTopicSet(QString,QString,QString,QString)));
    connect(irc, SIGNAL(sigKick(QString,QString,QString)), this, SLOT(sltKick(QString,QString,QString)));
    connect(irc, SIGNAL(sigQuit(QString,QString)), this, SLOT(sltQuit(QString,QString)));
    connect(irc, SIGNAL(sigChannelModeChanged(QString,QString,QStringList)), this, SLOT(sltChannelModeChanged(QString,QString,QStringList)));
    connect(irc, SIGNAL(sigUmode(QString,QString)), this, SLOT(sltUmode(QString,QString)));
    connect(irc, SIGNAL(sigInvite(QString,QString,QString)), this, SLOT(sltInvite(QString,QString,QString)));
    connect(irc, SIGNAL(sigCtcpAction(QString,QString,QString)), this, SLOT(sltCtcpAction(QString,QString,QString)));
    connect(irc, SIGNAL(sigErrorOccured(QString)), this, SLOT(sltErrorOccured(QString)));

    consolebrowser = new QTextEdit();
    consolebrowser->setReadOnly(true);
    QFont consolefont;
    if (consolefont.fromString(settings.consolefont))
        consolebrowser->setFont(consolefont);

    if (!settings.hideconsole)
        consoleindex =ui->tabWidget->insertTab(0, consolebrowser, tr("Console"));

    setsdlg = 0;

    usercontextmenu = new QMenu(this);
    usercontextmenu->addAction(ui->actionPrivateChat);
    usercontextmenu->addAction(ui->actionInsert_to_editor);
    QMenu *managementmenu = usercontextmenu->addMenu(tr("Management"));
    managementmenu->addAction(ui->actionKick);
    managementmenu->addAction(ui->actionBan);
    managementmenu->addAction(ui->actionKickBan);
    managementmenu->addSeparator();
    managementmenu->addAction(ui->actionOp);
    managementmenu->addAction(ui->actionDeOp);
    managementmenu->addSeparator();
    managementmenu->addAction(ui->actionVoice);
    managementmenu->addAction(ui->actiondeVoice);
    connect(ui->treeWidget, SIGNAL(userContextMenu(QPoint)), this, SLOT(userContextMenuRequested(QPoint)));

    channelcontextmenu = new QMenu(this);
    channelcontextmenu->addAction(ui->actionLeave_channel);
    channelcontextmenu->addAction(ui->actionChannel_settings);
    connect(ui->treeWidget, SIGNAL(channelContextMenu(QPoint)), this, SLOT(channelContextMenuRequested(QPoint)));

    channelsettingsdialog = 0;
    trayicon = 0;
    traymenu = 0;
    setTrayIcon(settings.trayicon);
    connect(ui->actionChannel_settings, SIGNAL(triggered()), this, SLOT(channelSettingsPressed()));

    //smile menu
    smilemenu = new QMenu(this);
    smilewidgetaction = new QWidgetAction(smilemenu);
    smilebar = new SmileBar();
    smilewidgetaction->setDefaultWidget(smilebar);
    smilemenu->addAction(smilewidgetaction);
    connect(smilebar, SIGNAL(smileClicked(QString)), ui->plainTextEditMessage, SLOT(insertPlainText(QString)));
    connect(smilebar, SIGNAL(smileClicked(QString)), ui->plainTextEditMessage, SLOT(setFocus()));
    ui->actionEmoticons->setMenu(smilemenu);
    smilebutton = new QToolButton();
    smilebutton->setDefaultAction(ui->actionEmoticons);
    smilebutton->setPopupMode(QToolButton::InstantPopup);
    ui->mainToolBar->addWidget(smilebutton);

    channellistdialog = new ChannelListDialog();
    connect(irc, SIGNAL(sigChannelListStart()), channellistdialog, SLOT(showAndClear()));
    connect(irc, SIGNAL(sigChannelListAddItem(QString,QString,QString)), channellistdialog,
            SLOT(addListItem(QString,QString,QString)));
    connect(irc, SIGNAL(sigChannelListEnd()), channellistdialog, SLOT(endOfList()));
    connect(channellistdialog, SIGNAL(channelDoubleClicked(QString)), irc, SLOT(joinChannel(QString)));

    connect(ui->pushButtonSend, SIGNAL(clicked()), ui->plainTextEditMessage, SIGNAL(sendKeyPressed()));
    connect(ui->plainTextEditMessage, SIGNAL(sendKeyPressed()), this, SLOT(sendPressed()));
    connect(ui->actionConnect, SIGNAL(triggered()), this, SLOT(connectPressed()));
    connect(ui->actionDisconnect, SIGNAL(triggered()), this, SLOT(disconnectPressed()));
    connect(ui->actionSettings, SIGNAL(triggered()), this, SLOT(settingsPressed()));
    connect(ui->actionJoin, SIGNAL(triggered()), this, SLOT(joinPressed()));
    connect(ui->treeWidget, SIGNAL(userDoubleClicked(QString)), this, SLOT(userDoubleClicked(QString)));
    connect(ui->actionPrivateChat, SIGNAL(triggered()), this, SLOT(privateChatPressed()));
    connect(ui->actionLeave_channel, SIGNAL(triggered()), this, SLOT(leaveChannelPressed()));
    connect(ui->actionKick, SIGNAL(triggered()), this, SLOT(kickPressed()));
    connect(ui->actionBan, SIGNAL(triggered()), this, SLOT(BanPressed()));
    connect(ui->actionKickBan, SIGNAL(triggered()), this, SLOT(kickBanPressed()));
    connect(ui->actionOp, SIGNAL(triggered()), this, SLOT(opPressed()));
    connect(ui->actionDeOp, SIGNAL(triggered()), this, SLOT(deOpPressed()));
    connect(ui->actionVoice, SIGNAL(triggered()), this, SLOT(voicePressed()));
    connect(ui->actiondeVoice, SIGNAL(triggered()), this, SLOT(devoicePressed()));
    connect(ui->pushButtonNick, SIGNAL(clicked()), this, SLOT(nickButtonPressed()));
    connect(ui->actionList_of_channels, SIGNAL(triggered()), irc, SLOT(getChannelsList()));
    connect(ui->actionAbout_Qt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
    connect(ui->tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(tabCloseRequested(int)));
    connect(ui->actionInsert_to_editor, SIGNAL(triggered()), this, SLOT(insertUserToEditorPressed()));
    connect(ui->actionShowHide, SIGNAL(triggered()), this, SLOT(trayIconActivated()));
    connect(ui->actionQuit_program, SIGNAL(triggered()), this, SLOT(quitProgram()));
    connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(aboutPressed()));
    connect(ui->actionSound, SIGNAL(toggled(bool)), this, SLOT(soundActionToogled(bool)));
}