You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mainwindow.cpp 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
  4. {
  5. Options::init(this);
  6. ui->setupUi(this);
  7. setFixedSize(size());
  8. m_chatMgr = new ChatManager(this);
  9. m_labelState = new QLabel();
  10. ui->statusbar->addPermanentWidget(m_labelState);
  11. m_labelState->setText(NetSoul::getStringFromState(NetSoul::Disconnected));
  12. ui->actionDisconnect->setEnabled(false);
  13. ui->actionAddContact->setEnabled(false);
  14. ui->contactsWidget->hide();
  15. m_tray = new QSystemTrayIcon(QIcon(":/epimafia/icon_red.jpg"), this);
  16. m_tray->setToolTip("Epimafia NetSoul - Disconnected");
  17. m_tray->show();
  18. QMenu* menu = new QMenu("Epimafia NetSoul", this);
  19. connect(menu->addAction("Show"), SIGNAL(triggered()), this, SLOT(showAndRaise()));
  20. menu->addSeparator();
  21. menu->addAction(ui->actionConnect);
  22. menu->addAction(ui->actionDisconnect);
  23. menu->addSeparator();
  24. connect(menu->addAction("Options"), SIGNAL(triggered()), this, SLOT(options()));
  25. connect(menu->addAction("Quit"), SIGNAL(triggered()), qApp, SLOT(quit()));
  26. m_tray->setContextMenu(menu);
  27. connect(m_tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayActivated(QSystemTrayIcon::ActivationReason)));
  28. m_netsoul = new NetSoul(this);
  29. connect(m_netsoul, SIGNAL(stateChanged(NetSoul::State)), this, SLOT(nsStateChanged(NetSoul::State)));
  30. connect(m_netsoul, SIGNAL(unknownCommand(QString)), this, SLOT(nsUnknownCommand(QString)));
  31. connect(m_netsoul, SIGNAL(stateChanged(NetSoul::State)), ui->loginWidget, SLOT(nsStateChanged(NetSoul::State)));
  32. connect(m_netsoul, SIGNAL(userDataChanged(NetSoul::User)), ui->contactsWidget, SLOT(nsUserDataChanged(NetSoul::User)));
  33. connect(m_netsoul, SIGNAL(stateChanged(NetSoul::State)), ui->contactsWidget, SLOT(nsStateChanged(NetSoul::State)));
  34. connect(ui->loginWidget, SIGNAL(loginRequested(EpiUser)), m_netsoul, SLOT(login(EpiUser)));
  35. connect(ui->contactsWidget, SIGNAL(userStateChanged(NetSoul::UserState)), m_netsoul, SLOT(setUserState(NetSoul::UserState)));
  36. connect(ui->contactsWidget, SIGNAL(removeFromContactsRequested(QStringList)), this, SLOT(removeContacts(QStringList)));
  37. connect(ui->contactsWidget, SIGNAL(chatRequested(NetSoul::Users)), m_chatMgr, SLOT(chat(NetSoul::Users)));
  38. connect(ui->actionConnect, SIGNAL(triggered()), ui->loginWidget, SLOT(login()));
  39. connect(ui->actionDisconnect, SIGNAL(triggered()), m_netsoul, SLOT(logout()));
  40. connect(ui->actionQuit, SIGNAL(triggered()), qApp, SLOT(quit()));
  41. connect(ui->actionOptions, SIGNAL(triggered()), this, SLOT(options()));
  42. connect(ui->actionAddContact, SIGNAL(triggered()), this, SLOT(addContact()));
  43. connect(m_chatMgr, SIGNAL(sendMessageRequested(NetSoul::User,QString)), m_netsoul, SLOT(sendMessage(NetSoul::User,QString)));
  44. connect(m_netsoul, SIGNAL(newMessage(NetSoul::Message)), m_chatMgr, SLOT(newMessage(NetSoul::Message)));
  45. m_contacts = Options::instance()->getContacts();
  46. if(Options::instance()->isAutoConnect() && ui->loginWidget->hasLogin(Options::instance()->getDefaultLogin()))
  47. ui->loginWidget->login();
  48. else
  49. show();
  50. }
  51. MainWindow::~MainWindow()
  52. {
  53. delete ui;
  54. }
  55. void MainWindow::closeEvent(QCloseEvent *)
  56. {
  57. if(qApp->arguments().contains("-debug"))
  58. qApp->exit(0);
  59. }
  60. void MainWindow::showAndRaise()
  61. {
  62. show();
  63. raise();
  64. }
  65. void MainWindow::toggleVisibility()
  66. {
  67. if(isVisible())
  68. hide();
  69. else
  70. showAndRaise();
  71. }
  72. void MainWindow::options()
  73. {
  74. DialogOptions dlg;
  75. dlg.exec();
  76. }
  77. void MainWindow::addContact()
  78. {
  79. DialogAddContact dlg;
  80. connect(&dlg, SIGNAL(contactAdded(QString)), this, SLOT(addContact(QString)));
  81. connect(&dlg, SIGNAL(chatRequested(NetSoul::Users)), m_chatMgr, SLOT(chat(NetSoul::Users)));
  82. m_netsoul->listUsers(&dlg, SLOT(setUsers(NetSoul::Users)));
  83. dlg.exec();
  84. }
  85. void MainWindow::addContact(QString contact)
  86. {
  87. addContacts(QStringList() << contact);
  88. }
  89. void MainWindow::addContacts(QStringList contacts)
  90. {
  91. for(int i = 0; i < contacts.size(); ++i)
  92. if(m_contacts.contains(contacts.at(i)) || m_netsoul->getUser().login == contacts.at(i))
  93. contacts.removeAt(i--);
  94. m_contacts.append(contacts);
  95. Options::instance()->setContacts(m_contacts);
  96. ui->contactsWidget->addContacts(contacts);
  97. m_netsoul->addToWatchList(contacts);
  98. m_netsoul->whois(contacts);
  99. }
  100. void MainWindow::removeContacts(QStringList contacts)
  101. {
  102. foreach(QString contact, contacts)
  103. if(contact != m_netsoul->getUser().login && m_contacts.removeAll(contact) != 0)
  104. ui->contactsWidget->removeFromContacts(contact);
  105. Options::instance()->setContacts(m_contacts);
  106. }
  107. void MainWindow::nsStateChanged(NetSoul::State s)
  108. {
  109. m_tray->setToolTip("Epimafia NetSoul - " + NetSoul::getStringFromState(s));
  110. if(s == NetSoul::LoggedIn)
  111. {
  112. m_tray->setIcon(QIcon(":/epimafia/icon.jpg"));
  113. QStringList contacts = m_contacts;
  114. if(!contacts.contains(m_netsoul->getUser().login))
  115. contacts += (QStringList() << m_netsoul->getUser().login);
  116. m_netsoul->addToWatchList(contacts);
  117. m_netsoul->whois(contacts);
  118. ui->contactsWidget->addContacts(contacts);
  119. m_chatMgr->setMe(m_netsoul->getUser());
  120. m_tray->setToolTip("Epimafia NetSoul - " + m_netsoul->getUser().login + " - " + NetSoul::getStringFromState(s));
  121. }
  122. else if(s == NetSoul::Disconnected || s == NetSoul::Disconnecting || s == NetSoul::NetworkError || s == NetSoul::ProtocolError || s == NetSoul::BadLogin)
  123. m_tray->setIcon(QIcon(":/epimafia/icon_red.jpg"));
  124. else
  125. m_tray->setIcon(QIcon(":/epimafia/icon_blue.jpg"));
  126. if(s == NetSoul::LoggedIn)
  127. m_labelState->setText(m_netsoul->getEpiUser().getLogin() + " - " + NetSoul::getStringFromState(s));
  128. else if(s == NetSoul::NetworkError)
  129. {
  130. const QMetaObject & mo = QAbstractSocket::staticMetaObject;
  131. QMetaEnum me = mo.enumerator(mo.indexOfEnumerator("SocketError"));
  132. m_labelState->setText(NetSoul::getStringFromState(s) + " - " + me.valueToKey(m_netsoul->getLastNetworkError()));
  133. }
  134. else
  135. m_labelState->setText(NetSoul::getStringFromState(s));
  136. ui->actionConnect->setEnabled(s == NetSoul::Disconnected || s == NetSoul::ProtocolError || s == NetSoul::NetworkError || s == NetSoul::BadLogin);
  137. ui->actionDisconnect->setEnabled(!ui->actionConnect->isEnabled());
  138. ui->loginWidget->setVisible(s != NetSoul::LoggedIn);
  139. ui->contactsWidget->setVisible(s == NetSoul::LoggedIn);
  140. ui->actionAddContact->setEnabled(s == NetSoul::LoggedIn);
  141. if(s == NetSoul::NetworkError || s == NetSoul::ProtocolError)
  142. QTimer::singleShot(Options::instance()->getAutoReconnectInterval(), ui->loginWidget, SLOT(login()));
  143. }
  144. void MainWindow::nsUnknownCommand(QString cmd)
  145. {
  146. qDebug()<<"Unknown command:"<<cmd;
  147. }
  148. void MainWindow::trayActivated(QSystemTrayIcon::ActivationReason r)
  149. {
  150. if(r == QSystemTrayIcon::DoubleClick)
  151. toggleVisibility();
  152. }