From ae2dd5eba967f8d47c625ba3a4a93a04801974a1 Mon Sep 17 00:00:00 2001 From: Mike Gelfand Date: Sat, 15 Oct 2022 09:13:50 -0700 Subject: [PATCH] Use fmt for string formatting (GTK client) (#3967) --- gtk/Application.cc | 39 +++++++++++++++++---------------------- gtk/Dialogs.cc | 12 +++++++----- gtk/FaviconCache.cc | 4 +++- gtk/FilterBar.cc | 4 +++- gtk/MessageLogWindow.cc | 2 +- gtk/Notify.cc | 4 +++- gtk/PrefsDialog.cc | 4 ++-- gtk/Session.cc | 13 ++++--------- gtk/Utils.cc | 30 ++++++++++++++++++++++-------- gtk/Utils.h | 24 ------------------------ gtk/main.cc | 13 ++++++++----- 11 files changed, 70 insertions(+), 79 deletions(-) diff --git a/gtk/Application.cc b/gtk/Application.cc index 393a3f44b..16f275633 100644 --- a/gtk/Application.cc +++ b/gtk/Application.cc @@ -22,6 +22,9 @@ #include #include +#ifdef G_OS_UNIX +#include +#endif #include #include @@ -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) diff --git a/gtk/Dialogs.cc b/gtk/Dialogs.cc index 59a68611b..74bc51af1 100644 --- a/gtk/Dialogs.cc +++ b/gtk/Dialogs.cc @@ -8,6 +8,8 @@ #include #include +#include + #include #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( parent, - gtr_sprintf("%s", primary_text), + fmt::format("{}", primary_text), true /*use_markup*/, TR_GTK_MESSAGE_TYPE(WARNING), TR_GTK_BUTTONS_TYPE(NONE), diff --git a/gtk/FaviconCache.cc b/gtk/FaviconCache.cc index a614f7bb3..9d1210030 100644 --- a/gtk/FaviconCache.cc +++ b/gtk/FaviconCache.cc @@ -10,6 +10,8 @@ #include /* g_remove() */ +#include + #include #include // tr_sessionFetch() #include @@ -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() diff --git a/gtk/FilterBar.cc b/gtk/FilterBar.cc index fb9d923c3..d1393f80b 100644 --- a/gtk/FilterBar.cc +++ b/gtk/FilterBar.cc @@ -12,6 +12,8 @@ #include #include +#include + #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() diff --git a/gtk/MessageLogWindow.cc b/gtk/MessageLogWindow.cc index 228963c54..71ef5919d 100644 --- a/gtk/MessageLogWindow.cc +++ b/gtk/MessageLogWindow.cc @@ -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); diff --git a/gtk/Notify.cc b/gtk/Notify.cc index 4bad13fc0..1c320d643 100644 --- a/gtk/Notify.cc +++ b/gtk/Notify.cc @@ -10,6 +10,8 @@ #include #include +#include + #include "Notify.h" #include "Prefs.h" #include "PrefsDialog.h" @@ -123,7 +125,7 @@ void dbus_proxy_ready_callback(Glib::RefPtr& 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; } diff --git a/gtk/PrefsDialog.cc b/gtk/PrefsDialog.cc index fdcb62b1d..9a5f5d5df 100644 --- a/gtk/PrefsDialog.cc +++ b/gtk/PrefsDialog.cc @@ -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 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 */ diff --git a/gtk/Session.cc b/gtk/Session.cc index 20445eebb..5cfd40c94 100644 --- a/gtk/Session.cc +++ b/gtk/Session.cc @@ -611,7 +611,7 @@ void rename_torrent(Glib::RefPtr 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); diff --git a/gtk/Utils.cc b/gtk/Utils.cc index e7b975c10..483dc9ae3 100644 --- a/gtk/Utils.cc +++ b/gtk/Utils.cc @@ -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 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 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()); diff --git a/gtk/Utils.h b/gtk/Utils.h index 04296a882..1b4b2e2a0 100644 --- a/gtk/Utils.h +++ b/gtk/Utils.h @@ -262,30 +262,6 @@ inline char const* sprintify(std::string const& arg) } // namespace gtr_detail -template -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 -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 inline Glib::RefPtr gtr_ptr_static_cast(Glib::RefPtr const& ptr) { diff --git a/gtk/main.cc b/gtk/main.cc index 0ed502b45..0cc8468ae 100644 --- a/gtk/main.cc +++ b/gtk/main.cc @@ -9,6 +9,8 @@ #include #include +#include + #include #include #include @@ -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; }