選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

mainwindow.cpp 6.6KB

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