PopupChatWidget.C 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (C) 2010 Emweb bvba, Heverlee, Belgium.
  3. *
  4. * See the LICENSE file for terms of use.
  5. */
  6. #include <Wt/WApplication>
  7. #include <Wt/WEnvironment>
  8. #include <Wt/WImage>
  9. #include <Wt/WText>
  10. #include <Wt/WVBoxLayout>
  11. #include "PopupChatWidget.h"
  12. #include "SimpleChatServer.h"
  13. // TODO:
  14. // - i18n
  15. PopupChatWidget::PopupChatWidget(SimpleChatServer& server,
  16. const std::string& id)
  17. : SimpleChatWidget(server),
  18. missedMessages_(0)
  19. {
  20. setId(id);
  21. if (Wt::WApplication::instance()->environment().agentIsIE()) {
  22. if (Wt::WApplication::instance()->environment().agent()
  23. == Wt::WEnvironment::IE6)
  24. setPositionScheme(Wt::Absolute);
  25. else
  26. setPositionScheme(Wt::Fixed);
  27. }
  28. implementJavaScript
  29. (&PopupChatWidget::toggleSize,
  30. "{"
  31. """var s = $('#" + id + "');"
  32. """s.toggleClass('chat-maximized chat-minimized');"
  33. + Wt::WApplication::instance()->javaScriptClass()
  34. + ".layouts2.scheduleAdjust(true);"
  35. "}");
  36. online_ = false;
  37. minimized_ = true;
  38. setStyleClass("chat-widget chat-minimized");
  39. clear();
  40. addWidget(createBar());
  41. updateUsers();
  42. connect();
  43. }
  44. void PopupChatWidget::setName(const Wt::WString& name)
  45. {
  46. if (name.empty())
  47. return;
  48. if (online_) {
  49. int tries = 1;
  50. Wt::WString n = name;
  51. while (!server().changeName(name_, n))
  52. n = name + boost::lexical_cast<std::string>(++tries);
  53. name_ = n;
  54. } else
  55. name_ = name;
  56. }
  57. Wt::WContainerWidget *PopupChatWidget::createBar()
  58. {
  59. Wt::WContainerWidget *bar = new Wt::WContainerWidget();
  60. bar->setStyleClass("chat-bar");
  61. Wt::WText *toggleButton = new Wt::WText();
  62. toggleButton->setInline(false);
  63. toggleButton->setStyleClass("chat-minmax");
  64. bar->clicked().connect(this, &PopupChatWidget::toggleSize);
  65. bar->clicked().connect(this, &PopupChatWidget::goOnline);
  66. bar->addWidget(toggleButton);
  67. title_ = new Wt::WText(bar);
  68. bar_ = bar;
  69. return bar;
  70. }
  71. void PopupChatWidget::toggleSize()
  72. {
  73. minimized_ = !minimized_;
  74. }
  75. void PopupChatWidget::goOnline()
  76. {
  77. if (!online_) {
  78. online_ = true;
  79. int tries = 1;
  80. Wt::WString name = name_;
  81. if (name.empty())
  82. name = server().suggestGuest();
  83. while (!startChat(name)) {
  84. if (name_.empty())
  85. name = server().suggestGuest();
  86. else
  87. name = name_ + boost::lexical_cast<std::string>(++tries);
  88. }
  89. name_ = name;
  90. }
  91. missedMessages_ = 0;
  92. bar_->removeStyleClass("alert");
  93. }
  94. void PopupChatWidget::createLayout(Wt::WWidget *messages,
  95. Wt::WWidget *userList,
  96. Wt::WWidget *messageEdit,
  97. Wt::WWidget *sendButton,
  98. Wt::WWidget *logoutButton)
  99. {
  100. Wt::WVBoxLayout *layout = new Wt::WVBoxLayout();
  101. layout->setContentsMargins(0, 0, 0, 0);
  102. layout->setSpacing(0);
  103. Wt::WContainerWidget *bar = createBar();
  104. layout->addWidget(bar);
  105. bar->setMinimumSize(Wt::WLength::Auto, 20);
  106. layout->addWidget(messages, 1);
  107. layout->addWidget(messageEdit);
  108. setLayout(layout);
  109. }
  110. void PopupChatWidget::updateUsers()
  111. {
  112. SimpleChatWidget::updateUsers();
  113. int count = server().users().size();
  114. if (!loggedIn()) {
  115. if (count == 0)
  116. title_->setText("Thoughts? Ventilate.");
  117. else if (count == 1)
  118. title_->setText("Chat: 1 user online");
  119. else
  120. title_->setText(Wt::WString("Chat: {1} users online").arg(count));
  121. } else {
  122. title_->setText(Wt::WString("Chat: <span class=\"self\">{1}</span>"
  123. " <span class=\"online\">({2} user{3})</span>")
  124. .arg(userName()).arg(count).arg(count == 1 ? "" : "s"));
  125. }
  126. }
  127. void PopupChatWidget::newMessage()
  128. {
  129. if (loggedIn() && minimized()) {
  130. ++missedMessages_;
  131. if (missedMessages_ == 1) {
  132. bar_->addStyleClass("alert");
  133. }
  134. }
  135. }
  136. bool PopupChatWidget::minimized() const
  137. {
  138. return minimized_;
  139. }