Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) CppWtChat1

 

CppWtChat1 is an example of a Wt chat application that is as simple as possible, except for showing About information.

 

 

 

 

 

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: CppWtChat1.pro

 

#-------------------------------------------------
#
# Project created by QtCreator 2011-04-10T11:16:42
#
#-------------------------------------------------
QT       += core
QT       -= gui
LIBS += -lwt -lwthttp -lboost_program_options
QMAKE_CXXFLAGS += -DNDEBUG
TARGET = CppWtChat1
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app

INCLUDEPATH += \
    ../../Classes/CppAbout \
    ../../Classes/CppWtAboutDialog

SOURCES += wtmain.cpp \
    wtchat1dialog.cpp \
    ../../Classes/CppAbout/about.cpp \
    ../../Classes/CppWtAboutDialog/wtaboutdialog.cpp

HEADERS  += \
    wtchat1dialog.cpp \
    ../../Classes/CppAbout/about.h \
    ../../Classes/CppWtAboutDialog/wtaboutdialog.h


 

 

 

 

 

wtchat1dialog.cpp

 

//---------------------------------------------------------------------------
/*
WtChat, chatting server
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppWtChat1.htm
//---------------------------------------------------------------------------
#include <fstream>
//---------------------------------------------------------------------------
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
//---------------------------------------------------------------------------
#include <Wt/WApplication>
#include <Wt/WBreak>
#include <Wt/WLabel>
#include <Wt/WLineEdit>
#include <Wt/WTextArea>
#include <Wt/WTimer>
//---------------------------------------------------------------------------
#include "about.h"
#include "wtaboutdialog.h"
#include "wtchat1dialog.h"
//---------------------------------------------------------------------------
const std::string WtChat1Dialog::m_chat_filename = "toolwtchat.txt";
//---------------------------------------------------------------------------
WtChat1Dialog::WtChat1Dialog()
{
  this->setContentAlignment(Wt::AlignCenter);
  ShowChat();
}
//---------------------------------------------------------------------------
///FileToVector reads a file and converts it to a std::vector<std::string>
///From http://www.richelbilderbeek.nl/CppFileToVector.htm
const std::vector<std::string> WtChat1Dialog::FileToVector(const std::string& filename)
{
  assert(!boost::filesystem::exists(m_chat_filename));

  std::vector<std::string> v;
  std::ifstream in(filename.c_str());
  std::string s;
  for (int i=0; !in.eof(); ++i)
  {
    std::getline(in,s);
    v.push_back(s);
  }
  return v;
}
//---------------------------------------------------------------------------
const About WtChat1Dialog::GetAbout()
{
  About a(
    "Richel Bilderbeek",
    "WtChat1",
    "very simple chatting server",
    "the 12th of April 2011",
    "2011",
    "http://www.richelbilderbeek.nl/CppWtChat1.htm",
    GetVersion(),
    GetVersionHistory());
  //a.AddLibrary("Encranger version: " + Encranger::GetVersion());
  //a.AddLibrary("LoopReader version: " + LoopReader<int>::GetVersion());
  return a;
}
//---------------------------------------------------------------------------
const std::string WtChat1Dialog::GetVersion()
{
  return "1.0";
}
//---------------------------------------------------------------------------
const std::vector<std::string> WtChat1Dialog::GetVersionHistory()
{
  std::vector<std::string> v;
  v.push_back("2011-04-12: Version 1.0: initial version");
  return v;
}
//---------------------------------------------------------------------------
void WtChat1Dialog::OnAboutClick()
{
  m_timer->stop();
  ShowAbout();
}
//---------------------------------------------------------------------------
void WtChat1Dialog::OnCloseClick()
{
  m_signal_close();
}
//---------------------------------------------------------------------------
void WtChat1Dialog::OnEditChange()
{
  if (m_edit->text().empty()) return;
  const std::string new_text
    = m_text->text().toUTF8()
    + boost::lexical_cast<std::string>('\n')
    + m_edit->text().toUTF8();
  {
    std::ofstream f(m_chat_filename.c_str());
    f << new_text;
  }
  m_text->setText(new_text);
  m_edit->setText("");
}
//---------------------------------------------------------------------------
void WtChat1Dialog::OnTimer()
{
  if (!boost::filesystem::exists(m_chat_filename)) return;
  const std::vector<std::string> v = FileToVector(m_chat_filename);
  std::string text;
  BOOST_FOREACH(const std::string s,v)
  {
    if(s.empty()) continue;
    text+=s;
    text+='\n';
  }
  if (!text.empty()) text.resize(text.size() - 1); //pop '\n'
  m_text->setText(text);
}
//---------------------------------------------------------------------------
void WtChat1Dialog::ShowAbout()
{
  About a = GetAbout();
  WtAboutDialog * const d = new WtAboutDialog(a);
  d->m_signal_close.connect(
    boost::bind(
    &WtChat1Dialog::ShowChat,
    this));
  this->clear();
  this->addWidget(d);
}
//---------------------------------------------------------------------------
void WtChat1Dialog::ShowChat()
{
  this->clear();
  //Title
  {
    Wt::WLabel * const title = new Wt::WLabel("WtChat1");
    title->setStyleClass("title");
    this->addWidget(title);
  }
  this->addWidget(new Wt::WBreak);
  {
    m_text = new Wt::WTextArea;
    this->addWidget(m_text);
  }
  this->addWidget(new Wt::WBreak);
  {
    m_edit = new Wt::WLineEdit;
    m_edit->changed().connect(
      this, &WtChat1Dialog::OnEditChange);
    this->addWidget(m_edit);
  }
  this->addWidget(new Wt::WBreak);
  //About button
  {
    Wt::WPushButton * const button = new Wt::WPushButton("About");
    button->clicked().connect(
      this, &WtChat1Dialog::OnAboutClick);
    this->addWidget(button);
  }
  //Close button
  {
    Wt::WPushButton * const button = new Wt::WPushButton("Close");
    button->clicked().connect(
      this, &WtChat1Dialog::OnCloseClick);
    this->addWidget(button);
  }
  m_timer = new Wt::WTimer;
  m_timer->setInterval(1000);
  m_timer->start();
  m_timer->timeout().connect(
    this, &WtChat1Dialog::OnTimer);
  }
//---------------------------------------------------------------------------

 

 

 

 

 

wtchat1dialog.h

 

//---------------------------------------------------------------------------
/*
WtChat, chatting server
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppWtChat1.htm
//---------------------------------------------------------------------------
#ifndef WTCHAT1DIALOG_H
#define WTCHAT1DIALOG_H
//---------------------------------------------------------------------------
#include <boost/signals2.hpp>
//---------------------------------------------------------------------------
#include <Wt/WContainerWidget>
//---------------------------------------------------------------------------
#include "about.h"
//---------------------------------------------------------------------------
namespace Wt
{
  struct WLineEdit;
  struct WTextArea;
  struct WTimer;
}
//---------------------------------------------------------------------------
struct WtChat1Dialog : public Wt::WContainerWidget
{
  WtChat1Dialog();
  mutable boost::signals2::signal<void ()> m_signal_close;

  private:
  Wt::WLineEdit * m_edit;
  Wt::WTextArea * m_text;
  Wt::WTimer * m_timer;
  static const std::string m_chat_filename;

  void OnAboutClick();
  void OnCloseClick();
  void OnEditChange();
  void OnTimer();
  void ShowAbout();
  void ShowChat();

  public:
  static const std::vector<std::string> FileToVector(const std::string& filename);
  static const About GetAbout();
  static const std::string GetVersion();
  static const std::vector<std::string> GetVersionHistory();

};
//---------------------------------------------------------------------------
#endif // WTCHAT1DIALOG_H

 

 

 

 

 

wtmain.cpp

 

//---------------------------------------------------------------------------
/*
WtChat, chatting server
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppWtChat1.htm
//---------------------------------------------------------------------------
#include <iostream>
//---------------------------------------------------------------------------
#include <boost/program_options.hpp>
//---------------------------------------------------------------------------
#include <Wt/WApplication>
#include <Wt/WContainerWidget>
#include <Wt/WEnvironment>
//---------------------------------------------------------------------------
#include "wtchat1dialog.h"
//---------------------------------------------------------------------------
struct WtApplication : public Wt::WApplication
{
  WtApplication(const Wt::WEnvironment& env)
    : Wt::WApplication(env),
    m_dialog(new WtChat1Dialog)
  {
    this->setTitle("WtChat1");
    this->useStyleSheet("wt.css");
    root()->addWidget(m_dialog);

  }
  private:
  WtChat1Dialog * const m_dialog;
};
//---------------------------------------------------------------------------
Wt::WApplication *createApplication(
  const Wt::WEnvironment& env)
{
  return new WtApplication(env);
}
//---------------------------------------------------------------------------
int main(int argc, char **argv)
{
  // Declare the supported options.
  boost::program_options::options_description d(
    "Allowed options for TestLed");
  std::string docroot;
  std::string http_address;
  std::string http_port;
  d.add_options()
      ("help",
        "produce this help message")
      ("docroot",
         boost::program_options::value<std::string>(&docroot)->default_value("."),
         "the docroot")
      ("http-address",
         boost::program_options::value<std::string>(&http_address)->default_value("0.0.0.0"),
         "the server's http address")
      ("http-port",
         boost::program_options::value<std::string>(&http_port)->default_value("8080"),
         "the server's http port")
      ;

  boost::program_options::variables_map m;
  boost::program_options::store(
    boost::program_options::parse_command_line(
      argc, argv, d), m);
  boost::program_options::notify(m);

  if (m.count("help"))
  {
    //Display the options_description
    std::cout << d << "\n";
    return 0;
  }

  //Create the arguments in std::string format
  std::vector<std::string> v(7);
  v[0] = argv[0];
  v[1] = "--docroot";
  v[2] = docroot;
  v[3] = "--http-address";
  v[4] = http_address;
  v[5] = "--http-port";
  v[6] = http_port;

  //Convert the arguments to char* format
  std::vector<char*> w(7);
  for (int i=0; i!=7; ++i) w[i] = &v[i][0];

  //Give Wt the modified parameters
  return WRun(w.size(), &w[0], &createApplication);
}
//---------------------------------------------------------------------------

 

 

 

 

 

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict