2008-03-19 19:30:03 +00:00
|
|
|
/*
|
2021-11-12 09:12:50 +00:00
|
|
|
* This file Copyright (C) 2007-2021 Mnemosyne LLC
|
2007-02-19 22:09:05 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2008-09-23 19:11:04 +00:00
|
|
|
*
|
2008-03-19 19:30:03 +00:00
|
|
|
*/
|
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
|
|
|
|
2010-08-05 13:24:46 +00:00
|
|
|
#define ICON_NAME "transmission"
|
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();
|
|
|
|
|
|
|
|
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;
|
|
|
|
char up[64];
|
2021-10-18 20:22:31 +00:00
|
|
|
Glib::ustring upLimit;
|
2017-04-19 12:04:45 +00:00
|
|
|
char down[64];
|
2021-10-18 20:22:31 +00:00
|
|
|
Glib::ustring downLimit;
|
2017-04-20 16:02:19 +00:00
|
|
|
char 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);
|
|
|
|
|
|
|
|
if (KBps < 0.001)
|
|
|
|
{
|
|
|
|
g_strlcpy(up, idle, sizeof(up));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_formatter_speed_KBps(up, KBps, sizeof(up));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* up limit */
|
|
|
|
if (tr_sessionGetActiveSpeedLimit_KBps(session, TR_UP, &limit))
|
2013-01-04 19:45:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char buf[64];
|
|
|
|
tr_formatter_speed_KBps(buf, limit, sizeof(buf));
|
2021-10-18 20:22:31 +00:00
|
|
|
upLimit = gtr_sprintf(_(" (Limit: %s)"), buf);
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (KBps < 0.001)
|
2013-01-04 19:45:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
g_strlcpy(down, idle, sizeof(down));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_formatter_speed_KBps(down, KBps, sizeof(down));
|
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))
|
|
|
|
{
|
|
|
|
char buf[64];
|
|
|
|
tr_formatter_speed_KBps(buf, limit, sizeof(buf));
|
2021-10-18 20:22:31 +00:00
|
|
|
downLimit = gtr_sprintf(_(" (Limit: %s)"), buf);
|
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-10-18 20:22:31 +00:00
|
|
|
std::string getIconName()
|
|
|
|
{
|
|
|
|
std::string icon_name;
|
|
|
|
|
|
|
|
auto theme = Gtk::IconTheme::get_default();
|
2013-01-04 19:45:39 +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.
|
2021-10-18 20:22:31 +00:00
|
|
|
if (!theme->has_icon(TRAY_ICON))
|
2013-01-04 19:45:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
icon_name = ICON_NAME;
|
2013-01-04 19:45:39 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
auto const icon_info = theme->lookup_icon(TRAY_ICON, 48, Gtk::ICON_LOOKUP_USE_BUILTIN);
|
|
|
|
bool const icon_is_builtin = icon_info.get_filename().empty();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
icon_name = icon_is_builtin ? ICON_NAME : TRAY_ICON;
|
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-10-18 20:22:31 +00:00
|
|
|
indicator_ = app_indicator_new(ICON_NAME, icon_name.c_str(), APP_INDICATOR_CATEGORY_SYSTEM_SERVICES);
|
|
|
|
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
|
|
|
}
|