Freeciv21
Develop your civilization from humble roots to a global empire
chatline_common.cpp
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 
14 #include <QGlobalStatic>
15 #include <cstdarg>
16 
17 // utility
18 #include "fcintl.h"
19 #include "fcthread.h"
20 
21 // common
22 #include "featured_text.h"
23 #include "packets.h"
24 
25 // client
26 #include "chatline.h"
27 #include "chatline_common.h"
28 #include "client_main.h"
29 
30 Q_GLOBAL_STATIC(QMutex, ow_mutex);
31 
35 int send_chat(const char *message)
36 {
37  return dsend_packet_chat_msg_req(&client.conn, message);
38 }
39 
44 int send_chat_printf(const char *format, ...)
45 {
46  struct packet_chat_msg_req packet;
47  va_list args;
48  QByteArray ba;
49 
50  va_start(args, format);
51  auto str = QString::vasprintf(format, args);
52  va_end(args);
53 
54  ba = str.toLocal8Bit();
55  qstrncpy(packet.message, ba.data(), sizeof(packet.message));
56 
57  return send_packet_chat_msg_req(&client.conn, &packet);
58 }
59 
63 void fc_allocate_ow_mutex() { ow_mutex->lock(); }
64 
68 void fc_release_ow_mutex() { ow_mutex->unlock(); }
69 
74 
79 
84 void output_window_append(const struct ft_color color,
85  const char *featured_text)
86 {
87  char plain_text[MAX_LEN_MSG];
88  struct text_tag_list *tags;
89 
90  // Separate the text and the tags.
91  featured_text_to_plain_text(featured_text, plain_text, sizeof(plain_text),
92  &tags, false);
93 
94  if (ft_color_requested(color)) {
95  // A color is requested.
96  struct text_tag *ptag =
98 
99  if (ptag) {
100  // Prepends to the list, to avoid to overwrite inside colors.
101  text_tag_list_prepend(tags, ptag);
102  } else {
103  qCritical(
104  "Failed to create a color text tag (fg = %s, bg = %s).",
105  (nullptr != color.foreground ? color.foreground : "nullptr"),
106  (nullptr != color.background ? color.background : "nullptr"));
107  }
108  }
109 
111  real_output_window_append(plain_text, tags);
113  text_tag_list_destroy(tags);
114 }
115 
120 void output_window_vprintf(const struct ft_color color, const char *format,
121  va_list args)
122 {
123  char featured_text[MAX_LEN_MSG];
124 
125  fc_vsnprintf(featured_text, sizeof(featured_text), format, args);
126  output_window_append(color, featured_text);
127 }
128 
133 void output_window_printf(const struct ft_color color, const char *format,
134  ...)
135 {
136  va_list args;
137 
138  va_start(args, format);
139  output_window_vprintf(color, format, args);
140  va_end(args);
141 }
142 
146 void output_window_event(const char *plain_text,
147  const struct text_tag_list *tags)
148 {
150  real_output_window_append(plain_text, tags);
152 }
153 
157 void chat_welcome_message(bool gui_has_copying_mitem)
158 {
159  output_window_append(ftc_any, _("Freeciv21 is free software and you are "
160  "welcome to distribute copies of it "
161  "under certain conditions;"));
162  if (gui_has_copying_mitem) {
163  output_window_append(ftc_any, _("See the \"Copying\" item on the "
164  "Help menu."));
165  } else {
166  output_window_append(ftc_any, _("See COPYING file distributed with "
167  "this program."));
168  }
169  output_window_append(ftc_any, _("Now ... Go give 'em hell!"));
170 }
void real_output_window_append(const QString &astring, const text_tag_list *tags)
Appends the string to the chat output window.
Definition: chatline.cpp:881
int send_chat_printf(const char *format,...)
Send the message as a chat to the server.
void fc_release_ow_mutex()
Release output window mutex.
void fc_destroy_ow_mutex()
Destroy output window mutex.
void output_window_event(const char *plain_text, const struct text_tag_list *tags)
Add a line of text to the output ("chatline") window from server event.
int send_chat(const char *message)
Send the message as a chat to the server.
void output_window_append(const struct ft_color color, const char *featured_text)
Add a line of text to the output ("chatline") window, like puts() would do it in the console.
void fc_allocate_ow_mutex()
Allocate output window mutex.
void output_window_vprintf(const struct ft_color color, const char *format, va_list args)
Add a line of text to the output ("chatline") window.
void output_window_printf(const struct ft_color color, const char *format,...)
Add a line of text to the output ("chatline") window.
void fc_init_ow_mutex()
Initialize output window mutex.
void chat_welcome_message(bool gui_has_copying_mitem)
Standard welcome message.
Q_GLOBAL_STATIC(QMutex, ow_mutex)
struct civclient client
#define _(String)
Definition: fcintl.h:50
size_t featured_text_to_plain_text(const char *featured_text, char *plain_text, size_t plain_text_len, struct text_tag_list **tags, bool replace_link_text)
Separate the text from the text features.
struct text_tag * text_tag_new(enum text_tag_type tag_type, ft_offset_t start_offset, ft_offset_t stop_offset,...)
Returns a new text_tag or nullptr on error.
const struct ft_color ftc_any
#define FT_OFFSET_UNSET
Definition: featured_text.h:95
static bool ft_color_requested(const struct ft_color color)
@ TTT_COLOR
#define MAX_LEN_MSG
Definition: packets.h:37
struct connection conn
Definition: client_main.h:89
struct text_tag::@23::@25 color
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap)
Definition: support.cpp:512