2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2007-2022 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2007-02-19 22:09:05 +00:00
|
|
|
|
2021-11-12 09:12:50 +00:00
|
|
|
// _AppIndicatorClass::{fallback,unfallback} use deprecated GtkStatusIcon
|
|
|
|
#undef GTK_DISABLE_DEPRECATED
|
|
|
|
// We're using deprecated Gtk::StatusItem ourselves as well
|
|
|
|
#undef GTKMM_DISABLE_DEPRECATED
|
|
|
|
|
2022-12-26 21:13:21 +00:00
|
|
|
#include "SystemTrayIcon.h"
|
|
|
|
|
|
|
|
#include "Actions.h"
|
|
|
|
#include "Session.h"
|
|
|
|
#include "Utils.h"
|
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/utils.h>
|
2022-04-08 01:50:26 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
#include <glibmm/i18n.h>
|
2022-12-27 01:43:20 +00:00
|
|
|
#include <glibmm/ustring.h>
|
|
|
|
|
|
|
|
#if !GTKMM_CHECK_VERSION(4, 0, 0)
|
|
|
|
#include <giomm/menu.h>
|
|
|
|
#include <glibmm/miscutils.h>
|
|
|
|
#include <gtkmm/icontheme.h>
|
|
|
|
#include <gtkmm/menu.h>
|
|
|
|
#if !defined(HAVE_APPINDICATOR)
|
|
|
|
#include <gtkmm/statusicon.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-12-26 21:13:21 +00:00
|
|
|
#include <string>
|
|
|
|
|
2022-10-20 17:15:13 +00:00
|
|
|
#ifdef HAVE_APPINDICATOR
|
|
|
|
#ifdef APPINDICATOR_IS_AYATANA
|
|
|
|
#include <libayatana-appindicator/app-indicator.h>
|
|
|
|
#else
|
2017-04-20 16:02:19 +00:00
|
|
|
#include <libappindicator/app-indicator.h>
|
2010-02-07 19:32:35 +00:00
|
|
|
#endif
|
2022-10-20 17:15:13 +00:00
|
|
|
#endif
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-10-20 17:15:13 +00:00
|
|
|
#ifdef HAVE_APPINDICATOR
|
2022-11-13 17:36:16 +00:00
|
|
|
#define TR_SYS_TRAY_IMPL_APPINDICATOR
|
2022-10-08 22:50:03 +00:00
|
|
|
#elif !GTKMM_CHECK_VERSION(4, 0, 0)
|
2022-11-13 17:36:16 +00:00
|
|
|
#define TR_SYS_TRAY_IMPL_STATUS_ICON
|
2022-10-08 22:50:03 +00:00
|
|
|
#else
|
2022-11-13 17:36:16 +00:00
|
|
|
#define TR_SYS_TRAY_IMPL_NONE
|
2022-10-08 22:50:03 +00:00
|
|
|
#endif
|
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2022-11-13 17:36:16 +00:00
|
|
|
#if !defined(TR_SYS_TRAY_IMPL_NONE)
|
2021-12-14 08:43:27 +00:00
|
|
|
auto const TrayIconName = Glib::ustring("transmission-tray-icon"s);
|
|
|
|
auto const AppIconName = Glib::ustring("transmission"s);
|
2022-10-08 22:50:03 +00:00
|
|
|
#endif
|
|
|
|
|
2022-11-13 17:36:16 +00:00
|
|
|
#if defined(TR_SYS_TRAY_IMPL_APPINDICATOR)
|
2021-12-14 08:43:27 +00:00
|
|
|
auto const AppName = Glib::ustring("transmission-gtk"s);
|
2022-10-08 22:50:03 +00:00
|
|
|
#endif
|
2021-12-14 08:43:27 +00:00
|
|
|
|
|
|
|
} // namespace
|
2010-02-14 14:26:45 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
class SystemTrayIcon::Impl
|
|
|
|
{
|
|
|
|
public:
|
2021-11-12 09:12:50 +00:00
|
|
|
Impl(Gtk::Window& main_window, Glib::RefPtr<Session> const& core);
|
2021-10-18 20:22:31 +00:00
|
|
|
~Impl();
|
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
TR_DISABLE_COPY_MOVE(Impl)
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void refresh();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void activated();
|
|
|
|
void popup(guint button, guint when);
|
|
|
|
|
2022-04-07 13:23:04 +00:00
|
|
|
[[nodiscard]] std::string make_tooltip_text() const;
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
private:
|
2021-11-01 00:11:23 +00:00
|
|
|
Glib::RefPtr<Session> const core_;
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-11-13 17:36:16 +00:00
|
|
|
#if !defined(TR_SYS_TRAY_IMPL_NONE)
|
2021-10-18 20:22:31 +00:00
|
|
|
Gtk::Menu* menu_;
|
2022-10-08 22:50:03 +00:00
|
|
|
#endif
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-11-13 17:36:16 +00:00
|
|
|
#if defined(TR_SYS_TRAY_IMPL_APPINDICATOR)
|
2021-10-18 20:22:31 +00:00
|
|
|
AppIndicator* indicator_;
|
2022-11-13 17:36:16 +00:00
|
|
|
#elif defined(TR_SYS_TRAY_IMPL_STATUS_ICON)
|
2021-10-18 20:22:31 +00:00
|
|
|
Glib::RefPtr<Gtk::StatusIcon> icon_;
|
|
|
|
#endif
|
|
|
|
};
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-13 17:36:16 +00:00
|
|
|
#if defined(TR_SYS_TRAY_IMPL_APPINDICATOR)
|
2021-10-18 20:22:31 +00:00
|
|
|
|
|
|
|
SystemTrayIcon::Impl::~Impl()
|
|
|
|
{
|
|
|
|
g_object_unref(indicator_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SystemTrayIcon::Impl::refresh()
|
2010-02-07 19:32:35 +00:00
|
|
|
{
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-13 17:36:16 +00:00
|
|
|
#elif defined(TR_SYS_TRAY_IMPL_STATUS_ICON)
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
SystemTrayIcon::Impl::~Impl() = default;
|
2020-08-18 10:36:10 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void SystemTrayIcon::Impl::activated()
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
gtr_action_activate("toggle-main-window");
|
2007-02-19 22:09:05 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 10:08:17 +00:00
|
|
|
void SystemTrayIcon::Impl::popup(guint /*button*/, guint /*when*/)
|
2007-02-19 22:09:05 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
menu_->popup_at_pointer(nullptr);
|
2007-02-19 22:09:05 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void SystemTrayIcon::Impl::refresh()
|
2008-01-16 16:03:18 +00:00
|
|
|
{
|
2022-04-07 13:23:04 +00:00
|
|
|
icon_->set_tooltip_text(make_tooltip_text());
|
2008-05-24 23:23:20 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-10-08 22:50:03 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
SystemTrayIcon::Impl::~Impl() = default;
|
|
|
|
|
2010-02-07 19:32:35 +00:00
|
|
|
#endif
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
namespace
|
2010-02-07 19:32:35 +00:00
|
|
|
{
|
2013-01-04 19:45:39 +00:00
|
|
|
|
2022-11-13 17:36:16 +00:00
|
|
|
#if !defined(TR_SYS_TRAY_IMPL_NONE)
|
2022-10-08 22:50:03 +00:00
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
Glib::ustring getIconName()
|
2021-10-18 20:22:31 +00:00
|
|
|
{
|
2021-12-14 08:43:27 +00:00
|
|
|
Glib::ustring icon_name;
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2020-11-02 15:16:12 +00:00
|
|
|
// if the tray's icon is a 48x48 file, use it.
|
|
|
|
// otherwise, use the fallback builtin icon.
|
2022-02-02 01:09:11 +00:00
|
|
|
if (auto theme = Gtk::IconTheme::get_default(); !theme->has_icon(TrayIconName))
|
2013-01-04 19:45:39 +00:00
|
|
|
{
|
2021-12-14 08:43:27 +00:00
|
|
|
icon_name = AppIconName;
|
2013-01-04 19:45:39 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
|
|
|
{
|
2021-12-14 08:43:27 +00:00
|
|
|
auto const icon_info = theme->lookup_icon(TrayIconName, 48, Gtk::ICON_LOOKUP_USE_BUILTIN);
|
2021-10-18 20:22:31 +00:00
|
|
|
bool const icon_is_builtin = icon_info.get_filename().empty();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
icon_name = icon_is_builtin ? AppIconName : TrayIconName;
|
2010-02-14 14:26:45 +00:00
|
|
|
}
|
2010-03-17 17:07:40 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return icon_name;
|
2010-07-04 18:27:03 +00:00
|
|
|
}
|
|
|
|
|
2022-10-08 22:50:03 +00:00
|
|
|
#endif
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
} // namespace
|
|
|
|
|
2021-11-12 09:12:50 +00:00
|
|
|
SystemTrayIcon::SystemTrayIcon(Gtk::Window& main_window, Glib::RefPtr<Session> const& core)
|
|
|
|
: impl_(std::make_unique<Impl>(main_window, core))
|
2021-10-18 20:22:31 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SystemTrayIcon::~SystemTrayIcon() = default;
|
|
|
|
|
|
|
|
void SystemTrayIcon::refresh()
|
2010-07-04 18:27:03 +00:00
|
|
|
{
|
2022-11-13 17:36:16 +00:00
|
|
|
#if !defined(TR_SYS_TRAY_IMPL_NONE)
|
2021-10-18 20:22:31 +00:00
|
|
|
impl_->refresh();
|
2022-10-08 22:50:03 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SystemTrayIcon::is_available()
|
|
|
|
{
|
2022-11-13 17:36:16 +00:00
|
|
|
#if !defined(TR_SYS_TRAY_IMPL_NONE)
|
2022-10-08 22:50:03 +00:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<SystemTrayIcon> SystemTrayIcon::create(Gtk::Window& main_window, Glib::RefPtr<Session> const& core)
|
|
|
|
{
|
|
|
|
return is_available() ? std::make_unique<SystemTrayIcon>(main_window, core) : nullptr;
|
2021-10-18 20:22:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-08 22:50:03 +00:00
|
|
|
SystemTrayIcon::Impl::Impl([[maybe_unused]] Gtk::Window& main_window, Glib::RefPtr<Session> const& core)
|
2021-10-18 20:22:31 +00:00
|
|
|
: core_(core)
|
|
|
|
{
|
2022-11-13 17:36:16 +00:00
|
|
|
#if !defined(TR_SYS_TRAY_IMPL_NONE)
|
2021-10-18 20:22:31 +00:00
|
|
|
auto const icon_name = getIconName();
|
2021-11-12 09:12:50 +00:00
|
|
|
menu_ = Gtk::make_managed<Gtk::Menu>(gtr_action_get_object<Gio::Menu>("icon-popup"));
|
|
|
|
menu_->attach_to_widget(main_window);
|
2022-10-08 22:50:03 +00:00
|
|
|
#endif
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-11-13 17:36:16 +00:00
|
|
|
#if defined(TR_SYS_TRAY_IMPL_APPINDICATOR)
|
2021-12-14 08:43:27 +00:00
|
|
|
indicator_ = app_indicator_new(AppName.c_str(), icon_name.c_str(), APP_INDICATOR_CATEGORY_SYSTEM_SERVICES);
|
2021-10-18 20:22:31 +00:00
|
|
|
app_indicator_set_status(indicator_, APP_INDICATOR_STATUS_ACTIVE);
|
|
|
|
app_indicator_set_menu(indicator_, Glib::unwrap(menu_));
|
|
|
|
app_indicator_set_title(indicator_, Glib::get_application_name().c_str());
|
2022-11-13 17:36:16 +00:00
|
|
|
#elif defined(TR_SYS_TRAY_IMPL_STATUS_ICON)
|
2021-10-18 20:22:31 +00:00
|
|
|
icon_ = Gtk::StatusIcon::create(icon_name);
|
2021-11-12 09:12:50 +00:00
|
|
|
icon_->signal_activate().connect(sigc::mem_fun(*this, &Impl::activated));
|
|
|
|
icon_->signal_popup_menu().connect(sigc::mem_fun(*this, &Impl::popup));
|
2010-02-07 19:32:35 +00:00
|
|
|
#endif
|
2011-08-08 16:02:37 +00:00
|
|
|
}
|
2022-04-07 13:23:04 +00:00
|
|
|
|
|
|
|
std::string SystemTrayIcon::Impl::make_tooltip_text() const
|
|
|
|
{
|
|
|
|
auto const* const session = core_->get_session();
|
|
|
|
return fmt::format(
|
|
|
|
_("{upload_speed} ▲ {download_speed} ▼"),
|
|
|
|
fmt::arg("upload_speed", tr_formatter_speed_KBps(tr_sessionGetRawSpeed_KBps(session, TR_UP))),
|
|
|
|
fmt::arg("download_speed", tr_formatter_speed_KBps(tr_sessionGetRawSpeed_KBps(session, TR_DOWN))));
|
|
|
|
}
|