1 | /****************************************************************************** |
---|
2 | * $Id: tr_window.c 3353 2007-10-10 18:52:08Z charles $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2007 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 | |
---|
32 | #include "actions.h" |
---|
33 | #include "hig.h" |
---|
34 | #include "tr_cell_renderer_progress.h" |
---|
35 | #include "tr_core.h" |
---|
36 | #include "tr_torrent.h" |
---|
37 | #include "tr_window.h" |
---|
38 | #include "util.h" |
---|
39 | |
---|
40 | typedef struct |
---|
41 | { |
---|
42 | GtkWidget * scroll; |
---|
43 | GtkWidget * view; |
---|
44 | GtkWidget * status; |
---|
45 | GtkWidget * ul_lb; |
---|
46 | GtkWidget * dl_lb; |
---|
47 | GtkTreeSelection * selection; |
---|
48 | GtkCellRenderer * namerend; |
---|
49 | } |
---|
50 | PrivateData; |
---|
51 | |
---|
52 | #define PRIVATE_DATA_KEY "private-data" |
---|
53 | |
---|
54 | PrivateData* |
---|
55 | get_private_data( TrWindow * w ) |
---|
56 | { |
---|
57 | return (PrivateData*) g_object_get_data (G_OBJECT(w), PRIVATE_DATA_KEY); |
---|
58 | } |
---|
59 | |
---|
60 | /*** |
---|
61 | **** |
---|
62 | ***/ |
---|
63 | |
---|
64 | /* kludge to have the progress bars notice theme changes */ |
---|
65 | static void |
---|
66 | stylekludge( GObject * obj, GParamSpec * spec, gpointer data ) |
---|
67 | { |
---|
68 | if( 0 == strcmp( "style", spec->name ) ) |
---|
69 | { |
---|
70 | tr_cell_renderer_progress_reset_style( |
---|
71 | TR_CELL_RENDERER_PROGRESS( data ) ); |
---|
72 | gtk_widget_queue_draw( GTK_WIDGET( obj ) ); |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | static void |
---|
77 | formatname( GtkTreeViewColumn * col SHUTUP, GtkCellRenderer * rend, |
---|
78 | GtkTreeModel * model, GtkTreeIter * iter, gpointer data SHUTUP ) |
---|
79 | { |
---|
80 | TrTorrent * gtor; |
---|
81 | char * name, * mb, * str, * top, * bottom; |
---|
82 | const char * fmt; |
---|
83 | guint64 size; |
---|
84 | int status, err, eta, tpeers, upeers, dpeers; |
---|
85 | |
---|
86 | gtk_tree_model_get( model, iter, MC_NAME, &name, MC_STAT, &status, |
---|
87 | MC_ERR, &err, MC_SIZE, &size, |
---|
88 | MC_ETA, &eta, MC_PEERS, &tpeers, MC_UPEERS, &upeers, |
---|
89 | MC_DPEERS, &dpeers, MC_TORRENT, >or, -1 ); |
---|
90 | |
---|
91 | tpeers = MAX( tpeers, 0 ); |
---|
92 | upeers = MAX( upeers, 0 ); |
---|
93 | dpeers = MAX( dpeers, 0 ); |
---|
94 | mb = readablesize(size); |
---|
95 | |
---|
96 | top = tr_torrent_status_str ( gtor ); |
---|
97 | |
---|
98 | if( TR_OK != err ) |
---|
99 | { |
---|
100 | char * terr; |
---|
101 | gtk_tree_model_get( model, iter, MC_TERR, &terr, -1 ); |
---|
102 | bottom = g_strconcat( _("Error: "), terr, NULL ); |
---|
103 | g_free( terr ); |
---|
104 | } |
---|
105 | else if( TR_STATUS_DOWNLOAD & status ) |
---|
106 | { |
---|
107 | bottom = g_strdup_printf( ngettext( "Downloading from %i of %i peer", |
---|
108 | "Downloading from %i of %i peers", |
---|
109 | tpeers ), dpeers, tpeers ); |
---|
110 | } |
---|
111 | else |
---|
112 | { |
---|
113 | bottom = NULL; |
---|
114 | } |
---|
115 | |
---|
116 | fmt = err==TR_OK |
---|
117 | ? "<b>%s (%s)</b>\n<small>%s\n%s</small>" |
---|
118 | : "<span color='red'><b>%s (%s)</b>\n<small>%s\n%s</small></span>"; |
---|
119 | str = g_markup_printf_escaped( fmt, name, mb, top, (bottom ? bottom : "") ); |
---|
120 | g_object_set( rend, "markup", str, NULL ); |
---|
121 | g_free( name ); |
---|
122 | g_free( mb ); |
---|
123 | g_free( str ); |
---|
124 | g_free( top ); |
---|
125 | g_free( bottom ); |
---|
126 | g_object_unref( gtor ); |
---|
127 | } |
---|
128 | |
---|
129 | static void |
---|
130 | formatprog( GtkTreeViewColumn * col SHUTUP, GtkCellRenderer * rend, |
---|
131 | GtkTreeModel * model, GtkTreeIter * iter, gpointer data SHUTUP ) |
---|
132 | { |
---|
133 | char * dlstr, * ulstr, * str, * marked; |
---|
134 | gfloat prog, dl, ul; |
---|
135 | guint64 down, up; |
---|
136 | |
---|
137 | gtk_tree_model_get( model, iter, MC_PROG_D, &prog, MC_DRATE, &dl, |
---|
138 | MC_URATE, &ul, MC_DOWN, &down, MC_UP, &up, -1 ); |
---|
139 | prog = MAX( prog, 0.0 ); |
---|
140 | prog = MIN( prog, 1.0 ); |
---|
141 | |
---|
142 | ulstr = readablespeed (ul); |
---|
143 | if( 1.0 == prog ) |
---|
144 | { |
---|
145 | dlstr = ratiostr( down, up ); |
---|
146 | str = g_strdup_printf( _("Ratio: %s\nUL: %s"), dlstr, ulstr ); |
---|
147 | } |
---|
148 | else |
---|
149 | { |
---|
150 | dlstr = readablespeed( dl ); |
---|
151 | str = g_strdup_printf( _("DL: %s\nUL: %s"), dlstr, ulstr ); |
---|
152 | } |
---|
153 | marked = g_markup_printf_escaped( "<small>%s</small>", str ); |
---|
154 | g_object_set( rend, "markup", str, "progress", prog, NULL ); |
---|
155 | g_free( dlstr ); |
---|
156 | g_free( ulstr ); |
---|
157 | g_free( str ); |
---|
158 | g_free( marked ); |
---|
159 | } |
---|
160 | |
---|
161 | static void |
---|
162 | on_popup_menu ( GtkWidget * self UNUSED, GdkEventButton * event ) |
---|
163 | { |
---|
164 | GtkWidget * menu = action_get_widget ( "/main-window-popup" ); |
---|
165 | gtk_menu_popup (GTK_MENU(menu), NULL, NULL, NULL, NULL, |
---|
166 | (event ? event->button : 0), |
---|
167 | (event ? event->time : 0)); |
---|
168 | } |
---|
169 | |
---|
170 | static void |
---|
171 | view_row_activated ( GtkTreeView * tree_view UNUSED, |
---|
172 | GtkTreePath * path UNUSED, |
---|
173 | GtkTreeViewColumn * column UNUSED, |
---|
174 | gpointer user_data UNUSED ) |
---|
175 | { |
---|
176 | action_activate( "show-torrent-details" ); |
---|
177 | } |
---|
178 | |
---|
179 | static GtkWidget* |
---|
180 | makeview( PrivateData * p ) |
---|
181 | { |
---|
182 | GtkWidget * view; |
---|
183 | GtkTreeViewColumn * col; |
---|
184 | GtkTreeSelection * sel; |
---|
185 | GtkCellRenderer * namerend, * progrend; |
---|
186 | char * str; |
---|
187 | |
---|
188 | view = gtk_tree_view_new(); |
---|
189 | |
---|
190 | p->selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(view) ); |
---|
191 | namerend = gtk_cell_renderer_text_new(); |
---|
192 | p->namerend = namerend; |
---|
193 | /* note that this renderer is set to ellipsize, just not here */ |
---|
194 | col = gtk_tree_view_column_new_with_attributes( _("Name"), namerend, |
---|
195 | NULL ); |
---|
196 | gtk_tree_view_column_set_cell_data_func( col, namerend, formatname, |
---|
197 | NULL, NULL ); |
---|
198 | gtk_tree_view_column_set_expand( col, TRUE ); |
---|
199 | gtk_tree_view_column_set_sizing( col, GTK_TREE_VIEW_COLUMN_AUTOSIZE ); |
---|
200 | gtk_tree_view_column_set_sort_column_id( col, MC_NAME ); |
---|
201 | gtk_tree_view_append_column( GTK_TREE_VIEW( view ), col ); |
---|
202 | |
---|
203 | progrend = tr_cell_renderer_progress_new(); |
---|
204 | /* this string is only used to determine the size of the progress bar */ |
---|
205 | str = g_markup_printf_escaped( "<big>%s</big>", _(" fnord fnord ") ); |
---|
206 | g_object_set( progrend, "bar-sizing", str, NULL ); |
---|
207 | g_free(str); |
---|
208 | col = gtk_tree_view_column_new_with_attributes( _("Progress"), progrend, |
---|
209 | NULL); |
---|
210 | gtk_tree_view_column_set_cell_data_func( col, progrend, formatprog, |
---|
211 | NULL, NULL ); |
---|
212 | gtk_tree_view_column_set_sizing( col, GTK_TREE_VIEW_COLUMN_AUTOSIZE ); |
---|
213 | gtk_tree_view_column_set_sort_column_id( col, MC_PROG_D ); |
---|
214 | gtk_tree_view_append_column( GTK_TREE_VIEW( view ), col ); |
---|
215 | |
---|
216 | /* XXX this shouldn't be necessary */ |
---|
217 | g_signal_connect( view, "notify::style", |
---|
218 | G_CALLBACK( stylekludge ), progrend ); |
---|
219 | |
---|
220 | gtk_tree_view_set_rules_hint( GTK_TREE_VIEW( view ), TRUE ); |
---|
221 | sel = gtk_tree_view_get_selection( GTK_TREE_VIEW( view ) ); |
---|
222 | gtk_tree_selection_set_mode( GTK_TREE_SELECTION( sel ), |
---|
223 | GTK_SELECTION_MULTIPLE ); |
---|
224 | |
---|
225 | g_signal_connect( view, "popup-menu", |
---|
226 | G_CALLBACK(on_popup_menu), NULL ); |
---|
227 | g_signal_connect( view, "button-press-event", |
---|
228 | G_CALLBACK(on_tree_view_button_pressed), (void *) on_popup_menu); |
---|
229 | g_signal_connect( view, "row-activated", |
---|
230 | G_CALLBACK(view_row_activated), NULL); |
---|
231 | |
---|
232 | return view; |
---|
233 | } |
---|
234 | |
---|
235 | static void |
---|
236 | realized_cb ( GtkWidget * wind, gpointer unused UNUSED ) |
---|
237 | { |
---|
238 | PrivateData * p = get_private_data( GTK_WINDOW( wind ) ); |
---|
239 | sizingmagic( GTK_WINDOW(wind), GTK_SCROLLED_WINDOW( p->scroll ), |
---|
240 | GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS ); |
---|
241 | g_object_set( p->namerend, "ellipsize", PANGO_ELLIPSIZE_END, NULL ); |
---|
242 | } |
---|
243 | |
---|
244 | /*** |
---|
245 | **** PUBLIC |
---|
246 | ***/ |
---|
247 | |
---|
248 | GtkWidget * |
---|
249 | tr_window_new( GtkUIManager * ui_manager ) |
---|
250 | { |
---|
251 | PrivateData * p = g_new( PrivateData, 1 ); |
---|
252 | GtkWidget *vbox, *w, *self, *h; |
---|
253 | |
---|
254 | /* make the window */ |
---|
255 | self = gtk_window_new (GTK_WINDOW_TOPLEVEL); |
---|
256 | g_object_set_data_full(G_OBJECT(self), PRIVATE_DATA_KEY, p, g_free ); |
---|
257 | gtk_window_set_title( GTK_WINDOW( self ), g_get_application_name()); |
---|
258 | gtk_window_set_role( GTK_WINDOW( self ), "tr-main" ); |
---|
259 | gtk_window_add_accel_group (GTK_WINDOW(self), |
---|
260 | gtk_ui_manager_get_accel_group (ui_manager)); |
---|
261 | g_signal_connect( self, "realize", G_CALLBACK(realized_cb), NULL); |
---|
262 | |
---|
263 | /* window's main container */ |
---|
264 | vbox = gtk_vbox_new (FALSE, 0); |
---|
265 | gtk_container_add (GTK_CONTAINER(self), vbox); |
---|
266 | |
---|
267 | /* main menu */ |
---|
268 | w = action_get_widget( "/main-window-menu" ); |
---|
269 | gtk_box_pack_start( GTK_BOX(vbox), w, FALSE, FALSE, 0 ); |
---|
270 | |
---|
271 | /* toolbar */ |
---|
272 | w = action_get_widget( "/main-window-toolbar" ); |
---|
273 | gtk_box_pack_start( GTK_BOX(vbox), w, FALSE, FALSE, 0 ); |
---|
274 | |
---|
275 | /* workarea */ |
---|
276 | p->view = makeview( p ); |
---|
277 | w = p->scroll = gtk_scrolled_window_new( NULL, NULL ); |
---|
278 | gtk_container_add( GTK_CONTAINER(w), p->view ); |
---|
279 | gtk_box_pack_start_defaults( GTK_BOX(vbox), w ); |
---|
280 | gtk_container_set_focus_child( GTK_CONTAINER( vbox ), w ); |
---|
281 | |
---|
282 | /* spacer */ |
---|
283 | w = gtk_alignment_new (0.0f, 0.0f, 0.0f, 0.0f); |
---|
284 | gtk_widget_set_usize (w, 0u, 6u); |
---|
285 | gtk_box_pack_start( GTK_BOX(vbox), w, FALSE, FALSE, 0 ); |
---|
286 | |
---|
287 | |
---|
288 | /* statusbar */ |
---|
289 | h = gtk_hbox_new( FALSE, 0 ); |
---|
290 | w = p->ul_lb = gtk_label_new( NULL ); |
---|
291 | gtk_box_pack_end( GTK_BOX(h), w, FALSE, FALSE, GUI_PAD ); |
---|
292 | w = gtk_vseparator_new( ); |
---|
293 | gtk_box_pack_end( GTK_BOX(h), w, FALSE, FALSE, GUI_PAD ); |
---|
294 | w = p->dl_lb = gtk_label_new( NULL ); |
---|
295 | gtk_box_pack_end( GTK_BOX(h), w, FALSE, FALSE, GUI_PAD ); |
---|
296 | gtk_box_pack_start( GTK_BOX(vbox), h, FALSE, FALSE, 0 ); |
---|
297 | |
---|
298 | /* show all but the window */ |
---|
299 | gtk_widget_show_all( vbox ); |
---|
300 | |
---|
301 | return self; |
---|
302 | } |
---|
303 | |
---|
304 | void |
---|
305 | tr_window_update( TrWindow * self, float downspeed, float upspeed ) |
---|
306 | { |
---|
307 | PrivateData * p = get_private_data( self ); |
---|
308 | char *tmp1, *tmp2; |
---|
309 | |
---|
310 | tmp1 = readablespeed( downspeed ); |
---|
311 | tmp2 = g_strdup_printf( _("Total DL: %s"), tmp1 ); |
---|
312 | gtk_label_set_text( GTK_LABEL(p->dl_lb), tmp2 ); |
---|
313 | g_free( tmp2 ); |
---|
314 | g_free( tmp1 ); |
---|
315 | |
---|
316 | tmp1 = readablespeed( upspeed ); |
---|
317 | tmp2 = g_strdup_printf( _("Total UL: %s"), tmp1 ); |
---|
318 | gtk_label_set_text( GTK_LABEL(p->ul_lb), tmp2 ); |
---|
319 | g_free( tmp2 ); |
---|
320 | g_free( tmp1 ); |
---|
321 | } |
---|
322 | |
---|
323 | GtkTreeSelection* |
---|
324 | tr_window_get_selection ( TrWindow * w ) |
---|
325 | { |
---|
326 | return get_private_data(w)->selection; |
---|
327 | } |
---|