123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
-
- MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
- {
- Options::init(this);
- ui->setupUi(this);
- setFixedSize(size());
- m_chatMgr = new ChatManager(this);
-
- m_labelState = new QLabel();
- ui->statusbar->addPermanentWidget(m_labelState);
- m_labelState->setText(NetSoul::getStringFromState(NetSoul::Disconnected));
- ui->actionDisconnect->setEnabled(false);
- ui->actionAddContact->setEnabled(false);
- ui->contactsWidget->hide();
-
- m_tray = new QSystemTrayIcon(QIcon(":/epimafia/icon_red.jpg"), this);
- m_tray->setToolTip("Epimafia NetSoul - Disconnected");
- m_tray->show();
- QMenu* menu = new QMenu("Epimafia NetSoul", this);
- connect(menu->addAction("Show"), SIGNAL(triggered()), this, SLOT(showAndRaise()));
- menu->addSeparator();
- menu->addAction(ui->actionConnect);
- menu->addAction(ui->actionDisconnect);
- menu->addSeparator();
- connect(menu->addAction("Options"), SIGNAL(triggered()), this, SLOT(options()));
- connect(menu->addAction("Quit"), SIGNAL(triggered()), qApp, SLOT(quit()));
- m_tray->setContextMenu(menu);
- connect(m_tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayActivated(QSystemTrayIcon::ActivationReason)));
-
- m_netsoul = new NetSoul(this);
- connect(m_netsoul, SIGNAL(stateChanged(NetSoul::State)), this, SLOT(nsStateChanged(NetSoul::State)));
- connect(m_netsoul, SIGNAL(unknownCommand(QString)), this, SLOT(nsUnknownCommand(QString)));
-
- connect(m_netsoul, SIGNAL(stateChanged(NetSoul::State)), ui->loginWidget, SLOT(nsStateChanged(NetSoul::State)));
- connect(m_netsoul, SIGNAL(userDataChanged(NetSoul::User)), ui->contactsWidget, SLOT(nsUserDataChanged(NetSoul::User)));
- connect(m_netsoul, SIGNAL(stateChanged(NetSoul::State)), ui->contactsWidget, SLOT(nsStateChanged(NetSoul::State)));
-
- connect(ui->loginWidget, SIGNAL(loginRequested(EpiUser)), m_netsoul, SLOT(login(EpiUser)));
- connect(ui->contactsWidget, SIGNAL(userStateChanged(NetSoul::UserState)), m_netsoul, SLOT(setUserState(NetSoul::UserState)));
- connect(ui->contactsWidget, SIGNAL(removeFromContactsRequested(QStringList)), this, SLOT(removeContacts(QStringList)));
- connect(ui->contactsWidget, SIGNAL(chatRequested(NetSoul::Users)), m_chatMgr, SLOT(chat(NetSoul::Users)));
- connect(ui->actionConnect, SIGNAL(triggered()), ui->loginWidget, SLOT(login()));
- connect(ui->actionDisconnect, SIGNAL(triggered()), m_netsoul, SLOT(logout()));
- connect(ui->actionQuit, SIGNAL(triggered()), qApp, SLOT(quit()));
- connect(ui->actionOptions, SIGNAL(triggered()), this, SLOT(options()));
- connect(ui->actionAddContact, SIGNAL(triggered()), this, SLOT(addContact()));
- connect(m_chatMgr, SIGNAL(sendMessageRequested(NetSoul::User,QString)), m_netsoul, SLOT(sendMessage(NetSoul::User,QString)));
-
- connect(m_netsoul, SIGNAL(newMessage(NetSoul::Message)), m_chatMgr, SLOT(newMessage(NetSoul::Message)));
-
- m_contacts = Options::instance()->getContacts();
- if(Options::instance()->isAutoConnect() && ui->loginWidget->hasLogin(Options::instance()->getDefaultLogin()))
- ui->loginWidget->login();
- else
- show();
- }
-
- MainWindow::~MainWindow()
- {
- delete ui;
- }
-
- void MainWindow::closeEvent(QCloseEvent *)
- {
- if(qApp->arguments().contains("-debug"))
- qApp->exit(0);
- }
-
- void MainWindow::showAndRaise()
- {
- show();
- raise();
- }
-
- void MainWindow::toggleVisibility()
- {
- if(isVisible())
- hide();
- else
- showAndRaise();
- }
-
- void MainWindow::options()
- {
- DialogOptions dlg;
- dlg.exec();
- }
-
- void MainWindow::addContact()
- {
- DialogAddContact dlg;
- connect(&dlg, SIGNAL(contactAdded(QString)), this, SLOT(addContact(QString)));
- connect(&dlg, SIGNAL(chatRequested(NetSoul::Users)), m_chatMgr, SLOT(chat(NetSoul::Users)));
- m_netsoul->listUsers(&dlg, SLOT(setUsers(NetSoul::Users)));
- dlg.exec();
- }
-
- void MainWindow::addContact(QString contact)
- {
- addContacts(QStringList() << contact);
- }
-
- void MainWindow::addContacts(QStringList contacts)
- {
- for(int i = 0; i < contacts.size(); ++i)
- if(m_contacts.contains(contacts.at(i)) || m_netsoul->getUser().login == contacts.at(i))
- contacts.removeAt(i--);
- m_contacts.append(contacts);
- Options::instance()->setContacts(m_contacts);
- ui->contactsWidget->addContacts(contacts);
- m_netsoul->addToWatchList(contacts);
- m_netsoul->whois(contacts);
- }
-
- void MainWindow::removeContacts(QStringList contacts)
- {
- foreach(QString contact, contacts)
- if(contact != m_netsoul->getUser().login && m_contacts.removeAll(contact) != 0)
- ui->contactsWidget->removeFromContacts(contact);
- Options::instance()->setContacts(m_contacts);
- }
-
- void MainWindow::nsStateChanged(NetSoul::State s)
- {
- m_tray->setToolTip("Epimafia NetSoul - " + NetSoul::getStringFromState(s));
- if(s == NetSoul::LoggedIn)
- {
- m_tray->setIcon(QIcon(":/epimafia/icon.jpg"));
- QStringList contacts = m_contacts;
- if(!contacts.contains(m_netsoul->getUser().login))
- contacts += (QStringList() << m_netsoul->getUser().login);
-
- m_netsoul->addToWatchList(contacts);
- m_netsoul->whois(contacts);
- ui->contactsWidget->addContacts(contacts);
- m_chatMgr->setMe(m_netsoul->getUser());
- m_tray->setToolTip("Epimafia NetSoul - " + m_netsoul->getUser().login + " - " + NetSoul::getStringFromState(s));
- }
- else if(s == NetSoul::Disconnected || s == NetSoul::Disconnecting || s == NetSoul::NetworkError || s == NetSoul::ProtocolError || s == NetSoul::BadLogin)
- m_tray->setIcon(QIcon(":/epimafia/icon_red.jpg"));
- else
- m_tray->setIcon(QIcon(":/epimafia/icon_blue.jpg"));
-
- if(s == NetSoul::LoggedIn)
- m_labelState->setText(m_netsoul->getEpiUser().getLogin() + " - " + NetSoul::getStringFromState(s));
- else if(s == NetSoul::NetworkError)
- {
- const QMetaObject & mo = QAbstractSocket::staticMetaObject;
- QMetaEnum me = mo.enumerator(mo.indexOfEnumerator("SocketError"));
- m_labelState->setText(NetSoul::getStringFromState(s) + " - " + me.valueToKey(m_netsoul->getLastNetworkError()));
- }
- else
- m_labelState->setText(NetSoul::getStringFromState(s));
-
- ui->actionConnect->setEnabled(s == NetSoul::Disconnected || s == NetSoul::ProtocolError || s == NetSoul::NetworkError || s == NetSoul::BadLogin);
- ui->actionDisconnect->setEnabled(!ui->actionConnect->isEnabled());
- ui->loginWidget->setVisible(s != NetSoul::LoggedIn);
- ui->contactsWidget->setVisible(s == NetSoul::LoggedIn);
- ui->actionAddContact->setEnabled(s == NetSoul::LoggedIn);
-
- if(s == NetSoul::NetworkError || s == NetSoul::ProtocolError)
- QTimer::singleShot(Options::instance()->getAutoReconnectInterval(), ui->loginWidget, SLOT(login()));
- }
-
- void MainWindow::nsUnknownCommand(QString cmd)
- {
- qDebug()<<"Unknown command:"<<cmd;
- }
-
- void MainWindow::trayActivated(QSystemTrayIcon::ActivationReason r)
- {
- if(r == QSystemTrayIcon::DoubleClick)
- toggleVisibility();
- }
|