File: tr-icon.c

package info (click to toggle)
transmission 2.94-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 30,236 kB
  • sloc: ansic: 75,129; objc: 18,912; cpp: 14,872; sh: 4,474; makefile: 1,059; xml: 139
file content (156 lines) | stat: -rw-r--r-- 4,303 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * This file Copyright (C) 2007-2014 Mnemosyne LLC
 *
 * It may be used under the GNU GPL versions 2 or 3
 * or any future license endorsed by Mnemosyne LLC.
 *
 * $Id$
 */


#include <glib/gi18n.h>
#include <gtk/gtk.h>
#ifdef HAVE_LIBAPPINDICATOR
 #include <libayatana-appindicator/app-indicator.h>
#endif
#include <libtransmission/transmission.h>
#include <libtransmission/utils.h>
#include "actions.h"
#include "tr-icon.h"
#include "util.h"

static TR_DEFINE_QUARK (tr_core, core)

#define ICON_NAME "transmission"

#ifdef HAVE_LIBAPPINDICATOR
void
gtr_icon_refresh (gpointer vindicator UNUSED)
{
}
#else
static void
activated (GtkStatusIcon * self UNUSED, gpointer user_data UNUSED)
{
  gtr_action_activate ("toggle-main-window");
}

static void
popup (GtkStatusIcon * self,
       guint           button,
       guint           when,
       gpointer        data UNUSED)
{
  GtkWidget * w = gtr_action_get_widget ("/icon-popup");

  gtk_menu_popup (GTK_MENU (w), NULL, NULL,
                  gtk_status_icon_position_menu,
                  self, button, when);
}

void
gtr_icon_refresh (gpointer vicon)
{
  double KBps;
  double limit;
  char up[64];
  char upLimit[64];
  char down[64];
  char downLimit[64];
  char tip[1024];
  const char * idle = _("Idle");
  GtkStatusIcon * icon = GTK_STATUS_ICON (vicon);
  tr_session * session = gtr_core_session (g_object_get_qdata (G_OBJECT (icon), core_quark ()));

  /* 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 */
  *upLimit = '\0';
  if (tr_sessionGetActiveSpeedLimit_KBps (session, TR_UP, &limit))
    {
      char buf[64];
      tr_formatter_speed_KBps (buf, limit, sizeof (buf));
      g_snprintf (upLimit, sizeof (upLimit), _(" (Limit: %s)"), buf);
    }

  /* down */
  KBps = tr_sessionGetRawSpeed_KBps (session, TR_DOWN);
  if (KBps < 0.001)
    g_strlcpy (down, idle, sizeof (down));
  else
    tr_formatter_speed_KBps (down, KBps, sizeof (down));

  /* down limit */
  *downLimit = '\0';
  if (tr_sessionGetActiveSpeedLimit_KBps (session, TR_DOWN, &limit))
    {
      char buf[64];
      tr_formatter_speed_KBps (buf, limit, sizeof (buf));
      g_snprintf (downLimit, sizeof (downLimit), _(" (Limit: %s)"), buf);
    }

  /* %1$s: current upload speed
   * %2$s: current upload limit, if any
   * %3$s: current download speed
   * %4$s: current download limit, if any */
  g_snprintf (tip, sizeof (tip), _("Transmission\nUp: %1$s %2$s\nDown: %3$s %4$s"), up, upLimit, down, downLimit);

  gtk_status_icon_set_tooltip_text (GTK_STATUS_ICON (icon), tip);
}
#endif

static const char *
getIconName (void)
{
  const char * icon_name;

  GtkIconTheme * theme = gtk_icon_theme_get_default ();

  /* if the tray's icon is a 48x48 file, use it;
   * otherwise, use the fallback builtin icon */
  if (!gtk_icon_theme_has_icon (theme, TRAY_ICON))
    {
      icon_name = ICON_NAME;
    }
  else
   {
     GtkIconInfo * icon_info = gtk_icon_theme_lookup_icon (theme, TRAY_ICON, 48, GTK_ICON_LOOKUP_USE_BUILTIN);
     const gboolean icon_is_builtin = gtk_icon_info_get_filename (icon_info) == NULL;
#if GTK_CHECK_VERSION(3,8,0)
    g_object_unref (icon_info);
#else
    gtk_icon_info_free (icon_info);
#endif
     icon_name = icon_is_builtin ? ICON_NAME : TRAY_ICON;
    }

  return icon_name;
}

gpointer
gtr_icon_new (TrCore * core)
{
#ifdef HAVE_LIBAPPINDICATOR
  GtkWidget * w;
  const char * icon_name = getIconName ();
  AppIndicator * indicator = app_indicator_new (ICON_NAME, icon_name, APP_INDICATOR_CATEGORY_SYSTEM_SERVICES);
  app_indicator_set_status (indicator, APP_INDICATOR_STATUS_ACTIVE);
  w = gtr_action_get_widget ("/icon-popup");
  app_indicator_set_menu (indicator, GTK_MENU (w));
  app_indicator_set_title (indicator, g_get_application_name ());
  g_object_set_qdata (G_OBJECT (indicator), core_quark (), core);
  return indicator;
#else
  const char * icon_name = getIconName ();
  GtkStatusIcon * icon = gtk_status_icon_new_from_icon_name (icon_name);
  g_signal_connect (icon, "activate", G_CALLBACK (activated), NULL);
  g_signal_connect (icon, "popup-menu", G_CALLBACK (popup), NULL);
  g_object_set_qdata (G_OBJECT (icon), core_quark (), core);
  return icon;
#endif
}