1 | /* |
---|
2 | * This file Copyright (C) Mnemosyne LLC |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
5 | * Transmission project are granted a special exemption to clause 2 (b) |
---|
6 | * so that the bulk of its code can remain under the MIT license. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id: file-list.c 13808 2013-01-19 08:44:23Z jordan $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <stddef.h> |
---|
14 | #include <stdio.h> |
---|
15 | #include <string.h> |
---|
16 | #include <glib/gi18n.h> |
---|
17 | #include <gtk/gtk.h> |
---|
18 | |
---|
19 | #include <libtransmission/transmission.h> |
---|
20 | #include <libtransmission/utils.h> |
---|
21 | |
---|
22 | #include "file-list.h" |
---|
23 | #include "hig.h" |
---|
24 | #include "icons.h" |
---|
25 | #include "tr-prefs.h" |
---|
26 | #include "util.h" |
---|
27 | |
---|
28 | #define TR_DOWNLOAD_KEY "tr-download-key" |
---|
29 | #define TR_COLUMN_ID_KEY "tr-model-column-id-key" |
---|
30 | #define TR_PRIORITY_KEY "tr-priority-key" |
---|
31 | |
---|
32 | enum |
---|
33 | { |
---|
34 | /* these two fields could be any number at all so long as they're not |
---|
35 | * TR_PRI_LOW, TR_PRI_NORMAL, TR_PRI_HIGH, TRUE, or FALSE */ |
---|
36 | NOT_SET = 1000, |
---|
37 | MIXED = 1001 |
---|
38 | }; |
---|
39 | |
---|
40 | enum |
---|
41 | { |
---|
42 | FC_ICON, |
---|
43 | FC_LABEL, |
---|
44 | FC_LABEL_ESC, |
---|
45 | FC_PROG, |
---|
46 | FC_INDEX, |
---|
47 | FC_SIZE, |
---|
48 | FC_SIZE_STR, |
---|
49 | FC_HAVE, |
---|
50 | FC_PRIORITY, |
---|
51 | FC_ENABLED, |
---|
52 | N_FILE_COLS |
---|
53 | }; |
---|
54 | |
---|
55 | typedef struct |
---|
56 | { |
---|
57 | TrCore * core; |
---|
58 | GtkWidget * top; |
---|
59 | GtkWidget * view; |
---|
60 | GtkTreeModel * model; /* same object as store, but recast */ |
---|
61 | GtkTreeStore * store; /* same object as model, but recast */ |
---|
62 | int torrentId; |
---|
63 | guint timeout_tag; |
---|
64 | } |
---|
65 | FileData; |
---|
66 | |
---|
67 | static void |
---|
68 | clearData (FileData * data) |
---|
69 | { |
---|
70 | data->torrentId = -1; |
---|
71 | |
---|
72 | if (data->timeout_tag) { |
---|
73 | g_source_remove (data->timeout_tag); |
---|
74 | data->timeout_tag = 0; |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | static void |
---|
79 | freeData (gpointer data) |
---|
80 | { |
---|
81 | clearData (data); |
---|
82 | g_free (data); |
---|
83 | } |
---|
84 | |
---|
85 | /*** |
---|
86 | **** |
---|
87 | ***/ |
---|
88 | |
---|
89 | struct RefreshData |
---|
90 | { |
---|
91 | int sort_column_id; |
---|
92 | gboolean resort_needed; |
---|
93 | |
---|
94 | tr_file_stat * refresh_file_stat; |
---|
95 | tr_torrent * tor; |
---|
96 | |
---|
97 | FileData * file_data; |
---|
98 | }; |
---|
99 | |
---|
100 | static gboolean |
---|
101 | refreshFilesForeach (GtkTreeModel * model, |
---|
102 | GtkTreePath * path UNUSED, |
---|
103 | GtkTreeIter * iter, |
---|
104 | gpointer gdata) |
---|
105 | { |
---|
106 | struct RefreshData * refresh_data = gdata; |
---|
107 | FileData * data = refresh_data->file_data; |
---|
108 | unsigned int index; |
---|
109 | uint64_t size; |
---|
110 | uint64_t old_have; |
---|
111 | int old_prog; |
---|
112 | int old_priority; |
---|
113 | int old_enabled; |
---|
114 | const gboolean is_file = !gtk_tree_model_iter_has_child (model, iter); |
---|
115 | |
---|
116 | gtk_tree_model_get (model, iter, FC_ENABLED, &old_enabled, |
---|
117 | FC_PRIORITY, &old_priority, |
---|
118 | FC_INDEX, &index, |
---|
119 | FC_HAVE, &old_have, |
---|
120 | FC_SIZE, &size, |
---|
121 | FC_PROG, &old_prog, |
---|
122 | -1); |
---|
123 | |
---|
124 | if (is_file) |
---|
125 | { |
---|
126 | tr_torrent * tor = refresh_data->tor; |
---|
127 | const tr_info * inf = tr_torrentInfo (tor); |
---|
128 | const int enabled = !inf->files[index].dnd; |
---|
129 | const int priority = inf->files[index].priority; |
---|
130 | const uint64_t have = refresh_data->refresh_file_stat[index].bytesCompleted; |
---|
131 | const int prog = size ? (int)((100.0*have)/size) : 1; |
---|
132 | |
---|
133 | if ((priority!=old_priority) || (enabled!=old_enabled) || (have!=old_have) || (prog!=old_prog)) |
---|
134 | { |
---|
135 | /* Changing a value in the sort column can trigger a resort |
---|
136 | * which breaks this foreach () call. (See #3529) |
---|
137 | * As a workaround: if that's about to happen, temporarily disable |
---|
138 | * sorting until we finish walking the tree. */ |
---|
139 | if (!refresh_data->resort_needed) |
---|
140 | { |
---|
141 | if ((refresh_data->resort_needed = |
---|
142 | ((refresh_data->sort_column_id==FC_PRIORITY) && (priority!=old_priority)) || |
---|
143 | ((refresh_data->sort_column_id==FC_ENABLED) && (enabled!=old_enabled)))) |
---|
144 | { |
---|
145 | refresh_data->resort_needed = TRUE; |
---|
146 | gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (data->model), |
---|
147 | GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, |
---|
148 | GTK_SORT_ASCENDING); |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | gtk_tree_store_set (data->store, iter, FC_PRIORITY, priority, |
---|
153 | FC_ENABLED, enabled, |
---|
154 | FC_HAVE, have, |
---|
155 | FC_PROG, prog, |
---|
156 | -1); |
---|
157 | } |
---|
158 | } |
---|
159 | else |
---|
160 | { |
---|
161 | GtkTreeIter child; |
---|
162 | uint64_t sub_size = 0; |
---|
163 | uint64_t have = 0; |
---|
164 | int prog; |
---|
165 | int enabled = NOT_SET; |
---|
166 | int priority = NOT_SET; |
---|
167 | |
---|
168 | /* since gtk_tree_model_foreach () is depth-first, we can |
---|
169 | * get the `sub' info by walking the immediate children */ |
---|
170 | |
---|
171 | if (gtk_tree_model_iter_children (model, &child, iter)) do |
---|
172 | { |
---|
173 | int child_enabled; |
---|
174 | int child_priority; |
---|
175 | int64_t child_have, child_size; |
---|
176 | |
---|
177 | gtk_tree_model_get (model, &child, FC_SIZE, &child_size, |
---|
178 | FC_HAVE, &child_have, |
---|
179 | FC_PRIORITY, &child_priority, |
---|
180 | FC_ENABLED, &child_enabled, |
---|
181 | -1); |
---|
182 | |
---|
183 | sub_size += child_size; |
---|
184 | have += child_have; |
---|
185 | |
---|
186 | if (enabled == NOT_SET) |
---|
187 | enabled = child_enabled; |
---|
188 | else if (enabled != child_enabled) |
---|
189 | enabled = MIXED; |
---|
190 | |
---|
191 | if (priority == NOT_SET) |
---|
192 | priority = child_priority; |
---|
193 | else if (priority != child_priority) |
---|
194 | priority = MIXED; |
---|
195 | } |
---|
196 | while (gtk_tree_model_iter_next (model, &child)); |
---|
197 | |
---|
198 | prog = sub_size ? (int)((100.0*have)/sub_size) : 1; |
---|
199 | |
---|
200 | if ((size!=sub_size) || (have!=old_have) |
---|
201 | || (priority!=old_priority) |
---|
202 | || (enabled!=old_enabled) |
---|
203 | || (prog!=old_prog)) |
---|
204 | { |
---|
205 | char size_str[64]; |
---|
206 | tr_strlsize (size_str, sub_size, sizeof size_str); |
---|
207 | gtk_tree_store_set (data->store, iter, FC_SIZE, sub_size, |
---|
208 | FC_SIZE_STR, size_str, |
---|
209 | FC_HAVE, have, |
---|
210 | FC_PRIORITY, priority, |
---|
211 | FC_ENABLED, enabled, |
---|
212 | FC_PROG, prog, |
---|
213 | -1); |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | return FALSE; /* keep walking */ |
---|
218 | } |
---|
219 | |
---|
220 | static void |
---|
221 | gtr_tree_model_foreach_postorder_subtree (GtkTreeModel * model, |
---|
222 | GtkTreeIter * parent, |
---|
223 | GtkTreeModelForeachFunc func, |
---|
224 | gpointer data) |
---|
225 | { |
---|
226 | GtkTreeIter child; |
---|
227 | if (gtk_tree_model_iter_children (model, &child, parent)) do |
---|
228 | gtr_tree_model_foreach_postorder_subtree (model, &child, func, data); |
---|
229 | while (gtk_tree_model_iter_next (model, &child)); |
---|
230 | if (parent) |
---|
231 | func (model, NULL, parent, data); |
---|
232 | } |
---|
233 | |
---|
234 | static void |
---|
235 | gtr_tree_model_foreach_postorder (GtkTreeModel * model, |
---|
236 | GtkTreeModelForeachFunc func, |
---|
237 | gpointer data) |
---|
238 | { |
---|
239 | GtkTreeIter iter; |
---|
240 | if (gtk_tree_model_iter_nth_child (model, &iter, NULL, 0)) do |
---|
241 | gtr_tree_model_foreach_postorder_subtree (model, &iter, func, data); |
---|
242 | while (gtk_tree_model_iter_next (model, &iter)); |
---|
243 | } |
---|
244 | |
---|
245 | static void |
---|
246 | refresh (FileData * data) |
---|
247 | { |
---|
248 | tr_torrent * tor = gtr_core_find_torrent (data->core, data->torrentId); |
---|
249 | |
---|
250 | if (tor == NULL) |
---|
251 | { |
---|
252 | gtr_file_list_clear (data->top); |
---|
253 | } |
---|
254 | else |
---|
255 | { |
---|
256 | GtkSortType order; |
---|
257 | int sort_column_id; |
---|
258 | tr_file_index_t fileCount; |
---|
259 | struct RefreshData refresh_data; |
---|
260 | GtkTreeSortable * sortable = GTK_TREE_SORTABLE (data->model); |
---|
261 | gtk_tree_sortable_get_sort_column_id (sortable, &sort_column_id, &order); |
---|
262 | |
---|
263 | refresh_data.sort_column_id = sort_column_id; |
---|
264 | refresh_data.resort_needed = FALSE; |
---|
265 | refresh_data.refresh_file_stat = tr_torrentFiles (tor, &fileCount); |
---|
266 | refresh_data.tor = tor; |
---|
267 | refresh_data.file_data = data; |
---|
268 | |
---|
269 | gtr_tree_model_foreach_postorder (data->model, refreshFilesForeach, &refresh_data); |
---|
270 | |
---|
271 | if (refresh_data.resort_needed) |
---|
272 | gtk_tree_sortable_set_sort_column_id (sortable, sort_column_id, order); |
---|
273 | |
---|
274 | tr_torrentFilesFree (refresh_data.refresh_file_stat, fileCount); |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | static gboolean |
---|
279 | refreshModel (gpointer file_data) |
---|
280 | { |
---|
281 | refresh (file_data); |
---|
282 | |
---|
283 | return G_SOURCE_CONTINUE; |
---|
284 | } |
---|
285 | |
---|
286 | /*** |
---|
287 | **** |
---|
288 | ***/ |
---|
289 | |
---|
290 | struct ActiveData |
---|
291 | { |
---|
292 | GtkTreeSelection * sel; |
---|
293 | GArray * array; |
---|
294 | }; |
---|
295 | |
---|
296 | static gboolean |
---|
297 | getSelectedFilesForeach (GtkTreeModel * model, |
---|
298 | GtkTreePath * path UNUSED, |
---|
299 | GtkTreeIter * iter, |
---|
300 | gpointer gdata) |
---|
301 | { |
---|
302 | const gboolean is_file = !gtk_tree_model_iter_has_child (model, iter); |
---|
303 | |
---|
304 | if (is_file) |
---|
305 | { |
---|
306 | struct ActiveData * data = gdata; |
---|
307 | |
---|
308 | /* active means: if it's selected or any ancestor is selected */ |
---|
309 | |
---|
310 | gboolean is_active = gtk_tree_selection_iter_is_selected (data->sel, iter); |
---|
311 | |
---|
312 | if (!is_active) |
---|
313 | { |
---|
314 | GtkTreeIter walk = *iter; |
---|
315 | GtkTreeIter parent; |
---|
316 | while (!is_active && gtk_tree_model_iter_parent (model, &parent, &walk)) |
---|
317 | { |
---|
318 | is_active = gtk_tree_selection_iter_is_selected (data->sel, &parent); |
---|
319 | walk = parent; |
---|
320 | } |
---|
321 | } |
---|
322 | |
---|
323 | if (is_active) |
---|
324 | { |
---|
325 | unsigned int i; |
---|
326 | gtk_tree_model_get (model, iter, FC_INDEX, &i, -1); |
---|
327 | g_array_append_val (data->array, i); |
---|
328 | } |
---|
329 | } |
---|
330 | |
---|
331 | return FALSE; /* keep walking */ |
---|
332 | } |
---|
333 | |
---|
334 | static GArray* |
---|
335 | getSelectedFilesAndDescendants (GtkTreeView * view) |
---|
336 | { |
---|
337 | struct ActiveData data; |
---|
338 | |
---|
339 | data.sel = gtk_tree_view_get_selection (view); |
---|
340 | data.array = g_array_new (FALSE, FALSE, sizeof (tr_file_index_t)); |
---|
341 | gtk_tree_model_foreach (gtk_tree_view_get_model (view), |
---|
342 | getSelectedFilesForeach, &data); |
---|
343 | return data.array; |
---|
344 | } |
---|
345 | |
---|
346 | struct SubtreeForeachData |
---|
347 | { |
---|
348 | GArray * array; |
---|
349 | GtkTreePath * path; |
---|
350 | }; |
---|
351 | |
---|
352 | static gboolean |
---|
353 | getSubtreeForeach (GtkTreeModel * model, |
---|
354 | GtkTreePath * path, |
---|
355 | GtkTreeIter * iter, |
---|
356 | gpointer gdata) |
---|
357 | { |
---|
358 | const gboolean is_file = !gtk_tree_model_iter_has_child (model, iter); |
---|
359 | |
---|
360 | if (is_file) |
---|
361 | { |
---|
362 | struct SubtreeForeachData * data = gdata; |
---|
363 | |
---|
364 | if (!gtk_tree_path_compare (path, data->path) || gtk_tree_path_is_descendant (path, data->path)) |
---|
365 | { |
---|
366 | unsigned int i; |
---|
367 | gtk_tree_model_get (model, iter, FC_INDEX, &i, -1); |
---|
368 | g_array_append_val (data->array, i); |
---|
369 | } |
---|
370 | } |
---|
371 | |
---|
372 | return FALSE; /* keep walking */ |
---|
373 | } |
---|
374 | |
---|
375 | static void |
---|
376 | getSubtree (GtkTreeView * view, GtkTreePath * path, GArray * indices) |
---|
377 | { |
---|
378 | struct SubtreeForeachData tmp; |
---|
379 | tmp.array = indices; |
---|
380 | tmp.path = path; |
---|
381 | gtk_tree_model_foreach (gtk_tree_view_get_model (view), getSubtreeForeach, &tmp); |
---|
382 | } |
---|
383 | |
---|
384 | /* if `path' is a selected row, all selected rows are returned. |
---|
385 | * otherwise, only the row indicated by `path' is returned. |
---|
386 | * this is for toggling all the selected rows' states in a batch. |
---|
387 | */ |
---|
388 | static GArray* |
---|
389 | getActiveFilesForPath (GtkTreeView * view, GtkTreePath * path) |
---|
390 | { |
---|
391 | GArray * indices; |
---|
392 | GtkTreeSelection * sel = gtk_tree_view_get_selection (view); |
---|
393 | |
---|
394 | if (gtk_tree_selection_path_is_selected (sel, path)) |
---|
395 | { |
---|
396 | /* clicked in a selected row... use the current selection */ |
---|
397 | indices = getSelectedFilesAndDescendants (view); |
---|
398 | } |
---|
399 | else |
---|
400 | { |
---|
401 | /* clicked OUTSIDE of the selected row... just use the clicked row */ |
---|
402 | indices = g_array_new (FALSE, FALSE, sizeof (tr_file_index_t)); |
---|
403 | getSubtree (view, path, indices); |
---|
404 | } |
---|
405 | |
---|
406 | return indices; |
---|
407 | } |
---|
408 | |
---|
409 | /*** |
---|
410 | **** |
---|
411 | ***/ |
---|
412 | |
---|
413 | void |
---|
414 | gtr_file_list_clear (GtkWidget * w) |
---|
415 | { |
---|
416 | gtr_file_list_set_torrent (w, -1); |
---|
417 | } |
---|
418 | |
---|
419 | struct build_data |
---|
420 | { |
---|
421 | GtkWidget * w; |
---|
422 | tr_torrent * tor; |
---|
423 | GtkTreeIter * iter; |
---|
424 | GtkTreeStore * store; |
---|
425 | }; |
---|
426 | |
---|
427 | struct row_struct |
---|
428 | { |
---|
429 | uint64_t length; |
---|
430 | char * name; |
---|
431 | int index; |
---|
432 | }; |
---|
433 | |
---|
434 | static void |
---|
435 | buildTree (GNode * node, gpointer gdata) |
---|
436 | { |
---|
437 | char size_str[64]; |
---|
438 | GtkTreeIter child_iter; |
---|
439 | struct build_data * build = gdata; |
---|
440 | struct row_struct *child_data = node->data; |
---|
441 | const gboolean isLeaf = node->children == NULL; |
---|
442 | |
---|
443 | const char * mime_type = isLeaf ? gtr_get_mime_type_from_filename (child_data->name) : DIRECTORY_MIME_TYPE; |
---|
444 | GdkPixbuf * icon = gtr_get_mime_type_icon (mime_type, GTK_ICON_SIZE_MENU, build->w); |
---|
445 | const tr_info * inf = tr_torrentInfo (build->tor); |
---|
446 | const int priority = isLeaf ? inf->files[ child_data->index ].priority : 0; |
---|
447 | const gboolean enabled = isLeaf ? !inf->files[ child_data->index ].dnd : TRUE; |
---|
448 | char * name_esc = g_markup_escape_text (child_data->name, -1); |
---|
449 | |
---|
450 | tr_strlsize (size_str, child_data->length, sizeof size_str); |
---|
451 | |
---|
452 | gtk_tree_store_insert_with_values (build->store, &child_iter, build->iter, INT_MAX, |
---|
453 | FC_INDEX, child_data->index, |
---|
454 | FC_LABEL, child_data->name, |
---|
455 | FC_LABEL_ESC, name_esc, |
---|
456 | FC_SIZE, child_data->length, |
---|
457 | FC_SIZE_STR, size_str, |
---|
458 | FC_ICON, icon, |
---|
459 | FC_PRIORITY, priority, |
---|
460 | FC_ENABLED, enabled, |
---|
461 | -1); |
---|
462 | |
---|
463 | if (!isLeaf) |
---|
464 | { |
---|
465 | struct build_data b = *build; |
---|
466 | b.iter = &child_iter; |
---|
467 | g_node_children_foreach (node, G_TRAVERSE_ALL, buildTree, &b); |
---|
468 | } |
---|
469 | |
---|
470 | g_free (name_esc); |
---|
471 | g_object_unref (icon); |
---|
472 | |
---|
473 | /* we're done with this node */ |
---|
474 | g_free (child_data->name); |
---|
475 | g_free (child_data); |
---|
476 | } |
---|
477 | |
---|
478 | static GNode* |
---|
479 | find_child (GNode* parent, const char * name) |
---|
480 | { |
---|
481 | GNode * child = parent->children; |
---|
482 | while (child) { |
---|
483 | const struct row_struct * child_data = child->data; |
---|
484 | if ((*child_data->name == *name) && !strcmp (child_data->name, name)) |
---|
485 | break; |
---|
486 | child = child->next; |
---|
487 | } |
---|
488 | return child; |
---|
489 | } |
---|
490 | |
---|
491 | void |
---|
492 | gtr_file_list_set_torrent (GtkWidget * w, int torrentId) |
---|
493 | { |
---|
494 | GtkTreeStore * store; |
---|
495 | FileData * data = g_object_get_data (G_OBJECT (w), "file-data"); |
---|
496 | |
---|
497 | /* unset the old fields */ |
---|
498 | clearData (data); |
---|
499 | |
---|
500 | /* instantiate the model */ |
---|
501 | store = gtk_tree_store_new (N_FILE_COLS, |
---|
502 | GDK_TYPE_PIXBUF, /* icon */ |
---|
503 | G_TYPE_STRING, /* label */ |
---|
504 | G_TYPE_STRING, /* label esc */ |
---|
505 | G_TYPE_INT, /* prog [0..100] */ |
---|
506 | G_TYPE_UINT, /* index */ |
---|
507 | G_TYPE_UINT64, /* size */ |
---|
508 | G_TYPE_STRING, /* size str */ |
---|
509 | G_TYPE_UINT64, /* have */ |
---|
510 | G_TYPE_INT, /* priority */ |
---|
511 | G_TYPE_INT); /* dl enabled */ |
---|
512 | |
---|
513 | data->store = store; |
---|
514 | data->model = GTK_TREE_MODEL (store); |
---|
515 | data->torrentId = torrentId; |
---|
516 | |
---|
517 | /* populate the model */ |
---|
518 | if (torrentId > 0) |
---|
519 | { |
---|
520 | tr_torrent * tor = gtr_core_find_torrent (data->core, torrentId); |
---|
521 | if (tor != NULL) |
---|
522 | { |
---|
523 | tr_file_index_t i; |
---|
524 | const tr_info * inf = tr_torrentInfo (tor); |
---|
525 | struct row_struct * root_data; |
---|
526 | GNode * root; |
---|
527 | struct build_data build; |
---|
528 | |
---|
529 | /* build a GNode tree of the files */ |
---|
530 | root_data = g_new0 (struct row_struct, 1); |
---|
531 | root_data->name = g_strdup (tr_torrentName (tor)); |
---|
532 | root_data->index = -1; |
---|
533 | root_data->length = 0; |
---|
534 | root = g_node_new (root_data); |
---|
535 | for (i=0; i<inf->fileCount; ++i) { |
---|
536 | int j; |
---|
537 | GNode * parent = root; |
---|
538 | const tr_file * file = &inf->files[i]; |
---|
539 | char ** tokens = g_strsplit (file->name, G_DIR_SEPARATOR_S, 0); |
---|
540 | for (j=0; tokens[j]; ++j) { |
---|
541 | const gboolean isLeaf = tokens[j+1] == NULL; |
---|
542 | const char * name = tokens[j]; |
---|
543 | GNode * node = find_child (parent, name); |
---|
544 | if (node == NULL) { |
---|
545 | struct row_struct * row = g_new (struct row_struct, 1); |
---|
546 | row->name = g_strdup (name); |
---|
547 | row->index = isLeaf ? (int)i : -1; |
---|
548 | row->length = isLeaf ? file->length : 0; |
---|
549 | node = g_node_new (row); |
---|
550 | g_node_append (parent, node); |
---|
551 | } |
---|
552 | parent = node; |
---|
553 | } |
---|
554 | g_strfreev (tokens); |
---|
555 | } |
---|
556 | |
---|
557 | /* now, add them to the model */ |
---|
558 | build.w = w; |
---|
559 | build.tor = tor; |
---|
560 | build.store = data->store; |
---|
561 | build.iter = NULL; |
---|
562 | g_node_children_foreach (root, G_TRAVERSE_ALL, buildTree, &build); |
---|
563 | |
---|
564 | /* cleanup */ |
---|
565 | g_node_destroy (root); |
---|
566 | g_free (root_data->name); |
---|
567 | g_free (root_data); |
---|
568 | } |
---|
569 | |
---|
570 | refresh (data); |
---|
571 | data->timeout_tag = gdk_threads_add_timeout_seconds (SECONDARY_WINDOW_REFRESH_INTERVAL_SECONDS, refreshModel, data); |
---|
572 | } |
---|
573 | |
---|
574 | gtk_tree_view_set_model (GTK_TREE_VIEW (data->view), data->model); |
---|
575 | gtk_tree_view_expand_all (GTK_TREE_VIEW (data->view)); |
---|
576 | g_object_unref (data->model); |
---|
577 | } |
---|
578 | |
---|
579 | /*** |
---|
580 | **** |
---|
581 | ***/ |
---|
582 | |
---|
583 | static void |
---|
584 | renderDownload (GtkTreeViewColumn * column UNUSED, |
---|
585 | GtkCellRenderer * renderer, |
---|
586 | GtkTreeModel * model, |
---|
587 | GtkTreeIter * iter, |
---|
588 | gpointer data UNUSED) |
---|
589 | { |
---|
590 | gboolean enabled; |
---|
591 | gtk_tree_model_get (model, iter, FC_ENABLED, &enabled, -1); |
---|
592 | g_object_set (renderer, "inconsistent", (enabled==MIXED), |
---|
593 | "active", (enabled==TRUE), |
---|
594 | NULL); |
---|
595 | } |
---|
596 | |
---|
597 | static void |
---|
598 | renderPriority (GtkTreeViewColumn * column UNUSED, |
---|
599 | GtkCellRenderer * renderer, |
---|
600 | GtkTreeModel * model, |
---|
601 | GtkTreeIter * iter, |
---|
602 | gpointer data UNUSED) |
---|
603 | { |
---|
604 | int priority; |
---|
605 | const char * text; |
---|
606 | gtk_tree_model_get (model, iter, FC_PRIORITY, &priority, -1); |
---|
607 | switch (priority) { |
---|
608 | case TR_PRI_HIGH: text = _("High"); break; |
---|
609 | case TR_PRI_NORMAL: text = _("Normal"); break; |
---|
610 | case TR_PRI_LOW: text = _("Low"); break; |
---|
611 | default: text = _("Mixed"); break; |
---|
612 | } |
---|
613 | g_object_set (renderer, "text", text, NULL); |
---|
614 | } |
---|
615 | |
---|
616 | /* build a filename from tr_torrentGetCurrentDir () + the model's FC_LABELs */ |
---|
617 | static char* |
---|
618 | buildFilename (tr_torrent * tor, GtkTreeModel * model, |
---|
619 | GtkTreePath * path, GtkTreeIter * iter) |
---|
620 | { |
---|
621 | char * ret; |
---|
622 | GtkTreeIter child; |
---|
623 | GtkTreeIter parent = *iter; |
---|
624 | int n = gtk_tree_path_get_depth (path); |
---|
625 | char ** tokens = g_new0 (char*, n + 2); |
---|
626 | tokens[0] = g_strdup (tr_torrentGetCurrentDir (tor)); |
---|
627 | do { |
---|
628 | child = parent; |
---|
629 | gtk_tree_model_get (model, &child, FC_LABEL, &tokens[n--], -1); |
---|
630 | } while (gtk_tree_model_iter_parent (model, &parent, &child)); |
---|
631 | ret = g_build_filenamev (tokens); |
---|
632 | g_strfreev (tokens); |
---|
633 | return ret; |
---|
634 | } |
---|
635 | |
---|
636 | static gboolean |
---|
637 | onRowActivated (GtkTreeView * view, GtkTreePath * path, |
---|
638 | GtkTreeViewColumn * col UNUSED, gpointer gdata) |
---|
639 | { |
---|
640 | gboolean handled = FALSE; |
---|
641 | FileData * data = gdata; |
---|
642 | tr_torrent * tor = gtr_core_find_torrent (data->core, data->torrentId); |
---|
643 | |
---|
644 | if (tor != NULL) |
---|
645 | { |
---|
646 | GtkTreeIter iter; |
---|
647 | GtkTreeModel * model = gtk_tree_view_get_model (view); |
---|
648 | |
---|
649 | if (gtk_tree_model_get_iter (model, &iter, path)) |
---|
650 | { |
---|
651 | int prog; |
---|
652 | char * filename = buildFilename (tor, model, path, &iter); |
---|
653 | gtk_tree_model_get (model, &iter, FC_PROG, &prog, -1); |
---|
654 | |
---|
655 | /* if the file's not done, walk up the directory tree until we find |
---|
656 | * an ancestor that exists, and open that instead */ |
---|
657 | if (filename && (prog<100 || !g_file_test (filename, G_FILE_TEST_EXISTS))) do |
---|
658 | { |
---|
659 | char * tmp = g_path_get_dirname (filename); |
---|
660 | g_free (filename); |
---|
661 | filename = tmp; |
---|
662 | } |
---|
663 | while (filename && *filename && !g_file_test (filename, G_FILE_TEST_EXISTS)); |
---|
664 | |
---|
665 | if ((handled = filename && *filename)) |
---|
666 | gtr_open_file (filename); |
---|
667 | } |
---|
668 | } |
---|
669 | |
---|
670 | return handled; |
---|
671 | } |
---|
672 | |
---|
673 | static gboolean |
---|
674 | onViewPathToggled (GtkTreeView * view, |
---|
675 | GtkTreeViewColumn * col, |
---|
676 | GtkTreePath * path, |
---|
677 | FileData * data) |
---|
678 | { |
---|
679 | int cid; |
---|
680 | tr_torrent * tor; |
---|
681 | gboolean handled = FALSE; |
---|
682 | |
---|
683 | if (!col || !path) |
---|
684 | return FALSE; |
---|
685 | |
---|
686 | cid = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (col), TR_COLUMN_ID_KEY)); |
---|
687 | tor = gtr_core_find_torrent (data->core, data->torrentId); |
---|
688 | if ((tor != NULL) && ((cid == FC_PRIORITY) || (cid == FC_ENABLED))) |
---|
689 | { |
---|
690 | GtkTreeIter iter; |
---|
691 | GArray * indices = getActiveFilesForPath (view, path); |
---|
692 | GtkTreeModel * model = data->model; |
---|
693 | |
---|
694 | gtk_tree_model_get_iter (model, &iter, path); |
---|
695 | |
---|
696 | if (cid == FC_PRIORITY) |
---|
697 | { |
---|
698 | int priority; |
---|
699 | gtk_tree_model_get (model, &iter, FC_PRIORITY, &priority, -1); |
---|
700 | switch (priority) { |
---|
701 | case TR_PRI_NORMAL: priority = TR_PRI_HIGH; break; |
---|
702 | case TR_PRI_HIGH: priority = TR_PRI_LOW; break; |
---|
703 | default: priority = TR_PRI_NORMAL; break; |
---|
704 | } |
---|
705 | tr_torrentSetFilePriorities (tor, |
---|
706 | (tr_file_index_t *) indices->data, |
---|
707 | (tr_file_index_t) indices->len, |
---|
708 | priority); |
---|
709 | } |
---|
710 | else |
---|
711 | { |
---|
712 | int enabled; |
---|
713 | gtk_tree_model_get (model, &iter, FC_ENABLED, &enabled, -1); |
---|
714 | enabled = !enabled; |
---|
715 | |
---|
716 | tr_torrentSetFileDLs (tor, |
---|
717 | (tr_file_index_t *) indices->data, |
---|
718 | (tr_file_index_t) indices->len, |
---|
719 | enabled); |
---|
720 | } |
---|
721 | |
---|
722 | refresh (data); |
---|
723 | g_array_free (indices, TRUE); |
---|
724 | handled = TRUE; |
---|
725 | } |
---|
726 | |
---|
727 | return handled; |
---|
728 | } |
---|
729 | |
---|
730 | /** |
---|
731 | * @note 'col' and 'path' are assumed not to be NULL. |
---|
732 | */ |
---|
733 | static gboolean |
---|
734 | getAndSelectEventPath (GtkTreeView * treeview, |
---|
735 | GdkEventButton * event, |
---|
736 | GtkTreeViewColumn ** col, |
---|
737 | GtkTreePath ** path) |
---|
738 | { |
---|
739 | GtkTreeSelection * sel; |
---|
740 | |
---|
741 | if (gtk_tree_view_get_path_at_pos (treeview, |
---|
742 | event->x, event->y, |
---|
743 | path, col, NULL, NULL)) |
---|
744 | { |
---|
745 | sel = gtk_tree_view_get_selection (treeview); |
---|
746 | if (!gtk_tree_selection_path_is_selected (sel, *path)) |
---|
747 | { |
---|
748 | gtk_tree_selection_unselect_all (sel); |
---|
749 | gtk_tree_selection_select_path (sel, *path); |
---|
750 | } |
---|
751 | return TRUE; |
---|
752 | } |
---|
753 | |
---|
754 | return FALSE; |
---|
755 | } |
---|
756 | |
---|
757 | static gboolean |
---|
758 | onViewButtonPressed (GtkWidget * w, GdkEventButton * event, gpointer gdata) |
---|
759 | { |
---|
760 | GtkTreeViewColumn * col; |
---|
761 | GtkTreePath * path = NULL; |
---|
762 | gboolean handled = FALSE; |
---|
763 | GtkTreeView * treeview = GTK_TREE_VIEW (w); |
---|
764 | FileData * data = gdata; |
---|
765 | |
---|
766 | if ((event->type == GDK_BUTTON_PRESS) |
---|
767 | && (event->button == 1) |
---|
768 | && ! (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) |
---|
769 | && getAndSelectEventPath (treeview, event, &col, &path)) |
---|
770 | { |
---|
771 | handled = onViewPathToggled (treeview, col, path, data); |
---|
772 | |
---|
773 | if (path != NULL) |
---|
774 | gtk_tree_path_free (path); |
---|
775 | } |
---|
776 | |
---|
777 | return handled; |
---|
778 | } |
---|
779 | |
---|
780 | struct rename_data |
---|
781 | { |
---|
782 | int error; |
---|
783 | char * newname; |
---|
784 | char * path_string; |
---|
785 | FileData * file_data; |
---|
786 | }; |
---|
787 | |
---|
788 | static int |
---|
789 | on_rename_done_idle (struct rename_data * data) |
---|
790 | { |
---|
791 | if (data->error == 0) |
---|
792 | { |
---|
793 | GtkTreeIter iter; |
---|
794 | |
---|
795 | if (gtk_tree_model_get_iter_from_string (data->file_data->model, &iter, data->path_string)) |
---|
796 | gtk_tree_store_set (data->file_data->store, &iter, FC_LABEL, data->newname, -1); |
---|
797 | } |
---|
798 | else |
---|
799 | { |
---|
800 | GtkWidget * w = gtk_message_dialog_new ( |
---|
801 | GTK_WINDOW (gtk_widget_get_toplevel(data->file_data->top)), |
---|
802 | GTK_DIALOG_MODAL, |
---|
803 | GTK_MESSAGE_ERROR, |
---|
804 | GTK_BUTTONS_CLOSE, |
---|
805 | _("Unable to rename file as \"%s\": %s"), |
---|
806 | data->newname, |
---|
807 | tr_strerror(data->error)); |
---|
808 | gtk_message_dialog_format_secondary_text ( |
---|
809 | GTK_MESSAGE_DIALOG (w), "%s", |
---|
810 | _("Please correct the errors and try again.")); |
---|
811 | gtk_dialog_run (GTK_DIALOG (w)); |
---|
812 | gtk_widget_destroy (w); |
---|
813 | } |
---|
814 | |
---|
815 | /* cleanup */ |
---|
816 | g_free (data->path_string); |
---|
817 | g_free (data->newname); |
---|
818 | g_free (data); |
---|
819 | return G_SOURCE_REMOVE; |
---|
820 | } |
---|
821 | |
---|
822 | static void |
---|
823 | on_rename_done (tr_torrent * tor G_GNUC_UNUSED, |
---|
824 | const char * oldpath G_GNUC_UNUSED, |
---|
825 | const char * newname G_GNUC_UNUSED, |
---|
826 | int error, |
---|
827 | struct rename_data * rename_data) |
---|
828 | { |
---|
829 | rename_data->error = error; |
---|
830 | gdk_threads_add_idle ((GSourceFunc)on_rename_done_idle, rename_data); |
---|
831 | } |
---|
832 | |
---|
833 | static void |
---|
834 | cell_edited_callback (GtkCellRendererText * cell G_GNUC_UNUSED, |
---|
835 | gchar * path_string, |
---|
836 | gchar * newname, |
---|
837 | FileData * data) |
---|
838 | { |
---|
839 | tr_torrent * tor; |
---|
840 | GString * oldpath; |
---|
841 | GtkTreeIter iter; |
---|
842 | struct rename_data * rename_data; |
---|
843 | |
---|
844 | tor = gtr_core_find_torrent (data->core, data->torrentId); |
---|
845 | if (tor == NULL) |
---|
846 | return; |
---|
847 | if (!gtk_tree_model_get_iter_from_string (data->model, &iter, path_string)) |
---|
848 | return; |
---|
849 | |
---|
850 | /* build oldpath */ |
---|
851 | oldpath = g_string_new (NULL); |
---|
852 | for (;;) |
---|
853 | { |
---|
854 | char * token = NULL; |
---|
855 | GtkTreeIter child; |
---|
856 | gtk_tree_model_get (data->model, &iter, FC_LABEL, &token, -1); |
---|
857 | g_string_prepend (oldpath, token); |
---|
858 | g_free (token); |
---|
859 | |
---|
860 | child = iter; |
---|
861 | if (!gtk_tree_model_iter_parent (data->model, &iter, &child)) |
---|
862 | break; |
---|
863 | |
---|
864 | g_string_prepend_c (oldpath, G_DIR_SEPARATOR); |
---|
865 | } |
---|
866 | |
---|
867 | /* do the renaming */ |
---|
868 | rename_data = g_new0 (struct rename_data, 1); |
---|
869 | rename_data->newname = g_strdup (newname); |
---|
870 | rename_data->file_data = data; |
---|
871 | rename_data->path_string = g_strdup (path_string); |
---|
872 | tr_torrentRenamePath (tor, oldpath->str, newname, (tr_torrent_rename_done_func*)on_rename_done, rename_data); |
---|
873 | |
---|
874 | /* cleanup */ |
---|
875 | g_string_free (oldpath, TRUE); |
---|
876 | } |
---|
877 | |
---|
878 | |
---|
879 | GtkWidget * |
---|
880 | gtr_file_list_new (TrCore * core, int torrentId) |
---|
881 | { |
---|
882 | int size; |
---|
883 | int width; |
---|
884 | GtkWidget * ret; |
---|
885 | GtkWidget * view; |
---|
886 | GtkWidget * scroll; |
---|
887 | GtkCellRenderer * rend; |
---|
888 | GtkTreeSelection * sel; |
---|
889 | GtkTreeViewColumn * col; |
---|
890 | GtkTreeView * tree_view; |
---|
891 | const char * title; |
---|
892 | PangoLayout * pango_layout; |
---|
893 | PangoContext * pango_context; |
---|
894 | PangoFontDescription * pango_font_description; |
---|
895 | FileData * data = g_new0 (FileData, 1); |
---|
896 | |
---|
897 | data->core = core; |
---|
898 | |
---|
899 | /* create the view */ |
---|
900 | view = gtk_tree_view_new (); |
---|
901 | tree_view = GTK_TREE_VIEW (view); |
---|
902 | gtk_tree_view_set_rules_hint (tree_view, TRUE); |
---|
903 | gtk_container_set_border_width (GTK_CONTAINER (view), GUI_PAD_BIG); |
---|
904 | g_signal_connect (view, "button-press-event", |
---|
905 | G_CALLBACK (onViewButtonPressed), data); |
---|
906 | g_signal_connect (view, "row_activated", |
---|
907 | G_CALLBACK (onRowActivated), data); |
---|
908 | g_signal_connect (view, "button-release-event", |
---|
909 | G_CALLBACK (on_tree_view_button_released), NULL); |
---|
910 | |
---|
911 | |
---|
912 | pango_context = gtk_widget_create_pango_context (view); |
---|
913 | pango_font_description = pango_font_description_copy (pango_context_get_font_description (pango_context)); |
---|
914 | size = pango_font_description_get_size (pango_font_description); |
---|
915 | pango_font_description_set_size (pango_font_description, size * 0.8); |
---|
916 | g_object_unref (G_OBJECT (pango_context)); |
---|
917 | |
---|
918 | /* set up view */ |
---|
919 | sel = gtk_tree_view_get_selection (tree_view); |
---|
920 | gtk_tree_selection_set_mode (sel, GTK_SELECTION_MULTIPLE); |
---|
921 | gtk_tree_view_expand_all (tree_view); |
---|
922 | gtk_tree_view_set_search_column (tree_view, FC_LABEL); |
---|
923 | |
---|
924 | /* add file column */ |
---|
925 | col = GTK_TREE_VIEW_COLUMN (g_object_new (GTK_TYPE_TREE_VIEW_COLUMN, |
---|
926 | "expand", TRUE, |
---|
927 | "title", _("Name"), |
---|
928 | NULL)); |
---|
929 | gtk_tree_view_column_set_resizable (col, TRUE); |
---|
930 | rend = gtk_cell_renderer_pixbuf_new (); |
---|
931 | gtk_tree_view_column_pack_start (col, rend, FALSE); |
---|
932 | gtk_tree_view_column_add_attribute (col, rend, "pixbuf", FC_ICON); |
---|
933 | /* add text renderer */ |
---|
934 | rend = gtk_cell_renderer_text_new (); |
---|
935 | g_object_set (rend, "editable", TRUE, NULL); |
---|
936 | g_object_set (rend, "ellipsize", PANGO_ELLIPSIZE_END, "font-desc", pango_font_description, NULL); |
---|
937 | g_signal_connect (rend, "edited", (GCallback)cell_edited_callback, data); |
---|
938 | gtk_tree_view_column_pack_start (col, rend, TRUE); |
---|
939 | gtk_tree_view_column_set_attributes (col, rend, "text", FC_LABEL, NULL); |
---|
940 | gtk_tree_view_column_set_sort_column_id (col, FC_LABEL); |
---|
941 | gtk_tree_view_append_column (tree_view, col); |
---|
942 | |
---|
943 | /* add "size" column */ |
---|
944 | title = _("Size"); |
---|
945 | rend = gtk_cell_renderer_text_new (); |
---|
946 | g_object_set (rend, "alignment", PANGO_ALIGN_RIGHT, |
---|
947 | "font-desc", pango_font_description, |
---|
948 | "xpad", GUI_PAD, |
---|
949 | "xalign", 1.0f, |
---|
950 | "yalign", 0.5f, |
---|
951 | NULL); |
---|
952 | col = gtk_tree_view_column_new_with_attributes (title, rend, NULL); |
---|
953 | gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_GROW_ONLY); |
---|
954 | gtk_tree_view_column_set_sort_column_id (col, FC_SIZE); |
---|
955 | gtk_tree_view_column_set_attributes (col, rend, "text", FC_SIZE_STR, NULL); |
---|
956 | gtk_tree_view_append_column (tree_view, col); |
---|
957 | |
---|
958 | /* add "progress" column */ |
---|
959 | title = _("Have"); |
---|
960 | pango_layout = gtk_widget_create_pango_layout (view, title); |
---|
961 | pango_layout_get_pixel_size (pango_layout, &width, NULL); |
---|
962 | width += 30; /* room for the sort indicator */ |
---|
963 | g_object_unref (G_OBJECT (pango_layout)); |
---|
964 | rend = gtk_cell_renderer_progress_new (); |
---|
965 | col = gtk_tree_view_column_new_with_attributes (title, rend, "value", FC_PROG, NULL); |
---|
966 | gtk_tree_view_column_set_fixed_width (col, width); |
---|
967 | gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED); |
---|
968 | gtk_tree_view_column_set_sort_column_id (col, FC_PROG); |
---|
969 | gtk_tree_view_append_column (tree_view, col); |
---|
970 | |
---|
971 | /* add "enabled" column */ |
---|
972 | title = _("Download"); |
---|
973 | pango_layout = gtk_widget_create_pango_layout (view, title); |
---|
974 | pango_layout_get_pixel_size (pango_layout, &width, NULL); |
---|
975 | width += 30; /* room for the sort indicator */ |
---|
976 | g_object_unref (G_OBJECT (pango_layout)); |
---|
977 | rend = gtk_cell_renderer_toggle_new (); |
---|
978 | col = gtk_tree_view_column_new_with_attributes (title, rend, NULL); |
---|
979 | g_object_set_data (G_OBJECT (col), TR_COLUMN_ID_KEY, |
---|
980 | GINT_TO_POINTER (FC_ENABLED)); |
---|
981 | gtk_tree_view_column_set_fixed_width (col, width); |
---|
982 | gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED); |
---|
983 | gtk_tree_view_column_set_cell_data_func (col, rend, renderDownload, NULL, NULL); |
---|
984 | gtk_tree_view_column_set_sort_column_id (col, FC_ENABLED); |
---|
985 | gtk_tree_view_append_column (tree_view, col); |
---|
986 | |
---|
987 | /* add priority column */ |
---|
988 | title = _("Priority"); |
---|
989 | pango_layout = gtk_widget_create_pango_layout (view, title); |
---|
990 | pango_layout_get_pixel_size (pango_layout, &width, NULL); |
---|
991 | width += 30; /* room for the sort indicator */ |
---|
992 | g_object_unref (G_OBJECT (pango_layout)); |
---|
993 | rend = gtk_cell_renderer_text_new (); |
---|
994 | g_object_set (rend, "xalign", (gfloat)0.5, "yalign", (gfloat)0.5, NULL); |
---|
995 | col = gtk_tree_view_column_new_with_attributes (title, rend, NULL); |
---|
996 | g_object_set_data (G_OBJECT (col), TR_COLUMN_ID_KEY, |
---|
997 | GINT_TO_POINTER (FC_PRIORITY)); |
---|
998 | gtk_tree_view_column_set_fixed_width (col, width); |
---|
999 | gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED); |
---|
1000 | gtk_tree_view_column_set_sort_column_id (col, FC_PRIORITY); |
---|
1001 | gtk_tree_view_column_set_cell_data_func (col, rend, renderPriority, NULL, NULL); |
---|
1002 | gtk_tree_view_append_column (tree_view, col); |
---|
1003 | |
---|
1004 | /* add tooltip to tree */ |
---|
1005 | gtk_tree_view_set_tooltip_column (tree_view, FC_LABEL_ESC); |
---|
1006 | |
---|
1007 | /* create the scrolled window and stick the view in it */ |
---|
1008 | scroll = gtk_scrolled_window_new (NULL, NULL); |
---|
1009 | gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll), |
---|
1010 | GTK_POLICY_AUTOMATIC, |
---|
1011 | GTK_POLICY_AUTOMATIC); |
---|
1012 | gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scroll), |
---|
1013 | GTK_SHADOW_IN); |
---|
1014 | gtk_container_add (GTK_CONTAINER (scroll), view); |
---|
1015 | gtk_widget_set_size_request (scroll, -1, 200); |
---|
1016 | |
---|
1017 | ret = scroll; |
---|
1018 | data->view = view; |
---|
1019 | data->top = scroll; |
---|
1020 | g_object_set_data_full (G_OBJECT (ret), "file-data", data, freeData); |
---|
1021 | gtr_file_list_set_torrent (ret, torrentId); |
---|
1022 | |
---|
1023 | pango_font_description_free (pango_font_description); |
---|
1024 | return ret; |
---|
1025 | } |
---|