From d86feee48221f5c92a16a62ce4a411d28cc1ae96 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 26 May 2015 17:18:52 +0200 Subject: [PATCH] cleaner code, nogui mode more stable --- .gitignore | 6 +++- main.cpp | 35 +++++++++++------------ mainwindow.cpp | 10 ------- mainwindow.ui | 29 ------------------- socketirc.cpp | 41 +++++++++++++-------------- socketirc.h | 6 ++-- sparrowbot.cpp | 37 ++++++++++--------------- sparrowbot.h | 3 +- ui_mainwindow.h | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 137 insertions(+), 104 deletions(-) create mode 100644 ui_mainwindow.h diff --git a/.gitignore b/.gitignore index b81d87c..14f331c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,6 @@ build/* -*.user \ No newline at end of file +debug/* +release/* +*.user +*.Debug +*.Release \ No newline at end of file diff --git a/main.cpp b/main.cpp index 43081f4..fbe5068 100644 --- a/main.cpp +++ b/main.cpp @@ -5,45 +5,46 @@ int main(int argc, char *argv[]) { - // set up the socket + // create the socket SocketIRC sock; sock.setServer("irc.freenode.net"); sock.setPort(6667); - // set up the bot + // create the bot SparrowBot* bot = new SparrowBot("SparrowBot", "epicsparrow"); + + // connect the bot to the socket QObject::connect(&sock, SIGNAL(receivedMsg(QString)), bot, SLOT(receiveMsg(QString))); QObject::connect(bot, SIGNAL(sendMsg(QString)), &sock, SLOT(sendMsg(QString))); - QObject::connect(bot, SIGNAL(changeSocketStatus(int)), &sock, SLOT(setConnected(int))); - - // set up Qt + // parsing arguments bool nogui = argc > 1 && QString(argv[1]).compare("-nogui") == 0; // set up UI if(!nogui) { QApplication app(argc, argv); + + // init window MainWindow w; - - // bind signals to slots QObject::connect(&sock, SIGNAL(receivedMsg(QString)), w.getConsole(), SLOT(append(QString))); - QObject::connect(&sock, SIGNAL(stateChanged(int)), w.getLed(), SLOT(setValue(int))); - QObject::connect(&sock, SIGNAL(stateChanged(int)), w.getSwitch(), SLOT(setValue(int))); - + QObject::connect(bot, SIGNAL(sendMsg(QString)), w.getConsole(), SLOT(append(QString))); QObject::connect(w.getPrompt(), SIGNAL(sendMsg(QString)), &sock, SLOT(sendMsg(QString))); QObject::connect(w.getPrompt(), SIGNAL(returnPressed()), w.getPrompt(), SLOT(confirmationPerformed())); - - QObject::connect(w.getSwitch(), SIGNAL(valueChanged(int)), bot, SLOT(forceStatus(int))); - - // show window w.show(); - return app.exec(); + + QObject::connect(&app, SIGNAL(aboutToQuit()), bot, SLOT(disconnect())); + + sock.connectToServer(&app); } else { QCoreApplication coreApp(argc, argv); - bot->forceStatus(1); - return coreApp.exec(); + + QObject::connect(&coreApp, SIGNAL(aboutToQuit()), bot, SLOT(disconnect())); + + sock.connectToServer(&coreApp); } + delete(bot); + return 0; } diff --git a/mainwindow.cpp b/mainwindow.cpp index 399f54e..87d54f5 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -13,16 +13,6 @@ QTextBrowser* MainWindow::getConsole() return this->ui->monitoringConsole; } -QSlider* MainWindow::getSwitch() -{ - return this->ui->connectSwitch; -} - -QProgressBar* MainWindow::getLed() -{ - return this->ui->statusLed; -} - Prompt* MainWindow::getPrompt() { return this->ui->prompt; diff --git a/mainwindow.ui b/mainwindow.ui index 2cd4ba3..50a717a 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -15,35 +15,6 @@ - - - - 1 - - - 1 - - - Qt::Horizontal - - - - - - - 1 - - - 0 - - - false - - - false - - - diff --git a/socketirc.cpp b/socketirc.cpp index 12d740c..b3a6550 100644 --- a/socketirc.cpp +++ b/socketirc.cpp @@ -4,6 +4,7 @@ SocketIRC::SocketIRC() : server("irc.freenode.net"), port(6667), isConnected(false) { connect(&sock, SIGNAL(readyRead()), this, SLOT(readMsg())); + connect(&sock, SIGNAL(disconnected()), this, SLOT(onDisconnect())); } void SocketIRC::setServer(std::string server_) @@ -18,34 +19,25 @@ void SocketIRC::setPort(int port_) void SocketIRC::sendMsg(QString msg) { - if(isConnected) - { - sock.write(msg.toStdString().c_str()); - sock.flush(); - std::cout << msg.toStdString(); - } + msg.append("\r\n"); + sock.write(msg.toStdString().c_str()); + sock.flush(); } -void SocketIRC::setConnected(int c) +void SocketIRC::connectToServer(QCoreApplication* app_) { - if(c != isConnected) + app = app_; + + sock.connectToHost(QString(server.c_str()), quint16(port)); + if(!sock.waitForConnected(3000)) { - if(isConnected) - { - sock.close(); - isConnected = false; - emit stateChanged(isConnected); - } + std::cerr << "failed to connect : " << sock.errorString().toStdString() << std::endl; + if(app != NULL) + app->exit(); else - { - sock.connectToHost(QString(server.c_str()), quint16(port)); - if(sock.waitForConnected(5000)) - isConnected = true; - else - std::cerr << "failed to connect : " << sock.errorString().toStdString() << std::endl; - emit stateChanged(isConnected); - } + exit(0); } + app->exec(); } void SocketIRC::readMsg() @@ -59,3 +51,8 @@ void SocketIRC::readMsg() emit receivedMsg(QString(buffer)); } } + +void SocketIRC::onDisconnect() +{ + app->exit(); +} diff --git a/socketirc.h b/socketirc.h index 31803ea..fbe6f55 100644 --- a/socketirc.h +++ b/socketirc.h @@ -4,6 +4,7 @@ #include #include #include +#include class SocketIRC : public QObject { @@ -14,9 +15,11 @@ public: SocketIRC(); void setServer(std::string server_); void setPort(int port_); + void connectToServer(QCoreApplication* app_); private: + QCoreApplication* app; std::string server; int port; QTcpSocket sock; @@ -24,14 +27,13 @@ private: private slots: void readMsg(); + void onDisconnect(); public slots: void sendMsg(QString msg); - void setConnected(int c); signals: void receivedMsg(QString msg); - void stateChanged(int newState); }; #endif // SOCKETIRC_H diff --git a/sparrowbot.cpp b/sparrowbot.cpp index 7b6f920..2047f79 100644 --- a/sparrowbot.cpp +++ b/sparrowbot.cpp @@ -31,21 +31,6 @@ void SparrowBot::receiveMsg(QString str) } } -void SparrowBot::forceStatus(int newStatus) -{ - emit changeSocketStatus(newStatus); - if(newStatus) - { - //status = ONLINE; - //sendRawMessage("NICK " + nick + "\r\nUSER " + nick + " 0 * " + nick + "\r\n"); - } - else - { - status = OFFLINE; - users.clear(); - } -} - void SparrowBot::handleMessage(Message msg) { // si la ligne est un message @@ -53,10 +38,8 @@ void SparrowBot::handleMessage(Message msg) { // mise à jour du bot if(msg.args.compare("!update") == 0 && msg.src != NULL && msg.src->isOp()) - { - emit sendMsg("QUIT :i'll be back\r\n"); - exit(0); - } + disconnect("Rebooting for update..."); + // affiche les gens connectés if(msg.args.compare("!list") == 0) { @@ -95,7 +78,7 @@ void SparrowBot::handleMessage(Message msg) for(QString s : names) { users.getOrAdd(s); - emit sendMsg(QString("WHOIS %1\r\n").arg(s)); + emit sendMsg(QString("WHOIS %1").arg(s)); } } @@ -117,11 +100,21 @@ void SparrowBot::handleMessage(Message msg) void SparrowBot::say(QString str) { - emit sendMsg(QString("PRIVMSG #%1 :%2\r\n").arg(chan).arg(str)); + emit sendMsg(QString("PRIVMSG #%1 :%2").arg(chan).arg(str)); } void SparrowBot::action(QString str) { - emit sendMsg(QString("PRIVMSG #%1 :ACTION %2\r\n").arg(chan).arg(str)); + emit sendMsg(QString("PRIVMSG #%1 :ACTION %2 ").arg(chan).arg(str)); +} + +void SparrowBot::disconnect() +{ + disconnect("I'll be back"); +} + +void SparrowBot::disconnect(QString str) +{ + emit sendMsg(QString("QUIT :%1").arg(str)); } diff --git a/sparrowbot.h b/sparrowbot.h index c559c2c..2c4f39f 100644 --- a/sparrowbot.h +++ b/sparrowbot.h @@ -27,7 +27,8 @@ private: public slots: void receiveMsg(QString msg); - void forceStatus(int newStatus); + void disconnect(); + void disconnect(QString str); signals: void sendMsg(QString msg); diff --git a/ui_mainwindow.h b/ui_mainwindow.h new file mode 100644 index 0000000..c283a1c --- /dev/null +++ b/ui_mainwindow.h @@ -0,0 +1,74 @@ +/******************************************************************************** +** Form generated from reading UI file 'mainwindow.ui' +** +** Created by: Qt User Interface Compiler version 5.4.1 +** +** WARNING! All changes made in this file will be lost when recompiling UI file! +********************************************************************************/ + +#ifndef UI_MAINWINDOW_H +#define UI_MAINWINDOW_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "prompt.h" + +QT_BEGIN_NAMESPACE + +class Ui_MainWindow +{ +public: + QWidget *centralWidget; + QGridLayout *gridLayout; + QTextBrowser *monitoringConsole; + Prompt *prompt; + + void setupUi(QMainWindow *MainWindow) + { + if (MainWindow->objectName().isEmpty()) + MainWindow->setObjectName(QStringLiteral("MainWindow")); + MainWindow->resize(374, 404); + centralWidget = new QWidget(MainWindow); + centralWidget->setObjectName(QStringLiteral("centralWidget")); + gridLayout = new QGridLayout(centralWidget); + gridLayout->setSpacing(6); + gridLayout->setContentsMargins(11, 11, 11, 11); + gridLayout->setObjectName(QStringLiteral("gridLayout")); + monitoringConsole = new QTextBrowser(centralWidget); + monitoringConsole->setObjectName(QStringLiteral("monitoringConsole")); + + gridLayout->addWidget(monitoringConsole, 1, 0, 1, 2); + + prompt = new Prompt(centralWidget); + prompt->setObjectName(QStringLiteral("prompt")); + + gridLayout->addWidget(prompt, 0, 0, 1, 2); + + MainWindow->setCentralWidget(centralWidget); + + retranslateUi(MainWindow); + + QMetaObject::connectSlotsByName(MainWindow); + } // setupUi + + void retranslateUi(QMainWindow *MainWindow) + { + MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0)); + } // retranslateUi + +}; + +namespace Ui { + class MainWindow: public Ui_MainWindow {}; +} // namespace Ui + +QT_END_NAMESPACE + +#endif // UI_MAINWINDOW_H