source: trunk/gtk/tr-window.c @ 13625

Last change on this file since 13625 was 13625, checked in by jordan, 9 years ago

Follow more common whitespace style conventions in the C code (libtransmission, daemon, utils, cli, gtk).

  • Property svn:keywords set to Date Rev Author Id
File size: 31.6 KB
Line 
1/******************************************************************************
2 * $Id: tr-window.c 13625 2012-12-05 17:29:46Z jordan $
3 *
4 * Copyright (c) Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#include <string.h>
26
27#include <gtk/gtk.h>
28#include <glib/gi18n.h>
29
30#include <libtransmission/transmission.h>
31#include <libtransmission/utils.h> /* tr_formatter_speed_KBps () */
32
33#include "actions.h"
34#include "conf.h"
35#include "filter.h"
36#include "hig.h"
37#include "torrent-cell-renderer.h"
38#include "tr-prefs.h"
39#include "tr-window.h"
40#include "util.h"
41
42typedef struct
43{
44    GtkWidget *           speedlimit_on_item[2];
45    GtkWidget *           speedlimit_off_item[2];
46    GtkWidget *           ratio_on_item;
47    GtkWidget *           ratio_off_item;
48    GtkWidget *           scroll;
49    GtkWidget *           view;
50    GtkWidget *           toolbar;
51    GtkWidget *           filter;
52    GtkWidget *           status;
53    GtkWidget *           status_menu;
54    GtkWidget *           ul_lb;
55    GtkWidget *           dl_lb;
56    GtkWidget *           stats_lb;
57    GtkWidget *           gutter_lb;
58    GtkWidget *           alt_speed_image;
59    GtkWidget *           alt_speed_button;
60    GtkWidget *           options_menu;
61    GtkTreeSelection *    selection;
62    GtkCellRenderer *     renderer;
63    GtkTreeViewColumn *   column;
64    GtkTreeModel *        filter_model;
65    TrCore *              core;
66    gulong                pref_handler_id;
67}
68PrivateData;
69
70static GQuark
71get_private_data_key (void)
72{
73    static GQuark q = 0;
74    if (!q) q = g_quark_from_static_string ("private-data");
75    return q;
76}
77
78static PrivateData*
79get_private_data (GtkWindow * w)
80{
81    return g_object_get_qdata (G_OBJECT (w), get_private_data_key ());
82}
83
84/***
85****
86***/
87
88static void
89on_popup_menu (GtkWidget * self UNUSED,
90               GdkEventButton * event)
91{
92    GtkWidget * menu = gtr_action_get_widget ("/main-window-popup");
93
94    gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
95                  (event ? event->button : 0),
96                  (event ? event->time : 0));
97}
98
99static void
100view_row_activated (GtkTreeView       * tree_view UNUSED,
101                    GtkTreePath       * path      UNUSED,
102                    GtkTreeViewColumn * column    UNUSED,
103                    gpointer            user_data UNUSED)
104{
105    gtr_action_activate ("show-torrent-properties");
106}
107
108static gboolean
109tree_view_search_equal_func (GtkTreeModel * model,
110                             gint           column UNUSED,
111                             const gchar  * key,
112                             GtkTreeIter  * iter,
113                             gpointer       search_data UNUSED)
114{
115    gboolean match;
116    char * lower;
117    const char * name = NULL;
118
119    lower = g_strstrip (g_utf8_strdown (key, -1));
120    gtk_tree_model_get (model, iter, MC_NAME_COLLATED, &name, -1);
121    match = strstr (name, lower) != NULL;
122    g_free (lower);
123
124    return !match;
125}
126
127static GtkWidget*
128makeview (PrivateData * p)
129{
130    GtkWidget *         view;
131    GtkTreeViewColumn * col;
132    GtkTreeSelection *  sel;
133    GtkCellRenderer *   r;
134    GtkTreeView * tree_view;
135
136    view = gtk_tree_view_new ();
137    tree_view = GTK_TREE_VIEW (view);
138    gtk_tree_view_set_search_column (tree_view, MC_NAME_COLLATED);
139    gtk_tree_view_set_search_equal_func (tree_view,
140                                         tree_view_search_equal_func,
141                                         NULL, NULL);
142    gtk_tree_view_set_headers_visible (tree_view, FALSE);
143    gtk_tree_view_set_fixed_height_mode (tree_view, TRUE);
144
145    p->selection = gtk_tree_view_get_selection (tree_view);
146
147    p->column = col = GTK_TREE_VIEW_COLUMN (g_object_new (GTK_TYPE_TREE_VIEW_COLUMN,
148        "title", _("Torrent"),
149        "resizable", TRUE,
150        "sizing", GTK_TREE_VIEW_COLUMN_FIXED,
151        NULL));
152
153    p->renderer = r = torrent_cell_renderer_new ();
154    gtk_tree_view_column_pack_start (col, r, FALSE);
155    gtk_tree_view_column_add_attribute (col, r, "torrent", MC_TORRENT);
156    gtk_tree_view_column_add_attribute (col, r, "piece-upload-speed", MC_SPEED_UP);
157    gtk_tree_view_column_add_attribute (col, r, "piece-download-speed", MC_SPEED_DOWN);
158
159    gtk_tree_view_append_column (tree_view, col);
160    g_object_set (r, "xpad", GUI_PAD_SMALL, "ypad", GUI_PAD_SMALL, NULL);
161
162    gtk_tree_view_set_rules_hint (tree_view, TRUE);
163    sel = gtk_tree_view_get_selection (tree_view);
164    gtk_tree_selection_set_mode (GTK_TREE_SELECTION (sel),
165                                 GTK_SELECTION_MULTIPLE);
166
167    g_signal_connect (view, "popup-menu",
168                      G_CALLBACK (on_popup_menu), NULL);
169    g_signal_connect (view, "button-press-event",
170                      G_CALLBACK (on_tree_view_button_pressed),
171                    (void *) on_popup_menu);
172    g_signal_connect (view, "button-release-event",
173                      G_CALLBACK (on_tree_view_button_released), NULL);
174    g_signal_connect (view, "row-activated",
175                      G_CALLBACK (view_row_activated), NULL);
176
177
178    gtk_tree_view_set_model (tree_view, p->filter_model);
179    g_object_unref (p->filter_model);
180
181    return view;
182}
183
184static void syncAltSpeedButton (PrivateData * p);
185
186static void
187prefsChanged (TrCore * core UNUSED,
188              const char *  key,
189              gpointer      wind)
190{
191    PrivateData * p = get_private_data (GTK_WINDOW (wind));
192
193    if (!strcmp (key, PREF_KEY_COMPACT_VIEW))
194    {
195        g_object_set (p->renderer, "compact", gtr_pref_flag_get (key), NULL);
196        /* since the cell size has changed, we need gtktreeview to revalidate
197         * its fixed-height mode values. Unfortunately there's not an API call
198         * for that, but it *does* revalidate when it thinks the style's been tweaked */
199        g_signal_emit_by_name (p->view, "style-updated", NULL, NULL);
200    }
201    else if (!strcmp (key, PREF_KEY_STATUSBAR))
202    {
203        const gboolean isEnabled = gtr_pref_flag_get (key);
204        g_object_set (p->status, "visible", isEnabled, NULL);
205    }
206    else if (!strcmp (key, PREF_KEY_FILTERBAR))
207    {
208        const gboolean isEnabled = gtr_pref_flag_get (key);
209        g_object_set (p->filter, "visible", isEnabled, NULL);
210    }
211    else if (!strcmp (key, PREF_KEY_TOOLBAR))
212    {
213        const gboolean isEnabled = gtr_pref_flag_get (key);
214        g_object_set (p->toolbar, "visible", isEnabled, NULL);
215    }
216    else if (!strcmp (key, PREF_KEY_STATUSBAR_STATS))
217    {
218        gtr_window_refresh (wind);
219    }
220    else if (!strcmp (key, TR_PREFS_KEY_ALT_SPEED_ENABLED) ||
221             !strcmp (key, TR_PREFS_KEY_ALT_SPEED_UP_KBps) ||
222             !strcmp (key, TR_PREFS_KEY_ALT_SPEED_DOWN_KBps))
223    {
224        syncAltSpeedButton (p);
225    }
226}
227
228static void
229privateFree (gpointer vprivate)
230{
231    PrivateData * p = vprivate;
232    g_signal_handler_disconnect (p->core, p->pref_handler_id);
233    g_free (p);
234}
235
236static void
237onYinYangReleased (GtkWidget * w UNUSED, gpointer vprivate)
238{
239    PrivateData * p = vprivate;
240
241    gtk_menu_popup (GTK_MENU (p->status_menu),
242                    NULL, NULL, NULL, NULL, 0,
243                    gtk_get_current_event_time ());
244}
245
246#define STATS_MODE "stats-mode"
247
248static struct
249{
250    const char *  val, *i18n;
251} stats_modes[] = {
252    { "total-ratio",      N_("Total Ratio")                },
253    { "session-ratio",    N_("Session Ratio")              },
254    { "total-transfer",   N_("Total Transfer")             },
255    { "session-transfer", N_("Session Transfer")           }
256};
257
258static void
259status_menu_toggled_cb (GtkCheckMenuItem * menu_item,
260                        gpointer           vprivate)
261{
262    if (gtk_check_menu_item_get_active (menu_item))
263    {
264        PrivateData * p = vprivate;
265        const char *  val = g_object_get_data (G_OBJECT (
266                                                   menu_item), STATS_MODE);
267        gtr_core_set_pref (p->core, PREF_KEY_STATUSBAR_STATS, val);
268    }
269}
270
271static void
272syncAltSpeedButton (PrivateData * p)
273{
274    char u[32];
275    char d[32];
276    char * str;
277    const char * fmt;
278    const gboolean b = gtr_pref_flag_get (TR_PREFS_KEY_ALT_SPEED_ENABLED);
279    const char * stock = b ? "alt-speed-on" : "alt-speed-off";
280    GtkWidget * w = p->alt_speed_button;
281
282    tr_formatter_speed_KBps (u, gtr_pref_int_get (TR_PREFS_KEY_ALT_SPEED_UP_KBps), sizeof (u));
283    tr_formatter_speed_KBps (d, gtr_pref_int_get (TR_PREFS_KEY_ALT_SPEED_DOWN_KBps), sizeof (d));
284    fmt = b ? _("Click to disable Alternative Speed Limits\n (%1$s down, %2$s up)")
285            : _("Click to enable Alternative Speed Limits\n (%1$s down, %2$s up)");
286    str = g_strdup_printf (fmt, d, u);
287
288    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), b);
289    gtk_image_set_from_stock (GTK_IMAGE (p->alt_speed_image), stock, -1);
290    gtk_button_set_alignment (GTK_BUTTON (w), 0.5, 0.5);
291    gtk_widget_set_tooltip_text (w, str);
292
293    g_free (str);
294}
295
296static void
297alt_speed_toggled_cb (GtkToggleButton * button, gpointer vprivate)
298{
299    PrivateData * p = vprivate;
300    const gboolean b = gtk_toggle_button_get_active (button);
301    gtr_core_set_pref_bool (p->core, TR_PREFS_KEY_ALT_SPEED_ENABLED,  b);
302}
303
304/***
305****  FILTER
306***/
307
308static void
309findMaxAnnounceTime (GtkTreeModel *      model,
310                     GtkTreePath  * path UNUSED,
311                     GtkTreeIter *       iter,
312                     gpointer            gmaxTime)
313{
314    tr_torrent *    tor;
315    const tr_stat * torStat;
316    time_t *        maxTime = gmaxTime;
317
318    gtk_tree_model_get (model, iter, MC_TORRENT, &tor, -1);
319    torStat = tr_torrentStatCached (tor);
320    *maxTime = MAX (*maxTime, torStat->manualAnnounceTime);
321}
322
323static gboolean
324onAskTrackerQueryTooltip (GtkWidget *            widget UNUSED,
325                          gint                   x UNUSED,
326                          gint                   y UNUSED,
327                          gboolean               keyboard_tip UNUSED,
328                          GtkTooltip *           tooltip,
329                          gpointer               gdata)
330{
331    const time_t now = time (NULL);
332    time_t       maxTime = 0;
333    PrivateData * p = gdata;
334
335    gtk_tree_selection_selected_foreach (p->selection,
336                                         findMaxAnnounceTime,
337                                         &maxTime);
338    if (maxTime <= now)
339    {
340        return FALSE;
341    }
342    else
343    {
344        char      buf[512];
345        char      timebuf[64];
346        const int seconds = maxTime - now;
347
348        tr_strltime (timebuf, seconds, sizeof (timebuf));
349        g_snprintf (buf, sizeof (buf),
350                    _("Tracker will allow requests in %s"), timebuf);
351        gtk_tooltip_set_text (tooltip, buf);
352        return TRUE;
353    }
354}
355
356static gboolean
357onAltSpeedToggledIdle (gpointer vp)
358{
359    PrivateData * p = vp;
360    gboolean b = tr_sessionUsesAltSpeed (gtr_core_session (p->core));
361    gtr_core_set_pref_bool (p->core, TR_PREFS_KEY_ALT_SPEED_ENABLED, b);
362
363    return FALSE;
364}
365
366static void
367onAltSpeedToggled (tr_session * s UNUSED, bool isEnabled UNUSED, bool byUser UNUSED, void * p)
368{
369    gdk_threads_add_idle (onAltSpeedToggledIdle, p);
370}
371
372/***
373****  Speed limit menu
374***/
375
376#define DIRECTION_KEY "direction-key"
377#define ENABLED_KEY "enabled-key"
378#define SPEED_KEY "speed-key"
379
380static void
381onSpeedToggled (GtkCheckMenuItem * check, gpointer vp)
382{
383    PrivateData * p = vp;
384    GObject * o = G_OBJECT (check);
385    gboolean isEnabled = g_object_get_data (o, ENABLED_KEY) != 0;
386    tr_direction dir = GPOINTER_TO_INT (g_object_get_data (o, DIRECTION_KEY));
387    const char * key = dir == TR_UP ? TR_PREFS_KEY_USPEED_ENABLED
388                                    : TR_PREFS_KEY_DSPEED_ENABLED;
389
390    if (gtk_check_menu_item_get_active (check))
391        gtr_core_set_pref_bool (p->core, key, isEnabled);
392}
393
394static void
395onSpeedSet (GtkCheckMenuItem * check, gpointer vp)
396{
397    const char * key;
398    PrivateData * p = vp;
399    GObject * o = G_OBJECT (check);
400    const int KBps = GPOINTER_TO_INT (g_object_get_data (o, SPEED_KEY));
401    tr_direction dir = GPOINTER_TO_INT (g_object_get_data (o, DIRECTION_KEY));
402
403    key = dir==TR_UP ? TR_PREFS_KEY_USPEED_KBps : TR_PREFS_KEY_DSPEED_KBps;
404    gtr_core_set_pref_int (p->core, key, KBps);
405
406    key = dir==TR_UP ? TR_PREFS_KEY_USPEED_ENABLED : TR_PREFS_KEY_DSPEED_ENABLED;
407    gtr_core_set_pref_bool (p->core, key, TRUE);
408}
409
410static GtkWidget*
411createSpeedMenu (PrivateData * p, tr_direction dir)
412{
413    int i, n;
414    GtkWidget *w, *m;
415    const int speeds_KBps[] = { 5, 10, 20, 30, 40, 50, 75, 100, 150, 200, 250, 500, 750 };
416
417    m = gtk_menu_new ();
418
419    w = gtk_radio_menu_item_new_with_label (NULL, _("Unlimited"));
420    p->speedlimit_off_item[dir] = w;
421    g_object_set_data (G_OBJECT (w), DIRECTION_KEY, GINT_TO_POINTER (dir));
422    g_object_set_data (G_OBJECT (w), ENABLED_KEY, GINT_TO_POINTER (FALSE));
423    g_signal_connect (w, "toggled", G_CALLBACK (onSpeedToggled), p);
424    gtk_menu_shell_append (GTK_MENU_SHELL (m), w);
425
426    w = gtk_radio_menu_item_new_with_label_from_widget (GTK_RADIO_MENU_ITEM (w), "");
427    p->speedlimit_on_item[dir] = w;
428    g_object_set_data (G_OBJECT (w), DIRECTION_KEY, GINT_TO_POINTER (dir));
429    g_object_set_data (G_OBJECT (w), ENABLED_KEY, GINT_TO_POINTER (TRUE));
430    g_signal_connect (w, "toggled", G_CALLBACK (onSpeedToggled), p);
431    gtk_menu_shell_append (GTK_MENU_SHELL (m), w);
432
433    w = gtk_separator_menu_item_new ();
434    gtk_menu_shell_append (GTK_MENU_SHELL (m), w);
435
436    for (i=0, n=G_N_ELEMENTS (speeds_KBps); i<n; ++i)
437    {
438        char buf[128];
439        tr_formatter_speed_KBps (buf, speeds_KBps[i], sizeof (buf));
440        w = gtk_menu_item_new_with_label (buf);
441        g_object_set_data (G_OBJECT (w), DIRECTION_KEY, GINT_TO_POINTER (dir));
442        g_object_set_data (G_OBJECT (w), SPEED_KEY, GINT_TO_POINTER (speeds_KBps[i]));
443        g_signal_connect (w, "activate", G_CALLBACK (onSpeedSet), p);
444        gtk_menu_shell_append (GTK_MENU_SHELL (m), w);
445    }
446
447    return m;
448}
449
450/***
451****  Speed limit menu
452***/
453
454#define RATIO_KEY "stock-ratio-index"
455
456static const double stockRatios[] = { 0.25, 0.5, 0.75, 1, 1.5, 2, 3 };
457
458static void
459onRatioToggled (GtkCheckMenuItem * check, gpointer vp)
460{
461    PrivateData * p = vp;
462    if (gtk_check_menu_item_get_active (check))
463    {
464        gboolean f = g_object_get_data (G_OBJECT (check), ENABLED_KEY) != 0;
465        gtr_core_set_pref_bool (p->core, TR_PREFS_KEY_RATIO_ENABLED, f);
466    }
467}
468static void
469onRatioSet (GtkCheckMenuItem * check, gpointer vp)
470{
471    PrivateData * p = vp;
472    int i = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (check), RATIO_KEY));
473    const double ratio = stockRatios[i];
474    gtr_core_set_pref_double (p->core, TR_PREFS_KEY_RATIO, ratio);
475    gtr_core_set_pref_bool (p->core, TR_PREFS_KEY_RATIO_ENABLED, TRUE);
476}
477
478static GtkWidget*
479createRatioMenu (PrivateData * p)
480{
481    int i, n;
482    GtkWidget *m, *w;
483
484    m = gtk_menu_new ();
485
486    w = gtk_radio_menu_item_new_with_label (NULL, _("Seed Forever"));
487    p->ratio_off_item = w;
488    g_object_set_data (G_OBJECT (w), ENABLED_KEY, GINT_TO_POINTER (FALSE));
489    g_signal_connect (w, "toggled", G_CALLBACK (onRatioToggled), p);
490    gtk_menu_shell_append (GTK_MENU_SHELL (m), w);
491
492    w = gtk_radio_menu_item_new_with_label_from_widget (GTK_RADIO_MENU_ITEM (w), "");
493    p->ratio_on_item = w;
494    g_object_set_data (G_OBJECT (w), ENABLED_KEY, GINT_TO_POINTER (TRUE));
495    g_signal_connect (w, "toggled", G_CALLBACK (onRatioToggled), p);
496    gtk_menu_shell_append (GTK_MENU_SHELL (m), w);
497
498    w = gtk_separator_menu_item_new ();
499    gtk_menu_shell_append (GTK_MENU_SHELL (m), w);
500
501    for (i=0, n=G_N_ELEMENTS (stockRatios); i<n; ++i)
502    {
503        char buf[128];
504        tr_strlratio (buf, stockRatios[i], sizeof (buf));
505        w = gtk_menu_item_new_with_label (buf);
506        g_object_set_data (G_OBJECT (w), RATIO_KEY, GINT_TO_POINTER (i));
507        g_signal_connect (w, "activate", G_CALLBACK (onRatioSet), p);
508        gtk_menu_shell_append (GTK_MENU_SHELL (m), w);
509    }
510
511    return m;
512}
513
514/***
515****  Option menu
516***/
517
518static GtkWidget*
519createOptionsMenu (PrivateData * p)
520{
521    GtkWidget * m;
522    GtkWidget * top = gtk_menu_new ();
523
524    m = gtk_menu_item_new_with_label (_("Limit Download Speed"));
525    gtk_menu_item_set_submenu (GTK_MENU_ITEM (m), createSpeedMenu (p, TR_DOWN));
526    gtk_menu_shell_append (GTK_MENU_SHELL (top), m);
527
528    m = gtk_menu_item_new_with_label (_("Limit Upload Speed"));
529    gtk_menu_item_set_submenu (GTK_MENU_ITEM (m), createSpeedMenu (p, TR_UP));
530    gtk_menu_shell_append (GTK_MENU_SHELL (top), m);
531
532    m = gtk_separator_menu_item_new ();
533    gtk_menu_shell_append (GTK_MENU_SHELL (top), m);
534
535    m = gtk_menu_item_new_with_label (_("Stop Seeding at Ratio"));
536    gtk_menu_item_set_submenu (GTK_MENU_ITEM (m), createRatioMenu (p));
537    gtk_menu_shell_append (GTK_MENU_SHELL (top), m);
538
539    gtk_widget_show_all (top);
540    return top;
541}
542
543static void
544onOptionsClicked (GtkButton * button UNUSED, gpointer vp)
545{
546    char buf1[512];
547    char buf2[512];
548    gboolean b;
549    GtkWidget * w;
550    PrivateData * p = vp;
551
552    w = p->speedlimit_on_item[TR_DOWN];
553    tr_formatter_speed_KBps (buf1, gtr_pref_int_get (TR_PREFS_KEY_DSPEED_KBps), sizeof (buf1));
554    gtr_label_set_text (GTK_LABEL (gtk_bin_get_child (GTK_BIN (w))), buf1);
555
556    b = gtr_pref_flag_get (TR_PREFS_KEY_DSPEED_ENABLED);
557    w = b ? p->speedlimit_on_item[TR_DOWN] : p->speedlimit_off_item[TR_DOWN];
558    gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
559
560    w = p->speedlimit_on_item[TR_UP];
561    tr_formatter_speed_KBps (buf1, gtr_pref_int_get (TR_PREFS_KEY_USPEED_KBps), sizeof (buf1));
562    gtr_label_set_text (GTK_LABEL (gtk_bin_get_child (GTK_BIN (w))), buf1);
563
564    b = gtr_pref_flag_get (TR_PREFS_KEY_USPEED_ENABLED);
565    w = b ? p->speedlimit_on_item[TR_UP] : p->speedlimit_off_item[TR_UP];
566    gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
567
568    tr_strlratio (buf1, gtr_pref_double_get (TR_PREFS_KEY_RATIO), sizeof (buf1));
569    g_snprintf (buf2, sizeof (buf2), _("Stop at Ratio (%s)"), buf1);
570    gtr_label_set_text (GTK_LABEL (gtk_bin_get_child (GTK_BIN (p->ratio_on_item))), buf2);
571
572    b = gtr_pref_flag_get (TR_PREFS_KEY_RATIO_ENABLED);
573    gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (b ? p->ratio_on_item : p->ratio_off_item), TRUE);
574
575    gtk_menu_popup (GTK_MENU (p->options_menu), NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time ());
576}
577
578/***
579****  PUBLIC
580***/
581
582GtkWidget *
583gtr_window_new (GtkApplication * app, GtkUIManager * ui_mgr, TrCore * core)
584{
585    int           i, n;
586    const char  * pch;
587    PrivateData * p;
588    GtkWidget   * mainmenu, *toolbar, *filter, *list, *status;
589    GtkWidget   * vbox, *w, *self, *h, *hbox, *menu;
590    GtkWindow   * win;
591    GSList      * l;
592
593    p = g_new0 (PrivateData, 1);
594
595    /* make the window */
596    self = gtk_application_window_new (app);
597    g_object_set_qdata_full (G_OBJECT (self), get_private_data_key (), p, privateFree);
598    win = GTK_WINDOW (self);
599    gtk_window_set_title (win, g_get_application_name ());
600    gtk_window_set_role (win, "tr-main");
601    gtk_window_set_default_size (win,
602                                 gtr_pref_int_get (PREF_KEY_MAIN_WINDOW_WIDTH),
603                                 gtr_pref_int_get (PREF_KEY_MAIN_WINDOW_HEIGHT));
604    gtk_window_move (win, gtr_pref_int_get (PREF_KEY_MAIN_WINDOW_X),
605                          gtr_pref_int_get (PREF_KEY_MAIN_WINDOW_Y));
606    if (gtr_pref_flag_get (PREF_KEY_MAIN_WINDOW_IS_MAXIMIZED))
607        gtk_window_maximize (win);
608    gtk_window_add_accel_group (win, gtk_ui_manager_get_accel_group (ui_mgr));
609
610    /* window's main container */
611    vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
612    gtk_container_add (GTK_CONTAINER (self), vbox);
613
614    /* main menu */
615    mainmenu = gtr_action_get_widget ("/main-window-menu");
616    w = gtr_action_get_widget ("/main-window-menu/torrent-menu/torrent-reannounce");
617    g_signal_connect (w, "query-tooltip", G_CALLBACK (onAskTrackerQueryTooltip), p);
618
619    /* toolbar */
620    toolbar = p->toolbar = gtr_action_get_widget ("/main-window-toolbar");
621    gtk_style_context_add_class (gtk_widget_get_style_context (toolbar),
622                                 GTK_STYLE_CLASS_PRIMARY_TOOLBAR);
623    gtr_action_set_important ("open-torrent-toolbar", TRUE);
624    gtr_action_set_important ("show-torrent-properties", TRUE);
625
626    /* filter */
627    h = filter = p->filter = gtr_filter_bar_new (gtr_core_session (core),
628                                                 gtr_core_model (core),
629                                                 &p->filter_model);
630    gtk_container_set_border_width (GTK_CONTAINER (h), GUI_PAD_SMALL);
631
632    /* status menu */
633    menu = p->status_menu = gtk_menu_new ();
634    l = NULL;
635    pch = gtr_pref_string_get (PREF_KEY_STATUSBAR_STATS);
636    for (i = 0, n = G_N_ELEMENTS (stats_modes); i < n; ++i)
637    {
638        const char * val = stats_modes[i].val;
639        w = gtk_radio_menu_item_new_with_label (l, _ (stats_modes[i].i18n));
640        l = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (w));
641        gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w),
642                                       !strcmp (val, pch));
643        g_object_set_data (G_OBJECT (
644                               w), STATS_MODE,
645                         (gpointer)stats_modes[i].val);
646        g_signal_connect (w, "toggled", G_CALLBACK (
647                              status_menu_toggled_cb), p);
648        gtk_menu_shell_append (GTK_MENU_SHELL (menu), w);
649        gtk_widget_show (w);
650    }
651
652    /* status */
653    h = status = p->status = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, GUI_PAD_BIG);
654    gtk_container_set_border_width (GTK_CONTAINER (h), GUI_PAD_SMALL);
655
656        w = gtk_button_new ();
657        gtk_container_add (GTK_CONTAINER (w), gtk_image_new_from_stock ("utilities", -1));
658        gtk_widget_set_tooltip_text (w, _("Options"));
659        gtk_box_pack_start (GTK_BOX (h), w, 0, 0, 0);
660        gtk_button_set_relief (GTK_BUTTON (w), GTK_RELIEF_NONE);
661        p->options_menu = createOptionsMenu (p);
662        g_signal_connect (w, "clicked", G_CALLBACK (onOptionsClicked), p);
663
664        p->alt_speed_image = gtk_image_new ();
665        w = p->alt_speed_button = gtk_toggle_button_new ();
666        gtk_button_set_image (GTK_BUTTON (w), p->alt_speed_image);
667        gtk_button_set_relief (GTK_BUTTON (w), GTK_RELIEF_NONE);
668        g_signal_connect (w, "toggled", G_CALLBACK (alt_speed_toggled_cb), p);
669        gtk_box_pack_start (GTK_BOX (h), w, 0, 0, 0);
670
671        w = p->gutter_lb = gtk_label_new ("N Torrents");
672        gtk_label_set_single_line_mode (GTK_LABEL (w), TRUE);
673        gtk_box_pack_start (GTK_BOX (h), w, 1, 1, GUI_PAD);
674
675        hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, GUI_PAD);
676            w = gtk_alignment_new (0.0f, 0.0f, 0.0f, 0.0f);
677            gtk_widget_set_size_request (w, GUI_PAD, 0u);
678            gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
679            w = p->ul_lb = gtk_label_new (NULL);
680            gtk_label_set_single_line_mode (GTK_LABEL (w), TRUE);
681            gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
682            w = gtk_image_new_from_stock (GTK_STOCK_GO_UP, GTK_ICON_SIZE_MENU);
683            gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
684        gtk_box_pack_end (GTK_BOX (h), hbox, FALSE, FALSE, 0);
685
686        hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, GUI_PAD);
687            w = gtk_alignment_new (0.0f, 0.0f, 0.0f, 0.0f);
688            gtk_widget_set_size_request (w, GUI_PAD, 0u);
689            gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
690            w = p->dl_lb = gtk_label_new (NULL);
691            gtk_label_set_single_line_mode (GTK_LABEL (w), TRUE);
692            gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
693            w = gtk_image_new_from_stock (GTK_STOCK_GO_DOWN, GTK_ICON_SIZE_MENU);
694            gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
695        gtk_box_pack_end (GTK_BOX (h), hbox, FALSE, FALSE, 0);
696
697        hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, GUI_PAD);
698            w = gtk_button_new ();
699            gtk_widget_set_tooltip_text (w, _("Statistics"));
700            gtk_container_add (GTK_CONTAINER (w), gtk_image_new_from_stock ("ratio", -1));
701            gtk_button_set_relief (GTK_BUTTON (w), GTK_RELIEF_NONE);
702            g_signal_connect (w, "clicked", G_CALLBACK (onYinYangReleased), p);
703            gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
704            w = p->stats_lb = gtk_label_new (NULL);
705            gtk_label_set_single_line_mode (GTK_LABEL (w), TRUE);
706            gtk_box_pack_end (GTK_BOX (hbox), w, FALSE, FALSE, 0);
707        gtk_box_pack_end (GTK_BOX (h), hbox, FALSE, FALSE, 0);
708
709
710    /* workarea */
711    p->view = makeview (p);
712    w = list = p->scroll = gtk_scrolled_window_new (NULL, NULL);
713    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (w),
714                                    GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
715    gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (w),
716                                         GTK_SHADOW_IN);
717    gtk_container_add (GTK_CONTAINER (w), p->view);
718
719    /* lay out the widgets */
720    gtk_box_pack_start (GTK_BOX (vbox), mainmenu, FALSE, FALSE, 0);
721    gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
722    gtk_box_pack_start (GTK_BOX (vbox), filter, FALSE, FALSE, 0);
723    gtk_box_pack_start (GTK_BOX (vbox), list, TRUE, TRUE, 0);
724    gtk_box_pack_start (GTK_BOX (vbox), status, FALSE, FALSE, 0);
725
726    {
727        /* this is to determine the maximum width/height for the label */
728        int w=0, h=0;
729        PangoLayout * pango_layout;
730        pango_layout = gtk_widget_create_pango_layout (p->ul_lb, "999.99 kB/s");
731        pango_layout_get_pixel_size (pango_layout, &w, &h);
732        gtk_widget_set_size_request (p->ul_lb, w, h);
733        gtk_widget_set_size_request (p->dl_lb, w, h);
734        gtk_misc_set_alignment (GTK_MISC (p->ul_lb), 1.0, 0.5);
735        gtk_misc_set_alignment (GTK_MISC (p->dl_lb), 1.0, 0.5);
736        g_object_unref (G_OBJECT (pango_layout));
737    }
738
739    /* show all but the window */
740    gtk_widget_show_all (vbox);
741
742    /* listen for prefs changes that affect the window */
743    p->core = core;
744    prefsChanged (core, PREF_KEY_COMPACT_VIEW, self);
745    prefsChanged (core, PREF_KEY_FILTERBAR, self);
746    prefsChanged (core, PREF_KEY_STATUSBAR, self);
747    prefsChanged (core, PREF_KEY_STATUSBAR_STATS, self);
748    prefsChanged (core, PREF_KEY_TOOLBAR, self);
749    prefsChanged (core, TR_PREFS_KEY_ALT_SPEED_ENABLED, self);
750    p->pref_handler_id = g_signal_connect (core, "prefs-changed",
751                                           G_CALLBACK (prefsChanged), self);
752
753    tr_sessionSetAltSpeedFunc (gtr_core_session (core), onAltSpeedToggled, p);
754
755    return self;
756}
757
758static void
759updateTorrentCount (PrivateData * p)
760{
761    if (p && p->core)
762    {
763        char      buf[512];
764        const int torrentCount = gtk_tree_model_iter_n_children (gtr_core_model (p->core), NULL);
765        const int visibleCount = gtk_tree_model_iter_n_children (p->filter_model, NULL);
766
767        if (!torrentCount)
768            *buf = '\0';
769        else if (torrentCount != visibleCount)
770            g_snprintf (buf, sizeof (buf),
771                        ngettext ("%1$'d of %2$'d Torrent",
772                                  "%1$'d of %2$'d Torrents",
773                                  torrentCount),
774                        visibleCount, torrentCount);
775        else
776            g_snprintf (buf, sizeof (buf),
777                        ngettext ("%'d Torrent", "%'d Torrents", torrentCount),
778                        torrentCount);
779        gtr_label_set_text (GTK_LABEL (p->gutter_lb), buf);
780    }
781}
782
783static void
784updateStats (PrivateData * p)
785{
786    const char *            pch;
787    char                    up[32], down[32], ratio[32], buf[512];
788    struct tr_session_stats stats;
789    tr_session *            session = gtr_core_session (p->core);
790
791    /* update the stats */
792    pch = gtr_pref_string_get (PREF_KEY_STATUSBAR_STATS);
793    if (!strcmp (pch, "session-ratio"))
794    {
795        tr_sessionGetStats (session, &stats);
796        tr_strlratio (ratio, stats.ratio, sizeof (ratio));
797        g_snprintf (buf, sizeof (buf), _("Ratio: %s"), ratio);
798    }
799    else if (!strcmp (pch, "session-transfer"))
800    {
801        tr_sessionGetStats (session, &stats);
802        tr_strlsize (up, stats.uploadedBytes, sizeof (up));
803        tr_strlsize (down, stats.downloadedBytes, sizeof (down));
804        /* Translators: "size|" is here for disambiguation. Please remove it from your translation.
805           %1$s is the size of the data we've downloaded
806           %2$s is the size of the data we've uploaded */
807        g_snprintf (buf, sizeof (buf), Q_ (
808                        "Down: %1$s, Up: %2$s"), down, up);
809    }
810    else if (!strcmp (pch, "total-transfer"))
811    {
812        tr_sessionGetCumulativeStats (session, &stats);
813        tr_strlsize (up, stats.uploadedBytes, sizeof (up));
814        tr_strlsize (down, stats.downloadedBytes, sizeof (down));
815        /* Translators: "size|" is here for disambiguation. Please remove it from your translation.
816           %1$s is the size of the data we've downloaded
817           %2$s is the size of the data we've uploaded */
818        g_snprintf (buf, sizeof (buf), Q_ (
819                        "size|Down: %1$s, Up: %2$s"), down, up);
820    }
821    else     /* default is total-ratio */
822    {
823        tr_sessionGetCumulativeStats (session, &stats);
824        tr_strlratio (ratio, stats.ratio, sizeof (ratio));
825        g_snprintf (buf, sizeof (buf), _("Ratio: %s"), ratio);
826    }
827    gtr_label_set_text (GTK_LABEL (p->stats_lb), buf);
828}
829
830static void
831updateSpeeds (PrivateData * p)
832{
833    tr_session * session = gtr_core_session (p->core);
834
835    if (session != NULL)
836    {
837        char buf[128];
838        double up=0, down=0;
839        GtkTreeIter iter;
840        GtkTreeModel * model = gtr_core_model (p->core);
841
842        if (gtk_tree_model_iter_nth_child (model, &iter, NULL, 0)) do
843        {
844            double u, d;
845            gtk_tree_model_get (model, &iter, MC_SPEED_UP, &u,
846                                              MC_SPEED_DOWN, &d,
847                                              -1);
848            up += u;
849            down += d;
850        }
851        while (gtk_tree_model_iter_next (model, &iter));
852
853        tr_formatter_speed_KBps (buf, down, sizeof (buf));
854        gtr_label_set_text (GTK_LABEL (p->dl_lb), buf);
855
856        tr_formatter_speed_KBps (buf, up, sizeof (buf));
857        gtr_label_set_text (GTK_LABEL (p->ul_lb), buf);
858    }
859}
860
861void
862gtr_window_refresh (GtkWindow * self)
863{
864    PrivateData * p = get_private_data (self);
865
866    if (p && p->core && gtr_core_session (p->core))
867    {
868        updateSpeeds (p);
869        updateTorrentCount (p);
870        updateStats (p);
871    }
872}
873
874GtkTreeSelection*
875gtr_window_get_selection (GtkWindow * w)
876{
877    return get_private_data (w)->selection;
878}
879
880void
881gtr_window_set_busy (GtkWindow * w, gboolean isBusy)
882{
883    if (w && gtk_widget_get_realized (GTK_WIDGET (w)))
884    {
885        GdkDisplay * display = gtk_widget_get_display (GTK_WIDGET (w));
886        GdkCursor * cursor = isBusy ? gdk_cursor_new_for_display (display, GDK_WATCH) : NULL;
887
888        gdk_window_set_cursor (gtk_widget_get_window (GTK_WIDGET (w)), cursor);
889        gdk_display_flush (display);
890
891        g_clear_object (&cursor);
892    }
893}
Note: See TracBrowser for help on using the repository browser.