From 2e0ac6f3d748b252ff78bd85de5da68bda64528c Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Tue, 3 Oct 2023 01:21:15 +0300
Subject: [PATCH 51/51] Qt: Fix UTF8 handling

See osdn #48795

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 client/gui-qt/chatline.cpp   |  6 +++---
 client/gui-qt/citydlg.cpp    | 10 +++++-----
 client/gui-qt/cityrep.cpp    |  4 ++--
 client/gui-qt/dialogs.cpp    |  8 ++++----
 client/gui-qt/fc_client.cpp  | 16 ++++++++--------
 client/gui-qt/fonts.cpp      |  6 +++---
 client/gui-qt/gui_main.cpp   |  2 +-
 client/gui-qt/hudwidget.cpp  |  2 +-
 client/gui-qt/luaconsole.cpp |  4 ++--
 client/gui-qt/mapctrl.cpp    |  2 +-
 client/gui-qt/menu.cpp       |  6 +++---
 client/gui-qt/pages.cpp      | 14 +++++++-------
 client/gui-qt/sidebar.cpp    |  2 +-
 client/gui-qt/themes.cpp     |  6 +++---
 14 files changed, 44 insertions(+), 44 deletions(-)

diff --git a/client/gui-qt/chatline.cpp b/client/gui-qt/chatline.cpp
index 02783f1fd9..58a43bef9e 100644
--- a/client/gui-qt/chatline.cpp
+++ b/client/gui-qt/chatline.cpp
@@ -144,7 +144,7 @@ void chat_listener::send_chat_message(const QString &message)
 
       if (!splayer.compare(s)) {
         if (is_ai(pplayer)) {
-          send_chat(message.toLocal8Bit());
+          send_chat(message.toUtf8());
           send_chat("/away");
           return;
         }
@@ -157,9 +157,9 @@ void chat_listener::send_chat_message(const QString &message)
     if (client_state() >= C_S_RUNNING && gui_options.gui_qt_allied_chat_only
         && is_plain_public_message(message)) {
       send_chat((QString(CHAT_ALLIES_PREFIX)
-                 + " " + message).toLocal8Bit());
+                 + " " + message).toUtf8());
     } else {
-      send_chat(message.toLocal8Bit());
+      send_chat(message.toUtf8());
     }
   }
   // Empty messages aren't sent
diff --git a/client/gui-qt/citydlg.cpp b/client/gui-qt/citydlg.cpp
index dd53ae94b5..f283baa4d6 100644
--- a/client/gui-qt/citydlg.cpp
+++ b/client/gui-qt/citydlg.cpp
@@ -2308,7 +2308,7 @@ void city_dialog::city_rename()
       return;
     }
 
-    ask_bytes = ask->input_edit.text().toLocal8Bit();
+    ask_bytes = ask->input_edit.text().toUtf8();
     ::city_rename(pcity, ask_bytes.data());
   });
   ask->show();
@@ -2354,7 +2354,7 @@ void city_dialog::save_cma()
   connect(ask, &hud_message_box::accepted, this,
           CAPTURE_DEFAULT_THIS () {
     struct cm_parameter param;
-    QByteArray ask_bytes = ask->input_edit.text().toLocal8Bit();
+    QByteArray ask_bytes = ask->input_edit.text().toUtf8();
     QString text = ask_bytes.data();
     if (!text.isEmpty()) {
       param.allow_disorder = false;
@@ -2368,7 +2368,7 @@ void city_dialog::save_cma()
         param.factor[i] = slider_tab[2 * i + 1]->value();
       }
 
-      ask_bytes = text.toLocal8Bit();
+      ask_bytes = text.toUtf8();
       cmafec_preset_add(ask_bytes.data(), &param);
       update_cma_tab();
     }
@@ -3638,10 +3638,10 @@ void city_dialog::save_worklist()
     QString text;
     struct city *pcity = game_city_by_number(city_id);
 
-    ask_bytes = ask->input_edit.text().toLocal8Bit();
+    ask_bytes = ask->input_edit.text().toUtf8();
     text = ask_bytes.data();
     if (!text.isEmpty()) {
-      ask_bytes = text.toLocal8Bit();
+      ask_bytes = text.toUtf8();
       gw = global_worklist_new(ask_bytes.data());
       city_get_queue(pcity, &queue);
       global_worklist_set(gw, &queue);
diff --git a/client/gui-qt/cityrep.cpp b/client/gui-qt/cityrep.cpp
index 02b7ae9ab2..0abbf32b0d 100644
--- a/client/gui-qt/cityrep.cpp
+++ b/client/gui-qt/cityrep.cpp
@@ -43,8 +43,8 @@ bool city_sort_model::lessThan(const QModelIndex &left,
 
   qleft = sourceModel()->data(left);
   qright = sourceModel()->data(right);
-  l_bytes = qleft.toString().toLocal8Bit();
-  r_bytes = qright.toString().toLocal8Bit();
+  l_bytes = qleft.toString().toUtf8();
+  r_bytes = qright.toString().toUtf8();
   i = cityrepfield_compare(l_bytes.data(), r_bytes.data());
 
   if (i >= 0) {
diff --git a/client/gui-qt/dialogs.cpp b/client/gui-qt/dialogs.cpp
index 7a03bbc253..ddb8776e56 100644
--- a/client/gui-qt/dialogs.cpp
+++ b/client/gui-qt/dialogs.cpp
@@ -923,9 +923,9 @@ void notify_dialog::restart()
       q = q + QChar('\n');
     }
   }
-  capt_bytes = qcaption.toLocal8Bit();
-  hl_bytes = qheadline.toLocal8Bit();
-  qb_bytes = q.toLocal8Bit();
+  capt_bytes = qcaption.toUtf8();
+  hl_bytes = qheadline.toUtf8();
+  qb_bytes = q.toUtf8();
   popup_notify_dialog(capt_bytes.data(),
                       hl_bytes.data(),
                       qb_bytes.data());
@@ -1173,7 +1173,7 @@ void races_dialog::nationset_changed(int index)
   struct option *poption = optset_option_by_name(server_optset, "nationset");
 
   rule_name = qnation_set->currentData().toString();
-  rn_bytes = rule_name.toLocal8Bit(); /* Hold QByteArray in a variable to
+  rn_bytes = rule_name.toUtf8(); /* Hold QByteArray in a variable to
                                        * extend its, and data() buffer's,
                                        * lifetime */
   rn = rn_bytes.data();
diff --git a/client/gui-qt/fc_client.cpp b/client/gui-qt/fc_client.cpp
index 1bb036483e..8429a7281a 100644
--- a/client/gui-qt/fc_client.cpp
+++ b/client/gui-qt/fc_client.cpp
@@ -1025,16 +1025,16 @@ QIcon fc_icons::get_icon(const QString &id)
 
   str = QString("themes") + DIR_SEPARATOR + "gui-qt" + DIR_SEPARATOR;
   // Try custom icon from theme
-  pn_bytes = str.toLocal8Bit();
+  pn_bytes = str.toUtf8();
   png_bytes = QString(pn_bytes.data() + current_theme + DIR_SEPARATOR
-                      + id + ".png").toLocal8Bit();
+                      + id + ".png").toUtf8();
   icon.addFile(fileinfoname(get_data_dirs(),
                             png_bytes.data()));
   str = str + "icons" + DIR_SEPARATOR;
   // Try icon from icons dir
   if (icon.isNull()) {
-    pn_bytes = str.toLocal8Bit();
-    png_bytes = QString(pn_bytes.data() + id + ".png").toLocal8Bit();
+    pn_bytes = str.toUtf8();
+    png_bytes = QString(pn_bytes.data() + id + ".png").toUtf8();
     icon.addFile(fileinfoname(get_data_dirs(),
                               png_bytes.data()));
   }
@@ -1058,13 +1058,13 @@ QPixmap *fc_icons::get_pixmap(const QString &id)
   }
   str = QString("themes") + DIR_SEPARATOR + "gui-qt" + DIR_SEPARATOR;
   png_bytes = QString(str + current_theme + DIR_SEPARATOR
-                      + id + ".png").toLocal8Bit();
+                      + id + ".png").toUtf8();
   status = pm->load(fileinfoname(get_data_dirs(),
                                  png_bytes.data()));
 
   if (!status) {
     str = str + "icons" + DIR_SEPARATOR;
-    png_bytes = QString(str + id + ".png").toLocal8Bit();
+    png_bytes = QString(str + id + ".png").toUtf8();
     pm->load(fileinfoname(get_data_dirs(), png_bytes.data()));
   }
   QPixmapCache::insert(id, *pm);
@@ -1082,7 +1082,7 @@ QString fc_icons::get_path(const QString &id)
 
   str = QString("themes") + DIR_SEPARATOR + "gui-qt"
         + DIR_SEPARATOR + "icons" + DIR_SEPARATOR;
-  png_bytes = QString(str + id + ".png").toLocal8Bit();
+  png_bytes = QString(str + id + ".png").toUtf8();
 
   return fileinfoname(get_data_dirs(),
                       png_bytes.data());
@@ -1335,7 +1335,7 @@ void pregame_options::ruleset_change(int i)
   if (!cruleset->currentText().isEmpty()) {
     QByteArray rn_bytes;
 
-    rn_bytes = cruleset->currentText().toLocal8Bit();
+    rn_bytes = cruleset->currentText().toUtf8();
     set_ruleset(rn_bytes.data());
   }
 }
diff --git a/client/gui-qt/fonts.cpp b/client/gui-qt/fonts.cpp
index 4e7054a1df..21b62d3f66 100644
--- a/client/gui-qt/fonts.cpp
+++ b/client/gui-qt/fonts.cpp
@@ -137,13 +137,13 @@ static void configure_single(QString role, QStringList sl, int size,
   if (font_name.isEmpty()) {
     QByteArray fn_bytes;
 
-    fn_bytes = role.toLocal8Bit();
+    fn_bytes = role.toUtf8();
     log_error(_("Failed to setup font for role %s."),
               fn_bytes.data());
   } else {
     QByteArray fn_bytes;
 
-    fn_bytes = font_name.toLocal8Bit();
+    fn_bytes = font_name.toUtf8();
     fc_strlcpy(font_opt, fn_bytes.data(), FONT_NAME_SIZE);
   }
 }
@@ -263,7 +263,7 @@ QString configure_font(QString font_name, QStringList sl, int size,
         f->setBold(true);
       }
       fc_font::instance()->set_font(font_name, f);
-      fn_bytes = f->toString().toLocal8Bit();
+      fn_bytes = f->toString().toUtf8();
 
       return fn_bytes.data();
     }
diff --git a/client/gui-qt/gui_main.cpp b/client/gui-qt/gui_main.cpp
index 8b803da0b9..8833b813ab 100644
--- a/client/gui-qt/gui_main.cpp
+++ b/client/gui-qt/gui_main.cpp
@@ -588,7 +588,7 @@ void qtg_insert_client_build_info(char *outbuf, size_t outlen)
 {
   // There's also an separate entry about Qt in help menu.
 
-  QByteArray ver = QLibraryInfo::version().toString().toLocal8Bit();
+  QByteArray ver = QLibraryInfo::version().toString().toUtf8();
 
   cat_snprintf(outbuf, outlen, _("\nBuilt against Qt %s, using %s"),
                QT_VERSION_STR, ver.data());
diff --git a/client/gui-qt/hudwidget.cpp b/client/gui-qt/hudwidget.cpp
index 959230b1a9..9f813cafce 100644
--- a/client/gui-qt/hudwidget.cpp
+++ b/client/gui-qt/hudwidget.cpp
@@ -655,7 +655,7 @@ void hud_units::update_actions(unit_list *punits)
   } else if (num > 1) {
     QByteArray ut_bytes;
 
-    ut_bytes = snum.toLocal8Bit();
+    ut_bytes = snum.toUtf8();
     // TRANS: preserve leading space
     text_str = text_str + QString(PL_(" +%1 unit",
                                       " +%1 units", num-1))
diff --git a/client/gui-qt/luaconsole.cpp b/client/gui-qt/luaconsole.cpp
index c1b6ac7810..93fb0eb049 100644
--- a/client/gui-qt/luaconsole.cpp
+++ b/client/gui-qt/luaconsole.cpp
@@ -79,7 +79,7 @@ void qload_lua_script()
                                               _("Load lua script"),
                                               QDir::homePath(), str);
   if (!qlua_filename.isEmpty()) {
-    script_client_do_file(qlua_filename.toLocal8Bit().constData());
+    script_client_do_file(qlua_filename.toUtf8().constData());
   }
 }
 
@@ -89,6 +89,6 @@ void qload_lua_script()
 void qreload_lua_script()
 {
   if (!qlua_filename.isEmpty()) {
-    script_client_do_file(qlua_filename.toLocal8Bit().constData());
+    script_client_do_file(qlua_filename.toUtf8().constData());
   }
 }
diff --git a/client/gui-qt/mapctrl.cpp b/client/gui-qt/mapctrl.cpp
index d78a28af51..0be65c9073 100644
--- a/client/gui-qt/mapctrl.cpp
+++ b/client/gui-qt/mapctrl.cpp
@@ -63,7 +63,7 @@ void popup_newcity_dialog(struct unit *punit, const char *suggestname)
     if (result == QDialog::Accepted) {
       QByteArray ask_bytes;
 
-      ask_bytes = ask->input_edit.text().toLocal8Bit();
+      ask_bytes = ask->input_edit.text().toUtf8();
       finish_city(index_to_tile(&(wld.map), index),
                   ask_bytes.data());
     } else {
diff --git a/client/gui-qt/menu.cpp b/client/gui-qt/menu.cpp
index 1253c37c80..4c918547f9 100644
--- a/client/gui-qt/menu.cpp
+++ b/client/gui-qt/menu.cpp
@@ -3635,7 +3635,7 @@ void mr_menu::tileset_custom_load()
   foreach (s, sl) {
     QByteArray on_bytes;
 
-    on_bytes = s.toLocal8Bit();
+    on_bytes = s.toUtf8();
     poption = optset_option_by_name(client_optset, on_bytes.data());
     tlset_list = get_tileset_list(poption);
     strvec_iterate(tlset_list, value) {
@@ -3658,7 +3658,7 @@ void mr_menu::load_new_tileset()
   QByteArray tn_bytes;
 
   but = qobject_cast<QPushButton *>(sender());
-  tn_bytes = but->text().toLocal8Bit();
+  tn_bytes = but->text().toUtf8();
   tilespec_reread(tn_bytes.data(), true, 1.0f);
   gui()->map_scale = 1.0f;
   but->parentWidget()->close();
@@ -3858,7 +3858,7 @@ void mr_menu::save_game_as()
   if (!current_file.isEmpty()) {
     QByteArray cf_bytes;
 
-    cf_bytes = current_file.toLocal8Bit();
+    cf_bytes = current_file.toUtf8();
     send_save_game(cf_bytes.data());
   }
 }
diff --git a/client/gui-qt/pages.cpp b/client/gui-qt/pages.cpp
index 830c4a338d..f2074480d8 100644
--- a/client/gui-qt/pages.cpp
+++ b/client/gui-qt/pages.cpp
@@ -1107,7 +1107,7 @@ void fc_client::start_scenario()
   if (is_server_running() && !current_file.isEmpty()) {
     QByteArray c_bytes;
 
-    c_bytes = current_file.toLocal8Bit();
+    c_bytes = current_file.toUtf8();
     send_chat_printf("/load %s", c_bytes.data());
     switch_page(PAGE_GAME + 1);
   }
@@ -1125,7 +1125,7 @@ void fc_client::start_from_save()
   if (is_server_running() && !current_file.isEmpty()) {
     QByteArray c_bytes;
 
-    c_bytes = current_file.toLocal8Bit();
+    c_bytes = current_file.toUtf8();
     send_chat_printf("/load %s", c_bytes.data());
     switch_page(PAGE_GAME + 1);
   }
@@ -1232,7 +1232,7 @@ void fc_client::slot_selection_changed(const QItemSelection &selected,
       load_save_text->setText("");
       break;
     }
-    fn_bytes = current_file.toLocal8Bit();
+    fn_bytes = current_file.toUtf8();
     if ((sf = secfile_load_section(fn_bytes.data(),
                                    "game", TRUE))) {
       const char *sname;
@@ -1259,7 +1259,7 @@ void fc_client::slot_selection_changed(const QItemSelection &selected,
       }
       for (int i = 0; i < num_players; i++) {
         pl_str = QString("player") + QString::number(i);
-        pl_bytes = pl_str.toLocal8Bit();
+        pl_bytes = pl_str.toUtf8();
         if ((sf = secfile_load_section(fn_bytes.data(),
                                        pl_bytes.data(), true))) {
           if (!secfile_lookup_bool_default(sf, true,
@@ -1277,7 +1277,7 @@ void fc_client::slot_selection_changed(const QItemSelection &selected,
       }
 
       // Information about human player
-      pl_bytes = pl_str.toLocal8Bit();
+      pl_bytes = pl_str.toUtf8();
       if ((sf = secfile_load_section(fn_bytes.data(),
                                      pl_bytes.data(), true))) {
         sname = secfile_lookup_str_default(sf, nullptr, "player%d.nation",
@@ -1608,9 +1608,9 @@ void fc_client::slot_connect()
 
   switch (connection_status) {
   case LOGIN_TYPE:
-    ba_bytes = connect_login_edit->text().toLocal8Bit();
+    ba_bytes = connect_login_edit->text().toUtf8();
     sz_strlcpy(user_name, ba_bytes.data());
-    ba_bytes = connect_host_edit->text().toLocal8Bit();
+    ba_bytes = connect_host_edit->text().toUtf8();
     sz_strlcpy(server_host, ba_bytes.data());
     server_port = connect_port_edit->text().toInt();
 
diff --git a/client/gui-qt/sidebar.cpp b/client/gui-qt/sidebar.cpp
index dd3014b41f..7aba06aa30 100644
--- a/client/gui-qt/sidebar.cpp
+++ b/client/gui-qt/sidebar.cpp
@@ -382,7 +382,7 @@ void fc_sidewidget::some_slot()
     QByteArray cn_bytes;
 
     s = QString("/observe \"%1\"").arg(obs_player->name);
-    cn_bytes = s.toLocal8Bit();
+    cn_bytes = s.toUtf8();
     send_chat(cn_bytes.data());
   }
 }
diff --git a/client/gui-qt/themes.cpp b/client/gui-qt/themes.cpp
index 6773f81884..1a5e51c311 100644
--- a/client/gui-qt/themes.cpp
+++ b/client/gui-qt/themes.cpp
@@ -178,9 +178,9 @@ char **qtg_get_useable_themes_in_directory(const char *directory, int *count)
   for (int i = 0; i < *count; i++) {
     QByteArray tn_bytes;
 
-    qba = theme_list[i].toLocal8Bit();
-    data = new char[theme_list[i].toLocal8Bit().length() + 1];
-    tn_bytes = theme_list[i].toLocal8Bit();
+    qba = theme_list[i].toUtf8();
+    data = new char[theme_list[i].toUtf8().length() + 1];
+    tn_bytes = theme_list[i].toUtf8();
     strcpy(data, tn_bytes.data());
     array[i] = data;
   }
-- 
2.40.1