ApplicationLogic.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "ApplicationLogic.h"
  2. #include "NetworkModel.h"
  3. #include "HttpServer.h"
  4. #include "PhoneBookServlet.h"
  5. #include <QNetworkSession>
  6. #include <QNetworkAddressEntry>
  7. ApplicationLogic::ApplicationLogic(QObject *parent)
  8. : QObject(parent)
  9. , m_networkModel(new NetworkModel(this))
  10. , m_session (NULL)
  11. {
  12. }
  13. ApplicationLogic::~ApplicationLogic()
  14. {
  15. }
  16. QObject* ApplicationLogic::networkModel() const
  17. {
  18. return m_networkModel;
  19. }
  20. void ApplicationLogic::setNetworkConfigurationRow(int row)
  21. {
  22. // Destroy old session if neccessary;
  23. if (m_session != NULL) {
  24. m_session->close();
  25. m_session = NULL;
  26. }
  27. Q_ASSERT (m_session == NULL);
  28. QNetworkConfiguration configuration(m_networkModel->configurationForRow(row));
  29. m_session = new QNetworkSession(configuration, NULL);
  30. m_session->open();
  31. m_session->waitForOpened(-1);
  32. HttpServer* server = new HttpServer(NULL);
  33. PhoneBookServlet* phone_book_servlet = new PhoneBookServlet(server);
  34. server->addServlet("/phonebook", phone_book_servlet);
  35. server->setPort(8080);
  36. server->setEnabled(true);
  37. emit ipChanged();
  38. }
  39. QString ApplicationLogic::ip() const
  40. {
  41. QString visible_ip;
  42. foreach (QNetworkAddressEntry entry, m_session->interface().addressEntries()) {
  43. visible_ip.append(entry.ip().toString());
  44. visible_ip.append('\n');
  45. }
  46. return visible_ip;
  47. }