Use fmt for string formatting (GTK client) (#3967)

This commit is contained in:
Mike Gelfand 2022-10-15 09:13:50 -07:00 committed by GitHub
parent 79068c512a
commit ae2dd5eba9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 70 additions and 79 deletions

View File

@ -22,6 +22,9 @@
#include <giomm.h>
#include <glib/gmessages.h>
#ifdef G_OS_UNIX
#include <glib-unix.h>
#endif
#include <glibmm/i18n.h>
#include <libtransmission/transmission.h>
@ -524,23 +527,17 @@ tr_rpc_callback_status Application::Impl::on_rpc_changed(
namespace
{
sig_atomic_t global_sigcount = 0;
gpointer sighandler_cbdata = nullptr;
#ifdef G_OS_UNIX
void signal_handler(int sig)
gboolean signal_handler(gpointer user_data)
{
if (++global_sigcount > 1)
{
signal(sig, SIG_DFL);
raise(sig);
}
else if (sig == SIGINT || sig == SIGTERM)
{
g_message(_("Got signal %d; trying to shut down cleanly. Do it again if it gets stuck."), sig);
gtr_actions_handler("quit", sighandler_cbdata);
}
g_message(_("Got termination signal, trying to shut down cleanly. Do it again if it gets stuck."));
gtr_actions_handler("quit", user_data);
return G_SOURCE_REMOVE;
}
#endif
} // namespace
/****
@ -574,10 +571,10 @@ void Application::Impl::on_startup()
tr_session* session;
::signal(SIGINT, signal_handler);
::signal(SIGTERM, signal_handler);
sighandler_cbdata = this;
#ifdef G_OS_UNIX
g_unix_signal_add(SIGINT, &signal_handler, this);
g_unix_signal_add(SIGTERM, &signal_handler, this);
#endif
/* ensure the directories are created */
if (auto const str = gtr_pref_string_get(TR_KEY_download_dir); !str.empty())
@ -1385,14 +1382,12 @@ void Application::Impl::update_model_soon()
bool Application::Impl::update_model_loop()
{
bool const done = global_sigcount != 0;
if (!done)
if (!is_closing_)
{
update_model_once();
}
return !done;
return !is_closing_;
}
void Application::Impl::show_about_dialog()
@ -1673,7 +1668,7 @@ void Application::Impl::actions_handler(Glib::ustring const& action_name)
}
else
{
g_error("Unhandled action: %s", action_name.c_str());
g_error("%s", fmt::format("Unhandled action: {}", action_name).c_str());
}
if (changed)

View File

@ -8,6 +8,8 @@
#include <glibmm.h>
#include <glibmm/i18n.h>
#include <fmt/core.h>
#include <libtransmission/transmission.h>
#include "Dialogs.h"
@ -49,11 +51,11 @@ void gtr_confirm_remove(
}
}
auto const primary_text = gtr_sprintf(
auto const primary_text = fmt::format(
!delete_files ?
ngettext("Remove torrent?", "Remove %d torrents?", count) :
ngettext("Delete this torrent's downloaded files?", "Delete these %d torrents' downloaded files?", count),
count);
ngettext("Remove torrent?", "Remove {count:L} torrents?", count) :
ngettext("Delete this torrent's downloaded files?", "Delete these {count:L} torrents' downloaded files?", count),
fmt::arg("count", count));
Glib::ustring secondary_text;
if (incomplete == 0 && connected == 0)
@ -100,7 +102,7 @@ void gtr_confirm_remove(
auto d = std::make_shared<Gtk::MessageDialog>(
parent,
gtr_sprintf("<big><b>%s</b></big>", primary_text),
fmt::format("<big><b>{}</b></big>", primary_text),
true /*use_markup*/,
TR_GTK_MESSAGE_TYPE(WARNING),
TR_GTK_BUTTONS_TYPE(NONE),

View File

@ -10,6 +10,8 @@
#include <glib/gstdio.h> /* g_remove() */
#include <fmt/core.h>
#include <libtransmission/transmission.h>
#include <libtransmission/web.h> // tr_sessionFetch()
#include <libtransmission/web-utils.h>
@ -33,7 +35,7 @@ struct favicon_data
Glib::ustring get_url(std::string const& host, size_t image_type)
{
return gtr_sprintf("http://%s/favicon.%s", host, image_types[image_type]);
return fmt::format("http://{}/favicon.{}", host, image_types[image_type]);
}
std::string favicon_get_cache_dir()

View File

@ -12,6 +12,8 @@
#include <glibmm.h>
#include <glibmm/i18n.h>
#include <fmt/core.h>
#include "FaviconCache.h" // gtr_get_favicon()
#include "FilterBar.h"
#include "HigWorkarea.h" // GUI_PAD
@ -323,7 +325,7 @@ void render_pixbuf_func(Gtk::CellRendererPixbuf* cell_renderer, Gtk::TreeModel::
void render_number_func(Gtk::CellRendererText* cell_renderer, Gtk::TreeModel::const_iterator const& iter)
{
auto const count = iter->get_value(tracker_filter_cols.count);
cell_renderer->property_text() = count >= 0 ? gtr_sprintf("%'d", count) : "";
cell_renderer->property_text() = count >= 0 ? fmt::format("{:L}", count) : "";
}
Gtk::CellRendererText* number_renderer_new()

View File

@ -208,7 +208,7 @@ void MessageLogWindow::Impl::doSave(Gtk::Window& parent, Glib::ustring const& fi
auto const it = level_names_.find(node->level);
auto const* const level_str = it != std::end(level_names_) ? it->second : "???";
fprintf(fp, "%s\t%s\t%s\t%s\n", date.c_str(), level_str, node->name.c_str(), node->message.c_str());
fmt::print(fp, "{}\t{}\t{}\t{}\n", date, level_str, node->name, node->message);
}
fclose(fp);

View File

@ -10,6 +10,8 @@
#include <giomm.h>
#include <glibmm/i18n.h>
#include <fmt/core.h>
#include "Notify.h"
#include "Prefs.h"
#include "PrefsDialog.h"
@ -123,7 +125,7 @@ void dbus_proxy_ready_callback(Glib::RefPtr<Gio::AsyncResult>& res)
if (proxy == nullptr)
{
g_warning("Failed to create proxy for %s", NotificationsDbusName.c_str());
g_warning("%s", fmt::format(_("Couldn't create proxy for '{bus}'"), fmt::arg("bus", NotificationsDbusName)).c_str());
return;
}

View File

@ -733,7 +733,7 @@ void RemotePage::refreshRPCSensitivity()
void onLaunchClutchCB()
{
gtr_open_uri(gtr_sprintf("http://localhost:%d/", (int)gtr_pref_int_get(TR_KEY_rpc_port)));
gtr_open_uri(fmt::format("http://localhost:{}/", gtr_pref_int_get(TR_KEY_rpc_port)));
}
} // namespace
@ -879,7 +879,7 @@ void SpeedPage::init_time_combo(Gtk::ComboBox& combo, Glib::RefPtr<Session> cons
{
auto const iter = store->append();
(*iter)[time_cols.offset] = i;
(*iter)[time_cols.title] = gtr_sprintf("%02d:%02d", i / 60, i % 60);
(*iter)[time_cols.title] = fmt::format("{:02}:{:02}", i / 60, i % 60);
}
/* build the widget */

View File

@ -611,7 +611,7 @@ void rename_torrent(Glib::RefPtr<Gio::File> const& file)
if (info != nullptr)
{
auto const old_name = info->get_attribute_as_string(G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
auto const new_name = gtr_sprintf("%s.added", old_name);
auto const new_name = fmt::format("{}.added", old_name);
try
{
@ -889,7 +889,7 @@ namespace
Glib::ustring get_collated_name(tr_torrent const* tor)
{
return gtr_sprintf("%s\t%s", Glib::ustring(tr_torrentName(tor)).lowercase(), tr_torrentView(tor).hash_string);
return fmt::format("{}\t{}", Glib::ustring(tr_torrentName(tor)).lowercase(), tr_torrentView(tor).hash_string);
}
struct metadata_callback_data
@ -1600,7 +1600,7 @@ bool core_read_rpc_response_idle(TrVariantPtr const& response)
}
else
{
g_warning("Pending RPC request for tag %" PRId64 " not found", tag);
g_warning("%s", fmt::format(_("Couldn't find pending RPC request for tag {tag}"), fmt::arg("tag", tag)).c_str());
}
}
@ -1631,12 +1631,7 @@ void Session::Impl::send_rpc_request(
/* make the request */
#ifdef DEBUG_RPC
{
struct evbuffer* buf = tr_variantToBuf(request, TR_VARIANT_FMT_JSON_LEAN);
size_t const buf_len = evbuffer_get_length(buf);
g_message("request: [%*.*s]", TR_ARG_TUPLE((int)buf_len, (int)buf_len, evbuffer_pullup(buf, -1)));
evbuffer_free(buf);
}
g_message("%s", fmt::format("request: [{}]", tr_variantToStr(request, TR_VARIANT_FMT_JSON_LEAN)).c_str());
#endif
tr_rpc_request_exec_json(session_, request, core_read_rpc_response, nullptr);

View File

@ -393,7 +393,14 @@ bool gtr_file_trash_or_remove(std::string const& filename, tr_error** error)
}
catch (Glib::Error const& e)
{
g_message("Unable to trash file \"%s\": %s", filename.c_str(), TR_GLIB_EXCEPTION_WHAT(e));
g_message(
"%s",
fmt::format(
_("Couldn't move '{path}' to trash: {error} ({error_code})"),
fmt::arg("path", filename),
fmt::arg("error", TR_GLIB_EXCEPTION_WHAT(e)),
fmt::arg("error_code", e.code()))
.c_str());
tr_error_set(error, e.code(), TR_GLIB_EXCEPTION_WHAT(e));
}
}
@ -406,7 +413,14 @@ bool gtr_file_trash_or_remove(std::string const& filename, tr_error** error)
}
catch (Glib::Error const& e)
{
g_message("Unable to delete file \"%s\": %s", filename.c_str(), TR_GLIB_EXCEPTION_WHAT(e));
g_message(
"%s",
fmt::format(
_("Couldn't remove '{path}': {error} ({error_code})"),
fmt::arg("path", filename),
fmt::arg("error", TR_GLIB_EXCEPTION_WHAT(e)),
fmt::arg("error_code", e.code()))
.c_str());
tr_error_clear(error);
tr_error_set(error, e.code(), TR_GLIB_EXCEPTION_WHAT(e));
result = false;
@ -418,7 +432,7 @@ bool gtr_file_trash_or_remove(std::string const& filename, tr_error** error)
Glib::ustring gtr_get_help_uri()
{
static auto const uri = gtr_sprintf("https://transmissionbt.com/help/gtk/%d.%dx", MAJOR_VERSION, MINOR_VERSION / 10);
static auto const uri = fmt::format("https://transmissionbt.com/help/gtk/{}.{}x", MAJOR_VERSION, MINOR_VERSION / 10);
return uri;
}
@ -458,7 +472,7 @@ void gtr_open_uri(Glib::ustring const& uri)
if (!opened)
{
g_message("Unable to open \"%s\"", uri.c_str());
g_message("%s", fmt::format(_("Couldn't open '{url}'"), fmt::arg("url", uri)).c_str());
}
}
}
@ -760,9 +774,9 @@ std::list<std::string> gtr_get_recent_dirs(std::string const& pref)
for (size_t i = 0; i < max_recent_dirs; ++i)
{
auto const key = gtr_sprintf("recent-%s-dir-%d", pref, i + 1);
auto const key = fmt::format("recent-{}-dir-{}", pref, i + 1);
if (auto const val = gtr_pref_string_get(tr_quark_new({ key.c_str(), key.size() })); !val.empty())
if (auto const val = gtr_pref_string_get(tr_quark_new(key)); !val.empty())
{
list.push_back(val);
}
@ -791,8 +805,8 @@ void gtr_save_recent_dir(std::string const& pref, Glib::RefPtr<Session> const& c
int i = 0;
for (auto const& d : list)
{
auto const key = gtr_sprintf("recent-%s-dir-%d", pref, ++i);
gtr_pref_string_set(tr_quark_new({ key.c_str(), key.size() }), d);
auto const key = fmt::format("recent-{}-dir-{}", pref, ++i);
gtr_pref_string_set(tr_quark_new(key), d);
}
gtr_pref_save(core->get_session());

View File

@ -262,30 +262,6 @@ inline char const* sprintify(std::string const& arg)
} // namespace gtr_detail
template<typename... Ts>
inline Glib::ustring gtr_sprintf(char const* fmt, Ts const&... args)
{
#if G_ENCODE_VERSION(GLIBMM_MAJOR_VERSION, GLIBMM_MINOR_VERSION) < G_ENCODE_VERSION(2, 62)
auto* const c_str = g_strdup_printf(fmt, gtr_detail::sprintify(args)...);
Glib::ustring ustr(c_str);
g_free(c_str);
return ustr;
#else
return Glib::ustring::sprintf(fmt, args...);
#endif
}
template<typename... Ts>
inline Glib::ustring gtr_sprintf(Glib::ustring const& fmt, Ts const&... args)
{
#if G_ENCODE_VERSION(GLIBMM_MAJOR_VERSION, GLIBMM_MINOR_VERSION) < G_ENCODE_VERSION(2, 62)
return gtr_sprintf(fmt.c_str(), args...);
#else
return Glib::ustring::sprintf(fmt, args...);
#endif
}
template<typename T, typename U>
inline Glib::RefPtr<T> gtr_ptr_static_cast(Glib::RefPtr<U> const& ptr)
{

View File

@ -9,6 +9,8 @@
#include <glibmm/i18n.h>
#include <gtkmm.h>
#include <fmt/core.h>
#include <libtransmission/transmission.h>
#include <libtransmission/utils.h>
#include <libtransmission/version.h>
@ -87,17 +89,18 @@ int main(int argc, char** argv)
}
catch (Glib::OptionError const& e)
{
g_print(
_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
TR_GLIB_EXCEPTION_WHAT(e),
argv[0]);
fmt::print(stderr, "{}\n", TR_GLIB_EXCEPTION_WHAT(e));
fmt::print(
stderr,
_("Run '{program} --help' to see a full list of available command line options.\n"),
fmt::arg("program", argv[0]));
return 1;
}
/* handle the trivial "version" option */
if (show_version)
{
fprintf(stderr, "%s %s\n", AppName, LONG_VERSION_STRING);
fmt::print(stderr, "{} {}\n", AppName, LONG_VERSION_STRING);
return 0;
}