add the time to all chat messages...

...and only govern the display of the timestamp by the preferences

also display the timestamp everywhere not only in-game (fixes bug
#10572: Making the timestamp feature better)
This commit is contained in:
Gunter Labes 2008-01-07 22:24:13 +00:00
parent da1de3871b
commit 77b313a3a2
16 changed files with 92 additions and 82 deletions

View file

@ -385,7 +385,8 @@ void ai_interface::diagnostic(const std::string& msg)
void ai_interface::log_message(const std::string& msg)
{
if(game_config::debug) {
info_.disp.add_chat_message("ai",info_.team_num,msg,game_display::MESSAGE_PUBLIC,false);
info_.disp.add_chat_message(time(NULL), "ai", info_. team_num, msg,
game_display::MESSAGE_PUBLIC, false);
}
}

View file

@ -191,7 +191,8 @@ namespace gui{
completion_list += " ";
completion_list += *it;
}
gui.add_chat_message("",0,completion_list,game_display::MESSAGE_PRIVATE,false);
gui.add_chat_message(time(NULL), "", 0, completion_list,
game_display::MESSAGE_PRIVATE, false);
}
box_->set_text(text);
}

View file

@ -1131,19 +1131,6 @@ void game_display::set_playing_team(size_t teamindex)
invalidate_game_status();
}
//! timestring() returns the current date as a string.
//! Uses preferences::clock_format() for formatting.
static std::string timestring ()
{
time_t now = time (NULL);
struct tm *lt = localtime(&now);
char buf[10];
strftime(buf,sizeof(buf),preferences::clock_format().c_str(),lt);
return buf;
}
void game_display::begin_game()
{
in_game_ = true;
@ -1159,8 +1146,9 @@ namespace {
const SDL_Color chat_message_bg = {0,0,0,140};
}
void game_display::add_chat_message(const std::string& speaker, int side,
const std::string& message, game_display::MESSAGE_TYPE type, bool bell)
void game_display::add_chat_message(const time_t& time, const std::string& speaker,
int side, const std::string& message, game_display::MESSAGE_TYPE type,
bool bell)
{
std::string sender = speaker;
if (speaker.find("whisper: ") == 0) {
@ -1233,9 +1221,7 @@ void game_display::add_chat_message(const std::string& speaker, int side,
// prepend message with timestamp
std::stringstream message_complete;
if (preferences::chat_timestamp()) {
message_complete << timestring() << " ";
}
message_complete << preferences::get_chat_timestamp(time);
message_complete << str.str();
const SDL_Rect rect = map_outside_area();

View file

@ -174,8 +174,8 @@ public:
const std::set<std::string>& observers() const { return observers_; }
enum MESSAGE_TYPE { MESSAGE_PUBLIC, MESSAGE_PRIVATE };
void add_chat_message(const std::string& speaker, int side,
const std::string& msg, MESSAGE_TYPE type, bool bell);
void add_chat_message(const time_t& time, const std::string& speaker,
int side, const std::string& msg, MESSAGE_TYPE type, bool bell);
void clear_chat_messages() { prune_chat_messages(true); }
void begin_game();

View file

@ -150,7 +150,8 @@ static void show_wml_errors()
msg << " (" << itor->second << ")";
}
screen->add_chat_message(caption, 0, msg.str(), game_display::MESSAGE_PUBLIC, false);
screen->add_chat_message(time(NULL), caption, 0, msg.str(),
game_display::MESSAGE_PUBLIC, false);
std::cerr << caption << ": " << msg.str() << '\n';
}
}

View file

@ -639,13 +639,18 @@ bool compress_saves()
return utils::string_bool(preferences::get("compress_saves"), true);
}
bool chat_timestamp()
{
std::string get_chat_timestamp(const time_t& t) {
if (chat_timestamping()) {
return lg::get_timestamp(t, clock_format()) + " ";
}
return "";
}
bool chat_timestamping() {
return utils::string_bool(preferences::get("chat_timestamp"), false);
}
void set_chat_timestamp(bool value)
{
void set_chat_timestamping(bool value) {
preferences::set("chat_timestamp", value ? "yes" : "no");
}

View file

@ -171,8 +171,9 @@ namespace preferences {
const std::string upload_id();
// Multiplayer functions
bool chat_timestamp();
void set_chat_timestamp(bool value);
std::string get_chat_timestamp(const time_t& t);
bool chat_timestamping();
void set_chat_timestamping(bool value);
int chat_lines();
void set_chat_lines(int lines);

View file

@ -288,7 +288,7 @@ preferences_dialog::preferences_dialog(display& disp, const config& game_cfg)
// Have the tooltip appear over the static "Chat lines" label, too.
chat_lines_label_.set_help_string(_("Set the amount of chat lines shown"));
chat_timestamp_button_.set_check(chat_timestamp());
chat_timestamp_button_.set_check(chat_timestamping());
chat_timestamp_button_.set_help_string(_("Add a timestamp to chat messages"));
gamma_button_.set_check(adjust_gamma());
@ -851,7 +851,7 @@ void preferences_dialog::process_event()
if (iconize_list_button_.pressed())
set_iconize_list(iconize_list_button_.checked());
if (chat_timestamp_button_.pressed())
set_chat_timestamp(chat_timestamp_button_.checked());
set_chat_timestamping(chat_timestamp_button_.checked());
if (friends_list_button_.pressed())
set_selection(FRIENDS_TAB);

View file

@ -105,10 +105,9 @@ bool logger::dont_log(log_domain const &domain) const
return severity_ > d.severity_;
}
std::string get_timestamp() {
time_t t = time(NULL);
std::string get_timestamp(const time_t& t, const std::string& format) {
char buf[100];
strftime(buf, 100, "%b %d %H:%M:%S ", localtime(&t));
strftime(buf, 100, format.c_str(), localtime(&t));
return buf;
}
@ -119,7 +118,7 @@ std::ostream &logger::operator()(log_domain const &domain, bool show_names) cons
return null_ostream;
else {
if (timestamp)
std::cerr << get_timestamp();
std::cerr << get_timestamp(time(NULL));
if (show_names)
std::cerr << name_ << ' ' << d.name_ << ": ";
return std::cerr;
@ -139,7 +138,7 @@ scope_logger::~scope_logger()
const int ticks = SDL_GetTicks() - ticks_;
--indent;
do_indent();
if (timestamp) output_ << get_timestamp();
if (timestamp) output_ << get_timestamp(time(NULL));
output_ << "END: " << str_ << " (took " << ticks << "ms)\n";
}

View file

@ -46,7 +46,7 @@ public:
};
void timestamps(bool);
std::string get_timestamp();
std::string get_timestamp(const time_t& t, const std::string& format="%b %d %H:%M:%S ");
extern logger err, warn, info, debug;
extern log_domain general, ai, config, display, engine, network, mp_server,

View file

@ -1786,9 +1786,11 @@ private:
chat_handler::do_speak(textbox_info_.box()->text(),textbox_info_.check() != NULL ? textbox_info_.check()->checked() : false);
}
void menu_handler::add_chat_message(const std::string& speaker, int side, const std::string& message, game_display::MESSAGE_TYPE type)
void menu_handler::add_chat_message(const time_t& time,
const std::string& speaker, int side, const std::string& message,
game_display::MESSAGE_TYPE type)
{
gui_->add_chat_message(speaker,side,message,type,false);
gui_->add_chat_message(time, speaker, side, message, type, false);
}
chat_handler::~chat_handler()
@ -1858,7 +1860,7 @@ private:
cwhisper["sender"] = preferences::login();
cwhisper["receiver"] = arg1;
data.add_child("whisper", cwhisper);
add_chat_message("whisper to " + cwhisper["receiver"], 0,
add_chat_message(time(NULL), "whisper to " + cwhisper["receiver"], 0,
cwhisper["message"], game_display::MESSAGE_PRIVATE);
network::send_data(data, 0, true);
} else if (cmd == "/help") {
@ -1868,15 +1870,15 @@ private:
const std::string& subcommand = arg2;
if (!have_command) {
add_chat_message("help", 0, help_chat_help);
add_chat_message(time(NULL), "help", 0, help_chat_help);
} else if (command == "whisper" || command == "msg") {
//! @todo /msg should be replaced by the used command.
add_chat_message("help", 0, _("Sends a private message. "
add_chat_message(time(NULL), "help", 0, _("Sends a private message. "
"You can't send messages to players that control "
"any side in a game. Usage: /msg <nick> <message>"));
} else if (command == "list") {
if (!have_subcommand) {
add_chat_message("help", 0,
add_chat_message(time(NULL), "help", 0,
_("Ignore messages from players on the ignore list"
" and highlight players on the friends list."
" Usage: /list <subcommand> [<argument>]"
@ -1884,35 +1886,35 @@ private:
" display, clear."
" Type /help list <subcommand> for more info."));
} else if (subcommand == "addfriend"){
add_chat_message("help", 0,
add_chat_message(time(NULL), "help", 0,
_("Add a nick to your friends list."
" Usage: /list addfriend <nick>"));
} else if (subcommand == "addignore"){
add_chat_message("help", 0 ,
add_chat_message(time(NULL), "help", 0 ,
_("Add a nick to your ignores list."
" Usage: /list ignore <nick>"));
} else if (subcommand == "remove") {
add_chat_message("help", 0,
add_chat_message(time(NULL), "help", 0,
_("Remove a nick from your ignores or friends list."
" Usage: /list remove <nick>"));
} else if (subcommand == "clear") {
add_chat_message("help", 0,
add_chat_message(time(NULL), "help", 0,
_("Clear your complete ignores and friends list."
" Usage: /list clear"));
} else if (subcommand == "display") {
add_chat_message("help", 0,
add_chat_message(time(NULL), "help", 0,
_("Show your ignores and friends list."
" Usage: /list display"));
} else {
add_chat_message("help", 0, _("Unknown subcommand."));
add_chat_message(time(NULL), "help", 0, _("Unknown subcommand."));
}
} else if (command == "emote" || command == "me") {
//! @todo /me should be replaced by the used command.
add_chat_message("help", 0,
add_chat_message(time(NULL), "help", 0,
_("Send an emotion or personal action in chat. "
"Usage: /me <message>"));
} else {
add_chat_message("help", 0, _("Unknown command."));
add_chat_message(time(NULL), "help", 0, _("Unknown command."));
}
} else if (cmd == "/list" && argc > 0) {
if (arg1 == "addignore") {
@ -1920,40 +1922,40 @@ private:
(preferences::add_ignore(arg2)
? _("Added to ignore list: ") : _("Invalid username: "))
+ arg2;
add_chat_message("ignores list", 0, msg);
add_chat_message(time(NULL), "ignores list", 0, msg);
} else if (arg1 == "addfriend") {
const std::string msg =
(preferences::add_friend(arg2)
? _("Added to friends list: ") : _("Invalid username: "))
+ arg2;
add_chat_message("friends list", 0, msg);
add_chat_message(time(NULL), "friends list", 0, msg);
} else if (arg1 == "remove") {
preferences::remove_friend(arg2);
preferences::remove_ignore(arg2);
add_chat_message("list", 0, _("Removed from list: ") + arg2,
game_display::MESSAGE_PRIVATE);
add_chat_message(time(NULL), "list", 0, _("Removed from list: ")
+ arg2, game_display::MESSAGE_PRIVATE);
} else if (arg1 == "display") {
const std::string& text_friend = preferences::get_friends();
const std::string& text_ignore = preferences::get_ignores();
if (!text_friend.empty()) {
add_chat_message("friends list", 0, text_friend);
add_chat_message(time(NULL), "friends list", 0, text_friend);
}
if (!text_ignore.empty()) {
add_chat_message("ignores list", 0, text_ignore);
add_chat_message(time(NULL), "ignores list", 0, text_ignore);
} else if (text_friend.empty()) {
add_chat_message("list", 0,
add_chat_message(time(NULL), "list", 0,
_("There are no players on your friends or ignore list."));
}
} else if (arg1 == "clear") {
preferences::clear_friends();
preferences::clear_ignores();
} else {
add_chat_message("list", 0, _("Unknown command: ") + arg1);
add_chat_message(time(NULL), "list", 0, _("Unknown command: ") + arg1);
}
} else {
//! @todo Rather show specific error messages for missing arguments.
// Command not accepted, show help.
add_chat_message("help", 0, help_chat_help);
add_chat_message(time(NULL), "help", 0, help_chat_help);
}
}
@ -1979,8 +1981,8 @@ private:
}
recorder.speak(cfg);
add_chat_message(cfg["description"],side,message,
private_message ? game_display::MESSAGE_PRIVATE : game_display::MESSAGE_PUBLIC);
add_chat_message(time(NULL), cfg["description"], side, message,
private_message ? game_display::MESSAGE_PRIVATE : game_display::MESSAGE_PUBLIC);
}

View file

@ -61,7 +61,9 @@ protected:
void do_speak(const std::string& message, bool allies_only=false);
//called from do_speak
virtual void add_chat_message(const std::string& speaker, int side, const std::string& message, game_display::MESSAGE_TYPE type=game_display::MESSAGE_PRIVATE)=0;
virtual void add_chat_message(const time_t& time,
const std::string& speaker, int side, const std::string& message,
game_display::MESSAGE_TYPE type=game_display::MESSAGE_PRIVATE)=0;
virtual void send_chat_message(const std::string& message, bool allies_only=false)=0;
void send_command(const std::string& cmd, const std::string& args="");
};
@ -130,7 +132,9 @@ public:
void autosave(const std::string &label, unsigned turn, const config &starting_pos) const;
bool has_team() const;
protected:
void add_chat_message(const std::string& speaker, int side, const std::string& message, game_display::MESSAGE_TYPE type=game_display::MESSAGE_PRIVATE);
void add_chat_message(const time_t& time, const std::string& speaker,
int side, const std::string& message,
game_display::MESSAGE_TYPE type=game_display::MESSAGE_PRIVATE);
void send_chat_message(const std::string& message, bool allies_only=false);
private:
//void do_speak(const std::string& message, bool allies_only);

View file

@ -176,9 +176,10 @@ chat::chat()
{
}
void chat::add_message(const std::string& user, const std::string& message)
void chat::add_message(const time_t& time, const std::string& user,
const std::string& message)
{
message_history_.push_back(msg(user, message));
message_history_.push_back(msg(time, user, message));
while (message_history_.size() > 1024) {
message_history_.pop_front();
@ -219,9 +220,11 @@ void chat::update_textbox(gui::textbox& textbox)
std::string chat::format_message(const msg& message)
{
if(message.message.substr(0,3) == "/me") {
return "<" + message.user + message.message.substr(3) + ">\n";
return preferences::get_chat_timestamp(message.time) + "<" + message.user
+ message.message.substr(3) + ">\n";
} else {
return "<" + message.user + ">" + message.message + "\n";
return preferences::get_chat_timestamp(message.time) + "<" + message.user
+ ">" + message.message + "\n";
}
}
@ -391,9 +394,9 @@ void ui::handle_event(const SDL_Event& event)
}
}
void ui::add_chat_message(const std::string& speaker, int /*side*/, const std::string& message, game_display::MESSAGE_TYPE /*type*/)
void ui::add_chat_message(const time_t& time, const std::string& speaker, int /*side*/, const std::string& message, game_display::MESSAGE_TYPE /*type*/)
{
chat_.add_message(speaker,message);
chat_.add_message(time, speaker, message);
chat_.update_textbox(chat_textbox_);
}
@ -404,7 +407,7 @@ void ui::send_chat_message(const std::string& message, bool /*allies_only*/)
msg["sender"] = preferences::login();
data.add_child("message", msg);
add_chat_message(preferences::login(),0, message); //local echo
add_chat_message(time(NULL), preferences::login(),0, message); //local echo
network::send_data(data, 0, true);
}
@ -468,7 +471,7 @@ void ui::handle_key_event(const SDL_KeyboardEvent& event)
completion_list += " ";
completion_list += *it;
}
chat_.add_message("",completion_list);
chat_.add_message(time(NULL), "", completion_list);
chat_.update_textbox(chat_textbox_);
}
entry_textbox_.set_text(text);
@ -493,7 +496,8 @@ void ui::process_message(const config& msg, const bool whisper) {
} else {
sound::play_UI_sound(game_config::sounds::receive_message);
}
chat_.add_message((whisper ? "whisper: " : "") + msg["sender"], msg["message"]);
chat_.add_message(time(NULL), (whisper ? "whisper: " : "") + msg["sender"],
msg["message"]);
chat_.update_textbox(chat_textbox_);
}

View file

@ -45,15 +45,17 @@ class chat
public:
chat();
void add_message(const std::string& user, const std::string& message);
void add_message(const time_t& time, const std::string& user,
const std::string& message);
void init_textbox(gui::textbox& textbox);
void update_textbox(gui::textbox& textbox);
private:
struct msg {
msg(const std::string& user, const std::string& message) :
user(user), message(message) {};
msg(const time_t& time, const std::string& user, const std::string& message)
: time(time), user(user), message(message) {};
time_t time;
std::string user;
std::string message;
};
@ -114,7 +116,9 @@ protected:
virtual void handle_key_event(const SDL_KeyboardEvent& event);
// Override chat_handler
void add_chat_message(const std::string& speaker, int side, const std::string& message, game_display::MESSAGE_TYPE type=game_display::MESSAGE_PRIVATE);
void add_chat_message(const time_t& time, const std::string& speaker,
int side, const std::string& message,
game_display::MESSAGE_TYPE type=game_display::MESSAGE_PRIVATE);
void send_chat_message(const std::string& message, bool allies_only=false);
//! Process chat messages.

View file

@ -75,13 +75,13 @@ turn_info::PROCESS_DATA_RESULT turn_info::process_network_data(const config& cfg
{
if (cfg.child("message")) {
const config& cmessage = *cfg.child("message");
gui_.add_chat_message(cmessage["sender"], 0,
gui_.add_chat_message(time(NULL), cmessage["sender"], 0,
cmessage["message"], game_display::MESSAGE_PUBLIC,
preferences::message_bell());
}
if (cfg.child("whisper") != NULL /*&& is_observer()*/) {
const config& cwhisper = *cfg.child("whisper");
gui_.add_chat_message("whisper: " + cwhisper["sender"], 0,
gui_.add_chat_message(time(NULL), "whisper: " + cwhisper["sender"], 0,
cwhisper["message"], game_display::MESSAGE_PRIVATE,
preferences::message_bell());
}

View file

@ -666,13 +666,15 @@ static void check_checksums(game_display& disp,const unit_map& units,const confi
if(u == units.end()) {
std::stringstream message;
message << "non existant unit to checksum at " << loc.x+1 << "," << loc.y+1 << "!";
disp.add_chat_message("verification",1,message.str(),game_display::MESSAGE_PRIVATE,false);
disp.add_chat_message(time(NULL), "verification", 1, message.str(),
game_display::MESSAGE_PRIVATE, false);
continue;
}
if(get_checksum(u->second) != (**ci)["value"]) {
std::stringstream message;
message << "checksum mismatch at " << loc.x+1 << "," << loc.y+1 << "!";
disp.add_chat_message("verification",1,message.str(),game_display::MESSAGE_PRIVATE,false);
disp.add_chat_message(time(NULL), "verification", 1, message.str(),
game_display::MESSAGE_PRIVATE, false);
}
}
}
@ -757,7 +759,7 @@ bool do_replay(game_display& disp, const gamemap& map, const game_data& gameinfo
bool is_whisper = (speaker_name.find("whisper: ") == 0);
if (!replayer.is_skipping() || is_whisper) {
const int side = lexical_cast_default<int>((*child)["side"].c_str(),0);
disp.add_chat_message(speaker_name,side,message,
disp.add_chat_message(time(NULL), speaker_name, side, message,
(team_name == "" ? game_display::MESSAGE_PUBLIC
: game_display::MESSAGE_PRIVATE),
preferences::message_bell());