2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2007-2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
|
|
|
|
// 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
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
#include <glibmm.h>
|
|
|
|
#include <glibmm/i18n.h>
|
|
|
|
|
2010-02-07 19:32:35 +00:00
|
|
|
#ifdef HAVE_LIBAPPINDICATOR
|
2017-04-20 16:02:19 +00:00
|
|
|
#include <libappindicator/app-indicator.h>
|
2010-02-07 19:32:35 +00:00
|
|
|
#endif
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2010-07-03 00:25:22 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/utils.h>
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2021-11-01 00:11:23 +00:00
|
|
|
#include "Actions.h"
|
|
|
|
#include "Session.h"
|
|
|
|
#include "SystemTrayIcon.h"
|
|
|
|
#include "Utils.h"
|
2007-02-19 22:09:05 +00:00
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
auto const TrayIconName = Glib::ustring("transmission-tray-icon"s);
|
|
|
|
auto const AppIconName = Glib::ustring("transmission"s);
|
|
|
|
auto const AppName = Glib::ustring("transmission-gtk"s);
|
|
|
|
|
|
|
|
} // 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);
|
|
|
|
|
|
|
|
private:
|
2021-11-01 00:11:23 +00:00
|
|
|
Glib::RefPtr<Session> const core_;
|
2021-10-18 20:22:31 +00:00
|
|
|
|
|
|
|
Gtk::Menu* menu_;
|
|
|
|
|
2010-02-07 19:32:35 +00:00
|
|
|
#ifdef HAVE_LIBAPPINDICATOR
|
2021-10-18 20:22:31 +00:00
|
|
|
AppIndicator* indicator_;
|
|
|
|
#else
|
|
|
|
Glib::RefPtr<Gtk::StatusIcon> icon_;
|
|
|
|
#endif
|
|
|
|
};
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
#ifdef HAVE_LIBAPPINDICATOR
|
|
|
|
|
|
|
|
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
|
|
|
|
2010-02-07 19:32:35 +00:00
|
|
|
#else
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
double KBps;
|
|
|
|
double limit;
|
2021-10-18 20:22:31 +00:00
|
|
|
Glib::ustring upLimit;
|
|
|
|
Glib::ustring downLimit;
|
2021-12-31 00:30:21 +00:00
|
|
|
char const* const idle = _("Idle");
|
2021-10-18 20:22:31 +00:00
|
|
|
auto* session = core_->get_session();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* up */
|
|
|
|
KBps = tr_sessionGetRawSpeed_KBps(session, TR_UP);
|
|
|
|
|
2021-12-31 00:30:21 +00:00
|
|
|
auto const up = KBps < 0.001 ? idle : tr_formatter_speed_KBps(KBps);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* up limit */
|
|
|
|
if (tr_sessionGetActiveSpeedLimit_KBps(session, TR_UP, &limit))
|
2013-01-04 19:45:39 +00:00
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
upLimit = gtr_sprintf(_(" (Limit: %s)"), tr_formatter_speed_KBps(limit));
|
2009-06-30 18:08:50 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* down */
|
|
|
|
KBps = tr_sessionGetRawSpeed_KBps(session, TR_DOWN);
|
2013-01-04 19:45:39 +00:00
|
|
|
|
2021-12-31 00:30:21 +00:00
|
|
|
auto const down = KBps < 0.001 ? idle : tr_formatter_speed_KBps(KBps);
|
2009-06-30 18:08:50 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* down limit */
|
|
|
|
if (tr_sessionGetActiveSpeedLimit_KBps(session, TR_DOWN, &limit))
|
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
downLimit = gtr_sprintf(_(" (Limit: %s)"), tr_formatter_speed_KBps(limit));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* %1$s: current upload speed
|
|
|
|
* %2$s: current upload limit, if any
|
|
|
|
* %3$s: current download speed
|
|
|
|
* %4$s: current download limit, if any */
|
2021-10-18 20:22:31 +00:00
|
|
|
auto const tip = gtr_sprintf(_("Transmission\nUp: %1$s %2$s\nDown: %3$s %4$s"), up, upLimit, down, downLimit);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
icon_->set_tooltip_text(tip);
|
2008-05-24 23:23:20 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2022-02-01 17:30:51 +00:00
|
|
|
auto theme = Gtk::IconTheme::get_default();
|
|
|
|
|
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-01 17:30:51 +00:00
|
|
|
if (!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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
impl_->refresh();
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:12:50 +00:00
|
|
|
SystemTrayIcon::Impl::Impl(Gtk::Window& main_window, Glib::RefPtr<Session> const& core)
|
2021-10-18 20:22:31 +00:00
|
|
|
: core_(core)
|
|
|
|
{
|
|
|
|
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);
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2011-08-08 16:02:37 +00:00
|
|
|
#ifdef HAVE_LIBAPPINDICATOR
|
2017-04-19 12:04:45 +00:00
|
|
|
|
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());
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2010-02-07 19:32:35 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
|
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));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2010-02-07 19:32:35 +00:00
|
|
|
#endif
|
2011-08-08 16:02:37 +00:00
|
|
|
}
|